
<!--
var flag = 0;
var calculated = 0;

function Fn (x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue)
{
	var y;
//	y = LoanAmountValue + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow (1.0 + clci, -x)) / clci);
//	y = LoanAmountValue + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow (1.0 + clci, -x)) / clci);
	y = (1 * LoanAmountValue) + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow (1.0 + clci, -x)) / clci);
    y = (y * 1) + clcfv * Math.pow (1.0 + clci, -x);
    return y;
}

function Fi (x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue)
{
    var y;
    y = (LoanAmountValue * 1) + ((1 + (x * clcs)) * clcpmt * (1 - Math.pow (1.0 + x, -clcn)) / x);
	y = (y * 1) + clcfv * Math.pow (1.0 + x, -clcn);
    return y;
}

function Fpmt (x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue)
{
    var y;
	y = 0;
//	y = LoanAmountValue + ((1 + (clci * clcs)) * x * (1 - Math.pow (1.0 + clci, -clcn)) / clci);
	y = ((1 + (clci * clcs)) * (1 - Math.pow (1.0 + clci, -clcn)) / clci); 
	y = y * x;
 	y = (y + (LoanAmountValue * 1));
    y = (y * 1) + (clcfv * Math.pow (1.0 + clci, -clcn));
    return y;
}

function Ffv (x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue)
{
    var y;
	y = (LoanAmountValue * 1) + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow (1.0 + clci, -clcn)) / clci);
    y = (y * 1) + x * Math.pow (1.0 + clci, -clcn);
    return y;
}

function Fpv (x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue)
{
    var y;
    y = (x * 1) + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow (1.0 + clci, -clcn)) / clci);
    y = (y * 1) + clcfv * Math.pow (1.0 + clci, -clcn);
    return y;
}

function Calculate(i_type, sval, ev, iv, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue)
{
    var d_temp, d_temp2, eval, val1, val2;
	if (i_type == 0) // Number of Payments
	{
		while (true)
        {
			val1 = Fn((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = sval * val1;
            val2 = Fn(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = d_temp - (sval+ev)*val2;
            d_temp2 = val1 - val2;
            d_temp = d_temp/d_temp2;
            eval = d_temp;
            if ( (Math.abs(eval) > ev) && (++iv < 100) )
				sval = eval;
            else
				break;
		}
		return eval;
	}
	if (i_type == 1) // Interest
	{
		while (true)
		{
			val1 = Fi((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
			d_temp = sval * val1;
			val2 = Fi(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
			d_temp = d_temp - (sval+ev)*val2;
			d_temp2 = val1 - val2;
			d_temp = d_temp/d_temp2;
			eval = d_temp;
			if ( (Math.abs(eval) > ev) && (++iv < 100) )
				sval = eval;
			else
                break;
		}
		return eval;
	}
    if (i_type == 2) // Payment
    {
		while (true)
        {
			val1 = Fpmt((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = sval * val1;
            val2 = Fpmt(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = d_temp - (sval+ev)*val2;
		//	alert ("d_temp");
		//	alert (d_temp);
            d_temp2 = val1 - val2;
		//	alert (d_temp2);
            d_temp = d_temp/d_temp2;
            eval = d_temp;
            if ( (Math.abs(eval) > ev) && (++iv < 100) )
				sval = eval;
			else
				break;
        }
		return eval;
	}
    if (i_type == 3) // Present Value
    {
		while (true)
        {
			val1 = Fpv((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = sval * val1;
            val2 = Fpv(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = d_temp - (sval+ev)*val2;
            d_temp2 = val1 - val2;
            d_temp = d_temp/d_temp2;
            eval = d_temp;
            if ( (Math.abs(eval) > ev) && (++iv < 100) )
				sval = eval;
            else
                break;
		}
		return eval;
	}
    if (i_type == 4) //Future Value
    {
		while (true)
        {
			val1 = Ffv((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = sval * val1;
            val2 = Ffv(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
            d_temp = d_temp - (sval+ev)*val2;
            d_temp2 = val1 - val2;
            d_temp = d_temp/d_temp2;
            eval = d_temp;
            if ( (Math.abs(eval) > ev) && (++iv < 100) )
				sval = eval;
			else
				break;
		}
		return eval;
	}
}



function SetitemHilighted(field)
{
	//field.select();
}



function HelpNdx_onChange()
{
//      form.help1.rowset.applyLocate("HLP_INDEX='" + form.HelpNdx.value + "'");
//      form.HTMLHelpArea.text = form.help1.rowset.fields["TEXT"].value;
}

function validation (numb, field)
{
	dec_cnt = 0;
   for (var i = 0; i < numb.length; i++)
   {
       ch = numb.charAt(i)
       if (ch == '.')
	   {
			dec_cnt++;
	   }
		   
       if (((ch < "0" || "9" < ch) && ch != '.') || dec_cnt > 1)
       {

          alert("That's not a valid price....try again!");
		  field.focus();
		  return 0;
	   }
   }
   return numb;
}

function rvalidat(form)
{
	perc = (form.basevalue.value / (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value))) * 100 ;
	if (form.TextR.value > perc)
	{
		alert("That much residual....you must be kidding!! let's try again..");
		form.TextR.selectedIndex = 0;
		form.TextValue.value = 0;
	}
}

function strngformat (numb)	
{
	numb = Math.round(numb * 100) / 100 + 0.0001;
    numstring = "" + numb;
    index = numstring.lastIndexOf(".");
    numb = numstring.substring(0, index + 3);
	if (numb == 0.00) 
	{
	   return 0;
	} else {
		return numb;
	}

}

function vehiclepriceOnChange(form)
{
   flag = 1;
   form.vehicleprice.value = validation(form.vehicleprice.value, form.vehicleprice);
   final_validation(form)
  
   if ((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) < -(-form.deposit.value - form.trade_in_value.value))
   {
	    form.deposit.value = 0;
	    form.depositP.selectedIndex = 0;
	    form.trade_in_value.value = 0;
   };


   if (form.Buyertype.value == 1)
   {  	
   		alert('A non-tax based individual must put down a minimum of a 10% deposit of the value of the car+ the extra’s. The installment will be calculated using this minimum value')
		form.deposit.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) / 100 * 10);
		form.depositP.selectedIndex = 1;
   };
   
   
  form.vehicleprice.value = strngformat(form.vehicleprice.value);
  
  
   form.basevalue.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - 
      form.deposit.value - form.trade_in_value.value);
  form.TextValue.value = strngformat(((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * form.TextR.value) / 100);
 rvalidat(form);
 
 if (calculated == 1)
 {
 Calculate1(form)
 }

 }
 
 function extraspriceOnChange(form)
{
   flag = 1;
   form.extrasprice.value = validation(form.extrasprice.value, form.extrasprice);  
 
   form.extrasprice.value = strngformat(form.extrasprice.value);
   
   if (form.Buyertype.value == 1)
   {  	
   	//alert('A non-tax based individual must put down a minimum of a 10% deposit of the value of the car+ the extra’s. The installment will be calculated using this minimum value')
	form.deposit.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) / 100 * 10);
	form.depositP.selectedIndex = 1;
   };  
  
   form.basevalue.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - 
   form.deposit.value - form.trade_in_value.value);   
   rvalidat(form);
   
 if (calculated == 1)
 {
 Calculate1(form)
 }

 }

function buyertypeOnChange(form)
{
  			
   flag = 1;
   
   // if (form.Buyertype.value == 1)
   // {
   //	alert("A non-tax based individual must put down a minimum of a 10% deposit of the value of the car+ the extra’s. The installment will be calculated using this minimum value")
   // }
         
    if (form.Buyertype.value != 1)
    {
   	alert("You do not require a deposit as a tax-based user")
    }
    
if (form.vehicleprice.value > 0)
{
	   temp = form.deposit.value / (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * 100;
   
		  if (form.Buyertype.value == 1 && temp < 10)
  	{
  			form.deposit.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) / 10);
	}

depositonChange(form);
}
   
if (form.term.value == 60)
{ form.term.value = 54;};   
   
   
form.TextR.selectedIndex = 0;
form.TextValue.value = 0;  

	
rvalidat(form);

 }

 
function TextRonChange(form)
{
   flag = 1;
   
   
 if (form.Buyertype.value == 1 )
  	{	
			alert("No Residual allowed for Personal buyers")
			form.TextR.selectedIndex = 0;
			form.TextValue.value = 0;  
	}

	form.TextR.value = validation(form.TextR.value, form.TextR);
	if (form.TextR.value > 100)
	{
		alert("Nope, we're looking for a percentage here...something smaller than 100.");
		form.TextR.focus();
	}
	while (form.TextR.value > 100) 
		form.TextR.value /= 10;

	perc = (form.basevalue.value / (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value))) * 100 ;
	if ((form.TextR.value * 1) > (perc * 1))
	{
		alert("The residual you entered is too high..please try again");
		form.TextR.selectedIndex = 0;
		form.TextR.focus();
	}
   form.TextValue.value = strngformat(((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * form.TextR.value) / 100);
   
   if (calculated == 1)
   {
   Calculate1(form)
   }

}

//function selectPmtFreq_onChange()
//{
//	flag = 1;
//}

function TextValueonChange(form)
{
	flag = 1;

 
 if (form.Buyertype.value == 1 )
  	{	
			alert("No Residual allowed for Personal buyers")
			form.TextR.selectedIndex = 0;
			form.TextValue.value = 0;  
	}
      


	form.TextValue.value = validation(form.TextValue.value, form.TextValue);
	if ((form.TextValue.value * 1) > ((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * 1))
	{
		alert("The residual you entered is too high..please try again");
		form.TextValue.value = 0;
		form.TextValue.focus();
	}

	if ((form.TextValue.value * 1) > (form.basevalue.value * 1))
	{
		alert("The residual you entered is too high..please try again.");
		form.TextValue.value = 0;
		form.TextValue.focus();
	}

	form.TextR.selectedIndex = 0;
	 if (form.Buyertype.value == 1 )
  	{	

			form.TextR.selectedIndex = 0;
			form.TextValue.value = 0;
	}
	
	if (calculated == 1)
 	{
 	Calculate1(form)
 	}
}

function iratenterestonChange(form)
{
	flag = 1;
	form.irate.value = validation(form.irate.value, form.irate);
	if (form.irate.value > 100)
	{
		alert("We're looking for a percentage here You entered a number greater than 100.");
		form.irate.focus();
	}
	while (form.irate.value > 100) 
		form.irate.value /= 10;		
}


function irateOnChange(form)
{
	flag = 1;
	form.irate.value = validation(form.irate.value, form.irate);
	if (form.irate.value > 100)
	{
		alert("We're looking fo a percentage here You entered a number greater than 100.");
		form.irate.focus();
	}
	while (form.irate.value > 100) 
	form.irate.value /= 10;		
 	rvalidat(form);
 	
 	if (calculated == 1)
 	{
 	Calculate1(form)
 	}
}


function termOnChange(form)
{
	flag = 1;

   if (form.Buyertype.value == 1)
   {
		if (form.term.value == 60)
		{ 
			   	alert("Maximum of 54 months allowed for individuals")	
		form.term.value = 54;
		};   
   };


	form.term.value = validation(form.term.value, form.term);
	if (form.term.value > 60)
	{
		alert("Term cannot be greater than 60 months.");
		form.term.value = 60;
	}

	thisstr = "" + form.term.value;
    if (thisstr.lastIndexOf(".") > -1)
	{
		alert("The Term can only be full months, ie: 30 months NOT 30,5 months.");
		form.term.value = 0;
		form.term.focus();
		form.term.select();
		return;
	}
	
	if (calculated == 1)
 	{
 	Calculate1(form)
 	}
 		
	return;

}

function TextLoanAmtonChange(form)
{
	flag = 1;

   form.basevalue.value = validation(form.basevalue.value, form.basevalue);

	if (form.basevalue.value < -(-form.deposit.value - form.trade_in_value.value))
	{
		form.deposit.value = 0;
		form.trade_in_value.value = 0;
	}

	form.vehicleprice.value = strngformat(-(-form.basevalue.value - form.deposit.value - form.trade_in_value.value));
	form.extrasprice.value = 0
	form.TextValue.value = strngformat(((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * form.TextR.value) / 100);
	form.basevalue.value = strngformat(form.basevalue.value);
    rvalidat(form);
    
    if (calculated == 1)
 	{
 	Calculate1(form)
 	}
}

function depositonChange(form)
{
	flag = 1;

   form.deposit.value = validation(form.deposit.value, form.deposit);

   if ((form.deposit.value * 1) > ((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * 1))
   {
	   alert("Oops - Your deposit is more than the price of the vehicle - try again");
   	   form.deposit.value = (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value));
	   form.deposit.focus();
   };

   if (form.deposit.value > ((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - form.trade_in_value.value))
   {
	   form.trade_in_value.value = 0;
   };



   temp = form.deposit.value / (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * 100;
   
		  if (form.Buyertype.value == 1 && temp < 10)
  	{	
			alert("A non-tax based individual must put down a minimum of a 10% deposit of the value of the car+ the extra’s. The installment will be calculated using this minimum value")
  			form.deposit.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) / 10);
  			form.depositP.selectedIndex = 1  			
	};



 form.basevalue.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - 
 form.deposit.value - form.trade_in_value.value);
 form.deposit.value = strngformat(form.deposit.value);
 if (form.vehicleprice.value > 0)
 {
 form.depositP.selectedIndex = 0;
 }
 rvalidat(form);
 
 if (calculated == 1)
 {
 Calculate1(form)
 }

}

function depositPonChange(form)
{
   flag = 1;   
   
	form.depositP.value = validation(form.depositP.value, form.depositP);	
	
	if (form.Buyertype.value == 1 && (form.depositP.value * 1) < 10)
  	{	
			alert("A non-tax based individual must put down a minimum of a 10% deposit of the value of the car+ the extra’s. The installment will be calculated using this minimum value")
  			form.deposit.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) / 10);
  			form.depositP.selectedIndex = 1  			
	};

	while (form.depositP.value > 100) 
		form.depositP.value /= 10;

	perc = (form.basevalue.value / (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value))) * 100 ;
	if ((form.depositP.value * 1) > (perc * 1))
	{
		alert("The Deposit Percentage you entered is too high..please try again");
		form.depositP.selectedIndex = 1	;
		form.depositP.focus();
	}
   form.deposit.value = strngformat(((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) * form.depositP.value) / 100);
   form.basevalue.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - form.deposit.value - form.trade_in_value.value);
   
   if (calculated == 1)
   {
   Calculate1(form)
   }
}


function trade_in_valueonChange(form)
{
	flag = 1;
   form.trade_in_value.value = validation(form.trade_in_value.value, form.trade_in_value);

   if (form.deposit.value > ((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - form.trade_in_value.value))
   {
	   alert("Nope, the deposit plus trade in value is more than the new price of the vehicle, you don't need finance!!!");
   	   form.trade_in_value.value = (parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - form.deposit.value;
	   form.trade_in_value.focus();
   }

   form.basevalue.value = strngformat((parseInt(form.vehicleprice.value) + parseInt(form.extrasprice.value)) - 
      form.deposit.value - form.trade_in_value.value);
   form.trade_in_value.value = strngformat(form.trade_in_value.value);
   rvalidat(form);
 
 if (calculated == 1)
 {
 Calculate1(form)
 }
 
}

function Clear(form)
{
   flag = 0;
   form.vehicleprice.value = 0.00;
   form.extrasprice.value = 0.00;
   form.deposit.value = 0;
   form.depositP.selectedIndex = 0;
   form.trade_in_value.value = 0;
   form.basevalue.value = 0.00;
   form.irate.value = 16;
   form.TextR.selectedIndex = 0;
   form.TextValue.value = 0;
   form.term.value = 48;
   form.instalment.value = 0;   
   calculated = 0;
}

function final_validation(form)
{

if (form.vehicleprice.value < 10000 || form.vehicleprice.value > 10000000)
  {  
  flag == 0
  alert('Please enter a price between 10000 and 10000000')
  form.vehicleprice.value = 0
  }  
 
  if (form.extrasprice.value < 1)
  {
  form.extrasprice.value = 0
  }  
  if (form.trade_in_value.value < 1)
  {
  form.trade_in_value.value = 0
  }
  if (form.TextR.value < 1)
  {
  form.TextR.selectedIndex = 0
  }
  if (form.TextValue.value < 1)
  {
  form.TextValue.value = 0
  }
  if (form.basevalue.value < 1)
  {
  form.basevalue.value = 0
  }  
  if (form.deposit.value < 1)
  {
  form.deposit.value = 0
  }  
}

function Calculate1(form)
{
  
   final_validation(form)

   if (flag == 1 || flag == 0)
   {

	var value, d_temp, d_temp2;
	ev = Math.pow(10, (-6));
	clcpv = tmppv = form.vehicleprice.value;
	clcz = ((form.irate.value/100)/360)*30;
	clcpv = clcpv + ( clcpv * clcz );
	frequency = 12;
	clci = form.irate.value/100/frequency;
	clcn = form.term.value * frequency / 12;
	
	clcnstr = "" + clcn;
    	clcfv = (form.TextValue.value)*-1;
	clcs = 0; // Interest Paid at end.
	LoanAmountValue = form.basevalue.value;
	clcpmt = 0.0;
	sval = 1.0;
	iv = 0;
	Pmt = 2;

	if (clcn == 0)
	{
		temp = 0;
		temp3 = temp;
	}
	else if (clci == 0)
	{
		temp = form.basevalue.value / clcn;
		temp3 = temp;
	}
	else
	{
		temp = Math.abs(Calculate(Pmt, sval, ev, iv, clcs, clcn, clcfv, clci, clcpmt, 
			LoanAmountValue));
		temp3 = temp;
	}	
	form.instalment.value = strngformat (temp);
	flag = 0;
	calculated = 1;
   }   	

  // form.instalment.blur();
  // form.voluntryextra.focus();
  // form.voluntryextra.select();
  return;
}

// -->
 

