﻿var hasSearchBlock = false; //indicates search block filled in
var gomap;
var doAvSearching = false; //indicates calling availability

function setMsg(msg) {
	msg.innerHTML = msg;
}

function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}

function getUrl(url, cb) {
  var xmlhttp = getXmlHttp();
  xmlhttp.open("GET", url);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      cb(xmlhttp.status, 
         xmlhttp.getAllResponseHeaders(), 
         xmlhttp.responseText);
      }
  }
  xmlhttp.send(null);
}

/* ranking */
var xmlUrls, crntUrl;

function getRank() {
	try {
		//if (navigator.appName=="Microsoft Internet Explorer") {
			try { 
				var ctrprefix = getGenCrtlPrefix();
				document.getElementById(ctrprefix + 'frrank').src="/Pages/callingpr.aspx"; 
		} catch(e){}
			//GeneralWs.General.GetUrlsForR('', GotUrlsForR);
		//}
	}
  catch(e){}
}

function GotUrlsForR(result, eventArgs) {
	try {
		var parsDoc = GXml.parse(result);
		if (parsDoc.documentElement != undefined && parsDoc.documentElement.getElementsByTagName("url") != undefined) {
			xmlUrls = parsDoc.documentElement.getElementsByTagName("url");
			crntUrl = 0;
			callRank(xmlUrls[crntUrl].getAttribute("url"));
		}
	}
	catch(e){}
}

function callRank(url) {
	var strGR;
	try {
		strGR = xmlUrls[crntUrl].getAttribute("url");
		GDownloadUrl(strGR, gotRank);
		//getUrl(strGR, gotRank);
	}
	catch(e){ try { document.getElementById("frrank").src=strGR; } catch(e){} }
}

function gotRank(data, responseCode) {
	if (data.substring(9, 10) != '' && isFinite(data.substring(9, 10))) {
		xmlUrls[crntUrl].setAttribute("rank", data.substring(9, 10));
	}
	else {
		xmlUrls[crntUrl].setAttribute("rank", '0');
	}
	xmlUrls[crntUrl].removeAttribute("url");
	if ( (crntUrl+1) < xmlUrls.length ) {
		crntUrl++;
		window.setTimeout('callRank("'+xmlUrls[crntUrl].getAttribute("url")+'");', Math.ceil((1+Math.random()*5) * 1000));
	}
	else {
		GeneralWs.General.GotRForUrls(xmlUrls.context.xml);
	}
}
/* EO ranking */

//dhtmlHistory.initialize();
//dhtmlHistory.addListener(historyChange);

/** Our callback to receive history change events. */
function historyChange(newLocation, historyData) {
  //alert("A history change has occurred: " + "newLocation=" + newLocation + ", historyData=" + historyData);
}

function expSearch() {
	try {
		var oBody = document.body, ctrprefix = getGenCrtlPrefix();
		document.getElementById(ctrprefix + 'tabSearch').className= "tabActive";
		document.getElementById(ctrprefix + 'tabMap').className = "tabInactive";
		document.getElementById(ctrprefix + 'tabHotel').className = "tabInactive";
		document.getElementById(ctrprefix + 'mainContentSearch').className = "";
		document.getElementById(ctrprefix + 'mainContentMap').className = "contentInactive";
		document.getElementById(ctrprefix + 'mainContentHotel').className = "contentInactive";
		showSearch();
	}
  catch(e){alert(e.message);}
}

function expMap() {
	try {
		var oBody = document.body, ctrprefix = getGenCrtlPrefix();
		document.getElementById(ctrprefix + 'tabSearch').className = "tabInactive";
		document.getElementById(ctrprefix + 'tabMap').className = "tabActive";
		document.getElementById(ctrprefix + 'tabHotel').className = "tabInactive";
		document.getElementById(ctrprefix + 'mainContentSearch').className = "contentInactive";
		document.getElementById(ctrprefix + 'mainContentMap').className = "";
		document.getElementById(ctrprefix + 'mainContentHotel').className = "contentInactive";
		//
		if (gomap == undefined) firstMap();
		if ( document.body.currentPropID != undefined )
			hiProp('hli' + document.body.currentPropID);
	}
  catch(e){alert(e.message);}
}

function expHotel() {
	try {
		var oBody = document.body, ctrprefix = getGenCrtlPrefix();
		document.getElementById(ctrprefix + 'tabSearch').className= "tabInactive";
		document.getElementById(ctrprefix + 'tabMap').className = "tabInactive";
		document.getElementById(ctrprefix + 'tabHotel').className = "tabActive";
		document.getElementById(ctrprefix + 'mainContentSearch').className = "contentInactive";
		document.getElementById(ctrprefix + 'mainContentMap').className = "contentInactive";
		document.getElementById(ctrprefix + 'mainContentHotel').className = "";
	}
  catch(e){alert(e.message);}
}

function startBook() {
	var arr = document.getElementsByTagName('select');
	var argsRooms = '';
  var args = document.getElementById('propID').value;
	
	for ( var i = 0; i < arr.length; i++ ) {
		if ( arr[i].className == 'book') {
			if ( arr[i].value != "0" ) {
				argsRooms += arr[i].name + '=' + arr[i].value + '&';
			}
		}
	};
	if ( argsRooms == '' ) {alert ('Please first select the number of rooms required');}
	else {
		var url = 'https://secure.booking.com/book.en.html?aid=308395&label=my&stage=1&' +  args + '&' + argsRooms;
		var wdwopt = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + screen.width + ',height=' + screen.height + ',left=0,top=0';
		//window.navigate(url);
		window.location.href=url;
		return;
	}
	for ( var i = 0; i < arr.length; i++ ){
		if ( arr[i].className == 'book') {
			arr[i].focus();
			return;
		}
	};
	return;
}


function displayRoomDetails(roomAViewDetails, roomDetails) {
	try {
		var o = document.getElementById(roomDetails);
		if ( o != undefined ) {
			if ( o.style.display == 'block' ) {
				o.style.display = 'none';
				roomAViewDetails.innerHTML = 'view details';
			}
			else {
				o.style.display = 'block';
				//o.scrollIntoView(0);
				roomAViewDetails.innerHTML = 'hide details';
			};
		}
	}
  catch(e){}
}

function showSearch() {
	try {
		if (hasSearchBlock) return;
		var divSearch = document.getElementById('divContentSearch');
		if (divSearch != undefined ) {
			GeneralWs.General.GetContCountries('',GotContCountries);
			hasSearchBlock = true;
		}
	}
  catch(e){alert(e.message);}
}

function GotContCountries(result, eventArgs) {
	try {
		document.getElementById("divContentSearch").innerHTML = result;
	}
  catch(e){}
}

function showSecPage(pageName) {
	try {
		toggleSecPage();
		GeneralWs.General.GetSecPage(pageName,GotSecPage);
	}
  catch(e){alert(e.message);}
}

function hideSecPage() {
	try {
		toggleSecPage();
		document.getElementById("secPageContent").innerHTML = "";
	}
  catch(e){alert(e.message);}
}

function toggleSecPage() {
	try {
		var o = document.getElementById("secPage");
		var c = document.getElementById("secPageBox")
		if ( o != undefined ) {
			if ( o.style.display == 'block' ) {
				o.style.display = 'none';
				c.style.display = 'none';
			}
			else {
				o.style.display = 'block';
				c.style.display = 'block';
				o.scrollIntoView(1);
			};
		}
	}
  catch(e){alert(e.message);}
}

function GotSecPage(result, eventArgs) {
	try {
		document.getElementById("secPageContent").innerHTML = result;
	}
  catch(e){}
}

function showPhotos(propertyID) {
	try {
		var photoFrame = document.getElementById("photoFrame");
		if (photoFrame != undefined) {
			photoFrame.style.display = 'block';
			if (photoFrame.propertyID == undefined || photoFrame.propertyID != propertyID) {
				photoFrame.propertyID = propertyID;
				document.getElementById("photoFrame").innerHTML = '';
				GeneralWs.General.GetPhotos(propertyID,GotPhotos);
			}
		}
	}
  catch(e){}
}

function hidePhotos() {
	try {
		var photoFrame = document.getElementById("photoFrame");
		if (photoFrame != undefined) {
			photoFrame.style.display = 'none';
		}
	}
  catch(e){}
}

function GotPhotos(result, eventArgs) {
	try {
		document.getElementById("photoFrame").innerHTML = result;
	}
  catch(e){}
}

function showPhoto(url,width,height) {
	try {
		var photo = document.getElementById("photoContent");
		var strurl = "url('" + url + "')";
		if (photo.style != undefined && photo.style.backgroundImage != strurl) {
			photo.style.backgroundImage = strurl;
		}
	}
  catch(e){alert(e.message);}
}

/*************************** MAPPING *************************/
function unloadMap() {
	try {
    if (GBrowserIsCompatible()) {
			GUnload();
		}
	}
  catch(e){alert(e.message);}
}

function firstMap() {
	try {
		if (gomap != undefined) gomap.clearOverlays();
		var mapOuter = document.getElementById('mapOuter');
		if (mapOuter != undefined) {
			gomap = new GMap2(mapOuter);
			gomap.doubleClickZoomEnabled();
			gomap.enableContinuousZoom();
			gomap.addControl(new GLargeMapControl());
			gomap.addControl(new GScaleControl());
			var oBody = document.body;
			loadMap( gomap, oBody.getAttribute("swlat"), oBody.getAttribute("swlon"), oBody.getAttribute("nelat"), oBody.getAttribute("nelon") );
		}
	}
  catch(e){alert(e.message);}
}

function loadMap(map,swlat,swlon,nelat,nelon) {
	try {
		if (map != undefined && swlat != undefined && swlon != undefined && nelat != undefined && nelon != undefined) {
			map.clearOverlays();
			GEvent.addListener(map, "moveend", function() {
				if(map.getZoom() < 10) {
					setMsg('To view hotels on the map, please zoom to a detailed map using the zoom bar at the left');
				}
				else { getHotelsPage(1,10); }
			} );
			var bnds = new GLatLngBounds();
			var point = new GLatLng(swlat,swlon);
			bnds.extend(point);
			point = new GLatLng(nelat,nelon);
			bnds.extend(point);
			var clat = (bnds.getNorthEast().lat() + bnds.getSouthWest().lat()) /2;
			var clng = (bnds.getNorthEast().lng() + bnds.getSouthWest().lng()) /2;
			map.setCenter(new GLatLng(clat,clng), map.getBoundsZoomLevel(bnds));
		}
	}
  catch(e){alert(e.message);}
}

function feHo() {
	gomap.clearOverlays();
	var lp = document.getElementById("listProperties");
	if (lp != undefined) {
		var lpi = document.getElementsByName("listPropertyLink");
		for (var i = 0; i < lpi.length; i++) {
			lpi[i].marker = addMarker( lpi[i].getAttribute("id").substring(3)
				, parseFloat(lpi[i].getAttribute("lat"))
				, parseFloat(lpi[i].getAttribute("lon")) );
		}
	}
}

function addMarker(id,lat,lon,iconsrc) {
	try {
		var opt = new Object();
		var point = new GLatLng(lat,lon);
		if (iconsrc == undefined) {
			var icon = new GIcon();
			icon.image = "/images/mpgr.png";
			icon.shadow = "/images/mpsh.png";
			icon.iconSize = new GSize(12, 20);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
			opt.icon = icon;
		}
		opt.clickable = true;
		opt.title = "See highlighted hotel in the list of hotels at the right. Click to view hotel details";
		var mrk = new GMarker(point, opt);
		mrk.id = id;
		GEvent.addListener(mrk, "click", function() {
			try {
				var prop = document.getElementById('hli' + mrk.id);
				fireClickEvent(prop);
			}
			catch(e){alert(e.message);}
		});
		GEvent.addListener(mrk, "mouseover", function() {
			var prop = document.getElementById('hli' + mrk.id);
			if ( prop != undefined) {
				prop.parentNode.parentNode.scrollIntoView(1);
				prop.parentNode.parentNode.style.backgroundImage = "url('/Images/buttonBgHotHi.png')";
				prop.parentNode.parentNode.style.backgroundColor = "#FFFFFF";
			}
		});
		GEvent.addListener(mrk, "mouseout", function() {
			var prop = document.getElementById('hli' + mrk.id);
			if ( prop != undefined) {
				prop.parentNode.parentNode.style.backgroundImage = "";
				prop.parentNode.parentNode.style.backgroundColor = "";
			}
		});
		gomap.addOverlay(mrk);
		return mrk;
  }
  catch(e){alert(e.message);}
}

function fireClickEvent(control) { 
	if (document.all) { 
		control.fireEvent("onclick"); 
	} 
	else {
		var clickEvent = window.document.createEvent("MouseEvents"); 
		clickEvent.initEvent("click", true, true); 
		control.dispatchEvent(clickEvent); 
  } 
} 

function hiProp(propId) {
	try {
		if (gomap == undefined) return;
		var prop = document.getElementById(propId);
		if ( prop != undefined ) {
			gomap.removeOverlay(prop.marker);
			prop.marker = addMarker( prop.getAttribute("id").substring(3), parseFloat(prop.getAttribute("lat")), parseFloat(prop.getAttribute("lon")), "/images/mpwh.png" );
		}
	}
  catch(e){}
}

function loProp(propId) {
	try {
		if (gomap == undefined) return;
		var prop = document.getElementById(propId);
		if ( prop != undefined) {
			gomap.removeOverlay(prop.marker);
			prop.marker = addMarker( prop.getAttribute("id").substring(3), parseFloat(prop.getAttribute("lat")), parseFloat(prop.getAttribute("lon")) );}
	}
  catch(e){}
}

function getHotelsPage(fst,lst) {
	try {
		if (gomap == undefined) return;
		var bnds = gomap.getBounds();
		var inSort = document.getElementById('inSort'), srt;
		if (inSort != undefined) { srt = inSort.value; }
		else { srt = 'DA'; }
		setMsg("Looking up hotels for this map ...</p><p><img src='/images/wait.gif' alt='Searching for availability' width='30' height='30'/>");
		GeneralWs.General.GetHotelList(bnds.getSouthWest().lat(), bnds.getSouthWest().lng()
			, bnds.getNorthEast().lat(), bnds.getNorthEast().lng(), 'en', fst, lst, srt, GotHotelList);
  }
  catch(e){}
	//try {document.body.style.cursor = 'default';}catch(e){}
}

function GotHotelList(result, eventArgs) {
	try {
		var lblLocs = document.getElementById(getGenCrtlPrefix() + "hotelList");
		if ( lblLocs != undefined ) {
			lblLocs.innerHTML = result;
			feHo();
		}
	}
  catch(e){}
}

/********************* My ajax functions ***********************/
// returns prefix attached to each asp server control
function getGenCrtlPrefix() {
	var prefix;
	var objGenCrtlPrefix = document.getElementById("ctrlfrmPrefix");
	if (objGenCrtlPrefix) prefix = objGenCrtlPrefix.value;
	return prefix;
}

function getCrtlPrefix() {
	var prefix;
	var objCrtlPrefix = document.getElementById("ctrlPrefix");
	if (objCrtlPrefix) prefix = objCrtlPrefix.value;
	return prefix;
}

// display property details
function dispProp(propID) {
	try {
		document.getElementById("mainContentHotelOverview").innerHTML = "<div class='wait' />";
		document.getElementById("mainContentHotelDetails").innerHTML = "<div class='wait' />";
		expHotel();
		GeneralWs.General.GetHotelOverview(propID, GotHotelOverview);
		GeneralWs.General.GetHotelDetails(propID, GotHotelDetails);
		document.body.currentPropID = propID;
		var inpPropID = document.getElementById(getCrtlPrefix() + "propID");
		if ( inpPropID != undefined ) {
			inpPropID.value = propID;
		}
		var inpAvRes = document.getElementById(getCrtlPrefix() + "avres");
		if ( inpAvRes != undefined ) {
			if ( inpAvRes.innerHTML == "" ) {
				doAvSearching = false;
			}
			else {
				doAvSearching = true;
			};
			inpAvRes.innerHTML = "";
		}
		//dhtmlHistory.add('dispProp:' + propID, 'dispProp:' + propID);
		return false;
	}
  catch(e){return false;}
}

// This is the callback function invoked if the Web service succeeded.
// It accepts the result object as a parameter.
function GotHotelOverview(result, eventArgs) {
	try {
		document.getElementById("mainContentHotelOverview").innerHTML = result;
	}
  catch(e){}
}

function GotHotelDetails(result, eventArgs) {
	try {
		document.getElementById("mainContentHotelDetails").innerHTML = result;
	}
  catch(e){}
}
/********************* ***********************/
function jump(pos) {
	try {
		document.getElementById(pos).scrollIntoView(1);
		return(false);
	}
	catch(e){}
}
/********************* ***********************/
