
var gerrflag=false;

function printable()
{
	document.temps.submit();
	
}

function floor(number)
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function computeForm(form){

	basicedit();

	var IR = replacePct(form.IR.value) / 1200;
	var BB = replaceChars(form.BB.value);
	var BB = replaceChar(BB);
	var MS = replaceChars(form.MS.value);
	var MS = replaceChar(MS);
	var SG1 = replaceChars(form.SG.value);
	var SG1 = replaceChar(SG1);
	
	var YS = eval(form.YS.value);	
	
	if (SG1<="0" || SG1=="")
	{
		alert("Sorry, the Savings Goal must be greater than zero.");
		form.SG.value='$20,000';		
		form.SG.focus();
		form.SG.select();	
		init();
		return false;					
	}
	
	if (MS<="0" || MS=="")
	{
		if (BB=="0" || IR=="0")
		{
			alert("Sorry, the Monthly Savings must be greater than zero.");
			form.MS.value='$100';		
			form.MS.focus();
			form.MS.select();	
			init();
			return false;					
		}
	}	
	
	if (YS<="0" || YS=="")
	{
		alert("Sorry, the Months to Save must be greater than zero.");
		form.YS.value='12';		
		form.YS.focus();
		form.YS.select();	
		init();
		return false;					
	}	
		
	if (checkForm(SG1) && checkForm(IR) && checkForm(BB) && checkForm(MS) && checkForm(YS));
	

	if (SG1>0)
	{
		if (parseInt(BB) >= parseInt(SG1))
		{
			if (gerrflag) 
				gerrflag=false;
			else
			{
				gerrflag=true;
				alert("Congratulations! Your current savings amount already exceeds your Saving Goal");
				form.SG.value='$20,000';		
			}				
			init();		
			return false;
		}
	}
	
	if (IR==0)
		{			
		var OP = (SG1 - BB) / MS;
		var MA = parseInt(OP);			
		var Y = eval(form.YS.value);
		//var Y = Y * 12;		 
		var MI = (SG1 - BB) / Y;	
		}	
	else
		{
		var Y = eval(form.YS.value);
		//var Y = Y * 12;
		var MS = MS - (2*MS);
		var x = Math.pow(1 + IR,Y);
		var OP = (Math.log((-SG1 * IR + MS) / (MS - IR * BB)) / Math.log(1 + IR));
		var MI = parseInt((IR * (-SG1 + x * BB)) / (-1 + x))* -1;
		var MA = parseInt(OP);
		}

	if (MI<0)
		MI=0;
	
	form.MA.value = MA;
	form.MI.value = formatCurrency(MI);

	}

function init()
{
		var space="";
		var dollarsign="$";
		document.temps.MA.value = space;		
		document.temps.MI.value = dollarsign;
		return false;
}

function replaceChars(entry) {
	out = ","; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}
function replaceChar(entry) {
	out = "$"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function replacePct(entry) {
	out = "%"; 
	add = ""; 
	temp = "" + entry;
	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function formatCurrency(num) {			
			num=replaceChar(num);
			num=replaceChars(num);
		if ( checkForm(num));
{
			num = num.toString().replace(/$|,/g,'');
			if(isNaN(num)) num = "0";
			num = Math.floor(num).toString();
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
			num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
			 
	return ('$' + num );	
}
}

function basicedit()
{
	if 	(!IsMoney(document.temps.SG.value) || document.temps.SG.value.length ==0)
		document.temps.SG.value='$20,000';
		
	if 	(!IsMoney(document.temps.BB.value) || document.temps.BB.value.length ==0)
		document.temps.BB.value='$2,500';		

	if 	(!IsMoney(document.temps.MS.value) || document.temps.MS.value.length ==0)
		document.temps.MS.value='$100';

	if 	(!IsPct(document.temps.IR.value) || document.temps.IR.value.length ==0)
		document.temps.IR.value='12%';
		
	if 	(!IsNumber(document.temps.YS.value) || document.temps.YS.value.length ==0)
		document.temps.YS.value='12';		
}	

function IsMoney(val)
	{
		var number="0123456789$,.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}

function IsNumber(val)
	{
		var number="0123456789.";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	
function IsPct(val)
	{
		var number="0123456789.%,";

		for (var i=0;i<val.length;i++)
		{
			if (number.indexOf(val.charAt(i)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	

function addPct(pct) {
			pct=replacePct(pct);
			pct=parseFloat(pct);		
		if (checkForm(pct));
	{
			return (pct + '%');
		}
}
	
function checkForm(toCheck) {
	isNum = true;
	for (j = 0; j < toCheck.length; j++) {
	if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) != ".") || (toCheck.substring(j,j+1) > "9")) {
      isNum = false;
      }
    }
  if ((isNum == false) || (toCheck.length == 0) || (toCheck == null)) {
  alert("Please enter only numerical data in all fields.");
  return false;
  }
  else {
  return true;
  }
}


