// Library: macdnav.js
// Purpose: Shared functions for generating content on individual hotel
// web sites.
// Dependencies: cookies.js and qs.js JavaScript includes.

// Purpose: Testing. Uncomment to enable
function	macdnav_dbg(msg) {
	//document.write(msg + '<br>');
}


// Purpose: Displays links back to main Macdonald Hotels site if user navigated
// to hotel site via main site.
// Remarks: Status detected via value specified in "macd_source_link" querystring
// parameter and persisted in cookie
// Remarks: Return value could be used on hotel site to determine whether
// to display individual hotel navigation.
// Remarks: To be replaced by functions taht rewrite complete header (see above)
// Input: link caption (string), link css class (string)
// Output: boolean: indicates whether link from Macdonald Hotels
// was detected and Macdonald header displayed
function macdnav_writeMacdHotelsHeader (linkClass)
{
	var strQSMacdonaldLink;
	var strCookieMacdonaldLink;
	var strLinkCaption;

	// Default value if parameters not defined
	if (String(linkClass) == 'undefined') linkClass = 'menu';
	strLinkCaption = 'Back to Macdonald Hotels web site';
	// Check if value defined in querystring
	strQSMacdonaldLink = qs_getQueryStringValue('macd_source_link');
	if (strQSMacdonaldLink != null)
	{
		macdnav_dbg('link in qs - setting cookie: ' + strQSMacdonaldLink);
		// Querystring indicates visit coming from Macdonalds site
		// Store status in cookie so that link can be displayed on other pages
		WM_setCookie('macd_source_link', strQSMacdonaldLink, 1, '/');
	}
	strCookieMacdonaldLink = WM_readCookie('macd_source_link');
	if (strCookieMacdonaldLink != false)
	{
		// Cookie has been set so display link
		macdnav_dbg('cookie exists: ' + strCookieMacdonaldLink);
		macdnav_writeMacdonaldLinkHTML(strCookieMacdonaldLink, strLinkCaption, linkClass);
		//macdnav_writeMacdHotelsHeaderHTML();
		return true;
	}
	else
	{
		macdnav_writeMacdonaldLinkHTML("http://www.macdonaldhotels.co.uk", "Visit Macdonald Hotel &amp; Resorts website", linkClass);
		return true;
	}
}

// Purpose: Writes link HTML to browser
// Remarks: Used by macdnav_writeMacdHotelsHeader
function macdnav_writeMacdonaldLinkHTML (linkHref, linkCaption, linkClass)
{
	var strLinkHTML;

	strLinkHTML = '<img src="http://www.macdonaldhotels.co.uk/sharedhotelsitefiles/interface/darkarrow.gif" width="15" height="5" alt="" /><a class="' + linkClass + '" href="' + linkHref + '">' + linkCaption + '</a>'
	document.write(strLinkHTML);
}



//Created: Ben Smawfield 11 August 2004
//Purpose: to find out where the user has entered the site from
//Remarks: called at end of this script
function macdnav_trackAffiliateCode()
{
	var strQSOriginLink;

	//Check if value is defined in query string
	strQSOriginLink = qs_getQueryStringValue('ac');
	if (strQSOriginLink != null)
	{
		//Store status in a cookie so that this can be passed to MacDonalds so they can see where their users are coming from
		WM_setCookie('wbsReferrerCode', strQSOriginLink, 720, '/');
	}
}


//Created: Ben Smawfield 11 August 2004
//Purpose: passes any referring cookies to the booking form function so that they can be sent to MacDonalds
//Remarks: called from macdnav_writeBookingFormHtml()
//Returns: the cookie given by the referring site (or null)
function macdnav_getAffiliateCode()
{
	var strQSOriginLink;

	//Put the cookie into a variable to pass to MacDonalds
	strCookieOriginLink = WM_readCookie('wbsReferrerCode');

	//Pass the cookie back to the booking form function so that the referrer code can be passed to MacDonalds

	if (!strCookieOriginLink)
	{
	return null;
	}
	else
	{
		return strCookieOriginLink;
	}
}

// Writes out the Affilate Code on the new sites. Jk
function macdnav_newWriteAffiliateCode() {
	document.getElementById("referrerCode").value = macdnav_getAffiliateCode();
	
	//var html = '<input type="hidden" name="referrerCode" value="'+macdnav_getAffiliateCode()+'" />';
	//alert(html);
	//document.write(html);
}

//Created: Gavin Cummings 01 July 2005
//Purpose: to find out what keywords activated the advert the user clicked through
//Remarks: called at end of this script
function macdnav_trackKeyword()
{
	var strQSOriginLink;

	//Check if value is defined in query string
	strQSOriginLink = qs_getQueryStringValue('kw');
	if (strQSOriginLink != null)
	{
		//Store status in a cookie so that this can be passed to MacDonalds so they can see where their users are coming from
		WM_setCookie('wbsReferrerKeyword', strQSOriginLink, 720, '/');
	}
}


//Created: Gavin Cummings 01 July 2005
//Purpose: passes any keyword cookies to the booking form function so that they can be sent to MacDonalds
//Remarks: called from macdnav_writeBookingFormHtml() and macdnav_writeWBSBookingFormHtml()
//Returns: the cookie given by the referring site (or null)
function macdnav_getKeyWord()
{
	var strQSOriginLink;

	//Put the cookie into a variable to pass to MacDonalds
	strCookieOriginLink = WM_readCookie('wbsReferrerKeyword');

	//Pass the cookie back to the booking form function so that the referrer code can be passed to MacDonalds

	if (!strCookieOriginLink)
	{
	return null;
	}
	else
	{
		return strCookieOriginLink;
	}
}


function macdnav_buildDate(iYear, iMonth, iDay) {
	// pass iMonth as 0=Jan, 1=Feb, etc
	var month31  = [0,2,4,6,7,9,11];
	var month30  = [3,5,8,10];
	var yearLeap = [2004,2008,2012,2016];

	var bErr = false

	// is it Feb?
	if (iMonth == 1) {
		bLeapYear = false;
		for (i = 0; i < yearLeap.length; i ++) {
			if (iYear == yearLeap[i]) bLeapYear = true;
		}
		if (bLeapYear && iDay > 29) {
			bErr = true;
			thisForm.arrDay.selectedIndex = 29;
		}
		if (!bLeapYear && iDay > 28) {
			bErr = true;
		}
	}
	else {
		b30DayMonth = false;
		for (i = 0; i < month30.length; i ++) {
			if (iMonth == month30[i]) b30DayMonth = true
		}
		if (b30DayMonth && iDay > 30) {
			bErr = true;
		}
	}

	return !bErr;
}


//Created: 22/07/2004, BenSmawfield
//Purpose: To ascertain if the selected year is a leap year
//Input: The selected year (integer)
//Returns: Whether or not the year is a leap year (Boolean)
function macdnav_leapYear(year)
{
	var yearLeap = [2004,2008,2012,2016];

	bLeapYear = false;
	for (i = 0; i < yearLeap.length; i ++) {
		if (year+2004 == yearLeap[i])
		{
			bLeapYear = true;
		}
	}
	return bLeapYear;
}


//Created: 22/07/2004, BenSmawfield
//Purpose: To ascertain if the selected month is a 30 day month
//Input: The selected month (integer)
//Returns: Whether or not the selected month is a 30 day month (Boolean)
function macdnav_shortMonth(month)
{
	var month30  = [3,5,8,10];

	b30DayMonth = false;
	for (i = 0; i < month30.length; i ++) {
		if (month == month30[i])
		{
			b30DayMonth = true;
		}
	}
	return b30DayMonth;
}


//Created: 21/07/2004, BenSmawfield
//Purpose: To make sure the user cannot enter invalid dates into the booking form.  If a day greater than the number of days
//in the month is entered, the day value will automatically be set to the max possible in that month.
//Imput: Selected dates from values from dropdown menus
function macdnav_adjustDates(thisForm)
{
	// If it is February check to see if it is a leap year
	if (thisForm.arrMonth.selectedIndex == 1) {
		bLeapYear = macdnav_leapYear(thisForm.arrYear.selectedIndex);
		if (bLeapYear && thisForm.arrDay.selectedIndex+1 > 29) {
			thisForm.arrDay.selectedIndex = 28;
		}
		if (!bLeapYear && thisForm.arrDay.selectedIndex+1 > 28) {
			thisForm.arrDay.selectedIndex = 27;
		}
	}
	else {
		//Check to see if it is a 30 day month
		b30DayMonth = macdnav_shortMonth(thisForm.arrMonth.selectedIndex);
		if (b30DayMonth && thisForm.arrDay.selectedIndex+1 > 30) {
			thisForm.arrDay.selectedIndex = 29;
		}
	}
}


//Created: 22/07/2004, BenSmawfield
//Purpose: To make sure the user cannot enter invalid dates into the booking form.  If a day greater than the number of days
//in the month is entered, the day value will automatically be set to the max possible in that month.
//Imput: Selected dates from values from dropdown menus
function macdnav_adjustDepDates(thisForm)
{
	// If it is February check to see if it is a leap year
	if (thisForm.depMonth.selectedIndex == 1) {
		bLeapYear = macdnav_leapYear(thisForm.depYear.selectedIndex);
		if (bLeapYear && thisForm.depDay.selectedIndex+1 > 29) {
			thisForm.depDay.selectedIndex = 28;
		}
		if (!bLeapYear && thisForm.depDay.selectedIndex+1 > 28) {
			thisForm.depDay.selectedIndex = 27;
		}
	}
	else {
		//Check to see if it is a 30 day month
		b30DayMonth = macdnav_shortMonth(thisForm.arrMonth.selectedIndex);
		if (b30DayMonth && thisForm.depDay.selectedIndex+1 > 30) {
			thisForm.depDay.selectedIndex = 29;
		}
	}
}


//Created: 22/07/2004, BenSmawfield
//Purpose: If the user enters a departure date that is earlier than the arrival date, the arrival date will be adjusted to the day
//before the selected departure date
//Remarks: not currently in use - in future make it so that this function will set an error message when the user presses check availability with these invalid conditions
//Imput: Selected dates from values from dropdown menus
function macdnav_setArr(thisForm) {

/*	//Make sure the arrival date is before the departure date
	macdnav_adjustDepDates(thisForm);

	var lDay      = 1000 * 60 * 60 * 24		// number of milliseconds in a day
	var dArrival   = new Date(thisForm.arrYear.selectedIndex+2004, thisForm.arrMonth.selectedIndex, thisForm.arrDay.selectedIndex+1, 0, 0, 0, 0);
	var dDeparture = new Date(thisForm.depYear.selectedIndex+2004, thisForm.depMonth.selectedIndex, thisForm.depDay.selectedIndex+1, 0, 0, 0, 0);

	//If the arrival date is after the departure date
	if (dDeparture < dArrival)
	{
		var dPrevDay   = new Date(dDeparture.valueOf() - lDay);

		//Set the arrival date to the day before the selected arrival date
		thisForm.arrDay.selectedIndex   = dPrevDay.getDate() - 1;
		thisForm.arrMonth.selectedIndex = dPrevDay.getMonth();
		thisForm.arrYear.selectedIndex  = dPrevDay.getFullYear() - 2004;
	}*/
}


function macdnav_setDep(thisForm) {

	//Make sure the departure date is after the arrival date
	macdnav_adjustDates(thisForm);
	var lDay      = 1000 * 60 * 60 * 24		// number of milliseconds in a day
	if (macdnav_buildDate(thisForm.arrYear.selectedIndex+2004, thisForm.arrMonth.selectedIndex, thisForm.arrDay.selectedIndex+1)) {
		var dArrival   = new Date(thisForm.arrYear.selectedIndex+2004, thisForm.arrMonth.selectedIndex, thisForm.arrDay.selectedIndex+1, 0, 0, 0, 0);
		var dNextDay   = new Date(dArrival.valueOf() + lDay);
		thisForm.depDay.selectedIndex   = dNextDay.getDate() - 1;
		thisForm.depMonth.selectedIndex = dNextDay.getMonth();
		thisForm.depYear.selectedIndex  = dNextDay.getFullYear() - 2004;
	}
}


// Class: PageQueryString
// Purpose: Allows simple access to key-value pairs stored in querystring
// Input: Value of querystring
function qs_PageQuery(qs)
{
	if(qs.length > 1) this.strQS = qs.substring(1, qs.length);
	else this.strQS = null;
	this.keyValuePairs = new Array();
	if(this.strQS)
	{
		for (var i=0; i < this.strQS.split("&").length; i++)
		{
			this.keyValuePairs[i] = this.strQS.split("&")[i];
		}
	}
	
	// Purpose: Getter
	this.getKeyValuePairs = function()
	{
		return this.keyValuePairs;
	}
	
	// Purpose: Returns value from querystring
	// Input: key (string)
	this.getValue = function(key) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) {
		if(this.keyValuePairs[j].split("=")[0] == key)
			return unescape(this.keyValuePairs[j].split("=")[1]);
		}
		return null;
	}
	
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() 
	{
		return this.keyValuePairs.length; 
	} 
}

// Purpose: Shortcut function to returns value stored in querystring
// Remarks: qs_PageQuery object more efficient if checking multiple values
function qs_getQueryStringValue(key)
{
	var page = new qs_PageQuery(window.location.search); 
	return page.getValue(key); 
}



// Track affiliate code
macdnav_trackAffiliateCode();
macdnav_trackKeyword();