// =============================================================================
// JavaScript Document
//
// Author:	Anonymous
// script:	QuickReserve.js
// Version: 2.9
//
// note:	XML file rel. 1.2
//
// Changes:
//  - (04/10/2008) 1.0	Added:	Multi language support
//  - (24/10/2008) 1.0	Fixed: 	Same year next month gave old date msg
//  - (31/10/2008) 1.0	Added:	date check for 30/31 and bissextile
//  - (12/11/2008) 1.0	Added:	More languages
//  - (16/02/2010) 2.0	Added:	UTF-8
//	- (11/10/2010) 2.1	Fixed:	TRACKING - call to function in onsubmit 
//	- (18/10/2010) 2.2	Added:	Language Multidomain URL support
//						Added:	GA independent tracking and event support
//						Added:	GA Asynchronous script support    
//	- (04/11/2011) 2.3	Added:	Generic channel search (no hotel List)
//						Added:	addHotel(...) to add dynamically hotels	
//	- (06/11/2010) 2.4	Fixed:	GA on submit doesn't need return false
//  - (07/11/2010) 2.5  Added:  Departure Date sync with nights
//	- (08/11/2010) 2.6	Added:	Check: the date cannot be less today
//	- (13/12/2010) 2.7	Added:	Rooms distributions and children age request
//	- (05/12/2011) 2.8	Fixed:	Nights preset with value greater than 1 doesn't update departure date
//	- (25/01/2011) 2.9	Fixed:	Nights preset with value equal to date gap update nights
//
//  TO DO LIST:
//      - Add:  Function to dynamically change arrival date, nights, Product (must became obj) 
//		- Add:	use an image as a submit button (normal image and on hover image)
//		- Add:	Transform everything as an object to permit more than one instance  
// =============================================================================

// CONSTANT
var NIGHTS			= 0;			// used for set depdate in case of nights selection
var START 			= 1; 			// used for arrival date
var END				= 2;			// used for departure date

var nightsUsed		= false;

var sLang			= new Array("eng","ita","fra","deu","esp","rus");
var cLang			= 0;							// Set default language

var oHotels			= new Object();					// Contains hotel code name location
var oLoc			= new Object();					// Unique location list
var oMonth			= new Object();					// Months Translations

var sMsgOldDate		= "";							// "Warning: the date is in the paste."
var sMsgWrongDate	= "";							// "Warning: the date is in wrong."
var sMsgNumAdult	= "";							// "at least one adult is needed to continue"
var sMsgNumNights	= "";							// "input the number of nights to continue"
var sMsgChildAge	= "";							// "input children age to continue"
var sMsgOccRoom		= "";							// "insert at least one adult per room to continue"
var sBookNow		= "";							// "Quick Reserve"
var sHotel			= "";			 				// "Hotel"
var sAllHotels		= "";							// "All the hotels"
var sArrival		= "";							// "Arrival Date"
var sDeparture		= "";							// "Departure Date"
var sNights			= "";							// "Nights"
var sAdults			= "";							// "Adults"
var sChild			= "";							// "Children"
var sRooms			= "";							// "Rooms"
var sRoom			= "";							// "Room"
var sAge			= "";							// "Children's ages"
var sCodCli			= "";							// "Customer code"
var sImageUrl		= "";							// "images/quick_reserve/prenota_ing.png"
var sPromo			= "";							// "Special code"
var sButton			= "";							// "Reserve"
var sAllLocations	= "";							// "All the locations"
var sDelMod			= "";							// "Cancel/Modify reservation"

var sActionURL		= "";							// URL per submit action in form
var	nIdStile		= "";							// "Template style"
var	nNightMax		= "";							// "MAX Number of  Nights in preselect list"
var	nAdultMax		= "";							// "MAX Number of  Adults in preselect list"
var	nChildMax		= "";							// "MAX Number of  Children in preselect list"
var	nRoomMax		= "";							// "MAX Number of  Rooms in preselect list"
var	nNightSel		= "";							// "Preselect Nights"
var	nAdultSel		= "";							// "Preselect Adults"
var	nChildSel		= "";							// "Preselect Children"
var	nAgeMax			= "";							// "Max Children Age"
var	nRoomSel		= "";							// "Preselect Rooms"
var	sPromoSel		= "";							// "Preselect Special code"
var	sCodCliSel		= "";							// "Preselect Customer code"var	nGroupID
var	nIdCanale		= "";							// "Vertical Channel ID"
var	nIdGruppo		= "";							// "Vertical Channel ID"
var nIdProdSel		= "";							// "Search only in specific product"
var bGAAsync		= false;						// "Eanble GA Asynchronous false=NO true=YES"
var bGATrack		= false;						// "Eanble GA tracking 0=NO 1=YES"
var bGAEvent		= false;						// "Enagle GA Event Tracking"
var sGAEventCat		= "";							// "Define Event Tracking Category"
var sGAEventSubCat	= "";							// "Define Event Tracking SubCategory"
var sGAEventDesc	= "";							// "Description of the event"
var bTestPrn		= false;						// "Test flag "



// Date
var wData	= new Date();
var gg1	    = wData.getDate();
var mm1	    = wData.getMonth();
var aa1	    = wData.getFullYear();

//correzione necessaria per firefox
// if (aa1 < 199) {var  aaaa=1900+aa1; }else // getFullYear fix it
var aaaa	= aa1;														//per gli altri browser diversi da firefox
/**
 * Initialize parameters and translations
 * 
 * @param parameterFile
 * @param sLang
 */
function InitData( parameterFile, sLang) {
	// Load XML in to array
	try {
		var oXML 	= xml2array(caricaXMLSync(parameterFile));
		var oParam  = oXML.Contents.Parameter;
	
		if (oXML) {
			// Load Hotels information
			// sHotels = oParam.Contents.Hotel;
			// extract locations information
			oLoc 	= new Object();     // just in case recall InitData();
			oHotels = new Object();

			i=0;

			if (oXML.Contents.Hotel) {
				for (var key in oXML.Contents.Hotel) {
					oLoc[oXML.Contents.Hotel[key].location] = 1;		// extract locations from Hotels definition
					oHotels[key] = oXML.Contents.Hotel[key];
					i++;
				}
			
				if(oXML.Contents.Hotel[0]) {
					// Hotels group or Channel with hotels list
					nHotelsInList	= i;		// number of hotels in hotels list  - obj has not the lenght
				} else {
					// Single Hotel 
	                nHotelsInList	= 1;
				}
			} else {
				// Hotels group or Channel without hotels list
				nHotelsInList = 0;
			}
			// Translated text
			oMonth			= oXML.Contents.Translation[sLang].month;
			sMsgOldDate		= oXML.Contents.Translation[sLang].sMsgOldDate;
			sMsgWrongDate	= oXML.Contents.Translation[sLang].sMsgWrongDate;
			sMsgNumAdult	= oXML.Contents.Translation[sLang].sMsgNumAdult;
			sMsgNumNights	= oXML.Contents.Translation[sLang].sMsgNumNights;
			sMsgChildAge	= oXML.Contents.Translation[sLang].sMsgChildAge;
			sMsgOccRoom		= oXML.Contents.Translation[sLang].sMsgOccRoom;
			sBookNow		= oXML.Contents.Translation[sLang].sBookNow;
			sHotel			= oXML.Contents.Translation[sLang].sHotel;
			sAllHotels		= oXML.Contents.Translation[sLang].sAllHotels;
			sArrival		= oXML.Contents.Translation[sLang].sArrival;
			sDeparture		= oXML.Contents.Translation[sLang].sDeparture;
			sNights			= oXML.Contents.Translation[sLang].sNights;
			sAdults			= oXML.Contents.Translation[sLang].sAdults;
			sChild			= oXML.Contents.Translation[sLang].sChild;
			sRooms			= oXML.Contents.Translation[sLang].sRooms;
			sRoom			= oXML.Contents.Translation[sLang].sRoom;
			sAge			= oXML.Contents.Translation[sLang].sAge;
			sCodCli			= oXML.Contents.Translation[sLang].sCodCli;
			sImageUrl		= oXML.Contents.Translation[sLang].sImageUrl;
			sPromo			= oXML.Contents.Translation[sLang].sPromo;
			sButton			= oXML.Contents.Translation[sLang].sButton;
			sAllLocations	= oXML.Contents.Translation[sLang].sAllLocations;
			sLocLabel		= oXML.Contents.Translation[sLang].sLocLabel;
			sDelMod			= oXML.Contents.Translation[sLang].sDelMod;
	
			// Parameters
			sActionURL		= oParam.actionURL[sLang];
			nIdStile		= getParam( oParam, "nIdStile"		, "");
			nNightMax		= getParam( oParam, "nNightMax"		, 4);
			nAdultMax		= getParam( oParam, "nAdultMax"		, 4);
			nChildMax		= getParam( oParam, "nChildMax"		, 0);
			nRoomMax		= getParam( oParam, "nRoomMax"		, 4);
			nNightSel		= getParam( oParam, "nNightSel"		, 1);
			nAdultSel		= getParam( oParam, "nAdultSel" 	, 1);
			nChildSel		= getParam( oParam, "nChildSel" 	, 0);
			nAgeMax			= getParam( oParam, "nAgeMax"		, 10);
			nRoomSel		= getParam( oParam, "nRoomSel"		, 1);
			sCodCliSel		= getParam( oParam, "sCodCliSel"	, "");
			sPromoSel		= getParam( oParam, "sPromoSel"		, "");
			nIdGruppo		= getParam( oParam, "nIdGruppo"     , "");
			nIdCanale		= getParam( oParam, "nIdCanale"     , "");
			nIdProdSel		= getParam( oParam, "nIdProdSel"	, "");
			bGAAsync		= getParam( oParam, "bGAAsync"		, false);
			bGATrack		= getParam( oParam, "bGATrack"		, false);
			bGAEvent		= getParam( oParam, "bGAEvent"		, false);
			sGAEventCat		= getParam( oParam, "sGAEventCat"	, "");
			sGAEventSubCat	= getParam( oParam, "sGAEventSubCat", "");
			sGAEventDesc	= getParam( oParam, "sGAEventDesc"	, "");
			bTestPrn		= getParam( oParam, "bTestPrn"		, false);
	
		}
	} catch(e) {
		alert("Init Data error: "+e.message);
	}

}

/**
 * add hotel to hotel list for dynamic update
 *  
 * @param code
 * @param name
 * @param location
 * @returns
 */
function addHotel( code, name, location) {
	var wHotel = Array();
	wHotel.code 	= code;
	wHotel.name 	= name;
	wHotel.location	= location;

	switch (nHotelsInList) {
		case 0:
			oHotels = wHotel;
			break;
		case 1:
			wHotel 			= oHotels;
			oHotels 		= new Object;
			oHotels[0]		= wHotel;
			oHotels[nHotelsInList]			= new Object;
			oHotels[nHotelsInList].code 	= code;
			oHotels[nHotelsInList].name 	= name;
			oHotels[nHotelsInList].location	= location;
			// oHotels[nHotelsInList] = wHotel; wron obj is passed by reference
			break;
		default:
			oHotels[nHotelsInList] = wHotel;
			break;
	}
	nHotelsInList++;
	oLoc[location] = 1;
}


/**
 * Convert parameter in the right type or return a default value
 * @param oObj
 * @param sParam
 * @param xDefVal
 * @returns
 */
function getParam( oObj, sParam, xDefVal) {
	var xParam = oObj[sParam];
	try {
		if (xParam) {
		    switch(typeof xDefVal) {
		      case 'number':            // Convert the number to a hexadecimal integer
		        return ( (Number(xParam) != NaN) ? Number(xParam): xDefVal);
		      case 'boolean':           // Convert to TRUE or FALSE, in uppercase
		        return ( (xParam.toLowerCase()=="false") ? false : Boolean( xParam ));
		      case 'string':            // Return the string enclosed in quotes
		      default:                  // Convert any other type in the usual way
		        return xParam;
		    }
		}
		return xDefVal;
	} catch (e) {
		return xDefVal;
	}
}

/**
 * @param frm
 * @returns {Boolean}
 */
function frmcheck(frm) {
	var count=0;
	if (parseInt(frm.yy.value) < aaaa) 	{
		alert (sMsgOldDate);
		frm.yy.focus();
		return false;
	} else {
		if (parseInt(frm.yy.value) == aaaa) {
			if (parseInt(frm.mm.value) < mm1+1) {
				alert (sMsgOldDate);
				frm.mm.focus();
				return false;
			} else {
				if (parseInt(frm.mm.value) == mm1+1) {
					if (parseInt(frm.dd.value) < gg1) {
						alert (sMsgOldDate);
						frm.dd.focus();
						return false;
					}
				}
			}
		}
	}
	// alert(frm.dd.value+"/"+frm.mm.value+"/"+frm.yy.value);
	if(parseInt(frm.dd.value)==31){
		switch(parseInt(frm.mm.value)) {
			case 2:
			case 4:
			case 6:
			case 9:
			case 11:
				alert (sMsgWrongDate);
				return false;
				break;
			default:
				break;
		}
	}
	if(parseInt(frm.mm.value)==2 && parseInt(frm.dd.value)>28){
		if ( (( parseInt(frm.yy.value) % 4 != 0)  ||  (parseInt(frm.yy.value) % 100 == 0))  &&  (parseInt(frm.yy.value) % 400 != 0 )) {
			alert (sMsgWrongDate);
			return false;
		}
	}
	
	var tot_adulti_pren = 0;
	var tot_camere_pren = 0;
	if(frm.rooms.value>1){
		for ( var i = 1; i <= frm.rooms.value; i++) {
			count= parseInt(frm["adults"+i].value)+parseInt(frm["children"+i].value);
			if(count==0){
				alert(sMsgNumAdult);
				frm["adults"+i].focus();
				return false;
			}	
		}
	} else if (parseInt(frm.adults.value) < 1) {
		alert(sMsgNumAdult);
		frm.adults.focus();
		return false;
	}
	if (!(parseInt(frm.nights_1.value) > 0)) {
		alert(sMsgNumNights);
		frm.nights_1.focus();
		return false;
	}
	if ((parseInt(frm.children.value)>0)&&(parseInt(frm.rooms.value)==1)) {
		tutto_ok = 1;
		for (i=1; i<=parseInt(frm.children.value); i++) {
			myvar = "st1bamb" + i;
			eta = parseInt(document.getElementById(myvar).value);
			if (eta < 0) tutto_ok = 0;
		}
		if (tutto_ok==0) {
			alert(sMsgChildAge);
			frm.st1bamb1.focus();
			return false;
		}
	}
	tot_adulti_pren 	= parseInt(frm.adults.value);
	tot_camere_pren 	= parseInt(frm.rooms.value);
	tot_bambini_pren 	= parseInt(frm.children.value);
	tot_occupanti_pren 	= tot_adulti_pren + tot_bambini_pren+count;
	// str = "occ = " + tot_occupanti_pren + " cam = " + tot_camere_pren;
	// alert(str); // debug
	if (tot_occupanti_pren < tot_camere_pren || isNaN(tot_occupanti_pren) )	{
		alert(sMsgOccRoom);
		frm.adults.focus();
		return false;
	}
	if(tot_camere_pren> 1) {
		
	} else {
		
	}
//debugger; // alert(frm.)
}
/**
 * Change number of rooms
 * @param oForm
 */
function addRooms( oForm){
	o = document.getElementById("distrib");
	o.innerHTML = "";
	oForm.adults.disabled=false;
	oForm.children.disabled=false;
	if( typeof(oForm.adults1) !== "undefined" ){
		oForm.adults.value 		= oForm.adults1.value; 
		oForm.children.value	= oForm.children1.value;
	}
	document.getElementById("GuestNumber").setAttribute("style", "display:visible");
	if (oForm.rooms.value > 1 ) {
		for (i=1; i<= oForm.rooms.value; i++){
			var divCont = addTag( o, "div", "roomCont"+i, "roomCont");
			
			addTag(	divCont, "div",	"lblRoom"+i, "roomLabel", sRoom +" "+i);
			
			var div = addTag( divCont, "div", "room"+i, "roomBlock");
			addTag(	div, "div", "lblAdults"+i, "adultsLabel", sAdults);
			
			oSel = document.createElement("select");
			oSel.id 	= "adults"+i;
			oSel.name 	= "adults"+i;
			for (j=0; j< nAdultMax; j++)
				oSel.options[j] = new Option( j, j);

			oSel.options[1].selected = true;
			
			div.appendChild(oSel);
			addChild( oForm, div, i);
			document.getElementById("GuestNumber").setAttribute("style", "display:none");
			oForm.adults.disabled=true;
			oForm.children.disabled=true;
		}
	} 
} 

/**
 * Add Children to distribution form
 * @param div
 * @param div
 * @param nRoom
 */
function addChild( oForm, div, nRoom) {
	addTag( div, "div", "lblChild"+nRoom, "childBlock", sChild);
	
	oSel = document.createElement("select");
	oSel.id 	= "children"+i;
	oSel.name 	= "children"+i;
	oSel.options[0] = new Option( "", 0);
	for (j=1; j<=nChildMax; j++)
		oSel.options[j] = new Option( j, j);
	div.appendChild(oSel);
	var wDiv	= addTag( div, "div", "lblAge"+nRoom, "ageBlock");
	oSel.onchange	= function( ) { addAge( this, wDiv, nRoom );};
}

/**
 * Att HTML Tag
 * @param obj
 * @param type
 * @param id
 * @param sClass
 * @param contents
 * @returns
 */

function addTag( obj, type, id, sClass, contents){
	Div				= document.createElement(type);
	Div.id 			= id;
	Div.className 	= sClass;
	Div 			= obj.appendChild(Div);
	AddContent(id, contents);
	return Div;
}

/**
 * Add Children to distribution form
 * @param div

 */
function addAge( obj, wDiv, nRoom) {
	wDiv.innerHTML = "";
	if (obj.value>0){
		addTag( wDiv, "div", "lblAge"+nRoom, "lblAgeBlock", sAge);
		var wDiv1 = addTag( wDiv, "div", "ageCont"+nRoom, "ageBlock");
		
		for ( var i = 1; i <= obj.value; i++) {
			var wDiv2 = addTag( wDiv1, "div", "ageDiv"+i, "ageDiv");
			oSel = document.createElement("select");
			oSel.id 	= "r"+nRoom+"c"+i;
			oSel.name 	= "r"+nRoom+"c"+i;
			
			oSel.options[0] = new Option( "<0", 0);
			for (j=1; j<=nAgeMax; j++)
				oSel.options[j] = new Option( j, j);
			wDiv2.appendChild(oSel);
		}
	}
}

/**
 * Populate option list with hotels if location is selected
 */
function populateData(oOptions) {
	nIndex= oOptions.options.selectedIndex;

	var name = oOptions.options[nIndex].value;

	var oSelect	=	oForm.hotelid;

	// 0 - mostra solo le nuove opzioni
	// 1 - mostra la prima opzione esistente più le nuove opzioni
	count	= 1;
	// Cancella la vecchia lista (se count = 0)
	oSelect.options.length = count;
	// Inserisce nelle opzioni della lista tutte le categorie corrispondenti
	// oSelect.options[count++] = new Option( sHotels[1][0] );
	
	for (var key in oHotels) {
		if( oHotels[key].location == name || nIndex==0) {
			oSelect.options[count++] = new Option( oHotels[key].name, oHotels[key].code );
		}
	}

	// Imposta quali opzioni della sottocategoria devono essere selezionate
	oSelect.options.selectedIndex = 0;
	// Da il focus alle sottocategorie
	// oSelect.focus();
}

/**
 * Update ID_albergo if changed for cancel/Modify link
 */
function updateCanHref( oForm) {
	try {
		// Add cancel modify reservation link
		obj	= document.getElementById("ModCancLinkText");
		obj.href = sActionURL+"?style="+nIdStile+"&hotelid="+ oForm.hotelid.value +"&channelid="+nIdCanale+"&groupid="+nIdGruppo+"&lang="+sLang[cLang]+"&tstBook="+(bTestPrn? "si":"")+"&cancel=pren";
		// Add cancel modify reservation Label

		if(bGATrack) {
			var fFunc = function() {pageTracker._linkByPost(this);};
			if(bGAEvent) {
				fFunc = function() {pageTracker._linkByPost(this);pageTracker._trackEvent( sGAEventCat,sGAEventSubCat,"CANC"+sGAEventDesc, 0);}; 
			}
			obj.onclick = fFunc;
		}
	} catch(e) {
	}
}

/**
 * Update form fields 
 */
function updateQRForm( oForm) {
	try {
		// Add BookNow Title
		AddContent("bookNow", sBookNow );

		obj	=	oForm.location;

		// Add Place List data only for group or channel
		if(nHotelsInList > 1) {
			i=1;
			obj.options[0] = new Option( sAllLocations, "");
			for (var key in oLoc) {
				obj.options[i] = new Option( key);
				i++;
			}
			if(i > 1) {
				obj.onchange	= function( ) { populateData( this );};
			}
		}
		// Add Places list label
		AddContent("LocListLabel", sLocLabel);

		// Add Hotels list if grou or Channel or hotel id if single hotel
		obj	=	oForm.hotelid;

		if(nHotelsInList > 1) {
			// Add Hotel Label
			AddContent("HotelsListLabel", sHotel);

			// Add Hotel List
			i=1;
			obj.options[0] = new Option( sAllHotels, "");
			for (var key in oHotels ) {
				obj.options[i] = new Option( oHotels[key].name, oHotels[key].code);
				i++;
			}

			obj.onchange	= function( ) {updateCanHref( oForm );};

		} else if (nHotelsInList == 1) {
			// Add Hotel code if is not a group
			if (obj.tagName=="SELECT") {
				obj.options[0] = new Option( oHotels.name,oHotels.code, true, true );
			} else {
				obj.value =  oHotels.code;
				obj.type  = 'hidden';
			}
		}

		// Add Arrival Date Label
		AddContent("ArrivalDateLabel",sArrival);

		// Add List Box for year select
		obj			=	oForm.yy;
		obj.name	=	"yy";
		obj.id		=	"yy";
		obj.size	= 	1;
		obj.onchange = 	function( ) {setDayOfTheMonth( oForm, START);};

		obj.options[0] = new Option( (aaaa.toString()).substr(2),aaaa , true, true);
		obj.options[1] = new Option( ((aaaa+1).toString()).substr(2),aaaa+1 );

		// Add List Box for month select
		obj	=	oForm.mm;

		obj.size  = 1;
		obj.onchange = function( ) {setDayOfTheMonth( oForm, START);};

		for (i=0; i<12; i++)
			obj.options[i] = new Option( oMonth[i], i+1, (mm1==i),  (mm1==i));
			
        // handle days
		obj = 	oForm.dd;

		obj.length = 31;
		for (i=0; i<31; i++)
			obj.options[i] = new Option( i+1, i+1);
		
		obj.value = gg1;
		obj.onchange = function( ) { setDayOfTheMonth( oForm, START);};
		
		// Add List Box for day select

		// setDayOfTheMonth( oForm, START, true);

		// Add Departure Date Label
		AddContent("DepartureDateLabel", sDeparture);

		// Add List Box for year select
		obj			= oForm.eyy;
		obj.name	= "eyy";
		obj.id		= "eyy";
		obj.size	= 1;
		obj.onchange = function( ) { setDayOfTheMonth( oForm, END);};
		obj.options[0] = new Option( (aaaa.toString()).substr(2),aaaa , true, true);
		obj.options[1] = new Option( ((aaaa+1).toString()).substr(2),aaaa+1 );

		// Add List Box for month select
		obj	=	oForm.emm;

		obj.size  = 1;
		obj.onchange = function( ) { setDayOfTheMonth( oForm, END);};

		for (i=0; i<12; i++)
			obj.options[i] = new Option( oMonth[i], i+1, (mm1==i),  (mm1==i));
		
        // handle days
		obj = 	oForm.edd;
		obj.length = 31;
		for (i=0; i<31; i++)
			obj.options[i] = new Option( i+1, i+1);

/*		obj.onchange = function( ) {
			setDayOfTheMonth( oForm, START);
		};
*/		
		obj.onchange = function( ) { setDayOfTheMonth( oForm, END);};


		// Add List Box for day select

		setDayOfTheMonth( oForm, START);

		// Add Nights Label
		AddContent("NightsLabel", sNights);

		// Add number of nights
		obj	=	oForm.nights_1;

		obj.options[0] = new Option( "", 0);
		for (i=1; i<=nNightMax; i++)
			obj.options[i] = new Option( i, i, (nNightSel==i), (nNightSel==i));
		
		obj.onchange = function( ) { setDayOfTheMonth( oForm, NIGHTS);};
		setDayOfTheMonth( oForm, NIGHTS);
		
		// Add Adults Number Label
		AddContent("AdultsLabel", sAdults);

		// Add number of Adults
		obj	=	oForm.adults;

		obj.options[0] = new Option( "", 0);
		for (i=1; i<=nAdultMax; i++)
			obj.options[i] = new Option( i, i, (nAdultSel==i), (nAdultSel==i));

		// Add Children Number Label
		AddContent("ChildLabel", sChild);

		// Add number of Children
		obj	=	oForm.children;

		obj.options[0] = new Option( "", 0);
		for (i=1; i<=nChildMax; i++)
			obj.options[i] = new Option( [i], i, (nChildSel==i), (nChildSel==i));
		
		obj.onchange = function( ) { addAge(this, document.getElementById("ChildrenAge"), 1); };
		
		// Add Rooms Number Label
		AddContent("RoomLabel", sRooms);

		// Add number of Rooms
		obj	=	oForm.rooms;

		obj.options[0] = new Option( "", 0);
		for (i=1; i<=nRoomMax; i++)
			obj.options[i] = new Option( [i], i, (nRoomSel==i), (nRoomSel==i));
		
		obj.onchange = function( ) { addRooms( oForm);};
		addRooms( oForm);
		
		// Add customer code Label
		AddContent("CodeCliLabel", sCodCli);

		// Add Customer code preselect
		obj	=	oForm.customerId;
		obj.value = sCodCliSel;

		// Add Special code Label
		AddContent("PromoLabel", sPromo);

		// Add Special code preselect
		obj	=	oForm.discountId;
		obj.value = sPromoSel;

		// Add Special code preselect
		obj	=	oForm.confirm;
		obj.value = sButton;

		// Add cancel modify reservation link
		updateCanHref( oForm);

		AddContent( "ModCancLinkText", sDelMod);

		// set hidden data
		oForm.lang.value		= sLang[cLang];
		oForm.style.value 		= nIdStile;
		oForm.channelid.value	= nIdCanale;
		oForm.groupid.value		= nIdGruppo;
		oForm.prodid.value		= nIdProdSel;
        oForm.tstBook.value		= bTestPrn;
		oForm.action      		= sActionURL;
		// set tracking parameters
		var fFunc = function() {};
		
		if(bGATrack && bGAEvent) {
			//track and event set
			if(bGAAsync)
				fFunc = function() {_gaq.push(['_trackEvent', sGAEventCat,sGAEventSubCat,sGAEventDesc, 0]);_gaq.push(['_linkByPost', this]);}; 
			else
				fFunc = function() {pageTracker._trackEvent( sGAEventCat,sGAEventSubCat,sGAEventDesc, 0);pageTracker._linkByPost(this);}; 
		} else if(bGATrack) {
			// only Track
			if(bGAAsync)
				fFunc = function() {_gaq.push(['_linkByPost', this]);}; 
			else
				fFunc = function() {pageTracker._linkByPost(this);};
		} else if ( bGAEvent) {
			// only event
			if(bGAAsync)
				fFunc = function() {_gaq.push(['_trackEvent', sGAEventCat,sGAEventSubCat,sGAEventDesc, 0]);}; 
			else			
				fFunc = function() {pageTracker._trackEvent( sGAEventCat,sGAEventSubCat,sGAEventDesc, 0);};
		}

		oForm.onsubmit= fFunc;
		
  
	} catch(e){
		alert("Data update error: "+e.message);
	}

}

/**
 * Add Content to the label 
 */
function AddContent(x, xContent ) {
	var o;
	
	if (typeof x == "string") {
		o = document.getElementById(x);
		// esiste ???
		if (o) {
			switch (typeof xContent) {
				case 'number':            // Convert the number to a hexadecimal integer
				case 'string':            // Return the string enclosed in quotes
				case 'boolean':
					xContent= document.createTextNode(xContent);
					break;
				default:
					break;
			}
			if (o.hasChildNodes())
				o.replaceChild(xContent, o.firstChild);
			else if (xContent!==undefined)
				o.appendChild(xContent);
		}
	}
}

/**
 * Set days on the month
 * @param oForm
 * @param sType
 * @param bPreset
 */
 function setDayOfTheMonth( oForm, sType) {
	// Add List Box for day select
	var OneDay = 86400000;
	var ArrDate = new Date(oForm.yy.value,	oForm.mm.value-1,	oForm.dd.value);
	var DepDate = new Date(oForm.eyy.value,	oForm.emm.value-1,	oForm.edd.value);
	var toDay	= new Date();
	var nNights = parseInt(oForm.nights_1.value);
	
	switch (sType) {
	case START:
		if(ArrDate < toDay)
			ArrDate = toDay;
		if(ArrDate >= DepDate) {
			nNights = 1;
			if(nightsUsed)
				nNights = parseInt(oForm.nights_1.value);
			DepDate = new Date(ArrDate.getTime() + OneDay*nNights);
			updateDeparture(oForm, DepDate);
		}
		updateArrival(oForm, ArrDate);
		if (!nightsUsed) break;
	case NIGHTS:
		if((DepDate-ArrDate)/OneDay != parseInt(oForm.nights_1.value ))
			nNights = parseInt(oForm.nights_1.value);
		DepDate = new Date(ArrDate.getTime() + OneDay * nNights);
		updateDeparture(oForm, DepDate);
		nightsUsed	= true;
		break;
	case END:
		if(ArrDate >= DepDate) {
			nNights = 1;
			ArrDate = new Date(DepDate.getTime() - OneDay * nNights);
			if(ArrDate < toDay) {
				ArrDate = toDay;
				DepDate = new Date(ArrDate.getTime() + OneDay * nNights);
			}
			updateArrival(oForm, ArrDate);
		}
		updateDeparture(oForm, DepDate);
		nightsUsed	= false;		
		break;
	default:
		break;
	}
	oForm.nights_1.value = (DepDate - ArrDate)/OneDay;
}

/**
 * Set SELECT for Arrival Date
 * 
 * @param oForm
 * @param date
 */
function updateArrival( oForm, date){
	oForm.yy.value = date.getFullYear();
	oForm.mm.value = date.getMonth()+1;
	oForm.dd.value = date.getDate();
}

/**
 * Set SELECT for Departure Date 
 *
 * @param oForm
 * @param date
 */
function updateDeparture( oForm, date) {
	oForm.eyy.value = date.getFullYear();
	oForm.emm.value = date.getMonth()+1;
	oForm.edd.value = date.getDate();
}
// ******************************************************************************************************************************
