
// 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 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("jsadultsPerRoom")[0] != null){

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;
adultsPerRoom[0]=2;
childrenPerRoom[0]=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 refresh() {
	maxChildren = 0;
	var showChildHelp = false;
	if (childrenPerRoom[0] > maxChildren) {
			maxChildren = childrenPerRoom[0];
	}

	var x = '';
	if (adultHelp.length > 0) {
		x = adultHelp + "<p>\n";
	}

	x += '<table border="0" width="470"><tr><td>';
	x += '<table><tr>';
	x +='<td valign="top" width="55">';
	x += textAdults+'<br />';
	x += '<span class="weak">'+getValue(textRoomX, 1)+'</span>';
	x += buildSelect(('adult0'), 'setNumAdults(0, this.options[this.selectedIndex].value)', 1, 5, adultsPerRoom[0]);

	x += '</td><td width="100" valign="top" class="padLft5">';
	x += textChildren+'<br />';
	x += '<span class="weak">'+getValue(textRoomX, 1)+'</span>';
	x += buildSelect(('child0'), 'setNumChildren(0, this.options[this.selectedIndex].value)', 0, 3, childrenPerRoom[0]);
	x += '</td><td valign="top">';

	var didHeader = false;
	var agesOfchildrenFlag=false;
	if (childrenPerRoom[0] > 0) {
		showChildHelp = true;
		if(agesOfchildrenFlag == false){
			x += textAgeOfChildren;
			x += '</br>';
			agesOfchildrenFlag =true;
		}
 
		x +='<div>';
		for (var j = 0; j < childrenPerRoom[0]; j++) {
			var def = -1;
			if (childAgesPerRoom[0] != null) {
				if (childAgesPerRoom[0][j] != null) {
					def = childAgesPerRoom[0][j];
				}
			}
			x += getValue(textChildX, j+1)+' ';
			x += '<select  height="20" class="select mgnTop5" style="width:45px; margin-left:1px;" name="age0_'+j+'" onchange="setChildAge(0, '+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></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) {
var missingAge = false;
var numChildren = childrenPerRoom[0];
if (numChildren != null && numChildren > 0) {
for (var j = 0; j < numChildren; j++) {
if (childAgesPerRoom[0] == null || childAgesPerRoom[0][j] == null || childAgesPerRoom[0][j] == -1) {
missingAge = true;
}
}
}
if (missingAge) {
alert(textChildError);
return false;
} else {
return true;
}

}


function submitGuestInfoForm(form) {
if (!validateGuests(form)) {
return false;
}
if (isFireFox) {
var gi = '';
gi += adultsPerRoom[0];
if (childrenPerRoom[0] > 0) {
gi += '-';
for (var j = 0; j < childrenPerRoom[0]; j++) {
if (j > 0) gi += ',';
gi += childAgesPerRoom[0][j];
}
}

form.guestInfo.value = gi;
}
return true;
}
function getValue(str, val) {
return str.replace(/\?/g, val);
}

