/*--------------------------------------------------------
Form validation Scripts for pancake-rocks.co.nz

Author: Aidan Curran, Stellar Web Works
Date: 8 Sep 2008
--------------------------------------------------------*/


function validateForm() {
   var valid = true;
   var errorString = "Please complete the missing information before continuing:\n\n";
   var theForm = document.forms.hydrangeacottages;
   
   if (theForm.Name.value == null || strip_spaces(theForm.Name.value) == "") {
      valid = false;
      errorString += "\t- Please enter your name\n";
   }
   if (theForm.Phone.value == null || strip_spaces(theForm.Phone.value) == "") {
      valid = false;
      errorString += "\t- Please enter a phone number that you may be contacted at\n";
   }
   if (theForm.email.value == null || !isValidEmail(theForm.email.value)) {
      valid = false;
      errorString += "\t- You must enter a valid email\n";
   }
   if (theForm.Subject.value == null || strip_spaces(theForm.Subject.value) == "") {
      valid = false;
      errorString += "\t- Please enter a subject for your message\n";
   }
   if (theForm.Message.value == null || strip_spaces(theForm.Message.value) == "") {
      valid = false;
      errorString += "\t- You did not enter a message\n";
   }
   if (theForm.VerificationCode.value != "33ag") {
      valid = false;
      errorString += "\t- You did not correctly enter the verification code\n";
   }
   if (!valid) {
      alert(errorString);
   } else {
    theForm.action = "/cgi-bin/customFormMail.pl";
   }
   return valid;
}


function isValidEmail(strValue)
{

   var apos = strValue.indexOf("@");
   var dotpos = strValue.lastIndexOf(".");
   if (apos<1||dotpos-apos<2) {
      return false;
   }
   else {
      return true;
   }
}


function getSelectVal(sel) {
   return sel.options[sel.selectedIndex].value;
}


function strip_spaces(inStr) {
   var stripped = inStr.replace(/^\W+/,'').replace(/\W+$/,'');
   return stripped;
}