/*
Travel Specials
L&S DC - 4/2007
*/

// "global" variables
var searchform; // main search form
var tab_date; // date selection tabs
var tab_rng;
var tab_month;

var tab_results; // search result tabs
var tab_detail;

var search_datebegin; // search parameters
var search_dateend;
var search_city;
var search_radius;
var search_type;
var search_price;
var search_keyword;

var results_container; // travel specials results containers
var results_header; // search result tbodies
var results_list;
var results_detail;

var button_container; // holds buttons for options

var SELECTED_ITEMS = []; // array to hold items that have been viewed
var SEARCH_QUERIES = []; // array to hold search url strings
var SEARCH_DATA = []; // array to hold items returned by ajax query
var STARTROW = 1; // holds results paging start row
var ENDROW = 1; // holds resutls paging end row

var HDSELECTED_ITEMS = []; // array to hold items that have been viewed
var HDSEARCH_DATA = []; // array to hold items returned by ajax query

var hotdeal_container; // hot deal results containers
var hotdeal_header; // hot deal result tbodies
var hotdeal_list;
var hotdeal_detail;

var tab_hotdealresults; // hot deal tabs
var tab_hotdealdetail;

var hdbutton_container;

var loadqs = {};
var POPUP_INVOKE = false;

var inntopia_domain = 'vacations.travelsd.inntopia.travel';
// var inntopia_domain = 'http'; // For testing.

var conversionTracking = null;

// set "global" variables after page loads
addLoadEvent(function() {
		tab_date = document.getElementById("tab_date"); // find search tabs
		tab_rng = document.getElementById("tab_daterange");
		tab_month = document.getElementById("tab_month");
		
		tab_results = document.getElementById("tab_searchresults"); // find search results tabs
		tab_detail = document.getElementById("tab_searchdetail");
		
		tab_hotdealresults = document.getElementById("tab_hotdealresults"); // find hot deal tabs
		tab_hotdealdetail = document.getElementById("tab_hotdealdetail");
		
		searchform = document.getElementById("tssearchform"); // find search parameter fields
		search_datebegin = document.getElementById("date_start");
		search_dateend = document.getElementById("date_end");
			try {
				if(search_datebegin.value == "") {
					var dd = new Date();
					search_datebegin.value = (dd.getMonth()+1)+"/"+dd.getDate()+"/"+dd.getFullYear();
					search_dateend.value = (dd.getMonth()+1)+"/"+dd.getDate()+"/"+(dd.getFullYear()+1);
				}
			} catch(err) {
				//alert("Search Form Not Present");
			}
		search_city = document.getElementById("sel_city");
		search_radius = document.getElementById("sel_radius");
		search_type = document.getElementById("sel_type");
		search_price = document.getElementById("sel_price");
		search_keyword = document.getElementById("txt_kwd");
		
		results_container = document.getElementById("resultscontainer"); // find search results display areas
		results_header = document.getElementById("results_header");
		results_list = document.getElementById("results_list");
		results_detail = document.getElementById("results_detail");
		
		hotdeal_container = document.getElementById("hotdealcontainer"); // find hot deail display areas
		hotdeal_header = document.getElementById("hotdeal_header");
		hotdeal_list = document.getElementById("hotdeal_list");
		hotdeal_detail = document.getElementById("hotdeal_detail");
		
		button_container = document.getElementById("actionbuttons");
		hdbutton_container = document.getElementById("hdactionbuttons");
		
		if(location.search.length) {
			loadqs = parseURLVarString();
			if(typeof loadqs["id"] != "undefined") {
				//alert("load travel special detail");
				getDetail(loadqs["id"]);
			} else if(typeof loadqs["hd"] != "undefined") {
				//alert("load hot deal detail");
				getHotDealDetail(loadqs["hd"]);
			} else if(typeof loadqs["method"] == "undefined") {
				//alert("run default search");
				searchData(location.search+"&method=search");
			}
		}
	});

function toggleDateSearch(opt) { // control date search tabs
	if(opt == "range") {
		tab_date.src = tab_date.src.replace(/_on\.gif/,"_off.gif");
		tab_rng.src = tab_rng.src.replace(/_off\.gif/,"_on.gif");
		tab_month.src = tab_month.src.replace(/_on\.gif/,"_off.gif");
		toggleDisplay('flatjscalendar',false);
		toggleDisplay('daterageinput',true);
		toggleDisplay('datemonthinput',false);
	} else if(opt == "month") {
		tab_date.src = tab_date.src.replace(/_on\.gif/,"_off.gif");
		tab_rng.src = tab_rng.src.replace(/_on\.gif/,"_off.gif");
		tab_month.src = tab_month.src.replace(/_off\.gif/,"_on.gif");
		setSearchEndDate("");
		toggleDisplay('flatjscalendar',false);
		toggleDisplay('daterageinput',false);
		toggleDisplay('datemonthinput',true);
	} else {
		tab_date.src = tab_date.src.replace(/_off\.gif/,"_on.gif");
		tab_rng.src = tab_rng.src.replace(/_on\.gif/,"_off.gif");
		tab_month.src = tab_month.src.replace(/_on\.gif/,"_off.gif");
		setSearchEndDate("");
		toggleDisplay('flatjscalendar',true);
		toggleDisplay('daterageinput',false);
		toggleDisplay('datemonthinput',false);
	}
}

function toggleResultDisplay(opt) { // control search result tabs and display
	clearData(button_container);
	if(!POPUP_INVOKE) {
		if(opt == "detail") {
			tab_detail.src = tab_detail.src.replace(/_off\.gif/,"_on.gif");
			tab_results.src = tab_results.src.replace(/_on\.gif/,"_off.gif");
			toggleDisplay(results_header,false);
			toggleDisplay(results_list,false);
			toggleDisplay(results_detail,true);
			// update button options
			if(SELECTED_ITEMS.length) {
				var img_pe = document.createElement("IMG");
					img_pe.setAttribute("src","/_com/travelspecials/images/bt_printspecial.gif");
					img_pe.style.marginRight = ".5em";
					img_pe.onclick = function() { return openPopupWindow('/_com/travelspecials/_travelspecial.htm?id='+SELECTED_ITEMS[SELECTED_ITEMS.length-1]+'&method=detail'); };
					img_pe.onmouseover = function() { this.style.cursor = "pointer"; };
				button_container.appendChild(img_pe);
				var img_ee = document.createElement("IMG");
					img_ee.setAttribute("src","/_com/travelspecials/images/bt_emailspecial.gif");
					img_ee.onclick = function() { return openPopupWindow('/_com/travelspecials/_email.htm?id='+SELECTED_ITEMS[SELECTED_ITEMS.length-1]+'&method=email'); };
					img_ee.onmouseover = function() { this.style.cursor = "pointer"; };
				button_container.appendChild(img_ee);
			}
		} else if(opt == "results") {
			tab_detail.src = tab_detail.src.replace(/_on\.gif/,"_off.gif");
			tab_results.src = tab_results.src.replace(/_off\.gif/,"_on.gif");
			toggleDisplay(results_header,true);
			toggleDisplay(results_list,true);
			toggleDisplay(results_detail,false);
			// update button options
			if(SEARCH_QUERIES.length) {
				var img_pr = document.createElement("IMG");
					img_pr.setAttribute("src","/_com/travelspecials/images/bt_printableresults.gif");
					img_pr.onclick = function() { return openPopupWindow('/_com/travelspecials/_results.htm'+SEARCH_QUERIES[SEARCH_QUERIES.length-1]+'&detail=1'); };
					img_pr.onmouseover = function() { this.style.cursor = "pointer"; };
				button_container.appendChild(img_pr);
			}
		}
	} else {
		// POPUP WINDOW ACTIVE
		toggleDisplay(results_header,true);
		toggleDisplay(results_list,true);
		toggleDisplay(results_detail,true);
	}
}

function resetSearch() { // reset the search parameters
	searchform.reset();
}

function setSearchStartDate(txt) { // set search start date
	search_datebegin.value = txt;
}
function setSearchEndDate(txt) { // set search end date
	search_dateend.value = txt;
}
function getSearchStartDate() { // return search start date
	//alert("sd="+search_datebegin.value);
	return search_datebegin.value;
}

function getSearchQueryString() { // get search query_string
	var dd = new Date();
	var qs = "?timestamp="+dd.getTime();
	if(!isNaN(Date.parse(search_datebegin.value))) { qs += "&sd=" + escape(search_datebegin.value); }
	if(!isNaN(Date.parse(search_dateend.value))) { qs += "&ed=" + escape(search_dateend.value); }
	if(search_city.value.length && !isNaN(search_city.value)) { qs += "&city=" + escape(search_city.value); }
	if(search_radius.value.length && !isNaN(search_radius.value)) { qs += "&radius=" + escape(search_radius.value); }
	if(search_type.value.length && !isNaN(search_type.value)) { qs += "&type=" + escape(search_type.value); }
	if(search_price.value.length && !isNaN(search_price.value)) { qs += "&price=" + escape(search_price.value); }
	if(search_keyword.value.length) { qs += "&kwd=" + escape(trim(search_keyword.value)); }
	return qs;
}

function searchData(_qs) { // search data via ajax call
	var qs = _qs;
	if(typeof _qs != "string") { qs = getSearchQueryString() + "&method=search"; }
	//alert(qs);
	SEARCH_QUERIES.push(qs);
	var ajaxobj = new AjaxObject("get","/_com/travelspecials/ajax_switch.asp",qs,updateSearchResults);
		ajaxobj.execute();
	return false;
}
function updateSearchResults(xmlHttp) {
	SEARCH_DATA = xmlHttp.responseXML.getElementsByTagName("item");
	//alert(SEARCH_DATA.length);
	toggleResultDisplay("results");
	displaySearchResults();
}
function displaySearchResults(start) {
	var maxperpage = 10; // records per page
	if(POPUP_INVOKE) { maxperpage = SEARCH_DATA.length; }
	STARTROW = start; // record to display first
		if(typeof start == "undefined") { STARTROW = 1;}
	ENDROW = STARTROW+maxperpage-1; // last record to display
		if(ENDROW > (SEARCH_DATA.length)) { ENDROW = SEARCH_DATA.length; }
	//alert(STARTROW + "|" + ENDROW + "|" + SEARCH_DATA.length);
	clearData(results_list); // empty the result set rows
	var itemrow; var datecell; var citycell; var namecell; // dom objects to be created
	var edrow; var edcell; // more dom objects for item detail
	if(SEARCH_DATA.length > 0) {
		for(var i=STARTROW-1; i<ENDROW; i++) {
			itemrow = document.createElement("tr");
			(function(i) { // closure to properly scope the iterator (i) variable for onclick method
					var i2 = i;
						itemrow.onmouseover = function() { updateClassName(this,"hover",""); this.style.cursor = "pointer"; };
						itemrow.onmouseout = function() { updateClassName(this,"","hover"); };
						if(!POPUP_INVOKE) {
							itemrow.onclick = function() { getDetail(SEARCH_DATA[i2].getElementsByTagName("pid")[0].firstChild.nodeValue); };
						} else {
							itemrow.onclick = function() {
									//return openPopupWindow('/_com/travelspecials/_special.htm?id='+SEARCH_DATA[i2].getElementsByTagName("pid")[0].firstChild.nodeValue);
									//alert("expand Detail");
									toggleDisplay("ed__"+SEARCH_DATA[i2].getElementsByTagName("pid")[0].firstChild.nodeValue)
									updateClassName(this,"inlineactive","");
								};
						}
				})(i);
			datecell = document.createElement("td");
				updateClassName(datecell,"datecell","");
				datecell.appendChild(document.createTextNode(SEARCH_DATA[i].getElementsByTagName("dates")[0].firstChild.nodeValue));				
			citycell = document.createElement("td");
				citycell.appendChild(document.createTextNode(SEARCH_DATA[i].getElementsByTagName("city")[0].firstChild.nodeValue));
				updateClassName(citycell,"green","");
			namecell = document.createElement("td");
				namecell.appendChild(document.createTextNode(SEARCH_DATA[i].getElementsByTagName("title")[0].firstChild.nodeValue));
				updateClassName(namecell,"red","");
			itemrow.appendChild(datecell);
			itemrow.appendChild(citycell);
			itemrow.appendChild(namecell);
			results_list.appendChild(itemrow);
			
			if(POPUP_INVOKE) {
				edrow = document.createElement("tr");
					edrow.setAttribute("id","ed__"+SEARCH_DATA[i].getElementsByTagName("pid")[0].firstChild.nodeValue);
					toggleDisplay(edrow,false);
				edcell = document.createElement("td");
					edcell.setAttribute("colspan",3); edcell.colSpan = 3;
					updateClassName(edcell,"inlinedetail","");
				edcell.innerHTML = createDetailHTML(SEARCH_DATA[i]);
				edrow.appendChild(edcell);
				results_list.appendChild(edrow);
			}
		}
		itemrow = document.createElement("tr");
		datecell = document.createElement("td");
			datecell.setAttribute("colspan",3); datecell.colSpan = 3;
			updateClassName(datecell,"pages","");
			var pagehtml = "Displaying (<strong>"+STARTROW+" - "+ENDROW+"</strong>) of <strong>"+SEARCH_DATA.length+"</strong> matching travel specials.<br /><br /><span>";
				if(!POPUP_INVOKE) {
					if(STARTROW > 1) { pagehtml += '&nbsp; &lt;&lt; <a href="#first" onclick="javascript: displaySearchResults(1);">First Page</a> &nbsp;'; } else { pagehtml += '&nbsp; &lt;&lt; First Page &nbsp;'; }
					if(STARTROW > maxperpage) { pagehtml += '&nbsp; &lt; <a href="#prev" onclick="javascript: displaySearchResults('+(STARTROW-maxperpage)+');">Previous Page</a> &nbsp;'; } else { pagehtml += '&nbsp; &lt; Previous Page &nbsp;'; }
					if(STARTROW < SEARCH_DATA.length-(maxperpage - 1)) { pagehtml += '&nbsp; <a href="#next" onclick="javascript: displaySearchResults('+(STARTROW+maxperpage)+');">Next Page</a> &gt; &nbsp; '; } else { pagehtml += '&nbsp; Next Page &gt; &nbsp; '; }
					if(ENDROW < SEARCH_DATA.length) { pagehtml += '&nbsp; <a href="#last" onclick="javascript: displaySearchResults('+(SEARCH_DATA.length-(maxperpage-1))+');">Last Page</a> &gt;&gt; &nbsp;'; } else { pagehtml += '&nbsp; Last Page &gt;&gt; &nbsp;'; }
				}
			datecell.innerHTML = "</span>" + pagehtml;
		itemrow.appendChild(datecell);
		results_list.appendChild(itemrow);
	} else {
		// show empty result set
		itemrow = document.createElement("tr");
		datecell = document.createElement("td");
		datecell.setAttribute("colspan",3); datecell.colSpan = 3;
		datecell.style.padding = "10px";
		datecell.innerHTML = '<p><span style="font-weight:bold; color:#bc0736;">No travel specials found matching your search criteria.</span></p><p>Try expanding your search criteria or selecting a wider date range.</p>';
		itemrow.appendChild(datecell);
		results_list.appendChild(itemrow);
	}
	location.hash = "results";
}

function getDetail(_id,_qs) { // get data detail via ajax call
	var qs;
	SELECTED_ITEMS.push(_id);
	if(typeof _qs == "string") { qs = _qs; }
	else { qs = "?id="+_id+"&method=detail"; }
	var ajaxobj = new AjaxObject("get","/_com/travelspecials/ajax_switch.asp",qs,updateSearchDetail);
		ajaxobj.execute();
	return false;
}

function updateSearchDetail(xmlHttp) { // show detail and paging
	var items = xmlHttp.responseXML.getElementsByTagName("item");
	for(var c=0; c<items[0].childNodes.length; c++) { if(!items[0].childNodes[c].hasChildNodes()) { items[0].childNodes[c].appendChild(document.createTextNode("")); } }
	clearData(results_detail);
	toggleResultDisplay("detail");
	var datarow = document.createElement("tr");
	var datacell = document.createElement("td");
	datarow.appendChild(datacell);
	results_detail.appendChild(datarow);
	if(items.length < 1) {
		datacell.innerHTML = "Sorry, no data could be found for this travel special.<br />Please ensure that you've previously selected a travel special from the \"Result List\".";
		return;
	}
	// get and write Detail HTML
	datacell.innerHTML = createDetailHTML(items[0]);
	
	//show paging element
	var itemid = items[0].getElementsByTagName("pid")[0].firstChild.nodeValue;
	if(!POPUP_INVOKE && SEARCH_DATA.length > 0) {
		var currentrow = -1;
		//alert(STARTROW + "|" + ENDROW + "|" + SEARCH_DATA.length);
		for(var d=(STARTROW-1); d<ENDROW; d++) {
			if(itemid == SEARCH_DATA[d].getElementsByTagName("pid")[0].firstChild.nodeValue) {
				currentrow = d+1;
			}
		}
		//alert("currentrow: "+currentrow);
		if(currentrow > -1) {
			navrow = document.createElement("tr");
			navcell = document.createElement("td");
				updateClassName(navcell,"pages","");
				var pagehtml = "Displaying <strong>#"+(currentrow-STARTROW+1)+"</strong> of <strong>"+(ENDROW-STARTROW+1)+"</strong> travel specials for the current results page.<br /><br />";
					if(currentrow > STARTROW) { pagehtml += '&nbsp; &lt;&lt; <a href="#first" onclick="javascript: getDetail('+SEARCH_DATA[STARTROW-1].getElementsByTagName("pid")[0].firstChild.nodeValue+');">First</a> &nbsp;'; } else { pagehtml += '&nbsp; &lt;&lt; First &nbsp;'; }
					if(currentrow > STARTROW) { pagehtml += '&nbsp; &lt; <a href="#prev" onclick="javascript: getDetail('+SEARCH_DATA[currentrow-2].getElementsByTagName("pid")[0].firstChild.nodeValue+');">Previous</a> &nbsp;'; } else { pagehtml += '&nbsp; &lt; Previous &nbsp;'; }
					if(currentrow < (ENDROW)) { pagehtml += '&nbsp; <a href="#next" onclick="javascript: getDetail('+SEARCH_DATA[currentrow].getElementsByTagName("pid")[0].firstChild.nodeValue+');">Next</a> &gt; &nbsp;'; } else { pagehtml += '&nbsp; Next &gt; &nbsp;'; }
					if(currentrow < (ENDROW)) { pagehtml += '&nbsp; <a href="#last" onclick="javascript: getDetail('+SEARCH_DATA[ENDROW-1].getElementsByTagName("pid")[0].firstChild.nodeValue+');">Last</a> &gt;&gt; &nbsp;'; } else { pagehtml += '&nbsp; Last &gt;&gt; &nbsp;'; }
				navcell.innerHTML = pagehtml;
			navrow.appendChild(navcell);
			results_detail.appendChild(navrow);
		}
	}
	if(SEARCH_DATA.length == 0) { location.hash = "results"; }
	trackConversion();
}

function createDetailHTML(items) { // create data detail html
	var itemid = items.getElementsByTagName("pid")[0].firstChild.nodeValue;
	var detailhtml = "";
	
	detailhtml += '<table class="detailheading" cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
	detailhtml += '<td>' + items.getElementsByTagName("title")[0].firstChild.nodeValue + '</td>';
	detailhtml += '</tr></table>';
	
	detailhtml += '<div class="detail">';
		detailhtml += '<strong>Offer Good: <span style="color:#cf0230;">' + items.getElementsByTagName("dates")[0].firstChild.nodeValue + "</span></strong><br />";
		detailhtml += "<strong>Price: </strong>" + items.getElementsByTagName("price")[0].firstChild.nodeValue + "<br />";
		detailhtml += "<strong>City: </strong>" + items.getElementsByTagName("city")[0].firstChild.nodeValue;
		var tmp_map = items.getElementsByTagName("map")[0].firstChild.nodeValue;
		if(tmp_map.length > 0) {
			detailhtml += ' &nbsp; <a href="';
			if(tmp_map.slice(0,4) != "http") { detailhtml += "http://"; }
			detailhtml += tmp_map + '" target="_blank">view map</a><br />';
		}
	
	detailhtml += "<hr />";
	detailhtml += items.getElementsByTagName("desc")[0].firstChild.nodeValue + '<br style="clear:both;" /><hr />';
	
	if(items.getElementsByTagName("book")[0].firstChild.nodeValue.length > 0) {
		var link = items.getElementsByTagName("book")[0].firstChild.nodeValue;
		var encoded = "'" + encodeURIComponent(link) + "'";
		detailhtml += '<a href="' + link + '" onclick="return inntopiaClick(' + encoded + ');"><img src="/_com/travelspecials/images/bt_bookonline.gif" height="27" width="119" border="0" style="float: right; margin: 0 0 1em 1em;" alt="Book Online" /></a>';
	}
	detailhtml += '<strong>Phone Number: <span style="color:#cf0230;">' + items.getElementsByTagName("phone")[0].firstChild.nodeValue + "</span></strong><br />";
	var tmp_website = items.getElementsByTagName("web")[0].firstChild.nodeValue;
		if(tmp_website.length > 0) {
			detailhtml += '<strong>Website: </strong><a href="';
			if(tmp_website.slice(0,4) != "http") { detailhtml += "http://"; }
			detailhtml += tmp_website + '" target="_blank">' + tmp_website + "</a><br />";
		}
	var tmp_email = items.getElementsByTagName("email")[0].firstChild.nodeValue;
		if(tmp_email.length > 0) {
			detailhtml += '<strong>Email: </strong><a href="mailto:' + tmp_email + '">' + tmp_email + "</a><br />";
		}
	detailhtml += '</div>';
	return detailhtml;
	//detailhtml += '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tbody><tr><td width="60%" style="padding: 0; vertical-align:top;">';
	//detailhtml += '</td><td width="40%" style="padding: 0; vertical-align:top; text-align: right;">';
	//detailhtml += '</td></tr></tbody></table>';
}

function showResultDetails(_status) {
	var rows = results_list.getElementsByTagName("tr");
	var imgtop = document.getElementById("sdtop");
	var imgbot = document.getElementById("sdbot");
	for(var i=0; i<rows.length; i++) {
		if(rows[i].id.length) {
			if(_status == false) {
				toggleDisplay(rows[i],true);
				updateClassName(rows[i-1],"inlineactive","");
				imgtop.src = imgtop.src.replace("show","hide");
				imgbot.src = imgtop.src.replace("show","hide");
			} else {
				toggleDisplay(rows[i],false);
				updateClassName(rows[i-1],"","inlineactive");
				imgtop.src = imgtop.src.replace("hide","show");
				imgbot.src = imgtop.src.replace("hide","show");
			}
		}
	}
}

// popup widow function for travel special buttons/links
function openPopupWindow(src) {
	var pwndw = window.open(src, "pwndw", "location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=565,height=450",true);
		pwndw.focus();
	return true;
}

// trim function for strings
function trim(str) { return str.replace(/^\s+/,"").replace(/\s+$/,""); }


/* #########################################
####	Hot Deals Functions
##########################################*/

// control search result tabs
function toggleHotDealDisplay(opt) {
	clearData(hdbutton_container);
	if(!POPUP_INVOKE) {
		if(opt == "detail") {
			tab_hotdealdetail.src = tab_hotdealdetail.src.replace(/_off\.gif/,"_on.gif");
			tab_hotdealresults.src = tab_hotdealresults.src.replace(/_on\.gif/,"_off.gif");
			toggleDisplay(hotdeal_header,false);
			toggleDisplay(hotdeal_list,false);
			toggleDisplay(hotdeal_detail,true);
			// update button options
			if(HDSELECTED_ITEMS.length) {
				var img_pe = document.createElement("IMG");
					img_pe.setAttribute("src","/_com/travelspecials/images/bt_printhotdeal.gif");
					img_pe.style.marginRight = ".5em";
					img_pe.onclick = function() { return openPopupWindow('/_com/travelspecials/_hotdeal.htm?hd='+HDSELECTED_ITEMS[HDSELECTED_ITEMS.length-1]+'&method=detail'); };
					img_pe.onmouseover = function() { this.style.cursor = "pointer"; };
				hdbutton_container.appendChild(img_pe);
				var img_ee = document.createElement("IMG");
					img_ee.setAttribute("src","/_com/travelspecials/images/bt_emailhotdeal.gif");
					img_ee.onclick = function() { return openPopupWindow('/_com/travelspecials/_emailhotdeal.htm?hd='+HDSELECTED_ITEMS[HDSELECTED_ITEMS.length-1]+'&method=email'); };
					img_ee.onmouseover = function() { this.style.cursor = "pointer"; };
				hdbutton_container.appendChild(img_ee);
			}
		} else if(opt == "results") {
			tab_hotdealdetail.src = tab_hotdealdetail.src.replace(/_on\.gif/,"_off.gif");
			tab_hotdealresults.src = tab_hotdealresults.src.replace(/_off\.gif/,"_on.gif");
			toggleDisplay(hotdeal_header,true);
			toggleDisplay(hotdeal_list,true);
			toggleDisplay(hotdeal_detail,false);
			// update button options
			var img_pr = document.createElement("IMG");
				img_pr.setAttribute("src","/_com/travelspecials/images/bt_printableresults.gif");
				img_pr.onclick = function() { return openPopupWindow('/_com/travelspecials/_hotdeals.htm?method=hdsearch&detail=1'); };
				img_pr.onmouseover = function() { this.style.cursor = "pointer"; };
			hdbutton_container.appendChild(img_pr);
		}
	} else {
		// POPUP WINDOW ACTIVE
		toggleDisplay(hotdeal_header,true);
		toggleDisplay(hotdeal_list,true);
		toggleDisplay(hotdeal_detail,true);
	}
}

function mouseoverHotDeal(row) {
	updateClassName(row,"hover","");
	row.style.cursor = "pointer";
}
function mouseoutHotDeal(row) {
	updateClassName(row,"","hover");
}

function searchHotDeals(_qs) { // search data via ajax call
	//alert(_qs);
	var qs = _qs;
	//if(typeof _qs != "string") { qs = getSearchQueryString() + "&method=hdsearch"; }
	//alert(qs);
	var ajaxobj = new AjaxObject("get","/_com/travelspecials/ajax_switch.asp",qs,updateHotDealSearchResults);
		ajaxobj.execute();
	return false;
}
function updateHotDealSearchResults(xmlHttp) {
	HDSEARCH_DATA = xmlHttp.responseXML.getElementsByTagName("item");
	//alert(SEARCH_DATA.length);
	toggleResultDisplay("results");
	displayHotDealSearchResults();
}
function displayHotDealSearchResults(start) {
	clearData(hotdeal_list); // empty the result set rows
	var itemrow; var datecell; var citycell; var namecell; var pricecell; // dom objects to be created
	var edrow; var edcell; // more dom objects for item detail
	if(HDSEARCH_DATA.length > 0) {
		for(var i=0; i<HDSEARCH_DATA.length; i++) {
			itemrow = document.createElement("tr");
			(function(i) { // closure to properly scope the iterator (i) variable for onclick method
					var i2 = i;
						itemrow.onmouseover = function() { updateClassName(this,"hover",""); this.style.cursor = "pointer"; };
						itemrow.onmouseout = function() { updateClassName(this,"","hover"); };
						if(!POPUP_INVOKE) {
							itemrow.onclick = function() { getHotDealDetail(HDSEARCH_DATA[i2].getElementsByTagName("pid")[0].firstChild.nodeValue); };
						} else {
							itemrow.onclick = function() {
									toggleDisplay("ed__"+HDSEARCH_DATA[i2].getElementsByTagName("pid")[0].firstChild.nodeValue)
									updateClassName(this,"inlineactive","");
								};
						}
				})(i);
			pricecell = document.createElement("td");
				updateClassName(pricecell,"price","");
				pricecell.appendChild(document.createTextNode("$"+HDSEARCH_DATA[i].getElementsByTagName("price")[0].firstChild.nodeValue));
			namecell = document.createElement("td");
				namecell.appendChild(document.createTextNode(HDSEARCH_DATA[i].getElementsByTagName("title")[0].firstChild.nodeValue));
				updateClassName(namecell,"bcell","");
			datecell = document.createElement("td");
				updateClassName(datecell,"datecell","");
				datecell.appendChild(document.createTextNode(HDSEARCH_DATA[i].getElementsByTagName("exp")[0].firstChild.nodeValue));				
			citycell = document.createElement("td");
				citycell.appendChild(document.createTextNode(HDSEARCH_DATA[i].getElementsByTagName("city")[0].firstChild.nodeValue));
				updateClassName(citycell,"green","");
			itemrow.appendChild(pricecell);
			itemrow.appendChild(namecell);
			itemrow.appendChild(datecell);
			itemrow.appendChild(citycell);
			hotdeal_list.appendChild(itemrow);
			
			if(POPUP_INVOKE) {
				edrow = document.createElement("tr");
					edrow.setAttribute("id","ed__"+HDSEARCH_DATA[i].getElementsByTagName("pid")[0].firstChild.nodeValue);
					toggleDisplay(edrow,false);
				edcell = document.createElement("td");
					edcell.setAttribute("colspan",4); edcell.colSpan = 4;
					updateClassName(edcell,"inlinedetail","");
				edcell.innerHTML = createHotDealHTML(HDSEARCH_DATA[i]);
				edrow.appendChild(edcell);
				hotdeal_list.appendChild(edrow);
			}
		}
	} else {
		// show empty result set
		itemrow = document.createElement("tr");
		datecell = document.createElement("td");
		datecell.setAttribute("colspan",4); datecell.colSpan = 4;
		datecell.style.padding = "10px";
		datecell.innerHTML = '<p><span style="font-weight:bold; color:#fff;">No Hot Deals available at this time.</span></p>';
		itemrow.appendChild(datecell);
		hotdeal_list.appendChild(itemrow);
	}
	location.hash = "hdresults";
}

function getHotDealDetail(_id,_qs) { // get hot deal detail via ajax call
	var qs;
	HDSELECTED_ITEMS.push(_id);
	if(typeof _qs == "string") { qs = _qs; }
	else { qs = "?id="+_id+"&method=hddetail"; }
	var ajaxobj = new AjaxObject("get","/_com/travelspecials/ajax_switch.asp",qs,updateHotDealDetail);
		ajaxobj.execute();
	return false;
}

function updateHotDealDetail(xmlHttp) {
	var items = xmlHttp.responseXML.getElementsByTagName("item");
	//alert(items.length);
	for(var c=0; c<items[0].childNodes.length; c++) { if(!items[0].childNodes[c].hasChildNodes()) { items[0].childNodes[c].appendChild(document.createTextNode("")); } }
	clearData(hotdeal_detail);
	toggleHotDealDisplay("detail");
	var datarow = document.createElement("tr");
	var datacell = document.createElement("td");
	datarow.appendChild(datacell);
	hotdeal_detail.appendChild(datarow);
	if(items.length < 1) {
		datacell.innerHTML = "Sorry, no data could be found for this Hot Deal.<br />Please ensure that you've previously selected a Hot Deal from the \"Current Hot Deals\" list.";
		return;
	}
	// get and write Detail HTML
	datacell.innerHTML = createHotDealHTML(items[0]);
	trackConversion();
}

function createHotDealHTML(items) {
	var itemid = items.getElementsByTagName("pid")[0].firstChild.nodeValue;
	var detailhtml = "";
	
	detailhtml += '<table class="detailheading" cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
	detailhtml += '<td>' + items.getElementsByTagName("title")[0].firstChild.nodeValue + '</td>';
	detailhtml += '<td class="price">$' + parseFloat(items.getElementsByTagName("price")[0].firstChild.nodeValue).toFixed(2);
		var tmp_price2 = parseFloat(items.getElementsByTagName("price2")[0].firstChild.nodeValue);
		if(tmp_price2 > 0) {
			detailhtml += ' - $' + tmp_price2.toFixed(2);
		}
	detailhtml += '</td></tr></table>';
	
	detailhtml += '<div class="detail">';
	detailhtml += items.getElementsByTagName("desc")[0].firstChild.nodeValue + "<br />";
	detailhtml += '<strong>Valid Dates: <span style="color:#cf0230;">' + items.getElementsByTagName("dates")[0].firstChild.nodeValue + "</span></strong><br />";
	detailhtml += "<hr />";
	
	var tmp_rdmp = items.getElementsByTagName("rdmp")[0].firstChild.nodeValue;
		if(tmp_rdmp.length > 0) {
			detailhtml += "<strong>Redemption Information:</strong> &nbsp;" + tmp_rdmp + "<br />";
		}
	detailhtml += '<strong>Phone Number: </strong><span style="color:#cf0230;">' + items.getElementsByTagName("phone")[0].firstChild.nodeValue + "</span><br />";
	
	detailhtml += "<br /><strong>Address: </strong>" + items.getElementsByTagName("addr")[0].firstChild.nodeValue;
	detailhtml += "<br /><strong>City: </strong>" + items.getElementsByTagName("city")[0].firstChild.nodeValue;
	var tmp_map = items.getElementsByTagName("map")[0].firstChild.nodeValue;
		if(tmp_map.length > 0) {
			detailhtml += '&nbsp; &nbsp; <a href="';
			if(tmp_map.slice(0,4) != "http") { detailhtml += "http://"; }
			detailhtml += tmp_map + '" target="_blank">view map</a><br />';
		}
	
	var tmp_website = items.getElementsByTagName("web")[0].firstChild.nodeValue;
		if(tmp_website.length > 0) {
			detailhtml += '<br /><strong>Website: </strong><a href="';
			if(tmp_website.slice(0,4) != "http") { detailhtml += "http://"; }
			detailhtml += tmp_website + '" target="_blank">' + tmp_website + "</a><br />";
		}
	detailhtml += '</div>';
	return detailhtml;
}

function showHotDealDetails(_status) {
	var rows = hotdeal_list.getElementsByTagName("tr");
	var imgtop = document.getElementById("sdtop");
	var imgbot = document.getElementById("sdbot");
	for(var i=0; i<rows.length; i++) {
		if(rows[i].id.length) {
			if(_status == false) {
				toggleDisplay(rows[i],true);
				updateClassName(rows[i-1],"inlineactive","");
				imgtop.src = imgtop.src.replace("show","hide");
				imgbot.src = imgtop.src.replace("show","hide");
			} else {
				toggleDisplay(rows[i],false);
				updateClassName(rows[i-1],"","inlineactive");
				imgtop.src = imgtop.src.replace("hide","show");
				imgbot.src = imgtop.src.replace("hide","show");
			}
		}
	}
}

function inntopiaClick(link) {
	var decoded = decodeURIComponent(link);
	if (decoded.toLocaleString().indexOf(inntopia_domain) < 0)
		return true;

	var pop = window.open(decoded, 'popup', 'width=800,height=600,status=yes,scrollbars=yes');
	if (pop == null || typeof (pop) == "undefined") {
		return true;
	} else {
		return false;
	}
}

function trackConversion() {
	if(conversionTracking != null)
		return;
	conversionTracking = document.createElement('iframe');
	conversionTracking.setAttribute('src', 'http://c5.zedo.com/jsc/c5/bh.html?n=305;g=20;a=73;s=1;t=r');
	conversionTracking.setAttribute('frameborder', '0');
	conversionTracking.setAttribute('marginheight', '0');
	conversionTracking.setAttribute('marginwidth', '0');
	conversionTracking.setAttribute('scrolling', 'no');
	conversionTracking.setAttribute('allowTransparency', 'true');
	conversionTracking.setAttribute('width', '1');
	conversionTracking.setAttribute('height', '1');
	
	document.body.appendChild(conversionTracking);
	
	var t = document.createElement('div');
	t.innerHTML = '<sc' + 'ript src="http://adreadytractions.com/rt/168371?p=2381"></sc' + 'ript>';
	document.body.appendChild(t);
}
