/* Js Document */
function formatNum(Vnum) {

   if(Vnum > 99000000) {
   		alert("Sorry, this will not generate numbers larger that 99 million.");
		focus();
   } else {
  		 var V10million = parseInt(Vnum / 10000000);

   		var V1million = (Vnum % 10000000)  / 1000000;
     	 if(V1million / 1000000 == 1) {
     		 V1million = 1;
      	}else if(V1million < 1){
     		 V1million = "0";
    	}else{
     		V1million = parseInt(V1million,10);
     	}
		
   		var V100thousand = (Vnum % 1000000)  / 100000;
      	if(V100thousand / 100000 == 1) {
     		 V100thousand = 1;
      	} else if(V100thousand < 1) {
      		V100thousand = "0";
     	} else {
      		V100thousand = parseInt(V100thousand,10);
     	}
		
  		var V10thousand = (Vnum % 100000)  / 10000;
     	if(V10thousand / 10000 == 1) {
      		V10thousand = 1;
      	} else if(V10thousand < 1) {
      		V10thousand = "0";
      	} else {
     		V10thousand = parseInt(V10thousand,10);
     	}
		
	    var V1thousand = (Vnum % 10000)  / 1000;
	    if(V1thousand / 1000 == 1) {
	 		 V1thousand = 1;
	 	} else if(V1thousand < 1) {
	  		V1thousand = "0";
	 	} else {
	 		 V1thousand = parseInt(V1thousand,10);
	 	}

	    var Vhundreds = (Vnum % 1000)  / 100;
		if(Vhundreds / 100 == 1) {
		 	 Vhundreds = 1;
		} else if(Vhundreds < 1) {
		  	Vhundreds = "0";
		} else {
		  	Vhundreds = parseInt(Vhundreds,10);
		}
	
	   var Vtens = (Vnum % 100)  / 10;
		if(Vtens / 10 == 1) {
		  	Vtens = 1;
		} else if(Vtens < 1) {
		 	 Vtens = "0";
		} else {
		  	Vtens = parseInt(Vtens,10);
		}
	
	   var Vones = (Vnum % 10)  / 1;
		if(Vones / 1 == 1) {
		  Vones = 1;
		  } else if(Vones < 1) {
		  Vones = "0";
		 } else {
		  Vones = parseInt(Vones,10);
		 }
	
	  //START UPGRADE
		var Vcents = 0;
		if(Vnum % 1 * 100 < 1) {
		   Vcents = 0;
		   } else {
		   Vcents = parseInt(((eval(Vnum % 1) * 100)),10);
		   }
		   
		//END UPGRADE
		 if(Vcents < 1) {
		  Vcents = "00";
		  }
		  else if(Vcents % 10 == 0) {
		  Vcents = Vcents + "0";
		  }
		  else if(Vcents % 10 == Vcents) {
		  Vcents = "0" + Vcents;
		  } else {
		  Vcents = Vcents;
		  }
		
		  if(Vcents == "900") {
			Vcents = "90";
		  } else  if(Vcents == "800") {
			 Vcents = "80";
		  } else  if(Vcents == "700") {
			 Vcents = "70";
		  } else  if(Vcents == "600") {
			Vcents = "60";
		  } else  if(Vcents == "500") {
			 Vcents = "50";
		  } else if(Vcents == "400") {
			  Vcents = "40";
		  } else  if(Vcents == "300") {
			 Vcents = "30";
		  } else if(Vcents == "200") {
			 Vcents = "20";
		  } else if(Vcents == "100") {
			 Vcents = "10";
		  } else {
			Vcents = Vcents;
		  }
		
		   
		   var Vformat = "";
		
		   if(Vnum >= 10000000) {
				 Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 1000000) {
				 Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 100000) {
				 Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 10000) {
				 Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 1000) {
				 Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 100) {
				 Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 10) {
				Vformat = (Vtens + "" + Vones + "." + Vcents);
		   }else if(Vnum >= 1) {
				 Vformat = (Vones + "." + Vcents);
		   } else {
				 Vformat = ("0." + Vcents);
		   }	
	  	return Vformat;
	
	 }
}

function computeForm1(form) {

if(form.closingCost.value == "") {
	 alert("Please enter the closing cost percentage points.");
    } 
 else
  {
    	
     var mpmt = replaceAll(form.MonthlyPayment.value,',','');
	 mpmt = replaceAll(mpmt,'$','');
	 mpmt = eval(mpmt);
	
	 var mpmt2 = replaceAll(form.MonthlyPayment2.value,',','');
	 mpmt2 = replaceAll(mpmt2,'$','');
	 mpmt2 = eval(mpmt2);
	
	 var mpmt3 = replaceAll(form.MonthlyPayment3.value,',','');
	 mpmt3 = replaceAll(mpmt3,'$','');
	 mpmt3 = eval(mpmt3);
	 
	 var mpmt4 = replaceAll(form.MonthlyPayment4.value,',','');
	 mpmt4 = replaceAll(mpmt4,'$','');
	 mpmt4 = eval(mpmt4);
	 
	 var mpmt5 = replaceAll(form.MonthlyPayment5.value,',','');
	 mpmt5 = replaceAll(mpmt5,'$','');
	 mpmt5 = eval(mpmt5);

    var pmt1 = mpmt + mpmt2 + mpmt3 + mpmt4 + mpmt5;
	

	var ao = replaceAll(form.AmountOwed.value,',','');
	 ao = replaceAll(ao,'$','');
	 ao = eval(ao);
	 
	 var ao2 = replaceAll(form.AmountOwed2.value,',','');
	 ao2 = replaceAll(ao2,'$','');
	 ao2 = eval(ao);
	 
	 var ao3 = replaceAll(form.AmountOwed3.value,',','');
	 ao3 = replaceAll(ao3,'$','');
	 ao3 = eval(ao3);
	 
	 var ao4 = replaceAll(form.AmountOwed4.value,',','');
	 ao4 = replaceAll(ao4,'$','');
	 ao4 = eval(ao4);
	 
	 var ao5 = replaceAll(form.AmountOwed5.value,',','');
	 ao5 = replaceAll(ao5,'$','');
	 ao5 = eval(ao5);
	 
    var prin = ao + ao2 + ao3 + ao4 + ao5;

    var prin1 = prin;
	
    var closeCostAmt = 0;

    //if(form.ptsDol.selectedIndex == 0) {
	if(eval(form.closingCost.value) > 0)
   		 var closeCostPerc = eval(form.closingCost.value) / 100;
	else 
		 var closeCostPerc = 1;
      closeCostAmt = closeCostPerc * prin;

    var intPort1 = 0;
    var prinPort1 = 0;
    var accumPrin1 = 0;
    var i2 = form.intRate2.value;
       if (i2 > 1.0) {
           i2 = i2 / 100.0;
        var sumi2 = i2;   
       }
    var i2  = i2  / 12;
    var count1 = 0;
    var prin2 = 0;
    prin2 = eval(prin) + eval(closeCostAmt);
    var pow = 1;
    for (var j = 0; j < form.nper2.value *12; j++)
        pow = pow * (1 + i2);
    var fpayment2 = (prin2 * pow * i2) / (pow - 1);
    fpaymentfinal = (parseFloat(Math.round(parseFloat(fpayment2) * 100)) / 100.0)
    form.payment2.value = currency(fpaymentfinal);
   var fmoSave = (mpmt + mpmt2 + mpmt3 + mpmt4 + mpmt5) - fpaymentfinal;
   fmosavefinal = (parseFloat(Math.round(parseFloat(fmoSave) * 100)) / 100.0)
   form.moSave.value = currency(fmosavefinal);
    var prin3 = prin2;
    var prin4 = prin;
    var intPort3 = 0;
    var intPort4 = 0;
    var prinPort3 = 0;
    var prinPort4 = 0;
    var accumInt3 = 0;
    var accumInt4 = 0;
    var accumPrin3 = 0;
    var accumPrin4 = 0;
    var count3 = parseInt(closeCostAmt/fmoSave);
    if(count3 > 0)
		form.closeMo.value = count3;
	else
		form.closeMo.value = 0;
   }
}


function computeForm(form) {
if(form.closingCost.value == "") {
 	alert("Please enter the closing cost percentage points.");
} else{
    var mpmt = replaceAll(form.MonthlyPayment.value,',','');
	mpmt = replaceAll(mpmt,'$','');
	mpmt = eval(mpmt);
	
	 var mpmt2 = replaceAll(form.MonthlyPayment2.value,',','');
	 mpmt2 = replaceAll(mpmt2,'$','');
	 mpmt2 = eval(mpmt2);
	
	 var mpmt3 = replaceAll(form.MonthlyPayment3.value,',','');
	 mpmt3 = replaceAll(mpmt3,'$','');
	 mpmt3 = eval(mpmt3);
	 
	 var mpmt4 = replaceAll(form.MonthlyPayment4.value,',','');
	 mpmt4 = replaceAll(mpmt4,'$','');
	 mpmt4 = eval(mpmt4);
	 
	 var mpmt5 = replaceAll(form.MonthlyPayment5.value,',','');
	 mpmt5 = replaceAll(mpmt5,'$','');
	 mpmt5 = eval(mpmt5);

    var pmt1 = mpmt + mpmt2 + mpmt3 + mpmt4 + mpmt5;
	
    var ao = replaceAll(form.AmountOwed.value,',','');
	 ao = replaceAll(ao,'$','');
	 ao = eval(ao);
	 
	 var ao2 = replaceAll(form.AmountOwed2.value,',','');
	 ao2 = replaceAll(ao2,'$','');
	 ao2 = eval(ao2);
	 
	 var ao3 = replaceAll(form.AmountOwed3.value,',','');
	 ao3 = replaceAll(ao3,'$','');
	 ao3 = eval(ao3);
	 
	 var ao4 = replaceAll(form.AmountOwed4.value,',','');
	 ao4 = replaceAll(ao4,'$','');
	 ao4 = eval(ao4);
	 
	 var ao5 = replaceAll(form.AmountOwed5.value,',','');
	 ao5 = replaceAll(ao5,'$','');

	 ao5 = eval(ao5);
     var prin = ao + ao2 + ao3 + ao4 + ao5;

    var prin1 = prin;

    var closeCostAmt = 0;
	if(eval(form.closingCost.value) > 0){
    	var closeCostPerc = eval(form.closingCost.value) / 100;
	}else { 
		var closeCostPerc =  0;
	}
      closeCostAmt = closeCostPerc * prin;
    var intPort1 = 0;
    var prinPort1 = 0;
    var accumPrin1 = 0;
    var i2 = form.intRate2.value;
       if (i2 > 1.0) {
           i2 = i2 / 100.0;
        var sumi2 = i2;   
       }
       var i2  = i2  / 12;
    var count1 = 0;
    var prin2 = 0;
    prin2 = eval(prin) + eval(closeCostAmt);
    var pow = 1;
    for (var j = 0; j < form.nper2.value *12; j++)
        pow = pow * (1 + i2);
    var fpayment2 = (prin2 * pow * i2) / (pow - 1);
    fpaymentfinal = (parseFloat(Math.round(parseFloat(fpayment2) * 100)) / 100.0)
    form.payment2.value = "$" + fpaymentfinal;
   var fmoSave = (mpmt + mpmt2 + mpmt3 + mpmt4 + mpmt5) - fpaymentfinal;
   fmosavefinal = (parseFloat(Math.round(parseFloat(fmoSave) * 100)) / 100.0)
   form.moSave.value = "$" + fmosavefinal;
    var prin3 = prin2;
    var prin4 = prin;
    var intPort3 = 0;
    var intPort4 = 0;
    var prinPort3 = 0;
    var prinPort4 = 0;
    var accumInt3 = 0;
    var accumInt4 = 0;
    var accumPrin3 = 0;
    var accumPrin4 = 0;
    var count3 = parseInt(closeCostAmt/fmoSave);
    if(count3 > 0)
		form.closeMo.value = count3;
	else
		form.closeMo.value = 0;
   }
                
}

function clearForm(form){
    form.principal.value = "";
    form.payment.value = "";
    form.intRate.value = "";
    form.intRate2.value = "";
    form.nper2.value = "";
    form.closingCost.value = "";
    form.payment2.value = "";
    form.moSave.value = "";
    form.closeMo.value = "";
    form.summary.value = "";
}

function currency(pNum) {
  //-- Returns passed number as string in $xxx,xxx.xx format.
	var tRtnValue = "";
	if (pNum != "") {
		var n = pNum.toString().replace(/\$|\,/g,'');
		if (isNaN(n)) {n = "0";}
		var tSign = (n == (n = Math.abs(n)));
		n = Math.floor(n * 100 + 0.50000000001);
		var tCents = n % 100;
		if (tCents < 10) {tCents = "0" + tCents;}
		n = Math.floor(n / 100).toString();
		for (var i = 0; i < Math.floor((n.length - (1 + i)) / 3); i++) {
			n = n.substring(0, n.length - (4 * i + 3)) + ',' + n.substring(n.length - (4 * i + 3));
		}
		tRtnValue = (((tSign)?'':'-') + '$' + n + '.' + tCents);
	}else{
		tRtnValue = 0;
	}
	return tRtnValue;
}

function valid_number(string) {
var sTmp = "";
var sValid = "0123456789.";

for (var i=0; i< string.length; i++) {
 if (sValid.indexOf(string.charAt(i)) != -1 )  
 	sTmp += string.charAt(i);
	}
return(sTmp)
}

function replaceAll(oldStr,findStr,repStr) {
  var srchNdx = 0;  
  var newStr = ""; 
  while (oldStr.indexOf(findStr,srchNdx) != -1)  							
  {
	newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));							
	newStr += repStr;
	srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);           
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);          
  return newStr;
}

function perform(){
	var newAmount = replaceAll(document.getElementById('amount').value,',','');
	newAmount = replaceAll(newAmount,'$','');
	getAmortization(newAmount,document.getElementById('term').value,document.getElementById('apr').value,document.getElementById('year').value,document.getElementById('month').value)
}



