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

  $("#customForm").submit(function(){
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	var name = $("input#name").val();
	if (name == 'Name' || name.length < 3) {
      $("span#nameInfo").show();
      $("input#name").focus();
      return false;
    }
	
	//var a = $("#email").val();
	//var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
	//if it's valid email
	//if(filter.test(a)){
			
	var email = $("input#email").val();
	if (email == "e-Mail address" || email.length < 6) {
      $("span#emailInfo").show();
      $("input#email").focus();
      return false;
    }
	
	var boxesTicked = ""
		for (i = document.getElementsByName('terms').length - 1; i >= 0; i--) {
			if (document.getElementsByName('terms')[i].checked) {
				boxesTicked = boxesTicked + document.getElementsByName('terms')[i].value + "\n"
			}
		}
		
	if (boxesTicked == "") {
	//var terms = $("input#terms");
	//if(terms.checked == false){
      $("span#termsInfo").show();
      $("input#terms").focus();
		return false;
	}
		
	var phone = $("input#phone").val();	
	var company = $("input#company").val();
	var pais = $("input#pais").val();
	var message = $("textarea#message").val();
	
	//Form submitted correctly
	//var submited = $("input#send").val();	
	
	var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&pais=' + pais + '&company=' + company + '&message=' + message;// '&submited=' + submited;
		//alert (dataString);return false;
		
	
		$.ajax({
      type: "POST",
      url: "http://www.critflow.com/sendMail.php",
      data: dataString,
      success: function() {
        $('#customForm').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("");
        });
      }
     });
    return false;
	});
	
});

