var useDHTML = false;
if (document.getElementById) useDHTML = true;

var fulljauntlet = false;
//if (window.top != window.self) window.top.location = window.self.location;

/*
function showSamples() {
	var s = "";
	document.getElementById("Samples").innerHtml = "visible";
}
*/

var helpvisible = false;

function showHelp() {
	if (useDHTML) {
		if (helpvisible) {
			document.getElementById("helptext").innerHTML = "&nbsp;";
			document.getElementById("helplink").innerHTML = "&gt; Show Help";
			helpvisible = false;
		} else {
			document.getElementById("helptext").innerHTML  = document.getElementById("helpcontents").innerHTML;
			document.getElementById("helplink").innerHTML = "V Hide Help";
			helpvisible = true;
		}
		setDivHeight();
	}
}

var useproxy = false;
function getXMLDoc() {
	var x = false;
	//url = "http://www.jauntlet.com/xmlproxy.aspx?URL="+encodeURIComponent(url);
    if(window.XMLHttpRequest) {
    	try {
			x = new XMLHttpRequest();
			//x.overrideMimeType("text/xml");
			//useproxy = true;
        } catch(e) { }
    } else if(window.ActiveXObject) {
       	try {
        	x = new ActiveXObject("Microsoft.XMLHTTP");
      	} catch(e) {
        	try {
          		x = new ActiveXObject("Msxml2.XMLHTTP");
        	} catch(e) { }
		}
    }
	return x;
}

var xmlmapdoc;
function loadmap() {
    try {
		if (xmlmapdoc.readyState == 4) {
			if (xmlmapdoc.status == 200) {
				document.getElementById("map").src = xmlmapdoc.responseXML.documentElement.firstChild.nodeValue;
			}
   	 	}
	} catch(e) { }
}

/*
function showMap() {
	if (hasMap) {
		try {
			//if (GBrowserIsCompatible()) {
			
			/* google 
			var map = new GMap(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			if (clat != 99999) {
				map.centerAndZoom(new GPoint(clon, clat), 12);
				createMarker(map, clon, clat);
			}
			*/
			
			/* yahoo */
			/*
			if (clat == 99999) {
				document.getElementById("map").src = "images/world.gif";
			} else {
				xmlmapdoc = getXMLDoc();	
				var mapurl = "http://api.local.yahoo.com/MapsService/V1/mapImage?appid=jauntlet&latitude="+clat+"&longitude="+clon+"&image_type=gif&image_height=180&image_width=360&zoom=12";
				if (true) mapurl = "http://www.jauntlet.com/xmlproxy.aspx?URL="+encodeURIComponent(mapurl);
				if(xmlmapdoc) {
					xmlmapdoc.onreadystatechange = loadmap;
					xmlmapdoc.open("GET", mapurl, true);
					xmlmapdoc.send(null);
				}
			}
			
			if(document.MainForm.SM) {
				// set hostelworld
				document.getElementById("HostelWorld").contentWindow.document.theForm.SearchTerm.value = document.MainForm.City[document.MainForm.City.selectedIndex].text;
				var m = document.MainForm.SM.value;
				if (m < 10) m = "0"+m;
				var d = document.MainForm.SD.value;
				if (d < 10) d = "0"+d;
				var y = document.MainForm.SD.value.substr(2,2);
				var dd = document.MainForm.ED.value;
				if (dd < 10) dd = "0"+dd;
				var s = new Date(document.MainForm.SM[document.MainForm.SM.selectedIndex].text+" "+d+", "+document.MainForm.SY.value);
				var e = new Date(document.MainForm.EM[document.MainForm.EM.selectedIndex].text+" "+dd+", "+document.MainForm.EY.value);
				var ns = new Date(e.getTime() - s.getTime());
				var nights = ns.getDate();
				if (nights > 7) nights = 7;
				document.getElementById("HostelWorld").contentWindow.document.theForm.selMonth.value = m;
				document.getElementById("HostelWorld").contentWindow.document.theForm.selDay.value = d;
				document.getElementById("HostelWorld").contentWindow.document.theForm.selYear.value = document.MainForm.SY.value;
				document.getElementById("HostelWorld").contentWindow.document.theForm.NumNights.value = nights;
			}
		//}
		} catch(e) { }
	}
}
*/


function initMap() {
	if (hasMap) {
		xmlcitydoc = getXMLDoc();	
		var url = "http://www.jauntlet.com/xmlcity.aspx?search=id&cityid="+document.MainForm.CityID.value;
		if(xmlcitydoc) {
			xmlcitydoc.onreadystatechange = loadInitCity;
			xmlcitydoc.open("GET", url, true);
			xmlcitydoc.send(null);
		}
	} else {
		if (document.getElementById("CitySelect")) {
			document.getElementById("CitySelect").style.display="block";
		}
	}
	if (fulljauntlet) {
		showJourneyMap();
	}
}


function loadInitCity() {
    //try {
		if (xmlcitydoc.readyState == 4) {
			if (xmlcitydoc.status == 200) {
				var c = xmlcitydoc.responseXML.documentElement.getElementsByTagName("city");
				var id = c[0].getElementsByTagName("id")[0].firstChild.nodeValue;
				var d = c[0].getElementsByTagName("name")[0].firstChild.nodeValue;
				if (c[0].getElementsByTagName("region")[0].firstChild) d += ", " + c[0].getElementsByTagName("region")[0].firstChild.nodeValue;
				dests[0] = d + ", " + c[0].getElementsByTagName("country")[0].firstChild.nodeValue;
				lats[0] = parseFloat(c[0].getElementsByTagName("lat")[0].firstChild.nodeValue);
				lons[0] = parseFloat(c[0].getElementsByTagName("lon")[0].firstChild.nodeValue);
				selectCity(id,0)
			}
   	 	}
	//} catch(e) { }
}





function showMap(i) {
	//if (hasMap) {
		try {
			//if (GBrowserIsCompatible()) {
			
			/* google 
			var map = new GMap(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			if (clat != 99999) {
				map.centerAndZoom(new GPoint(clon, clat), 12);
				createMarker(map, clon, clat);
			}
			*/
			
			/* yahoo */
			//if (clat == 99999) {
				//document.getElementById("map").src = "images/world.gif";
			//} else {
				xmlmapdoc = getXMLDoc();
				// id also jauntlet or travelmap?
				var mapurl = "http://api.local.yahoo.com/MapsService/V1/mapImage?appid=1eUKVhfV34F18QNDpXp2.jbFUAFa471hFu3eG4f2PK.1ZYCPyJUKYyJw.jAlGyMxJOtg6xQ-&latitude="+lats[i]+"&longitude="+lons[i]+"&image_type=gif&image_height=180&image_width=360&zoom=12";
				mapurl = "http://www.jauntlet.com/xmlproxy.aspx?URL="+encodeURIComponent(mapurl);
				if(xmlmapdoc) {
					xmlmapdoc.onreadystatechange = loadmap;
					xmlmapdoc.open("GET", mapurl, true);
					xmlmapdoc.send(null);
					//alert(mapurl);
				}
			//}
			
			if(document.MainForm.SM) {
				// set hostelworld
				var dest = document.getElementById("Dest").innerHTML.split(",");
				document.getElementById("HostelWorld").contentWindow.document.theForm.SearchTerm.value = dest[0];
				var m = document.MainForm.SM.value;
				if (m < 10) m = "0"+m;
				var d = document.MainForm.SD.value;
				if (d < 10) d = "0"+d;
				var y = document.MainForm.SD.value.substr(2,2);
				var dd = document.MainForm.ED.value;
				if (dd < 10) dd = "0"+dd;
				var s = new Date(document.MainForm.SM[document.MainForm.SM.selectedIndex].text+" "+d+", "+document.MainForm.SY.value);
				var e = new Date(document.MainForm.EM[document.MainForm.EM.selectedIndex].text+" "+dd+", "+document.MainForm.EY.value);
				var ns = new Date(e.getTime() - s.getTime());
				var nights = ns.getDate();
				if (nights > 7) nights = 7;
				document.getElementById("HostelWorld").contentWindow.document.theForm.selMonth.value = m;
				document.getElementById("HostelWorld").contentWindow.document.theForm.selDay.value = d;
				document.getElementById("HostelWorld").contentWindow.document.theForm.selYear.value = document.MainForm.SY.value;
				document.getElementById("HostelWorld").contentWindow.document.theForm.NumNights.value = nights;
			}
		//}
		} catch(e) { }
	//}
}


var dests = new Array();
var lats = new Array();
var lons = new Array();
var xmlcitydoc;
function loadcity() {
    //try {
		if (xmlcitydoc.readyState == 4) {
			if (xmlcitydoc.status == 200) {
				//alert(xmlcitydoc.responseXML.documentElement);
				var list = "";
				var c = xmlcitydoc.responseXML.documentElement.getElementsByTagName("city");
				for (var i = 0; i < c.length; i++) {
					var d = c[i].getElementsByTagName("name")[0].firstChild.nodeValue;
					if (c[i].getElementsByTagName("region")[0].firstChild) d += ", " + c[i].getElementsByTagName("region")[0].firstChild.nodeValue;
					if (c[i].getElementsByTagName("uscounty")[0].firstChild) d += " (" + c[i].getElementsByTagName("uscounty")[0].firstChild.nodeValue + " County)";
					list += "<a href=\"javascript:selectCity("+c[i].getElementsByTagName("id")[0].firstChild.nodeValue+","+i+");\">Select This &gt;&gt;</a> " + d + "<br />\n";;
					dests[i] = d + ", " + c[i].getElementsByTagName("country")[0].firstChild.nodeValue;
					lats[i] = parseFloat(c[i].getElementsByTagName("lat")[0].firstChild.nodeValue);
					lons[i] = parseFloat(c[i].getElementsByTagName("lon")[0].firstChild.nodeValue);
					if (i == 99) {
						list += "100 cities found; please try a narrower search.";
						break;
					}
				}
				if (list == "") {
					list = "Can't find any cities with that name.";
				}
				document.getElementById("Cities").innerHTML = list;
				document.MainForm.findbut.disabled = false;
			}
   	 	}
	//} catch(e) { }
}


function findCity() {
	xmlcitydoc = getXMLDoc();	
	var url = "http://www.jauntlet.com/xmlcity.aspx?search=name&countryid="+document.MainForm.Country[document.MainForm.Country.selectedIndex].value+"&city="+encodeURIComponent(document.MainForm.City.value);
	//alert(url);
	if(xmlcitydoc) {
		document.getElementById("Cities").innerHTML = "Finding cities...";
		xmlcitydoc.onreadystatechange = loadcity;
		xmlcitydoc.open("GET", url, true);
		xmlcitydoc.send(null);
		document.MainForm.findbut.disabled = true;
	}
}


function selectCity(id,i) {
	document.MainForm.CityID.value = id;
	//document.MainForm.Lat.value = lats[i];
	//document.MainForm.Lon.value = lons[i];
	//document.getElementById("CitySelect").style.visibility="hidden";
	document.getElementById("Cities").innerHTML = "";
	document.MainForm.City.value = "";
	document.getElementById("Saver").innerHTML = document.getElementById("CitySelect").innerHTML;
	document.getElementById("CitySelect").innerHTML = "";
	//document.getElementById("Details").style.top = "-" + document.getElementById("CitySelect").offsetHeight + "px";
	document.getElementById("Dest").innerHTML=dests[i] + " <a style=\"font-weight:normal\" href=\"javascript:changeCity();\">&lt;&lt; Change This</a>";
	document.getElementById("Details").style.display="block";
	showMap(i);
}

function changeCity() {
	document.getElementById("CitySelect").innerHTML = document.getElementById("Saver").innerHTML;
	document.getElementById("Saver").innerHTML = "";
	document.getElementById("Details").style.display="none";
	document.getElementById("CitySelect").style.display="block";
}


function createMarker (map, lon, lat) {
		var point = new GPoint(lon, lat);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		//var html = "<span style=\"font-size:9pt;\"><strong>"+name+"</strong><br /><a href=\""+url+"\" target=\"_blank\">"+url+"</a></span>";
  		//GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
		//marker.openInfoWindowHtml(html);
}

function setDivHeight () {
// set content and sidebar divs to the larger height
	if (useDHTML) {
		if (document.getElementById("main")) {
			var m = document.getElementById("main").offsetHeight;
			var r = document.getElementById("right").offsetHeight;
			var h = document.body.offsetHeight - document.getElementById("header").offsetHeight;
			//if (hasMap) h += 190; // don't know why map causes this
			// fix for non IE
			if (window.innerHeight) h = window.innerHeight - document.getElementById("header").offsetHeight;
			//alert(m+" "+r+" "+h);
			if (h > m) m = h;
			if (r > m) m = r;
			else r = m;
			//alert(m+" "+r+" "+h);
			document.getElementById("main").style.height = m+"px";
			document.getElementById("right").style.height = r+"px";
		} else {
			var mm = document.getElementById("mainbig").offsetHeight;
			var hh = document.body.offsetHeight - document.getElementById("header").offsetHeight;
			if (window.innerHeight) hh = window.innerHeight - document.getElementById("header").offsetHeight;
			//alert(mm+" "+hh);
			if (hh > mm) mm = hh;
			document.getElementById("mainbig").style.height = mm+"px";
		}
	}
}


function doRollOn(whichMenu) {
	if (useDHTML) {
		var s = whichMenu.split("-");
		var doRoll = true;
		if (s[0] == "mainmenu") {
			if (parseInt(s[1]) == currentMainMenu) doRoll = false;
		} else {
			ss = s[0].split("-");
			if ((parseInt(ss[1]) == currentMainMenu) && (parseInt(s[1]) == currentSubMenu)) doRoll = false;
		}
		if (doRoll) {
			var id = s[0] + "_" + s[1];
			var src = "http://www.jauntlet.com/images/" + s[0] + "_on_0" + s[1] + ".gif";
			//alert(id+" "+src);
			document.getElementById(id).src = src;
		}
	}
}


function doRollOff(whichMenu) {
	if (useDHTML) {
		var s = whichMenu.split("-");
		var doRoll = true;
		if (s[0] == "mainmenu") {
			if (parseInt(s[1]) == currentMainMenu) doRoll = false;
		} else {
			ss = s[0].split("_");
			if ((parseInt(ss[1]) == currentMainMenu) && (parseInt(s[1]) == currentSubMenu)) doRoll = false;
		}
		if (doRoll) {
			var id = s[0] + "_" + s[1];
			var src = "http://www.jauntlet.com/images/" + s[0] + "_off_0" + s[1] + ".gif";
			document.getElementById(id).src = src;
		}
	}
}


function showMenu(whichMenu) {
	if (useDHTML) {
		if (whichMenu != currentMainMenu) {
			var m = document.getElementById("submenu_"+whichMenu);
			var t = getAbsoluteTop(document.getElementById("mainmenu_1")) +27;
			var l = getAbsoluteLeft(document.getElementById("mainmenu_1"));
			m.style.top = t + "px";
			m.style.left = l + "px";
			m.style.visibility = "visible";
			doRollOn("mainmenu-"+whichMenu);
		}
	}
}


function hideMenu(whichMenu) {
	if (useDHTML) {
		if (whichMenu != currentMainMenu) {
			var m = document.getElementById("submenu_"+whichMenu);
			m.style.visibility = "hidden";
			doRollOff("mainmenu-"+whichMenu);
		}
	}
}

function getAbsoluteTop(whichElement) {
// returns the top pixel position of an element on a page
	var t; 
	if (whichElement == document.body) {
		t = 0;
	} else {
		t = whichElement.offsetTop + getAbsoluteTop(whichElement.offsetParent);
	}
	return t;
} 


function getAbsoluteLeft(whichElement) {
// returns the left pixel position of an element on a page
	var l; 
	if (whichElement == document.body) {
		l = 0;
	} else {
		l = whichElement.offsetLeft + getAbsoluteLeft(whichElement.offsetParent);
	}
	return l;
} 


function showCalendar (whichCal) {
	if (useDHTML) {
		document.getElementById("CalFrame").style.top = getAbsoluteTop(document.getElementById(whichCal+"C")) - 20 +"px";
		document.getElementById("CalFrame").style.left = getAbsoluteLeft(document.getElementById(whichCal+"C")) + 20 +"px";
		document.getElementById("CalFrame").style.visibility = "visible";
		document.getElementById("CalFrame").src = "http://www.jauntlet.com/calendar.aspx?d="+document.getElementById(whichCal+"D").value+"&m="+document.getElementById(whichCal+"M").value+"&y="+document.getElementById(whichCal+"Y").value+"&c="+whichCal;
	}
	
}


function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}