function bookmark (sessionid) {
 var url="http://www.i4hotels.com/index.php/page0.44.html";
 var title="i4hotels.com - Las Vegas Discounts"
 var mt = /mac/i;
 var mac = mt.test(navigator.platform);
 if (document.all) {
  if (mac) {
   alert("For Quick Access to the Best Las Vegas Hotel Rates:\nClick <OK>, then press Apple+D to bookmark i4hotels.com");
  } else {
   window.external.AddFavorite(url,title);
   var tag="bookmark_IE"
  }
 } else {
  if (mac) {
   alert("For Quick Access to the Best Las Vegas Hotel Rates:\nClick <OK>, then press Apple+D to bookmark i4hotels.com");
  } else {
   alert("For Quick Access to the Best Las Vegas Hotel Rates:\nClick <OK>, then press CTRL+D to bookmark i4hotels.com");
  }
  var tag="bookmark_other"
 }
 if (document.imgtracker.src) {
  logChange(sessionid,tag);
 } 
}

function focusSearch() {
 if (document.search.Submit) {
  document.search.Submit.focus();
 }
}

function showWait(red) {
  //divMain.style.display = 'none';
  //divWait.style.display = '';
  redstr = "_red";
  // (red) ? "_red" : "";
  waitlogo = (waitlogo) ? waitlogo : "/images/i4hotels_waitinglogo.gif";
  waitPage = "<table valign=center width=100% height=100%>" +
             "<tr><td align=center><table width=400>"     +
             "<tr><td align=center><font class='text'><img name='logo' src='" + waitlogo + "'><BR><img name='i4hotels' src='/images/waitingblank.gif' width='400' height='134' border='0' ></font></td></tr>"          +
             "</table></td></tr></table>";
 Mac = navigator.userAgent.indexOf("Mac") != -1;
 Msie = navigator.userAgent.indexOf("MSIE") != -1;
 if (!(Mac && Msie)) {
  if (document.documentElement && document.all)
    document.body.innerHTML = waitPage;
  else if (document.all)
    document.body.innerHTML = waitPage;
  else if (document.getElementById)
    document.documentElement.innerHTML = waitPage;
  else if (contentDocument)
    contentDocument.body.innerHTML  = waitPage
 }
}

function showWaitD() {
 setTimeout('showWait(false)', 500);
}

function showWait2(red) {
  //divMain.style.display = 'none';
  //divWait.style.display = '';
  redstr = "_red";
  // (red) ? "_red" : "";
  waitPage = "<table valign=center width=100%>" +
             "<tr><td align=center><table width=400>"     +
             "<tr><td align=center><font class='text'><img name='logo' src='/images/i4hotels_waitinglogo.gif' width='253' height='144'><BR><img name='i4hotels_inter' src='/images/waitingblank.gif' width='400' height='134' border='0' ></font></td></tr>"          +
             "</table></td></tr></table>";
  if (waitdiv = document.getElementById("waitdiv")) {
   waitdiv.style.visibility = 'visible';
   waitdiv.innerHTML = waitPage;
  }
}

function showWaitD2() {
 setTimeout('showWait2(false)', 500);
}

/*function showWaitD(red) {
 setTimeout('showWait('+red+')', 500);
}*/


todayDate = new Date();

function setToday (secs) {
 todayDate = new Date(secs*1000);
}

function doSO(f,tV,tF){
  for(i=0;i<f[tF].options.length;i++)
   if(f[tF].options[i].value==tV)
    {
      f[tF].options[i].selected=true;
    }
}


function dateChange(f,field) { 
 //window.status = field.name;
 thisdate = new Date(f.inyear[f.inyear.selectedIndex].value, f.inmonth[f.inmonth.selectedIndex].value-1, 
                     f.inday[f.inday.selectedIndex].value); 
 outdate  = new Date(f.outyear[f.outyear.selectedIndex].value, f.outmonth[f.outmonth.selectedIndex].value-1, 
                     f.outday[f.outday.selectedIndex].value); 
 anotherdate = new Date(thisdate);
 
 testdate = new Date(outdate - (86400000*14));
 
 if ((thisdate-0)<(outdate-0) && anotherdate >= testdate && ((f.inmonth[f.inmonth.selectedIndex].value-1) >= todayDate.getMonth() ||
     f.inyear[f.inyear.selectedIndex].value > todayDate.getFullYear())) {
  return;
 }
 msecs = (thisdate-0); 
 twoday = new Date(msecs+(86400000*2)); 
 im = thisdate.getMonth()+1; 
 id = thisdate.getDate(); 
 iy = thisdate.getFullYear(); 
 om = twoday.getMonth()+1; 
 od = twoday.getDate(); 
 oy = twoday.getFullYear();
 
 if (im < (todayDate.getMonth()+1) && iy == todayDate.getFullYear()) {
  //window.status = im + " < " + (todayDate.getMonth()+1);
  /*
  newsecs = (todayDate-0);
  newtwoday = new Date(newsecs+(86400000*2));
  */
//  im = todayDate.getMonth()+1;
//  om = newtwoday.getMonth()+1;
  if (field.name != "inyear" && field.name != "outyear") {
   iy = thisdate.getFullYear() + 1;
   oy = outdate.getFullYear() + 1;
  }
 // id = todayDate.getDate();
 // od = newtwoday.getDate();
  
 }
 
 doSO(f,im,"inmonth"); 
 doSO(f,id,"inday"); 
 doSO(f,iy,"inyear"); 
 doSO(f,om,"outmonth"); 
 doSO(f,od,"outday"); 
 doSO(f,oy,"outyear"); 
}

function validateCity(f){
//  if(f.FrAirport.value == ""){
//    alert("Please enter the city you are departing from. There may be regional travel discounts.");
//    f.FrAirport.focus();
//    return false;
//  } else {
    return true;
//  }
}

var formSubmitted;
function checkSubmit() {
 if (formSubmitted) {
  alert('Already processing, please wait a moment...');
  return false;
 } else {
  formSubmitted = true;
  return true;
 }
}

function logChange(sessionid,tag,form)
{
 window.status='Done';
 if (form) {
  str="";
  for (x=0;x<form.elements.length;x++) {
   if (form.elements[x].value && form.elements[x].name!='CCnum') {
    str = str + "&" + form.elements[x].name + "=" + form.elements[x].value;
   }
  }
  eval("document.imgtracker.src = '/imgtracker.php?sessionid=' + sessionid + '&tag=' + tag + '&data=' + escape(str)");
 } else { 
  eval("document.imgtracker.src = '/imgtracker.php?sessionid=' + sessionid + '&tag=' + tag");
 }
 window.status='Done';
}


var nameSet;
function setFirstRoomname (form) { 
 
 for (x=0;x<form.elements.length;x++) {
  if (form.elements[x].name.indexOf("roomname[]")!=-1) {
   if (!nameSet) {
    form.elements[x].value = form.fname.value + " " + form.lname.value;
    nameSet = true;
   }
   break;
  }
 }
}

var ccNameSet;
function setCCName (form) { 
 
 for (x=0;x<form.elements.length;x++) {
  if (form.elements[x].name.indexOf("CCname")!=-1) {
   if (!ccNameSet) {
    form.elements[x].value = form.fname.value + " " + form.lname.value;
    ccNameSet = true;
   }
   break;
  }
 }
}

function setFirstPassenger (form) { 
 for (x=0;x<form.elements.length;x++) {
  if (form.elements[x].name.indexOf("airfname[]")!=-1) {
   if (!nameSet) {
    form.elements[x].value = form.fname.value;
   }
   break;
  }
 }
 for (x=0;x<form.elements.length;x++) {
  if (form.elements[x].name.indexOf("airlname[]")!=-1) {
   if (!nameSet) {
    form.elements[x].value = form.lname.value;
   }
   break;
  }
 }
 nameSet = true;
}


function setFirstRoomname2 (form) { 
 for (x=0;x<form.elements.length;x++) {
  if (form.elements[x].name.indexOf("roomfname[]")!=-1) {
   if (!nameSet) {
    form.elements[x].value = form.fname.value;
   }
   break;
  }
 }
 for (x=0;x<form.elements.length;x++) {
  if (form.elements[x].name.indexOf("roomlname[]")!=-1) {
   if (!nameSet) {
    form.elements[x].value = form.lname.value;
   }
   break;
  }
 }
 nameSet = true;
}


function setBillingInfo(form) {
  if (form.same.checked) {
   form.badd1.value  = form.add1.value;
   form.badd2.value  = form.add2.value;
   form.bzip.value   = form.zip.value;
   form.bcity.value  = form.city.value;
   form.bstate.value = form.state.value;
  }
}

function checkDates(form) {
  inday    = form.inday[form.inday.selectedIndex].value;
  inmonth  = form.inmonth[form.inmonth.selectedIndex].value;
  inyear   = form.inyear[form.inyear.selectedIndex].value;

  outday   = form.outday[form.outday.selectedIndex].value;
  outmonth = form.outmonth[form.outmonth.selectedIndex].value;
  outyear  = form.outyear[form.outyear.selectedIndex].value;

  d1 = new Date(inyear,inmonth-1,inday);
  d2 = new Date(inyear,inmonth-1,1);
  d3 = new Date(outyear,outmonth-1,outday); 
  d4 = todayDate;
  d5 = new Date(d4.getFullYear(),d4.getMonth(),d4.getDate());
  message = ""
 // window.status = outyear + "-" + outmonth + "-" + outday + " = " + d3;
  if (d1.getMonth() != d2.getMonth()) {
   message += "* Your check-in date is not valid.\n";
  } 
  if (d1.getTime() >= d3.getTime()) {
   message += "* Your check-out date must not be the same, or before your check-in date.\n";
  }
 /* if (d1.getTime() < (d5.getTime()-86400000)) {
   message += "* Your check-in date may not be before today.\n";
  }*/
  if (message.length > 0) {
   alert("Please address the following errors:\n" + message);
   return false;
  }
  return true;
}

function validateForm(form) {
  message = ""
  // Check Name
  if (isEmpty(form.fname.value)) {
   message += "* Please enter your first name\n";
  }
  if (isEmpty(form.lname.value)) {
   message += "* Please enter your last name\n";
  }
  // Check Address
  if (isEmpty(form.add1.value)) {
   message += "* Please enter your street address\n";
  }
  // Check City/State/Zip
  if (isEmpty(form.city.value)) {
   message += "* Please enter your city\n";
  }
  if (!isStateCode(form.state.value.toUpperCase())) {
   message += "* Please enter a valid U.S. state abbreviation (CA for California,etc)\n";
  }
  if (!isZipCode(form.zip.value)) {
   message += "* Please enter a valid 5 digit U.S. zip code\n";
  }
  // Check Phone
  if (!isPhone(form.phone.value)) {
   message += "* Please enter a valid 10 digit U.S. phone number\n";
  } else {
   form.phone.value = formatPhone(form.phone.value);
  }
  // Check E-mail
  if (!isEmail(form.email.value)) {
   message += "* Please enter a valid e-mail address\n";
  }
  if (isEmpty(form.CCnum.value)) {
   message += "* Please enter your credit card number\n";
  } else {
   // Check Credit Card Type
   if (!checkCardType(form)) {
    message += "* Please make sure you have selected the correct credit card type\n";
   }
   // Check Credit Card Luhn Mod 10
   if (!isCreditCard(form.CCnum.value)) {
    message += "* Please make sure you have entered the credit card number correctly\n";
   }
  }
  // Check Credit Card Exp.
  if (!checkExp(form.CCexp.value)) {
   message += "* Please enter a valid credit card expiration date (MM/YYYY)\n";
  }
  // Check Credit card Name
  if (isEmpty(form.CCname.value)) {
   message += "* Please enter the credit card holder's name (first last)\n";
  }
  // Check Credit Card Address
  if (isEmpty(form.badd1.value) && !form.same.checked) {
   message += "* Please enter your billing address\n";
  }
  // Check Credit Card City/State/Zip
  if (isEmpty(form.bcity.value) && !form.same.checked) {
   message += "* Please enter your billing city\n";
  }
  if (!isStateCode(form.bstate.value) && !form.same.checked) {
   message += "* Please enter a valid U.S. state abbreviation for your billing address\n";
  }
  if (!isZipCode(form.bzip.value) && !form.same.checked) {
   message += "* Please enter a valid 5 digit U.S. zip code for your billing address\n";
  }

  // Check if we had an error.
  if (message.length > 0) {
   alert("Please address the following errors:\n" + message);
   return false;
  } else {
   //form.submit();
   return true;
  }
}

function isEmail(st) {
 if ((st.indexOf("@") == -1) || (st.charAt(0) == ".") || 
     (st.charAt(0) == "@") || (st.length < 6) || (st.indexOf(".") == -1) || 
     (st.charAt(st.indexOf("@")+1) == ".") || 
     (st.charAt(st.indexOf("@")-1) == "."))
  {
    return false;
  }
 return true;
}

var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";
function isStateCode(s)
{   
    return ( (USStateCodes.indexOf(s) != -1) &&
             (s.indexOf(USStateCodeDelimiter) == -1) )
}


function isEmpty(s) { 
  return ((s == null) || (s.length == 0))
}

// Removes all characters which do NOT appear in string bag 
// from string s.
function stripCharsNotInBag (s, bag) {
    var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

function isPhone (st) {
 st =  stripCharsNotInBag (st,"0123456789");
 return (st.length == 10);
}

function formatPhone (st) {
 
 st = stripCharsNotInBag (st,"0123456789");
 st = st.split("");
 return "(" + st[0] + st[1] + st[2] + ") " + st[3] + st[4] + st[5] + "-" + st[6] + st[7] + st[8] + st[9];
}

function isZipCode (st) {
 st =  stripCharsNotInBag (st,"0123456789");
 return (st.length == 5);
}


function checkExp(st) {
 st = st.split("/");
 if (st.length < 2) {
  return false;
 } else if ((st[0] >= 1 && st[0] <= 12) && st[1].length == 4 && st[1] >= 2002) {
    return true;
 }
 return false;
}

function checkCardType(form) {
  CCtype = form.CCtype[form.CCtype.selectedIndex].value;
  CCnum = stripCharsNotInBag (form.CCnum.value, "0123456789");
  if (CCtype == "Visa") {
    return isVisa(CCnum);
  } else if (CCtype == "Mastercard") {
    return isMasterCard(CCnum);
  } else if (CCtype == "Discover") {
    return isDiscover(CCnum);
  } else if (CCtype == "Amex") {
    return isAmericanExpress(CCnum);
  } 
}

// Test card to see if it passes Luhn Mod-10
function isCreditCard(st) {
  st = stripCharsNotInBag (st, "0123456789");
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()


function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()

