var delay = 250;
var timerID = null;
var closeTimerID = null;
var timerRunning = false;

function onKeyPressEdit(e,controlId){
    if (e.keyCode==9){
        return;
    }
    $("areaCodeAR").value = "";
//hidePopup();
    StopTheClock();
    StartTheTimer(controlId);
}
function StopTheClock(){
    if(timerRunning)
        clearTimeout(timerID);
    timerRunning = false;
}
function StartTheTimer(controlId){
        timerRunning = true;
        timerID = self.setTimeout("run('"+controlId+"')", delay);
}

var HttpObjDest = getHttpObject();

function run(controlId){
  if ($(controlId).value.length < 3){
    hidePopup();
    return;
  }

  HttpObjDest.open("GET", "../include/destinations.jsp?searchWord="+$(controlId).value , true);
  HttpObjDest.onreadystatechange= function (){
    if(HttpObjDest.readyState == 4) {
		try {
          var ret =  HttpObjDest.responseText;
          if (ret !=null && ret.replace(/^\s+|\s+$/g,"").length > 0){
              newDiv = document.createElement('div');
              newDiv.innerHTML = ret;
              $("popupcontent").innerHTML = "";
              $("popupcontent").appendChild(newDiv);
              var rowCount = $("destMaxCount").value;
              $("destMaxCount").parentNode.removeChild($("destMaxCount"));
              divHeight = 16 * rowCount + 15;
              if (divHeight > autoRecommendHeight) divHeight = autoRecommendHeight;
              if (divHeight < autoRecommendMinHeight) divHeight = autoRecommendMinHeight;
              showPopup(document.getElementById(controlId),autoRecommendWidth,divHeight);
          } else {
              hidePopup();
          }
		} catch (ex) {
		}
    }
  }
  HttpObjDest.send(null);
}



    function showPopup(anchor,w,h) {
        var popUp = document.getElementById("popupcontent");

        __destAnchor = anchor;
        __destW = w;
        __destH = h;

        // If window.leftoffset is not null, it means the auto recommend popup
        // should be displayed to the left of the destination textbox.
        // It means we have to calculate the popup's top-left corner coordinate
        // dynamically depending on its width.

        adjustPopupCoordinates();

        popUp.style.visibility = "visible";
        popUp.scrollTop = 0;
        popUp.scrollLeft = 0;

        iframe = $("popupcontent_iFrame");
        iframe.style.display = 'block';

        setTimeout("adjustPopupCoordinates()", 100);
        setTimeout("adjustPopupCoordinates()", 200);
        setTimeout("adjustPopupCoordinates()", 500);
        setTimeout("adjustPopupCoordinates()", 1000);
    }

    var __destAnchor;
    var __destW;
    var __destH;

    function adjustPopupCoordinates() {
		var popUp = document.getElementById("popupcontent");

		var anchorPos = getElementPosition(__destAnchor);

		var topCoord = anchorPos.y + "px";
        var offset = 0;
        if ( window.leftoffset !== undefined ) {
            offset = - popUp.offsetWidth - leftoffset;
        }
		var leftCoord = (anchorPos.x + __destAnchor.clientWidth + 7 + offset) + "px";

		popUp.style.top = topCoord;
		popUp.style.left = leftCoord;

		popUp.style.height = __destH + "px";

        divElement = popUp;
        iframe = $("popupcontent_iFrame");
        iframe.style.width = divElement.offsetWidth;
        iframe.style.height = divElement.offsetHeight;
        iframe.style.left = divElement.offsetLeft;
        iframe.style.top = divElement.offsetTop;
	}



    function hidePopup() {
		var popUp = document.getElementById("popupcontent");
		popUp.style.visibility = "hidden";
        $("popupcontent_iFrame").style.display = "none";
	}

    function populateSForm(dest, state, country, type, areaCode){
        //remove for airport (type=='7') area code from destination
        if (type=='7')
            dest = dest.replace(/ \(\w+\)$/,"");
        $("destination").value = dest;
        preselect($("country"), country);
        preselect($("state"), state);
        $("areaType3").checked = true;
        if (type=='1')
            $("areaType1").checked = true;
        if (type=='7')
            $("areaType2").checked = true;
        $("areaCodeAR").value = areaCode;
        $("areaCode").value = areaCode;
        hidePopup();
    }

function onFocusLost(controlId){
    if ($("popupcontent") == document.activeElement || $("destination") == document.activeElement){
        return;
    }
    StopTheClock();
    closeTimerID = setTimeout('hidePopup()',500);
}

function onDivClick(e){
    if (closeTimerID != null) {
        clearTimeout(closeTimerID);
        closeTimerID = null;
    }
    var b = true;
    /*@cc_on
    b = false;
    @*/
    if (b) $("destination").focus();
}

function onDivMOver() {
    var b = true;
    /*@cc_on
    b = false;
    @*/
    if (b){
    if (closeTimerID != null) {
    clearTimeout(closeTimerID);
    closeTimerID = null;
    }
    }
}
