function validateEmailAddress() {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)) {
    return true;
  }

  alert("Invalid E-mail Address! Please re-enter.")
  return (false)
}

function submitToInnLink() { //submits to InnLink
  if ((document.frmCheckAvailability.selProperty != null) && (document.frmCheckAvailability.selProperty.selectedIndex == 0)) {
    //if (document.frmCheckAvailability.selProperty.selectedIndex == 0) {
      alert("Please select a Location!");
      document.frmCheckAvailability.selProperty.focus();
    //}
  }

  else if (document.frmCheckAvailability.selAdults.selectedIndex == 0) {
    alert("Please select a Number of Guests!");
    document.frmCheckAvailability.selAdults.focus();
  }

  else {
    populateInnLinkFormFields();
    //alert(document.frmInnLinkBooking.PI.value);
    //alert(document.frmInnLinkBooking._INMMM.value);
    //alert(document.frmInnLinkBooking._INDD.value);
    //alert(document.frmInnLinkBooking._INYY.value);
    //alert("Number of Nights: " + document.frmInnLinkBooking._NN.value);
    //alert(document.frmInnLinkBooking._NA.value);
    //alert(document.frmInnLinkBooking._NC.value);
    //alert(document.frmInnLinkBooking._PostedDates.value);
    //alert(document.frmInnLinkBooking.BS.value);
    //alert(document.frmInnLinkBooking._QuText.value);
    //alert(document.frmInnLinkBooking._QuText2.value);
    //alert(document.frmInnLinkBooking._Mbr.value);

    document.frmInnLinkBooking.action = "https://www.innlinkcrs.com/quickbook.asp";
    document.frmInnLinkBooking.target = "new";
    document.frmInnLinkBooking.submit();
    //location.href="/";
  }
}


function submitInnLinkBooking(bBooking) { //submits only locally (not to InnLink)
  document.frmInnLinkBooking.hdnCity.value = document.frmCitySearch.txtCity.value;
  document.frmInnLinkBooking.hdnState.value = document.frmCitySearch.selState.value;
  document.frmInnLinkBooking.hdnBooking.value = bBooking;
  document.frmInnLinkBooking.submit();
}


function validateCitySearchInput() {
  var string = document.frmCitySearch.txtCity.value;

  if (string == "") {
    alert("Please enter a City!");
    document.frmCitySearch.txtCity.focus();
    return false;
  }

  else if (!validateCityName(string) || (string.length == 1)) {
    alert("You have entered an invalid City. Please re-enter!");
    document.frmCitySearch.txtCity.focus();
    return false;
  }

  else if (document.frmCitySearch.selState.selectedIndex == 0) {
    alert("Please select a State!");
    document.frmCitySearch.selState.focus();
    return false;
  }
  return true;
}


function submitCitySearch() {
  if (validateCitySearchInput()) {
    submitInnLinkBooking(false);
  }
}


function populateInnLinkFormFields() {
  var sCheckinDay = document.frmCheckAvailability.selCheckinDate.value;
  var sCheckinMonth = document.frmCheckAvailability.selCheckinMonth.value;
  var sCheckinYear = document.frmCheckAvailability.selCheckinYear.value;

  var sCheckoutDay = document.frmCheckAvailability.selCheckoutDate.value;
  var sCheckoutMonth = document.frmCheckAvailability.selCheckoutMonth.value;
  var sCheckoutYear = document.frmCheckAvailability.selCheckoutYear.value;

  var tCheckinDate = new Date(sCheckinMonth + " " + sCheckinDay + ", " + sCheckinYear);
  var tCheckoutDate = new Date(sCheckoutMonth + " " + sCheckoutDay + ", " + sCheckoutYear);

  var nAmountOfNights = parseInt((tCheckoutDate - tCheckinDate) / 86400000); //date difference divided by the amount of milliseconds in one day

  with (document.frmInnLinkBooking) {
    if (document.frmCheckAvailability.hdnBookingCode != null) {
      PI.value = document.frmCheckAvailability.hdnBookingCode.value;
    }

     //PI.value = "TSHUNT";

    _INMMM.value = sCheckinMonth.substr(0, 3);
    _INDD.value = sCheckinDay;
    _INYY.value = sCheckinYear.substr(2, 2);

    _NN.value = nAmountOfNights; //number of nights
    _NA.value = document.frmCheckAvailability.selAdults.value; //number of adults
    _NC.value = document.frmCheckAvailability.selChildren.value; //number of children

    BS.value = document.frmCheckAvailability.txtTravelAgentID.value; //travel agent
    _QuText.value = document.frmCheckAvailability.txtPromoCode.value; //Promo Code
    _QuText2.value = document.frmCheckAvailability.txtCorpAccountID.value; //Corporate Account ID
    _Mbr.value = document.frmCheckAvailability.txtClubNumber.value; //INN-Pressive Club

    hdnCheckinDate.value = sCheckinMonth + " " + sCheckinDay + ", " + sCheckinYear;
    hdnCheckoutDate.value = sCheckoutMonth + " " + sCheckoutDay + ", " + sCheckoutYear;
  }
}


function submitCheckAvailability() {
  if (validateCitySearchInput()) {
    populateInnLinkFormFields();
    submitInnLinkBooking(true);
  }
}


function populateDateDropdowns(nCIDay, nCIMonth, nCIYear, nCODay, nCOMonth, nCOYear) {
  document.frmCheckAvailability.selCheckinDate.selectedIndex = nCIDay - 1;
  document.frmCheckAvailability.selCheckinMonth.selectedIndex = nCIMonth - 1;

  for (var i=0; i < document.frmCheckAvailability.selCheckinYear.length; i++) {
    if (document.frmCheckAvailability.selCheckinYear[i].value == nCIYear) {
      document.frmCheckAvailability.selCheckinYear[i].selected = true;
    }
  }

  document.frmCheckAvailability.selCheckoutDate.selectedIndex = nCODay - 1;
  document.frmCheckAvailability.selCheckoutMonth.selectedIndex = nCOMonth - 1;

  for (var i=0; i < document.frmCheckAvailability.selCheckoutYear.length; i++) {
    if (document.frmCheckAvailability.selCheckinYear[i].value == nCOYear) {
      document.frmCheckAvailability.selCheckinYear[i].selected = true;
    }
  }
}

  // Validating Names.
  function validateName(string) {
  var digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. ";
  var temp = string;
    for (var i=0; i < string.length; i++) {
      temp = string.substring(i,i+1)
      if (digits.indexOf(temp)==-1)
      {
        return false;
      }
    }
    return true;
  }

  function validateCityName(string) {
  var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.'- ";
  var temp = string;
    for (var i=0; i<string.length; i++) {
      temp = string.substring(i, i+1)
      if (characters.indexOf(temp) == -1) {
        return false;
      }
    }
    return true;
  }
