
/**
 * ******************** UNCOMPRESSED VERSION ********************
 *
 * THIS IS AN UNCOMPRESSED VERSION OF THE optionSelector.js FILE
 * THIS FILE SHOULD BE ONLY USED FOR AMMENDMENTS & SHOULD NOT
 * BE USED AS THE VERSION SERVED TO USER AGENTS
 *
 * ONCE ALL CHANGES ARE COMPLETED THIS FILE MUST BE COMPRESSED 
 * USING THE javascriptCompress.cfm UTILITY - TO SAVE AS THE 
 * SERVED VERSION - optionSelector.js
 *
 * ******************** UNCOMPRESSED VERSION ********************
 */
 

function optionSelector (formID, nightSelect) {	

	// define methods
	this.getCookieData = getCookieData;
	this.setCookieData = setCookieData;
	this.saveValue = saveValue;
	this.selectOptions = selectOptions;
	this.isLondonSelected = isLondonSelected;	
	this.starsSelected = starsSelected;
	
	this.isAttached = true;
	if(isUndefined(document.forms[formID])) { this.isAttached = false; return false; } /* quit if we can't get the form */
	
	// get the form elements
	this.theForm = document.forms[formID];	
	this.fStartDay = this.theForm['startDay'];
	this.fStartMonth = this.theForm['startMonth'];
	this.fStartYear = this.theForm['startYear'];	

	//default the flag for nights, adults... fields to true
	if(isUndefined(nightSelect))this.nightSelect = true;

	if(this.nightSelect){
		this.fNights = this.theForm['nights'];
		this.fAdults = this.theForm['adults'];
		this.fChildren = this.theForm['children'];
		this.fInfants = this.theForm['infants'];
	}
	
	this.starRanges = new Array();
	// see if we can pickup the starsRange fields
	var els = this.theForm.elements;
	for(var i = 0; i < els.length; i++) {
		if(els[i].name == 'starsRange') this.starRanges[this.starRanges.length] = els[i];
	}
	
	var today = new Date();
	var tomorrow = today.getDate();
	var month = today.getMonth();
	var year = today.getYear();
	
	var maxDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if((year % 4) == 0) maxDays[1] = 29;
	
	if(tomorrow + 1 == maxDays[month] + 1) { tomorrow = 0; month++; }
	else tomorrow = tomorrow + 1;
	if(month == 12) { month = 0; year++; }
	
	// cookie values and defaults
	this.today = new Date();
	// I know this is stupid (setting + 1 on tomorrow & month) but blame stu...
	this.startDay = this.getCookieData('startDay') ||  tomorrow;
	this.startMonth = this.getCookieData('startMonth') || month + 1;
	this.startYear = this.getCookieData('startYear') || year;
	if(this.nightSelect){
		this.nights = this.getCookieData('nights') || 1;
		this.adults = this.getCookieData('adults') || 2;
		this.children = this.getCookieData('children') || 0;
		this.infants = this.getCookieData('infants') || 0;
	}
	if(this.starRanges.length) this.starsRange = this.getCookieData('starsRange') || '1-5';

	// fake object for NS event handler
	var fakeThis = this;
	
	this._setCookieData = function (e) {
		fakeThis.setCookieData(e);
	};
	this._saveValue = function (e) {
		fakeThis.saveValue(e);
	};
	this._starsSelected = function (e) {
		fakeThis.starsSelected(e);
	};	
	this._isLondonSelected = function (e) {
		fakeThis.isLondonSelected(e);
	};
	
	
	// attatch event listeners
	if(!addEvent(this.theForm, 'submit', this._setCookieData, false)) this.theForm.onsubmit = this._setCookieData;
	// added hack as the startMonth & startYear aren't always there anymore (because of calendar)
	if(!isUndefined(this.fStartMonth) && !isUndefined(this.fStartYear)) {
		if(!addEvent(this.fStartDay, 'change', this._saveValue, false)) this.fStartDay.onchange = this._saveValue;
		if(!addEvent(this.fStartMonth, 'change', this._saveValue, false)) this.fStartMonth.onchange = this._saveValue;
		if(!addEvent(this.fStartYear, 'change', this._saveValue, false)) this.fStartYear.onchange = this._saveValue;
	}
	if(this.nightSelect){
		if(!addEvent(this.fNights, 'change', this._saveValue, false)) this.fNights.onchange = this._saveValue;
		if(!addEvent(this.fAdults, 'change', this._saveValue, false)) this.fAdults.onchange = this._saveValue;
		if(!addEvent(this.fChildren, 'change', this._saveValue, false)) this.fChildren.onchange = this._saveValue;
		if(!addEvent(this.fInfants, 'change', this._saveValue, false)) this.fInfants.onchange = this._saveValue;
	}
	
		
	//var to store if user stars selection made
	this.starsSelectionMade = false;
	
	if(this.starRanges.length) {
		
		for(var i = 0; i < this.starRanges.length; i++) {
			if(!addEvent(this.starRanges[i], 'change', this._saveValue, false)) this.starRanges[i].onchange = this._saveValue;
			
			if(!addEvent(this.starRanges[i], 'change', this._starsSelected, false)) this.starRanges[i].onchange = this._starsSelected;
			
		}
	
		//Change star rating when London selected - attach event-handler to location select
		this.atopLocation = this.theForm['atopLocation'];

		if(!addEvent(this.atopLocation, 'change', this._isLondonSelected , false)) this.atopLocation.onchange = this._isLondonSelected; 
		
	}
}

//check if london has been selected from the atopLocation dropdown
function isLondonSelected(e) {

	//if london selected and no cookie or previous user selection...
	if(this.atopLocation.options[this.atopLocation.selectedIndex].value == 'LO' &&
		!this.getCookieData('starsRange') &&
		!this.starsSelectionMade){

		for(var i = 0; i < this.starRanges.length; i++){
			if(this.starRanges[i].value == '3-4'){
				this.starRanges[i].checked = true;
				//save the starsRange value
				this.starsRange = '3-4';
			}
		}	
	}
}

//stars selected event-handler
function  starsSelected(e) {

	this.starsSelectionMade = true;
}

// select the form options
function selectOptions() {
	
	if(this.isAttached) {
		// added hack as the startMonth & startYear aren't always there anymore (because of calendar)
		if(!isUndefined(this.fStartMonth) && !isUndefined(this.fStartYear)) {
			var yearOpt = 0;
			if(this.today.getYear() + 1 == this.startYear) yearOpt = 1;
			if(this.startDay > 0) this.fStartDay.options[this.startDay - 1].selected = true;
			this.fStartMonth.options[this.startMonth - 1].selected = true;
			this.fStartYear.options[yearOpt].selected = true;
		}
		if(this.nightSelect){
			this.fNights.options[this.nights - 1].selected = true;
			this.fAdults.options[this.adults].selected = true;
			this.fChildren.options[this.children].selected = true;
			this.fInfants.options[this.infants].selected = true;
		}
		if(this.starRanges.length) {
			for(var i = 0; i < this.starRanges.length; i++) {
				if(this.starRanges[i].value == this.starsRange) this.starRanges[i].checked = true;
				else this.starRanges[i].checked = false;
			}
		}
	}
}
// cross-browser function for checking isDefined
function isUndefined(v) {
	var undef;
	return v===undef;
}
// saves a select value to the instance
function saveValue (e) {

	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
	
	var n = targ.name;
	
	if(n == 'starsRange') this[n] = targ.value;
	else this[n] = this.theForm[n].options[this.theForm[n].selectedIndex].value;
}
// set cookie values
function setCookieData (e) {

	// added hack as the startMonth & startYear aren't always there anymore (because of calendar)
	if(!isUndefined(this.fStartMonth) && !isUndefined(this.fStartYear)) {
		document.cookie = 'startDay=' + this.startDay;
		document.cookie = 'startMonth=' + this.startMonth;
		document.cookie = 'startYear=' + this.startYear;
	}
	
	if(this.nightSelect){	
		document.cookie = 'nights=' + this.nights;
		document.cookie = 'adults=' + this.adults;
		document.cookie = 'infants=' + this.infants;
		document.cookie = 'children=' + this.children;
	}
	
	if(this.starRanges.length) document.cookie = 'starsRange=' + this.starsRange;
}

// get cookie data
function getCookieData (name) {
	
	var nameLen = name.length;
	var cLen = document.cookie.length;
	var i = 0;
	var cEnd;
	while (i < cLen) {
		var j = i + nameLen;
		if (document.cookie.substring(i,j) == name) {
			cEnd = document.cookie.indexOf(";", j);
			if (cEnd == -1) cEnd = document.cookie.length;
			return unescape(document.cookie.substring(j+1, cEnd));
		}
		i++;
	}

	return null;
}