/* Contact form handler */
var prepare_form = function () {


  var first_name = $("#first-name").val();
  var last_name = $("#last-name").val();
  var country = $("#country").val();
  var company = $("#company").val();
  var email = $("#email").val();
  var phone = $("#phone").val();
  var message = $("#message").val();
    
  //sets close link
  $("#close-link").click(function()
  {
    $("#contact-container").css("display", "none").empty();
    $("#overlay").css("display", "none");
    
  });

  //prepares form for ajax submit
  var options =
  {
    url: "contact/send/",
    type: "POST",
    dataType: "xml",
    beforeSubmit: function (formData, jqForm, options)
                  {
                    $("#form-message").html("Sending...");
                    return true;
                  },
    success:  function(responseXML)
              {
                var response_message = $("message", responseXML).text();
                var response_code = $("code", responseXML).text();
                
                $("#form-message").html($("message", responseXML).text());
                
                if (response_code == "0") //success!
                {
                  //console.log("hiding");
                  $("#form-message").delay(3000).queue(function() {
                    $("#contact-container").css("display", "none").empty();
                    $("#overlay").css("display", "none");
                  });
                }
              }
  }

  $("#contact-contents #send").click(function(event)
  {
    event.preventDefault();
    $("#contact-form").ajaxSubmit(options);
    
  });
  
  //first-name
  $("#first-name").focus(function(event)
  {
    if ($(this).val() == first_name)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#first-name").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(first_name);
    }
  });
  
  //last-name
  $("#last-name").focus(function(event)
  {
    if ($(this).val() == last_name)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#last-name").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(last_name);
    }
  });
  
  //country
  $("#country").focus(function(event)
  {
    if ($(this).val() == country)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#country").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(country);
    }
  });

  //company
  $("#company").focus(function(event)
  {
    if ($(this).val() == company)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#company").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(company);
    }
  });
  
  //email
  $("#email").focus(function(event)
  {
    if ($(this).val() == email)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#email").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(email);
    }
  });
  
  //phone
  $("#phone").focus(function(event)
  {
    if ($(this).val() == phone)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#phone").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(phone);
    }
  });

  //message
  $("#message").focus(function(event)
  {
    if ($(this).val() == message)
    {
      var obj = $(this);
      obj.val("");
      obj.css("color", "#8f8d8d");
    }
  });
  
  $("#message").blur(function(event)
  {
    if ($(this).val() == "")
    {
      var obj = $(this);
      obj.css("color", "#868686");
      obj.val(message);
    }
  });
  
}
