	//Copyright A W Cox T/A mediafusion 2009

	var sortCol = 4;
	var lastCol = 4;
	var currentHotel = 0;
	var currentLateDeal = 0;
	var latedeallist = "";
	var lastsection = "photos";
	var storebutton = "false"
	var loading = "false"
	var image_server = "http://images.activehotels.com/images/hotel/max300/";

	
	function imagepostload()
		{
		placepics();
		grabTable();
		}
		
	
	function placepics(){
		var picArray = piclist.split(",");
		for (i = 0; i < picArray.length; i++)
			{
			targetid = "pic_" + (i + 1);
			document.getElementById(targetid).src = image_server + picArray[i];
			}
		}
		
		
	var dStyle = "";
	
	function toggleTable() {
		if ( dStyle == "none" )
			{ document.getElementById('togglepics').innerHTML = "Condense Hotel Results";
			dStyle = ""; }
		else { document.getElementById('togglepics').innerHTML = "Expand Hotel Results";
			dStyle = "none";
			}
		tbl = document.getElementById('hotelGrid');
		var len = tbl.rows.length;
		
		for( i=1 ; i< len; i++ ){
			if ( tbl.rows[i].className == "picrow" ) {
			 tbl.rows[i].style.display = dStyle; }
			 }
		}
		
		
	function grabTable()
		{
		var hotelTable = document.getElementById('hotelGrid');
		arrayRows = hotelTable.rows.length/4;
		hotelArray = new Array(arrayRows);
		var rowPtr = 0;
		for ( r = 0; r < arrayRows; ++r )
			{
			hotelArray[r] = new Array(7);
			hotelArray[r][0] = hotelTable.rows[rowPtr].cells[0].innerHTML;
			hotelArray[r][1] = hotelTable.rows[rowPtr + 1].cells[0].innerHTML;
			hotelArray[r][2] = hotelTable.rows[rowPtr + 1].cells[1].innerHTML.substring(1, hotelTable.rows[rowPtr + 1].cells[1].innerHTML.length);
			hotelArray[r][3] = hotelTable.rows[rowPtr + 1].cells[2].innerHTML;
			hotelArray[r][4] = hotelTable.rows[rowPtr + 1].cells[3].innerHTML;
			hotelArray[r][5] = hotelTable.rows[rowPtr + 1].cells[4].innerHTML.replace("%", '');
			hotelArray[r][6] = hotelTable.rows[rowPtr + 1].cells[5].innerHTML;
			hotelArray[r][7] = hotelTable.rows[rowPtr + 2].cells[0].innerHTML;
			rowPtr = rowPtr + 4
			}
		}
		
		
		
	function updatelinkTable() {
			var tbody = document.getElementById("hotelGrid");
			for (r = tbody.rows.length; r > 0; --r)
			{ tbody.deleteRow(r - 1); }
			for (i = 0; i < hotelArray.length; ++i)
				{
				hotellink = hotelArray[i][7];
				
				//Hotel Name Row
				var row = document.createElement("tr");
				row.className = "hoteltitlebg";
				if ( i % 2 ) {
					row.setAttribute("bgColor", "\#ccc997");
					//row.onmouseout = function() { this.style.backgroundColor="\#ccc997"; };
					} else {
					row.setAttribute("bgColor", "\#f2efb3");
					//row.onmouseout = function() { this.style.backgroundColor="\#f2efb3"; };
					}
					
				var hotelNameCell = document.createElement("td");
				hotelNameCell.setAttribute("colSpan", "6");
				hotelNameCell.innerHTML = hotelArray[i][0];
				row.appendChild(hotelNameCell);
				tbody.appendChild(row);
				
				//Hotel Buttons and Stats Row
				var row = document.createElement("tr");
				row.className = "hoteltitlebg";
				//row.onmouseover = function() { this.style.backgroundColor="e0e0e0"; };
				if ( i % 2 ) {
					row.setAttribute("bgColor", "\#ccc997");
					//row.onmouseout = function() { this.style.backgroundColor="\#ccc997"; };
					} else {
					row.setAttribute("bgColor", "\#f2efb3");
					//row.onmouseout = function() { this.style.backgroundColor="\#f2efb3"; };
					}

				var hotelCell = document.createElement("td");
				//hotelCell.className = "hotelnamecell";
				hotelCell.innerHTML = hotelArray[i][1];
				row.appendChild(hotelCell);
				
				var roomsCell = document.createElement("td");
				roomsCell.setAttribute("align", "center");
				roomsCell.innerHTML = "&pound;" + hotelArray[i][2];
				row.appendChild(roomsCell);
				
				var starsCell = document.createElement("td");
				starsCell.setAttribute("align", "center");
				starsCell.innerHTML = hotelArray[i][3];
				row.appendChild(starsCell);
				
				var distanceCell = document.createElement("td");
				distanceCell.setAttribute("align", "center");
				distanceCell.innerHTML = hotelArray[i][4];
				row.appendChild(distanceCell);
				
				var hapCell = document.createElement("td");
				hapCell.setAttribute("align", "right");
				hapCell.setAttribute("width", "32");
				hapCell.innerHTML = hotelArray[i][5] + "%";
				row.appendChild(hapCell);
				
				var tGauge = document.createElement("td");
				tGauge.setAttribute("valign", "middle");
				tGauge.setAttribute("width", "50");
				tGauge.innerHTML = hotelArray[i][6];
				row.appendChild(tGauge);
				tbody.appendChild(row);
				
				// Hotel image row
				var row = document.createElement("tr");
				row.className = "picrow";
				
				var picCell = document.createElement("td");
				picCell.setAttribute("colSpan", "6");
				picCell.innerHTML = hotelArray[i][7];
				row.appendChild(picCell);
				tbody.appendChild(row);
				
				// spacer row (not contained in array)
				var row = document.createElement("tr");
				//row.className = "picrow";
				
				var spacerCell = document.createElement("td");
				spacerCell.setAttribute("colSpan", "6");
				spacerCell.className = "cell_bb";
				spacerCell.innerHTML = '<img src="../../../logos/pixel.gif" alt="" width="1" height="1" border="0">';
				row.appendChild(spacerCell);
				tbody.appendChild(row);
				}
			if ( dStyle == "none" )
			{ dStyle = ""; }
			else 
			{ dStyle = "none"; }
			toggleTable();
		}
		
		
	
	function tableSort(selectedCol) {
			sortCol = selectedCol;
			if (selectedCol == lastCol) {
				hotelArray.reverse(); // reverse sort - descending
				if ( document.getElementById('col_' + selectedCol).className == "sort_asc" )
				{
				document.getElementById('col_' + selectedCol).className = "sort_desc";
				} else {
				document.getElementById('col_' + selectedCol).className = "sort_asc";}
			}
			else { // clicked on a new column - sort as indicated ascending
				document.getElementById('col_' + selectedCol).className = "sort_asc";
				document.getElementById('col_' + lastCol).className = "sort_none";
				hotelArray.sort(numSort);
			}
			updatelinkTable();
			lastCol = selectedCol;
			return 0;
		}
		
		
		
	function numSort(a, b) {
			numA = a[sortCol]
			numB = b[sortCol]
			if (isNaN(numA)) { return 0;}
			else {
				if (isNaN(numB)) { return 0; }
				else { return numA - numB; }
			}
		}
		
		
		
	function loadingstuff(target,size,id) {
			var graphicholder = document.createElement("div");
			graphicholder.className = "loading";
			graphicholder.setAttribute("align", "center");
			graphicholder.style.height = size + "px";
			graphicholder.id = "loading_" + id;
			offset = ( size / 2 ) - 40;
			var loadingHTML = '<img src="http://www.uk-hotel-accommodation.co.uk/logos/loading.gif" alt="Loading" width="80" height="80" border="0" style="margin-top:' + offset + 'px;">';
			graphicholder.innerHTML = loadingHTML;
			var loadingdivTag = document.createElement("div");
			loadingdivTag.id = id + "";
			if ( size == 480 ) {
				document.getElementById(target).appendChild(loadingdivTag); //append hotel
				} else {
				document.getElementById(target).insertBefore(loadingdivTag, document.getElementById(target).firstChild);//prepend late deals
				}
			document.getElementById(id).appendChild(graphicholder);
		}
		
		
	
	function displaybrochure(hotel_id,hotelname) {
		if ( loading == "true" ) {
		alert("Please wait until previous request finishes loading");
		return;
		}
		if (document.getElementById("hotel_" + hotel_id) ) {
			if ( currentHotel != hotel_id ) {
				if ( document.getElementById('hotel_' + currentHotel) ) {
				document.getElementById('hotel_' + currentHotel).style.display = "none"; //hide last hotel
				}
			document.getElementById("hotel_" + hotel_id).style.display = "block";
			document.getElementById(lastsection + '_' + currentHotel).style.display = "none"; //hide last section selected of above hotel
			if ( document.getElementById(lastsection + '_' + hotel_id) ) { //if section not available show photos
				if ( lastsection == "reviewdetails"  && document.getElementById("loading_review_" + hotel_id) ) { //get reviews if not loaded
				fetchReviews(hotel_id,hotelname);
				}
			document.getElementById(lastsection + '_' + hotel_id).style.display = "";
			} else {
			document.getElementById('photos_' + hotel_id).style.display = "";
			lastsection = "photos";
			}
		}

		} else {  //hotel doesn't exist so fetch brochure
			loadingstuff('hotelinfobox',480,'hotel_' + hotel_id);
			brochurefetch(hotel_id,hotelname); //fetch hotel brochure
		}
		
		//deal with currentHotel (last hotel viewed)
		if ( currentHotel != hotel_id ) {
			//close last hotel if open
			if (document.getElementById('hotel_' + currentHotel) ) {
				document.getElementById('hotel_' + currentHotel).style.display = "none";
				if ( document.getElementById(lastsection + '_' + currentHotel) ) {
				document.getElementById(lastsection + '_' + currentHotel).style.display = "none"; // hide lastsection
				}
			}
			//close last late deals if open
			if (document.getElementById(currentHotel) ) {
				document.getElementById(currentHotel).style.display = "none";
				}
			//open new late deals if previously loaded
			if (document.getElementById(hotel_id) ) {
				document.getElementById(hotel_id).style.display = "block";
				}
		}
		currentHotel = hotel_id;
	}
	
	
	
	function brochurefetch(hotel_id,hotelname) {
	        if (window.XMLHttpRequest) {
	            var req = new XMLHttpRequest();
	        }
	        else if (window.ActiveXObject) {
	            var req = new ActiveXObject("Microsoft.XMLHTTP");
	        }
			loading = "true";
	    	req.open("GET", pathext + "hotel-brochure.cfm?hotel_id=" + hotel_id + "&hotelname=" + hotelname, true);
			req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	        req.send(null);
			var reqTimer = setTimeout(function() {
			req.abort();
			timeout(hotel_id,'loading_hotel_',hotelname);
			}, 15000);
	        req.onreadystatechange = function()
				{
	            if ((req.readyState == 4) && (req.status == 200))
					{
					clearTimeout(reqTimer);
	                var hotelbrochure = req.responseText;
					document.getElementById('hotel_' + hotel_id).innerHTML = hotelbrochure;
					lastsection = "photos";
					loading = "false";
					}
				}
			}
			
			
			
	function timeout(hotel_id,target,hotelname) {
		if ( target == "loading_hotel_" ) {
			var exHREF = "http://www.activehotels.com/wl/servlet/xmlbrochure/index.do?hotelid=" + hotel_id + "&trkref=MDF";
			var timeoutComment = "Unable to load " + hotelname;
			} else if ( target == "loading_" ) {
			var exHREF = "http://www.activehotels.com/wl/servlet/xmlbrochure/lateavail.do?hotelid=" + hotel_id + "&trkref=MDF";
			var timeoutComment = '<img src="../../../logos/closebtn.gif" alt="Close" width="15" ' + 'height="15" border="0" onclick="closeMe(' + hotel_id + ');" class="closebtn">'
			if ( latedeallist == "" ) {
					latedeallist = hotel_id + "";
				} else {
					latedeallist = latedeallist + "," + hotel_id;
				}
			timeoutComment = timeoutComment + "Unable to load " + hotelname + " Late Deals";
			} else {
			var exHREF = "http://www.activehotels.com/hotel/brochure/en/reviews/" + hotel_id + "?trkref=MDF";
			var timeoutComment = "Unable to load " + hotelname + " Reviews";
			}
			var exLinkHTML = '<a href="' + exHREF + '" target="extHotelFrame">Click here to view in new window</a>';
			window.open(exHREF,"hotel","menubar=yes,toolbar=yes");
			document.getElementById(target + hotel_id).innerHTML = timeoutComment + "<br>" + exLinkHTML;
			document.getElementById(target + hotel_id).style.height = "40px";
			loading = "false";
		}
		
	
	function showSection(sectionName,hotel_id){
		if ( sectionName != lastsection ) {
			if ( sectionName == "reviewdetails"  && document.getElementById("loading_review_" + hotel_id) ) {
			fetchReviews(hotel_id,'Hotel');
			}
			document.getElementById(sectionName + "_" + hotel_id).style.display = "";
			document.getElementById(lastsection + "_" + hotel_id).style.display = "none";
			lastsection = sectionName;
	    }
	}
	
	
	function otherDates(hotel_id,hotelname) {
		if ( loading == "true" ) {
		alert("Please wait until previous request finishes loading");
		return;
		}
		if (document.getElementById("hotel_" + hotel_id) ) {
			showSection('brochuredates',hotel_id);
		} else {
			displaybrochure(hotel_id,hotelname);
		}
	}
	
	
	function clickToEnlarge(source,hotel_id){
        document.getElementById("mainpic_" + hotel_id).src = source;
    }
	
	
	function fetchReviews(hotel_id,hotelname) {
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
		loading = "true";
        req.open("GET", pathext + "fetch-reviews.cfm?hotel_id=" + hotel_id, true);
		req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        req.send(null);
        var reqTimer = setTimeout(function() {
			req.abort();
			timeout(hotel_id,'loading_review_',hotelname);
			}, 10000);
        req.onreadystatechange = function()
			{
            if ((req.readyState == 4) && (req.status == 200))
				{
				clearTimeout(reqTimer);
                var reviewpage = req.responseText;
				document.getElementById('reviews_' + hotel_id).innerHTML = reviewpage;
				loading = "false";
				}
			}
		} 
		
		
	function lateDealsExist(hotel_id,hotelname) {
		if ( loading == "true" ) {
		alert("Please wait until previous request finishes loading");
		return;
		}
		
		if (document.getElementById(hotel_id) ) {
			document.getElementById(hotel_id).style.display = "block";
		} else {
			loadingstuff('hotelinfobox',82,hotel_id);
			fetchLateDeals(hotel_id,hotelname);
		}
		
		if ( currentHotel != hotel_id ) {
			//close previous late deal
			if (document.getElementById(currentHotel) ) {
				document.getElementById(currentHotel).style.display = "none";
				}
			//close last hotel if open
			if (document.getElementById('hotel_' + currentHotel) ) {
				document.getElementById('hotel_' + currentHotel).style.display = "none";
				}
		}
		
		//open this hotel if previously loaded
		if (document.getElementById('hotel_' + hotel_id) ) {
			document.getElementById('hotel_' + hotel_id).style.display = "block";
			document.getElementById(lastsection + '_' + currentHotel).style.display = "none"; //hide last section selected of above hotel
			if ( document.getElementById(lastsection + '_' + hotel_id) ) { //if section not available show photos
				if ( lastsection == "reviewdetails"  && document.getElementById("loading_" + hotel_id) ) { //get reviews if not loaded
				fetchReviews(hotel_id,hotelname);
				}
			document.getElementById(lastsection + '_' + hotel_id).style.display = "";
			} else {
			document.getElementById('photos_' + hotel_id).style.display = "";
			lastsection = "photos";
			}
			
			}
		currentHotel = hotel_id;
	}
	
	
	function fetchLateDeals(hotel_id,hotelname) {
        if (window.XMLHttpRequest) {
            requestDeals = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            requestDeals = new ActiveXObject("Microsoft.XMLHTTP");
        }
		loading = "true";
        requestDeals.open("GET", pathext + "fetch-latedeals.cfm?hotel_id=" + hotel_id + "&hotelname=" + hotelname, true);
		requestDeals.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        requestDeals.send(null);
		var reqTimer = setTimeout(function() {
			req.abort();
			timeout(hotel_id,'loading_',hotelname);
			}, 10000);
        requestDeals.onreadystatechange = function()
			{
            if ((requestDeals.readyState == 4) && (requestDeals.status == 200))
				{
				clearTimeout(reqTimer);
                var latedealstable = requestDeals.responseText;
				document.getElementById(hotel_id).innerHTML = latedealstable;
				loading = "false";

				}
			}
		}	
		
		
	function toggleRoom(roomtype) {
		if ( document.getElementById(roomtype).style.display == "" || document.getElementById(roomtype).style.display == "none" ) {
			document.getElementById(roomtype).style.display = "block";
			} else {
			document.getElementById(roomtype).style.display = "none";
			}
		}
		
		
	function toggleDeals() {
		if ( document.getElementById('latedeals').style.display == "none" ) {
			document.getElementById('latedeals').style.display = "block";	
			} else {
			document.getElementById('latedeals').style.display = "none";
			}
		}
		
		
	function closeMe(box_id) {
		document.getElementById(box_id).style.display = "none";
		}
