$(function() {
    $('.error').hide(); // hide the error labels
    $("#preQualifyFormButton").click(function() {
     	
      // validate and process form here
      $('.error').hide();

	  var firstName = $("#firstName1").val();
  	  if (firstName == "") {
        $("label#fullName_error").show();
        $("#firstName1").focus();
        return false;
      }
  	  
  	  var lastName = $("#lastName1").val();
  	  if (lastName == "") {
        $("label#fullName_error").show();
        $("lastName1").focus();
        return false;
      }
  	  
  	var gender = $("#gender").val();
	  if (gender == "") {
      $("label#gender_error").show();
      $("gender").focus();
      return false;
    }
	  
  	 var birthMonth = $("#birthMonth").val();
	  if (birthMonth == "") {
	     $("label#birthMonth_error").show();
	     $("birthMonth").focus();
	     return false;
     }
	
	  var birthDay = $("#birthDay").val();
	  if (birthDay == "") {
	     $("label#birthDay_error").show();
	     $("birthDay").focus();
	     return false;
      }
	  
	  var birthYear = $("#birthYear").val();
	  if (birthYear != "") {
    	  var y = parseInt(birthYear);
    	  if (y!=birthYear) {
    		  birthYear="";
    	  } else {
    		  if (y<1900 || y>2100)
    			  birthYear="";
    	  }   	  
      }
	  
	  if (birthYear=="") {
		  $("label#birthYear_error").show();
	      $("birthYear").focus();
	      return false;
	  }
	  
 	  var phone = $("#phone1").val();
  	  if (phone == "" || !/^[0-9-\.\()]+$/.test(phone)  ) {
        $("label#phone_error").show();
        $("phone1").focus();
        return false;
      }
  	  
  	 var email = $("#email1").val();
 	  if (email == "" || !/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email)) {
       $("label#email_error").show();
       $("email1").focus();
       return false;
     }
 	  var dwelling = $("#dwellingType").val();
  	  if (dwelling == "") {
        $("label#dwelling_error").show();
        $("dwellingType").focus();
        return false;
      }
	  
	  var residency = $("#residencyDuration").val();
	  if (residency == "") {
		 $("label#residency_error").show();
	     $("residencyDuration").focus();
	     return false;
      }
 	  
 	 var street = $("#street").val();
	  if (street == "") {
      $("label#street_error").show();
      $("street").focus();
      return false;
     }
	  
	 var city = $("#city").val();
	  if (city == "") {
	      $("label#city_error").show();
	      $("city").focus();
	      return false;
      }
	  
	  var state = $("#state").val();
	  if (state == "") {
		 $("label#state_error").show();
	     $("state").focus();
	     return false;
      }
	  
	  var zipcode = $("#zipcode").val();
	  if (zipcode == "" || !/^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/.test(zipcode)) {
	      $("label#zipcode_error").show();
	      $("zipcode").focus();
	      return false;
      }
	    
	  var income = $("#incomeLevel").val();
	  if (income == "") {
		 $("label#income_error").show();
	     $("incomeLevel").focus();
	     return false;
      }
	  
	  var banking = $("#banking").val();
	  if (banking == "") {
		 $("label#banking_error").show();
	     $("banking").focus();
	     return false;
      }
	  
	  var employer = $("#employer").val();
	  if (employer == "") {
		 $("label#employer_error").show();
	     $("employer").focus();
	     return false;
      }
	  
	  var employment = $("#employmentDuration").val();
	  if (employment == "") {
		 $("label#employment_error").show();
	     $("employmentDuration").focus();
	     return false;
      }
	  var dob = birthMonth+"/"+birthDay+"/"+birthYear;
 	  var address = street+" "+ $("#apartment").val()+" "+city+", "+state+" "+zipcode;
  	  var dataString = 'locationId='+locationId+'&firstName='+firstName+'&lastName='+lastName+'&dateOfBirth='+dob+
  	  				   '&gender='+gender+'&email='+ email + '&phone=' + phone+
  	  				   '&address='+address+'&residencyDuration='+residency+'&dwelling='+dwelling+
  	  				   '&income='+income+'&employer='+employer+'&employmentDuration='+employment+'&banking='+banking;
  	  //alert(dataString);
  	  $.ajax({
		url: "/dmsgenie-cms/message/prequalify",
		type: "GET",
		data: dataString,
		cache: false,
		//success
		success: function (data, textStatus, XMLHttpRequest) {	
  		    var msg = "";
  		    //alert('post submit = '+data);
			if (data=="1") {
				msg = "<div id='success'>Thank you for contacting us.<br/><br/>"+
				      "Your application has been successfully submitted and will be reviewed.<br/><br/> One of our representatives will contact you.</div>";
				 //$("#firstName1").val("");
				 //$("#middleName").val("");
				 //$("#lastName1").val("");
				 //$("#gender").val("");
			} else {
				msg = "<div id='error'>We are unable to process your application.<br/>Please try again later.</div>"
			}
			displayMessage("#prequalifyFormContainer",msg);
		},
        error:function (xhr, err){
  		displayMessage("#prequalifyFormContainer",xhr.responseText);
        }  
  	  });
      return false;
    });
});



