/* Scripts for the Price pages */

var PriceData = {

	selectionChanged: function(elSel){
		this.arrivaldate='0';
		this.departuredate='0';
		this.loaddata();
	},
	
	setoffset: function( _direction){
		if (_direction=='prev') {
			this.arrivaldate=this.firstshownarrivaldate;
			this.departuredate=this.firstshowndeparturedate;
			this.direction='prev';
		} else {
			this.arrivaldate=this.lastshownarrivaldate;
			this.departuredate=this.lastshowndeparturedate;
			this.direction='next';
		}
		this.loaddata();
		/*alert(this.arrivaldate);
		alert(this.departuredate);
		alert(this.direction);*/
	},

	createSelectOptions: function(xmlDoc,xmlNodeName, selectId){
		var elSel = document.getElementById(selectId);
		if (elSel){
			var months = xmlDoc.getElementsByTagName(xmlNodeName);
			elSel.options.length = 0;
			
			this.addSelectOption(elSel,pricedata.label_all,'',false);
			
			for(i = 0; i < months.length; i++){
				var month = months[i];
				this.addSelectOption(elSel,month.firstChild.nodeValue,month.getAttribute("value"),(month.getAttribute("selected")=='true'));
			}
		}
	},
	
	addSelectOption: function(elSel,text,value,selected){
		//add month selections
		var elOptNew = document.createElement('option');
		elOptNew.text = text;
		elOptNew.value =  value;
		if (selected) {elOptNew.selected='true'};
		try {
			elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
		} catch(ex) {
			elSel.add(elOptNew); // IE only
		}	
	},

	loaddata: function(){
		if (!this.isLoading){
			isLoading=true;
			
			document.getElementById('pricewrapper').className="wait";
			/*
			if (window.XMLHttpRequest) { // If IE7, Mozilla, Safari, and so on: Use native object
				request = new XMLHttpRequest();
			}
			else
			{
				if (window.ActiveXObject) { // ...otherwise, use the ActiveX control for IE5.x and IE6
					request = new ActiveXObject('MSXML2.XMLHTTP.3.0');
				}
			}
			*/
		
			request = new XMLHttpRequest;
			//Setup the request
					
			var url=
				"/cs/Satellite?pagename=Sunparks/Village_C/SPVillagePricesLoadDataXML&c=Village_C&cid="+pricedata.parcid
					+"&country="+pricedata.country
					+"&lang="+pricedata.lang;
			
			if (gup("rendermode")!="" && gup("rendermode")!="live") url = url+"&rendermode="+gup("rendermode");

			if (pricedata.accommodationid){
				url = url +"&s_accommodation="+pricedata.accommodationid;	
			}
			
			if (document.getElementById('selPersons')){
				if (document.getElementById('selPersons').options.length>0){
					if (document.getElementById('selPersons').selectedIndex!=0){
						url = url +"&s_persons="+document.getElementById('selPersons').options[document.getElementById('selPersons').selectedIndex].value;	
					}
				}
			}
			
			if (document.getElementById('selMonth').options.length>0){
				if (document.getElementById('selMonth').selectedIndex!=0){
					url = url +"&s_month="+document.getElementById('selMonth').options[document.getElementById('selMonth').selectedIndex].value;	
				}
			}

			if (document.getElementById('selPeriod').options.length>0){
				if (document.getElementById('selPeriod').selectedIndex!=0){
					url = url +"&s_staycode="+document.getElementById('selPeriod').options[document.getElementById('selPeriod').selectedIndex].value;	
				}
			}

			url = url +"&s_arrivaldate="+this.arrivaldate;	
			url = url +"&s_departuredate="+this.departuredate;
			url = url +"&s_direction="+this.direction;
			
			/* requeststates are:
				0  Uninitialised 
				1  Loading 
				2  Loaded 
				3  Interactive 
				4  Completed 
			*/
			request.theparent = this;
			request.onreadystatechange = function() {
				if (this.readyState == 4) {
					var xmlDoc = request.responseXML;
					var responsetext = request.responseText;
					// strip off invalid chars
					var xmlDoc = '<?xml version="1.0" encoding="ISO-8859-1"?>' + responsetext.substring(responsetext.indexOf('<parkpricedata>'));
					xmlDoc = customloadXMLDoc(xmlDoc);

					
					//Update the HTML table
					document.getElementById("pricediv").innerHTML=xmlDoc.getElementsByTagName("htmldata")[0].firstChild.nodeValue;
					
					//Create select options
					request.theparent.createSelectOptions(xmlDoc,'person','selPersons');
					request.theparent.createSelectOptions(xmlDoc,'period','selPeriod');
					request.theparent.createSelectOptions(xmlDoc,'month','selMonth');
					
					//Determine the total number of columns for the sooner/later link
					request.theparent.columncount=xmlDoc.getElementsByTagName("columns")[0].getAttribute("totalcolumns");
				
					//Bind thickbox popups to new links
					tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
				
					//set offset variables
					request.theparent.firstshownarrivaldate = xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("firstshownarrivaldate")[0].firstChild.nodeValue;
					request.theparent.firstshowndeparturedate = xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("firstshowndeparturedate")[0].firstChild.nodeValue;
					request.theparent.lastshownarrivaldate = xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("lastshownarrivaldate")[0].firstChild.nodeValue;
					request.theparent.lastshowndeparturedate = xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("lastshowndeparturedate")[0].firstChild.nodeValue;

					if ("yes"==xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("isFirstPage")[0].firstChild.nodeValue) {
						document.getElementById("previous").style.display = "none";
					} else {
						document.getElementById("previous").style.display = "inline";

					}

					if ("yes"==xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("isLastPage")[0].firstChild.nodeValue) {
						document.getElementById("next").style.display = "none";
					} else {
						document.getElementById("next").style.display = "inline";
					}

					if ("no"==xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("isFirstPage")[0].firstChild.nodeValue && "no"==xmlDoc.getElementsByTagName("offset")[0].getElementsByTagName("isLastPage")[0].firstChild.nodeValue) {
						document.getElementById("npseparator").style.display = "inline";
					} else {
						document.getElementById("npseparator").style.display = "none";
					}

					document.getElementById('pricewrapper').className="";
					request.theparent.isLoading=false;
				}
			}
			request.open("GET", url, true);
			request.send(null);
		}
	},

	intialize: function(){
		this.isLoading=false;
		this.columncount=0;
		this.arrivaldate='0';
		this.departuredate='0';
		this.direction='next';
		this.firstshownarrivaldate='0';
		this.firstshowndeparturedate='0';
		this.lastshownarrivaldate='0';
		this.lastshowndeparturedate='0';
		
		this.loaddata();
		
	},

	selectOffer: function(p_staycode,p_arrivalday,p_arrivalmonth, p_arrivalyear, p_countryCode, p_villageCode, p_villaCategoryCode, p_nrOfBedRooms, p_villaType, p_offerPrice, p_bundlePrice, p_maxPersons){
		var fp = new FlowPoster();
		fp.setForm("singleClickForm");

		document.form.__event.value = "book";
		document.form.stayCode.value = p_staycode;
		document.form.arrivalDay.value = p_arrivalday;;
		document.form.arrivalMonth.value = p_arrivalmonth;
		document.form.arrivalYear.value = p_arrivalyear;	
		document.form.countryCode.value = p_countryCode;
		document.form.villageCode.value = p_villageCode;
		document.form.villaCategoryCode.value = p_villaCategoryCode;
		document.form.nrOfBedRooms.value = p_nrOfBedRooms;
		document.form.villaType.value = p_villaType;
		document.form.offerPrice.value = p_offerPrice;
		document.form.bundlePrice.value = p_bundlePrice;
		document.form.maxPersons.value = p_maxPersons;	
		document.form.submit();
	}		


	
};

$(document).ready(function()
	{
		PriceData.intialize();
	}
)

function gup(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function customloadXMLDoc(str) {
	var _xmlDoc = null;
	if (document.implementation && document.implementation.createDocument)
	{
		_xmlDoc = document.implementation.createDocument("", "", null);
		var parser = new DOMParser();
		_xmlDoc = parser.parseFromString(str,"text/xml");
		
	}
	else if (window.ActiveXObject)
	{
		_xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		_xmlDoc.loadXML(str);
		
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return _xmlDoc;
	}

	return _xmlDoc;
}