/* Copyright Kayak.com 2006
 * Kayak Developer Network
 * affiliate-support at kayak.com
 *
 */

/* If we're in the linkShare environment, the variable lsn_clk will be
 * defined and this function will respond in the affirmative.
 */
function isLinkShare() {
  return false;
}

function kykOpenWindow(url) {
  window.open(url);
}


function submitLink() {
  if (isLinkShare()) {
    linkshareLink();
  } else {
    simpleLink();
  }
}

function redirectLink(redirTo) {
  if (isLinkShare()) {
    linkshareRedirect(redirTo);
  } else {
    simpleRedirect(redirTo);
  }
}

function submitBuzz(aiValue) {

  var ap = document.forms['buzz'].ap;
  if ((null == ap) || (null == ap.value) || "" == ap.value) {
    alert("Missing 3 character airport code");
    return false;
  }
  ap = ap.value;

  var kayakUrl = 'http://lonelyplanet.kayak.com/s/search/buzz?ap=' + ap + '&ai=' + aiValue;
  redirectLink(kayakUrl);

}



/* Locate an object by its id and return handle to its
   style.
*/

function getObjStyle(divId) {
  var obj;

  if (document.getElementById)    // DOM3 = IE5, NS6
  {
    if (null == document.getElementById(divId)) {
      return null;
    }
    obj = document.getElementById(divId).style;
  }
  else if (document.layers) // Netscape 4
  {
    obj = document.divId;
  }
  else // IE 4
  {
    if (null == document.all.divId) {
      return null;
    }
    obj = document.all.divId.style;
  }
  return obj;
}

function getObj(objId) {
  var obj;


  if (document.getElementById)    // DOM3 = IE5, NS6
  {
    obj = document.getElementById(objId);
  }
  else if (document.layers) // Netscape 4
  {
    obj = document.objId;
  }
  else // IE 4
  {
    obj = document.all.objId;
  }


  return obj;
}

function selectLayer(buttonObj) {

  // input ids are of the form btn_#
  var cmpn = buttonObj.id.split('_');
  var num = cmpn[1];


  // First, disable all the buttons and panes
  for (var j = 1; j <= 4; j++) {
    var buttonName = "btn_" + j;
    var button = getObj(buttonName);
    if (null != button) {
      button.className = "btnOff";
    }

    var divName = "pane_" + j;
    var divObj = getObjStyle(divName);
    if (null != divObj) {
      divObj.visibility = 'hidden';
      divObj.display = 'none';
    }
   
  }

  // Now, turn on just the one button and pane
  var button = getObj("btn_" + num);
  buttonObj.className = "btnOn";

  var divObj = getObjStyle("pane_" + num);
  divObj.visibility = 'visible';
  divObj.display = 'inline';
}


function showLayer(buttonObj) {

  // input ids are of the form btn_#
  var cmpn = buttonObj.id.split('_');
  var num = cmpn[1];

  // First, disable all the buttons and panes
  for (var j = 1; j <= 4; j++) {

    var divName = "pane_" + j;
    var divObj = getObjStyle(divName);
    if (null != divObj) {
      divObj.visibility = 'hidden';
      divObj.display = 'none';
    }
    
  }
  var divObj = getObjStyle("pane_" + num);
  divObj.visibility = 'visible';
  divObj.display = 'inline';
}

function kayakSubmitForm(formName) {

  var ai = document.forms[formName].ai;
  if ((null == ai) || (null == ai.value) || "" == ai.value) {
    alert("Missing affiliate id form variable ai");
    return false;
  }

  if (formName == 'flight') {
    return submitFlight(ai.value);
  }

  else if (formName == 'hotel') {
    return submitHotel(ai.value);
  }

  else if (formName == 'car') {
    return submitCar(ai.value);
  }
  
  else if (formName == 'cruise') {
    return submitCruise(ai.value);
  }

  else if (formName == 'buzz') {
    return submitCar(ai.value);
  }


  else {
    alert("Unknown form " + formName);
    return false;
  }

}

function submitForm(formName) {

  var ai = document.forms[formName].ai;
  if ((null == ai) || (null == ai.value) || "" == ai.value) {
    alert("Missing affiliate id form variable ai");
    return false;
  }

  if (formName == 'flight') {
    return submitFlight(ai.value);
  }

  else if (formName == 'hotel') {
    return submitHotel(ai.value);
  }

  else if (formName == 'car') {
    return submitCar(ai.value);
  }
  
  else if (formName == 'cruise') {
    return submitCruise(ai.value);
  }

  else if (formName == 'buzz') {
    return submitCar(ai.value);
  }


  else {
    alert("Unknown form " + formName);
    return false;
  }

}

function submitFlight(ai) {

  var l1 = document.forms['flight'].l1.value;
  if ((null == l1) || (l1.length < 3)) {
    alert("Missing or Invalid Departure Airport");
    return false;
  }

  var l2 = document.forms['flight'].l2.value;
  if ((null == l2) || (l2.length < 3)) {
    alert("Missing or Invalid Destination Airport");
    return false;
  }

  var kayakUrl = 'http://lonelyplanet.kayak.com/s/search/air';
  if (isLinkShare()) {
    linkshareSubmit('flight', kayakUrl);
  } else {
    simpleSubmit('flight', kayakUrl);
  }
  return true;
}

function submitHotel(ai) {

  var l1 = document.forms['hotel'].crc.value;
  if ((null == l1) || (l1.length <= 3)) {
    alert("Missing or Invalid City.  Expecting something of form City,State or City,State,Country");
    return false;
  }

  var kayakUrl = 'http://lonelyplanet.kayak.com/s/search/hotel';
  if (isLinkShare()) {
    linkshareSubmit('hotel', kayakUrl);
  } else {
    simpleSubmit('hotel', kayakUrl);
  }
  return true;
}

function submitCar(ai) {

  var l1 = document.forms['car'].crc.value;
  if ((null == l1) || (l1.length < 3)) {
    alert("Missing or Invalid City.  Expecting something of form City,State or City,State,Country");
    return false;
  }

  var kayakUrl = 'http://lonelyplanet.kayak.com/s/search/car';
  if (isLinkShare()) {
    linkshareSubmit('car', kayakUrl);
  } else {
    simpleSubmit('car', kayakUrl);
  }

  return true;
}

function submitCruise(ai) {

  var kayakUrl = 'http://lonelyplanet.kayak.com/s/search/cruise';
  if (isLinkShare()) {
    linkshareSubmit('cruise', kayakUrl);
  } else {
    simpleSubmit('cruise', kayakUrl);
  }

  return true;
}

// Gather form parameters and submit
function simpleSubmit(fname, kayakUrl) {

    // eg of kayakUrl
    //   'http://lonelyplanet.kayak.com/s/search/air';
    //   'http://lonelyplanet.kayak.com/s/search/hotel';
    //   'http://lonelyplanet.kayak.com/s/search/car';

    var qstring = kayakUrl + "?";
    var argList = "";


    // Gather form elements
    for (i = 0; i < document.forms[fname].elements.length; i++) {

        if ((null != document.forms[fname]) &&
            (null != document.forms[fname].elements[i]) &&
            ("" != document.forms[fname].elements[i].name) &&
            ("" != document.forms[fname].elements[i].value) ) {
          argList = argList + '&' + document.forms[fname].elements[i].name + '=' +
                  escape(document.forms[fname].elements[i].value);
        }
    }


    // fix date
    if (argList.match(/\//g)) {
        argList = argList.replace(/\//g,"%2F");
    }


    qstring = qstring + argList;

    // Looks for "?&" within qstring and if found, replaces with "?"
    if (qstring.match(/\?\&/g)) {
        qstring = qstring.replace(/\?\&/g,"?");
    }


    // Submit request
    kykOpenWindow(qstring);
}


function simpleLink() {
  var kayakUrl = "http://kayak.com";
  simpleRedirect(kayakUrl);
}

function simpleRedirect(redirTo) {
  kykOpenWindow(redirTo);
}

function linkshareRedirect(redirTo) {
  var qstring = redirTo;
  var lsn_search_url = lsn_click + '&tmpid=2304&RD_PARM1=';


  // Looks for "?&" within qtring and if found, replaces with "?"
  if (qstring.match(/\?\&/g)) {
      qstring = qstring.replace(/\?\&/g,"?");
  }
  // Double escape form action
  qstring = escape(escape(qstring));

  kykOpenWindow(lsn_search_url + qstring);
}

function linkshareLink() {
  var kayakUrl = "http://kayak.com";
  linkshareRedirect(kayakUrl);
}


// Gather form parameters and submit
function linkshareSubmit(fname, kayakUrl) {
   // eg of kayakUrl
    //   'http://lonelyplanet.kayak.com/s/search/air';
    //   'http://lonelyplanet.kayak.com/s/search/hotel';
    //   'http://lonelyplanet.kayak.com/s/search/car';

    var qstring = kayakUrl + "?";

    // Gather form elements
    for (i = 0; i < document.forms[fname].elements.length; i++) {
        if ((null != document.forms[fname]) &&
            (null != document.forms[fname].elements[i]) &&
            ("" != document.forms[fname].elements[i].name) &&
            ("" != document.forms[fname].elements[i].value) ) {
          qstring = qstring + '&' + document.forms[fname].elements[i].name + '=' +
                  document.forms[fname].elements[i].value;
        }
    }

    // Looks for "?&" within qtring and if found, replaces with "?"
    if (qstring.match(/\?\&/g)) {
        qstring = qstring.replace(/\?\&/g,"?");
    }
    // Double escape form action
    qstring = escape(escape(qstring));

    // Submit request
    var lsn_search_url = lsn_click + '&tmpid=2304&RD_PARM1=';

    var finalUrl = lsn_search_url + qstring;
    kykOpenWindow(finalUrl);
}



 function compareDates(date1, date2) {
    if(date1 == null || date2 == null) {
      return false;
    }

  if(date1.getFullYear() < date2.getFullYear() || (date1.getFullYear() == date2.getFullYear() &&
  (date1.getMonth() < date2.getMonth() || (date1.getMonth() == date2.getMonth() && (date1.getDate() < date2.getDate()))))) {
      return false;
    }
    else {
      return true;
    }
  }


function showMonths() {
  var nummonths = 16;
  var MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var today = new Date();
  for (var i=0; i< nummonths; i++) {
      var theValue = (today.getMonth() - 0 + 1) + '/1/' + today.getFullYear();
      document.write('<option value="');
      document.write(theValue);
      document.write('"');
      if (i == 1 && today.getDate() > 15) {
         document.write(' selected');
      }
      document.write('>');
      document.write(MONTHS[today.getMonth()]);
      document.write(" ");
      document.write(today.getFullYear());
      document.writeln("</option>");

      if (today.getDate() > 28) {
          today.setDate(28);
      }
      today.setMonth(today.getMonth() + 1);
  }

}
