
function getDocObj(elem,parent) {

    if (document.layers) {
        if (parent) {
	    return "document."+parent+".document."+elem;
	} else {
	    return "document."+elem;
	}

    } else if (document.all) {
	return "document.all."+ elem;
    } else if (document.getElementById) {
	return "document.getElementById('"+elem+"')";
    }
}

function displayItem(itemName, flag) {

    if(flag){
        eval(getDocObj(itemName)).style.display="";
    }
	
    else {
	eval(getDocObj(itemName)).style.display="none";
    }
    
}

function checkSubmitLong(form) {


     if (form.purchaseMonth.value==""){
      alert("Please tell us the month you purchased your home.");
      return false;
     }
     
     if (form.purchaseYear.value==""){
      alert("Please tell us the year you purchased your home.");
      return false;
     }
     
     if (form.homeType.value==""){
      alert("Please describe your property type.");
      return false;
     }
     
     if (form.homeValue.value==""){
      alert("Please list your property value.");
      return false;
     }
     
     if (form.firstMortgageBalance.value==""){
      alert("Please list the balance on your first mortgage.");
      return false;
     }
     
     if (form.firstMortgagePayment.value==""){
      alert("How much are your monthly payments on your first mortgage?");
      return false;
     }
     
     if (form.firstMortgageRate.value==""){
      alert("What is your current rate on your first mortgage?");
      return false;
     }
     
     if (form.firstMortgageTerm.value==""){
      alert("What kind of term do you have on your first mortgage?");
      return false;
     }
     
     var hasSecMtgVal;

     for(var i=0; i < form.hasSecondMortgage.length; i++) {
         if (form.hasSecondMortgage[i].checked) {
             hasSecMtgVal = form.hasSecondMortgage[i].value;
         }
     }
	 
     if (hasSecMtgVal=="yes") {
	
         if(form.secondMortgageBalance.value=="") {
	     alert("Please enter the current balance of your second mortgage.");
	     return false;
	 }					
					
	 if(form.secondMortgagePayment.value=="") {
	     alert("Please enter the monthly payment of your second mortgage.");
	     return false;
	 }
     }
     
     if (form.householdIncome.value==""){
      alert("Please enter you annual household income.");
      return false;
     }
     
     if (form.workLength.value==""){
      alert("Please enter the duration at the current job.");
      return false;
     }
     
     if (form.creditHistory.value==""){
      alert("Please put in your credit history.");
      return false;
     }
     
     if (form.bankruptcy.value==""){
      alert("Please list your bankruptcy status.");
      return false;
     }
     
     if (form.firstName.value==""){
      alert("Please enter your first name.");
      return false;
     }
     
     if (form.lastName.value==""){
      alert("Please enter your last name.");
      return false;
     }
     
     if (form.addressStreet.value==""){
      alert("Please enter your street address.");
      return false;
     }
     
     if (form.addressCity.value==""){
      alert("Please enter your city.");
      return false;
     }
     
     if (form.addressState.value==""){
      alert("Please enter your state.");
      return false;
     }
     
     if ((form.addressZip.value.length != 5) || (isNaN(form.addressZip.value))) {
      alert("Please enter a valid 5-digit zip code.");
      return false;
     }
     
     var stripped = form.phoneWork.value.replace(/[\(\)\.\-\ ]/g, '');
     if (isNaN(parseInt(stripped))) {
        alert("The day phone number is not supplied or contains illegal characters.");
        return false;
     }
     
     if (!(stripped.length == 10)) {
      alert("The day phone number is the wrong length.\nMake sure you included an area code.\n");
      return false;
     }
     
     var stripped = form.phoneEvening.value.replace(/[\(\)\.\-\ ]/g, '');
     if (isNaN(parseInt(stripped))) {
        alert("The evening phone number is not supplied or contains illegal characters.");
        return false;
     }
     
     if (!(stripped.length == 10)) {
      alert("The evening phone number is the wrong length.\nMake sure you included an area code.\n");
      return false;
     }
     
     if (form.contactTime.value==""){
      alert("When is the best time to contact you?");
      return false;
     }
     
     if (form.email.value==""){
       alert("Please enter your email address.");
       return false;
     }
     
     if (!emailCheck(form.email.value)) {
       alert("Please enter a valid email address.");
       return false;
     }

     return true;
}

function emailCheck (emailStr) {
	  			 var emailPat=/^(.+)@(.+)$/
	  			 var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	   			 var validChars="\[^\\s" + specialChars + "\]"
	   			 var quotedUser="(\"[^\"]*\")"
	  			 var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	  			 var atom=validChars + '+'
	  			 var word="(" + atom + "|" + quotedUser + ")"
	  			 var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	  			 var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	   			 var matchArray=emailStr.match(emailPat)
	  				 if (matchArray==null) {
	    				alert("Email address seems incorrect (check @ and .'s)")
	  				  return false
	   				}
	   			var user=matchArray[1]
	   			var domain=matchArray[2]
	  				 // See if "user" is valid
	  				 if (user.match(userPat)==null) {
	      			 // user is not valid
	       				alert("The username of the email address doesn't seem to be valid.")
	       				return false
	  				 }
	   			var IPArray=domain.match(ipDomainPat)
	  				 if (IPArray!=null) {
	       			// this is an IP address
	     				 for (var i=1;i<=4;i++) {
	       					 if (IPArray[i]>255) {
	           					 alert("Destination IP address is invalid!")
	    					 return false
	        				}
	      			 }
	       		return true
	     }
	     
	   // Domain is symbolic name
	   var domainArray=domain.match(domainPat)
	   if (domainArray==null) {
	    alert("The domain name doesn't seem to be valid.")
	       return false
	   }
	   var atomPat=new RegExp(atom,"g")
	   var domArr=domain.match(atomPat)
	   var len=domArr.length
	   if (domArr[domArr.length-1].length<2 ||
	       domArr[domArr.length-1].length>3) {
	      // the address must end in a two letter or three letter word.
	      alert("The email address must end in a three-letter domain, or two letter country.")
	      return false
	   }
	   // Make sure there's a host name preceding the domain.
	   if (len<2) {
	      var errStr="This address is missing a hostname!"
	      alert(errStr)
	      return false;
	   }
	   // If we've gotten this far, everything's valid!
	   return true;
}
	   
	   
function checkSubmitSecond(form){	
	//alert(form.redirectPath.value);
     if (form.purchaseMonth.value==""){
      alert("Please tell us the month you purchased your home.");
      return false;
     }

     if (form.purchaseYear.value==""){
      alert("Please tell us the year you purchased your home.");
      return false;
     }

     if (form.homeValue.value==""){
      alert("Please list your property value.");
      return false;
     }
     

     if (form.firstMortgageBalance.value==""){
      alert("Please list the balance on your first mortgage.");
      return false;
     }
     
     if (form.firstMortgagePayment.value==""){
      alert("How much are your monthly payments on your first mortgage?");
      return false;
     }
     
     if (form.firstMortgageRate.value==""){
      alert("What is your current rate on your first mortgage?");
      return false;
     }
     
     if (form.firstMortgageTerm.value==""){
      alert("What kind of term do you have on your first mortgage?");
      return false;
     }
    
     var hasSecMtgVal;

     for(var i=0; i < form.hasSecondMortgage.length; i++) {
         if (form.hasSecondMortgage[i].checked) {
             hasSecMtgVal = form.hasSecondMortgage[i].value;
         }
     }

     if (hasSecMtgVal=="yes") {

         if(form.secondMortgageBalance.value=="") {
             alert("Please enter the current balance of your second mortgage.");
             return false;
         }

         if(form.secondMortgagePayment.value=="") {
             alert("Please enter the monthly payment of your second mortgage.");
             return false;
         }
     }
				
     if (form.householdIncome.value==""){
      alert("Please enter you annual household income.");
      return false;
     }
     
     if (form.workLength.value==""){
      alert("Please enter the duration at the current job.");
      return false;
     }
     
     if (form.creditHistory.value==""){
      alert("Please put in your credit history.");
      return false;
     }
     
     if (form.bankruptcy.value==""){
      alert("Please list your bankruptcy status.");
      return false;
     }
     
     if (form.firstName.value==""){
      alert("Please enter your first name.");
      return false;
     }
     
     if (form.lastName.value==""){
      alert("Please enter your last name.");
      return false;
     }
     
     if (form.addressStreet.value==""){
      alert("Please enter your street address.");
      return false;
     }
     
     if (form.addressCity.value==""){
      alert("Please enter your city.");
      return false;
     }
     
     if ((form.addressZip.value.length != 5) || (isNaN(form.addressZip.value))) {
      alert("Please enter a valid 5-digit zip code.");
      return false;
     }
     
     var stripped = form.phoneWork.value.replace(/[\(\)\.\-\ ]/g, '');
     if (isNaN(parseInt(stripped))) {
        alert("The day phone number is not provided or contains illegal characters.");
        return false;
     }
     
     if (!(stripped.length == 10)) {
      alert("The day phone number is the wrong length.\nMake sure you included an area code.\n");
      return false;
     }
     
     var stripped = form.phoneEvening.value.replace(/[\(\)\.\-\ ]/g, '');
     if (isNaN(parseInt(stripped))) {
        alert("The evening phone number is not provided or contains illegal characters.");
        return false;
     }
     
     if (!(stripped.length == 10)) {
      alert("The evening phone number is the wrong length.\nMake sure you included an area code.\n");
      return false;
     }
     
     if (form.email.value==""){
       alert("Please enter your email address.");
       return false;
     }
     
     if (!emailCheck(form.email.value)) {
       return false;
     }
     
     return true;
}
	   
function checkSubmitStart(form){
     if (form.homeType.value==""){
      alert("Please tell us what kind of home you own.");
      return false;
     }

     if (form.addressState.value==""){
      alert("Please tell us what state you live in.");
      return false;
     }

     // If we've gotten this far, everything's valid!
     return true;
}

