var interval1;
var interval2;
var request;
var req;
var selectedIndex = -1;
var codes;
var names = new Array();;


var controlKeys = new Array ( 13 , 38 , 40);

try
{
   request = new window.XMLHttpRequest();
   if (request.overrideMimeType)
   {
                 request.overrideMimeType('text/xml');
   }
}
catch (e)
{
          request = new ActiveXObject("Msxml2.XMLHTTP");
}
function isControlKey(key)
{
        for (var count=0;count<controlKeys.length ; count++ )
        {
                        if (controlKeys[count]==key)
                        {
                                return true;
                        }
        }
        return false;
}

function getValues(e, length, hotelSubString)
{

                hotelSubString = trim(hotelSubString);
        //      showRetrievedValues=trim(showRetrievedValues);
                if (!e)
                {
                        var e = window.event;
                }

                if (e.keyCode == 27)
                {
                        document.getElementById("hotelNamesDiv").style.display = "none";
                        return;
                }

                if (e.keyCode==13 || e.keyCode==9)
                {
//                      alert("selectedIndex::"+ selectedIndex);
                        if (selectedIndex == -1)
                        {
                                document.getElementById('hotelNameString').value= "";//names[selectedIndex];
                                document.getElementById('hotelNamesDiv').style.display = "none";
                                //document.getElementById('hotelNameString').blur;
                                selectedIndex = -1;
                                e.cancelBubble = true;
                                return;
                        }
                        filter();
                        return;
                }
                if (isControlKey(e.keyCode))
                {
                        return;
                }      
                if(hotelSubString.length >= length-1){
                        showRetrievedValues(e,hotelSubString)
                }else{
                        //alert(hotelSubString.length);
                        document.getElementById("hotelNamesDiv").style.display = "none";
                       
                }
}



function showRetrievedValues(e,hotelSubString)
{
        document.getElementById("hotelNamesDiv").style.display="none";
        codes = new Array();
        names = new Array();
       
        var tableString = "<TABLE WIDTH='100%' align='right'>";
        var givenString = hotelSubString;
        var selectHotelName = 0;
        for (var count=0;count<hotelDetails.length; count++ )
        {
                var nameElement = hotelDetails[count] ['HotelName'];
                //alert("nameElement::" + nameElement +" and givenString::" +givenString);
                if (nameElement.toUpperCase().indexOf(givenString.toUpperCase())!=-1)
                {
                        var name = nameElement;
                        names[names.length] = name;
                        var codeElement = hotelDetails[count] ['HotelCode'];
                        tableString += "<TR>";
                        codes[codes.length] = name;
                        var otherHotelString = document.getElementById('hotelNameString').value;
                        var boldString = "<B>" + otherHotelString + "</B>";
                        var displayHotelName = name.replace(otherHotelString , boldString);
                        tableString += "<TD WIDTH='100%'><A HREF='#' NAME='LinkHotels' onmousedown='putValueHotels(event,\""+selectHotelName+"\")' style='color:#005893'>" +displayHotelName+"</A></TD>";
                        tableString += "</TR>";
                        selectHotelName++;
                }
        }
        tableString += "</TABLE>";     
        if (names.length>0)
        {
                        var XValue;
                        var YValue;
                        var textElement = document.getElementById("hotelNameString");
                        document.getElementById("hotelNamesDiv").innerHTML =  tableString;
                        repositionDiv()
                        document.getElementById("hotelNamesDiv").style.display = "block";
                        if(selectedIndex<0){
                                selectedIndex = 0;
                        }
                        document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF";
        }
        else
        {
                alert("The hotel you are trying to find is not available.");
                selectedIndex = -1;
                document.getElementById("hotelNamesDiv").style.display = "none"
                document.getElementById('hotelNameString').value= "";
                document.getElementById('hotelNameString').focus();            
        }

               
}





     function  keyPressedHotel(e)
         {
                        var ENTER_KEY = 13;
                        var UP_KEY = 38;
                        var DOWN_KEY = 40;
                        var TAB_KEY =  9;
                        if (window.event)
                        {
                                        var keyCode = window.event.keyCode;
                        }
                        else
                        {
                                        var keyCode = e.keyCode
                        }
                //      alert("Key Code:" + keyCode);
                        if (keyCode==ENTER_KEY)
                        {
                //      alert("Key Code:" + keyCode);
                //              alert("Selected Index:" + selectedIndex + " and names:" + names);
                               
                                if (selectedIndex!=-1)
                                {
                                        document.getElementById('hotelNameString').value= names[selectedIndex];
                                        document.getElementById('hotelNamesDiv').style.display = "none";
                                        e.cancelBubble = true;
                                        selectedIndex = 0;
                                        filter();
                                        selectedIndex = -1;
                                        names = new Array();
                                    return;
                                }
                               
                        }


                        if (keyCode==TAB_KEY)
                        {
                                //alert("table");
                                if (selectedIndex!=-1)
                                {
                                        document.getElementById('hotelNameString').value= names[selectedIndex];
                                        document.getElementById('hotelNamesDiv').style.display = "none";
                                        e.cancelBubble = true;
                                        selectedIndex = 0;
                                        filter();
                                        selectedIndex = -1;
                                        names = new Array();
                                    return;
                                }
                        }
                        /*if (keyCode==DOWN_KEY)
                        {
                                movedown_Hotel();
                        }
                        if (keyCode==UP_KEY)
                        {
                                moveup_Hotel();
                        }*/
         }
       

        function moveup_Hotel()
        {
                        var totalMatches = getTotalMatches_Hotels();
                        //alert(totalMatches+"  Index "+selectedIndex);
                        if(totalMatches > 0)
                        {
                                totalMatches = document.getElementsByName("LinkHotels").length;
                                document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF"
                               
                                if (selectedIndex==0)
                                {
                                        selectedIndex = totalMatches - 1;
                                }
                                else
                                {
                                        selectedIndex -= 1;
                                }
                                /*for (var count=0;count<document.getElementsByName("LinkHotels").length ;count++ )
                                {
                                        document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF";
                                }*/
                                document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF";
                                document.getElementsByName("LinkHotels")[selectedIndex].focus();
                                document.getElementById("hotelNameString").focus();
                        }
               
        }



        function movedown_Hotel()
        {
                        //alert("MoveDownStart selectedIndex ::" + selectedIndex)
                        var totalMatches = getTotalMatches_Hotels();
                        if (totalMatches > 0)
                        {
                                document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF"
                                if (selectedIndex==totalMatches-1)
                                {
                                        selectedIndex = 0;

                                }
                                else
                                {
                                        selectedIndex += 1;
                                }
                                /*alert(" selectedIndex ::" + selectedIndex)
                                for (var count=0;count<document.getElementsByName("LinkHotels").length ;count++ )
                                {
                                        document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF";
                                }*/
                                //document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#FFFFFF";
                                document.getElementsByName("LinkHotels")[selectedIndex].parentNode.parentNode.bgColor = "#82CAFF"
                                document.getElementsByName("LinkHotels")[selectedIndex].focus();
                                document.getElementById("hotelNameString").focus();
                        }
                        //alert("MoveDownStart selectedIndex ::" + selectedIndex)
        }
               

        function getTotalMatches_Hotels()
        {
                        var totalMatches = 0;
                        if (document.getElementsByName("LinkHotels")==null || document.getElementsByName("LinkHotels").length==0)
                        {
                                totalMatches = 0;
                        }
                        else
                        {
                                totalMatches = document.getElementsByName("LinkHotels").length;
                        }
                        return totalMatches;
        }

        function setColors_Hotels()
        {
                var totalMatches = getTotalMatches_Hotels();
                //alert(totalMatches);
                for (var count=0;count<totalMatches ;count++ )
                {
                        var row =       document.getElementsByName("LinkHotels")[count].parentNode.parentNode;
                        if (count==selectedIndex)
                        {
                                row.bgcolor = "#82CAFF";
                        }
                        else
                        {
                                row.bgcolor = "#FFFFFF";
                        }
                }
        }



        function putValueHotels(e,count)
        {
                //alert(count);
                if(names[count] != "" && names[count] != null){
                        document.getElementById('hotelNameString').value= names[count];
                }
                document.getElementById('hotelNamesDiv').style.display = "none";
                e.cancelBubble = true;
                filter();
                selectedIndex = -1;
                names = new Array();
        }








function repositionDiv()
{
        //alert("In repositioning");
        var starRatingTitlePaneDivHeight = document.getElementById("starRatingTitlePaneDiv").offsetHeight;     
        var modifySearchDivHeight = document.getElementById("modifySearchDiv").offsetHeight;
        var initialHeightOccupied = 57;
        var topOffset = starRatingTitlePaneDivHeight + modifySearchDivHeight - initialHeightOccupied;
        //alert("topOffset::" + topOffset);
        document.getElementById("hotelNamesDiv").style.position = "absolute";
                        if (navigator.appName.indexOf("Explorer")==-1)
                        {
                                if(window.screen.width == 1152 && window.screen.height == 864){
                                        XValue = 194;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1280 && window.screen.height == 720){
                                        XValue = 259;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1280 && window.screen.height == 1024){
                                        XValue = 259;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1024 && window.screen.height == 768){
                                        XValue = 130;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1280 && window.screen.height == 768){
                                        XValue = 259;
                                        YValue = 349;
                                }
                                //alert(XValue+" IE "+YValue);
                                document.getElementById("hotelNamesDiv").style.left = XValue+2;//elementLeft + 2;
                                document.getElementById("hotelNamesDiv").style.top = YValue + topOffset + 9;                                   
                        }
                        else
                        {
                                if(window.screen.width == 1152 && window.screen.height == 864){
                                        XValue = 194;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1280 && window.screen.height == 720){
                                        XValue = 259;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1280 && window.screen.height == 1024){
                                        XValue = 259;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1024 && window.screen.height == 768){
                                        XValue = 130;
                                        YValue = 349;
                                }

                                if(window.screen.width == 1280 && window.screen.height == 768){
                                        XValue = 259;
                                        YValue = 349;
                                }
                                document.getElementById("hotelNamesDiv").style.left = XValue //elementLeft + 2;
                                document.getElementById("hotelNamesDiv").style.top = YValue + topOffset+ 12;                                   
                        }              
                                if(names.length<3){
                                                document.getElementById("hotelNamesDiv").style.height=names.length*21;
                                                document.getElementById("hotelNamesDiv").style.overflowY = "auto"
                                        }
                                        else{
                                                document.getElementById("hotelNamesDiv").style.height=63;
                                        }
}


/*
        Here, the 2 time outs set in the search results page have to be cleared
*/