/*global variables*/
		var txtSellingPrice;
		var txtCash;
		var txtRate;
		var lblEstimatedPayment;
	
			
		function CalculatePayment(){			
		
			/*start set global*/
			txtSellingPrice = document.getElementById('txtSellingPrice');
			txtCash = document.getElementById('txtCash');
			txtRate = document.getElementById('txtRate');
			lblEstimatedPayment = document.getElementById('lblEstimatedPayment');
			/*end set global*/	
			
			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);
							
			}
			else{
				lblEstimatedPayment.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 init(strSellingPrice, strCashDown, strRate, strTerm){		

			/*start set global*/
			txtSellingPrice = document.getElementById('txtSellingPrice');
			txtCash = document.getElementById('txtCash');
			txtRate = document.getElementById('txtRate');
			lblEstimatedPayment = document.getElementById('lblEstimatedPayment');		
			/*end set global*/	
			
								
			txtSellingPrice.value = strSellingPrice;					
			txtCash.value = strCashDown;					
			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;
				}					
			}		
			
			//strTerm		
			CalculatePayment();		
		}