
//Functions
function init()
{
	document.calculator.guidance1.value = "Guidance Notes on Application for FASP " + datestr;
	//alert(document.calculator.guidance1.value);
	document.result.guidance2.value = "Guidance Notes on Application for FASP " + datestr;
	document.result.afireckoner.value = datestr + " Ready Reckoner";
	
	with (document.calculator)
	{
		directinput[0].checked = true;
		institution.selectedIndex = 0;
		course.selectedIndex = 0;
		//level.selectedIndex = 0;
		fullyear[0].checked = true;
		//fee.selectedIndex = 0;
		fee.value = "0";
		fee.disabled = true;
				
		marital.selectedIndex = 0;
		parentnum.selectedIndex = 2;
		siblingnum.selectedIndex = 9;
		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 menu
		{
			institution.disabled = false;
			course.disabled = false;
			fee.value = 0;
			fee.disabled = true;
		}
		else			// direct input
		{
			institution.selectedIndex = 0;
			course.selectedIndex = 0;
			institution.disabled = true;
			course.disabled = true;
			fee.value = 0;
			fee.disabled = false;
		}
	}
}


function fillSel(selObj)
{
  //				  level 1 name    level 2 name   level 3 name	
  var dataList = (selObj.name == "institution") ? "course" : "level"; 	//datalist is the name of the destination list
  var i = j = k = 0;
  var newItem;
  var src;
  var srcName = "";

  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 3
        //switch (dataList) {
        //	case "course" :
        //		if (selObj.options[i].text!="--Select an institution--")
        //			userInst = selObj.options[i].text;
        //		break;        	
        //	case "level" : 
        //		if (selObj.options[i].text!="--Select a course--")
        //			userDis = selObj.options[i].text;
        //		break;
        //}
     }
  
  if (srcName == "arr") return;
  src = eval(srcName.substring(3,5));		//src store the name of the list of the next layer, represent the "institution number"
  //alert("srcName = " + srcName);
  //alert("src = " + src);
  
  var data = eval("document.calculator." + dataList);    //calculator is the name of the form
  
  with (data)   // destination list
  {
     options.length = 0;
     //alert("Length of array = " + arrCourseCode[src].length);
     for (i = 0; i < arrCourseCode[src].length+1; i++)
     {
       newItem = options.length;
       if (newItem == 0)
       {
          options[newItem] = new Option("--Select a course--");
          options[newItem].value = "";      
       }
       else
       {
       	  options[newItem] = new Option(arrCourseCode[src][i-1]);      
       	  options[newItem].value = arrCourseFee[src][i-1];
       }
       //i++;
     }
     options[0].selected = true;
  }
  // history.go(0);
}


function mapCourseToFee()
{
	document.calculator.fee.value = document.calculator.course.options[document.calculator.course.selectedIndex].value;	
}


function MaritalChange()
{
	with (document.calculator)
	{
		parentnum.selectedIndex = 2;
		siblingnum.selectedIndex = 9;
		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 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 = arrAFIBound.length-1; i >= 0 ; i--){
     	if (temp >= arrAFIBound[i])
     	{
     		//alert("income bound = " + arrAFIBound[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;
     	} 	     	     	     	     	     	     	     	     	     	     	     	
     }	
}


function ResultGL()
{
     var curMaxGrant;
     //var curMaxAE;
     var grant=0;
     var loan=0;
//Start - added on 14.8.2008 for LE Loan Enhancement
     var LEloan=0;	
//End   - added on 14.8.2008 for LE Loan Enhancement
     //curMaxAE=3000;
     //alert("max AE = " + curMaxAE);
     //alert("max LE = " + curMaxLE);
     curMaxGrant = parseInt(document.calculator.fee.value);  
     //alert("max grant = " + curMaxGrant);
     //alert("income factor = " + arrIncomePer[CalculateGL()]);
     //alert("asset factor = " + arrAssetPer[CalculateAssetTest()]);
     
     if (document.calculator.fullyear[0].checked == 1)
     {	
     	if ( (arrIncomePer[CalculateGL()] == "100") && (arrAssetPer[CalculateAssetTest()] == "100") )
     		grant = curMaxGrant;
     	else grant = Math.floor( Math.floor(curMaxGrant * (arrIncomePer[CalculateGL()]/100)) * (arrAssetPer[CalculateAssetTest()]/100));
        loan = Math.floor( Math.floor(curMaxAE * (arrIncomePer[CalculateGL()]/100)) * (arrAssetPer[CalculateAssetTest()]/100));
//Start - added on 14.8.2008 for LE Loan Enhancement
        LEloan = Math.floor( Math.floor(curMaxLE * (arrIncomePerLE[CalculateGL()]/100)) * (arrAssetPer[CalculateAssetTest()]/100));
//End   - added on 14.8.2008 for LE Loan Enhancement
     }
     else
     {
     	if ( (arrIncomePer[CalculateGL()] == "100") && (arrAssetPer[CalculateAssetTest()] == "100") )
     		grant = Math.floor(curMaxGrant/2);
     	else	grant = Math.floor( (Math.floor(curMaxGrant * (arrIncomePer[CalculateGL()]/100)/2) * (arrAssetPer[CalculateAssetTest()]/100)));
        loan = Math.floor( (Math.floor(curMaxAE * (arrIncomePer[CalculateGL()]/100)/2) * (arrAssetPer[CalculateAssetTest()]/100)));
//Start - added on 14.8.2008 for LE Loan Enhancement
        LEloan = Math.floor( (Math.floor(curMaxLE * (arrIncomePerLE[CalculateGL()]/100)/2) * (arrAssetPer[CalculateAssetTest()]/100)));
//End   - added on 14.8.2008 for LE Loan Enhancement
     }
     
     
     
     //handle the case when grant > max amount
     if (document.calculator.fullyear[1].checked == 1) //half-year
     {
     	//alert("max = " + curMaxLoan/2);
     	if (grant>(curMaxLoan/2)) grant = curMaxLoan/2;
//     	if (loan>(curMaxLoan/2)) loan = curMaxLoan/2;
     }
     if (grant>curMaxLoan) grant = curMaxLoan;
//     if (loan>curMaxLoan) loan = curMaxLoan;
     
     if (isNaN(grant))
     	grant = 0;
     if (isNaN(loan))
     	loan = 0;
//Start - added on 14.8.2008 for LE Loan Enhancement
     if (isNaN(LEloan))
     	LEloan = 0;
//End   - added on 14.8.2008 for LE Loan Enhancement
     
     //alert("grant/tiution fees = " + grant);
     //alert("loan/AE grant up = " + loan);
     //alert("LEloan = " + LEloan);
     	
     document.getElementById("grantresult").innerHTML="$ " + num2dollar(grant);
     document.getElementById("loanresult").innerHTML="$ " + num2dollar(loan); 
     document.getElementById("LEloanresult").innerHTML="$ " + num2dollar(LEloan); 

}


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 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 feeTooLarge(value)
{
	if (value>1000000)
	{
		alert("Invalid input range. Please input again!");
		return (1);
	}
	else	return (0);
}


function medicalExpenseCheck(value)
{
	if (parseInt(value)>MaxMedicalExpense)
	{
		//alert("Invalid input range. Please input again!");
		return (1);
	}
	else	return (0);
}

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);
}



                                          