var initialized = true;		// used to show last visit destination

//--------------------------------------------------------------------------------------------------
	function GetXmlHttpObject() 
	{
		// handler different object in IE & Firefox
		var objXMLHttp
		objXMLHttp=null
		if (window.XMLHttpRequest) {
			objXMLHttp=new XMLHttpRequest()
		} else if (window.ActiveXObject) {
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		return objXMLHttp
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function showAvailChart(frbox, request_no, LanguageCode) 
	{
		if(LanguageCode==''){LanguageCode='en_US';}
		//----------------------------------------------------------------------------------------------
		// show function name
		//----------------------------------------------------------------------------------------------
		if (typeof(frbox) == "object")
		{
			var select_value = frbox.value;
		}
		else
		{
			var select_value = frbox;
		}
		
		var func_code = select_value.substr(0, 1);
		var show_by_text = "";
		
		switch (func_code)
		{
			case "b":
				show_by_text = current_bestselling;
				break;
			
			case "n":
				show_by_text = str_all;
				break;
			
			case "p":
				show_by_text = str_all;
				break;
			
			case "a":
				var div3 = document.getElementById("destination3");
				var area_name = div3.options[div3.selectedIndex].text;
				show_by_text = area_name;
				break;
		}
		
		document.getElementById("show_by").innerHTML = show_by_text;
		//----------------------------------------------------------------------------------------------

		var chart_file_name = select_value;

		if (typeof(frbox) == "object")
		{
			chart_file_name += "_0";
		}
		
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) {
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "/ajax/3.0.02/availability.php";
		url += "?fc=ShowAvailableChart";
		url += "&src=" + chart_file_name;
		url += "&sid=" + Math.random();
		url += "&LanguageCode=" + LanguageCode;
		
		document.getElementById("availablechart").innerHTML = "";
		document.getElementById("loading").style.display = "";		
		
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = RefreshAvailableChart;
		xmlHttp.send(null);
		
		// GTA
		_DestinationID = select_value.substr(1);
		
		// Omniture tracking code
		if (typeof(s) != "undefined")
		{
			UpdateOmnitureValue(select_value);
		}
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function UpdateOmnitureValue(select_value)
	{
		xmlHttp_omniture = GetXmlHttpObject();
		if (xmlHttp_omniture == null) {
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "/ajax/3.0.02/omniture.php";
		url += "?page_id="+select_value;
		url += "&sid=" + Math.random();
		
		xmlHttp_omniture.open("GET", url, true);
		xmlHttp_omniture.onreadystatechange = SubmitOmnitureValue;
		xmlHttp_omniture.send(null);
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function SubmitOmnitureValue()
	{
		if (xmlHttp_omniture.readyState==4 || xmlHttp_omniture.readyState=="complete")
		{
			if (xmlHttp_omniture.responseText != '')
			{
				eval(xmlHttp_omniture.responseText);
				if (document.getElementById("tl_sent").value == 1)
				{
					void(s.t());
				}
				else
				{
					s.linkTrackVars = "prop2,prop3,prop4,events";
					s.linkTrackEvents="event1"					
					void(s.tl(s,"o","Change Destination"));
					document.getElementById("tl_sent").value = 1;
				}
			}
		}		
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function RefreshAvailableChart()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			
			if (xmlHttp.responseText != '')
			{
				/*
				// GTA
				var sFullDestination = getDestinationPath();
				pageTracker._trackPageview(sFullDestination);
				*/
				
				document.getElementById("availablechart").innerHTML = xmlHttp.responseText;
				document.getElementById("loading").style.display = "none";
				
				// read sDate parameter from URL (the start date to show in availability chart
				var sStartDate = getParameter("sDate");
				if ( sStartDate != "" )
				{
					setDisplayPeriodFirstTime = 0;
					setDisplayPeriod(sStartDate);
				}
				else
				{
					// show date according to current view
					setDisplayDate();
				}
				
				// show on selected currency
				readCurrency();
			}
		}
	}
//--------------------------------------------------------------------------------------------------	

//--------------------------------------------------------------------------------------------------
	function fillDestination(oSelect, level_id, max_level, LanguageCode, async) 
	{
		if(LanguageCode==''){LanguageCode='en_US';}

		if (level_id == 3)
		{
			showRecommended(oSelect.value, LanguageCode);		
		}
		if (async == null) { async = true; }
		if (oSelect.id == "destination2")
		{
			// show destination name
			var destination_name = oSelect.options[oSelect.selectedIndex].text;
			document.getElementById("destination_name").innerHTML = destination_name.toUpperCase();
			document.getElementById("destination_name2").innerHTML = destination_name;
			
			var div_cc_promotion = document.getElementById("div_cc_promotion");
			if (div_cc_promotion != null)
			{
				// check and show ccPromotion
				// clear existing one
				document.getElementById("div_cc_promotion").style.display = "none";
				div_cc_promotion.innerHTML = "";
				xmlHttp_cc = GetXmlHttpObject();
				if (xmlHttp == null) {
					alert("Browser does not support HTTP Request");
					return;
				}
				
				var url = "/ajax/3.0.02/ajax.php";
				url += "?fc=LoadCCPromotion";
				url += "&level=" + level_id;
				url += "&pid=" + oSelect.value;
				url += "&maxlevel=" + max_level;
				url += "&LanguageCode=" + LanguageCode;
				url += "&DestinationName=" + destination_name;
				url += "&sid=" + Math.random();
				
				xmlHttp_cc.open("GET", url, true);
				xmlHttp_cc.onreadystatechange = refreshCCPromotion;
				xmlHttp_cc.send(null);
			}
		}
		
		// call to report.php to get data
		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) {
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "/ajax/3.0.02/ajax.php";
		url += "?fc=LoadDestination";
		url += "&level=" + level_id;
		url += "&pid=" + oSelect.value;
		url += "&maxlevel=" + max_level;
		url += "&LanguageCode=" + LanguageCode;
		url += "&sid=" + Math.random();
		
		current_level = level_id;
		last_level = max_level;
		
		// clear selection for next level
		for (var i=current_level; i<=last_level; i++)
		{
			var oDiv = document.getElementById("location"+i);
			var tmpContent = "<select class=\"mybox\" size=\"6\">";
			tmpContent += "<option value=\"-1\">"+ loading_txt +"</option\n";
			tmpContent += "</select>\n";
			oDiv.innerHTML = tmpContent;
			oDiv.disable = true;

		}
		
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = refreshDestination;
		xmlHttp.send(null);
	}
//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function refreshCCPromotion()
	{
		if (xmlHttp_cc.readyState==4 || xmlHttp_cc.readyState=="complete")
		{
			if (xmlHttp_cc.responseText!='' && xmlHttp_cc.responseText!='-1')
			{
				document.getElementById("div_cc_promotion").innerHTML = xmlHttp_cc.responseText;
				document.getElementById("div_cc_promotion").style.display = "";
			}
		}
	}
//--------------------------------------------------------------------------------------------------	

//--------------------------------------------------------------------------------------------------
	function refreshDestination()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if (xmlHttp.responseText != '')
			{
				var oDiv = document.getElementById("location"+current_level);
				
				if (xmlHttp.responseText == "error:1040")
				{
					// Too many connections error, error comes from /scripts/3.0.02/alerts.js
					oDiv.innerHTML = db_timeout+"<br><br>&nbsp;";
					
					if (current_level == 2)
					{
						document.getElementById("location3").innerHTML = db_timeout+"<br><br>&nbsp;";
					}
					document.getElementById("loading").style.visibility = "hidden";
					return;
				}
				
				
				oDiv.innerHTML = xmlHttp.responseText
				
				
				
				if (!initialized)
				{
					if (current_level == 2)
					{
						var d2 = getParameter("d2");
						if (d2 != "")
						{
							var oSelect = document.getElementById("destination2");
							setSelectedIndex(oSelect, d2);
							fillDestination(oSelect, 3, 3, languageCode, false)
						}
						else
						{
							
							// check if cookie was set
							var destination_id2 = readCookie("lt_last_visit2");
							
							if (destination_id2 == null)
							{
								// set to current value of destination2
								var oSelect = document.getElementById("destination2");
								destination_id2 = oSelect.value;
							}
							// set selectedIndex accordingly
							initialized = true;
							var oSelect2 = document.getElementById("destination2");
							setSelectedIndexByValue(oSelect2, destination_id2);
							fillDestination(oSelect2, 3, 3, languageCode);
						}
					}
					else if (current_level == 3)
					{
						var oSelect = document.getElementById("destination3");
						var d3 = getParameter("d3");
						if (d3 != "")
						{
							setSelectedIndex(oSelect, d3);
						}
						initialized = true;
						showAvailChart(oSelect, 0, languageCode);
					}
				}
				else
				{
					if (current_level < last_level)
					{
						var next_level = current_level+1;
						var oSelect = document.getElementById("destination"+current_level);
						fillDestination(oSelect, next_level, last_level, languageCode);
					}
					else
					{
						// force to show availability chart
						oSelect = document.getElementById("destination3");
						showAvailChart(oSelect, 0, languageCode);
					}
				}
			}
		}
	}
//--------------------------------------------------------------------------------------------------	

//--------------------------------------------------------------------------------------------------
	function readCurrency()
	{
		var currency = readCookie("lt_currency");
		var currencyDropDowns = document.getElementsByName("currencySelect");
		if (currencyDropDowns.length > 0 && currency != null) {
		    currencyDropDowns[0].selectedIndex = currency;
		    UpdateCurrency(currencyDropDowns[0]);
		}
	}
//--------------------------------------------------------------------------------------------------	

//--------------------------------------------------------------------------------------------------
	function SetVisitCookie()
	{
		// level 1
		var  oSelect = document.getElementById("destination1");	
		if (oSelect != null)
		{
			var destination = oSelect.options[oSelect.selectedIndex].value;
			createCookie("lt_last_visit1", destination, 365);
		}
		
		// level 2
		var  oSelect = document.getElementById("destination2");	
		if (oSelect != null)
		{
			var destination = oSelect.options[oSelect.selectedIndex].value;
			createCookie("lt_last_visit2", destination, 365);
		}
	}
//--------------------------------------------------------------------------------------------------	

//--------------------------------------------------------------------------------------------------
	function SetLastVisitDestination()
	{
		//-------------------------------------------------------------//
		// Used by index.htm to show last visit destination as default //
		//-------------------------------------------------------------//
		
		// check if cookie was set
		var destination_id = readCookie("lt_last_visit1");
		if (destination_id != null)
		{
			// set selectedIndex accordingly
			var oSelect = document.getElementById("destination1");
			setSelectedIndexByValue(oSelect, destination_id);
			
			// fill destination2
			fillDestination(oSelect, 2, 3, '', false);
			return true;
		}
		initialized = true;
		return false;
	}
//--------------------------------------------------------------------------------------------------	
		
//--------------------------------------------------------------------------------------------------
	function showRecommended(destination, LanguageCode)
	{
		if(LanguageCode==''){LanguageCode='en_US';}
		xmlHttpRecommend = GetXmlHttpObject();
		if (xmlHttpRecommend == null) {
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "/ajax//3.0.02/recommended.php";
		url += "?destination=" + destination;
		url += "&LanguageCode=" + LanguageCode;
		url += "&sid=" + Math.random();
		
		document.getElementById("recommended").innerHTML = "";
		
		xmlHttpRecommend.open("GET", url, true);
		xmlHttpRecommend.onreadystatechange = RefreshRecommended;
		xmlHttpRecommend.send(null);
	}
//-------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
	function RefreshRecommended()
	{
		if (xmlHttpRecommend.readyState==4 || xmlHttpRecommend.readyState=="complete")
		{
			
			if (xmlHttpRecommend.responseText != '')
			{
				document.getElementById("recommended").innerHTML = xmlHttpRecommend.responseText;
			}
		}
	}
//--------------------------------------------------------------------------------------------------	


//--------------------------------------------------------------------------------------------------
	function getDestinationPath() 
	{
		var destination_path = "";
		xmlHttpPath = GetXmlHttpObject();
		if (xmlHttpPath == null) {
			alert("Browser does not support HTTP Request");
			return;
		}
		
		var url = "/ajax/3.0.02/ajax.php";
		url += "?fc=GetDestinationPath";
		url += "&dest_id=" + _DestinationID;
		url += "&sid=" + Math.random();
		
		xmlHttpPath.open("GET", url, false);
		xmlHttpPath.onreadystatechange = function ()
			{
				if (xmlHttpPath.readyState==4 || xmlHttpPath.readyState=="complete")
				{
					destination_path = xmlHttpPath.responseText;
				}
			};
		xmlHttpPath.send(null);
		
		return destination_path;
	}
//--------------------------------------------------------------------------------------------------
