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

function onKeyPressEdit(e,controlId){
    if (e.keyCode==9){
        return;
    }
    document.getElementById("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 (document.getElementById(controlId).value.length < 3){
    hidePopup();
    return;
  }
 // alert(document.getElementById(controlId).value);
  HttpObjDest.open("GET", "../include/destinations.jsp?searchWord="+document.getElementById(controlId).value , true);
  //alert("After destinations.jsp call");
  HttpObjDest.onreadystatechange= function (){
    if(HttpObjDest.readyState == 4) {
		try {
          var ret =  HttpObjDest.responseText;
          //alert(ret);
         // alert("Size::"+ret.length);
         // alert("Size in R::"+ret.replace(/^\s+|\s+$/g,"").length);
          //set starting value of HttpObjDest::response to 208 that means no results for POI-POR change Author::Cognizant Date:16/11/2011

          if (ret !=null && ret.replace(/^\s+|\s+$/g,"").length > 208){
              newDiv = document.createElement('div');
              newDiv.innerHTML = ret;
              document.getElementById("popupcontent").innerHTML = "";
              document.getElementById("popupcontent").appendChild(newDiv);
              var rowCount = document.getElementById("destMaxCount").value;
              document.getElementById("destMaxCount").parentNode.removeChild(document.getElementById("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;

        //alert(__destH);
        // 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;

        var iframe = document.getElementById("popupcontent_iFrame");
        iframe.style.display = 'block';

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

    }
    
    var __destAnchor;
    var __destW;
    var __destH;
    
    function adjustPopupCoordinates() {
    	
    	//alert('adjustPopupCoordinates');
		var popUp = document.getElementById("popupcontent");
		
		var anchorPosX= popUp.offsetLeft;
		var anchorPosY= __destAnchor.style.top + 5;
		var topCoord = anchorPosY + "px";
        
		var offset = 0;        
        if ( window.leftoffset !== undefined ) {
            offset = - popUp.offsetWidth - leftoffset;
        }
		var leftCoord = (anchorPosX + __destAnchor.clientWidth+ 7 + offset) + "px";

		//popUp.style.top = topCoord;
		//popUp.style.left = leftoffset;
		popUp.style.top = objectTopPosition(__destAnchor)+20;
		popUp.style.left = objectLeftPosition(__destAnchor);
		popUp.style.width = __destAnchor.offsetWidth;
		popUp.style.height = __destH + "px";
		divElement = popUp;
		var iframe = document.getElementById("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";
		document.getElementById("popupcontent_iFrame").style.display = "none";
	}

    


function getHttpObject(){
	  var xmlHttp;
	  try{
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
	  }
	  catch (e){
		  //Internet Explorer
		  try{
			  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		  catch (e){
			  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
	  }
	  return xmlHttp;
}	

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

function populateSForm(dest, state, country, type, areaCode){
    //remove for airport (type=='7') area code from destination
    if (type=='7')
        dest = dest.replace(/ \(\w+\)$/,"");
    document.getElementById("home_resv_city").value = dest;
    preselect(document.getElementById("home_resv_country"), country);
    preselect(document.getElementById("home_resv_state"), state);
    document.getElementById("areaCodeAR").value = areaCode;
    document.getElementById("areaCode").value = areaCode;
    hidePopup();
}

//FOR POI-POR CHNAGE Author ::Cognizant Date 11/03/2011
function populateSFormSolrPOI(dest, state, country, type, latitude,longitude){
    //remove for airport (type=='7') area code from destination

	document.getElementById("home_resv_city").value = dest;
    $("state").value = state;
    $("country").value = country;
    $("latitude").value = latitude;
    $("longitude").value = longitude;
    $("entityType").value = type;
    hidePopup();
}
//FOR POI-POR CHNAGE Author ::Cognizant Date 11/03/2011  

function preselect(selectObject, value) {
	for (i = 0; i < selectObject.options.length; i++) {
		if (selectObject.options[i].value.toUpperCase() == value.toUpperCase()) {
			selectObject.selectedIndex = i;
			return value;
		}
	}
	return "";
}

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

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

function objectLeftPosition(obj) {
    var curleft = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
            } while (obj = obj.offsetParent);
      }
      return curleft;
}
function objectTopPosition(obj) {
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return curtop;
}
function $(elementID) {
	if (document.getElementById) {
		return document.getElementById(elementID);
	} else if (document.all) {
		return document.all[elementID];
	} else if (document.layers) {
		document.layers[elementID];
	} else {
		window[elementID];
	}
}

/*Added to hide the popup*/
//document.onclick=check;
function check(e){
	var target = (e && e.target) || (event && event.srcElement);
	var obj = document.getElementById('popupcontent');
	var obj2 = document.getElementById('home_resv_city');
	iframe = document.getElementById("popupcontent_iFrame");
	if(checkParent(target)){
		obj.style.display='none';
		iframe.style.display='none';
	}
	if(target==obj2){
		obj.style.display='block';
	}
}
function checkParent(t){
	while(t.parentNode){
		if(t==document.getElementById('popupcontent')){
			return false
		}
		t=t.parentNode
	}
	return true
}


