// Core scripts for TSFS-Calculator
// Copyright (c) Student Financial Assistance Agency, HKSARG
// ************************************************************
// Amendment notes start from 27-MAY-2011
// Jeff LAI AP(ITMU)6       27-MAY-2001             Assistance tier adjustment


////Institution name
////var arrInst = new Array("The University of Hong Kong", "The Chinese University of Hong Kong", "The Hong Kong Polytechnic University", "Hong Kong Baptist University", "City University of Hong Kong", "Hong Kong University of Science & Technology", "Lingnan University", "Hong Kong Institute of Vocational Education (Chai Wai)", "Hong Kong Institute of Vocational Education (Haking  Wong)", "The Hong Kong Institute of Education (Lee Wai Lee)", "The Hong Kong Institute of Education (Shatin)", "The Hong Kong Institute of Education (Kwai Chung)", "The Hong Kong Institute of Education (Kwun Tong)", "The Hong Kong Institute of Education (Tsing Yi)", "The Hong Kong Institute of Education (Morrison Hill)", "The Hong Kong Institute of Education (Tuen Mun)", "The Prince Philip Dental Hospital", "Hong Kong Institute of Education", "The Hong Kong Academy for Performing Arts");
//
//var arrInst_Dis = new Array("--Select an institution--", "", "The University of Hong Kong", "arrAENormalDis", "The Chinese University of Hong Kong", "arrAENormalDis", "The Hong Kong Polytechnic University", "arrAENormalDis", "Hong Kong Baptist University", "arrAENormalDis", "City University of Hong Kong", "arrAENormalDis", "Hong Kong University of Science & Technology", "arrAENormalDis", "Lingnan University", "arrAENormalDis", "Hong Kong Institute of Vocational Education (Chai Wai)", "arrAENormalDis", "Hong Kong Institute of Vocational Education (Haking  Wong)", "arrAENormalDis", "The Hong Kong Institute of Education (Lee Wai Lee)", "arrAENormalDis", "The Hong Kong Institute of Education (Shatin)", "arrAENormalDis", "The Hong Kong Institute of Education (Kwai Chung)", "arrAENormalDis", "The Hong Kong Institute of Education (Kwun Tong)", "arrAENormalDis", "The Hong Kong Institute of Education (Tsing Yi)", "arrAENormalDis", "The Hong Kong Institute of Education (Morrison Hill)", "arrAENormalDis", "The Hong Kong Institute of Education (Tuen Mun)", "arrAENormalDis", "The Prince Philip Dental Hospital", "arrAENormalDis", "Hong Kong Institute of Education", "arrAENormalDis", "The Hong Kong Academy for Performing Arts", "arrAEAPADis");
//
//var arrAENormalDis = new Array("--Select a discipline--", "", "Arts", "", "Social Science", "", "Science", "", "Medicine", "", "Business Administration", "", "Engineering", "", "Architecture", "", "Building & Surveying", "", "Law", "", "Education", "", "Fine Arts/Design", "", "Music", "", "Dentistry", "", "Textile & Clothing", "", "Hospitality Management", "", "Speech & Hearing Science", "");
//var arrAEAPADis = new Array("--Select a discipline--", "", "Ballet", "", "Chinese Dance/Cantonese Opera", "", "Modern Dance", "", "Musical Theatre Dance", "", "Drama", "", "Music", "", "Technical Arts", "", "Applied Arts", "", "Theatre Lighting Design/Technology", "", "Stage Management/Stage Technology/Sound Design & Music Recording", "", "Set & Costume Design", "", "Film and Television", "");
//
////var arrULevel = new Array("--Select a level--", "", "Undergraduate", "Undergraduate", "Postgraduate", "Postgraduate");
//var arrULevel = new Array("--Select a level--", "", "Sub-degree or below", "Sub-degree or below", "Undergraduate", "Undergraduate", "Postgraduate", "Postgraduate");
//var arrAPALevel = new Array("--Select a level--", "", "Subdegree", "Subdegree", "Degree", "Degree");
//
//// use when discipline limit level
////var arrAENormalDis = new Array("--Select a discipline--", "", "Arts", "arrULevel", "Social Science", "arrULevel", "Science", "arrULevel", "Medicine", "arrULevel", "Business Administration", "arrULevel", "Engineering", "arrULevel", "Architecture", "arrULevel", "Building & Surveying", "arrULevel", "Law", "arrULevel", "Education", "arrULevel", "Fine Arts/Design", "arrULevel", "Music", "arrULevel", "Dentistry", "arrULevel", "Textile & Clothing", "arrULevel", "Hospitality Management", "arrULevel", "Speech & Hearing Science", "arrULevel");
////var arrAEAPADis = new Array("--Select a discipline--", "", "Ballet", "arrAPALevel", "Chinese Dance/Cantonese Opera", "arrAPALevel", "Modern Dance", "arrAPALevel", "Musical Theatre Dance", "arrAPALevel", "Drama", "arrAPALevel", "Music", "arrAPALevel", "Technical Arts", "arrAPALevel", "Applied Arts", "arrAPALevel", "Theatre Lighting Design/Technology", "arrAPALevel", "Stage Management/Stage Technology/Sound Design & Music Recording", "arrAPALevel", "Set & Costume Design", "arrAPALevel", "Film and Television", "arrAPALevel");
//
//
////AE of Universities
////var arrAENormalDis = new Array("Arts", "Social Science", "Science", "Medicine", "Business Administration", "Engeering", "Architecture", "Building & Surveying", "Law", "Education", "Fine Arts/Design", "Music", "Dentistry", "Textile & Clothing", "Hospitality Management", "Speech & Hearing Science");
//var arrAENormalUnder = new Array(7010, 5640, 4800, 12660, 5550, 5320, 24690, 8000, 9330, 4400, 25020, 18560, 11660, 6670, 5490, 8790);
//var arrAENormalPost = new Array(8970, 7210, 6140, 16200, 7100, 6800, 31600, 10240, 11940, 5630, 32020, 23750, 14920, 8530, 7020, 11250);
//
//
////AE of HKAPA
////var arrAEAPADis = new Array("Ballet", "Chinese Dance/Cantonese Opera", "Modern Dance", "Musical Theatre Dance", "Drama", "Music", "Technical Arts", "Applied Arts", "Theatre Lighting Design/Technology", "Stage Management/Stage Technology/Sound Design & Music Recording", "Set & Costume Design", "Film and Television");
//var arrAEAPASub = new Array(11750, 9310, 8690, 11030, 8170, 8500, 7950, 11780, 11280, 7950, 0, 7950);
//var arrAEAPADeg = new Array(13950, 11520, 10900, 0, 10390, 10710, 0, 13450, 13500, 10140, 24540, 15710);
//
//
////Grant Loan factor
//var arrGLBound = new Array(0, 20401, 21708, 23866, 26024, 28179, 30290, 32402, 34514, 36623, 38681, 40740, 42801, 44857, 46978, 49097, 51217, 54481);
//var arrGPer = new Array(100, 95, 91, 86, 82, 72, 63, 53, 44, 36, 28, 21, 13, 11, 8, 6, 4, 0);
//var arrLPer = new Array(100, 96, 92, 88, 83, 72, 61, 50, 39, 31, 24, 16, 9, 8, 6, 4, 2, 0);
//
//
////Asset factor
//var arrAssetBound = new Array(531000, 446000, 361000, 285000, 189000, 0);
//var arrAssetPer = new Array(0, 20, 40, 60, 80, 100);
//
////Tuition Fee
//var arrTFee = new Array(42100, 31575, 21950, 13700, 15040);
//
//
//
////Global Variable
//var curMaxLoan=33690;

var nlsloan = 0;
var tmpgrant = 0;
var tmploan = 0;
var tmptf = 0;

//Functions

function init()
{
	document.calculator.guidance1.value = "Guidance Notes on Application for TSFS"+datestr;
	document.result.guidance2.value = "Guidance Notes on Application for TSFS "+datestr;
	document.result.afireckoner.value = datestr + " Ready Reckoner";

		
	with (document.calculator)
	{
		institution.selectedIndex = 0;
		discipline.selectedIndex = 0;
		level.selectedIndex = 0;
		fullyear[0].checked = true;
		
		directinput[0].checked = true;
		fee_select.disabled = false;
		fee_direct.disabled = true;
		fee_select.selectedIndex = 0;
		
		
		marital.selectedIndex = 0;
		parentnum.selectedIndex = 2;
		siblingnum.selectedIndex = 5;
		studyabroadsiblingnum.selectedIndex = 5;
		grandparentnum.selectedIndex = 4;
		childandspouse.selectedIndex = 5;
		
		singlefamily[0].checked = true;
		
		parentorspouseincome.value = "0";
		siblingincome.value = 0;
		applicantincome.value = "0";
		medicalexpense.value = "0";
		
		parentorspouseasset.value = "0";
		applicantasset.value = "0";
	}
	
	document.getElementById("ae").innerHTML = "0";
	document.getElementById("familymembernum").innerHTML = "0";
	document.getElementById("afi").innerHTML = "0";
	document.getElementById("percapitaasset").innerHTML = "0";
}


function directInput(flag)
{
	with (document.calculator)
	{
		if (flag == "0")	// input by selection
		{
			
			fee_select.disabled = false;
			fee_select.selectedIndex = 0;
			fee_direct.disabled = true;
			fee_direct.value = 0;
		}
		else			// direct input
		{
			fee_select.disabled = true;
			fee_select.selectedIndex = 0;
			fee_direct.disabled = false;
			fee_direct.value = 0;
		}
	}
}


function fillSel(selObj)
{
  //				  level 1 name    level 2 name   level 3 name	
  var dataList = (selObj.name == "institution") ? "discipline" : "level"; 
  var i = j = 0;
  var newItem;
  var src;
  var srcName = "";

  //alert(arrGLBound.length);
  for (i = 0; i < selObj.length; i++)
     if (selObj.options[i].selected)
     {
        srcName = selObj.options[i].value;
        
        // use when level 1 narrow level 2 and then narrow 2 narrow level 1
        //switch (dataList) {
        //	case "discipline" :
        //		if (selObj.options[i].text!="--Select an institution--")
        //			userInst = selObj.options[i].text;
        //		break;        	
        //	case "level" : 
        //		if (selObj.options[i].text!="--Select a discipline--")
        //			userDis = selObj.options[i].text;
        //		break;
        //}
     }
  
  if (srcName == "") return;
  src = eval(srcName);

  var data = eval("document.calculator." + dataList);    //calculator is the name of the form
  
  with (data)   
  {
     options.length = 0;
     for (i = 0; i < src.length; i++)
     {
       newItem = options.length;
       options[newItem] = new Option(src[i]);
       options[newItem].value = src[i+1];
       i++;
     }
     options[0].selected = true;
  }
  
  document.getElementById("ae").innerHTML = "0";  
  // history.go(0);
}

function mapInstToLevel()
{	
	if (document.calculator.institution.value == "arrAEAPADis")
	{
		with (document.calculator.level) {
           		//options.length = 1;
           		options.length = 0;
           		for (i = 0; i < arrAPALevel.length; i++)
     			{
       				newItem = options.length;
       				options[newItem] = new Option(arrAPALevel[i]);
       				options[newItem].value = arrAPALevel[i+1];
       				i++;
       			}
     		}
	}
	else
	{
		
		with (document.calculator.level) {
           		//options.length = 1;
           		options.length = 0;
           		for (i = 0; i < arrULevel.length; i++)
     			{
       				newItem = options.length;
       				options[newItem] = new Option(arrULevel[i]);
       				options[newItem].value = arrULevel[i+1];
       				i++;
       			}
     		}
	}
}


function MaritalChange()
{
	with (document.calculator)
	{
		parentnum.selectedIndex = 2;
		siblingnum.selectedIndex = 5;
		studyabroadsiblingnum.selectedIndex = 5;
		grandparentnum.selectedIndex = 4;
		childandspouse.selectedIndex = 5;
		singlefamily[0].checked = true;
		siblingincome.value = "0";
				
		if (marital.value == "married")
		{
			parentnum.disabled = true;
			siblingnum.disabled = true;
			studyabroadsiblingnum.disabled = true;
			grandparentnum.disabled = true;
			childandspouse.disabled = false;
			singlefamily[0].disabled = true;
			singlefamily[1].disabled = true;
			siblingincome.disabled = true;
		}
		else if (marital.value == "single")
		{
			parentnum.disabled = false;
			siblingnum.disabled = false;
			studyabroadsiblingnum.disabled = false;
			grandparentnum.disabled = false;
			childandspouse.disabled = true;
			singlefamily[0].disabled = false;
			singlefamily[1].disabled = false;
			siblingincome.disabled = false;
		}
		else if (marital.value == "divorced")
		{
			parentnum.disabled = true;
			siblingnum.disabled = true;
			studyabroadsiblingnum.disabled = true;
			grandparentnum.disabled = true;
			childandspouse.disabled = false;
			singlefamily[0].disabled = false;
			singlefamily[1].disabled = false;
			//singlefamily[1].checked = true;
			siblingincome.disabled = true;
		}
		else if (marital.value == "widowed")
		{
			parentnum.disabled = true;
			siblingnum.disabled = true;
			studyabroadsiblingnum.disabled = true;
			grandparentnum.disabled = true;
			childandspouse.disabled = false;
			singlefamily[0].disabled = false;
			singlefamily[1].disabled = false;
			//singlefamily[1].checked = true;
			siblingincome.disabled = true;
		}
		else
		{
			parentnum.disabled = true;
			siblingnum.disabled = true;
			studyabroadsiblingnum.disabled = true;
			grandparentnum.disabled = true;
			childandspouse.disabled = true;
			singlefamily[0].disabled = true;
			singlefamily[1].disabled = true;
			siblingincome.disabled = true;
		}
	}
}


function NumOfParentChange()
{
	with (document.calculator)
	{
		if (parentnum.value == 2)
		{
			singlefamily[0].disabled = true;
			singlefamily[1].disabled = true;
			singlefamily[0].checked = true;	
		}
		else
		{
			singlefamily[0].disabled = false;
			singlefamily[1].disabled = false;
			singlefamily[0].checked = true;
		}
	}
}


function CalculateFamilyMemberNum() 
{ 
	var num;
	
	with (document.calculator) {
		switch (marital.value) {
			case "married":
				num = parseInt(childandspouse.value) + 1;
				break;
			case "divorced":
				//num = parseInt(parentnum.value) + parseInt(siblingnum.value) + parseInt(studyabroadsiblingnum.value) + parseInt(grandparentnum.value) + parseInt(childandspouse.value) + 1;
				num = parseInt(childandspouse.value) + 1;
				break;
			case "widowed":
				num = parseInt(childandspouse.value) + 1;
				break;
			default: //single case
				num = parseInt(parentnum.value) + parseInt(siblingnum.value) + parseInt(studyabroadsiblingnum.value) + parseInt(grandparentnum.value) + 1;
		}
	}
	document.getElementById("familymembernum").innerHTML = num;
	MaxMedicalExpense = curMaxME * num;
	CalculateAFI();
	CalculatePCA();
}


function CalculateAE()
{	
     var DisNo, LevelNo;
     for (i = 0; i < document.calculator.discipline.length; i++){
     	if (document.calculator.discipline.options[i].selected) {
        	userDis = document.calculator.discipline.options[i].text; 
        	DisNo = i;  
        	//alert("DisNo = " + DisNo); 
     	}
     }
     for (i = 0; i < document.calculator.level.length; i++){
     	if (document.calculator.level.options[i].selected) {
        	userLevel = document.calculator.level.options[i].text;    
        	LevelNo = i;
        	//alert("LevelNo = " + LevelNo); 
     	}
     }
     
     //alert("problem = " + document.calculator.institution.options.value);	//work in both IE only
     //alert("solution = " + document.calculator.institution.value);	//work in both IE and Netscape
     
     if (document.calculator.institution.value == "arrAEAPADis")
     {
     	if (LevelNo == 1) // LevelNo=1 ---> Subdegree, // LevelNo=2 --> Degree
     	{
     		//document.all["ae"].innerText = arrAEAPADeg[DisNo-1];	//work in IE only
     		document.getElementById("ae").innerHTML = arrAEAPASub[DisNo-1];    
     		//alert(arrAEAPASub[DisNo-1]); 	
     	}
     	else if (LevelNo == 2)
     	{
     		//document.all["ae"].innerText = arrAEAPADeg[DisNo];
     		document.getElementById("ae").innerHTML = arrAEAPADeg[DisNo-1];
     		//alert(arrAEAPADeg[DisNo-1]);
	}
     }
     else if (document.calculator.institution.value == "arrAENormalDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     	{
     		//alert("test = " + document.getElementById("ae").innerText);  //work in IE
     		//alert("test = " + document.getElementById("ae").innerHTML);  // work in both IE and Netscape
     		
     		document.getElementById("ae").innerHTML = arrAENormalUnder[DisNo-1];
     		//alert(arrAENormalUnder[DisNo-1]);     	
     	}
     	else if (LevelNo == 3)
     	{
     		document.getElementById("ae").innerHTML = arrAENormalPost[DisNo-1];
     		//alert(arrAENormalPost[DisNo-1]);
		}     	
     }
     // can insert more case besides normal discipline and APA discipline
     else if (document.calculator.institution.value == "arrAEIVE_STDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_STUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_STPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_LWLDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_LWLUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_LWLPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_KTDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_KTUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_KTPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_TYDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_TYUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_TYPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_MHDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_MHUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_MHPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_HWDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_HWUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_HWPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_KCDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_KCUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_KCPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_TMDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_TMUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_TMPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEIVE_CWDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEIVE_CWUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEIVE_CWPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEHKIEDDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEHKIEDUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEHKIEDPost[DisNo-1];
     }
     else if (document.calculator.institution.value == "arrAEPPDHDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEPPDHUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEPPDHPost[DisNo-1];
     }
	 // new added 20100909
	 else if (document.calculator.institution.value == "arrAEHKDIDis")
     {
     	if ((LevelNo == 1) || (LevelNo == 2)) // LevelNo=1 ---> subdegree or below, LevelNo=2 ---> Undergraduate, // LevelNo=3 ---> Postgraduate
     		document.getElementById("ae").innerHTML = arrAEHKDIUnder[DisNo-1];
     	else if (LevelNo == 3)
     		document.getElementById("ae").innerHTML = arrAEHKDIPost[DisNo-1];
     }
         
     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
     else
     {
     	document.getElementById("ae").innerHTML = "0";
     }
     
     if (document.getElementById("ae").innerHTML == "undefined")
     	document.getElementById("ae").innerHTML = "0";

}


function CalculateAFI() 
{
	var afi_value;
	var familymember;
	
	familymember = parseInt(document.getElementById("familymembernum").innerHTML);
	//alert("family member number = " + familymember);
	//if (document.calculator.singlefamily[1].checked == true) alert("single family")
		
	with (document.calculator) {
		if (marital.value != "married") //single or divorced or widowed
		{
			if ( (singlefamily[1].checked == true) && ((familymember == 2) || (familymember == 3)) )   //single parent family
			{
				familymember++;			//plus 1 factor for single
				familymember++;			//single parent family of 2 to 3 members
				//alert("single parent family Member = " + familymember);
				if ((marital.value == "divored") || (marital.value == "widowed"))  // divored or widow case for single parent family
					afi_value = ( parseInt(parentorspouseincome.value) + parseInt(applicantincome.value) - parseInt(medicalexpense.value) ) / familymember;
				else 	//single case for single parent family
					afi_value = ( parseInt(parentorspouseincome.value) + ( parseInt(siblingincome.value)*0.3 ) + parseInt(applicantincome.value) - parseInt(medicalexpense.value) ) / familymember;
			}
			else
			{
				familymember++;
				if ((marital.value == "divored") || (marital.value == "widowed"))
					afi_value = ( parseInt(parentorspouseincome.value) + parseInt(applicantincome.value) - parseInt(medicalexpense.value) ) / familymember;
				else	//single non-single parent family
					afi_value = ( parseInt(parentorspouseincome.value) + ( parseInt(siblingincome.value)*0.3 ) + parseInt(applicantincome.value) - parseInt(medicalexpense.value) ) / familymember;
			}
		}
		else	//married cases
		{
			familymember++;
			afi_value = ( parseInt(parentorspouseincome.value) + parseInt(applicantincome.value) - parseInt(medicalexpense.value) ) / familymember;
		}
		
		if (afi_value < 0)
			afi_value = 0;
	}
	document.getElementById("afi").innerHTML = Math.floor(afi_value);
	if ((document.getElementById("afi").innerHTML == "Infinity") || (document.getElementById("afi").innerHTML == "NaN"))
		document.getElementById("afi").innerHTML = "0";
}


function CalculatePCA() 
{
	var pca;
	var familymember;
	
	familymember = parseInt(document.getElementById("familymembernum").innerHTML);
	with (document.calculator) {
		if ((marital.value == "divored") || (marital.value == "widowed"))
			pca = parseInt(applicantasset.value) / familymember ;
		else // sinle or married cases 
			pca = ( parseInt(parentorspouseasset.value) + parseInt(applicantasset.value) )  / familymember ;
	}
	document.getElementById("percapitaasset").innerHTML = Math.floor(pca);
	if ((document.getElementById("percapitaasset").innerHTML == "NaN") || (document.getElementById("percapitaasset").innerHTML == "Infinity"))
		document.getElementById("percapitaasset").innerHTML = "0";
}


function CalculateGL()
{
     var temp;
     
     temp = parseInt(document.getElementById("afi").innerHTML);
     if (temp < 0)
     	temp = 0;
     
     for (i = arrGLBound.length-1; i >= 0 ; i--){
     	if (temp >= arrGLBound[i])
     	{
     		//alert("income bound = " + arrGLBound[i]);
		return i;
     	} 	     	     	     	     	     	     	     	     	     	     	     	
     }
}


function CalculateAssetTest()
{
     var temp;
     
     temp = parseInt(document.getElementById("percapitaasset").innerHTML);
     if (temp <= 0)
     	temp = 1;
     	
     for (i = 0; i < arrAssetBound.length; i++)
     {
     	if (temp > arrAssetBound[i])
     	{
		//alert("asset bound = " + arrAssetBound[i]);
		return i;
     	} 	     	     	     	     	     	     	     	     	     	     	     	
     }	
}

//Grant and Loan offer are rounded down to ten, i.e. 12345 ==> 12340
function ResultGL()
{
     var curMaxGrant;
     
     // Start - 27-MAY-2011 assistance tier adjustment
     var familymember;
     var afi_value;
	 familymember = parseInt(document.getElementById("familymembernum").innerHTML);
	 afi_value = parseInt(document.getElementById("afi").innerHTML);
     // End - 27-MAY-2011
     
     if (document.calculator.directinput[0].checked)	//input by selection
     {
     	for (i = 0; i < document.calculator.fee_select.length; i++){
     		if (document.calculator.fee_select.options[i].selected) {
     			//alert("fee = " + parseInt(document.calculator.fee_select.options[i].text));
     			//alert("AE = " + parseInt(document.calculator.ae.value));
     	   		curMaxGrant = parseInt(document.calculator.fee_select.options[i].text) + parseInt(document.getElementById("ae").innerHTML);        	
				tmptf = parseInt(document.calculator.fee_select.options[i].text);
     	   		//alert("max grant = " + curMaxGrant);
     		}
     		
     	}
     }
     else												// input by direct typing
     {
     	//alert("fee = " + parseInt(document.calculator.fee_select.options[i].text));
     	//alert("AE = " + parseInt(document.calculator.ae.value));
     	curMaxGrant = parseInt(document.calculator.fee_direct.value) + parseInt(document.getElementById("ae").innerHTML);        		     	
		tmptf = parseInt(document.calculator.fee_direct.value);
     	//alert("max grant = " + curMaxGrant);
     }
         
     //alert("grant factor = " + arrGPer[CalculateGL()]);
     //alert("loan factor = " + arrLPer[CalculateGL()]);
     //alert("asset factor = " + arrAssetPer[CalculateAssetTest()]);

     if (document.calculator.fullyear[0].checked == 1)
     {	
		// Start - 27-MAY-2011 assistance tier adjustment
		tmpgrant = Math.floor((curMaxGrant * (arrGPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
		tmploan = Math.floor((curMaxLoan * (arrLPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
		
		if(familymember==2 && document.calculator.singlefamily[1].checked == true && afi_value<=adj2MembersSingleParentFamily){ //2 members plus +2 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
		}
		if(familymember==3 && document.calculator.singlefamily[1].checked == true  && afi_value<=adj3MembersSingleParentFamily){ //3 members plus +2 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
		}
		if(familymember==3 && document.calculator.singlefamily[1].checked == false  && afi_value<=adj3MembersNonSingleParentFamily){ //3 members plus +1 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
		}
		if(familymember==4 && afi_value<=adj4MembersFamily){ //4 members plus +1 & +2 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ;
		}
		// End - 27-MAY-2011
		
     	nlsloan = (curMaxGrant - tmpgrant) + (curMaxLoan - tmploan);
        if (nlsloan > tmptf)
        {
            nlsloan = tmptf;
        }
      
		if (nlsloan < 1000 && nlsloan>0)
		{
            nlsloan = 0;
		}
		
		// Start - 27-MAY-2011 assistance tier adjustment 
		document.getElementById("grantresult").innerHTML="$ " + num2dollar(tmpgrant);
		document.getElementById("loanresult").innerHTML="$ " + num2dollar(tmploan);
		document.getElementById("nlsresult").innerHTML="$ " + num2dollar(nlsloan);
		// End - 27-MAY-2011
		
		//document.getElementById("grantresult").innerHTML="$ " + num2dollar( Math.floor((curMaxGrant * (arrGPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ); comment by AP(ITMU)6 27-MAY-2011
		//document.getElementById("loanresult").innerHTML="$ " + num2dollar( Math.floor((curMaxLoan * (arrLPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/10)*10 ); comment by AP(ITMU)6 27-MAY-2011
     }
     else
     {
		tmptf = tmptf /2;
     	
        // Start - 27-MAY-2011 assistance tier adjustment
		tmpgrant =  Math.floor((curMaxGrant * (arrGPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
		tmploan = Math.floor((curMaxLoan * (arrLPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
		
        if(familymember==2 && document.calculator.singlefamily[1].checked == true && afi_value<=adj2MembersSingleParentFamily){ //2 members plus +2 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
		}
		if(familymember==3 && document.calculator.singlefamily[1].checked == true  && afi_value<=adj3MembersSingleParentFamily){ //3 members plus +2 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
		}
		if(familymember==3 && document.calculator.singlefamily[1].checked == false  && afi_value<=adj3MembersNonSingleParentFamily){ //3 members plus +1 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
		}
		if(familymember==4 && afi_value<=adj4MembersFamily){ //4 members plus +1 & +2 factor
			tmpgrant = Math.floor((curMaxGrant * (arrGPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
			tmploan = Math.floor((curMaxLoan * (arrLPer[0]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ;
		}
		// End - 27-MAY-2011
		
     	nlsloan = (curMaxGrant/2 - tmpgrant) + (curMaxLoan/2 - tmploan);

        if (nlsloan > tmptf)
        {
            nlsloan = tmptf;
        }
		if (nlsloan < 1000 && nlsloan>0)
		{
			nlsloan = 0;
		}

		// Start - 27-MAY-2011 assistance tier adjustment 
		document.getElementById("grantresult").innerHTML="$ " + num2dollar(tmpgrant);
		document.getElementById("loanresult").innerHTML="$ " + num2dollar(tmploan);
		document.getElementById("nlsresult").innerHTML="$ " + num2dollar(nlsloan);
		// End - 27-MAY-2011
		//document.getElementById("grantresult").innerHTML="$ " + num2dollar( Math.floor((curMaxGrant * (arrGPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ); comment by AP(ITMU)6 27-MAY-2011
		//document.getElementById("loanresult").innerHTML="$ " + num2dollar( Math.floor((curMaxLoan * (arrLPer[CalculateGL()]/100) * (arrAssetPer[CalculateAssetTest()]/100))/2/10)*10 ); comment by AP(ITMU)6 27-MAY-2011
     } 
}


function num2dollar(num) 	//no cents dollar
{ 
   var tmp1 = dollars = "";
   var num;
   var i;
   //var cents = "";
   //var dec = -1;

   if (!checkNum(num))       return (0);

   num = "" + (Math.round(num * 100)/100);
   //dec = num.indexOf(".");
   //cents = ((dec > 0) ? num.substring(dec,num.length) : ".00");
   //if (cents.length == 2) cents += "0";
   dollars = "" + parseInt(num);
      
   tmp1 = insComma(dollars);
   num = "";

   for (i = tmp1.length-1; i >= 0; i--) 
      num += tmp1.charAt(i);;
   
   //num +=  cents;
   
   return num;
}


function insComma(data)
{
   var count = i = 0;
   var tmpStr = "";
   var comma = ",";

   for (i = data.length-1; i >= 0; i--) 
   {
      if (count == 3)
      {
         tmpStr += comma;
         count = 1;
      }
      else
         count ++;
      tmpStr += data.charAt(i); 
   }
   return (tmpStr);
}



function ValidInput(form, field) 
{ 
   var tmp1 = cents = dollars = "";
   var dec = -1;
   var num = i = 0;

   num = eval("document." + form + "." + field + ".value");
   if (!checkNum(num))       return (false);

   num = "" + Math.round(num);    // round to nearest integer

//Converting the number to currency format
//
//   dec = num.indexOf(".");
//
//   cents = ((dec > 0) ? num.substring(dec,num.length) : ".00");
//   if (cents.length == 2) cents += "0";
//
//   dollars = "" + parseInt(num);
//   tmp1 = insComma(dollars);
//   num = "$";
//
//   for (i = tmp1.length-1; i >= 0; i--) 
//      num += tmp1.charAt(i);
//
//   num +=  cents;
   
   eval("document." + form + "." + field + ".value = '" + num + "';");
   
   return(true);
}


function feeTooLarge(value)
{
	if (value>1000000)
	{
		alert("Invalid input range. Please input again!");
		return (1);
	}
	else	return (0);
}


function medicalExpenseCheck(value)
{
	//if (parseInt(value)>curMaxME)
	if (parseInt(value)>MaxMedicalExpense)
	{
		//alert("Invalid input range. Please input again!");
		//document.calculator.medicalexpense.value = curMaxME;
		return (1);
	}
	else	return (0);
}


function checkNum(data) 
{      
   var validNum = "0123456789.";  
   var i = count = 0;
   var dec = ".";
   var space = " ";

   for (i = 0; i < data.length; i++)
      if (data.substring(i, i+1) == space)
        return(alertBadNum(1));

   for (i = 0; i < data.length; i++)
      if (validNum.indexOf(data.substring(i, i+1)) == "-1")
         return(alertBadNum(2));

   for (i = 0; i < data.length; i++)
      if (data.substring(i, i+1) == dec)   count++;
   if (count > 1)      return(alertBadNum(3));
 
   return true;
}


function alertBadNum(errType)
{
   var errMsg = '';
   var goodNum = false;
   switch (errType)
   {
      case 1:
         errMsg = 'You have entered a blank space.\nPlease re-enter your number.';
         break;
      case 2:
         errMsg = 'You have entered a nonnumeric value.\n Please re-enter your number.';
         break;
      case 3:
         errMsg = 'You have entered two or more decimal points.\n Please re-enter your number.';
         break;
   }
   alert(errMsg);
   return(goodNum);
}


function checking()
{

	if ( document.calculator.institution.options[0].selected )
	{
		alert ("Please select an institution");
		return false;
	}
 
	if ( document.calculator.discipline.options[0].selected )
	{
		alert ("Please select a discipline");
		return false;
	}

	if ( document.calculator.level.options[0].selected )
	{
		alert ("Please select a level");
		return false;
	}


	return true;
}











                                          
