// 30/09/2008 - Changed the default values of the drop-down lists used for the ajax filter.
// 30/10/2008 - Removed #ip# from the links as it broke them all
// 15/01/2009 - Added Top 4 Listings
// 24/02/2009 - Added default sort order of 'best_sellers'
// 09/12/2009 - Added AdSense-for-Search functionality - ST


// Need to use this function for IE, as it ignores the 'disabled' attribute.

function CheckDisabledFields(elem_id)
{
	var bool_elem_disabled = false;
	var elem = document.getElementById(elem_id);
	
	if (elem.options[elem.selectedIndex].disabled) {
		bool_elem_disabled = true;
		// Now reset the drop down list so that the first item is selected.
		// If you didn't do this, the *disabled* item would still be selected
		alert('This element is unavailable. ');
		elem.selectedIndex = 0;
	}

}

// Ajax Connection code

var http_request = false;

function makeRequest(url) {
	
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = processResponse;
	http_request.open('GET', url, true);
	http_request.send(null);

}

function processResponse() {

	if (http_request.readyState == 4) {

		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var str_Output = '';

			// Get the form fields
			var num_selectItems = xmldoc.getElementsByTagName('criteria').length - 1;			
			for (var i = 0; i <= num_selectItems; i++) {
				var int_activityID = xmldoc.getElementsByTagName('activityID').item(i).firstChild.data;
				var int_destinationID = xmldoc.getElementsByTagName('destinationID').item(i).firstChild.data;
				var str_priceSort = xmldoc.getElementsByTagName('priceSort').item(i).firstChild.data;
				var int_nbResults = xmldoc.getElementsByTagName('nbResults').item(i).firstChild.data;
			}
			
			var num_programmes = xmldoc.getElementsByTagName('programme').length - 1;
			var int_Top4Listings = xmldoc.getElementsByTagName('topListing').length - 1;
			
			if (int_Top4Listings > -1) {
				var str_AjaxContainerID = 'AJAXContainerWithTop4';
			} else {
				var str_AjaxContainerID = 'AJAXContainer';
			}

			str_Output = str_Output + '<div id="'+str_AjaxContainerID+'"><div class="AJAX"><span class="SearchNumber SearchNumberstyle"> '+int_nbResults+' </span> trips have been found based on your filter/sort</div>';
			str_Output = str_Output + '<div id="FormRow"> Filter by: ';

			var str_previousSelectBox = '';
			var num_selectItems = xmldoc.getElementsByTagName('selectbox').length - 1;
			var int_previousLevel = 0;
			
			for (var i = 0; i <= num_selectItems; i++) {
			
				var str_selectName = xmldoc.getElementsByTagName('name').item(i).firstChild.data;
				var str_selectValue = xmldoc.getElementsByTagName('value').item(i).firstChild.data;
				var int_selectId = xmldoc.getElementsByTagName('id').item(i).firstChild.data;
				var int_selectLevel = xmldoc.getElementsByTagName('level').item(i).firstChild.data;
				
				if (str_previousSelectBox != str_selectName)	
					{
						int_selectIndex = 1;
						int_currentSelectLevel = 0;
					}


				switch (str_selectName) {
						
					case 'destination':
						if (int_previousLevel != int_selectLevel)
							int_currentSelectLevel = int_selectLevel;
							
						if ((int_selectIndex == 1) && (int_currentSelectLevel < 0) && (int_selectId != 0))
						{
							var j_from = int_currentSelectLevel;
						}
						else
							var j_from = 0;
						
						if (int_selectIndex == 1)
						{	
							str_Output = str_Output + '<select name="destination" size="1" class="selectArea" id="select_destination" onchange="makeRequest(\'/functions/filterResults.cfm?destinationID=\'+this.value+\'&activityID=\'+document.getElementById(\'select_activity\').value+\'&price_sort=\'+document.getElementById(\'select_priceSort\').value);">';
							//if (int_destinationID == 0)
							//{str_Output = str_Output + '<option value="0">Destination</option>';}
						}

						str_Output = str_Output + '<option value="'+int_selectId+'" ';
						// pre-select value
						if (int_destinationID == int_selectId)		str_Output = str_Output + 'selected ';
						
						// update adsense-for-search
						/*
						if (int_destinationID == int_selectId) {
							var m = document.getElementById("adsense_main");
							var e = document.getElementById("adsense_exist");
							m.removeChild(e);
							var d = document.createElement("div");
							d.id = "adsense_exist";
							var asf_channel = (int_nbResults == 0)?"2611357302":"0233404336";
							d.innerHTML = "<script language=\"JavaScript\" src=\"/javascript/adsense.js\"></script> \
								<script type=\"text/javascript\" language=\"JavaScript\"> \
								google_afs_query = '"+str_selectValue+" travel'; \
								google_afs_ad = 'w0n3'; \
								google_afs_client = 'pub-7811436858523093'; \
								google_afs_channel = '"+asf_channel+"'; \
								google_afs_hl = 'en'; \
								google_afs_ie = 'utf8'; \
								google_afs_oe = 'utf8'; \
								google_afs_adtest = 'on'; // REMOVE BEFORE DEPLOYMENT \
								</script> \
								<script src=\"http://www.google.com/afsonline/show_afs_ads.js\" language=\"javascript\"></script>"
							;
							m.appendChild(d);
						}
						*/
						
						// add spaces for level 2 dest.
						if (int_currentSelectLevel == 0)
							str_Output = str_Output + '>'+str_selectValue+'</option>';	
						else
						{
							str_Output = str_Output + '>';
							
							for (j=j_from;j<int_currentSelectLevel;j++)
								{str_Output = str_Output + '&nbsp;&nbsp;&nbsp;';}
							str_Output = str_Output + str_selectValue+'</option>';
						}

											
						break;
						
					case 'activity':
						if (int_previousLevel != int_selectLevel)
							int_currentSelectLevel = int_currentSelectLevel + 1;
						
					
						if (int_selectIndex == 1)
						{
							str_Output = str_Output + '</select> Filter by: <select name="activity" size="1" class="selectArea" id="select_activity" onchange="makeRequest(\'/functions/filterResults.cfm?destinationID=\'+document.getElementById(\'select_destination\').value+\'&activityID=\'+this.value+\'&price_sort=\'+document.getElementById(\'select_priceSort\').value);">';
							//if (int_activityID == 0)
							//{	str_Output = str_Output + '<option value="0">Activity</option>';}	
						}

						str_Output = str_Output + '<option value="'+int_selectId+'" ';
						// pre-select value
						if (int_activityID == int_selectId)		str_Output = str_Output + 'selected ';
						// add spaces for level 2 dest.
						if (int_currentSelectLevel == 1)
							str_Output = str_Output + '>'+str_selectValue+'</option>';	
						else
						{
							str_Output = str_Output + '>';
							for (j=1;j<int_currentSelectLevel;j++)
								{str_Output = str_Output + '&nbsp;&nbsp;&nbsp;';}
							str_Output = str_Output + str_selectValue+'</option>';
						}
						
						break;
				}
				int_selectIndex = int_selectIndex + 1;
				str_previousSelectBox = str_selectName;
				int_previousLevel = int_selectLevel;
			}
			
			// add price sort
			str_Output = str_Output + '</select>';
			str_Output = str_Output + ' Sort by: <select name="price_sort" size="1" class="selectArea" id="select_priceSort" onchange="makeRequest(\'/functions/filterResults.cfm?destinationID=\'+document.getElementById(\'select_destination\').value+\'&activityID=\'+document.getElementById(\'select_activity\').value+\'&price_sort=\'+this.value);">';
			str_Output = str_Output + ' <option value="best_sellers" ';
			if (str_priceSort == 'best_sellers')		str_Output = str_Output + 'selected ';
			str_Output = str_Output + '>Best Sellers</option>';
			str_Output = str_Output + ' <option value="desc" ';
			if (str_priceSort == 'desc')		str_Output = str_Output + 'selected ';
			str_Output = str_Output + '>Highest Price</option>';
			str_Output = str_Output + '<option value="asc" ';
			if (str_priceSort == 'asc')		str_Output = str_Output + 'selected ';
			str_Output = str_Output + '>Lowest Price</option></select></div>';
			
			
			// top 4 listings
			if (int_Top4Listings > -1) {
				str_Output = str_Output + '<div class="AjaxTopListings">';
				str_Output = str_Output + '<div class="TopListingIcon">';
				str_Output = str_Output + '<img src="/assets/4ofthebest.gif" alt="4 of the best" border="0" />';
				str_Output = str_Output + '</div>';
				for (var i = 0; i<=int_Top4Listings; i++) {
					var int_progId = xmldoc.getElementsByTagName('programmeID').item(num_programmes+i+1).firstChild.data;
					var str_progTitle = xmldoc.getElementsByTagName('programmeTitle').item(num_programmes+i+1).firstChild.data;
					var str_progShortcut = xmldoc.getElementsByTagName('programmeShortcut').item(num_programmes+i+1).firstChild.data;
					var str_progImageFilename = xmldoc.getElementsByTagName('programmeImage').item(num_programmes+i+1).firstChild.data;
					var str_progPrice = xmldoc.getElementsByTagName('programmePrice').item(num_programmes+i+1).firstChild.data;
					var str_progAltText = xmldoc.getElementsByTagName('programmeAlttext').item(num_programmes+i+1).firstChild.data;
					
					str_Output = str_Output + '<div class="TopListing">';
					str_Output = str_Output + '<a href="'+str_progShortcut+'"><img width="70" height="52" src="'+str_progImageFilename+'" alt="'+str_progAltText+'" border="0" class="TopListing" /></a><br />';
					str_Output = str_Output + '<a href="'+str_progShortcut+'" class="TopListingLink">'+str_progTitle+'</a>';
					str_Output = str_Output + '</div>';
				}
				str_Output = str_Output + '</div>';
			}
			
			
			str_Output = str_Output + '</div>';			

			// Deal with programmes now
			if (num_programmes == -1)
			{
				//str_Output = str_Output + '<tr><td align=center style="padding-top:10px;">Your selection results in no programmes,<BR>clear the selection to start a new search<BR><BR><a onclick="makeRequest(\'/functions/filterResults.cfm?activityID='+int_activityID+'&select_price=0&select_destination=0&select_duration=0\');" style="cursor:hand;cursor:pointer;"><img src="/images/buttons/btn_clear_selection.gif" align="absmiddle" border="0"></a></td></tr>';
			}
			else
			{
				for (var i = 0; i <= num_programmes; i++) {

					var int_progId = xmldoc.getElementsByTagName('programmeID').item(i).firstChild.data;
					var str_progTitle = xmldoc.getElementsByTagName('programmeTitle').item(i).firstChild.data;
					var str_progShortcut = xmldoc.getElementsByTagName('programmeShortcut').item(i).firstChild.data;
					var str_progImageFilename = xmldoc.getElementsByTagName('programmeImage').item(i).firstChild.data;
					var str_progDescription = xmldoc.getElementsByTagName('programmeDescription').item(i).firstChild.data;
					var str_progPrice = xmldoc.getElementsByTagName('programmePrice').item(i).firstChild.data;
					var str_progAltText = xmldoc.getElementsByTagName('programmeAlttext').item(i).firstChild.data;
					var str_progTripCode = xmldoc.getElementsByTagName('programmeTripCode').item(i).firstChild.data;
					
					
					str_Output = str_Output + '<br /><div id="programme-box">';
					str_Output = str_Output + '<div id="title-bar"><div id="title"><a class="class1" href="'+str_progShortcut+'">'+str_progTitle+'</a></div>';
					str_Output = str_Output + '<div id="price">'+str_progPrice+'</div>';
					str_Output = str_Output + '<div id="from">from</div><div style="clear:both;"></div></div>';
					str_Output = str_Output + '<div id="summary-container"><div id="prog-thumb">';

					if ((str_progImageFilename != '') && (str_progImageFilename != 'undefined'))
					{
						str_Output = str_Output + '<img src="'+str_progImageFilename+'" width="90" alt="'+str_progAltText+'" align="right" style="margin-right:5px;" />';
					}
					if (str_progDescription != 'undefined')
						str_Output = str_Output + '</div><div id="summary-blurb">'+str_progDescription+'</div>';
					else
						str_Output = str_Output + '</div><div id="summary-blurb">&nbsp;</div>';
						
					str_Output = str_Output + '<div id="left-links">';
					
					/*
					str_Output = str_Output + '<a class="class2" href="/Booking-a-great-gap-year">Book now</a><br />';
					str_Output = str_Output + '<a class="class2" href="'+str_progShortcut+'">More details</a></div>';
					*/
					
					str_Output = str_Output + '<div style="padding:5px 0px 0px 0px;width:81px;"><a href="/booking_request.cfm?tripcode='+str_progTripCode+'"><img src="/images/buttons/btn-book-now-small.gif" alt="book now button" title="Book now" width="81" height="21" border="0"></a></div>';
					str_Output = str_Output + '<div style="padding:5px 0px 0px 0px;width:81px;"><a href="'+str_progShortcut+'"><img src="/images/buttons/btn-details-small.gif" alt="details button" title="Details" width="81" height="21" border="0"></a></div>';

					str_Output = str_Output + '</div><div style="clear:both;"></div></div></div>';
				}
			}

			document.getElementById('FilteredResults').innerHTML = str_Output;
			
		} else {
			alert('There was a problem with the request.');
		}   //status
	}  // ready state
} //function
