$(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 hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var phoneReg = /^\d{9}$/;
		var ageReg = /^\d{2}$/;
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }if(!emailReg.test(email)) {	
			$("input#email").after('<span class="error">Por favor, entra un email correcto.</span>');
			$("input#email").focus();
			return false;
			}
		var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }if(!phoneReg.test(phone)) {	
			$("input#phone").after('<span class="error">Por favor, entra un telefono correcto.</span>');
			$("input#phone").focus();
			return false;
			}
			
		var age = $("input#age").val();
		if (age == "") {
      $("label#age_error").show();
      $("input#age").focus();
      return false;
    }if(!ageReg.test(age)) {	
			$("input#age").after('<span class="error">Por favor, entra solo 2 d&#237;gitos</span>');
			$("input#age").focus();
			return false;
			}
		
		var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&age=' + age;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "http://fiestasexclusivas.net/fns.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Solicitud de Invitaci&#243;n entregada</h2>")
        .append("<p>Nos pondremos en contacto</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<p><strong>Gracias</strong></p>");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
