function getAverage() {	

	var avgValue;
	var arrPriceRange;
	var priceSum;
	var priceRange = document.forms[0].PriceRangeFull.options[document.forms[0].PriceRangeFull.selectedIndex].value;
	
	if (priceRange == '') {
		document.forms[0].Price.value = '';
		document.forms[0].DesiredMonthlyPayment.value = '';
	}
	else {
	
		arrPriceRange = priceRange.split('-');
		avgValue = (arrPriceRange[0] + arrPriceRange[1]) / 2;
		priceSum = parseInt(arrPriceRange[0]) + parseInt(arrPriceRange[1]);
		avgValue = Math.round(priceSum / 2);		
		document.forms[0].Price.value = avgValue;
		recalcMonthlyPay2(avgValue, 5.9, 360);  
	}
}

function recalcMonthlyPay2(avgValue, annualInterestRate, lengthOfLoanInMonths) {
	var monthlyPayment = 0;
	var principle = (avgValue - (avgValue * .2));
	var monthlyInterestRate = annualInterestRate / (12.0 * 100.0);
	if (monthlyInterestRate == 0) {
		monthlyPayment = principle / lengthOfLoanInMonths;
	}
	else {
		monthlyPayment = principle * (monthlyInterestRate / (1 - Math.pow((1 + monthlyInterestRate), - lengthOfLoanInMonths)));
	}
	monthlyPayment = Math.round(monthlyPayment * 100) / 100;
	document.forms[0].DesiredMonthlyPayment.value = monthlyPayment;
}

function openPopUpWin() {
	var childwin = window.open('http://zip4.usps.com/zip4/welcome.jsp','PopUpSelectZip', 'width=800,height=600,toolbar=no,scrollbars=yes,resizeable=yes,status=no');
	childwin.focus();
}

function iCastleZipPopUpWin() {
	var childwin = window.open('http://zip4.usps.com/zip4/welcome.jsp','PopUpSelectZip', 'width=800,height=600,toolbar=no,scrollbars=yes,resizeable=yes,status=no');
	childwin.focus();
}


function cwlZipPopUpWin() {
	var childwin = window.open('http://zip4.usps.com/zip4/welcome.jsp','PopUpSelectZip', 'width=700,height=600,toolbar=no,scrollbars=yes,resizeable=yes,status=no');
	childwin.focus();
}

function showDiv(divId) {
	if (divId == 0) {	
	
		document.getElementById('div0').style.display = "block";
		document.getElementById('div1').style.display = "none";
		document.getElementById('Bankruptcy_Mort').checked = "CHECKED";
		
		if (document.forms[0].LeadType.value == '3'){
			document.getElementById('CreditHistory_Refi').disabled = true;
			document.getElementById('CreditHistory_Mort').disabled = false;
		}
		
		document.forms[0].MonthlyHousingPayment.noValidation = false;	
		document.forms[0].DownPayment.noValidation = false;					
		document.forms[0].FirstMortgageInterestRate.noValidation = true;				
		document.forms[0].FirstMortgageRateType.noValidation = true;

		document.forms[0].AnnualIncome[0].disabled = false;
		document.forms[0].TotalMonthlyBills[0].disabled = false;
		document.forms[0].BestTimeToContact[0].disabled = false;
		
		document.forms[0].AnnualIncome[1].disabled = true;
		document.forms[0].TotalMonthlyBills[1].disabled = true;
		document.forms[0].BestTimeToContact[1].disabled = true;
	}
	
	else {	
	
		document.getElementById('div0').style.display = "none";
		document.getElementById('div1').style.display = "block";
		document.getElementById('Bankruptcy_Refi').checked = "CHECKED";

		if (document.forms[0].LeadType.value == '3'){
			document.getElementById('CreditHistory_Refi').disabled = false;
			document.getElementById('CreditHistory_Mort').disabled = true;
		}		
		
		document.forms[0].MonthlyHousingPayment.noValidation = true;	
		document.forms[0].DownPayment.noValidation = true;					
		document.forms[0].FirstMortgageInterestRate.noValidation = false;				
		document.forms[0].FirstMortgageRateType.noValidation = false;

		document.forms[0].AnnualIncome[0].disabled = true;
		document.forms[0].TotalMonthlyBills[0].disabled = true;
		document.forms[0].BestTimeToContact[0].disabled = true;
		
		document.forms[0].AnnualIncome[1].disabled = false;
		document.forms[0].TotalMonthlyBills[1].disabled = false;
		document.forms[0].BestTimeToContact[1].disabled = false;
	}
}


function showOthersDiv() {
	var homeValue;
	homeValue = document.forms[0].reQHomeValuation.options[document.forms[0].reQHomeValuation.selectedIndex].value;
	if (homeValue != "5") {	
		document.getElementById('div1').style.display = "none";
		document.forms[0].OthersText.value = '';
	}
	else {	
		document.getElementById('div1').style.display = "block";
		//document.forms[0].OthersText.value = '';
	}
}

function validateFinanceForm(form) {

	var loanType;
	var errMsg = '';
	
	if (form.loantype[0].checked == true) {
		loanType = form.loantype[0].value;
	}
	else {
		loanType = form.loantype[1].value;
	}
	
	switch (loanType) {
		case '2':
			if (document.forms[0].AnnualIncome[0].value == '') {
				errMsg = REQUIRED_ERROR.replace('{0}', 'Annual Income');
			}				
			if (document.forms[0].TotalMonthlyBills[0].value == '') {
				errMsg = REQUIRED_ERROR.replace('{0}', 'Total Monthly Bills');
			}				
			break;		
		case '3':
			if (document.forms[0].AnnualIncome[1].value == '') {
				errMsg = REQUIRED_ERROR.replace('{0}', 'Annual Income');
			}				
			if (document.forms[0].TotalMonthlyBills[1].value == '') {
				errMsg = REQUIRED_ERROR.replace('{0}', 'Total Monthly Bills');
			}				
			break;		
	}
	
	if (errMsg != '') {
		alert(errMsg);
		return false;
	}
	else {
		return validateForm(form);
	}
}