//Javascript
var zipformat = /^[0-9]{5}$/;
var intformat = /^[0-9]+$/;
var dollarformat = /^\${0,1}([0-9]{1,3}(\,[0-9]{3})*|([0-9]+))(\.[0-9]{2})?$/;
var floatformat = /^[0-9]+(\.[0-9]+){0,1}$/;

function formCalculatorValidator() {
	var validation_result = true;

	if (!dollarformat.test(document.getElementById('txtCalculatorLoanAmount').value)) {
		document.getElementById('txtCalculatorLoanAmountError_msg').innerHTML = 'Invalid amount';
		validation_result = false;
	} else if (Number(document.getElementById('txtCalculatorLoanAmount').value.replace(/[^0-9]/g, '')) < 5000) {
		document.getElementById('txtCalculatorLoanAmountError_msg').innerHTML = 'Amount less<br />than $5,000';
		validation_result = false;
	} else if (!CheckLoanAmountMoreThanHomeValue()) {
		validation_result = false;
	}

	if (!floatformat.test(document.getElementById('txtCalculatorInterestRate').value)) {
		document.getElementById('txtCalculatorInterestRateError_msg').innerHTML = 'Invalid rate';
		validation_result = false;
	}

	if (!dollarformat.test(document.getElementById('txtCalculatorPropertyValue').value)) {
		document.getElementById('txtCalculatorPropertyValueError_msg').innerHTML = 'Invalid value';
		validation_result = false;
	}

	if (!zipformat.test(document.getElementById('txtCalculatorZipCode').value)) {
		document.getElementById('txtCalculatorZipCodeError_msg').innerHTML = 'Invalid zip code';
		validation_result = false;
	}

	if (!intformat.test(document.getElementById('txtCalculatorYearTerm').value)) {
		document.getElementById('txtCalculatorYearTermError_msg').innerHTML = 'Invalid terms';
		validation_result = false;
	}

	if (document.getElementById('chkEstimate').checked == false && !dollarformat.test(document.getElementById('txtCalculatorMonthTax').value)) {
		document.getElementById('txtCalculatorMonthTaxError_msg').innerHTML = 'Invalid amount';
		validation_result = false;
	}		

	return validation_result;
}

function CheckLoanAmountMoreThanHomeValue() {
	var temp1;
	var temp2;
	temp1 = document.getElementById('txtCalculatorLoanAmount').value.replace(/[^0-9]/g, '');
	temp2 = document.getElementById('txtCalculatorPropertyValue').value.replace(/[^0-9]/g, '');
	if (Number(temp1) > Number(temp2)) {
		document.getElementById('txtCalculatorLoanAmountError_msg').innerHTML = 'Amount exceeds<br />property value';
		return false;
	}
	document.getElementById('txtCalculatorLoanAmountError_msg').innerHTML = '';
	return true;
}

function EstimateTaxOnCheck(checkbox) {
	if (checkbox.checked) {
		document.getElementById('txtCalculatorMonthTax').value = '';
		document.getElementById('txtCalculatorMonthTax').style.backgroundColor = '#ededee';
		document.getElementById('txtCalculatorMonthTax').disabled = true;
		document.getElementById('txtCalculatorMonthTaxError_msg').innerHTML = '';
	} else {
		document.getElementById('txtCalculatorMonthTax').style.backgroundColor = '#ffffff';
		document.getElementById('txtCalculatorMonthTax').disabled = false;
	}
}
