
var isFirst = true;

function Calc_Payment(theForm) {	
	var tmpDownPay;
	//validate form
	/*try{*/
	
	if (check4Blank2(theForm.elements.Rate.value,"interest") == false) {
		return ;
	}
	if (checkIsNumeric(theForm.elements["Rate"].value, "interest") == false){
		theForm.elements["Rate"].value = ""
		theForm.elements["Rate"].focus();
		return ;
	}	 
	if (check4Blank(theForm.elements["term"].value,"loan term") == false) {
		return ;
	}
	
	if((isFirst == true) && (theForm.elements["carPrice"].value == ''))
	{
		isFirst = false; //dont show warning  if  no price 
		return;
	}
	else
	{
		if (checkFormField4Blank(theForm.elements["carPrice"], "Price") == false) {
			return ;
		}
	}
	
	if (checkIsNumeric(theForm.elements["carPrice"].value, "Price") == false){
		theForm.elements["carPrice"].value = ""
		theForm.elements["carPrice"].focus();
		return ;
	}
	
	/*if (theForm.elements["carPrice"].value.length >= 10 ){
		alert("Car price value  should be between 0 and 99,999.99");
		
			
		return ;
	}*/


	if (theForm.elements["down_payment"].value.length <= 0) {
		theForm.elements["down_payment"].value = "0.00";
	}
	
	if (checkIsNumeric(theForm.elements["down_payment"].value, "down payment") == false){
		theForm.elements["down_payment"].value = "0.00"
		theForm.elements["down_payment"].focus();
		return ;
	}
		 
	if ( getPrice(theForm.elements["down_payment"].value) >= getPrice(theForm.elements["carPrice"].value) ) {
		alert("The down payment cannot be greater than the loan amount.");
		theForm.elements["down_payment"].focus();
		return ;
	}

	var downPayment = theForm.elements["down_payment"].value;
	var monthPayment = 0;

	var loanRate = parseFloat(theForm.elements["Rate"].value);
	var loanTerm = parseInt(theForm.elements["term"].value);
	var carprice = getPrice(theForm.elements["carPrice"].value)
	
	//alert(carprice);
	if (carprice > getPrice('999,999.99')){
		alert("Car price value  should be between 0 and 999,999.99 " );
		theForm.elements["carPrice"].value = ""
		theForm.elements["carPrice"].focus();
		return;
	}
	var downPayment = getPrice(theForm.elements["down_payment"].value)

	var loanAmount = carprice - downPayment 
	var numerator, denominator, periods, interestRate = 0;
	var i = loanRate;	
	
	i = i * 0.01;
    interestRate = i / 12;

    periods = loanTerm;
    numerator = loanAmount * interestRate * Math.pow(1 + interestRate, periods);
    denominator = Math.pow(1 + interestRate, periods) - 1;
    monthPayment = numerator / denominator;

    if (interestRate == 0)
	monthPayment = loanAmount/periods;	


	if (document.all){
		document.all("monthPay").innerText = "$" + format2Dollars(monthPayment, true) + " ";
		theForm.elements["carPrice"].value = format2Dollars(carprice, false);
		theForm.elements["down_payment"].value = format2Dollars(downPayment, true);
		if (document.all("priceval")) {
			document.all("priceval").innerText = format2Dollars(carprice, false); 
		}

	}else if (document.getElementById){
		document.getElementById("monthPay").innerHTML = "$" + format2Dollars(monthPayment, true) + " ";
		theForm.elements["carPrice"].value = format2Dollars(carprice, false);
		theForm.elements["down_payment"].value = format2Dollars(downPayment, true);
		if (document.getElementById("priceval")){
			document.getElementById("priceval").innerHTML = format2Dollars(carprice, false);
		}
	}
	/*}
	catch(e)
	{; //?
	}*/ 
	
}

function getPrice(strPrice) {
        var price;
        price  = strPrice.replace(",", "")
        price = parseFloat(price.substring(price.lastIndexOf("$")+1,price.length));
        return price;
}
function FormatFloat2String(input) { //"nn.nn"
	if ((input.value == 0) && (input.length == null)) {
		input.value = "0.00";
	}

	var temp = input + 0.005; 
	var cents = "" + (100 + Math.floor((temp - Math.floor(temp)) * 100));
	var dollars = "" + Math.floor(temp);

	return dollars + "." + cents.substring(1,3);
}

function format2Dollars(input, isCents) {
	var temp = input + 0.005
	var cents = "" + (100 + Math.floor((temp - Math.floor(temp)) * 100));
	cents = cents.substring(1,3);
	var dollars = "" + Math.floor(temp);
	var output = dollars.substring((dollars.length),(dollars.length - 1));
	for (var i = 1; i < dollars.length; i++) {
		if ((i / 3) == Math.floor(i / 3)) {
			output = "," + output;
		}
		output = dollars.substring((dollars.length - i),(dollars.length - i - 1)) + output;
	}
	if (isCents) 
	{
		output = output + "." + cents;
	}
	
	
	return output;
}


function checkIsNumeric(input, msg) {
var tmpVal = new String(input);
tmpVal = tmpVal.replace(",","")

	msg = "The " + msg + " field should be numeric value .";	
	if  (isFloat(tmpVal)){
		return true;
	} 	
	else{
		alert(msg);
		return false;
	}
}

function check4Blank2(input,msg) {
	msg = "The " + msg + " field cannot be blank.";
	if ((input < 0) || (input.length == 0)) {
		alert (msg);
		return false;
	}
	return true;
}

function check4Blank(input,msg) {
	msg = "The " + msg + " field cannot be blank, zero, or less than zero.";
	if ((input <= 0) || (input.length == 0)) {
		alert (msg);
		return false;
	}
	return true;
}

function checkFormField4Blank(input,msg) {
	msg = "The " + msg + " field cannot be blank, zero, or less than zero.";
	if ((input.value <= 0) || (input.value.length == 0)) {
		alert (msg);
		return false;
	}
	return true;
}

function CalculateVehiclePrice(p_sVIN, p_oForm) {

	if (check4Blank(p_oForm.monthly_payment.value,"Monthly payment") == false) {
		return ;
	}
	if (checkIsNumeric(p_oForm.monthly_payment.value, "Monthly payment") == false){
		p_oForm.elements["monthly_payment"].value = ""
		p_oForm.elements["monthly_payment"].focus();
		return ;
	}
	if (checkIsNumeric(p_oForm.down_payment.value, "Down payment") == false){
		p_oForm.elements["down_payment"].value = ""
		p_oForm.elements["down_payment"].focus();
		return ;
	}

	var fRate = p_oForm.Rate.value;
	var iMonths = p_oForm.term.value;
	var fDownPayment = p_oForm.down_payment.value;
	var fMonthlyPayment = p_oForm.monthly_payment.value;
	
	openCalculator(p_sVIN, "r", fRate, iMonths, fDownPayment, fMonthlyPayment);
}


