﻿function IsFormValid(userForm) {
  var msg = "Please correct the following:\n\n";
  var isValid = true;
  
  if (userForm.txtYourName == null || trim(userForm.txtYourName.value) == "") {
    isValid = false;
    msg += "* Your Name is REQUIRED.\n"; 
  }
  
  if (userForm.txtEmail == null || trim(userForm.txtEmail.value) != "") {
    if (!isEmail(userForm.txtEmail.value)) {
       isValid = false;
       msg += "* Email does not look right.  Please check for typos.\n"; 
    }
  } else {
    isValid = false;
    msg += "* Email is REQUIRED.\n"; 
  }
  
  if (userForm.txtPhone == null || trim(userForm.txtPhone.value) != "") {
    if (!isPhone(userForm.txtPhone.value)) {
       isValid = false;
       msg += "* Phone does not look right.  Try this format: (###) ###-####\n"; 
    }
  } else {
    isValid = false;
    msg += "* Phone is REQUIRED.\n"; 
  }

  if (userForm.txtNumGuests == null || trim(userForm.txtNumGuests.value) != "") {
    if (!isPositiveWholeNumber(userForm.txtNumGuests.value)) {
       isValid = false;
       msg += "* # Guests needs to be a positive number\n"; 
    }
  }
  
  /*
  if (userForm.txtDateOfArrival == null || trim(userForm.txtDateOfArrival.value) != "") {
    if (!isDate(userForm.txtDateOfArrival.value)) {
       isValid = false;
       msg += "* Date Of Arrival doesnt look like a date. (dd-mm-yyyy) \n"; 
    }
  }
  
  if (userForm.txtDateOfDeparture == null || trim(userForm.txtDateOfDeparture.value) != "") {
    if (!isDate(userForm.txtDateOfDeparture.value)) {
       isValid = false;
       msg += "* Date Of Departure doesnt look like a date. (dd-mm-yyyy) \n"; 
    }
  }
  */


  //display error
  if (!isValid) {
     alert(msg);
     return false;
  } else {
     return true;
  }
  
}


/*
   PURPOSE: to send the email
*/
function update_message_body()
{        
    var userForm = document.EmailSubmit;

    if (IsFormValid(userForm)) {
       //assert: form passed all the rules

       var msgBody =  "\n*** INTERESTED CUSTOMER *** \n\n";
       msgBody  += "NAME: " + userForm.txtYourName.value + " \n";
       msgBody  += "EMAIL: "     + userForm.txtEmail.value  + " \n";
       msgBody  += "PHONE: "     + userForm.txtPhone.value  + " \n";
       msgBody  += "# GUESTS: "  + userForm.txtNumGuests.value    + " \n";
       msgBody  += "ARRIVAL: "   + userForm.txtDateOfArrival.value + " \n";
       msgBody  += "DEPARTURE: "   + userForm.txtDateOfDeparture.value + " \n";
       if(userForm.chkAreDatesFlexible.checked) {
          msgBody  += "DATES FLEXIBLE: Yes \n";
       } else {
          msgBody  += "DATES FLEXIBLE: No \n";
       }
       msgBody  += "\n*** COMMENTS ***" + " \n\n";
       msgBody  += "  " + trim(userForm.txtComments.value)  + " \n";
       
       document.proxy_form.message_body.value = msgBody;   
       var msgBody2 = msgBody;
       
//       for (i = 0; i< 100; i++) {     
//            msgBody2 = msgBody2.replace("\n","%0D%0A");
//       }
       for (i = 0; i< 10; i++) {     
             msgBody2 = msgBody2.replace("%","[percent]");
       }

       /*     
       if (!document.all) {
          //assert: not IE - need to switch out for octal carriage return line feeds
          for(i = 0; i< 100; i++) {     
            msgBody2 = msgBody2.replace("\n","%0D%0A");
          }
       } //end if IE            
       */
       
       //document.proxy_form.action +="&body=" + msgBody2;
       if (userForm.txtYourName != null && trim(userForm.txtYourName.value) != "") {
         document.proxy_form.realname.value = userForm.txtYourName.value;
       }
       document.proxy_form.email.value = userForm.txtEmail.value;
       document.proxy_form.submit();
     
       alert("Form Submitted - thank you.");

   } //end if valid
   return false; //dont really submit!!
}
