// JavaScript Document

$(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 subject = $("input#subject").val();
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
	
	var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }

		var message = $("#message").val();
		if (message == "") {
      $("label#message_error").show();
      $("input#message").focus();
      return false;		
	}
	
		var email = $("input#email").val();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (email == "") {
      		$("label#email_error").show();
      		$("input#email").focus();
      		return false;
    	}
		
		if(reg.test(email) == true){
			var dataString = 'name='+ name + '&email='+ email + ' &phone='+ phone + '&subject='+ subject + '&message='+ message;
			$.ajax({
			  type: "POST",
			  url: "post.php",
			  data: dataString,
			  success: function(data) {
				$('#contact_form').html("<div id='message'></div>");
				$('#message').html("<h4>Contact Form Submitted!</h4>")
				.append("<p>We will be in touch soon.</p>")
				.hide()
				.fadeIn(1500, function() {
				  $('#message').append("<img src='images/check.gif'/>");
				});
			  }
     		});
		} else {
			$("label#email_invalid").show();
      		$("input#email").focus();
			return false;
		}
			
    return false;
	});
});

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

