var myFlag;

function initArraySize(size)
{
	this.length = size;
	for (i=0; i< this.length; i++)	
		this[i] = "";
}

/* fn to determine whether a digit is in fact a digit */

function isdigit(c) 
{
	return ((c >= '0') && (c <= '10000000'));
}

/* code to do calculation */

function fv(pv,yrs,intr) {
	return (parseInt(pv) * Math.pow(1 + parseFloat(intr), parseInt(yrs)));
}

function pv(fv,yrs,intr) {
	return (parseInt(fv) / Math.pow(1 + parseFloat(intr), parseInt(yrs)));
}

//<!-- fn to update the subtotal on change events. -->
function UpdateTotal(formObj)
{
	var tuitionfees = parseInt(formObj.tuition.value);
	var livingexpenses = parseInt(formObj.livingexpenses.value);
	var studyyears = formObj.studyyears.options[formObj.studyyears.selectedIndex].value;
	var extras = parseInt(formObj.extras.value);
	
	var subtotal = (tuitionfees + livingexpenses) * studyyears + extras;
	if ((!isNaN(subtotal)) && (subtotal>0)) {
		formObj.collegecost.value = subtotal;
		formObj.collegecost.blur();
	} else {
		formObj.collegecost.value = 0;
	}
}

function CalculateGoal(formObj)
{
	document.forms["children"].amountsaved.value=document.forms["children"].amountsaved.value==0?0:(parseInt(document.forms["children"].amountsaved.value)?parseInt(document.forms["children"].amountsaved.value):'');
	document.forms["children"].collegecost.value=document.forms["children"].collegecost.value==0?0:(parseInt(document.forms["children"].collegecost.value)?parseInt(document.forms["children"].collegecost.value):'');
	myFlag=0;
	
	/* check the college cost sub total first */
	var tuitionfees = parseInt(formObj.tuition.value);
	var livingexpenses = parseInt(formObj.livingexpenses.value);
	var studyyears = formObj.studyyears.options[formObj.studyyears.selectedIndex].value;
	var extras = parseInt(formObj.extras.value);
	
	var subtotal = (tuitionfees + livingexpenses) * studyyears + extras;
	if (!isNaN(subtotal))
		goalamount = formObj.collegecost.value = subtotal;
	else
		goalamount = formObj.collegecost.value = 0;
		
	/* note nifty checks to trap non-numerics using logical NOT (!) character
	and also note full syntax to retrieve values from SELECT boxes - important for Netscape 
	which does not allow formObj.ratereturn.value from a SELECT object whereas MSIE does .. PC 7/1/98 */ 
	var goalamount = formObj.collegecost.value;
	var goalyears = formObj.goalyears.options[formObj.goalyears.selectedIndex].value;
	var goalinflation = formObj.inflationfactor.options[formObj.inflationfactor.selectedIndex].value;
	var savingsfactor = formObj.savingsfactor.options[formObj.savingsfactor.selectedIndex].value;
	var goalsaved = formObj.amountsaved.value;
	
	goalamount=parseInt(goalamount);
	goalsaved=parseInt(goalsaved);
	
	if (!isdigit(tuitionfees))
	{
		alert ("Please enter a valid and unformatted tuition fees [ between 0 and 10,000,000 ]");
		document.forms['children'].tuition.focus(); 
	}
	else if (emptyField(formObj.tuition))
	{
		alert ("Please enter a valid amount of tuition fees");
		document.forms['children'].tuition.focus(); 
	}
	else if (emptyField(formObj.livingexpenses))
	{
		alert ("Please enter a valid amount of living expenses");
		document.forms['children'].livingexpenses.focus();
	}
	else if (!isdigit(livingexpenses))
	{
		alert ("Please enter a valid amount of living expenses [ between 0 and 10,000,000 ]");
		document.forms['children'].livingexpenses.focus(); 
	}
	else if(studyyears<0)
	{
		alert("Please, choose Years of Study.");
		document.forms["children"].studyyears.focus();
	}
	else if (emptyField(formObj.extras))
	{
		alert ("Please enter a valid amount of extra expenses");
		document.forms['children'].extras.focus();
	}
	else if (!isdigit(extras))
	{
		alert ("Please enter a valid amount of extra expenses [ between 0 and 10,000,000 ]");
		document.forms['children'].extras.focus(); 
	}
	else if (emptyField(formObj.amountsaved))
	{
		alert ("Please enter the amount you have already saved towards your goal");
		document.forms['children'].amountsaved.focus();
	}
	else if (!isdigit(goalsaved))
	{
		alert ("Please enter a valid amount already saved [ between 0 and 10,000,000 ]");
		document.forms['children'].amountsaved.focus(); 
	}
	else if(goalyears<0)
	{
		alert("Please, choose Investment Duration.");
		document.forms["children"].goalyears.focus();
	}
	else if(goalinflation<0)
	{
		alert("Please, choose an inflation rate.");
		document.forms["children"].inflationfactor.focus();
	}
	else if(savingsfactor<0)
	{
		alert("Please, choose rate of return.");
		document.forms["children"].savingsfactor.focus();
	}
	
	else if (goalsaved > goalamount)
	{
		alert ("The amount you have already saved must be less than the cost of your goal; Please re-enter.");
	}
	/*
	else if(goalinflation==savingsfactor)
	{
		var temp=goalamount-goalsaved;
		formObj.amountsave.value = delimit(Math.round(temp));
		formObj.annualsavings.value = delimit(Math.round(temp/goalyears));
		formObj.monthlysavings.value = delimit(Math.round(temp/(goalyears*12)));
		formObj.collegecost.focus();
	} 
	*/  
	else 
	{
		var lump_sum;
		var months;
		var monthly_int;
		var int_factor;
		var future_goal;
				
			var inflation = parseInt(goalinflation) / 100;
			var returnRate = parseInt(savingsfactor) / 100;
	
			future_goal = fv(goalamount,goalyears,inflation);
			formObj.amountsave.value = delimit(Math.round(future_goal));
			
			// lump_sum = Math.round(pv(f.fut_goal.value,f.years.value,returnRate) - parseInt(f.pres_save.value));
			lump_sum = future_goal - fv(goalsaved,goalyears,returnRate);
	
			months = parseInt(goalyears) * 12;
			monthly_int = returnRate / 12;
			int_factor = (Math.pow(1+monthly_int,months)-1) / monthly_int;
			formObj.monthlysavings.value = delimit(Math.round(parseInt(lump_sum) / int_factor));
			
			yearly_int = returnRate / goalyears;
			int_factor = (Math.pow(1+yearly_int,goalyears)-1) / yearly_int;
			formObj.annualsavings.value = delimit(Math.round(parseInt(lump_sum) / int_factor));
	}
	return false;
}



//<!-- fn to switch focus back up to an enterable field when user tries to enter any of the r/o fields. -->

function reFocus() 
{
	if (emptyField(document.forms['children'].tuition))
		setTimeout("document.forms['children'].tuition.focus()",1);
	else if (emptyField(document.forms['children'].livingexpenses))
		setTimeout("document.forms['children'].livingexpenses.focus()",1);
	else if (document.forms['children'].studyyears.options[document.forms['children'].studyyears.selectedIndex].value < 0)
		setTimeout("document.forms['children'].studyyears.focus()",1);
	else if (emptyField(document.forms['children'].extras))
		setTimeout("document.forms['children'].extras.focus()",1);
	else {
		setTimeout("document.forms['children'].goalyears.focus()",1);
	}
} 

//<!-- Simple check to see if there is something in a field -->

function emptyField(textObj) 
{
	if (textObj.value.length == 0) 
		return true;
	for (var i=0; i<textObj.value.length; ++i) 
	{
		var ch = textObj.value.charAt(i);
		if (ch != ' ' && ch != '\t') 
			return false;
	}
	return true;
}

/*
Mar 2001, Mubeen Khateeb
clears the fields
*/

function clearFnc(formObj)
{
	for(var z=0;z<formObj.length;++z)
	{
		if(formObj[z].type=="text")
			formObj[z].value="";
		else
			formObj[z].selectedIndex=0;
	}
/*to avoid another window opening up always return false*/
		formObj.tuition.focus();
		return false;			
}


/*
Mar 2001, Mubeen Khateeb
returns numeral with a "," delimiter 
*/

function delimit(numObj)
{
	if((numObj+0)<=0)
	{
		if(myFlag==0)
			alert("Your already saved property will have crossed your investment aim with interests and compound interest after the chosen investment duration, inflation and yield. Your necessary investment amount is therefore 0 ");
		myFlag=1;				
		return "";
	}
	
	var arr=new Array();
	var temp=numObj+0;
	var n=0;
	while(temp>0)
	{
		arr[n]=temp%10;
		temp/=10;
		temp=Math.floor(temp);
		++n;
	}
	arr.reverse();

	var len=arr.length;
	var nOD=Math.floor(len/3);
	var fDP=len%3;
	if(nOD==0)
		return(numObj)
	else if((nOD==1) && (fDP==0))
		return(numObj)
	else
	{			
		if(fDP==0) 
		{
			fDP=3;			
			--nOD;
		}
		
		var p1=0,p2=0;
		var res=new Array();
		for(var i=0;i<fDP;++i)
			res[p2++]=arr[p1++]+"";
		res[p2++]=",";
		--nOD;
		while(nOD>=0)
		{
			for(var j=0;j<3;++j)
			{	
				res[p2++]=arr[p1++]+"";
			}
			res[p2++]=",";
			--nOD;
		}
		res[--p2]="";
		var str="";
		for(var m=0;m<res.length;++m)
		{
			str+=res[m];
		}
		return(str)
	}
}

