//==============	debt-to-income	============================
function debtoincome() {
	var f = document.forms[0];
	var inc = new Number(f.income.value);
	var al  = new Number(f.alimony.value);
	var oi  = new Number(f.otherincome.value);
	var ti  = inc + al + oi;
	var hs  = new Number(f.housing.value);
	var cc  = new Number(f.creditcards.value);
	var al  = new Number(f.autoloans.value);
	var oe  = new Number(f.other.value);
	var te  = hs + cc + al + oe;
	
	var ans,prb,comm;
		
	if ((ti =="") || (te == "") || (isNaN(ti)) || (isNaN(te)))
		alert('Please enter valid numbers and try again.');
	else {	
		prb = te/ti;
		ans = Math.round(100*prb);		

		f.answer.value = ans +"%";
		f.answer.focus();
	}	
}


//	=================	loanpayment	calculator	========================
function loanpayment() {
	var f = document.forms[0];
	var pv = new Number(f.pv.value);
	var n  = new Number(f.n.value);
	var i	 = new Number(f.i.value);

	var time = new Number(f.time.selectedIndex);

	var mpmt = 0;
	var tpmt = 0;
	var tint = 0;

    if (i >= 1.0) { i = i / 100;}
	i = i / 12;
	
	if ( time == 0 ){ n = n;}
	if ( time == 1 ){ n = n * 12;}


	var x = 1 + i;

   mpmt = ( pv * Math.pow(x,n) ) / ( ( ( Math.pow(x,n) - 1 ) / i ) );
   tpmt = mpmt * n;
   tint = tpmt - pv;

   f.mpmt.value = Math.round( 100 * mpmt)/ 100;
   f.tpmt.value = Math.round( 100 * tpmt)/ 100;
   f.tint.value = Math.round( 100 * tint)/ 100;

}

//	=================	saving calculator	=========================
function savings() {
	var f = document.forms[0];
	var pmt = new Number(f.pmt.value);
	var n   = new Number(f.n.value);
	var i   = new Number(f.i.value);
	var fv  = 0;
	var time = Number(f.time.selectedIndex);
    if (i >= 1.0) { i = i / 100;}
	i = i / 12;
	
	if ( time == 0 ){ n = n;}
	if ( time == 1 ){ n = n * 12;}

	var x = 1 + i;

	fv = pmt *  ( Math.pow(x,n) - 1 ) / i;

   f.fv.value = Math.round( 100 * fv)/ 100;
}

//	==========================	calculateEquity	==============================
function calculateEquity() {
	if (validateEq()) {
		var v, m1, m2, pct, e, strURL;
		var f = document.forms[0];
	
		v = parseInt(f.valueofhome.value);
		m1 = parseInt(f.firstmort.value);
		m2 = parseInt(f.secondmort.value);
		for (var i=0; i < f.percentage.length; i++)
			if (f.percentage[i].checked == true)
				pct = parseFloat(f.percentage[i].value);

		e = (v - (m1 + m2)) * pct;
		
		if (e == 0) 
			alert('You currently have no equity in your home.');
		else
			//alert('You have $' + e + ' of equity in your home.');
			f.equity.value = e;
	}
}

//	=================	validate	====================================
function validateEq() {
	var f = document.forms[0];
	
	var v = f.valueofhome.value;
	var m1 = f.firstmort.value;
	var m2 = f.secondmort.value;
	
	if (v.length < 1 || isNaN(v)) {
		alert("You must enter a valid number in the 'Value of Home' field");
		f.valueofhome.focus();
		return false;
	}
	else if (m1.length < 1 || isNaN(m1)) {
		alert("You must enter a valid number in the 'First Mortgage Balance' field");
		f.m1.focus();
		return false;
	}
	else if (m2.length < 1 || isNaN(m2)) {
		alert("You must enter a valid number in the 'Second Mortgage Balance' field");
		f.m2.focus();
		return false;
	}
	else
		return true;
}

//	===================	doubleinv	==================================
function doubleinv()	{
	var f = document.forms[0];
	var i = new Number(f.i.value);
	if ((isNaN(i)) || (i == null) || (i == 0)) { 
		i=0;
		f.y.value = 0;
		return;
	}
	y =  72/i;
   f.y.value = Math.round( 100 * y)/ 100;
}
	
	
//	===================	totalmonthlypay	================================

function totalmonthlypay()	{
	var f = document.forms[0];
	var p10 = 0;
	var p1 = new Number(f.housing.value);
	var p2 = new Number(f.auto.value);
	var p3 = new Number(f.creditcards.value);
	var p4 = new Number(f.studloan.value);
	var p5 = new Number(f.other1.value);
	var p6 = new Number(f.other2.value);
	var p7 = new Number(f.secmort.value);
	
	if (isNaN(p1))	{	p1= 0; f.housing.value = 0;	}
	if ((/\./.test(p1)) && (!/\.\d{1,2}$/.test(p1))) { alert(" House payment or Rent has too many numbers after the decimal point. "); 	}
	
	if (isNaN(p2))	{	p2= 0; f.auto.value = 0;	}
	if ((/\./.test(p2)) && (!/\.\d{1,2}$/.test(p2))) { alert(" Auto loans has too many numbers after the decimal point. "); 	}
	
	if (isNaN(p3))	{	p3= 0; f.creditcards.value = 0;	}
	if ((/\./.test(p3)) && (!/\.\d{1,2}$/.test(p3))) { alert(" Credit cards has too many numbers after the decimal point. "); 	}
	
	if (isNaN(p4))	{	p4= 0; f.studloan.value = 0;	}
	if ((/\./.test(p4)) && (!/\.\d{1,2}$/.test(p4))) { alert(" Student loan has too many numbers after the decimal point. "); 	}
	
	if (isNaN(p5))	{	p5= 0; f.other1.value = 0;	}
	if ((/\./.test(p5)) && (!/\.\d{1,2}$/.test(p5))) { alert(" Other has too many numbers after the decimal point. "); 	}
	
	if (isNaN(p6))	{	p6= 0; f.other2.value = 0;	}
	if ((/\./.test(p6)) && (!/\.\d{1,2}$/.test(p6))) { alert(" Other has too many numbers after the decimal point. "); 	}
	
	if (isNaN(p7))	{	p6= 0; f.secmort.value = 0;	}
	if ((/\./.test(p7)) && (!/\.\d{1,2}$/.test(p7))) { alert(" Second Mortgage has too many numbers after the decimal point. "); 	}
	
	with (Math) { p10 = p1 + p2 +  p3 + p4 + p5 + p6 + p7 }
	p10 = Math.round(p10);
	
	f.answer.focus();
	f.answer.value = p10;
	 
}	

//	===================	monthlyincome	================================

function grossmonthlyincome()	{
	var f = document.forms[0];	
	var p10 = 0;						// answer	
	var p9  = 0;						// total overtime pay
	var p8  = parseFloat(4.333);
	var p1  = new Number(f.hours.value);
	var p2  = new Number(f.wage.value);
	var p3  = new Number(f.overtime.value);
	
	if (isNaN(p1))	{	p1 = 0; f.hours.value = 0;		}
	if ((/\./.test(p1)) && (!/\.\d{1,2}$/.test(p1))) { alert(" Hours worked has too many numbers after the decimal point. "); 	} 
	
	if (isNaN(p2))	{	p2 = 0; f.wage.value = 0;		}
	if ((/\./.test(p2)) && (!/\.\d{1,2}$/.test(p2))) { alert(" Hourly pay has too many numbers after the decimal point. "); 	} 
	
	if (isNaN(p3)) {  p3 = 0; f.overtime.value = 0;	}
	if ((/\./.test(p3)) && (!/\.\d{1,2}$/.test(p3))) { alert(" Overtime hours has too many numbers after the decimal point. "); 	} 
	
	with (Math)		{  p10 =  p1 * p2 	}
	
	if (p3 > 0)	{ with (Math)	{ p9 = (p2 * parseInt(1.5)) * p3;	}
					  with (Math)	{	p10 = p9 + p10;	}
	}
	p10 = p10 * p8
	p10 = Math.round(p10);
	
	f.answer.focus();
	f.answer.value = p10;
}


//	===================	mortgagepayment	================================

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

function dosum() {
	var f = document.forms[0];	
	var p1 = new Number(f.LA.value);
	var p2 = new Number(f.AT.value);
	var p3 = new Number(f.AI.value); 
	var p4 = new Number(f.IR.value);
	var p5 = new Number(f.YR.value);   


	if (isNaN(p1)) {  p1 = 0; f.LA.value = 0;	}
	if (isNaN(p2)) {  p2 = 0; f.AT.value = 0;	}
	if (isNaN(p3)) {  p3 = 0; f.AI.value = 0;	}  
	if (isNaN(p4)) {  p4 = 0; f.IR.value = 0;	}
	if (isNaN(p5)) {  p5 = 0; f.YR.value = 0;	}

	var mi = p4 / 1200;
	var base = 1;
	var mbase = 1 + mi;
	for (i=0; i < p5 * 12; i++) {
		base = base * mbase;
	}
	f.PI.value = floor(p1 * mi / ( 1 - (1/base)));
	f.MT.value = floor(p2 / 12);
	f.MI.value = floor(p3 / 12);
	var dasum = p1 * mi / ( 1 - (1/base)) +	p2 / 12 + p3 / 12;
	f.MP.value = floor(dasum);
}



//	===================	auto loan	================================

function auto_loan(f) {
	var p=valid_num(f.purch.value);
	if(f.down.value.length==0) { f.down.value=0; }
	var d=valid_num(f.down.value);
	if(f.rate.value.length==0) { f.rate.value=0; }
	var rt=valid_num(f.rate.value) * .01;
	var m=valid_num(f.months.value);
	var pc=p-parseFloat(d);
	var monthly=buyMonthly(pc,rt,m);
	var cost=(monthly * m) + parseFloat(d);
	var interest = (monthly * m) - pc;
	if(d>=p) {
		alert("Down payment must be less than the loan amount.");
		f.purch.focus();
		f.answer.value = 0;
	}
	else {
		f.answer.value = (monthly == 0) ? 0 : rounding(monthly);
	}
}

function valid_num(entry) {
	var validlist = "1234567890";
	var number = "";
	var period = 1;
	if(entry.length == 0) { return 0; }
	for(var x=0; x < entry.length; x++) {
		var datum = entry.charAt(x);
		if(validlist.indexOf(datum) != -1) { number += datum; }
		else if(datum == "." && period) {
			number += datum;
			period = 0;
		}
		else { return 0; }
	}
	if(number == "") { return 0; }
	else { return(parseFloat(number)); }
}

function rounding(x) {
	var cents = Math.round(x*100);
	if (cents<10) { strCents = "0" + cents; }
	else { strCents = "" + cents; }
	var len = strCents.length;
	var y = strCents.substring(0, len - 2) + "." + strCents.substring((len - 2), len);
	var ylen = y.length;
	if(x>999) { y = y.substring(0, ylen - 6) + "," + y.substring((ylen - 6), ylen); }
	ylen = y.length;
	if(x>999999) { y = y.substring(0, ylen - 10) + "," + y.substring((ylen - 10), ylen); }
	return y;
}

function buyMonthly(pc,rt,m) {
	var b;
	if(pc==0 || m==0) { b=0; }
	else {	
		rt = rt/12;
		if(rt==0) { b = pc/m; }
		else { b = (pc*rt)/(1-Math.pow((1+rt),(-1*m))); }
	}
	return b;
}
