<!-- // *** travelrequest.js ***

// --- global variables ---

var oForm = 0;       // object set by initPage

// --- functions ---

function msgDone()
{
  var wName = sTrim(oForm.realname.value,'B');
  var wEmail = sTrim(oForm.email.value,'B');

  if (wName.length <= 0)
  {
    showError(oForm.realname,"Name is required.");
    return false;
  }
  if (oForm.email.value.length <= 0)
  {
    showError(oForm.email,"eMail address is required.")
    return false;
  }

  if (!isValidEmail(oForm.email.value))
  {
    showError(oForm.email,"You entered an invalid eMail address.")
    return false;
  }

  oForm.realname.value = wName;
  oForm.email.value = wEmail;
  oForm.subject.value  = "(Travel Request Form) "+wName;
  oForm.submit();
  oForm.realname.focus();
}

function showError(oFField,imsg)
{
  alert(imsg+"\nPlease try again.");
  oFField.focus();
}

function initPage()
{
  var eFirst = "@";
  var eSecond = "winecountryprivatetours"; 
  var eThird = "com";

  // *** set globals ***
  oForm = document.getElementById('fTravelRequest');

  // *** reset input area ***

  for (var i=0; i<oForm.elements.length; i++)
  {
    var oField = oForm.elements[i];

    if (oField.type == "submit") continue;
    if (oField.type == "hidden") continue;
    if (oField.type == "checkbox")
    {
      oField.checked=false;
      continue;
    }
    if (oField.type == "radio")
    {
      oField.checked=false;
      continue;
    }
    if (oField.type == "select-one")
    { // for each option set not selected, then set first as selected
      for (var x=0; x<oField.options.length; x++)
      { oField.options[x].selected = false; }
      oField.selectedIndex = 0; 
      continue; 
    }
    if (oField.type == "text") { oField.value = ""; continue; }
    if (oField.type == "textarea") { oField.value = ""; continue; }
  }
}

// ***** common functions ******

function isValidEmail(sText)
{
  var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;
  return reEmail.test(sText);
}

// ---- Trim a string of leading or trailing spaces or both ----

function sTrim(iString,iFlag)
{
  // iFlag:  L = Leading space(s) removal
  //         T = Trailing space(s) removal
  //         B = Both leading and trailing space(s) removal

  iFlag = iFlag.toUpperCase();
  if ((iFlag == "L") || (iFlag == "B"))
  {
    while (''+iString.charAt(0)==' ')
       iString = iString.substr(1,iString.length);
  }
  if ((iFlag == "T") || (iFlag == "B"))
  {
    while (''+iString.charAt(iString.length-1)==' ')
       iString = iString.substr(0,iString.length-1);
  }

  return iString;
}

window.onload = initPage;

