/**
 * @author bernardo
 */
$().ready(function(){
 //hide the result div -  for holding response form php script
 $("#result").hide();
 
 //loader spinning image
 var loader = jQuery('<div id="loader"><img src="/images/loading.gif" alt="loading..." /></div>')
   .css({position: "relative", top: "70px", left: "150px"})
   .appendTo("#content")
   .hide();
//ajaxSubmit options
 var options = {
  target: '#result',
  beforeSubmit: checkForm,
  success: returnHandler
 }
 //set-up form for ajax
//$('#contactForm').ajaxForm(options);

 
 //handle return form php - error or success 
 function returnHandler(evt) {
     
     $('#result').html('<p><strong>Thank you. <br />Your request has been sent</strong></p>').show();
     return false;
 }
  //ajax event handlers
  $("#contactForm").ajaxStart(function(){
     loader.show(),$('#contactForm').hide();
   }).ajaxStop(function(){
     loader.hide();
   }).ajaxError(function(){
     //ajax error return
   }); 
	 
	  
function checkForm() {
 if(!$('#contactForm').valid()) {
  //alert('errors');
  return false;
 } else {
   //$(form).ajaxSubmit(options);
   //var queryString = $('#contactForm').formSerialize();
   // 
   return true;
 }
}

$('#contactForm').submit(function() {
  //alert('form submitted');
  $(this).ajaxSubmit(options);
  return false;
});

//validate
 $('#contactForm').validate({
   
    rules: {
      firstname: {
        required: true,
        validChars: true
      },
      lastname: {
        required: true,
        validChars: true
      },
    email: {
      required: true,
      email: true,
      validChars: true
     },
    phone: {
      required: true,
      phone: true,
      validChars: true
    },
    bestTime: {
      validChars: true
    },
    comments: {
      validChars: true
    }
  },
  messages: {
    firstname: 'please enter your first name',
    lastname: 'pleae enter your last name',
    email: {
      required:  'please enter your email',
      email: 'please enter a valid email address'
    },
    phone: {
      required:'please enter your phone number',
      phone: 'please enter a valid  phone number'
     }
    }
 });	
});
