  $(document).ready(function(){


  });
  
/*
 * displayEmailForm
 *
 * Displays the post's email form
 */
function displayEmailForm(postID) {
  $("#email_form_" + postID).slideDown(400);
  $("#email_form_link_" + postID).hide();
}

/*
 * hideEmailForm
 *
 * Hide's the post's email form
 */
function hideEmailForm(postID) {
  $("#email_form_" + postID).slideUp(400);
  $("#email_form_link_" + postID).show();
}

/*
 * sendEmail
 *
 * Send the eamil
 */
function sendEmail(postID) {
  var errMessage = '';
  // check for input validation
  if ($("#name" + postID).val() == '') {
    errMessage += "Enter your name\n";
  }
  
  errMessage += validEmail($("#email" + postID).val());
  
  if ($("#question" + postID).val() == '') {
    errMessage += "Enter your question\n";
  }
  // Serialize the form data
  var serializedForm = 'author_email=' + $("#author_email" + postID).val();
  serializedForm    += '&name=' + $("#name" + postID).val();
  serializedForm    += '&email=' + $("#email" + postID).val();
  serializedForm    += '&subject=' + $("#subject" + postID).val();
  serializedForm    += '&question=' + $("#question" + postID).val();
  serializedForm    += '&permalink=' + $("#permalink" + postID).val();
  serializedForm    += '&phone=' + $("#phone" + postID).val();
  
  if(document.getElementById("call_me" + postID).checked) {
    serializedForm    += '&call_me=' + $("#call_me" + postID).val();
    if ($("#phone" + postID).val() == '' || $("#phone" + postID).val() == 'phone number') {
      errMessage += "Enter your phone number\n";
    }
  }
  if(document.getElementById("similar_cars" + postID).checked) {
    serializedForm    += '&similar_cars=' + $("#similar_cars" + postID).val();
  }
  if(document.getElementById("test_drive" + postID).checked) {
    serializedForm    += '&test_drive=' + $("#test_drive" + postID).val();
  }

  if(errMessage.length > 0) {
    errMessage = "Please provide the following information: \n\n" + errMessage;
    alert(errMessage);
  } else {
	  // Post the data
	  $.post(
	    '/send-email.php', 
	    serializedForm,
	    function() {
	      alert('email sent');
	    }
	  );
	  // Hide the form
	  jQuery().ajaxStop(
	    function() {
	      $("#email_form_"+postID).slideUp(400);
	      $("#email_form_link_" + postID).show();
	    }
	  );
  }
}

function emptyField(postID) {
  if ($("#phone" + postID).val() == 'phone number') {
    document.getElementById("phone" + postID).value = '';
    document.getElementById("call_me" + postID).checked = true;
  }
}

function validEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return "Enter a valid email address\n";
   } else {
     return '';
   }
}















