/*global variables*/
		var txtSellingPrice;
		var txtCash;
		var txtRate;
		var lblEstimatedPayment;
		var lblEstimatedMonthlyPayments;			
			
		function CalculatePayment(){			
			
			txtSellingPrice.value = txtSellingPrice.value.replace(",","");
			txtCash.value = txtCash.value.replace(",","");
			txtRate.value = txtRate.value.replace(",","");						
			
			var decFinanceRate;
			if (txtRate.value != 0)
			{
				decFinanceRate = txtRate.value / 100;
			}
			else
			{
				decFinanceRate = 0;
			}
			
			var ddlTerm;
			ddlTerm = document.getElementById('ddlTerm');
			
			var intTerm;
			intTerm = ddlTerm.options[ddlTerm.selectedIndex].value;
												
			if (ValidateInput() == true){
				//calc payment
				var strAmountFinanced;
				strAmountFinanced = txtSellingPrice.value - txtCash.value;
			
				var decPayment;
				if (txtRate.value != 0)
				{
					decPayment = (strAmountFinanced * (decFinanceRate / 12)) / (1 - Math.pow((1 + (decFinanceRate / 12)),(Math.abs(intTerm) * -1)))
				}
				else
				{
					decPayment = strAmountFinanced / intTerm;
				}
					
				lblEstimatedPayment.innerHTML = '$' + Math.round(decPayment);
				lblEstimatedMonthlyPayments.innerHTML = '$' + Math.round(decPayment);
										
			}
			else{
				lblEstimatedPayment.innerHTML = '';
				lblEstimatedMonthlyPayments.innerHTML = '';
			}			
		}
		
		function ValidateInput(){		
			if (!IsNumeric(txtSellingPrice.value)){
				alert('Selling Price must be Numeric.');
				return false;			
			}
			else if(!IsNumeric(txtCash.value)){
				alert('Cash/Trade-in value must be Numeric.');
				return false;			
			}
			else if(!IsNumeric(txtRate.value)){
				alert('Rate value must be Numeric.');
				return false;			
			}
			else if(parseFloat(txtSellingPrice.value) < parseFloat(txtCash.value)){
				alert('The Cash/Trade-in value you entered is greater than the selling price of the vehicle.');
				return false;			
			}
			else if(parseFloat(txtSellingPrice.value) < parseFloat(txtCash.value)){
				alert('The Cash/Trade-in value you entered is greater than the selling price of the vehicle.');
				return false;			
			}			
			
			return true;			
		}

		function IsNumeric(sText){
			var ValidChars = "0123456789.";
			var IsNumber=true;
			var Char;
			for (i = 0; i < sText.length && IsNumber == true; i++){
			Char = sText.charAt(i);
				if (ValidChars.indexOf(Char) == -1){
					IsNumber = false;
				}
			}
			return IsNumber;
			}

	
		function initCalculator(strSellingPrice,strRate,strPercentCashDefault,strTerm){		
		
		
			var divPaymentCalc;
			divPaymentCalc = document.getElementById('divPaymentCalc');
			divPaymentCalc.style.display="inline";
				
			/*start set global*/
			txtSellingPrice = document.getElementById('txtSellingPrice');
			txtCash = document.getElementById('txtCash');
			txtRate = document.getElementById('txtRate');
			lblEstimatedPayment = document.getElementById('lblEstimatedPayment');
			lblEstimatedMonthlyPayments = document.getElementById('lblEstimatedMonthlyPayments');
			/*end set global*/		
			
								
			txtSellingPrice.value = strSellingPrice;					
			txtCash.value = Math.round(strPercentCashDefault * (txtSellingPrice.value));					
			txtRate.value = strRate;			
			
			
			var ddlTerm;
			ddlTerm = document.getElementById('ddlTerm');
			
			for (var count = ddlTerm.options.length-1; count >-1; count--)
			{
				if (ddlTerm.options[count].value == strTerm){					
					ddlTerm.options[count].selected = true;
					break;
				}					
			}		
			
			
			CalculatePayment();
			
			var divMonthlyPaymentsAslowAs;
			divMonthlyPaymentsAslowAs = document.getElementById('divMonthlyPaymentsAslowAs');
			divMonthlyPaymentsAslowAs.style.display="inline";
			
			if (lblEstimatedMonthlyPayments.innerHTML == '$' + Math.round(0)){
				divMonthlyPaymentsAslowAs.style.display="none";
			}
			
			
			
			
					
		}
