<!--
var req;
var dataFetched = false;
var lastCityFetched = '';
var form = document.selectHotelForm;
var MAX_ITEM_TO_SHOW = 0;
var MAX_ITEM_TO_SHOW_IN_SCROLL = 0;

form.city.autocomplete="off";

function getData(){
var url = "/bestwestern/citySmartSearch.do?&city=";

if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
}
url = url + form.city.value;

   req.open("POST", url, true);
   req.onreadystatechange = callback;
   req.send('');
}
function callback() {
    if (req.readyState == 4) {
    	dataFetched = true;
        if (req.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            parseMessage(); 
            
        }
    }
}
var message;
var hotelSearchControl_hotelSmartLogicTextBox_Cities = new CityList_City("","", "", "");
function parseMessage() {
	  
      message = req.responseText;
      //alert(message);
      MAX_ITEM_TO_SHOW =  message.substring(0,message.indexOf(','));
      MAX_ITEM_TO_SHOW_IN_SCROLL = message.substring(message.indexOf(',')+1,message.indexOf('/'));
      //alert(MAX_ITEM_TO_SHOW);
      //alert(MAX_ITEM_TO_SHOW_IN_SCROLL);
      var firstIndex = message.indexOf('/') + 1;
      //alert(firstIndex);
      var secondIndex = message.length ;
      //alert(secondIndex);
      message = message.substring(firstIndex,secondIndex);
      //alert(message);
      var cityInResponse = message.substring(0,3).toUpperCase();
      var currCityInput = form.city.value.substring(0,3).toUpperCase();
      if(cityInResponse == currCityInput)
      {
      hotelSearchControl_hotelSmartLogicTextBox_Cities = message.split("^$");
      showAndMatchInitially();
      }
}



// global variables
var SmartLogic_OptionsDIV;
var SmartLogic_Processing = false;
var KEY_SPECIAL = 0;
var KEY_TAB = 9;
var KEY_ENTER = 13;
var KEY_SHIFT = 16;
var KEY_PG_UP = 33;
var KEY_PG_DOWN = 34;
var KEY_HOME = 36;
var KEY_LEFT_ARROW = 37;
var KEY_UP_ARROW = 38;
var KEY_RIGHT_ARROW = 39;
var KEY_DOWN_ARROW = 40;
var KEY_BACK_SPACE = 8;
var KEY_DELETE = 46;
var showDiv = false;
var matches = new Array();
var elem ;
var setText = false;
var input = '';
var output = '';
var btype = '';


var i_countryObj ;
var i_stateObj;

// finds and displays the matching city options
function SmartLogic_FindCities(f_elem, e, stateObj,countryObj)
{
	i_stateObj = stateObj;
	i_countryObj = countryObj;
	elem = f_elem;
	// this makes sure that the onblur and onfocus dont step on each other
	while(SmartLogic_Processing)
	{
		// do nothing, just wait 
	}

	// handle incomplete input
	 input = elem.value;
	 output = input;
	if(input.length < 3)
	{
		matches = new Array();
		SmartLogic_HideOptions();
		return;
	}
	else if(e.keyCode == KEY_HOME  || e.keyCode == KEY_LEFT_ARROW || e.keyCode == KEY_RIGHT_ARROW || key == KEY_SHIFT || key == KEY_TAB || key == KEY_PG_DOWN || key == KEY_PG_UP)
	{
		return;
	}
	else if( input.length == 3 && lastCityFetched != input.toUpperCase() && ( (e.keyCode >=48 &&  e.keyCode <=90) ||(e.keyCode >=96 &&  e.keyCode <=111) || (e.keyCode >=186 &&  e.keyCode <=192) || (e.keyCode >=219 &&  e.keyCode <=222) || e.keyCode == KEY_BACK_SPACE || e.keyCode == KEY_DELETE ))
	{
		dataFetched = false;
		matches = new Array();
		hotelSearchControl_hotelSmartLogicTextBox_Cities = new CityList_City("","", "", "");
		SmartLogic_HideOptions();
		lastCityFetched = input.toUpperCase();
		getData();
	}

	
	// ok
	matches = new Array();
	matches.selectedItem = 0;
	setText = false;

	// first, handle any special key presses (shift, tab, alt, etc)
	e = (e) ? e : event;
	var key = e.keyCode;

	

	// handle arrow keys	
	if(key == KEY_UP_ARROW || key == KEY_DOWN_ARROW)
	{
		setText = true;
		if(key == KEY_UP_ARROW)
		{
			if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem > 0)
			{
				SmartLogic_OptionsDIV.matches.selectedItem--;
			}
		}
		
		if(key == KEY_DOWN_ARROW)
		{
			if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem < SmartLogic_OptionsDIV.matches.length - 1)
			{
				SmartLogic_OptionsDIV.matches.selectedItem++;
			}
		}

		if(SmartLogic_OptionsDIV)
			matches = SmartLogic_OptionsDIV.matches;
	}
	else if(key == KEY_ENTER )
	{
		if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem >= 0 && SmartLogic_OptionsDIV.matches.selectedItem <= SmartLogic_OptionsDIV.matches.length - 1)
		{
			
			SmartLogic_SetCity(elem);
			return;
		}
	}
	else
	{
		matchTheData();
	}

	SmartLogic_HideOptions();

	showData();
}

function matchTheData(){
// check the input for matches
                 
		for(i=0;i< hotelSearchControl_hotelSmartLogicTextBox_Cities.length;i++)
		{
			var k = 0;
			var cityItr = hotelSearchControl_hotelSmartLogicTextBox_Cities[i];
			//alert(cityItr);
	
			var tempArr = cityItr.split("^%");
			
			var singleCity = new CityList_City(tempArr[k++], tempArr[k++], tempArr[k++],tempArr[k++]);
			
			var validCountry = false;
			//if(addressZIPSearch == true)
			//{
				for ( ctryList = 0; ctryList < i_countryObj.options.length; ctryList++)
				{

					if(i_countryObj.options[ctryList].value == singleCity.countryCode)
					{
						validCountry = true;
						break;
					}
				}
			//}
			//else
			//{validCountry = true; }
			// Handle case where singleCity.cityName is matched
			if(validCountry == true)
			{
					if(SmartLogic_StartsWith(singleCity.cityName, input))
					{
						// match
						
						matches[matches.length] = singleCity;
					}
			}
		}
}

function showAndMatchInitially(){
SmartLogic_HideOptions();
matchTheData();
showData();
}

function showData(){
var isScroll = false;
var innerDIV;
	if(matches.length > 0)
	{
		ajaxDIVOpen = true;
		//clearInterval(intv);
		dataFetched = true;
		SmartLogic_OptionsDIV = document.createElement("div");
		SmartLogic_OptionsDIV.style.position = "absolute";
		SmartLogic_OptionsDIV.style.display = "block";
		SmartLogic_OptionsDIV.style.top = (SmartLogic_FindPosY(elem) + elem.offsetHeight) + "px";
		SmartLogic_OptionsDIV.style.left = (SmartLogic_FindPosX(elem) ) + "px";
		SmartLogic_OptionsDIV.className = "citySmartDropdown";
		SmartLogic_OptionsDIV.style.zIndex = "1000";
		SmartLogic_OptionsDIV.matches = matches;
		SmartLogic_OptionsDIV.elem = elem;
	
		if(window.opera){btype = "Opera";}
		else if(window.netscape){btype = "Netscape";}
		if(navigator.appName == "Microsoft Internet Explorer"){btype = "Internet Explorer";}	
		
		if(SmartLogic_OptionsDIV.matches.length-1 >= MAX_ITEM_TO_SHOW ) {	
			isScroll = true;
						
			if(btype == "Opera") {	
			    SmartLogic_OptionsDIV.style.height = "300px";			
				SmartLogic_OptionsDIV.className = "citySmartDropdownScrollOpera";	
			} else if(btype == "Netscape") {
			    SmartLogic_OptionsDIV.style.height = "285px";		
				SmartLogic_OptionsDIV.className = "citySmartDropdownScrollNetscape";	
			} else if(btype == "Internet Explorer"){
			    SmartLogic_OptionsDIV.style.height = "285px";				
				SmartLogic_OptionsDIV.className = "citySmartDropdownScroll";	
			}else{
			    SmartLogic_OptionsDIV.style.height = "270px";				
				SmartLogic_OptionsDIV.className = "citySmartDropdownScroll";
			}
		}
		
		var numberDivsAdded = 0;
		
		/* calculate the length of cityDisplayText and convert it into pixels */
		var cityDisplayTextCount=0;		
		for(j=0;j<SmartLogic_OptionsDIV.matches.length-1 && j<MAX_ITEM_TO_SHOW_IN_SCROLL-1 ;j++)
		{
			var singleCity = matches[j];				
			var tmpCount = singleCity.cityStateCountryName.length + 8;
			if(cityDisplayTextCount <= tmpCount)
				cityDisplayTextCount =	tmpCount;
		}
		cityDisplayTextCount = cityDisplayTextCount * 5;
		// end of length calculation
		
			for(j=0;j<SmartLogic_OptionsDIV.matches.length-1 && j<MAX_ITEM_TO_SHOW_IN_SCROLL-1;j++)
			{
				var singleCity = matches[j];
				numberDivsAdded++;
				var cityDisplayText = document.createTextNode( " " + singleCity.cityStateCountryName + " " );
				var cityDisplaySpan = document.createElement("span");
				var cityDisplayDIV = document.createElement("div");
				var className = "citySmartDropdownOption";
				
				if(SmartLogic_OptionsDIV.matches.selectedItem == j)
				{
					className = "citySmartDropdownOptionSelected";
					var valueDisplayText = singleCity.cityName;
					if(setText) output = valueDisplayText;
				}
				
				cityDisplaySpan.className = className + "Text";
				cityDisplaySpan.appendChild(cityDisplayText);
				
				cityDisplayDIV.id = j;
				cityDisplayDIV.index = j;
				cityDisplayDIV.onmouseover = displaySelect;
				cityDisplayDIV.onclick = selectOption;
				cityDisplayDIV.className = className;				
				if(isScroll)
					cityDisplayDIV.style.width = cityDisplayTextCount + "px";			
				cityDisplayDIV.appendChild(cityDisplaySpan);
				SmartLogic_OptionsDIV.appendChild(cityDisplayDIV);
			}

		// Last item requires special DIV class

		var singleCity = matches[numberDivsAdded];			
		var cityDisplayText = document.createTextNode( " " + singleCity.cityStateCountryName + " " );
		var cityDisplaySpan = document.createElement("span");
		var cityDisplayDIV = document.createElement("div");
		var className = "citySmartDropdownOptionLast";
		if(SmartLogic_OptionsDIV.matches.selectedItem == numberDivsAdded)
		{
			className = "citySmartDropdownOptionLastSelected";
			var valueDisplayText = singleCity.cityName;
			if(setText) output = valueDisplayText;
		}
		
		cityDisplaySpan.className = className + "Text";
		cityDisplaySpan.appendChild(cityDisplayText);
		
		cityDisplayDIV.id = numberDivsAdded;
		cityDisplayDIV.index = numberDivsAdded;
		cityDisplayDIV.onmouseover = displaySelectLast;
		cityDisplayDIV.onclick = selectOption;
		cityDisplayDIV.className = className;
		cityDisplayDIV.appendChild(cityDisplaySpan);
		SmartLogic_OptionsDIV.appendChild(cityDisplayDIV);		
		
		
		document.body.appendChild(SmartLogic_OptionsDIV);

		var maxWidth = 0;
		for(j=0;j<SmartLogic_OptionsDIV.children.length;j++)
		{
			if (SmartLogic_OptionsDIV.children[j].clientWidth > maxWidth)
			{
				maxWidth = SmartLogic_OptionsDIV.children[j].clientWidth;
			}
		}
		
		SmartLogic_OptionsDIV.style.width = maxWidth + 6;

		for(j=0;j<SmartLogic_OptionsDIV.children.length;j++)
		{
			SmartLogic_OptionsDIV.children[j].style.width = "100%";
		}
		
		// handle the fact that IE cant show a dynamic DIV over windowed controls
		if(document.body.insertAdjacentHTML)
		{
		    var iframeSrcAttribute = 'src="about:blank"';

			var iframe = "<IFRAME " + iframeSrcAttribute + " id='_internal_iframe_id' style='POSITION:absolute;LEFT:" + SmartLogic_OptionsDIV.style.left + ";TOP:" + SmartLogic_OptionsDIV.style.top + ";WIDTH:" + SmartLogic_OptionsDIV.offsetWidth + "px;HEIGHT:" + SmartLogic_OptionsDIV.offsetHeight + "px;Z-INDEX:999;' src='about:blank' frameBorder='0' scrolling='no'></IFRAME>";
			SmartLogic_OptionsDIV.insertAdjacentHTML("afterEnd", iframe);
			
			SmartLogic_OptionsDIV.iframeID = "_internal_iframe_id";
			
		}

		elem.value = output;

		

	}

}

function displaySelect()
{

	var prevSelect = this.parentNode.childNodes[this.parentNode.matches.selectedItem];
	//special case for the last option in the list
	if (prevSelect.index == this.parentNode.childNodes.length-1)
	{
		prevSelect.className = "citySmartDropdownOptionLast";
	}
	else
		  prevSelect.className = "citySmartDropdownOption";

	this.className = "citySmartDropdownOptionSelected";
	this.parentNode.matches.selectedItem = this.index;
}

function displaySelectLast()
{
	var prevSelect = this.parentNode.childNodes[this.parentNode.matches.selectedItem];

		  prevSelect.className = "citySmartDropdownOption";
	this.className = "citySmartDropdownOptionLastSelected";
	this.parentNode.matches.selectedItem = this.index;
}

function selectOption()
{
SmartLogic_SetCity(this.parentNode.elem);
}



function SmartLogic_SetCity(elem)
{
	showDiv = false;
	SmartLogic_Processing = true;	
	if(SmartLogic_OptionsDIV != null && SmartLogic_OptionsDIV.matches != null && SmartLogic_OptionsDIV.matches.selectedItem >= 0 && SmartLogic_OptionsDIV.matches.selectedItem <= SmartLogic_OptionsDIV.matches.length - 1)
	{
		var singleCity = SmartLogic_OptionsDIV.matches[SmartLogic_OptionsDIV.matches.selectedItem];		


		elem.value = singleCity.cityName;
		i_countryObj.value = singleCity.countryCode;
		changeOfCountry(i_countryObj);
		if(singleCity.stateCode == "null" || singleCity.stateCode == null)
		i_stateObj.value = "";
		else if (singleCity.countryCode == 'US' || singleCity.countryCode == 'CA' || singleCity.countryCode == 'AU')
		i_stateObj.value = singleCity.countryCode + '_' + singleCity.stateCode;
		
	}
	ajaxDIVOpen = false;
	SmartLogic_HideOptions();
	SmartLogic_Processing = false;
}


function SmartLogic_OnClick(elem)
{
	var val = elem.value;
}

function SmartLogic_HideOptions()
{
	if(SmartLogic_OptionsDIV != null)
	{
		if(SmartLogic_OptionsDIV.iframeID != null)
		{
			var iframe = document.getElementById(SmartLogic_OptionsDIV.iframeID);
			document.body.removeChild(iframe);
			//shouldSubmit = true;
		}

		document.body.removeChild(SmartLogic_OptionsDIV);
		SmartLogic_OptionsDIV = null;
	}
}

function SmartLogic_StartsWith(string, phrase)
{
	var phraseLength = phrase.length;

	var result = string.substring(0, phraseLength);

	if(result.toUpperCase() == phrase.toUpperCase())
		return true;
	else
		return false;
}

function SmartLogic_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 SmartLogic_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 CityList_City(cityName, cityStateCountryName, stateCode, countryCode)
{
	this.cityName = cityName;	
	this.cityStateCountryName = cityStateCountryName;
	this.stateCode = stateCode;
	this.countryCode = countryCode;
}

/************************************************************************
* validiteCity
*	Call from search and select hotel page on bodyload. This function validate that
*	the city element is not null and then call to Ajax.js SmartLogic_SetCity function.
************************************************************************/
function validateCity(city , stateCode, countryCode){	
	if(city != null){		
		SmartLogic_SetCity(city , stateCode, countryCode);
	}
}

function checkTabIsPressed(e, t){				
	if(e.keyCode == 9){		
		SmartLogic_SetCity(t,document.selectHotelForm.stateCode,document.selectHotelForm.countryCode);
	}
}

//-->