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


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

function computePay(form){ //this function calculates monthly payment (value of PI)
	basicedit();
	
	var IR = replacePct(document.temps.IR.value);	
	
	if (IR==0)	
	{
		alert("Sorry, the Interest Rate must be greater than zero.");	
		document.temps.IR.value='7.5%';
		document.temps.IR.focus();	
		document.temps.IR.select();			
		IR = replacePct(document.temps.IR.value);	
	}	
				
	var mi = IR / 1200;
	var base = 1;
	var mbase = 1 + mi;
	var OL = document.temps.OL.value;
			
	var loanA = replaceChars(document.temps.LA.value);
	var loanA = replaceChar(loanA);	
	if ( checkForm(IR) && checkForm(loanA) && checkForm(document.temps.OL.value));
{
	for (i=0; i< OL * 12; i++)  
    base = base * mbase
  }		
	PI = floor(loanA * mi / ( 1 - (1/base)));
	document.temps.PI.value = formatCurrency(PI);
	
}

function computeMonth(form){  //this function calculates years remaining on loan (value of OL)
	basicedit();

	var IR = replacePct(document.temps.IR.value);
	
	if (IR==0)	
	{
		alert("Sorry, the Interest Rate must be greater than zero.");	
		document.temps.IR.value='7.5%';
		document.temps.IR.focus();	
		document.temps.IR.select();			
		IR = replacePct(document.temps.IR.value);	
	}	
		
	var mi = IR / 1200;
	var PI = replaceChars(document.temps.PI.value);
	var PI = replaceChar(PI);
	var loanA = replaceChars(document.temps.LA.value);
	var loanA = replaceChar(loanA);
	if ( checkForm(IR) && checkForm(loanA) && checkForm(PI));
	var OR = (Math.log((PI) / (PI - mi * loanA)) / Math.log(1 + mi));
	var OL = parseInt(OR)/12;
	document.temps.OL.value = Math.round(OL*10)/10;
	{
	computeForm(form);
	}
	
}

function computeForm(form){	
	basicedit();
			
	var OL = document.temps.OL.value;	
	var OL1 = OL;
	var loanA = replaceChars(document.temps.LA.value);
	var loanA = replaceChar(loanA);
	var PI = replaceChars(document.temps.PI.value);
	var PI = replaceChar(PI);
	var IR = replacePct(document.temps.IR.value);
	
	if (IR==0)	
	{
		alert("Sorry, the Interest Rate must be greater than zero.");	
		document.temps.IR.value='7.5%';
		document.temps.IR.focus();	
		document.temps.IR.select();			
		IR = replacePct(document.temps.IR.value);	
	}		
	
	if (checkForm(IR) && checkForm(loanA) && checkForm(PI) && checkForm(OL));
	PI2 = parseInt(PI) + parseInt(PI/12);
	var mi = IR / 1200;	
	var OP = (Math.log((PI2) / (PI2 - mi * loanA)) / Math.log(1 + mi));
	NP = parseInt(OP)/12;
	document.temps.NP.value = Math.round(NP*10)/10;
	var tp = parseInt(OL1 * 12 * PI);
	
	document.temps.tp.value = formatCurrency(tp);
	var tp2 = parseInt(NP * 12 * PI2);
	document.temps.tp2.value = formatCurrency(tp2);
	ti = parseInt(tp - loanA);
	ti2 = parseInt(tp2 - loanA);
	document.temps.TI.value = formatCurrency((ti) - (ti2));
}

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 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 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.LA.value) || document.temps.LA.value.length ==0)
		document.temps.LA.value='$125,000';
		
	if 	(!IsPct(document.temps.IR.value) || document.temps.IR.value.length ==0)
		document.temps.IR.value='7.5%';
		
	if 	(!IsNumber(document.temps.OL.value) || document.temps.OL.value.length ==0)
	{
		document.temps.OL.value='25';	
		computePay(this.form);			
	}
		
	if 	(!IsMoney(document.temps.PI.value) || document.temps.PI.value.length ==0)
	{
		document.temps.PI.value='$900';					
		computeMonth(this.form);		
	}
}

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) { // this function performs a validation that only allows numbers, periods and commas to pass
	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;
  }
}


