// JavaScript Document

function printSelection(node) {

  var content=document.getElementById(node).innerHTML
  var pwin=window.open('','print_content','width=100,height=100');

  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
 
  setTimeout(function(){pwin.close();},1000);
}

function IsValid(objToTest) {
if (objToTest == null || objToTest == undefined) {
return false;
}
return true;
}

function RoundtoCents (n) {
    n = Math.round(n * 100) / 100;
    n = (n + 0.001) + '';
    return n.substring(0, n.indexOf('.') + 3);
  }
function testIsValidObject(objToTest) {
		if (null == objToTest) {
			return false;
		}
		if ("undefined" == typeof(objToTest) ) {
			return false;
		}
		return true;
	}
	
	function Calculate_Price(frm){
	
		var SubTotal = eval(0);
		var Total = eval(0);
		var StateTax = eval(0);
		var Miles = eval(0);
		var Shipping = eval(0);
		var Equity = eval(0);
		var TotalDownPayment = eval(0);
		var FinancingAmount = eval(0);
		var Term = eval(0);
		var Rate = eval(0);
		var Pmt = eval(0);

		var delivery_miles_free = eval(0);
		var delivery_extra_500_miles = eval(0);
		var delivery_cost_per_mile = eval(0);

		// Total = Total + eval(document.features.BasePrice.value);
		Total = eval(document.features.BasePrice.value);
		
		for (var x = 0; x < document.features.elements.length; x++) {
			// Check Boxes - when checked, use their values
            if (document.features.elements[x].name.substring(0,2) == "CB") {
				if (document.features.elements[x].checked) {
					Total = Total + eval(document.features.elements[x].value);
					}
				}
 			// Drop Downs - use their values
        	if (document.features.elements[x].name.substring(0,2) == "DD") {
				if (eval(document.features.elements[x].value) > 0) {
					Total = Total + eval(document.features.elements[x].value);
					}
				// Total = eval(Total) + eval(document.features.elements[x].value);
				}
			}
		SubTotal = Total;
		
		StateTax = eval(document.features.StateTax.value)/100 * SubTotal;
		
		delivery_miles_free = 1000;
		delivery_extra_500_miles = 550;
		delivery_cost_per_mile = 1.5;
		
		if (IsValid(document.features.delivery_miles_free)) {
			if (eval(document.features.delivery_miles_free.value) > 0) {
				delivery_miles_free = eval(document.features.delivery_miles_free.value);
				}
			} 

		if (IsValid(document.features.delivery_extra_500_miles)) {
			if (eval(document.features.delivery_extra_500_miles.value) > 0) {
				delivery_extra_500_miles = eval(document.features.delivery_extra_500_miles.value);
				}
			} 

		if (IsValid(document.features.delivery_cost_per_mile)) {
			if (eval(document.features.delivery_cost_per_mile.value) > 0) {
				delivery_cost_per_mile = eval(document.features.delivery_cost_per_mile.value);
				}
			}
		
		Miles = eval(document.features.ShippingMiles.value);
		if (Miles <= delivery_miles_free) { Shipping = 0; }
		if (Miles > delivery_miles_free)  { Shipping = delivery_extra_500_miles; }
		if (Miles > delivery_miles_free+500)  
			{ Shipping = delivery_extra_500_miles + (Miles-delivery_miles_free-500)*delivery_cost_per_mile;	}
		
		Total = SubTotal + StateTax + Shipping  
			+ eval(document.features.LicensingFee.value)
			+ eval(document.features.DocumentFee.value)
			+ eval(document.features.ServiceFee.value)
			+ eval(document.features.ClosingFee.value)
			;
			
		Equity = eval(document.features.TradeValue.value)-eval(document.features.TradePayoff.value);
		TotalDownPayment = eval(document.features.DownPayment.value)+Equity;
		FinancingAmount = Total-TotalDownPayment;
		
		Rate = eval(document.features.InterestRate.value)/1200;
		Term = eval(document.features.TermOfLoan.value);

		Pmt = FinancingAmount * Rate / (1 - (Math.pow(1/(1 + Rate), Term)));
		
		if(Total != 0){			
            // alert(Total);
			document.getElementById("MonthlyPaymentsDisplay").innerHTML = "<strong>" + RoundtoCents(Pmt) + "</strong>";
			document.getElementById("StateTaxDisplay").innerHTML = RoundtoCents(StateTax) ;
			document.getElementById("ShippingDisplay").innerHTML = RoundtoCents(Shipping) ;
			document.getElementById("TotalDownPaymentDisplay").innerHTML = RoundtoCents(TotalDownPayment) ;
			document.getElementById("FinancingAmountDisplay").innerHTML =  "<strong>" + RoundtoCents(FinancingAmount) + "</strong>";
			document.getElementById("EquityDisplay").innerHTML = Equity ;

			document.getElementById("SubTotal").innerHTML = "<strong>" + RoundtoCents(SubTotal) + "</strong>";
			document.getElementById("GrandTotal").innerHTML = "<strong>"+  RoundtoCents(Total) + "</strong>";
			// document.Financing.principal.value = RoundtoCents(Total);
			// document.getElementById("GrandTotal").innerHTML = "The calculator is not currently available";
		}
		else {
			alert("Error in Calculation");
		}
	}	


