$(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"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();

	  var name = $("input#attendee_name").val();
		if (name == "") {
      $("label#attendee_name_error").show();
      $("input#attendee_name").focus();
      return false;
    }

	  var name = $("input#last_name").val();
		if (name == "") {
      $("label#last_name_error").show();
      $("input#last_name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "" || !isValidEmailAddress(email)) {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
	

		
		var str = $("#contact").serialize();
		//$('#log').html(str);
		
		$.post("http://www.helloyoga.com/registration/submit.php", str,
		  function(data){
			  
			$('#contact_form').html("<div id='message'></div>");
			$('#event-form-title').hide();
			$('#message').html("<br><h4>Application Submitted!</h4><br>")
			.append("")
			.hide()
			.fadeIn(1500, function() {
			  $('#message').html("<br>");				   
			  $('#message').append(data);
			});
		  }, "text");


    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

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);
	}


