String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function submitTrialForm() {
  var nameRE = /^[A-Za-z][\-.\' A-Za-z]+$/;
  var emailRE = /^[A-Za-z0-9_\-.]+@[A-Za-z0-9_\-.]+$/;
  var zipRE = /^\d\d\d\d\d(|-\d\d\d\d)$/;
  var phoneRE = /^\D*\d\d\d\D*\d\d\d\D*\d\d\d\d\D*$/;
  var anyRE = /^.*$/;
  var vfields = new Array (
			   {re: nameRE, formID: "FirstName", display: "First name", req: true},
			   {re: nameRE, formID: "LastName", display: "Last name", req: true},
			   {re: emailRE, formID: "Email", display: "E-mail address", req: true},
			   {re: anyRE, formID: "StreetAddress1", display: "Street address", req: true},
			   {re: nameRE, formID: "City", display: "City", req: true},
			   {re: anyRE, formID: "State", display: "State", req: true},
			   {re: zipRE, formID: "PostalCode", display: "Zip code", req: true},
			   {re: phoneRE, formID: "Phone1", display: "Daytime phone", req: true},
			   {re: phoneRE, formID: "Phone2", display: "Evening/other phone", req: false},
			   {re: anyRE, formID: "_BusinessLocation", display: "Franchise location", req: true},
			   {re: anyRE, formID: "_Goals", display: "Goals", req: true}
			   );

  for (vf in vfields) {
    formID = "Contact0" + vfields[vf].formID;
    element = document.getElementsByName(formID)[0];
    formVal = (element.value = element.value.trim());
    if ( formVal == "" ) {
      if ( vfields[vf].req ) {
	alert("The " + vfields[vf].display + " field is a required field. Please enter a value and re-submit.");
	return;
      }
      else {
	continue;
      }
    }
    if ( !vfields[vf].re.test(formVal) ) {
      alert("The '" + vfields[vf].display + "' field looks invalid. Check for punctuation or numbers "
	    + "that don't belong in names, a missing @ sign in e-mails, or insufficient digits in numeric fields.");
      return;
    }
  }
  
  document.getElementById("submitnow")["disabled"] = "yes";
  document.getElementById("confirmation_message").innerHTML = "Thank you for the submission."
    + " Please wait. You will be returned to the testimonials page after the submission is complete.";
  setTimeout(doSubmit(), 2000);
}

function doSubmit() {
  formObj = document.getElementById("freeWeekTrial");
  formObj.action = "http://getinshapeforwomen.com/cgi-bin/dbupdate-repost.pl";
  formObj.submit();
}
