$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#EEEEEE"});
  });

  $('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 email = $("input#email").val();
    if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }

    var company = $("input#company").val();
    if (company == "") {
      $("label#company_error").show();
      $("input#company").focus();
      return false;
    }

    var dataString = 'name=' + name + '&email=' + email + '&company=' + company;
//    alert (dataString);return false;

    $.ajax({
      type: "POST",
      url: "../bin/process.php",
      data: dataString,
      dataType: "json",
      success: function(data) {
        if (data.data_return == "Safe!") {
          $('#contact_form').html("<div id='message'></div>");
          $('#message').html("<h2>Contact Form Submitted!</h2>")
          .append("<p>You will receive a mail from us with a link to download our brochure.</p>");
        }
        else {
          $('#contact_form').html("<div id='message'></div>");
          $('#message').html("<h2>Contact Form Submitted unsuccessfully because of " + data.data_return + "!</h2>")
          .append("<p>Please try again after sometime.</p>");
        }
      }
    });

    return false;
  });
});

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

