// java script functions for city util
function getXmlHttpObject() {
    var objXMLHttp=null;
    try {
        objXMLHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
              // Something wrong
            }
        }
    }
  return objXMLHttp;
}
/*
/	Search cities by parameters
/	dir = ltr or rtl
*/

function doBeyondCitySearch(strSearch, listID, e , cityIndex , dir ){
	 var params = "TargetID="+listID;

	 params = params +  addToQuery ("departureConnectionCity_1" , listID == "originBeyond" ? "originBeyondText" : "originBeyond" , true); 	 
 	 params = params +  addToQuery ("destinationConnectionCity_1" , listID == "destinationBeyond" ? "destinationBeyondText" : "destinationBeyond" , true);
 	 params = params +  addToQuery ("departureConnectionCity_2" , listID == "retDeparturesBeyond" ? "retDeparturesBeyondText" : "retDeparturesBeyond" , true);
 	 params = params +  addToQuery ("destinationConnectionCity_2" , listID == "retDestinationsBeyond" ? "retDestinationsBeyondText" : "retDestinationsBeyond" , true);
 	 //params = params +  addToQuery ("departureConnectionCity_3" , "viaFromBeyond" , true);
 	 //params = params +  addToQuery ("destinationConnectionCity_3" , "viaToBeyond" , true);
 	
 	 
 	 params = params +  "&connectionCityIndex=" + cityIndex + "&referenceId=0";;
     doCitySearch(strSearch, listID, e , params , dir);
}

function doCitySearch(strSearch, listID, e , params) {
    // document.getElementById(listID).value = toUppercase( document.getElementById(listID));
    var keycode = e.keyCode;
    switch (keycode){
        case LEFT:
        case UP:
        case RIGHT:
        case DOWN:
        case ENTER:
        case ESC:
            return;
        default:
    }
    if (strSearch.length < 0) {

        hideCitySearchDiv(listID);
        return;
    }
   
    var oHttp = getXmlHttpObject();
    if(oHttp == null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    oHttp.onreadystatechange = function() {
        if(oHttp.readyState == 4 || oHttp.readyState == "complete") {
            var responseHtml = oHttp.responseText;
             if (responseHtml.indexOf("NO RESULTS")<0) {
                displayCitySearchResults(responseHtml, listID , dir);
            } else {
                hideCitySearchDiv(listID);
            }
        }
    }
    inputType = "1";
    beginWith = strSearch;
	var url = "getNewConnectionCity.do";
    oHttp.open( "POST", url, true );
    oHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    oHttp.send(params);
}



function displayCitySearchResults(responseHtml, listID) {
    var hiddenCitiesDiv = document.getElementById(listID + "_DIV");
    if (hiddenCitiesDiv != undefined) {
        hiddenCitiesDiv.innerHTML = responseHtml;
        hiddenCitiesDiv.style.display = "inline";
        var shift = 0;
        // add the input size to the div
        if(dir == "rtl"){
        	shift = 60; // Deafult size
        }
        hiddenCitiesDiv.style.left =  (findPosX(document.getElementById(listID))) - shift;
        hiddenCitiesDiv.style.top =  (findPosY(document.getElementById(listID))) + 33;
        HideCoveredDDLs(listID + "_DIV");
    }
}

function hideCitySearchDiv(listID) {
	    
   
	lastCityRowSelected=null;
	currentCityRowSelected=null;
	enteredToDiv = false;        

	var theDiv = document.getElementById(listID + "_DIV");
	theDiv.style.display="none";
	onBlorCityBox(listID);
	ShowHiddenDDLs();
    

}

function clickOnResult(text, code, listID ) {
   
    document.getElementById(listID).value=  code;
	document.getElementById(listID+"Text").value=  text;
    hideCitySearchDiv(listID);
    
}

function selectCityRow(curSel, listID ) {
   
    if(!document.getElementById(listID+"_"+curSel)) {

        return false;
    }
    var theText = document.getElementById(listID+"_"+curSel).getAttribute("cityDescription");
    var theCode = document.getElementById(listID+"_"+curSel).getAttribute("cityCode");
    clickOnResult(theText,theCode, listID );
    return true;
}

function highlightCityRow(curSel, listID) {
    var theLI = document.getElementById(listID+"_"+curSel);
    theLI.className = "citySearchContent citySearchContentHover";
    lastCityRowSelected = currentCityRowSelected;
    lowlightCityRow(lastCityRowSelected, listID);
    currentCityRowSelected = curSel;
}

function lowlightCityRow(curSel, listID) {
    var theLI = document.getElementById(listID+"_"+curSel);
    if (theLI != undefined) {
    	 
    	   if("1" == theLI.getAttribute("cityType")){
	    	 	  theLI.className = "citySearchContentElal citySearchContentOut";
	    	}else{
	    		 theLI.className = "citySearchContent citySearchContentOut";
	    	}
    }
}

function markCityRow(curSel, listID, isDown) {
    var selectedLI = document.getElementById(listID+"_"+curSel);
    if (selectedLI != undefined) {
        selectedLI.className = "citySearchContent citySearchContentHover";
    } else {
        if (isDown && document.getElementById(listID+"_1") != undefined) {
            lowlightCityRow(lastCityRowSelected,listID);
            lastCityRowSelected = currentCityRowSelected;
            currentCityRowSelected = 1;
            document.getElementById(listID+"_1").scrollIntoView(true);
            markCityRow(1, listID, isDown);
            return;
        }
    }
    var lastLI = document.getElementById(listID+"_"+lastCityRowSelected);
    if (lastLI != undefined) {
        if("1" == lastLI.getAttribute("cityType")){
    	 	  lastLI.className = "citySearchContentElal citySearchContentOut";
    	}else{
    		  lastLI.className = "citySearchContent citySearchContentOut";
    	}
    }
    var theDiv = document.getElementById(listID + "_DIV");
    if (curSel>11 || (!isDown && curSel<=11)) {
        selectedLI.scrollIntoView(isDown);
    }
}

var ESC=27;var TAB=9;var ALT=18;var UP=38;var DOWN=40;var LEFT=37;var RIGHT=39;var ENTER=13;var SHIFT=16;var PAGEUP=33;var PAGEDOWN=34;
var currentCityRowSelected=null;
var lastCityRowSelected=null;

var enteredToDiv = false;

function doOnDown(e, listID) {
	  //document.getElementById(listID).value = toUppercase( document.getElementById(listID));
    var keycode = e.keyCode;
    switch (keycode){
    case LEFT:
    case UP:
        if (currentCityRowSelected!=null) {
            if (currentCityRowSelected>1) {
                lastCityRowSelected = currentCityRowSelected;
                currentCityRowSelected--;
                markCityRow(currentCityRowSelected, listID, false);
            }
        }
        break;
    case RIGHT:
    case DOWN:
        if (currentCityRowSelected==null) {
            currentCityRowSelected=1;
        }  else {
            lastCityRowSelected = currentCityRowSelected;
            currentCityRowSelected++;
        }
        markCityRow(currentCityRowSelected, listID, true);
        break;
    case ENTER:

//        if (_itemCount>0) {
			//alert(selectCityRow(currentCityRowSelected, listID , false)) 
            if(!selectCityRow(currentCityRowSelected, listID)){
                //searchData();
            }
            //alert(enteredToDiv)
//        }
        break;
    case ESC:
           
        hideCitySearchDiv(listID);
        break;
    case TAB:
    case ALT:
    case SHIFT:
        break;
    default:
    }

}
/*
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  */
  // Function for hiding dropdown over div Start Here   
  
   function HideCoveredDDLs(sDiv)   
  
   {   
      var oDiv = document.getElementById(sDiv);   
      var colS = document.all.tags("SELECT");   
      if (colS!=null)   
      {   
         for (i=0; i<colS.length; i++)    
         {   
            if (OBJIsUnderDiv(colS[i], oDiv)) {  colS[i].style.visibility="hidden"; }   
         }   
      }   
   }   
  
   function OBJIsUnderDiv(oOBJ, oDiv)   
   {   
      var oX1 = findPosX(oOBJ); //oLeft1   
      //alert(oX1);   
      var oX2 = oX1 + oOBJ.offsetWidth; //oLeft2   
      //alert(oX2);   
      var oY1 = findPosY(oOBJ); //oTop1   
      //alert(oY1);   
      var oY2 = oY1 + oOBJ.offsetHeight; //oTop2   
      //alert(oY2);   
      var dX1 = findPosX(oDiv); //dLeft1   
      //alert(dX1);   
      var dX2 = dX1 + oDiv.offsetWidth; //dLeft2   
      //alert(dX2);   
      var dY1 = findPosY(oDiv); //dTop1   
      //alert(dY1);   
      var dY2 = dY1 + oDiv.offsetHeight; //dTop2   
      //alert(dY2);   
           
      if (oX1>=dX1 && oX1<=dX2 && oY1>=dY1 && oY1<=dY2)   
         return true;   
      if (oX2>=dX1 && oX2<=dX2 && oY2>=dY1 && oY2<=dY2)                    
         return true;   
      return false;   
   }   
  
   function findPosX(obj)   
   {   
       var curleft = 0;   
       if (obj.offsetParent)   
       {   
           while (obj.offsetParent)   
           {   
               curleft += obj.offsetLeft   
               obj = obj.offsetParent;   
           }   
       }   
       else if (obj.x)   
           curleft += obj.x;   
       return curleft;   
   }   
  
   function findPosY(obj)   
   {   
       var curtop = 0;   
       if (obj.offsetParent)   
       {   
           while (obj.offsetParent)   
           {   
               curtop += obj.offsetTop   
               obj = obj.offsetParent;   
           }   
       }   
       else if (obj.y)   
           curtop += obj.y;   
       return curtop;   
   }   
  
   function ShowHiddenDDLs()   
   {   
      var colS = document.all.tags("SELECT");   
      if (colS!=null)   
      {   
         for (i=0; i<colS.length; i++)    
            colS[i].style.visibility="inherit";    
      }   
   }   
  
// Function for hiding dropdown over div Ends Here  
  


	function onBlorCityBox (listID) {
		var textBoxElement = listID + "Text";
		if (document.getElementById(textBoxElement) != null && document.getElementById(textBoxElement).value == "") {
			document.getElementById(listID).value = "";
		}
		
	}
  



