/*
 	 creator: 	chip Kreis
	---------------------------------------------------------------------------------------------------------
	 date:		3/19/2006
	---------------------------------------------------------------------------------------------------------
	 purpose:	functions which call restHotelProfiles.asp via http,
				returning destination types, limited hotel list, complete hotel list,
				and hotel detail
	---------------------------------------------------------------------------------------------------------
	 change log:
	---------------------------------------------------------------------------------------------------------
*/

	// ------------------------------------------------------------------------------------------------------
	// set global variables
	var g_getHotelInfo_url = "HotelProfiles_REST.asp";
	var g_hotelListType;

	function createXmlHttpRequest()
	{
		var p_xmlHttp;
		if ( window.ActiveXObject ) 
		{
			p_xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
		}	
		else if ( window.XMLHttpRequest ) 
		{
			p_xmlHttp = new XMLHttpRequest();
		}
		return p_xmlHttp;
	}

	// ------------------------------------------------------------------------------------------------------
	function doXmlHttpPost( vals, hp_requestType, postURL )
	{
		var thisPostURL = postURL;
		
		if ( thisPostURL == "" )
		{
			thisPostURL = g_getHotelInfo_url;
		}
		
		var thisXmlHttpObject = new createXmlHttpRequest();
		
		thisXmlHttpObject.open( "POST", thisPostURL, true );
		
		thisXmlHttpObject.onreadystatechange = function()
		{
			handleXmlHttpPostStateChange( thisXmlHttpObject, hp_requestType );
		}
		
		thisXmlHttpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded;" );
		thisXmlHttpObject.send( vals );
	}


	// ------------------------------------------------------------------------------------------------------
	function handleXmlHttpPostStateChange( theXmlHttpObject, hp_requestType )
	{
		if ( theXmlHttpObject.readyState == 4 ) 
		{
			if ( theXmlHttpObject.status == 200 ) 
			{
				if ( theXmlHttpObject.responseText != "" )
				{
					//alert(theXmlHttpObject.responseText);
					
					if ( hp_requestType == "allDestAndTypes" )
					{
						var destNodes = theXmlHttpObject.responseXML.getElementsByTagName( 'Destination' );
						// call function to make global array of destType objects; this will be used to 
						// create the destinations dropdown as well as be queried for dest types when
						// dest is selected from dropdown.
						makeDestAndTypesObjArray( destNodes );
					}
					else if ( hp_requestType == "HotelBreadcrumb" )
					{							
						// populate breadcrumb div with appropriate breadcrumb
						document.getElementById("hotelBreadcrumb").innerHTML = theXmlHttpObject.responseText;						
					}
					else
					{
						// populate div with returned html
						document.getElementById("divHotelResults").innerHTML = theXmlHttpObject.responseText;
					}
				}
				else
				{
					var thisLocation = location.toString();
					if ( thisLocation.indexOf("HotelProfilesPage.asp") != -1 )
					{
						document.getElementById("divHotelResults").innerHTML = "No Hotel Results Returned";
					}
				}	
			}
		}
	}

	// ------------------------------------------------------------------------------------------------------
	// gets destinations and their corresponding types in xml format
	function getAllDestAndTypes()
	{
		var thisVals = "siteCode=" + g_aspDynamicVendor + "&searchReturnType=allDestAndTypes";
		doXmlHttpPost( thisVals, "allDestAndTypes", "" );
	}
	
	// ------------------------------------------------------------------------------------------------------
	// returns hotel profile html
	function getHotelProfile( thisSearchDest, thisSiteCode, thisSearchParentCode, thisTourOperator, thisVendor, thisSBU )
	{
		var thisVals = "searchReturnType=detail&searchDest=" + thisSearchDest + "&siteCode=" + thisSiteCode + "&searchParentCode=" + thisSearchParentCode + "&tourOperator=" + thisTourOperator + "&Vendor=" + thisVendor + "&SBU=" + thisSBU;
		doXmlHttpPost( thisVals, "detail", "" );
	}
	
	// ------------------------------------------------------------------------------------------------------
	// returns complete hotel list html
	function getHotelList( thisSearchDest, thisSiteCode )
	{
		var thisVals = "searchReturnType=all&searchDest=" + thisSearchDest + "&siteCode=" + thisSiteCode;
		doXmlHttpPost( thisVals, "all", "" );
	}
	
	// ---------------------------------------------------------------------------------------------------------
	// returns limited hotel list html
	function getHotelListLimited( thisSearchDest, thisSiteCode, thisSearchChoices, thisSearchHotelRating, thisSearchMealPlan )
	{
		var thisVals = "searchReturnType=limited&searchDest=" + thisSearchDest + "&siteCode=" + thisSiteCode + "&searchChoices=" + thisSearchChoices + "&searchHotelRating=" + thisSearchHotelRating + "&searchMealPlan=" + thisSearchMealPlan;
		doXmlHttpPost( thisVals, "limited", "" );
	}
	
	// ---------------------------------------------------------------------------------------------------------
	// returns breadcrumb html for HotelProfilesPage.asp
	function getHotelBreadCrumb( thisSearchDest, thisSearchParentCode )
	{	   
		var thisVals = "Dest=" + thisSearchDest + "&HotelCode=" + thisSearchParentCode;		
		doXmlHttpPost( thisVals, "HotelBreadcrumb", "HotelBreadcrumb_REST.asp" );
	}
	
	// ---------------------------------------------------------------------------------------------------------
	// constructor function for destType object
	function destType( Name, Code, Type )
	{	
		this.Name = Name;
		this.Code = Code;
		this.Type = Type;
	}
	
	// ---------------------------------------------------------------------------------------------------------
	// create array of destType objects from returned node set (web service - getSiteDestinationTypes)
	function makeDestAndTypesObjArray( xmlObj )
	{
		var thisName, thisCode, thisType;
		
		for ( i = 0; i < xmlObj.length; i++ ) 
		{
			thisName =  RTrim(xmlObj[i].getAttribute('Name'));
			thisCode =  RTrim(xmlObj[i].getAttribute('Code'));
			thisType =  RTrim(xmlObj[i].getAttribute('Type'));
		
			g_aryDestAndTypes[i] = new destType(thisName, thisCode, thisType);
		}
		
		// DEBUG
		//alert("makeDestAndTypesObjArray() --> dest value from cache form field: " + );
		
		var cachedDest = document.amenityFormCache.cachedDestCode.value;
		var finalDest;
		
		if(cachedDest == "")
		{
			finalDest = g_aspDest;
		}
		else {
			finalDest = cachedDest;
		}
		
		// build dest dropdown
		buildDestDropdown(finalDest);
		
		// show destination types
		showDestTypesSimple(finalDest);
	}

	function buildDestDropdown(currentDest)
	{
		//alert("buildDestDropdown() ==> g_aspDest = " + g_aspDest);
		
		var dropDownHTML = "";

		if ( g_aryDestAndTypes.length != -1 )
		{
			dropDownHTML += '<select name="destination" class="dropDown" onchange="getShowDestTypesFromObjArray(this)">';
			dropDownHTML += '<option value="xxx">Select destination</option>';
			
			for ( i = 0; i < g_aryDestAndTypes.length; i++ ) 
			{
				if ( i != 0 )
				{
					if ( g_aryDestAndTypes[i].Code != g_aryDestAndTypes[i-1].Code )
					{
						dropDownHTML += '<option value="' + g_aryDestAndTypes[i].Code + '"';
						
						if ( g_aryDestAndTypes[i].Code == currentDest )
						{
							dropDownHTML += " selected";
						}
						dropDownHTML += '>' + g_aryDestAndTypes[i].Name + '</option>';
					}
				}
			}
			dropDownHTML += '</select>';
			
			//alert(dropDownHTML);
			
			document.getElementById("divDestDropdown").innerHTML = dropDownHTML;
		}
		else
		{
			document.getElementById("divDestDropdown").innerHTML = "Array contains no items";
		}
	}

	//-------------------------------------------------------------------------------------------------
	// this will be involved in the onchange event for the destination dropdown; 
	// it will basically query the array of dest/name/type objects to find the current dest type(s)
	
	function getShowDestTypesFromObjArray( frm )
	{
		//alert("getShowDestTypesFromObjArray(" + frm + ")");
	
		var selectedDest = "";
		var typeString = "";
	
		if ( (typeof frm) == "object" )
		{
			selectedDest = frm.options[frm.selectedIndex].value;
		}
		else if ( (typeof frm) == "string" )
		{
			selectedDest = frm;
		}
		
		if (selectedDest != "xxx")
		{
			for ( i = 0; i < g_aryDestAndTypes.length; i++ ) 
			{
				if ( g_aryDestAndTypes[i].Code == selectedDest )
				{
					typeString += g_aryDestAndTypes[i].Type;
					typeString += "|";
				}
			}
			
			var endCharIdx = typeString.length - 1;
			
			if (typeString.charAt(endCharIdx) == "|" )
			{
				typeString = typeString.substring(0,typeString.length - 1);
			}
			
			showDestTypes(typeString);
			
			// place dest code in form to save value
			document.amenityFormCache.cachedDestCode.value = selectedDest;
		}
	}
	
	
	//-------------------------------------------------------------------------------------------------------
	// displays dest type checkboxes

	function showDestTypes( types )
	{
		var SearchByLength = document.forms["ReturnLimitedProfiles"].SearchBy.length;
		
		// check to see what is currently displayed
		var displayBeach = false;
		var displayCasino = false;
		var beachChecked = false;
		var casinoChecked = false;
		
		// see if beach checked
		for (i = 0; i < SearchByLength; i++)
		{
			if (document.forms["ReturnLimitedProfiles"].SearchBy[i].value == "10")
			{
				beachChecked = true;
			}
		}
	
		// see if casino checked
		for (i = 0; i < SearchByLength; i++)
		{
			if (document.forms["ReturnLimitedProfiles"].SearchBy[i].value == "22177")
			{
				casinoChecked = true;
			}
		}
		
		// --------------------------------------------------------------------------------------------------
		if (types != "") 
		{
			if (types.indexOf("|") != "") 
			{
				var aryTypes = types.split("|");
				
				for (i = 0; i < aryTypes.length; i++) 
				{
					if (aryTypes[i] == "Beach") 
					{
						document.getElementById("tblBeach").style.display = "block";
						displayBeach = true;
					}
					else if (aryTypes[i] == "Casino") 
					{
						document.getElementById("tblCasino").style.display = "block";
						displayCasino = true;
					}
				}
			}
			else 
			{
				// this dest has a single destType, so display appropriate type check box
				if ( types == "Beach" ) 
				{
					document.getElementById("tblBeach").style.display = "block";
					displayBeach = true;
				}
				else if ( types == "Casino" ) 
				{
					document.getElementById("tblCasino").style.display = "block";
					displayCasino = true;
				}
			}
		}
		
		// CLEANUP
		// hide dest type checkboxes and uncheck if they should not be displayed
		if ( !displayBeach ) 
		{
			document.getElementById("tblBeach").style.display = "none";
			
			if ( beachChecked )
			{
				// uncheck beach check box
				for (i = 0; i < SearchByLength; i++)
				{
					if (document.forms["ReturnLimitedProfiles"].SearchBy[i].value == "10")
					{
						document.forms["ReturnLimitedProfiles"].SearchBy[i].checked = false;
					}
				}
			}
		}
	
		if ( !displayCasino ) 
		{
			document.getElementById("tblCasino").style.display = "none";
			
			if ( casinoChecked )
			{
				// uncheck casino check box
				for (i = 0; i < SearchByLength; i++)
				{
					if (document.forms["ReturnLimitedProfiles"].SearchBy[i].value == "22177")
					{
						document.forms["ReturnLimitedProfiles"].SearchBy[i].checked = false;
					}
				}
			}
		}
	}
	
	function showDestTypesSimple( currentDest )
	{
		if ( (g_aryDestAndTypes.length != -1) || (g_aryDestAndTypes.length != 0)  )
		{
			getShowDestTypesFromObjArray( currentDest );
		}		
	}
	
	//-------------------------------------------------------------------------------------------------
	// sets global variable whenever "get" function is called so 
	// handleXmlHttpRequestStateChange() knows what to do with the response

	function setGlobalReturnType(returnType)
	{
		g_hotelListType = returnType;
	}
	
	//-------------------------------------------------------------------------------------------------
	// removes excess whitespace from right side of string
	
	// whitespace characters
	var whitespace = " \t\n\r";
	
	function RTrim( strTrim )
	{
		var str = new String( strTrim );
		var i = 0;
		var c = "";
		var endpos = 0;

		for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
			c = str.charAt(i);
			if (whitespace.indexOf(c) == -1)
			{
				endpos = i;
			}
		}

		return str.substring(0,endpos+1);
	}
	

	

	


	
	
	

	


