<!--


/*

SCRIPT NAME: alpharooms-booking-form-js-control.js

VERSION:     1.0.2

PURPOSE:     Various control functions relating to the booking form on hotel pages

DETAILS:     This script contains various functions to control the Alpharooms booking form
             which appears on a lot of the hotel pages (and maybe others in the future).
             Some of the functions are Alpharooms written, and some are TDP written (majority).


INSTALL:   Before installing, there are two slightly different versions of the HTML code depending
           whether we have an Alpharooms hotel affiliate ID (usually on an Alpharooms page), or
           whether we are looking to search a town instead.

           On Alpharooms pages there won't be a "destinations" dropdown box, and there'll be an
           Alpharooms ID tag in the form. For all other pages, use the version with "destinations".

           The installation instructions below include details for both types of installation hence
           the length, but in essence there are just 4 stages to install this.



           • Copy the booking form from another page including the reference to this script & CSS

             • Usually it'll be marked with comment tags, so just copy the entire thing & paste
               • The CSS goes above the form code usually at the top of the table
               • The J/script must go BELOW the form to prevent errors



           • Paste the code into the site where the booking form is to appear

             • If using on an Alpharooms hotel page:
               • Make sure the entry output script is at least v1.0.12
               • Everything else should be pretty much automatic


             • If using on a non-Alpharooms page:
               • The form contains a destinations box which is country specific
               • The content comes from the Alpharooms XML Destinations Excel file.
               • For a new country, the value is the Alpharooms ID and the text is the town as per the file
                 • For the UK see the UKhotel version which will work for all UK sites



           • Just above where we call this script with the <javascript src= line, we need to pass some settings.

             • If using this on an Alpharooms page then we can pass two vars.
               • Lookahead sets how many days ahead the form will be set as the booking start date (optional)
               • Town override tells the script we're using an Alpharooms ID and to ignore towns stuff (req.)

               var alr_master_lookahead_value  = 3; // Defaults to 5 if not set
               var alr_master_town_overide	   = 1; // Must be set to prevent errors


             • If using this on a non-Alpharooms page we can use the following vars.
               • Lookahead sets how many days ahead the form will be set as the booking start date (optional)
               • Master town name is the name of the town for this entry (recommended)


                var alr_master_lookahead_value  = 3; // Defaults to 5 if not set
                var alr_master_town_name 		= 'Town Name';


             • An example of what this might look like with all:

               <script language="javascript">
               <!--
                 var alr_master_town_overide	= 1; 			// Alpharooms hotel pages only!
                 var alr_master_town_name 		= '{TNAME}';  	// Non-alpharooms pages
                 var alr_master_lookahead_value = 3;			// Any page type
               // -->
               </script>



           • Finally, upload:
             • This script to root
             • The CSS script to root
             • The page/template(s) containing the new HTML code



           • Misc details:

             • Townname can be passed as normal and will be processed by the Javascript
             • If townname not passed then the destination box will return to "Please select..."




FUNCTIONS:

          • AlrSetInitialDate():   Sets the current date in the form on page/script load.

          • AlrSelectMasterTown(): Tries to match the entry town supplied in alr_master_town_name
                                   variable with a town in the dropdown list and make it SELECTED
                                   if possible. Else it leaves it alone.

          • AlrUpdateDateBoxes(): An internal function to update the number of days in the month
                                  depending on the month selected. Called on page load via the
                                  AlrSetInitialDate() function and on month change.

          • AlrCheckForm():		  An internal function that runs quick checks on the form such as
                                  whether a destination has been filled in.




HISTORY:

         v1.0.1: Updated the date handling code so the boxes are all filled in via Javascript
                 plus a few other tweaks.

                 • Blocks the user from submitting if they haven't selected a destination
                 • Updates the number of days in the month automatically on each month change
                 • Handles errors such as going from 31st July -> June, now sets as 30th June
                 • Year is auto incremented based on users PC at least so no updates required


         v1.0.2: Added an external setting for the lookahead value which states how many days
                 ahead to set the booking form.

                 Also added a new variable to override looking for town name matches for when
                 we're looking up a hotel ID instead of a broader search. This basically turns
                 off the AlrSelectMasterTown() function call and a check in AlrCheckForm().



FUTURE:

       • Plan to make this AJAX based so we can make a dbase lookup table to find nearby towns.
         Eg: if an entry is in Town A, but only nearby Town B is in Alpharooms, then Town B would show

       • Maybe hook the system up to the Alpharooms XML-RPC system so we can do some of the booking
         process ourselves, especially good when they don't have any entries but we can find nearby ones.


*/







// SETUP DAYS IN EACH MONTH ARRAY //

var alr_months_days_array = new Array();
alr_months_days_array[0] = 0;
alr_months_days_array[1] = 31;
alr_months_days_array[2] = 28;
alr_months_days_array[3] = 31;
alr_months_days_array[4] = 30;
alr_months_days_array[5] = 31;
alr_months_days_array[6] = 30;
alr_months_days_array[7] = 31;
alr_months_days_array[8] = 31;
alr_months_days_array[9] = 30;
alr_months_days_array[10] = 31;
alr_months_days_array[11] = 30;
alr_months_days_array[12] = 31;





// SETUP MONTH NAME ARRAY //

var alr_months_names_array = new Array();
alr_months_names_array[0] = '';
alr_months_names_array[1] = 'Jan';
alr_months_names_array[2] = 'Feb';
alr_months_names_array[3] = 'March';
alr_months_names_array[4] = 'April';
alr_months_names_array[5] = 'May';
alr_months_names_array[6] = 'June';
alr_months_names_array[7] = 'July';
alr_months_names_array[8] = 'Aug';
alr_months_names_array[9] = 'Sept';
alr_months_names_array[10] = 'Oct';
alr_months_names_array[11] = 'Nov';
alr_months_names_array[12] = 'Dec';





// Make sure external variables are set as they may not be included //

if (typeof(window['alr_master_town_overide']) == "undefined" ) {
	var alr_master_town_overide = 0;
}

if (typeof(window['alr_master_town_name']) == "undefined" ) {
	var alr_master_town_name = '';
}








// Function to set the date values and make it 5 days ahead
//
// Auto adjusts if X days ahead means we go into the next month or even year
// Uses the AlrUpdateDateBoxes() function to fillout the figures in the forms initially
// Responsible for selecting todays date (+5 days) in the form
//
// To adjust the lookahead, add "var alr_lookahead_value=5" or whatever to some J/script
// before this script is called or the default below will be used.

function AlrSetInitialDate() {



	// Not sure why, but checking for isNaN doesn't work here

	if (typeof(window['alr_master_lookahead_value']) == "undefined" ) {
		var LookaheadDays = 5;
	}
	else {
		var LookaheadDays = alr_master_lookahead_value;
	}



	var TodayDate 	= new Date();
	var ThisDay 	= TodayDate.getDate()  + LookaheadDays;
	var ThisMonth 	= TodayDate.getMonth() + 1; // Months go 0 - 11
	var ThisYear  	= TodayDate.getYear();



	// Make sure date is valid when looking 5 days ahead

	if (ThisDay > alr_months_days_array[ThisMonth]) {


		var DaysLeftInMonth = ThisDay - alr_months_days_array[ThisMonth];
		var DaysNextMonth   = LookaheadDays - DaysLeftInMonth;


		ThisDay   = DaysNextMonth + 1;
		ThisMonth = ThisMonth + 1;

		if (ThisMonth > 12) {

			ThisMonth = 1;
			ThisYear  = ThisYear + 1;
			var yearInc = 1;

		}

	}




	// Update the boxes to show the correct days & year
	// Pass it this month since we don't have anything selected yet

	AlrUpdateDateBoxes(ThisMonth);



	document.AlphaBedsSearch.FromDateDay.selectedIndex 		= ThisDay   - 1;
	document.AlphaBedsSearch.FromDateMonth.selectedIndex 	= ThisMonth - 1;
	document.AlphaBedsSearch.FromDate.selectedIndex 		= (yearInc == 1) ? 1 : 0;


	return true;


}






// Fill in the dropdown boxes with the appropriate values
//
// Days are set according to the month, eg: June -> 30 days listed
// Month changes call this function to update days list
// Years are set usually 2 years ahead (see ThisYearMax to change)
//
// Auto adjusts for switching to month with fewer days, eg: going from 31st July -> June = 30th June selected

function AlrUpdateDateBoxes(MonthSelected) {



	// Current day selected in the form
	DaySelected = document.AlphaBedsSearch.FromDateDay.options[document.AlphaBedsSearch.FromDateDay.selectedIndex].value;

	if (DaySelected > alr_months_days_array[MonthSelected]) {
		DaySelected = alr_months_days_array[MonthSelected];
	}





	var TodayDate 	= new Date();
	var ThisYear  	= TodayDate.getFullYear();
	var ThisYearMax = ThisYear + 2;




	// Reset all boxes to contain nothing

	document.AlphaBedsSearch.FromDateDay.length 	= 0;
	document.AlphaBedsSearch.FromDateMonth.length 	= 0;
	document.AlphaBedsSearch.FromDate.length		= 0;




	for (cdays=0; cdays < alr_months_days_array[MonthSelected]; cdays++) {

		NewDayOpt	 	= new Option;
		NewDayOpt.value = cdays + 1;
		NewDayOpt.text  = cdays + 1;

		document.AlphaBedsSearch.FromDateDay.options[cdays] = NewDayOpt;

	}

	if (DaySelected > 1) {

		// Only use previously selectedDay if not loading page as it won't be set yet
		document.AlphaBedsSearch.FromDateDay.selectedIndex = DaySelected - 1;

	}





	for (cmonth=0; cmonth < 12; cmonth++) {

		NewMonthOpt	 	  = new Option;
		NewMonthOpt.value = cmonth + 1;
		NewMonthOpt.text  = alr_months_names_array[cmonth + 1];

		document.AlphaBedsSearch.FromDateMonth.options[cmonth] = NewMonthOpt;

	}

	document.AlphaBedsSearch.FromDateMonth.selectedIndex = MonthSelected - 1;





	var cyear_cnt = 0;

	for (cyear=ThisYear; cyear < ThisYearMax; cyear++) {

		NewYearOpt	 	 = new Option;
		NewYearOpt.value = cyear;
		NewYearOpt.text  = cyear;

		document.AlphaBedsSearch.FromDate.options[cyear_cnt] = NewYearOpt;

		cyear_cnt++;

	}



	return true;


}








// Function to try to select the most appropriate town from destinations
//
// This takes the current town name usually from the UKSeries system
// The input name is lowercased and has all non-alphanumerics removed
// We use the HTML options list names to find the right town name in the form (no programming req. to update)
// The options are also normalised to hopefully remove quirks in town names
//
// If a matching name can't be found, the script simply lets the "Please select" option appear


function AlrSelectMasterTown() {



	// Setup the variables

	var list_towns_len 		= document.AlphaBedsSearch.DestinationID.options.length;
	var list_towns_array	= new Array();
	var list_towns_tmp		= '';




	// Process the master town name (where this entry is)

	alr_master_town_name = alr_master_town_name.toLowerCase();
	alr_master_town_name = alr_master_town_name.replace(/[^a-z0-9]/g,'');



	// Find all the towns in the list and check against master town name
	// If one matches, make that the selected option

	for (a=0; a < list_towns_len; a++) {

		list_towns_tmp = document.AlphaBedsSearch.DestinationID.options[a].text;
		list_towns_tmp = list_towns_tmp.toLowerCase();
		list_towns_tmp = list_towns_tmp.replace(/[^a-z0-9]/g,'');

		if (list_towns_tmp == alr_master_town_name) {

			document.AlphaBedsSearch.DestinationID.options[a].selected = true;

		}

	}

}





// Function to run basic pre-submit checks on the form
//
// Currently the Alpharooms system doesn't jump to the front page if no dest. is selected
// so we have to block people from doing that until they fix it.
//
// If we're passing an Alpharooms hotel ID instead of destination then skip that check

function AlrCheckForm() {



	// CHECK FOR NO DESTINATION SELECTED //

	if (alr_master_town_overide != 1) {

		if (document.AlphaBedsSearch.DestinationID.options[0].selected == true) {

			alert("Please select a destination to search");
			return false;

		}

	}

	return true;


}







// RUN THE FUNCTIONS //

// Alpharooms function to make the date work (if users computer is right)
AlrSetInitialDate();


// Try to select the town from the list
// If this is an Alpharooms page, then skip this as we're using the hotel ID

if (alr_master_town_overide != 1) {
	AlrSelectMasterTown();
}





//-->
