function changeLoanType(LPElem){
	//if 1, FE, else if 2 HE
	LPElemValue = LPElem.options[LPElem.selectedIndex].value;
	LT = document.getElementById('LoanType');
	if(3 == LPElemValue){
		LT.options.length=0;
		LT.options[0] = new Option('Select...','');
		for(x=0;x<HE.length;x++){
			LT.options[x+1] = new Option(HE[x].DESCRIPTION,HE[x].ID);
		}
	} else {
		LT.options.length=0;
		LT.options[0] = new Option('Select...','');
		for(x=0;x<FM.length;x++){
			LT.options[x+1] = new Option(FM[x].DESCRIPTION,FM[x].ID);
		}
	}
}
function formatLoanAmount(/*Form input object*/ obj){
	if(obj.value && obj.value.length > 3){
		obj.value = formatDollarAmount(obj.value);
	}
}
function formatDollarAmount(/*String number*/ str){
	str = str.replace(/[^0-9.]/g, "");
	num = parseFloat(str);
	if(!isNaN(num)){
		num = Math.round(num);
		str = num+"";
		var RE = /(\d+)(\d{3})/;
		while (RE.test(str)){
			str = str.replace(RE, '$1' + ',' + '$2');
		}
		return "$"+str;
	}
	else
		return "";
}

function checkForm(formType){
	if(formType == 1){
		return checkForm1();
	} else {
		return checkForm2();
	}
	
}
function checkForm1(){
	var msg = [];
	var LP = document.getElementById('LoanPurpose');
	var LT = document.getElementById('LoanType');
	var ST = document.getElementById('RatesState');
	var AM = document.getElementById('LoanAmount');
	var PT = document.getElementById('UserPoints');
	if(!LP.options[LP.selectedIndex].value){
		msg[msg.length] = 'Please Select a Loan Purpose.';
	}
	if(!LT.options[LT.selectedIndex].value){
		msg[msg.length] = 'Please Select a Loan Type.';
	}
	if(!ST.options[ST.selectedIndex].value){
		msg[msg.length] = 'Please Select State.';
	}
	if(PT){
		if(!PT.options[PT.selectedIndex].value){
			msg[msg.length] ='Please Select Points.';
		}
	}
	
	var tmp = AM.value.replace(/\\$/,'').replace(/,/,'');
	
	if(msg.length == 0){
		if(tmp == ''){
			if(LT.options[LT.selectedIndex].value == '13' || LT.options[LT.selectedIndex].value == '14'){
				AM.value = '$50,000';
				return true;
			}
			AM.value = '$250,000';
			return true;
		}
	}
	

	if(tmp < 10000){
			msg[msg.length] = 'Loan amount must be at least $10,000.';
	}
	msg = msg.join("\n");
	if(msg) {
		alert(msg);
		return false;
	}
	return true;
}
function checkForm2(){
	var errorDiv = document.getElementById('RatesError');
	var LP = document.getElementById('LoanPurpose');
	var LT = document.getElementById('LoanType');
	var ST = document.getElementById('RatesState');
	var AM = document.getElementById('LoanAmount');
	var PT = document.getElementById('UserPoints');
	if(!LP.options[LP.selectedIndex].value){
		errorDiv.innerHTML = 'Please Select a Loan Purpose.';
		errorDiv.style.display = '';
		return false;
	}
	if(!LT.options[LT.selectedIndex].value){
		errorDiv.innerHTML = 'Please Select a Loan Type.';
		errorDiv.style.display = '';
		return false;
	}
	if(!ST.options[ST.selectedIndex].value){
		errorDiv.innerHTML = 'Please Select State.';
		errorDiv.style.display = '';
		return false;
	}
	var tmp = AM.value.replace(/\\$/,'').replace(/,/,'');
	if(!tmp.match(/\d+/)){
		errorDiv.innerHTML = 'Please Enter valid Loan Amount.';
		errorDiv.style.display = '';
		return false;
	} else if(LT.options[LT.selectedIndex].value == '13' || LT.options[LT.selectedIndex].value == '14'){
		if(tmp < 10000){
			errorDiv.innerHTML = 'Home Equity Loan must be at least $10,000.';
			errorDiv.style.display = '';
			return false;
		}
	} else if(tmp < 10000 && !(LT.options[LT.selectedIndex].value == '13' || LT.options[LT.selectedIndex].value == '14')){
		errorDiv.innerHTML = 'Mortgage loan amount must be at least $10,000.';
		errorDiv.style.display = '';
		return false;
	}
	errorDiv.style.display = 'none';
	return true;
}

/**Following code to change the loan types based on loan purpose on load
 * to make sure the two lists match up after a page refresh
 * */

if(LoanTypeReset){
	if (document.addEventListener)
	  document.addEventListener("DOMContentLoaded", changeLoanTypeWrapper, false)
	else if (document.all && !window.opera){
	  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
	  var contentloadtag=document.getElementById("contentloadtag")
	  contentloadtag.onreadystatechange=function(){
	    if (this.readyState=="complete"){
	      changeLoanTypeWrapper();
	    }
	  }
	}
	
	window.onload=function(){
	  setTimeout("changeLoanTypeWrapper();", 0);
	}
	function changeLoanTypeWrapper(){
	  changeLoanType(document.getElementById('LoanPurpose'));
	}
}
