﻿/*
 Royal Garden Hotel
 4th March 2008
 Matthew Sach
 
 Used to identify the referring page, and select the appropriate campaign for booking links.
 This affects the standard quickbook form, and provides a mechanism for in-content links to
 be sent to the correct location.
 
 iHotelier URLs require a ReferID to specify the campaign they are for. This can be added to
 a URL which already includes a ProdID, so that eg. the Special Promotions can be booked
 independantly, or due to the customer arriving on the back of a particular campaign.
 
 */
RGH.Booking = {

	referrerList: {
		'johnhenry_net': 999999,
		'hotelrooms_com': 14450,
		'www_hotelrooms_com': 14450,
		'google_com': 15587,
		'www_google_com': 15587,
		'bestloved_com': 15702,
		'www_bestloved_com': 15702
	},
	
	reservationLink: 'http://reservations.ihotelier.com/istay.cfm?HotelID=6956&LanguageID=1',
	productLink: 'http://reservations.ihotelier.com/crs/p_prod.cfm?ProdID=###&HotelID=6956&killcookie=1&LanguageID=1',
	
	captureReferrer: function()
	{
		var referringDomain;
		if (!RGH.readCookie('rgh.booking.referrer')) 
		{
			if (document.referrer && document.referrer !== '') 
			{
				// process the referrer to get the domain name
				referringDomain = document.referrer.split('//')[1].split('/')[0];
			}
			else
			{
				// IE loses referrer info on window.open(), so check the opener for the info instead
				if (window.opener && window.opener.location)
				{
				// process the opener.location to get the domain name
					referringDomain = window.opener.location.split('//')[1].split('/')[0];
				}
			}

			if (referringDomain)
			{
				RGH.createCookie('rgh.booking.referrer', referringDomain.replace(/\./g, '_'));
			}
		}
	},
	
	quickBook: function()
	{
		var referringDomain = RGH.readCookie('rgh.booking.referrer');
		if (referringDomain) 
		{
			while (this.referrerList[referringDomain] === undefined && referringDomain.length > 0) 
			{
				referringDomain = referringDomain.substr(1);
			}
			
			if (referringDomain.length > 0) 
			{
				if (document.getElementById('referid')) 
				{
					var val = this.referrerList[referringDomain];
					document.getElementById('referid').value = val;
				}
			}
		}
	},
	
	baseBooking: function(url)
	{
		var referringDomain = RGH.readCookie('rgh.booking.referrer');
		if (referringDomain) 
		{
			while (this.referrerList[referringDomain] === undefined && referringDomain.length > 0) 
			{
				referringDomain = referringDomain.substr(1);
			}
			if (referringDomain.length > 0) 
			{
				url += '&ReferID=' + this.referrerList[referringDomain];
			}
		}
		window.open(url, 'reservation');
	},
	
	standardBooking: function()
	{
		this.baseBooking(this.reservationLink);
	},
	
	productBooking: function(productId)
	{
		var finalURL = this.productLink.replace(/###/, productId);
		this.baseBooking(finalURL);
	},
	
	
	dummy: {}
};

registerEventNode('load', window, RGH.Booking.captureReferrer);
//$(document).ready(RGH.Booking.captureReferrer);
