// JavaScript Document
// This file will be called by the homepage, and creation of the div's below will be required so that it renders correctly.
document.write('<div class="mgnTop7">');
document.write('	<div id="hot-search-params"></div>');
document.write('</div>');
 

// Start of code taken from hotels.com LIVE site. Contains modifications to ensure XHTML is generated and not HTML.
var adultHelp="";

var textChildError="";
var adultsPerRoom=new Array();
var childrenPerRoom=new Array();
var childAgesPerRoom=new Array();
var numRooms=0;
var maxChildren=0;
var textRooms;
var textRoom;
var textAdults;
var textChildren;
var textAgeOfChildren;
var textChildHelp;


// NOTE: Question marks ("?") get replaced with a numeric value
var textRoomX="";
var textChildX="";

if(document.getElementsByName("jsNoOfRooms")[0] != null){
	numRooms=document.getElementsByName("jsNoOfRooms")[0].value;
}else{
	//numRooms=document.getElementsByName("numberOfRooms")[0].value;
	numRooms=1;
}


if (numRooms < 1) {
numRooms = 1;
}

if(document.getElementsByName("jsadultsPerRoom")[0] != null){
if(numRooms > 1){
var ctrChildAges = 0;

for(var j=0;j<numRooms;j=j+1){
adultsPerRoom[j]=document.getElementsByName("jsadultsPerRoom")[j].value;
childrenPerRoom[j]=document.getElementsByName("jschildrenPerRoom")[j].value;
var childAges = new Array();
for(var k=0;k<childrenPerRoom[j];k++){
childAges[k]=document.getElementsByName("jschildAgesPerRoom")[ctrChildAges].value;
ctrChildAges++;
}
childAgesPerRoom[j]=childAges;
}
}
else{
adultsPerRoom[0]=document.getElementsByName("jsadultsPerRoom")[0].value;
childrenPerRoom[0]=document.getElementsByName("jschildrenPerRoom")[0].value;
var childAges = new Array();
for(var k=0;k<childrenPerRoom[0];k++){
childAges[k]=document.getElementsByName("jschildAgesPerRoom")[k].value;
}
childAgesPerRoom[0]=childAges;
}
}
else{ 
var ctrChildAges = 0;

for(var j=0;j<numRooms;j=j+1){
	
adultsPerRoom[j]=2;
childrenPerRoom[j]=0;
}
}

refresh();

function setChildAge(room, child, age) {
if (childAgesPerRoom[room] == null) {
childAgesPerRoom[room] = new Array();
}
childAgesPerRoom[room][child] = age;
}

function setNumAdults(room, numAdults) {
adultsPerRoom[room] = numAdults;
}

function setNumChildren(room, numChildren) {
childrenPerRoom[room] = numChildren;
refresh();
}

function setNumRooms(x) {
numRooms = x;
for (i = 0; i < x; i++) {
if (adultsPerRoom[i] == null) {
adultsPerRoom[i] = 2;
}
if (childrenPerRoom[i] == null) {
childrenPerRoom[i] = 0;
}
}
refresh();
}

function renderRoomSelect() {
var x = '';
x += '<select height="20" class="select mgnTop5" style="width:45px;" name="rooms" id="rooms1" onchange="setNumRooms(this.options[this.selectedIndex].value);">';
for (var i = 1; i < 6; i++) {
x += '<option value="'+i+'"'+(numRooms == i ? ' selected="selected"' : '')+'>' + i + '</option>';
}
x += '</select>';
return x;
}

function refresh() {
	maxChildren = 0;
	var showChildHelp = false;
	for (var i = 0; i < numRooms; i++) {
		if (childrenPerRoom[i] > maxChildren) {
			maxChildren = childrenPerRoom[i];
		}
	}
	var x = '';
	if (adultHelp.length > 0) {
		x = adultHelp + "<p>\n";
	}

		x += '<table border="0" width="220"><tr><td>';
		x += '<table width="100%"><tr><td width="80" valign="top">';
		x += '<label>'+textRooms+'</label><br />';
		x += renderRoomSelect()+'</td>';

		x +='<td width="55">';
		x += '<label>'+textAdults+'</label><br />';
		for (var i = 0; i < numRooms; i++) {
			x += '<span class="weak">'+getValue(textRoomX, i+1)+'</span>';
			x += buildSelect(('adult'+i), 'setNumAdults(' + i + ', this.options[this.selectedIndex].value)', 1, 5, adultsPerRoom[i]);
			if ((i+1) < numRooms) {
				x += '<br />';
			}
		}
		x += '</td><td width="80" class="padLft5">';
		x += '<label>'+textChildren+'</label><br />';
		for (var i = 0; i < numRooms; i++) {
			x += '<span class="weak">'+getValue(textRoomX, i+1)+'</span>';
			x += buildSelect(('child'+i), 'setNumChildren(' + i + ', this.options[this.selectedIndex].value)', 0, 3, childrenPerRoom[i]);
			if ((i+1) < numRooms) {
				x += '<br />';
			}
		}
		x += '</td></tr><tr><td colspan="3">';

		var didHeader = false;
		var agesOfchildrenFlag=false;
		for (var i = 0; i < numRooms; i++) {

			if (childrenPerRoom[i] > 0) {
				showChildHelp = true;
				if(agesOfchildrenFlag == false){
					x += '<label>'+textAgeOfChildren+'</label>';
					x += '</br>';
					agesOfchildrenFlag =true;
				}
 
				x +='<div>';
				for (var j = 0; j < childrenPerRoom[i]; j++) {
					var def = -1;
					if (childAgesPerRoom[i] != null) {
						if (childAgesPerRoom[i][j] != null) {
							def = childAgesPerRoom[i][j];
						}
					}
					x += getValue(textChildX, j+1)+' ';
					x += '<select  height="20" class="select mgnTop5" style="width:45px; margin-left:1px;" name="age'+i+'_'+j+'" onchange="setChildAge('+i+', '+j+', this.options[this.selectedIndex].value);">';
					x += '<option value="-1"'+(def == -1 ? ' selected="selected"' : '')+'>-?-' + '</option>';
					x += '<option value="0"'+(def == 0 ? ' selected="selected"' : '')+'>&lt;1' + '</option>';
					for (var k = 1; k <= 12; k++) {
						x += '<option value="'+k+'"'+(def == k ? ' selected="selected"' : '')+'>'+k + '</option>';
					}
					x += '</select>';

				}
				x += '</div>';
			}
			else{
				//x += '<select visibility="false"></select>';
				x += '<div class="clr" style="height:26px;"><!-- --></div>';
				//x += 'B { line-height: 16pt }';

			}
		}
		x += '</td></tr></table></td></tr>';
		if (showChildHelp) {
			x += '<tr><td class="weak padTop10">'+textChildHelp+'</td></tr>';
		}
		x += '</table>';
		document.getElementById("hot-search-params").innerHTML = x;

<!-- Tip: Used for debgging to see what the string "x" contains, where "x" is the generic variable name hotels.com has used to output the innerHTML of hot-search-params. The string is placed in the textarea box named "test". -->
//document.bookingForm.test.value = x;
}

function buildSelect(name, onchange, min, max, selected) {
var x = '<select height="20" id="' + name + '" style="width:45px;"  class="select mgnTop5" name="' + name + '"';
if (onchange != null) {
x += ' onchange="' + onchange + '"';
}
x +='>\n';
for (var i = min; i <= max; i++) {
x += '<option value="' + i + '"';
if (i == selected) {
x += ' selected="selected"';
}
x += '>' + i  + '</option>'+ '\n';
}
x += '</select>';
return x;
}

function validateGuests(form) {
if (numRooms < 5) {
var missingAge = false;
for (var i = 0; i < numRooms; i++) {
var numChildren = childrenPerRoom[i];
if (numChildren != null && numChildren > 0) {
for (var j = 0; j < numChildren; j++) {
if (childAgesPerRoom[i] == null || childAgesPerRoom[i][j] == null || childAgesPerRoom[i][j] == -1) {
missingAge = true;
}
}
}
}
if (missingAge) {
alert(textChildError);
return false;
} else {
return true;
}
} else {
return true;
}
}


function submitGuestInfoForm(form) {
if (!validateGuests(form)) {
return false;
}
if (isFireFox) {
var gi = '';
for (var i = 0; i < numRooms; i++) {
if (i > 0) gi += ':';
gi += adultsPerRoom[i];
if (childrenPerRoom[i] > 0) {
gi += '-';
for (var j = 0; j < childrenPerRoom[i]; j++) {
if (j > 0) gi += ',';
gi += childAgesPerRoom[i][j];
}
}
}
form.rooms.value = numRooms;
form.guestInfo.value = gi;
}
return true;
}
function getValue(str, val) {
return str.replace(/\?/g, val);
}
