//constants
var cstrstartdate = 'startdate';
var cstrenddate = 'enddate';
var cstrnights = 'nights';
var cstrprice = 'price';
var cstroccupancy = 'occupancy';
var cstrproductid = 'productid';
var cstrspecialoffertext = 'specialoffertext';
var cstrspecialoffertitle = 'specialoffertitle';
var cstrbookinglinkid = 'bookinglink'

//cookie functions
function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

//saves dates and prices for the selected holiday
function savePricesAndDates(productid,startdate,enddate,duration,price,specialoffertext,occupancy){
	//erase previous values
	eraseCookie(cstrproductid);
	eraseCookie(cstrstartdate);
	eraseCookie(cstrenddate);
	eraseCookie(cstrnights);
	eraseCookie(cstrprice);
	eraseCookie(cstrspecialoffertext);
	eraseCookie(cstroccupancy);
	//set cookies
	createCookie(cstrproductid,productid,0);
	createCookie(cstrstartdate,startdate,0);
	createCookie(cstrenddate,enddate,0);
	createCookie(cstrnights,duration,0);
	createCookie(cstrprice,price,0);
	createCookie(cstrspecialoffertext,specialoffertext,0);
	createCookie(cstroccupancy,occupancy,0);
}

function SaveRegionStructure(productid, regionstructure)
{
	eraseCookie('RS_' + productid);
	createCookie('RS_' + productid ,regionstructure,7);
}

//loads dates and prices for the selected holiday from cookie values
//set on search results page
function loadPricesAndDates(productid){
	//check date is more than 8 days from today
	var NowPlusEightDays = new Date();
	NowPlusEightDays.setTime(NowPlusEightDays.getTime()+(8*24*60*60*1000));
	
if (readCookie(cstrstartdate) != null) {

	var WebAppsDay = formatWebAppsDate(readCookie(cstrstartdate),'dd');
	var WebAppsMonth = formatWebAppsDate(readCookie(cstrstartdate),'MM');
	var WebAppsYear = formatWebAppsDate(readCookie(cstrstartdate),'yyyy');
	var WebAppsDate = new Date();
	WebAppsDate.setFullYear(parseInt(WebAppsYear),parseInt(WebAppsMonth)-1,parseInt(WebAppsDay));

	//check cookie setting is for this property!
	if(readCookie(cstrproductid)==productid && WebAppsDate > NowPlusEightDays) {		
		//show specific dates booking box
		document.getElementById('booking-table-1').style.display = 'block';
		//startdate
		writeInnerHtml(cstrstartdate,formatWebAppsDate(readCookie(cstrstartdate),'dd NNN yy'));
		//enddate
		writeInnerHtml(cstrenddate,formatWebAppsDate(readCookie(cstrenddate),'dd NNN yy'));
		//duration
		writeInnerHtml(cstrnights,readCookie(cstrnights) + " ");
		//price
		writeInnerHtml(cstrprice,readCookie(cstrprice));
		//special offer, clear title if no offer text
		if(readCookie(cstrspecialoffertext)!=''){
			writeInnerHtml(cstrspecialoffertitle,'<h2>Special offer</h2>');
			writeInnerHtml(cstrspecialoffertext,'<h6>' + readCookie(cstrspecialoffertext) + '</h6>');
		}
		//booking link
		document.getElementById(cstrbookinglinkid).href = document.getElementById(cstrbookinglinkid).href + '&startdate=' + readCookie(cstrstartdate) + '&enddate=' + readCookie(cstrenddate) + '&occupancy=' + readCookie(cstroccupancy);
	}
	else{
		//show normal booking box		
		var docBookingTable = document.getElementById('booking-table-2');		
		if (docBookingTable != null) {
			docBookingTable.style.display = 'block';
		}
	}
}
}

//writes into tag with given id
function writeInnerHtml(name,value){
	var docElement = document.getElementById(name);
	if (docElement != null) docElement.innerHTML = value;
}