function checkrepeat(str, n) {
	for (var i = 0; i < str.length; i++) {
	  var j = 0;
	  for (j; j < n; j++) {
	    if(!(str.charAt(i)==str.charAt(i+j+1))) 
	      break;
	  }
	  if (j == n) return true;
	}
	return false;
}
function move2next_3(fielda, fieldb) {
  if (fielda.value.length == 3)
		fieldb.focus();
}

function move2next_2(fielda, fieldb) {
  if (fielda.value.length == 2)
		fieldb.focus();
}
    function checkForm()
    {
       var temp;
        var re;

        // added by Cristine for sub id
        var urlString = new String();
        urlString = location.search;
        newString = urlString.substr(1, urlString.length);
        parameters = newString.split("&");

        for ( i=0; i < parameters.length; i++ ) {
            pair = parameters[i].split("=");

            
            if (pair[0] == 'subid') {
                document.autoForm.subid.value = pair[1];
            }
            if (pair[0] == 'refid') {
                document.autoForm.rcid.value  = pair[1];
            } 
        }
        // end add for sub/refid

        if (document.autoForm.first_name.value == "") {
            alert("Please enter a first name!");
      		document.autoForm.first_name.focus();            
            return false;
        }

        if (document.autoForm.last_name.value == "") {
            alert("Please enter a last name!");
      		document.autoForm.last_name.focus();            
            return false;
        }

        if (document.autoForm.street_address.value == "") {
            alert("Please enter an address!");
      		document.autoForm.street_address.focus();            
            return false;
        }

        if (document.autoForm.city.value == "") {
            alert("Please enter a city!");
      		document.autoForm.city.focus();            
            return false;
        }
        
        if (document.autoForm.state.value == "") {
            alert("Please enter a state!");
      		document.autoForm.state.focus();            
            return false;
        }
                        
        if (document.autoForm.zip.value == "") {
            alert("Please Type In Your ZIP!");
      		document.autoForm.zip.focus();            
            return false;
        }

       if (document.autoForm.home_phone1.value == ""
            || document.autoForm.home_phone2.value == ""
            || document.autoForm.home_phone3.value == "")
        {
            alert("Home Phone field is empty!");
      		document.autoForm.home_phone1.focus();            
            return false;
        } else {
            temp = document.autoForm.home_phone1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("Home Phone format error!");
       			document.autoForm.home_phone1.focus();            
                return false;
            }
            temp = document.autoForm.home_phone2.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("Home Phone format error!");
      			document.autoForm.home_phone2.focus();            
               return false;
            }
            temp = document.autoForm.home_phone3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("Home Phone format error!");
    	  		document.autoForm.home_phone3.focus();            
                return false;
            }
            document.autoForm.home_phone.value = document.autoForm.home_phone1.value + document.autoForm.home_phone2.value + document.autoForm.home_phone3.value;
            if (checkrepeat(document.autoForm.home_phone.value, 5)) {
            	alert("Home Phone conatins repetitive characters.");
	      		document.autoForm.home_phone1.focus();            
            	return false;
            }
        }

        if (document.autoForm.email_address.value == "") {
            alert("Email field is empty!");
            document.autoForm.email_address.focus();
            return false;
        } else {
        	document.autoForm.email_address.value = trim(document.autoForm.email_address.value);
            temp = document.autoForm.email_address.value;
            if (temp.search(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/i) == -1) {
                alert("Email format error!");
		        document.autoForm.email_address.focus();
                return false;
            }
        }
        document.autoForm.autofill.value = "yes";
        return true;
    }
    
function trim(str) {
  return str.replace(/^\s+|\s+$/g,"");
}    

function checkForm2()
    {
        if (document.autoForm.work_phone1.value == ""
            && document.autoForm.work_phone2.value == ""
            && document.autoForm.work_phone3.value == "")
        {
            // null
        } else {
            temp = document.autoForm.work_phone1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("work Phone format error!");
                return false;
            }
            temp = document.autoForm.work_phone2.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("work Phone format error!");
                return false;
            }
            temp = document.autoForm.work_phone3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("work Phone format error!");
                return false;
            }
            document.autoForm.work_phone.value = document.autoForm.work_phone1.value 
            + document.autoForm.work_phone2.value + document.autoForm.work_phone3.value;
            if (checkrepeat(document.autoForm.work_phone.value, 5)) {
            	alert("Work Phone conatins repetitive characters.");
            	return false;
            }
        }

        if (document.autoForm.field_1.value == "") {
            alert("Please enter a your social security number!");
      		document.autoForm.field_1.focus();            
            return false;
        }

       if (document.autoForm.dob_month.value == ""
            || document.autoForm.dob_day.value == ""
            || document.autoForm.dob_year.value == "")
        {
            alert("Your birthday is empty!");
      		document.autoForm.dob_month.focus();            
            return false;
        } else {
            temp = document.autoForm.dob_month.value;
            if (temp.search(/^[0-9]{2}$/) == -1) {
                alert("Date of birth format error!");
       			document.autoForm.dob_month.focus();            
                return false;
            }
            temp = document.autoForm.dob_day.value;
            if (temp.search(/^[0-9]{2}$/) == -1) {
                alert("Date of birth format error!");
      			document.autoForm.dob_day.focus();            
               return false;
            }
            temp = document.autoForm.dob_year.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("Date of birth format error!");
    	  		document.autoForm.dob_year.focus();            
                return false;
            }
            document.autoForm.field_2.value = document.autoForm.dob_year.value +'-'
            								+ document.autoForm.dob_month.value + '-' 
            								+ document.autoForm.dob_day.value;
            if (checkrepeat(document.autoForm.field_2.value, 5)) {
            	alert("Date of birth conatins repetitive characters.");
	      		document.autoForm.dob_month.focus();            
            	return false;
            }
        }

        if (document.autoForm.field_15.value == "") {
            alert("Please select a gender!");
      		document.autoForm.field_15.focus();            
            return false;
        }

        if (document.autoForm.field_3.value == "") {
            alert("Please select rent or own!");
      		document.autoForm.field_3.focus();            
            return false;
        }

        if ((document.autoForm.field_4.value == "") &&
        	(document.autoForm.field_5.value == "") ){
            alert("Please enter years or months at this address!");
      		document.autoForm.field_4.focus();            
            return false;
        } else if (document.autoForm.field_4.value > 100){
            alert("The years at address cannot be greater than 100!");
      		document.autoForm.field_4.focus();            
            return false;
        } else if (document.autoForm.field_5.value > 100) {
            alert("The months at the address cannot be greater thank 100!");
      		document.autoForm.field_5.focus();            
            return false;
        }
        
        if (document.autoForm.field_6.value == "") {
            alert("Please enter your employer");
      		document.autoForm.field_6.focus();            
            return false;
        }
        
        if ((document.autoForm.field_7.value == "") &&
        	(document.autoForm.field_8.value == "") ){
            alert("Please enter years or months at this job!");
      		document.autoForm.field_7.focus();            
            return false;
        } else if (document.autoForm.field_7.value > 100){
            alert("The years at job cannot be greater than 100!");
      		document.autoForm.field_7.focus();            
            return false;
        } else if (document.autoForm.field_8.value > 100) {
            alert("The months at the job cannot be greater thank 100!");
      		document.autoForm.field_8.focus();            
            return false;
        }

        if (document.autoForm.field_9.value == "") {
            alert("Please enter your job title");
      		document.autoForm.field_9.focus();            
            return false;
        }
                        
        if (document.autoForm.field_14.value == "") {
            alert("Please enter your monthly income.");
      		document.autoForm.field_14.focus();            
            return false;
        }

        if (document.autoForm.field_15.value == "") {
            alert("Please select your gender.");
      		document.autoForm.field_15.focus();            
            return false;
        }
        
        if (document.autoForm.field_16.value == "") {
            alert("Please enter current monthly income.");
      		document.autoForm.field_16.focus();            
            return false;
        }
        
        if (document.autoForm.field_11.value == "") {
            alert("Please select a bankruptcy option.");
      		document.autoForm.field_11.focus();            
            return false;
        }
        if (document.autoForm.field_12.value == "") {
            alert("Please select a co signer available.");
      		document.autoForm.field_16.focus();            
            return false;
        }

        if (document.autoForm.field_13.checked) {
            document.autoForm.field_13.value = 'Y';
            document.autoForm.field_18.value = 'Y';
        } 
                
        document.autoForm.autofill.value = "yes";
        return true;
    }
    

function checkForm3()
    {
        if (document.autoForm.ssn1.value == "")
        {
            alert("Please enter your social security number.");
      		document.autoForm.ssn1.focus();            
            return false;
        } else if  (document.autoForm.ssn2.value == "")
        {
            alert("Please enter your social security number.");
      		document.autoForm.ssn2.focus();            
            return false;
        } else if (document.autoForm.ssn3.value == "")
        {
            alert("Please enter your social security number.");
      		document.autoForm.ssn3.focus();            
            return false;
        } else { 
            temp = document.autoForm.ssn1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
	            alert("Please enter your social security number.");
	      		document.autoForm.ssn1.focus();            
	            return false;
            }
            temp = document.autoForm.ssn2.value;
            if (temp.search(/^[0-9]{2}$/) == -1) {
	            alert("Please enter your social security number.");
	      		document.autoForm.ssn2.focus();            
	            return false;
            }
            temp = document.autoForm.ssn3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
	            alert("Please enter your social security number.");
	      		document.autoForm.ssn3.focus();            
	            return false;
            }
            document.autoForm.field_1.value = document.autoForm.ssn1.value 
            + document.autoForm.ssn2.value + document.autoForm.ssn3.value;

        }

       if (document.autoForm.dob_month.value == ""
            || document.autoForm.dob_day.value == ""
            || document.autoForm.dob_year.value == "")
        {
            alert("Your birthday is empty!");
      		document.autoForm.dob_month.focus();            
            return false;
        } else {
            document.autoForm.field_2.value = document.autoForm.dob_year.value +'-'
            								+ document.autoForm.dob_month.value + '-' 
            								+ document.autoForm.dob_day.value;
        }

        if (document.autoForm.field_4.value == "") {
            alert("Please select time at residence!");
      		document.autoForm.field_4.focus();            
            return false;
        }
        
        if (document.autoForm.field_3.value == "") {
            alert("Please select rent or own!");
      		document.autoForm.field_3.focus();            
            return false;
        }

        
        if (document.autoForm.field_16.value == "") {
            alert("Please enter rent/mortgage.");
      		document.autoForm.field_16.focus();            
            return false;
        }
                 
        if (document.autoForm.field_6.value == "") {
            alert("Please enter your employer");
      		document.autoForm.field_6.focus();            
            return false;
        }
        
        if (document.autoForm.work_phone1.value == ""
            && document.autoForm.work_phone2.value == ""
            && document.autoForm.work_phone3.value == "")
        {
            // null
        } else {
            temp = document.autoForm.work_phone1.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("Employer Phone format error!");
                document.autoForm.work_phone1.focus();            
                return false;
            }
            temp = document.autoForm.work_phone2.value;
            if (temp.search(/^[0-9]{3}$/) == -1) {
                alert("Employer Phone format error!");
                document.autoForm.work_phone2.focus();            
                return false;
            }
            temp = document.autoForm.work_phone3.value;
            if (temp.search(/^[0-9]{4}$/) == -1) {
                alert("Employer Phone format error!");
                 document.autoForm.work_phone3.focus();            
                return false;
            }
            document.autoForm.work_phone.value = document.autoForm.work_phone1.value 
            + document.autoForm.work_phone2.value + document.autoForm.work_phone3.value;
            if (checkrepeat(document.autoForm.work_phone.value, 5)) {
            	alert("Employer Phone conatins repetitive characters.");
            	return false;
            }
        }
        if (document.autoForm.field_9.value == "") {
            alert("Please enter your job title");
      		document.autoForm.field_9.focus();            
            return false;
        }
                
        if ((document.autoForm.field_7.value == "") ){
            alert("Please select your time at job!");
      		document.autoForm.field_7.focus();            
            return false;
        } 

                        
        if (document.autoForm.field_14.value == "") {
            alert("Please enter your monthly income.");
      		document.autoForm.field_14.focus();            
            return false;
        }


        
        if (document.autoForm.field_11.checked) {
           document.autoForm.field_11.value = 'Y';
         }
        if (document.autoForm.field_12.checked) {
            document.autoForm.field_12.value = 'Y';
         }

        if (document.autoForm.field_13.checked) {
            document.autoForm.field_13.value = 'Y';
            document.autoForm.field_18.value = 'Y';
        } 
                
        document.autoForm.autofill.value = "yes";
        return true;
    }