﻿var currentRow = -1;
var txtRegion = 'MasterPage_Main_GenericHotelSearchControl_HotelSearchControl_txtRegion';
var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

function ajaxFunction()
{
    if (document.getElementById(txtRegion).value.length > 2)
    {
        var prefix = document.getElementById(txtRegion).value;
        var regEx = new RegExp("[^\-\(\)a-z*A-Z*0-9*,* *]");//supports all alphabets capital and small, all numbers,comma,dash and space
        var matches = regEx.exec(prefix);
        
        if (!matches)
        {
            var xmlHttp;
            try
            {  
                // Firefox, Opera 8.0+, Safari  
                xmlHttp=new XMLHttpRequest();  
            }
            catch (e)
            {  // Internet Explorer  
                try
                {    
                    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
                }
                catch (e)
                {    
                    try
                     {      
                            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
                     }
                     catch (e)
                     {      
                            alert("Your browser does not support AJAX!");      
                            return false;      
                     }    
                }  
            } 
            xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                {
                    document.getElementById("Level1").innerHTML=xmlHttp.responseText;
                    document.getElementById("Level1").style.display='';
                    setPositionOfDiv()  
                    currentRow = -1;
                }
            }
            var url=strURL + "Webfunction.aspx?prefixText={Keyword}";
            url=url.replace("{Keyword}",document.getElementById(txtRegion).value.toString()); 

            xmlHttp.open("GET",url,true);
            xmlHttp.send(null);
            //document.getElementById(txtRegion).innerHTML=xmlHttp.responseText;
       }
       else
       {
            clearAutocomplete();
       }
    }
    else
    {
        clearAutocomplete();
    }
}

function clearAutocomplete()
{
     document.getElementById("Level1").style.fontSize = 0;
     document.getElementById("Level1").innerHTML = '';
     document.getElementById("Level1").style.display='none';
}
function setPositionOfDiv()
{
    var ParentContainerClass = document.getElementById("ParentContainerClass");
    var RegionEle = document.getElementById(txtRegion);
    ParentContainerClass.style.position = 'absolute';
    ParentContainerClass.style.top = eval(curTop(RegionEle) + RegionEle.offsetHeight) + "px";          
    ParentContainerClass.style.left = curLeft(RegionEle) + "px";
    ParentContainerClass.style.width = RegionEle.offsetWidth + "px";

}
function curTop(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return toreturn;
}
function curLeft(obj){
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return toreturn;
}
function DownArrowKeyPressed(dataContainer)
{
    MakeAllInactive(dataContainer,currentRow);
    if (currentRow+1 < dataContainer.rows.length)
    {
        dataContainer.rows[currentRow+1].cells[0].className="ActiveRow";
        dataContainer.rows[currentRow + 1].cells[0].focus();
        document.getElementById(txtRegion).focus();
        currentRow = currentRow+1;
    }
    else
    {
        dataContainer.rows[0].cells[0].className="ActiveRow";
        dataContainer.rows[0].cells[0].focus();
        document.getElementById(txtRegion).focus();
        currentRow = 0;
    }
}


function MakeAllInactive1(dataContainer)
{
    for (i=0;i<dataContainer.rows.length;i++)
    {
        dataContainer.rows[i].cells[0].className="InactiveRow";
    }

}

function MakeAllInactive(dataContainer,MyCounter)
{
    if (MyCounter >= 0 &&MyCounter < dataContainer.rows.length)
    {
        dataContainer.rows[MyCounter].cells[0].className="InactiveRow";
    }
}

function UpArrowKeyPressed(dataContainer)
{
    MakeAllInactive(dataContainer,currentRow);
    if (currentRow-1 >=0)
    {
        dataContainer.rows[currentRow-1].cells[0].className="ActiveRow";
        dataContainer.rows[currentRow-1].cells[0].focus();
        document.getElementById(txtRegion).focus();
        currentRow = currentRow-1;
    }
    else
    {
        dataContainer.rows[dataContainer.rows.length-1].cells[0].className="ActiveRow";
        dataContainer.rows[dataContainer.rows.length - 1].cells[0].focus();
        document.getElementById(txtRegion).focus();
        currentRow = dataContainer.rows.length-1;
    }
}

function ShowSelectedItem(dataContainer)
{
    if (currentRow >=0 && currentRow < dataContainer.rows.length)
    {
        if (is_ie)
        {
            document.getElementById(txtRegion).value = dataContainer.rows[currentRow].cells[0].innerText;
        }
        else
        {
            document.getElementById(txtRegion).value = dataContainer.rows[currentRow].cells[0].textContent;
        }
    }
}

function ActivateMe(dataRow,dataContainer)
{
    var MyCount = dataRow.id.replace("Row_","");
    MakeAllInactive(dataContainer,currentRow);
    try
    {
        currentRow = parseInt(MyCount);
    }
    catch (ex)
    {}
    dataRow.className="ActiveRow";
}

function SelectMe(dataRow,dataContainer)
{
    ActivateMe(dataRow,dataContainer);
    if (is_ie)
    {
        document.getElementById(txtRegion).value = dataRow.innerText;
        document.getElementById("Level1").innerHTML = '';
        document.getElementById("Level1").style.display='none';
    }
    else
    {
        document.getElementById(txtRegion).value = dataRow.textContent;
        document.getElementById("Level1").innerHTML = '';
        document.getElementById("Level1").style.display='none';
    }
}

function Navigate(dataContainer,keyEvent)
{
    if (txtRegion == '')
    {
        intializeHotelControlIDs();
    }
    var keynum;
    if(is_ie) // IE
    {
      keynum = keyEvent.keyCode;
    }
    else // Netscape/Firefox/Opera
    {
      keynum = keyEvent.which;
    }

    if(dataContainer)
    {

        if(keynum) 
        {
            switch(keynum) 
            {
                case 9: 					//TAB EVENT
                    clearAutocomplete();
                	break;
                case 13: // enter
                   	//alert(dataContainer.rows[currentRow].cells[0].innerText);
                    switch(keynum) 
                    {
                        case 38: // keyup
                         UpArrowKeyPressed(dataContainer);
                         break;
                        case 40: //keydown
                         DownArrowKeyPressed(dataContainer);
                         break;                        
                    }
                    ShowSelectedItem(dataContainer);
                    clearAutocomplete();	
                    return false;
                    break;
            //			case 27:		//ESCAPE			
            //				break;
                case 38: // keyup
                    UpArrowKeyPressed(dataContainer);
                    ShowSelectedItem(dataContainer);
                    break;
                case 40: //keydown
                    DownArrowKeyPressed(dataContainer);
                    ShowSelectedItem(dataContainer);
                    break;
                default:
                    //ajaxFunction();
                    break;
            }
        }
        else
        {
            //ajaxFunction();
        }
    }
    else
    {
        //ajaxFunction();
    }
}

function BlankNavigate(keyEvent)
{
    if (txtRegion == '')
    {
        intializeHotelControlIDs();
    }
    var keynum;
    if(is_ie) // IE
    {
      keynum = keyEvent.keyCode;
    }
    else // Netscape/Firefox/Opera
    {
      keynum = keyEvent.which;
    }

    if(keynum) 
    {
        switch(keynum) 
        {
            case 9: // tab
                break;
            case 13: // enter
                return false;
                break;
            case 38: // keyup
                break;
            case 40: //keydown
                break;
            default:
                ajaxFunction();
                break;
        }
    }
    else
    {
        ajaxFunction();
    }
}

