var web_root = getClientURL();

var homeURL = web_root + "/index.htm";
var srvcURL = web_root + "/tm_srvc.htm";
var spceURL = web_root + "/tm_spci.htm";
var aboutURL = web_root + "/tm_about.htm";
var cntacURL = web_root + "/tm_cntac.htm";
var buyURL = web_root + "/tm_buy.htm";

// menu contents
var divName = "midMenu"; // name of div block
var divText = "<a href='"+homeURL+"' class='refc0'>Home</a> <font class='vbar'> &nbsp;|&nbsp; </font> <a href='"+srvcURL+"' class='refc0'>Services</a> <font class='vbar'> &nbsp;|&nbsp; </font> <a href='"+spceURL+"' class='refc0'>Specialties</a> <font class='vbar'> &nbsp;|&nbsp; </font> <a href='"+buyURL+"' class='refc0'>Purchase</a> <font class='vbar'> &nbsp;|&nbsp; </font> <a href='"+aboutURL+"' class='refc0'>About Us</a> <font class='vbar'> &nbsp;|&nbsp; </font> <a href='"+cntacURL+"' class='refc0'>Contact Us</a>";

// setup the page menu
function menuSetup() { 
  var theDivObj = document.getElementById(divName); // initialize obj
  theDivObj.innerHTML = divText;
}

function isInt(passedVal) { // check if the string is an integer
  if (passedVal == "") {
    return false;
  }
  for (i=0; i<passedVal.length; i++) {
//  if (passedVal.charAt(i) != "-") { // allow the dash (e.g. 95762-8122);
      if (passedVal.charAt(i) < "0") {
        return false;
      }
      if (passedVal.charAt(i) > "9") {
        return false;
      }
//  }
  }
  return true;
}


// server URL detection
function getCGIsvrURL() {
  if ((window.location.protocol == "file:") ||
      (window.location.host == "localhost")) {
        return "http://localhost/scripts/trigmax";
  } else {
        return "http://www.trigmax.com/cgi-bin";
  }
}

// define the webpage loading path
function getClientURL() {
  if ((window.location.protocol == "file:") ||
      (window.location.host == "localhost")) {
        return "http://localhost/trigmax";
  } else {
        return "http://www.trigmax.com";
  }
}


function goto_main(form_name) {

  var the_form = document[form_name];
  the_form.clicType.value = "gotoMain";

  var cgi_root = getCGIsvrURL();
  var baseUrl = cgi_root + "/ct_adddest.pl?";
  submit_form(baseUrl, form_name);
} // end function goto_main(form_name)

function start_ini(form_name) { // same as startover, except has a def date
  var the_form = document[form_name];
  the_form.clicType.value = "startover";

  the_form.def_arr_date.value = getNextWeekDate(); // default arrival date to one week past current day

  var cgi_root = getCGIsvrURL();
  var baseUrl = cgi_root + "/ct_adddest.pl?";
  submit_form(baseUrl, form_name);
} // end function goto_neworder(form_name)

function restart (form_name) {
  if (confirm("All your itinerary will be cleared. Are you sure you want to start over?")) {
    var the_form = document[form_name];
    the_form.clicType.value = "startover";
  var cgi_root = getCGIsvrURL();
  var baseUrl = cgi_root + "/ct_adddest.pl?";
    submit_form(baseUrl, form_name);
  }
} // end function goto_neworder(form_name)

function new_order(form_name) {
  var the_form = document[form_name];
  the_form.clicType.value = "new_order";
  var cgi_root = getCGIsvrURL();
  var baseUrl = cgi_root + "/ct_adddest.pl?";
  submit_form(baseUrl, form_name);
} // end function new_order(form_name)

function submit_form(baseUrl, form_name) {
  document[form_name].submit();
} // end function submit_form(form_name)

function error_alert() {
  if (document.main_form != undefined) {
    var errorFlag = document.main_form.cgiErr.value;
    if (errorFlag == "yes") {
      alert(document.main_form.cgiMsg.value);
    }
  }
} // end function error_alert()

function faq_popup() {
  var web_root = getClientURL();

  var thisURL = web_root + "/info/ct_faq.htm";
  newWindow = window.open(thisURL, "newWin", "toolbar=no,location=no,scrollbars=yes,resizable=yes,width=650,height=600");
  newWindow.focus();
} // end function faq_popup()

function moredeals_popup() {
  alert("To be implemented.");
} // end function moredeals_popup()

// update the optional features in a desination tour

function user_signup(form_name) {
  var the_form = document[form_name];
  var uPswd = the_form.uPswrd.value;
  var uPswdCnfm = the_form.uPswrdCnfm.value;
  var uEmail = the_form.uEmail.value;

  if (uPswd != uPswdCnfm) {
    alert("Re-entered password does not match your password. Try it again.");
    the_form.uPswrdCnfm.select();
  } else if (uEmail == "") {
    alert("Please enter your email address.");
      the_form.uEmail.focus();
  } else {
    the_form.clicType.value = "signMeUp";
    var cgi_root = getCGIsvrURL();
    var baseUrl = cgi_root + "/ct_signup.pl?";
    submit_form(baseUrl, form_name);
  }
} // end function user_signup(form_name)

// user login. Load existing Itinerary
function u_login_back(form_name) {
  var the_form = document[form_name];
  the_form.clicType.value = "login_back";
    var cgi_root = getCGIsvrURL();
  var baseUrl = cgi_root + "/ct_login.pl?";
  submit_form(baseUrl, form_name);
}

function popup_page(hotel_id) {
  newWindow = window.open(hotel_id, "newWin", "toolbar=no,location=no,scrollbars=yes,resizable=yes,width=700,height=400");
  newWindow.focus();
}

function getTodayDate() {
  var currentTime = new Date();
  var currMonth = currentTime.getMonth() + 1; // getMonth returns 0-11, Jan-Dec
    if (currMonth < 10) {
      currMonth = "0"+currMonth;
    }
  var currDate = currentTime.getDate();
  var currYear = currentTime.getYear();
  //alert(""+currMonth+"-"+currDate+"-"+currYear+"");
  return(""+currMonth+"-"+currDate+"-"+currYear+"");
}

function getNextWeekDate() {
  var currentTime = new Date();
  // getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
  var dateSevenAfter = new Date(currentTime.getTime() + (7 * 86400000));
  var theMonth = dateSevenAfter.getMonth() + 1; // getMonth returns 0-11, Jan-Dec
    if (theMonth < 10) {
      theMonth = "0"+theMonth;
    }
  var theDate = dateSevenAfter.getDate();
  var theYear = dateSevenAfter.getYear();
  //alert(""+theMonth+"-"+theDate+"-"+theYear+"");
  return(""+theMonth+"-"+theDate+"-"+theYear+"");
}



