function validate (){
	switch (type2){
	case "CobrandUtils.MortgageLoan":
		if (!nondefaultCreditRule("state", "Please select a property location.")||
			!nonemptyStringRule("areacode", "Please enter your property's area code.")||
			!nonemptyRadioButtonRule("foundhome", "Please indicate whether you have found a home.")||
			!nonemptyStringRule("estValue", "Please enter your estimated property value.")||
	        	!nonemptyStringRule("downPayment", "Please enter the down payment.")||
			!nonemptyRadioButtonRule("rateType", "Please indicate whether you would prefer a fixed or variable loan.")||
	        	!nondefaultCreditRule("credit", "Please select a credit status.")||
	        	!nonemptyStringRule("grossIncome", "Please enter your annual gross income.")||
	        	!nonemptyStringRule("oweEachMonth", "Please enter your monthly debt.")||
			(evaluateWhenNeedLoan&&!nonemptyStringRule("whenNeedLoan", "Please indicate how soon you want to close this loan."))){
			return false;
		}
		if (!validateDownPayment("estValue", "recommendedPercentage", "downPayment")){
			return false;
		}

		break;
	case "CobrandUtils.MortgageRefinance":
        	if (!nondefaultCreditRule("state", "Please select a property location.")||
                        !nonemptyStringRule("propertyzip", "Please enter the zip code of your property.")||        	
        		!nonemptyStringRule("estValue", "Please enter your estimated property value.")||
			!nonemptyStringRule("fstMortBal", "Please enter your mortgage balance.")||
			(evaluateMonthPayment&&!nonemptyStringRule("monthPayment", "Please enter your monthly mortgage payment."))||
			!nonemptyStringRule("interestRate", "Please enter a valid interest rate.")||
			(evaluateRefiType&&!nondefaultCreditRule("refinanceType", "Please select a mortgage."))||
			(evaluateLoanRate&&!nonemptyRadioButtonRule("loanRate", "Please indicate whether you would prefer a fixed or variable loan."))||
	        	!nondefaultCreditRule("credit", "Please select a credit status.")||
	        	!nonemptyStringRule("grossIncome", "Please enter your annual gross income.")||
	        	!nonemptyStringRule("oweEachMonth", "Please enter your monthly debt.")){
			return false;
        	}

		break;
	case "CobrandUtils.HomeEquity":
        	if (!nondefaultCreditRule("state", "Please select a property location.")||
                        !nonemptyStringRule("propertyzip", "Please enter the zip code of your property.")||        	
        		!nonemptyStringRule("estValue", "Please enter your estimated property value.")||
			!nonemptyStringRule("fstMortBal", "Please enter your mortgage balance.")||
			(evaluateMonthPayment&&!nonemptyStringRule("monthPayment", "Please enter your monthly mortgage payment."))||
			!nonemptyStringRule("interestRate", "Please enter a valid interest rate.")||
			!nonemptyStringRule("cashOut", "Please enter the amount of additional cash you are requesting.")||			
			(evaluateLoanRate&&!nonemptyRadioButtonRule("loanRate", "Please indicate whether you would prefer a fixed or variable loan."))||
	        	!nondefaultCreditRule("credit", "Please select a credit status.")||
	        	!nonemptyStringRule("grossIncome", "Please enter your annual gross income.")||
	        	!nonemptyStringRule("oweEachMonth", "Please enter your monthly debt.")){
	        	
			return false;
        	}

		break;
	case "PersonalInfo":
        	if (!nonemptyStringRule("firstName", "Please enter your first name.")||
			!nonemptyStringRule("lastName", "Please enter your last name.")||
			!nonemptyStringRule("address1", "Please enter your street address.")||
			!nonemptyStringRule("city", "Please enter your city.")||
	        	!nonemptyStringRule("zipcode", "Please enter your zip code.")||
	        	!nonemptyStringRule("homePhone", "Please enter a valid evening phone number.")||
	        	!nonemptyStringRule("busPhone", "Please enter a valid daytime phone number.")||
			!privacyRule("privacystatement", "Please check the box indicating you accept NexTag's Privacy Policy.")){
			return false;
        	}

		break;
	case "CobrandUtils.Realty":
		
		if (!nondefaultCreditRule("actn", "Please select whether you want to buy, sell, or buy AND sell.")){
			return false;
		}
		if (!nonemptyRadioButtonRule("workingwithagent", "Please select whether or not you are working with a real estate agent.")){
			return false;
		}
		if (!nondefaultCreditRule("whenmove", "Please select when you are planning on moving.")){
			return false;
		}

		var actionType= stringAttrValue("actn");
		if (actionType=="buy"||actionType=="both"){
        		if (
			!nonemptyStringRule("buycity", "Please enter the city you wish to buy property in.")||
			!nondefaultCreditRule("buypricerange", "Please select your target price range.")||
			!nondefaultCreditRule("buycommunity", "Please select your desired community type.")||
			!nondefaultCreditRule("buysize", "Please select the size of the home you wish to buy.")||	
			!nondefaultCreditRule("buypropertytype", "Please select the type of property.")||			
			!nondefaultCreditRule("credit", "Please select your credit status.")
			){
			return false;
			}
			
		}

		if (actionType=="sell"||actionType=="both"){

			if (!nonemptyStringRule("selladdress", "Please enter the address of the property you're selling.")||
			!nonemptyStringRule("sellcity", "Please enter the city you wish to sell property in.")||
			!nonemptyStringRule("sellzip", "Please enter the zip code of the property you're selling.")||
			!nondefaultCreditRule("sellpropertytype", "Please enter the type of property you're selling.")||			
//			!nonemptyStringRule("sellsize", "Please enter the square footage of the property you're selling.")||			
			!nondefaultCreditRule("selllotsize", "Please enter the lot size of the property you're selling.")||						
			!nondefaultCreditRule("sellaskingprice", "Please select the asking price of the property.")){
				return false;
			}
		}
		
		break;
	case "CobrandUtils.RealtyBroker":
		if (subType!="valuation"&&!nondefaultCreditRule("actn", "Please select whether you want to buy, sell, or buy AND sell.")){
			return false;
		}
		if (!nonemptyRadioButtonRule("workingwithagent", "Please select whether or not you are working with a real estate agent.")){
			return false;
		}
		if (!nondefaultCreditRule("whenmove", "Please select when you are planning on moving.")){
			return false;
		}
		if (subType=="valuation"){
			var actionType="sell";
		}else{
			var actionType= stringAttrValue("actn");
			if (actionType=="buy"||actionType=="both"){
	        		if (
				!nonemptyStringRule("buycity", "Please enter the city you wish to buy property in.")||
				!nondefaultCreditRule("buystate", "Please select a state.")||
				!nondefaultCreditRule("buypricerange", "Please select your target price range.")			
				){
				return false;
				}			
			}
		}
		if (actionType=="sell"||actionType=="both"){
			if (!nonemptyStringRule("selladdress", "Please enter the address of the property you're selling.")||
			!nonemptyStringRule("sellcity", "Please enter the city you wish to sell property in.")||
			!nondefaultCreditRule("sellstate", "Please select a state.")||			
			!nonemptyStringRule("sellzip", "Please enter the zip code of the property you're selling.")||
			!nonemptyStringRule("sellsize", "Please enter the square footage of the property you're selling.")||			
			!nonemptyStringRule("sellbedrooms", "Please indicate the Number of Bedrooms in the home you are selling.")||
			!nonemptyStringRule("sellbathrooms", "Please indicate the Number of Bathrooms in the home you are selling.")||
			!nondefaultCreditRule("sellaskingprice", "Please select the asking price of the property.")){
				return false;
			}
		}
		
		break;	
	case "RealtyPersonalInfo":
                if (!nonemptyStringRule("firstName", "Please enter your first name.")||
			!nonemptyStringRule("lastName", "Please enter your last name.")||
			!nonemptyStringRule("address1", "Please enter your street address.")||
			!nonemptyStringRule("city", "Please enter your city.")||
	        	!nonemptyStringRule("zipcode", "Please enter your zip code.")||
	        	!nonemptyStringRule("homePhone", "Please enter a valid evening phone number.")||
	        	!nonemptyStringRule("busPhone", "Please enter a valid daytime phone number.")||
			!privacyRule("privacystatement", "Please check the box indicating you accept NexTag's Privacy Policy.")){
			return false;
                }
		break;
	case "RealtyPick":
		break;
	}
	return true;
}

var alertedBefore=false;
function validateDownPayment(field1, field2, field3){
	var field1val=cleanUp(stringAttrValue(field1));
	var field2val=stringAttrValue(field2);
	var field3val=cleanUp(stringAttrValue(field3));
	var calculatedVal=field1val * field2val /100;
	if (!alertedBefore&&field3val<calculatedVal){
		alertedBefore=true;
		var calculatedValString=""+calculatedVal;
		var newString="";
	  	if (calculatedValString.length>3){
			var i=calculatedValString.length;
			while(i-3>0){
				i=i-3;
				calculatedValString=calculatedValString.substring(0, i)+","+calculatedValString.substring(i);  		
			}
	  	}	
		alert("To get matched with more lenders, we strongly recommend that your down payment be at least $"+ calculatedValString +" ("+field2val+"% of the estimated property value).  If you are able to, you should increase your down payment.");
		if (uagent.indexOf("MSIE")!=-1&&uagent.toLowerCase().indexOf("opera") == -1){
			location.hash='state';		
			subrfq_1.downPayment.select();
		}
		return false;
	}
	return true;
}


function privacyRule(attrName, errorMessage){
	if (uagent.indexOf("MSIE")!=-1&&uagent.toLowerCase().indexOf("opera") == -1){
	if (!document.getElementById(arguments[0]).checked) {
		alert(errorMessage);
		return false;
	}
	return true;
	}else{

	if (!document.subrfq_1.elements["privacystatement"].checked){
		alert(errorMessage);
		return false;
	}
	return true;

	}
}



function nonemptyStringRule(attrName, errorMessage) {
	var stringValue = stringAttrValue(arguments[0]);
	if (uagent.indexOf("MSIE")!=-1 &&uagent.toLowerCase().indexOf("opera") == -1){
		if (stringValue==""){
			document.getElementById(attrName).focus();
			if (document.getElementById(attrName).type == 'text')
				document.getElementById(attrName).select();
			alert(arguments[1]);
			return false;
		}
	return true;
	}else{
		
		if (stringValue==""){
			document.subrfq_1.elements[attrName].focus();
			if (document.subrfq_1.elements[attrName].type == 'text') {
				document.subrfq_1.elements[attrName].select();
			}
			alert(arguments[1]);
			return false;
		}
		return true;
	}
}


function nonemptyRadioButtonRule(attrName, errorMessage) {
	var grp = document.subrfq_1.elements[attrName]; // Get array of radio buttons
	for (var i=0;i < document.subrfq_1.elements.length;i++){ 
		if (document.subrfq_1.elements[i].name==attrName){
			if (document.subrfq_1.elements[i].type=="hidden"){
				return true;
			}
		}
	}
	var r = 0;
	var b;
	while (b=grp[r++]) if (b.checked) return true; // It's checked
	// None checked - error!
	if (r==1){ //if there are no radio buttons
		return true;
	}
	alert(arguments[1]);
	grp[0].focus();
	return false;
}

function nondefaultCreditRule(attrName, errorMessage){
	var stringValue = stringAttrValue(arguments[0]);
	if (uagent.indexOf("MSIE")!=-1 &&uagent.toLowerCase().indexOf("opera") == -1){
	if (stringValue==""||stringValue=="Select One"){
		document.getElementById(attrName).focus();
//		document.getElementById(attrName).select();
		alert(arguments[1]);
		return false;
	}
	return true;
	}else{
	if (stringValue==""||stringValue=="Select One"){
		document.subrfq_1.elements[attrName].focus();
//		document.subrfq_1.elements[attrName].select();
		alert(arguments[1]);
		return false;
	}
	return true;
	}
}


function stringAttrValue(attrName){
	if (uagent.indexOf("MSIE")!=-1&&uagent.toLowerCase().indexOf("opera") == -1){
//	if (document.getElementById(attrName)){
	var stringValue = document.getElementById(arguments[0]).value;
	if (typeof(stringValue)=="undefined"){
		return "";
	}else{
        	var nonspaceIndex=stringValue.search(/\S/);
		if (nonspaceIndex>=0){
			return stringValue;
		}else{
			return "";
		}
	}
	}else{
	if (attrName!="credit"&&attrName!="refinanceType"&&attrName!="actn"&&attrName!="buyrealtor"&&attrName!="sellrealtor"
&&attrName!="startedLoanRequest"&&attrName!="totalloan"&&attrName!="bedrooms1"&&attrName!="bathrooms1"&&attrName!="price"&&attrName!="freecma"
&&attrName!="buypricerange"&&attrName!="buycommunity"&&attrName!="buysize"&&attrName!="buypropertytype"&&attrName!="selllotsize"
&&attrName!="sellaskingprice"){
	var stringValue = document.subrfq_1.elements[arguments[0]].value;
	}else{
	   if (attrName=="credit"){
		var stringValue = document.subrfq_1.credit.options[document.subrfq_1.credit.selectedIndex].value;
	   }else if (attrName=="refinanceType"){
		var stringValue = document.subrfq_1.refinanceType.options[document.subrfq_1.refinanceType.selectedIndex].value;
	   }else if (attrName=="actn"){
		var stringValue = document.subrfq_1.actn.options[document.subrfq_1.actn.selectedIndex].value;
           }else if (attrName=="buyrealtor"){
		var stringValue = document.subrfq_1.buyrealtor.options[document.subrfq_1.buyrealtor.selectedIndex].value;
           }else if (attrName=="sellrealtor"){
		var stringValue = document.subrfq_1.sellrealtor.options[document.subrfq_1.sellrealtor.selectedIndex].value;
           }else if (attrName=="startedLoanRequest"){
		var stringValue = document.subrfq_1.startedLoanRequest.options[document.subrfq_1.startedLoanRequest.selectedIndex].value;
           }else if (attrName=="totalloan"){
		var stringValue = document.subrfq_1.totalloan.options[document.subrfq_1.totalloan.selectedIndex].value;
           }else if (attrName=="bedrooms1"){
		var stringValue = document.subrfq_1.bedrooms1.options[document.subrfq_1.bedrooms1.selectedIndex].value;
           }else if (attrName=="bathrooms1"){
		var stringValue = document.subrfq_1.bathrooms1.options[document.subrfq_1.bathrooms1.selectedIndex].value;
           }else if (attrName=="price"){
		var stringValue = document.subrfq_1.price.options[document.subrfq_1.price.selectedIndex].value;
           }else if (attrName=="freecma"){
		var stringValue = document.subrfq_1.freecma.options[document.subrfq_1.freecma.selectedIndex].value;
           }else if (attrName=="buypricerange"){
		var stringValue = document.subrfq_1.buypricerange.options[document.subrfq_1.buypricerange.selectedIndex].value;
		}else if (attrName=="buycommunity"){
		var stringValue = document.subrfq_1.buycommunity.options[document.subrfq_1.buycommunity.selectedIndex].value;		
		}else if (attrName=="buysize"){
		var stringValue = document.subrfq_1.buysize.options[document.subrfq_1.buysize.selectedIndex].value;
		}else if (attrName=="buypropertytype"){
		var stringValue = document.subrfq_1.buypropertytype.options[document.subrfq_1.buypropertytype.selectedIndex].value;		
		}else if (attrName=="sellaskingprice"){
		var stringValue = document.subrfq_1.sellaskingprice.options[document.subrfq_1.sellaskingprice.selectedIndex].value;		
		}else if (attrName=="selllotsize"){
		var stringValue = document.subrfq_1.selllotsize.options[document.subrfq_1.selllotsize.selectedIndex].value;		
		}


	}
	if (typeof(stringValue)=="undefined"){
		return "";
	}else{
        	var nonspaceIndex=stringValue.search(/\S/);
		if (nonspaceIndex>=0){
			return stringValue;
		}else{
			return "";
		}
	}


	}
}


function numberAttrValue(attrName){
	return parseFloat(stringAttrValue(arguments[0]));
}

function cleanUp(unclean) {
   var toKeep = "0123456789";   
   var cleaned="";
   var oneDecimalAlready=false;
   var c;
   for (var i=0; i<unclean.length; i++){
   	c=unclean.charAt(i);
   	if (c == '.')
   		break;
	for (var j=0; j<toKeep.length; j++){
		if (c==toKeep.charAt(j)){
			cleaned=cleaned+c;
			break;
		}
	}
	}	
   return cleaned;
}


function computedownpayment() {

	var estvalue = parseInt(cleanUp(document.getElementById("estValue").value));
	var downpayment = document.getElementById("downPayment");
	var result = 0;

	result = parseInt(estvalue / 10);

	if (parseInt(cleanUp(downpayment.value)) > result) {
		result = downpayment.value;
	}
	
	downpayment.value = result;

	addCommasNoEvent(downpayment);
	
}

function estValueChange() {
	var downPaymentValue = document.getElementById("downPayment").value;

	document.getElementById("downPayment_rec").innerHTML =
		"(min. " + downPaymentValue + " recommended)";
}

function addCommasNoEvent(field) {

  	var newString=cleanUp(field.value);

  	if (newString.length>3){
		var i=newString.length;
		while(i-3>0){
			i=i-3;
			newString=newString.substring(0, i)+","+newString.substring(i);
		}
  	}

	field.value=newString;


}


function addCommasValue(value) {

  if (uagent.indexOf("msie")!=-1&&uagent.toLowerCase().indexOf("opera") == -1){
  	if (event!=null){
  		var newString=cleanUp(value);
  	if (newString.length>3){
		var i=newString.length;
		while(i-3>0){
			i=i-3;
			newString=newString.substring(0, i)+","+newString.substring(i);  		
		}
  	}
	value=newString;	
	
  }
 }
 return newString;
}

function addCommas(field) {

  if (uagent.indexOf("MSIE")!=-1&&uagent.toLowerCase().indexOf("opera") == -1){
  	if (event!=null){
  		var newString=cleanUp(field.value);
  		
  	if (newString.length>3){
		var i=newString.length;
		while(i-3>0){
			i=i-3;
			newString=newString.substring(0, i)+","+newString.substring(i);  		
		}
  	}
  	
	field.value=newString;	
	
  }
 } else addCommasNoEvent(field)
}
