/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+

*/
var liveSearchReq = false;
var tPickup = null;
var tReturn = null;
var liveSearchLastPickup = "";
var liveSearchLastReturn = "";
var preventSubmit = false;

function checkIE7() {
  var agent = navigator.userAgent.toLowerCase();
  var ie = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
  var major = parseInt(navigator.appVersion);
  return (ie && (major == 4) && (agent.indexOf("msie 7.") != -1));
}

var isIE7 = checkIE7();
var isIE = false;
// on !IE we only have to initialize it once
if(window.XMLHttpRequest) {
  liveSearchReq = new XMLHttpRequest();
}

function liveSearchInit() {
  if(navigator.userAgent.indexOf("Safari") > 0) {
    document.getElementById('pickup').addEventListener("keydown",liveSearchKeyPressPickup,false);
    document.getElementById('return').addEventListener("keydown",liveSearchKeyPressReturn,false);
  } else if(navigator.product == "Gecko") {
    document.getElementById('pickup').addEventListener("keypress",liveSearchKeyPressPickup,false);
    document.getElementById('pickup').addEventListener("blur",liveSearchHideDelayedPickup,false);
    document.getElementById('return').addEventListener("keypress",liveSearchKeyPressReturn,false);
    document.getElementById('return').addEventListener("blur",liveSearchHideDelayedReturn,false);
  } else {
    document.getElementById('pickup').attachEvent('onkeydown',liveSearchKeyPressPickup);
    document.getElementById('return').attachEvent('onkeydown',liveSearchKeyPressReturn);
    document.getElementById('pickup').attachEvent("onblur",liveSearchHideDelayedPickup);
    document.getElementById('return').attachEvent("onblur",liveSearchHideDelayedReturn);
    isIE = true;
  }
}

// Pickup //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pickup //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pickup //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function liveSearchHideDelayedPickup() {
  preventSubmit = false;
  window.setTimeout("liveSearchHidePickup()",400);
}

function liveSearchHidePickup() {
  var res = document.getElementById("shadowboxPickup");
  var highlight = document.getElementById("LSHighlightPickup");
  if(highlight) {
    highlight.removeAttribute("id");
  }
  if(isIE) {
    var toHide = document.getElementsByTagName("select");
    for(var i = toHide.length; i; toHide[--i].style.visibility = "visible");
  }
  res.style.display = "none";
  preventSubmit = false;
}

function liveSearchMouseOverPickup(elm) {
  highlight = document.getElementById("LSHighlightPickup");
  if(highlight) {
    highlight.removeAttribute('id');
  }
  elm.parentNode.parentNode.setAttribute('id','LSHighlightPickup');
}

function liveSearchMouseOutPickup(elm) {
  elm.parentNode.parentNode.removeAttribute('id');
}

function liveSearchKeyPressPickup(event) {
  preventSubmit = false;
  //reset the hidden country value - in case of name change
  document.getElementById('pickup_cc').value = "";
  document.getElementById('pickup_ac').value = "";

  var code;
  if (event.keyCode) code = event.keyCode;
  else if (event.which) code = event.which;

  if(code == 40 )
  //KEY DOWN
  {
    highlight = document.getElementById("LSHighlightPickup");
    if(!highlight) {
      if(document.getElementById("LSShadowPickup").firstChild != undefined) {
        highlight = document.getElementById("LSShadowPickup").firstChild.firstChild.firstChild;
      } else {
        highlight = false;
      }
    } else {
      highlight.removeAttribute("id");
      highlight = highlight.nextSibling;
    }
    if(highlight) {
      highlight.setAttribute("id","LSHighlightPickup");
    }
    if(!isIE) { event.preventDefault(); }
  }
  //KEY UP
  else if(code == 38) {
    highlight = document.getElementById("LSHighlightPickup");
    if(!highlight) {
      if(document.getElementById("LSShadowPickup").firstChild != undefined) {
        highlight = document.getElementById("LSShadowPickup").firstChild.firstChild.lastChild;
      } else {
        highlight = false;
      }
    } else {
      highlight.removeAttribute("id");
      highlight = highlight.previousSibling;
    }
    if(highlight) {
      highlight.setAttribute("id","LSHighlightPickup");
    }
    if(!isIE) { event.preventDefault(); }
  }
  //ESC
  else if(code == 27) {
    highlight = document.getElementById("LSHighlightPickup");
    if(highlight) {
      highlight.removeAttribute("id");
    }
    document.getElementById("shadowboxPickup").style.display = "none";
  }
  // ENTER
  else if(code == 13) {
    highlight = document.getElementById("LSHighlightPickup");
    if(highlight) {
      highlight.firstChild.firstChild.click();
      highlight.removeAttribute("id");
      document.getElementById("shadowboxPickup").style.display = "none";
      liveSearchLastPickup = document.getElementById('pickup').value;
      preventSubmit = true;
      return false;
    }
  }
  // BACKSPACE AND DELETE
  else if(code == 8 || code == 46) {
    highlight = document.getElementById("LSHighlightPickup");
    if(highlight) {
      highlight.removeAttribute("id");
    }
    liveSearchStartPickup();
  }
}

function liveSearchStartPickup() {
  if(tPickup) {
    window.clearTimeout(tPickup);
  }
  tPickup = window.setTimeout("liveSearchDoSearchPickup()",200);
}

function liveSearchDoSearchPickup() {
  if(typeof liveSearchRoot == "undefined") {
    liveSearchRoot = "";
  }
  if(typeof liveSearchRootSubDir == "undefined") {
    liveSearchRootSubDir = "";
  }
  if(typeof liveSearchParams == "undefined") {
    liveSearchParams = "";
  }
  if(liveSearchLastPickup != document.getElementById('pickup').value) {
    if(liveSearchReq && liveSearchReq.readyState < 4) {
      liveSearchReq.abort();
    }
    if(Trim(document.getElementById('pickup').value).length <= 1) {
      liveSearchHidePickup();
      return false;
    }
    if(window.XMLHttpRequest) {
      if(isIE7) {
        liveSearchReq = new XMLHttpRequest();
      }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
      liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    liveSearchReq.onreadystatechange= liveSearchProcessReqChangePickup;
    liveSearchReq.open("GET", liveSearchRoot + "/include/db_functions/livesearch_car.php?f=pickup&q=" + encodeURIComponent(document.getElementById('pickup').value) + liveSearchParams);
    liveSearchLastPickup = document.getElementById('pickup').value;
    liveSearchReq.send(null);
  }
}

function liveSearchProcessReqChangePickup() {
  if(liveSearchReq.readyState == 4) {
    if(liveSearchReq.responseText != "") {
      var res = document.getElementById("shadowboxPickup");
      res.style.display = "inline";
      var sh = document.getElementById("LSShadowPickup");
      sh.innerHTML = liveSearchReq.responseText;
      if(isIE) {
        var toHide = hideCheck(res, document.getElementsByTagName("select"));
        for(var i = toHide.length; i; toHide[--i].style.visibility = "hidden");
      }
    } else {
      var res = document.getElementById("shadowboxPickup");
      if(isIE) {
        var toHide = document.getElementsByTagName("select");
        for(var i = toHide.length; i; toHide[--i].style.visibility = "visible");
      }
      res.style.display = "none";
    }
  }
}

// Return //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Return //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Return //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function liveSearchHideDelayedReturn() {
  preventSubmit = false;
  window.setTimeout("liveSearchHideReturn()",400);
}

function liveSearchHideReturn() {
  var res = document.getElementById("shadowboxReturn");
  var highlight = document.getElementById("LSHighlightReturn");
  if(highlight) {
    highlight.removeAttribute("id");
  }
  if(isIE) {
    var toHide = document.getElementsByTagName("select");
    for(var i = toHide.length; i; toHide[--i].style.visibility = "visible");
  }
  res.style.display = "none";
  preventSubmit = false;
}

function liveSearchMouseOverReturn(elm) {
  highlight = document.getElementById("LSHighlightReturn");
  if(highlight) {
    highlight.removeAttribute('id');
  }
  elm.parentNode.parentNode.setAttribute('id','LSHighlightReturn');
}

function liveSearchMouseOutReturn(elm) {
  elm.parentNode.parentNode.removeAttribute('id');
}

function liveSearchKeyPressReturn(event) {
  preventSubmit = false;
  //reset the hidden country value - in case of name change
  document.getElementById('return_cc').value = "";
  document.getElementById('return_ac').value = "";

  var code;
  if (event.keyCode) code = event.keyCode;
  else if (event.which) code = event.which;

  if(code == 40 )
  //KEY DOWN
  {
    highlight = document.getElementById("LSHighlightReturn");
    if(!highlight) {
      if(document.getElementById("LSShadowReturn").firstChild != undefined) {
        highlight = document.getElementById("LSShadowReturn").firstChild.firstChild.firstChild;
      } else {
        highlight = false;
      }
    } else {
      highlight.removeAttribute("id");
      highlight = highlight.nextSibling;
    }
    if(highlight) {
      highlight.setAttribute("id","LSHighlightReturn");
    }
    if(!isIE) { event.preventDefault(); }
  }
  //KEY UP
  else if(code == 38) {
    highlight = document.getElementById("LSHighlightReturn");
    if(!highlight) {
      if(document.getElementById("LSShadowReturn").firstChild != undefined) {
        highlight = document.getElementById("LSShadowReturn").firstChild.firstChild.lastChild;
      } else {
        highlight = false;
      }
    } else {
      highlight.removeAttribute("id");
      highlight = highlight.previousSibling;
    }
    if(highlight) {
      highlight.setAttribute("id","LSHighlightReturn");
    }
    if(!isIE) { event.preventDefault(); }
  }
  //ESC
  else if(code == 27) {
    highlight = document.getElementById("LSHighlightReturn");
    if(highlight) {
      highlight.removeAttribute("id");
    }
    document.getElementById("shadowboxReturn").style.display = "none";
  }
  // ENTER
  else if(code == 13) {
    highlight = document.getElementById("LSHighlightReturn");
    if(highlight) {
      highlight.firstChild.firstChild.click();
      highlight.removeAttribute("id");
      document.getElementById("shadowboxReturn").style.display = "none";
      liveSearchLastReturn = document.getElementById('return').value;
      preventSubmit = true;
      return false;
    }
  }
  // BACKSPACE AND DELETE
  else if(code == 8 || code == 46) {
    highlight = document.getElementById("LSHighlightReturn");
    if(highlight) {
      highlight.removeAttribute("id");
    }
    liveSearchStartReturn();
  }
}

function liveSearchStartReturn() {
  if(tReturn) {
    window.clearTimeout(tReturn);
  }
  tReturn = window.setTimeout("liveSearchDoSearchReturn()",200);
}

function liveSearchDoSearchReturn() {
  if(typeof liveSearchRoot == "undefined") {
    liveSearchRoot = "";
  }
  if(typeof liveSearchRootSubDir == "undefined") {
    liveSearchRootSubDir = "";
  }
  if(typeof liveSearchParams == "undefined") {
    liveSearchParams = "";
  }
  if(liveSearchLastReturn != document.getElementById('return').value) {
    if(liveSearchReq && liveSearchReq.readyState < 4) {
      liveSearchReq.abort();
    }
    if(Trim(document.getElementById('return').value).length <= 1) {
      liveSearchHideReturn();
      return false;
    }
    if(window.XMLHttpRequest) {
      if(isIE7) {
        liveSearchReq = new XMLHttpRequest();
      }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
      liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    liveSearchReq.onreadystatechange= liveSearchProcessReqChangeReturn;
    liveSearchReq.open("GET", liveSearchRoot + "/include/db_functions/livesearch_car.php?f=return&q=" + encodeURIComponent(document.getElementById('return').value) + liveSearchParams);
    liveSearchLastReturn = document.getElementById('return').value;
    liveSearchReq.send(null);
  }
}

function liveSearchProcessReqChangeReturn() {
  if(liveSearchReq.readyState == 4) {
    if(liveSearchReq.responseText != "") {
      var res = document.getElementById("shadowboxReturn");
      res.style.display = "inline";
      var sh = document.getElementById("LSShadowReturn");
      sh.innerHTML = liveSearchReq.responseText;
      if(isIE) {
        var toHide = hideCheck(res, document.getElementsByTagName("select"));
        for(var i = toHide.length; i; toHide[--i].style.visibility = "hidden");
      }
    } else {
      var res = document.getElementById("shadowboxReturn");
      if(isIE) {
        var toHide = document.getElementsByTagName("select");
        for(var i = toHide.length; i; toHide[--i].style.visibility = "visible");
      }
      res.style.display = "none";
    }
  }
}

// Misc

function hideCheck(o, l){
  function getOffset(o){
    for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight}; o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
    return r.r += r.l, r.b += r.t, r;
  }
  for(var b, s, r = [], a = getOffset(o), j = isNaN(l.length), i = (j ? l = [l] : l).length; i; b = getOffset(l[--i]), (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r)) && (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b)) && (r[r.length] = l[i]));
  return j ? !!r.length : r;
}

function Trim(TRIM_VALUE) {
  if(TRIM_VALUE.length < 1) {
    return "";
  }
  TRIM_VALUE = RTrim(TRIM_VALUE);
  TRIM_VALUE = LTrim(TRIM_VALUE);
  if(TRIM_VALUE == "") {
    return "";
  } else {
    return TRIM_VALUE;
  }
} //End Function

function RTrim(VALUE) {
  var w_space = String.fromCharCode(32);
  var v_length = VALUE.length;
  var strTemp = "";
  if(v_length < 0) {
    return "";
  }
  var iTemp = v_length -1;

  while(iTemp > -1) {
    if(VALUE.charAt(iTemp) == w_space) {
    } else {
      strTemp = VALUE.substring(0,iTemp +1);
      break;
    }
    iTemp = iTemp-1;
  } //End While
  return strTemp;
} //End Function

function LTrim(VALUE) {
  var w_space = String.fromCharCode(32);
  if(v_length < 1) {
    return "";
  }
  var v_length = VALUE.length;
  var strTemp = "";
  var iTemp = 0;
  while(iTemp < v_length){
    if(VALUE.charAt(iTemp) == w_space){
    } else {
      strTemp = VALUE.substring(iTemp,v_length);
      break;
    }
    iTemp = iTemp + 1;
  } //End While
  return strTemp;
} //End Function