//
//
//


//
function kwidOnSubmit() {
    kwidFlightOnSearch();
}

//
function kwidFlightOnSearch() {
  var fn = 'kFlightForm';
  var form = getObj(fn);
  if (!form) {
    alert("Invalid page structure " + fn);
    return false;
  }

  if (!form.ai || !form.ai.value) {
    alert("Missing required affiliate id");
    return false;
  }

  if (!form.l1 || !form.l2) {
    alert("Invalid page structure: missing l1 or l2");
    return false;       
  }

  var kayakUrl = 'http://www.kayak.com/s/search/air?' + kwUnpackForm(form);

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

  return kwidSubmitGet(kayakUrl);
}

//
// generic method to grab all the form fields.
//
function kwUnpackForm(form) {
  var s = "";
  for (var i = 0; i < form.elements.length; i++) {
    var nm = form.elements[i].name;
    var vl = form.elements[i].value;
    if (nm && vl) {
      s += "&" + nm + "=" + escape(vl);
    }
  }
  cb = Math.random()*400;
  s += "&cb=" + cb;
  return wgFixDate(s);
}

//
// tidy.
//
function wgFixDate(argList) {
  if (argList.match(/\//g)) {
    argList = argList.replace(/\//g,"%2F");
  }
  return argList;
}


//
function kwidKayakHome() {
  var kayakUrl = 'http://www.kayak.com/s/search/air?x=y';
  return kwidSubmitGet(kayakUrl);
}

//
// Route all URL redir through here.
//
function kwidSubmitGet(url) {


  if (isLinkShare()){
    url = lsn_click + '&tmpid=2304&RD_PARM1=' + escape(escape(url));
  }

  window.open(url);
  return true;
}


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