 
function init()
{
    calc.principal.value = "";  
    calc.interestrate.value  = "";  
    calc.months1.value = "";  
    calc.payment.value = "";    

  
}



// format floating point number to show dollars and cents
function currency2str(f)
{
    var str = "";
    var pos = 1;

    if (f < 0) // negative number
    {
        f = -f
        pos = 0;
    }  

    // extract dollars and cents
    var dollars = Math.floor(f);
    var cents   = Math.round(100*(f - dollars));
    if (cents == 100) 
    {
        dollars += 1;
        cents = 0;
    }

    if (pos == 0) str += "("; // show as negative

    if (cents == 0)
        str += dollars + ".00";
    else if (cents < 10)
        str += dollars + ".0" + cents;
    else
        str += dollars + "." + cents;

    if (pos == 0) str += ")"; // show as negative

    return str;
}

// compute the size of a periodic payment to amortize a loan
function monthly_payment( p, apr, n )
{
    var i = apr/1200;
    var m = p*i*Math.pow((1 + i), n)/(Math.pow((1 + i), n) - 1);

    return (0.01*Math.ceil(100*m));   // round up
}

// total interest and payment size for a loan
function compute1()
{
    var principal = removeCurrency(calc.principal.value);
	principal = parseFloat(principal);
    
    var int_rate  = parseFloat(calc.interestrate.value);
    var months1   = parseInt(calc.months1.value);
	months1 *= 12;
  
    var payment   = monthly_payment( principal, int_rate, months1 );
    var interest  = parseFloat("0");
	if (!principal || !int_rate || !months1)
		{
			alert("invalid data");
			return;
		}

    // compute total interest paid
    while (principal > 0.0)
    {
        var ip    = 0.01*Math.round(principal*int_rate/12);
        principal = principal + ip - payment;
        interest += ip;
    }

    calc.payment.value   = currency2str(payment);
    calc.interest1.value = currency2str(interest);
}

// interest and principal of each payment in a separate window
function listing1()
{
    var principal = removeCurrency(calc.principal.value);
	principal = parseFloat(principal);
    var int_rate  = parseFloat(calc.interestrate.value);
    var months1   = parseInt(calc.months1.value);
	months1 *= 12;
    var payment   = monthly_payment( principal, int_rate, months1 );
    var count     = parseInt("0");
	
	if (!principal || !int_rate || !months1)
		{	
			msgWindow=window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400");
	    	msgWindow.document.open();			
			msgWindow.document.writeln("<html><head><title>Invalid Data</title></head>");
			msgWindow.document.writeln("<body>");
			msgWindow.document.writeln("Please Enter Valid Data<br>");
   			return;
		}
		
    msgWindow=window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400");
    msgWindow.document.open();
    msgWindow.document.writeln("<html><head><title>Principal and interest listing</title></head>");
    msgWindow.document.writeln("<body>");
	msgWindow.document.writeln("<IMG SRC=images/set_logo.jpg><br>");   
	msgWindow.document.writeln("Creating Amortization Schedule<br>");
	msgWindow.document.writeln("This may take a few moments...<br><br>");
    msgWindow.document.writeln("Monthly Payment = $" + currency2str(payment));
    msgWindow.document.writeln("<table bordercolor=blue border=1 cellpadding = 3 cellspacing=0>");
    msgWindow.document.writeln("<tr><th width = 1%>month<th width = 35%>principal<th width = 35%>interest<th width = 45%>balance</tr>");
    while (principal > 0.0)
    {
        var ip      = 0.01 * Math.round(principal*int_rate/12);
        if (payment > principal + ip) payment = principal + ip; 
        var pp      = payment - ip;
        var principal = principal + ip - payment;
        count++;
 		
		if (count % 2 == 0)
			{
				 
				msgWindow.document.write("<tr><td>");
        		msgWindow.document.write(count);
        		msgWindow.document.write("<td   align=center>" + currency2str(pp));
        		msgWindow.document.write("<td   align=center>" + currency2str(ip));
        		msgWindow.document.write("<td  align=center>" + currency2str(principal));
        		msgWindow.document.writeln("</tr>");
			}
		else
			{
						 
				msgWindow.document.write("<tr><td>");
        		msgWindow.document.write(count);
        		msgWindow.document.write("<td bgcolor=  #F2F2F4  align=center>" + currency2str(pp));
        		msgWindow.document.write("<td bgcolor=#F2F2F4  align=center>" + currency2str(ip));
        		msgWindow.document.write("<td bgcolor=#F2F2F4 align=center>" + currency2str(principal));
        		msgWindow.document.writeln("</tr>");
			}
    }

    msgWindow.document.write("</table>");
    msgWindow.document.write("</body></html>");
    msgWindow.document.close();
}

 

function removeCurrency( strValue ) {
/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

// interest and principal of each payment in a separate window
function listing2()
{
    var principal = removeCurrency(fha_calc.mortAmt.value);
	principal = parseFloat(principal);
	var int_rate  = parseFloat(fha_calc.mortRate.value);
    var months1   = parseInt(fha_calc.numYears.value);
	months1 *= 12;
	var count     = parseInt("0");
	
	
    var payment   = monthly_payment( principal, int_rate, months1 );
    
	 
	
	if (!principal || !int_rate || !months1)
		{	
			msgWindow=window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400");
	    	msgWindow.document.open();			
			msgWindow.document.writeln("<html><head><title>Invalid Data</title></head>");
			msgWindow.document.writeln("<body>");
			msgWindow.document.writeln("Please Enter Valid Data<br>");
   			return;
		}

    msgWindow=window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400");
    msgWindow.document.open();
    msgWindow.document.writeln("<html><head><title>Principal and interest listing</title></head>");
    msgWindow.document.writeln("<body>");
	msgWindow.document.writeln("<IMG SRC=images/set_logo.jpg><br>");   
	msgWindow.document.writeln("Creating Amortization Schedule<br>");
	msgWindow.document.writeln("This may take a few moments...<br><br>");
    msgWindow.document.writeln("Monthly Payment = $" + currency2str(payment));
    msgWindow.document.writeln("<table bordercolor=blue border=1 cellpadding = 3 cellspacing=0>");
    msgWindow.document.writeln("<tr><th width = 1%>month<th width = 35%>principal<th width = 35%>interest<th width = 45%>balance</tr>");



while (principal > 0.0)
    {
        var ip      = 0.01 * Math.round(principal*int_rate/12);
        if (payment > principal + ip) payment = principal + ip; 
        var pp      = payment - ip;
        var principal = principal + ip - payment;
        count++;

		if (count % 2 == 0)
			{
				 
				msgWindow.document.write("<tr><td>");
        		msgWindow.document.write(count);
        		msgWindow.document.write("<td   align=center>" + currency2str(pp));
        		msgWindow.document.write("<td   align=center>" + currency2str(ip));
        		msgWindow.document.write("<td  align=center>" + currency2str(principal));
        		msgWindow.document.writeln("</tr>");
			}
		else
			{
						 
				msgWindow.document.write("<tr><td>");
        		msgWindow.document.write(count);
        		msgWindow.document.write("<td bgcolor=  #F2F2F4  align=center>" + currency2str(pp));
        		msgWindow.document.write("<td bgcolor=#F2F2F4  align=center>" + currency2str(ip));
        		msgWindow.document.write("<td bgcolor=#F2F2F4 align=center>" + currency2str(principal));
        		msgWindow.document.writeln("</tr>");
			}
    }

    msgWindow.document.write("</table>");
    msgWindow.document.write("</body></html>");
    msgWindow.document.close();




    
}
 
 

function rtrim(argvalue) {
  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }
  return argvalue;
}

function ltrim(argvalue) {
  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }
    return argvalue;
}
// Round a field two (2) decimals
function round(number)
{
  return Math.round(number*Math.pow(10,2))/Math.pow(10,2);
}

function trim(str)
{
     return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function count(form)
{
    if (( trim(form.mortAmt.value) != "" && trim(form.numYears.value) != "" && trim(form.propTax.value) != "" &&
        trim(form.debt.value) != "" && trim(form.mortRate.value) != ""))
		{}
			
	else
		{
		   alert('invalid or missing data');
		   return;
		}
 
{   
	
	var tmp1,tmp2, tmp3, tmp4, tmp5, tmp6 ,tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13, tmp14, tmp15
	
	tmp1 = removeCurrency(fha_calc.mortAmt.value);
	tmp1 = parseFloat(tmp1);
	
	if (isNaN(tmp1)) 
		tmp1=0;
		
	tmp2 = parseFloat(form.numYears.value);
	
	if (isNaN(tmp2)) 
		tmp2=0;
	
	tmp3 = parseFloat(form.mortRate.value);
	
	if (isNaN(tmp3)) 
		tmp3=0;
	
	tmp4 = removeCurrency(form.propTax.value);
	tmp4 = parseFloat(tmp4);

	if (isNaN(tmp4)) 
		tmp4=0;
	
	tmp5 = removeCurrency(form.debt.value);
	tmp5 = parseFloat(tmp5);

	if (isNaN(tmp5)) 
		tmp5=0;
	tmp6 = 0;

	tmp7 = tmp4/12;	 
	tmp8= tmp5/12;
 	tmp9 = tmp3/1200;
 	tmp10= tmp2 * 12;
 	tmp11 = (1 + tmp9);
	for (i=1; i < tmp10; i++) {
		tmp11 = tmp11 * (1 + tmp9);
		}
 	tmp12  = tmp1 * tmp11 * tmp9 / (tmp11 - 1);
 	tmp13 = tmp12 + tmp7 + tmp8;
 	tmp14= (tmp12 + tmp7) / 0.28;
 	tmp15 = tmp13 / 0.35;

	if (tmp14 > tmp15) {
		tmp6 = 12 * tmp14;
	} else {
		tmp6 = 12 * tmp15;
	}
	form.mortPay.value = round(tmp12);
	form.totalPay.value = round(tmp13);
	form.reqdSal.value = round(tmp6);
	} 
}