/*
 	 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:
    DATE        NAME                DESCRIPTION
    01/02/08    Patrick Duemling    FJT_ADVANCED_SEARCH - Added parameter "lnk" to string to support a parameterized detailed hotel profiles page.
	---------------------------------------------------------------------------------------------------------
*/

	// ------------------------------------------------------------------------------------------------------
	// set global variables
	var g_getHotelInfo_url = "HotelProfiles_REST.asp";
	var g_hotelListType;
	
	// Check if using IE or Mozilla
	IE4=(document.all);

	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, thisPlCode )
	{		
        //01/02/08 Patrick Duemling - Added parameter "lnk" to string to support a parameterized detailed hotel profiles page.
		var thisVals = "searchReturnType=detail&searchDest=" + thisSearchDest + "&siteCode=" + thisSiteCode + "&searchParentCode=" + thisSearchParentCode + "&tourOperator=" + thisTourOperator + "&Vendor=" + thisVendor + "&SBU=" + thisSBU + "&plCode=" + thisPlCode + "&showBookit=" + g_showBookit + "&lnk=" + g_lnk;
		doXmlHttpPost( thisVals, "detail", "" );
	}
	
	// ------------------------------------------------------------------------------------------------------
	// returns complete hotel list html
	function getHotelList( thisSearchDest, thisSiteCode, thisPlCode )
	{
		var thisVals = "searchReturnType=all&searchDest=" + thisSearchDest + "&siteCode=" + thisSiteCode + "&plCode=" + thisPlCode;
		doXmlHttpPost( thisVals, "all", "" );
	}
	
	// ---------------------------------------------------------------------------------------------------------
	// returns limited hotel list html
	function getHotelListLimited( thisSearchDest, thisSiteCode, thisSearchChoices, thisSearchHotelRating, thisSearchMealPlan, thisPlCode )
	{
		var thisVals = "searchReturnType=limited&searchDest=" + thisSearchDest + "&siteCode=" + thisSiteCode + "&searchChoices=" + thisSearchChoices + "&searchHotelRating=" + thisSearchHotelRating + "&searchMealPlan=" + thisSearchMealPlan + "&plCode=" + thisPlCode;
		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);
		
	}

	function buildDestDropdown(currentDest)
	{
		//alert("buildDestDropdown() ==> g_aspDest = " + g_aspDest);
		
		var dropDownHTML = "";
		
		if ( g_aryDestAndTypes.length != -1 )
		{
			dropDownHTML += '<select name="destination" class="dropDown">';
			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";
		}
	}

		
	//-------------------------------------------------------------------------------------------------
	// 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);
	}