/*

js_aspnetFormcalc.js

aspnetForm calculation JS

Ivan McAvinchey
02/08/2002

adapted from calculator on Volkswagen.ie

*/

function doLink1() {
 if (!(IsNumeric(document.aspnetForm.Rate.value) && IsNumeric(document.aspnetForm.NetAmount.value) && IsNumeric(document.aspnetForm.Deposit.value) && IsNumeric(document.aspnetForm.TradeIn.value))) {
       alert("You must enter an amount to be aspnetFormd."); return false;
}
Calculate();
var szURL;
var szCurrency;
var szListPrice=aspnetForm.NetAmount.value;
var szDeposit=document.aspnetForm.Deposit.value
var szTradein=document.aspnetForm.TradeIn.value
if (aspnetForm.Payback[0].checked){
	var szPayback=aspnetForm.Payback[0].value
	var szMonthlyAmount=aspnetForm.monthpay1.value
}
if (aspnetForm.Payback[1].checked){
	var szPayback=aspnetForm.Payback[1].value
	var szMonthlyAmount=aspnetForm.monthpay2.value
}
if (aspnetForm.Payback[2].checked){
	var szPayback=aspnetForm.Payback[2].value
	var szMonthlyAmount=aspnetForm.monthpay3.value
}
if (aspnetForm.Payback[3].checked){
	var szPayback=aspnetForm.Payback[3].value
	var szMonthlyAmount=aspnetForm.monthpay4.value
}
if (aspnetForm.Payback[4].checked){
	var szPayback=aspnetForm.Payback[4].value
	var szMonthlyAmount=aspnetForm.monthpay5.value
}
if (aspnetForm.PPP.checked){
	var szPPP="Include"}
	else{
	var szPPP="Exclude"}
var szFinalPrice=document.aspnetForm.NetAmount.value-document.aspnetForm.Deposit.value-document.aspnetForm.TradeIn.value

szURL = "proposalform.cfm?FinalPrice=" + szFinalPrice + "&CarSpec=&CarModel=&CarYear=2000&Username=&Currency=" + szCurrency + "&colour=&Upholstery=&OptionList=&Deposit=" + szDeposit + "&Tradein="+ szTradein + "&ListPrice=" + szListPrice + "&PPP=" + szPPP + "&Payback=" + szPayback + "&MonthlyAmount=" + szMonthlyAmount;
	
window.location.href=szURL.replace(/ /gi, "%20");
return false;
}

function doLink2() {
 if (!(IsNumeric(document.aspnetForm.Rate.value) && IsNumeric(document.aspnetForm.NetAmount.value) && IsNumeric(document.aspnetForm.Deposit.value) && IsNumeric(document.aspnetForm.TradeIn.value))) {
       alert("You must enter an amount to be aspnetFormd."); return false;
}
Calculate();
var szURL;
var szFinalPrice=document.aspnetForm.NetAmount.value-document.aspnetForm.Deposit.value-document.aspnetForm.TradeIn.value
var szDeposit=document.aspnetForm.Deposit.value
var szTradein=document.aspnetForm.TradeIn.value
var szCurrency="EU";
if (aspnetForm.Payback[0].checked){
	var szPayback=aspnetForm.Payback[0].value
	var szMonthlyAmount=aspnetForm.monthpay1.value
}
if (aspnetForm.Payback[1].checked){
	var szPayback=aspnetForm.Payback[1].value
	var szMonthlyAmount=aspnetForm.monthpay2.value
}
if (aspnetForm.Payback[2].checked){
	var szPayback=aspnetForm.Payback[2].value
	var szMonthlyAmount=aspnetForm.monthpay3.value
}
if (aspnetForm.Payback[3].checked){
	var szPayback=aspnetForm.Payback[3].value
	var szMonthlyAmount=aspnetForm.monthpay4.value
}
if (aspnetForm.Payback[4].checked){
	var szPayback=aspnetForm.Payback[4].value
	var szMonthlyAmount=aspnetForm.monthpay5.value
}
if (aspnetForm.PPP.checked){
	var szPPP="Include"}
	else{
	var szPPP="Exclude"}
var szListPrice=aspnetForm.NetAmount.value;

szURL = "proposalform2.cfm?FinalPrice=" + szFinalPrice + "&CarSpec=&CarModel=&CarYear=2000&Username=&Currency=" + szCurrency + "&colour=&Upholstery=&OptionList=&Deposit=" + szDeposit + "&Tradein="+ szTradein + "&ListPrice=" + szListPrice + "&PPP=" + szPPP + "&Payback=" + szPayback + "&MonthlyAmount=" + szMonthlyAmount;
	
window.location.href=szURL.replace(/ /gi, "%20");
return false;
}

function IsNumeric(strUserValue){
	var strValidChars="0123456789."
	var intIsNumeric=true
	for (var intCount=0 ; intCount < strUserValue.length; intCount++) {
		if(strValidChars.indexOf(strUserValue.charAt(intCount))==-1) intIsNumeric=false;
	}
	return (intIsNumeric) && (strUserValue!="");
}
	
function Calculate(){
	var szInc = "including"
	var szExc = "excluding"
	var PPPRate = 1.15;
     if (!(IsNumeric(document.aspnetForm.Rate.value) && IsNumeric(document.aspnetForm.NetAmount.value) && IsNumeric(document.aspnetForm.Deposit.value) && IsNumeric(document.aspnetForm.TradeIn.value)))
       alert("You must enter a valid number.")
    else {
       if ((document.aspnetForm.Rate.value<0) || (document.aspnetForm.Rate.value>100))
          alert("APR must be between 0 and 100")
       else{
       if ((parseInt(document.aspnetForm.NetAmount.value)<parseInt(document.aspnetForm.Deposit.value)))
          alert("The cash deposit may not be greater than the value of the car.")
       else{
	BalloonValue=parseFloat(document.aspnetForm.NetAmount.value)*parseFloat(document.aspnetForm.PcentBalloon.value/100)
	AmtaspnetFormd=parseFloat(document.aspnetForm.NetAmount.value)-parseFloat(BalloonValue)-parseFloat(document.aspnetForm.Deposit.value)-parseFloat(document.aspnetForm.TradeIn.value)
	NetCost=parseFloat(document.aspnetForm.NetAmount.value)-parseFloat(document.aspnetForm.Deposit.value)-parseFloat(document.aspnetForm.TradeIn.value)
        if (document.aspnetForm.Rate.value==0){
           MonthlyRate=0
           SubTotal1=0}
        else{
           MonthlyRate=Math.pow((1+(document.aspnetForm.Rate.value/100)),(1/12))-1
           SubTotal1=(1/MonthlyRate)}

	Periods=12
        if (document.aspnetForm.Rate.value==0){
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.Include.value=szInc
		   	document.aspnetForm.monthpay1.value=Math.round(((AmtaspnetFormd/Periods)*100)/100)*PPPRate}
		   else {
		   document.aspnetForm.Include.value=szExc
		   	document.aspnetForm.monthpay1.value=Math.round((AmtaspnetFormd/Periods)*100)/100
			}
			document.aspnetForm.finalpay1.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total1.value=AmtaspnetFormd
           document.aspnetForm.credit1.value=0}
        else{
	   SubTotal2=(1/(MonthlyRate*(Math.pow(1+MonthlyRate,Periods))));
           MonthlyaspnetFormCost1=(AmtaspnetFormd/(SubTotal1-SubTotal2))
           MonthlyaspnetFormCost2=(BalloonValue*MonthlyRate)
            if (document.aspnetForm.PPP.checked  == true) {
			document.aspnetForm.Include.value=szInc
		   document.aspnetForm.monthpay1.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*100)*PPPRate))/100}
		   else {
		   document.aspnetForm.Include.value=szExc
		   	document.aspnetForm.monthpay1.value=Math.round(((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2))*100)/100
			}
           document.aspnetForm.finalpay1.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total1.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*Periods)+BalloonValue)*100)/100
           document.aspnetForm.credit1.value=Math.round((document.aspnetForm.total1.value-NetCost)*100)/100}

	Periods=24
        if (document.aspnetForm.Rate.value==0){
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay2.value=Math.round(((AmtaspnetFormd/Periods)*100)/100)*PPPRate}
		   else {
		   document.aspnetForm.monthpay2.value=Math.round((AmtaspnetFormd/Periods)*100)/100
		   }
           document.aspnetForm.finalpay2.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total2.value=AmtaspnetFormd
           document.aspnetForm.credit2.value=0}
        else{
	   SubTotal2=(1/(MonthlyRate*(Math.pow(1+MonthlyRate,Periods))));
           MonthlyaspnetFormCost1=(AmtaspnetFormd/(SubTotal1-SubTotal2))
           MonthlyaspnetFormCost2=(BalloonValue*MonthlyRate)
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay2.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*100)*PPPRate))/100 }
		   else {
		   document.aspnetForm.monthpay2.value=Math.round(((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2))*100)/100
		   }
           document.aspnetForm.finalpay2.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total2.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*Periods)+BalloonValue)*100)/100
           document.aspnetForm.credit2.value=Math.round((document.aspnetForm.total2.value-NetCost)*100)/100}

	Periods=36
        if (document.aspnetForm.Rate.value==0){
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay3.value=Math.round(((AmtaspnetFormd/Periods)*100)/100)*PPPRate}
		   else {
		   document.aspnetForm.monthpay3.value=Math.round((AmtaspnetFormd/Periods)*100)/100
		   }
           document.aspnetForm.finalpay3.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total3.value=AmtaspnetFormd
           document.aspnetForm.credit3.value=0}
        else{
	   SubTotal2=(1/(MonthlyRate*(Math.pow(1+MonthlyRate,Periods))));
           MonthlyaspnetFormCost1=(AmtaspnetFormd/(SubTotal1-SubTotal2))
           MonthlyaspnetFormCost2=(BalloonValue*MonthlyRate)
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay3.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*100)*PPPRate))/100 }
		   else {
		   document.aspnetForm.monthpay3.value=Math.round(((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2))*100)/100
		   }
           document.aspnetForm.finalpay3.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total3.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*Periods)+BalloonValue)*100)/100
           document.aspnetForm.credit3.value=Math.round((document.aspnetForm.total3.value-NetCost)*100)/100}

	Periods=48
        if (document.aspnetForm.Rate.value==0){
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay4.value=Math.round(((AmtaspnetFormd/Periods)*100)/100)*PPPRate }
		   else {
		   document.aspnetForm.monthpay4.value=Math.round((AmtaspnetFormd/Periods)*100)/100
		   }
           document.aspnetForm.finalpay4.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total4.value=AmtaspnetFormd
           document.aspnetForm.credit4.value=0}
        else{
	   SubTotal2=(1/(MonthlyRate*(Math.pow(1+MonthlyRate,Periods))));
           MonthlyaspnetFormCost1=(AmtaspnetFormd/(SubTotal1-SubTotal2))
           MonthlyaspnetFormCost2=(BalloonValue*MonthlyRate)
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay4.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*100)*PPPRate))/100 }
		   else {
		   document.aspnetForm.monthpay4.value=Math.round(((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2))*100)/100
		   }
           document.aspnetForm.finalpay4.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total4.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*Periods)+BalloonValue)*100)/100
           document.aspnetForm.credit4.value=Math.round((document.aspnetForm.total4.value-NetCost)*100)/100}

	Periods=60
        if (document.aspnetForm.Rate.value==0){
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay5.value=Math.round(((AmtaspnetFormd/Periods)*100)/100)*PPPRate }
		   else {
		   document.aspnetForm.monthpay5.value=Math.round((AmtaspnetFormd/Periods)*100)/100 
		   }
           document.aspnetForm.finalpay5.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total5.value=AmtaspnetFormd
           document.aspnetForm.credit5.value=0}
        else{
	   SubTotal2=(1/(MonthlyRate*(Math.pow(1+MonthlyRate,Periods))));
           MonthlyaspnetFormCost1=(AmtaspnetFormd/(SubTotal1-SubTotal2))
           MonthlyaspnetFormCost2=(BalloonValue*MonthlyRate)
           if (document.aspnetForm.PPP.checked  == true) {
		   document.aspnetForm.monthpay5.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*100)*PPPRate))/100 }
		   else {
		   document.aspnetForm.monthpay5.value=Math.round(((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2))*100)/100 
		   }
           document.aspnetForm.finalpay5.value=Math.round((BalloonValue)*100)/100
           document.aspnetForm.total5.value=Math.round((((MonthlyaspnetFormCost1+MonthlyaspnetFormCost2)*Periods)+BalloonValue)*100)/100
           document.aspnetForm.credit5.value=Math.round((document.aspnetForm.total5.value-NetCost)*100)/100}
	    }
	  }
	}
}
