$(function() {
	

	$('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#D5E6F9"});
  });
  $('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#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;
		}
		else
		{
		var checkOK = "0123456789-+";
		var checkStr = phone;
		var allValid  = true;
		var allNum = "";
        
		for (i = 0;  i < checkStr.length;  i++)
		 {
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;

			if (j == checkOK.length)
		    {
				 allValid = false;
				 break;
			}
			if (ch != ",")
				 allNum += ch;
		}
		if (!allValid)
	    {
			$("label#phone_error1").show();
			$("input#phone").focus();
			return false;
		}
		
	}
	
		var fax = $("input#fax").val();
		if (fax == "") 
		{
			  $("label#fax_error").show();
			  $("input#fax").focus();
			  return false;
		}
		else
		{
		var checkOK = "0123456789-+";
		var checkStr = phone;
		var allValid  = true;
		var allNum = "";
        
		for (i = 0;  i < checkStr.length;  i++)
		 {
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;

			if (j == checkOK.length)
		    {
				 allValid = false;
				 break;
			}
			if (ch != ",")
				 allNum += ch;
		}
		if (!allValid)
	    {
			$("label#fax_error1").show();
			$("input#fax").focus();
			return false;
		}
		
	}

		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
	
	if(email != "")
	{
		var len = email.length;
			if (len < 6)
			{
				  $("label#email_error1").show();
				  $("input#email").focus();
				  return false;
			}
			var filter=/^.+@.+\..{2,3}$/
			if (!filter.test(email))	{
					  $("label#email_error2").show();
					  $("input#email").focus();
					  return false;
			  }		
	}
	
	
/*	var address = $("input#address").val();
		if (address == "") {
      $("label#address_error").show();
      $("input#address").focus();
      return false;
    }*/
	var subject = $("input#subject").val();
		if (subject == "") {
      $("label#subject_error").show();
      $("input#subject").focus();
      return false;
    }
	var dept = $("select#dept").val();
		if (dept == "") {
      $("label#dept_error").show();
      $("select#dept").focus();
      return false;
    }

var msg = $("#msg").val();
		if (msg == "") {
      $("label#msg_error").show();
      $("textarea#msg").focus();
      return false;
    }
		var company = $("#company").val();
		
		var dataString = 'name='+ name + '&company=' + company + '&phone=' + phone + '&fax=' + fax + '&email=' + email + '&dept=' + dept + '&subject=' + subject + '&msg=' + msg;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "process_mail.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message' style='width:300px;'></div>");
        $('#message').html("<b>Contact Form Submitted!</b>")
        .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();
});

