$(function() {
  $('.error').hide();
  /*$('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });*/
  
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}


  		// VALIDATE AND PROCESS THE FORM. 
  $("#submit_btn").click(function() {

		// first hide any error messages
    $('.error').hide();
		//process required fields
	  var name_first = $("input#name_first").val();
		if (name_first == "") {
      $("#name_first_error").show();
      $("input#name_first").focus();
      return false;
    }
		var name_last = $("input#name_last").val();
		if (name_last == "") {
      $("#name_last_error").show();
      $("input#name_last").focus();
      return false;
    }
	var email = $("input#email").val();
		if (email == "") {
      $("#email_error").show();
      $("input#email").focus();
      return false;
    }
	if(isValidEmailAddress(email)) { } 
		else {
	  $("#email_error").show();
      $("input#email").focus();
      return false;
						}
						
		var asc_name = $("input#asc_name").val();
		if (asc_name == "") {
      $("#asc_name_error").show();
      $("input#asc_name").focus();
      return false;
    }
	
						
		var zip = $("input#zip").val();
		if (zip == "") {
      $("#zip_error").show();
      $("input#zip").focus();
      return false;
    }
	
		var agreement = $("input#agreement").val();
		if ($('input[name=agreement]').is(':checked')) { }
		else {
      $("#agreement_error").show();
      $("input#agreement").focus();
      return false;
    }
	
	if ($('input[name=management_co]').is(':checked')) { 
		var management_co = $("input#management_co").val();
		} else {management_co = "No";}
	
	if ($("input#management_co_name").val() == "") { var management_co_name = "none"; } 
	else {
	var management_co_name = $("input#management_co_name").val();
	}
		
		var dataString = 'name_first='+ name_first + '&name_last=' + name_last + '&email=' + email + '&asc_name=' + asc_name +  '&zip=' + zip + '&management_co=' + management_co + '&management_co_name=' + management_co_name + '&agreement=' + agreement;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST", 
	  beforeSend: function(){$("#loading").addClass('loader'); $("#content").hide();  }, //show loading just when link is clicked
	  complete: function(){$("#loading").removeClass('loader'); }, //stop showing loading when the process is complete
      url: "process.php",
      data: dataString,
	  success: checkLogin
					 
	  
	/*  success: function() {
        $('#commentForm').html("<div id='message'></div>");
        $('#message').html("<h2>Contact Form Submitted!</h2>")
        .append("<p>We will be in touch soon.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }*/
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
		
function checkLogin(response)
{
 if (response == "success") {
   window.location='compare.php?user=' + $.cookie('compare_ID');
 } else {
  alert("There was an error processing your request. Please try again.");
 }
}

