﻿
var XmlHttp;
var check =0;
var flag  =0;	
var chkFlag = 0;

var j=0;
var citySuggetions = new Array();
var ifFromorTo = 0;
var statesArray = new Array();
var outp;
var oldins;
var posi = -1;
var words = new Array();
var input;
var key;

var CityId;
var StateId;
var countryId1;
var HotelId;

var CityIdTemp;
var DivIdTemp;
var hdCityIdTemp;
var location;

function get_random()
{
    var ranNum= Math.floor(Math.random()*500000);
    return ranNum;
}


function HandleResponse()
{
	// To make sure receiving response data from server is completed
	
	 if(XmlHttp.readyState == 4)
	 {
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			if (check == 1)
				ClearAndSetCityListItems(XmlHttp.responseText);					
			if (check == 2)
				AutoCompleteCity(XmlHttp.responseText);		
			if (check == 3)
				ClearAndSetStateListItems(XmlHttp.responseText);
			if (check == 4)
				ClearAndSetCityListItems1(XmlHttp.responseText);		
			if (check == 5)
				ClearAndSetCityListBoxItems(XmlHttp.responseText);		
			if (check == 6)
				ClearAndSetHotelListItems(XmlHttp.responseText);
			if (check == 7)
				AutoCompleteCity(XmlHttp.responseText);
			if (check == 8)
				ClearAndSetLocationItems(XmlHttp.responseText);				
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Load City by Country    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

function ChangeCities(Countryddl,Citydll)
{	
	var countryId	=  document.getElementById(Countryddl).value;
	CityId   	    =  Citydll;
		
	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=1&SelectedValue="+countryId+"&random="+ranNum;
	check			= 1;
	flag			= 1;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}
	
}

function ClearAndSetCityListItems(city)
{
	var citysList = city.split("|");
	
	//--------Clear----------------------------
    //var cityList = document.getElementById("ddlCity");
    var cityList = document.getElementById(CityId);
    
	//for (count = cityList.options.length-1; count >0; count--)
	for (count = cityList.options.length-1; count >=0; count--)
	{
		cityList.options[count] = null;
	}
	//-------Set--------------------------------	
	cityList.options[0] = new Option("---Select---","0", false, false);
	var optionItem;
	for (count = 0; count <= citysList.length-1; count++)
	{
		var citySplit = citysList[count].split(";");		
		optionItem = new Option(citySplit[0],citySplit[1], false, false);
		cityList.options[cityList.length] = optionItem;
	}
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Load City by Country Second   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

function ChangeCities1(Countryddl,Citydll,Statedll)
{	
	var countryId	=  document.getElementById(Countryddl).value;
	countryId1      =  Countryddl;
	CityId   	    =  Citydll;
	StateId   	    =  Statedll;
		
	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=4&SelectedValue="+countryId+"&random="+ranNum;
	check			= 4;
	flag			= 4;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}
	
}

function ClearAndSetCityListItems1(city)
{
	var citysList = city.split("|");
	
	//--------Clear----------------------------
    //var cityList = document.getElementById("ddlCity");
    var cityList = document.getElementById(CityId);
    
	//for (count = cityList.options.length-1; count >0; count--)
	for (count = cityList.options.length-1; count >=0; count--)
	{
		cityList.options[count] = null;
	}
	//-------Set--------------------------------
	cityList.options[0] = new Option("---Select---","0", false, false);
	var optionItem;
	for (count = 0; count <= citysList.length-1; count++)
	{
		var citySplit = citysList[count].split(";");		
		optionItem = new Option(citySplit[0],citySplit[1], false, false);
		cityList.options[cityList.length] = optionItem;
	}
	
	LoadStates(countryId1,StateId);
}


//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Load States by Country    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

function LoadStates(Countryddl,Statedll)
{	
	var countryId	=  document.getElementById(Countryddl).value;
	StateId   	    =  Statedll;
		
	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=3&SelectedValue="+countryId+"&random="+ranNum;
	check			= 3;
	flag			= 3;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}
	
}

function ClearAndSetStateListItems(state)
{
	var statesList = state.split("|");
	
	//--------Clear----------------------------    
    var stateList = document.getElementById(StateId);
    
	for (count = stateList.options.length-1; count >0; count--)
	{
		stateList.options[count] = null;
	}
	//-------Set--------------------------------
	stateList.options[0] = new Option("---Select---","0", false, false);
	var optionItem;
	for (count = 0; count <= statesList.length-1; count++)
	{
		var stateSplit = statesList[count].split(";");
		optionItem = new Option(stateSplit[0],stateSplit[1], false, false);
		stateList.options[stateList.length] = optionItem;
	}
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Load Hotel by City   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

function LoadHotels(Cityddl,HotelLb)
{	
	var cityId	=  document.getElementById(Cityddl).value;
	HotelId   	    =  HotelLb;
		
	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=6&SelectedValue="+cityId+"&random="+ranNum;
	check			= 6;
	flag			= 6;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}
	
}

function ClearAndSetHotelListItems(state)
{
	var hotelsList = state.split("|");
	
	//--------Clear----------------------------    
    var hotelList = document.getElementById(HotelId);
    
	for (count = hotelList.options.length-1; count >0; count--)
	{
		hotelList.options[count] = null;
	}
	//-------Set--------------------------------
	//hotelList.options[0] = new Option("---Select---","0", false, false);
	var optionItem;
	for (count = 0; count <= hotelsList.length-1; count++)
	{
		var hotelSplit = hotelsList[count].split(";");
		optionItem = new Option(hotelSplit[0],hotelSplit[1], false, false);
		hotelList.options[hotelList.length] = optionItem;
	}
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Load City ListBox by Country    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

function LoadCityListBox(CountryLb,CitydLb)
{	
    var lbCountry = document.getElementById(CountryLb);
	CityId   	    =  CitydLb;
		 
    var countryId ='0';   
    for (var j = 0; j < lbCountry.length; j++)
    {
        if (lbCountry.options[j].selected)
        { 
            countryId = lbCountry.options[j].value;              
            break;
        }
    }
    
    ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=5&SelectedValue="+countryId+"&random="+ranNum;
	check			= 5;
	flag			= 5;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}
	
}

function ClearAndSetCityListBoxItems(city)
{
	var citysList = city.split("|");
	
	//--------Clear----------------------------   
    var cityList = document.getElementById(CityId);
    	
	for (count = cityList.options.length-1; count >=0; count--)
	{
		cityList.options[count] = null;
	}
	//-------Set--------------------------------	
	//cityList.options[0] = new Option("---Select---","0", false, false);
	var optionItem;
	for (count = 0; count <= citysList.length-1; count++)
	{
		var citySplit = citysList[count].split(";");		
		optionItem = new Option(citySplit[0],citySplit[1]+'~'+citySplit[0]+'~'+citySplit[2], false, false);
		cityList.options[cityList.length] = optionItem;
	}
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Auto Complete City    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

function SendQuery(Like)
{
	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=2&SelectedValue="+Like+"&random="+ranNum;
	check			= 2;
	flag			= 2;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}	
}

function SendQuery_sp(Like)
{
	ranNum=get_random();
	var requestUrl	= "http://www.wonderlandhotels.com/AjaxServerPage.aspx?Check=2&SelectedValue="+Like+"&random="+ranNum;
	//var requestUrl	= "http://Localhost/Wonderland/AjaxServerPage.aspx?Check=2&SelectedValue="+Like+"&random="+ranNum;
	check			= 2;
	flag			= 2;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}	
}

function SendQuery1(Like,c,d,hdc)
{
    CityIdTemp =c;
    DivIdTemp = d;
    hdCityIdTemp = hdc;

	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=7&SelectedValue="+Like+"&random="+ranNum;
	check			= 7;
	flag			= 7;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}	
}

function AutoCompleteCity(cityNames)
{
	statesArray = new Array();
	var resCityStringArray =cityNames.split("|");
	for(i=0;i<(resCityStringArray.length);i++)
	{
		statesArray[i] =resCityStringArray[i];
	}	
	init();
		
}

function init()
{
    if(DivIdTemp != null)
    {
        outp = document.getElementById(DivIdTemp);
    }
    else
    {
	    outp = document.getElementById("output");		
	}
	lookAt();		
	document.onkeydown = keygetter; 
	document.onkeyup = keyHandler;
}

function setVisible(visi)
{
    var x ;	
    if(DivIdTemp != null)
    {
        x = document.getElementById(DivIdTemp);
    }
    else
    {
	    x = document.getElementById("output");	
	}
	x.style.display = visi;
}

function lookAt(){
        
        var ins ;
        if(CityIdTemp != null)
        {
           ins = document.getElementsByName(CityIdTemp)[0].value;
        }
        else
        {
		    ins = document.getElementsByName("txtCity")[0].value;
		}
		 
		if (oldins == ins) return;
		else if (posi > -1);
		else if (ins.length > 0){
			words = getWord(ins);
			if (words.length > 0)
			{
				clearOutput();
				for (var i=0;i < words.length; ++i) 
				addWord (words[i]);
				setVisible("block");
				DepartureVisible("none");
				//input = document.getElementsByName("txtCity")[0].value;
				input = ins;
			}
			else
			{
				setVisible("none");
				posi = -1;
			}
		}
		else{
			setVisible("none");
			posi = -1;
		}
		oldins = ins;
		fromvalue = oldins;	
	}
	
	function addWord(word){
		var sp = document.createElement("div");		
		sp.appendChild(document.createTextNode(word.split(";")[0]));	
		
		sp.title = word.split(";")[0];
		sp.dataFld = word.split(";")[1]; // City Id		
		sp.dataSrc = word.split(";")[2]; // Hotel Currency Code
			
		sp.onmouseover = mouseHandler;
		sp.onmouseout = mouseHandlerOut;
		sp.onclick = mouseClick;           
		
		outp.appendChild(sp);		
	}
	
	function clearOutput(){
		while (outp.hasChildNodes()){
			noten=outp.firstChild;
			outp.removeChild(noten);
		}
		posi = -1;
	}
	
	function getWord(beginning)
	{
		var words = new Array();
		for (var i=0;i < statesArray.length; ++i)
		{
			var j = -1;
			var correct = 1;
			while (correct == 1 && ++j < beginning.length)
			{
				if (statesArray[i].charAt(j) != beginning.charAt(j)) 
				correct = 1;
			}
			if (correct == 1) 
			words[words.length] = statesArray[i];
		}
		return words;
	}
	
	function setColor (_posi, _color, _forg)
	{
		outp.childNodes[_posi].style.background = _color;
		outp.childNodes[_posi].style.color = _forg;
	}
	
	function keygetter(event){
		if (!event && window.event) event = window.event;
		if (event) key = event.keyCode;
		else key = event.which;
	}
		
	function keyHandler(event)
	{
	    var otpt ;	
        if(DivIdTemp != null)
        {
            otpt = document.getElementById(DivIdTemp);
        }
        else
        {
	        otpt = document.getElementById("output");	
	    }
	
		if (otpt.style.display == "block")
		{
		    var textfield;
		    if(CityIdTemp != null)
            {
               textfield = document.getElementsByName(CityIdTemp)[0];
            }
            else
            {		
			    textfield = document.getElementsByName("txtCity")[0];
			}
			
			if (key == 40)
			{ 				
				if (posi >=0) setColor(posi, "#fff", "black");
				else input = textfield.value;
				setColor(++posi, "black", "white");
				textfield.value = outp.childNodes[posi].firstChild.nodeValue;				
			}
			else if (key == 38)
			{ //Key up
				if (words.length > 0 && posi >= 0)
				{
					if (posi >=1)
					{
						setColor(posi, "#fff", "black");
						setColor(--posi, "black", "white");
						textfield.value = outp.childNodes[posi].firstChild.nodeValue;
					}
					else
					{
						setColor(posi, "#fff", "black");
						textfield.value = input;
						textfield.focus();
						posi--;
					}
				}
			}
			else if (key == 27)
			{ // Esc
				textfield.value = input;
				setVisible("none");
				posi = -1;
				oldins = input;
			}
			else if (key == 8)
			{ // Backspace
				posi = -1;
				oldins=-1;
			}
			else if(key ==9)
			{
				textfield.value = input;
				setVisible("none");
				DepartureVisible("block");
				posi = -1;
				oldins = input;
			}
		}
	}
	
	var mouseHandler=function()
	{
		for (var i=0; i < words.length; ++i)
			setColor (i, "white", "black");
	
		this.style.background = "black";
		this.style.color= "white";		
	}
	
	var mouseHandlerOut=function()
	{
		this.style.background = "white";
		this.style.color= "black";
	}
	
	var mouseClick=function()
	{	    
        if(CityIdTemp != null)
        {
           document.getElementsByName(CityIdTemp)[0].value=this.firstChild.nodeValue;
        }
        else
        {
		   document.getElementsByName("txtCity")[0].value=this.firstChild.nodeValue;
		}		
		
		setVisible("none");
		DepartureVisible("block");
		posi = -1;
		oldins = this.firstChild.nodeValue;
		fromvalue = oldins;
		
		if(hdCityIdTemp != null)
		{
		    document.getElementsByName(hdCityIdTemp)[0].value = this.firstChild.parentNode.dataFld;
		}
		else
		{
		    document.getElementsByName("hdCityId")[0].value = this.firstChild.parentNode.dataFld;  
		}
		
		SelectHotelCurrency(this.firstChild.parentNode.dataSrc);
		
	}
	
function DepartureVisible(visi)
{   
    //document.getElementById("DepDateLabel").style.visibility=visi;	
}

function SelectHotelCurrency(HotelCurrency)
{    
    if(HotelCurrency != null && HotelCurrency != "undefined")
    {    
        var ddlCurrency = document.getElementById("ddlCurrency");
	    if(ddlCurrency != null)
	    {
	        for(var count = 0; count < ddlCurrency.options.length; count++)
	        {
	            if(ddlCurrency.options[count].value.split('~')[0] == HotelCurrency)
	            {
	                ddlCurrency.options[count].selected = true;
	                break;
	            }
	        }
	    }
	}
}

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ end Auto Complete City    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
function ChangeLocation(City,Location)
{	
	var CityId	=  document.getElementById(City).value;
	location   	    =  Location;
		
	ranNum=get_random();
	var requestUrl	= "AjaxServerPage.aspx?Check=8&SelectedValue="+CityId+"&random="+ranNum;
	check			= 8;
	flag			= 8;
	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleResponse;
		XmlHttp.open("GET", requestUrl,true);
		XmlHttp.send(null);	
	}	
}
function ClearAndSetLocationItems(locationDetails)
{
	var locationList = locationDetails.split("|");	
	//--------Clear----------------------------    
    var ddlLocation= document.getElementById(location);
    
	
	for (count = ddlLocation.options.length-1; count >=0; count--)
	{
		ddlLocation.options[count] = null;
	}
	//-------Set--------------------------------
    ddlLocation.options[0] = new Option("---Select---","0", false, false);
	var optionItem;
	for (count = 0; count <= locationList.length-1; count++)
	{
		var locationListSplit = locationList[count].split(";");		
		ddlLocation.options[count+1] = new Option(locationListSplit[0],locationListSplit[1]+'~'+locationListSplit[0], false, false);
//		optionItem = new Option(locationListSplit[0],locationListSplit[1]+'~'+locationListSplit[0], false, false);
//		locationListSplit.options[count+1] = optionItem;
	}	
}
