function VerifyWhole(num) {
	
	if (num.indexOf(".") > -1)
		return false
	else  
		return true
}

function calculateRESP(theForm) {
with (theForm) {

if(theForm.CurrentSavings.value=="" || theForm.CurrentSavings.value>10000001 || IsNumeric(theForm.CurrentSavings.value)==false || 
theForm.OtherSavings.value=="" || theForm.OtherSavings.value>10000001 || IsNumeric(theForm.OtherSavings.value)==false || 
theForm.Interest.value=="" || theForm.Interest.value>100 || IsNumeric(theForm.Interest.value)==false || 
theForm.AnnualContribution.value=="" || theForm.AnnualContribution.value>4000 || IsNumeric(theForm.AnnualContribution.value)==false || 
theForm.TuitionCost.value=="" || theForm.TuitionCost.value>10000001 || IsNumeric(theForm.TuitionCost.value)==false || 
theForm.RoomAndBoardCost.value=="" || theForm.RoomAndBoardCost.value>10000001 || IsNumeric(theForm.RoomAndBoardCost.value)==false || 
parseInt(theForm.NumOfYears.value)>parseInt(theForm.withdrawfunds.value))  {
		
    	if(theForm.CurrentSavings.value=="" || theForm.CurrentSavings.value>10000001 || IsNumeric(theForm.CurrentSavings.value)==false) {
			alert("Value of Existing RESP's is not in range 0..10,000,001");	
			return;
		} 
		else if (theForm.OtherSavings.value=="" || theForm.OtherSavings.value>10000001 || IsNumeric(theForm.OtherSavings.value)==false) {
			alert("Other Education Savings not in range 0..10,000,001");	
			return;
		}
		else if (theForm.Interest.value=="" || theForm.Interest.value>100 || IsNumeric(theForm.Interest.value)==false) {
				alert("Annual Rate of Return is not in range 0..100");	
				return;
		}
		else if (theForm.AnnualContribution.value=="" || theForm.AnnualContribution.value>4000 || IsNumeric(theForm.AnnualContribution.value)==false) {
				alert("Selected Annual RESP Contribution is not in range 0..4000");	
				return;
		}
		else if (theForm.TuitionCost.value=="" || theForm.TuitionCost.value>10000001 || IsNumeric(theForm.TuitionCost.value)==false) {
				alert("Total Tuition Cost is not in range 0..10,000,001");	
				return;
		}
		else if (theForm.RoomAndBoardCost.value=="" || theForm.RoomAndBoardCost.value>10000001 || IsNumeric(theForm.RoomAndBoardCost.value)==false) {
				alert("Total Room and Board Cost is not in range 0..10,000,001");	
				return;
		}
		else if (parseInt(theForm.NumOfYears.value)>parseInt(theForm.withdrawfunds.value)) {
			alert("Age the beneficiary RESP started must be less than the Age the benficiary Starts School");	
			
				return;
		}
	return;
}
	if (parseInt(strip_commas(theForm.AnnualContribution.value)) > 4000){
	        alert("0");
			theForm.AnnualContribution.value = "";
			theForm.AnnualContribution.focus();
			return false;
	}
	if (theForm.TuitionCost.value == null || theForm.TuitionCost.value.length == 0)  {
		theForm.TuitionCost.value =0;
	} 
	if (theForm.RoomAndBoardCost.value == null || theForm.RoomAndBoardCost.value.length == 0)  {
		theForm.RoomAndBoardCost.value =0;
	} 
	if (theForm.OtherSavings.value == null || theForm.OtherSavings.value.length == 0) {
		theForm.OtherSavings.value =0;
	} 
	
	theForm.TuitionCost.value = strip_commas(theForm.TuitionCost.value);
	theForm.RoomAndBoardCost.value = strip_commas(theForm.RoomAndBoardCost.value);

    theForm.CurrentSavings.value = strip_commas(theForm.CurrentSavings.value);
    theForm.OtherSavings.value = strip_commas(theForm.OtherSavings.value);
	theForm.OtherSavingsAvailable.value = exact(theForm.OtherSavings.value,10,2);
	theForm.TotalEducationCosts.value = exact((parseFloat(theForm.TuitionCost.value) + parseFloat(theForm.RoomAndBoardCost.value)),10,2);
	theForm.AnnualContribution.value = strip_commas(theForm.AnnualContribution.value);
	
    interest_selected=theForm.Interest.value;
	i = interest_selected;
	i = i / 100.0;
	
    NumOfYears_selected=theForm.NumOfYears.value;
	
	var Today = new Date()
	var ThisYear = Today.getYear()
	
	years_old = NumOfYears_selected;
    year_born = ThisYear-NumOfYears_selected; 

	years_to_18 = 18-parseInt(years_old);
	temp_years = parseInt(theForm.withdrawfunds.value)-parseInt(years_old);//19-10=9
NumOfYears_selected = temp_years;
	years_of_nogovt_contrib = parseInt(temp_years) - parseInt(years_to_18);//9-8=1
	     
	theForm.AnnualContribution.value = strip_commas(theForm.AnnualContribution.value);
	if (parseInt(years_to_18) > 0)  {
		if (parseFloat(theForm.AnnualContribution.value) <= 2000){
			if (parseInt(theForm.withdrawfunds.value) > 16) {
				var CESGMinimumAnnualContribution = parseFloat(theForm.AnnualContribution.value)*parseFloat(years_to_18-2);

				if ((parseFloat(years_to_18) >= 4 && theForm.AnnualContribution.value >= 100) || CESGMinimumAnnualContribution >= 2000) {
					flGovernmentContribution = parseFloat(theForm.AnnualContribution.value) * 0.2 * parseFloat(years_to_18);
					govt_contrib_peryear = parseFloat(theForm.AnnualContribution.value) * 0.2;
				}
				else {
					flGovernmentContribution = parseFloat(theForm.AnnualContribution.value) * 0.2 * (parseFloat(years_to_18)-2);
					govt_contrib_peryear = parseFloat(theForm.AnnualContribution.value) * 0.2;
				}
			}
			else {
				flGovernmentContribution = parseFloat(theForm.AnnualContribution.value) * 0.2 * parseFloat(NumOfYears_selected);
				govt_contrib_peryear = parseFloat(theForm.AnnualContribution.value) * 0.2;
			}
		}
		else {
			if (parseInt(theForm.withdrawfunds.value) > 18) {
				flGovernmentContribution = 400 * parseFloat(years_to_18)
				govt_contrib_peryear = 400;
			}
			else {
				flGovernmentContribution = 400 * parseFloat(NumOfYears_selected)
				govt_contrib_peryear = 400;
			}
		}
	}
	else {
		govt_contrib_peryear = 0;
		flGovernmentContribution = 0; 
	}
	
	flGovernmentContribution = Math.round(100 * flGovernmentContribution) / 100
	strResult = "" + eval(flGovernmentContribution);

	if (VerifyWhole(strResult))
		strResult = strResult + ".00"

	if (strResult.charAt(strResult.indexOf(".") + 2) == '') 
		strResult = strResult + '0';

	theForm.GovernmentContribution.value = strResult;
	if(theForm.GovernmentContribution.value>7200) 
	theForm.GovernmentContribution.value=7200;
	theForm.GovernmentContribution.value = exact(theForm.GovernmentContribution.value,10,2);

    interest_value = 0;
    current_value = parseFloat(theForm.CurrentSavings.value);
	interest_rate = parseFloat(interest_selected)/100;
	current_contribution = 0;
	var annualContributionHolder = theForm.AnnualContribution.value;
	for (a=0; a<NumOfYears_selected; a++){
	     current_contribution = current_contribution + parseFloat(theForm.AnnualContribution.value);
		 if (parseFloat(current_contribution) > 42000){
		    theForm.AnnualContribution.value = 0;
		 }
	     current_value = parseFloat(current_value) + parseFloat(theForm.AnnualContribution.value);
	     interest_value_new = ((parseFloat(current_value) + parseFloat(govt_contrib_peryear)) * parseFloat(interest_rate));
		 interest_value = parseFloat(interest_value) + parseFloat(interest_value_new);
		 current_value = parseFloat(current_value) + parseFloat(govt_contrib_peryear) + parseFloat(interest_value_new);
    }
	
	for (b=0; b<parseInt(years_of_nogovt_contrib); b++){
	     current_contribution = current_contribution + parseFloat(theForm.AnnualContribution.value);
		 if (parseFloat(current_contribution) > 42000){
		     theForm.AnnualContribution.value = 0;
		 }
	     current_value = parseFloat(current_value) + parseFloat(theForm.AnnualContribution.value);
         interest_value_new = (parseFloat(current_value) * parseFloat(interest_rate));
         interest_value = parseFloat(interest_value) + parseFloat(interest_value_new);
		 current_value = parseFloat(current_value) + parseFloat(interest_value_new);
    }
	     
	funds = ""+Math.round(100*current_value)/100+"";
	checkdecimal = parseInt(funds.length) - (parseInt(funds.indexOf('.')+1));
	if (parseInt(checkdecimal) == 1){
	   funds = ""+funds+"0";
	}
	theForm.FundsAvailable.value = exact(funds,10,2);
theForm.ShortfallSurplus.value = exact(parseFloat(strip_commas(theForm.OtherSavingsAvailable.value)) + parseFloat(strip_commas(theForm.FundsAvailable.value)) + parseFloat(strip_commas(theForm.GovernmentContribution.value)) - parseFloat(strip_commas(theForm.TotalEducationCosts.value)),10,2);

    theForm.CurrentSavings.value = theForm.CurrentSavings.value;
	theForm.AnnualContribution.value = annualContributionHolder;

	return true;

}

}

function exact(val, len, decimal) {

	if(decimal == null)
	decimal = 1;
	scale = Math.pow(10, decimal);
	var negative = 0;
	
	if(val<0) {
		val = val*-1;
		negative = 1;
	}
	
	tStr = "" + Math.round(parseFloat(val) * scale);

	if(tStr.length == 0 || tStr == "0") {
		tStr = "0";
		for(var i=0; i<=decimal; i++)
			tStr += "0";
		}
		str = "." + tStr.substring(tStr.length-decimal,tStr.length);
		
	for(i=tStr.length-decimal-1, j=0 ; i>=0; i--) {
		if(++j > 3) {
			str = "," + str;
			j = 1;
			}
		str = tStr.substring(i, i+1) + str;
	}
		
	if(negative==1)
	str = "-" + str;
	i = len - str.length;
	return str;
}

function IsNumeric(PossibleNumber){	
var PNum = new String(PossibleNumber);	
var regex = /[^0123456789.,]/;	
return !regex.test(PNum);
}

function strip_commas(number){
	 a = new Array();
	 a = number.split("");
	 b = new Array();
	 b_count = 0;
	 for (i=0;i<=a.length-1;i++) {
	    if (a[i] != ",") {		  
	      b[b_count] = a[i];
		  b_count++;
		}  
	 }
     number_stripped = "";
	 for (i=0; i<=b_count-1; i++){
	     number_stripped = number_stripped + b[i];
	 }
	 return (""+number_stripped+"");
}
// -->
