var childArray = new Array(0,0,0,0,0,0,0,0,0,0);
function showChildTitle() {
	var c = 0;
	var r = document.frm_booking.rooms[document.frm_booking.rooms.selectedIndex].value;
	for (var i=0;i<r;i++) {
		c = c + childArray[i];
	}
	/*
	if (c > 0) {
		document.getElementById('kids').style.display = '';
	} else {
		document.getElementById('kids').style.display = 'none';
	}
	*/
	return true;
}
function showChild(me,Span,nb) {
	var occ = me[me.selectedIndex].value;
	if (occ == '2|1|price_d') {
		alert ('When you select \'Double/Twin + one Child\', there is no additional bed. \nThe child will need to share the existing bedding. \n If you want to guarantee a separate bed, please click on \'Triple (3 people)\'');
		childArray[nb-1] = 1;
		document.getElementById(Span).style.display = '';
	} else {
		childArray[nb-1] = 0;
		document.getElementById(Span).style.display = 'none';
	}
	return showChildTitle();
}
function showRoom() {
	var nbR = document.frm_booking.rooms[document.frm_booking.rooms.selectedIndex].value;
	var j;
	for (var i=1; i<=10; i++) {
		document.getElementById('r'+i).style.display = 'none';
	}
	for (i=1; i<=nbR; i++) {
		document.getElementById('r'+i).style.display = '';
	}

	return showChildTitle();;
}
function buildRoomList() {
	for (var i=2;i<=10;i++) {
		document.writeln('<tr id="r'+i+'" class=f12 style="display:none;">');
		document.writeln('<td width="100" align="left" style="padding-left:23px"><font color=#17592B>&nbsp; Room '+i+'</font><\/td>');
		document.writeln('<td ><select name="room[]" onChange="return showChild(this,\'kid_'+i+'\','+i+');">');
		document.writeln('<script>');
		document.writeln('buildOcc();');
		document.writeln('<\/script>');
		document.writeln('<\/select><\/td>');
		document.writeln('<td colspan="'+i+'" align="left"><span id="kid_'+i+'" style="display:none;"><font color=#17592B>Child age : </font><select name="child[]">');
		document.writeln('<script>');
		document.writeln('buildOpt(childAge,childAge);');
		document.writeln('<\/script>');
		document.writeln('<\/select>');
		document.writeln('<\/span><\/td><\/tr>');		
	}
	return true;
}

var childAge = '2|3|4|5|6|7|8|9|10|11';
function buildOpt(values,texts){
	var valuesT = values.split("|");
	var textsT = texts.split("|");
	var j = parseInt(valuesT.length);
	for (var i=0;i<j;i++) {
		document.writeln('<option value="' + valuesT[i] + '">' + textsT[i] + '<\/option>');
	}
}

function buildOcc() {
	document.writeln('<option value="1|0|price_s|single">Single<\/option>');
	document.writeln('<option value="2|0|price_d|double">Double<\/option>');
	//document.writeln('<option value="2|1|price_d">Double/Twin + one Child<\/option>');
	document.writeln('<option value="3|0|price_t|triple">Triple<\/option>');
}

function check_extra_bed_adult(){
	var max_child=2;
	var room=parseFloat(document.frm_booking.elements[2].value);
	var child_open=parseFloat(document.frm_booking.child_open.value);
	var s_room=room+2;
	var sum_man=0;
	var child=parseFloat(document.frm_booking.child.value);
	var adult=parseFloat(document.frm_booking.adult.value);
	for(var n=3;n<=s_room;n++){
		var val_room=document.frm_booking.elements[n].value;
		var val_man=val_room.split("|");
		sum_man=sum_man+parseFloat(val_man[0]);
		//alert(document.frm_booking.elements[a].value);
	}
	
	if((adult>sum_man) && child_open!=0){
		alert("Exceed adult, please select extra room or extrabed.");
		document.getElementById('extra_adult').style.display = '';
		document.frm_booking.adult_open.value=1;
	}else 	if((adult>sum_man) && child_open==0){
		alert("Exceed adult, please select extra room or extrabed.");
		document.getElementById('extra').style.display = '';
		document.getElementById('extra_adult').style.display = '';
		document.frm_booking.adult_open.value=1;
	}else if((adult<=sum_man) && child_open==0){
		document.getElementById('extra').style.display = 'none';
		document.getElementById('extra_adult').style.display = 'none';
		document.frm_booking.adult_open.value=0;
	}else if((adult<=sum_man) && child_open!=0){
		document.getElementById('extra_adult').style.display = 'none';
		document.frm_booking.adult_open.value=0;
	}
	//else if((adult<=sum_man) && (child<=max_child)){
	//	document.getElementById('extra').style.display = 'none';
	//}
		
}

function check_extra_bed_child(){
	var max_child=2;
	var adult_open=parseFloat(document.frm_booking.adult_open.value);
	var room=parseFloat(document.frm_booking.elements[2].value);
	//var s_room=room+2;
	//var sum_man=0;
	var child=parseFloat(document.frm_booking.child.value);
	//var adult=parseFloat(document.frm_booking.adult.value);
	/*for(var n=3;n<=s_room;n++){
		var val_room=document.frm_booking.elements[n].value;
		var val_man=val_room.split("|");
		sum_man=sum_man+parseFloat(val_man[0]);
		//alert(document.frm_booking.elements[a].value);
	}*/
	
	if((child>max_child) && adult_open!=0){
		alert("Exceed child, please select extra room or extrabed.");
		document.getElementById('extra_child').style.display = '';
		document.frm_booking.child_open.value=1;
	}else if((child>max_child) && adult_open==0){
		alert("Exceed child, please select extra room or extrabed.");
		document.getElementById('extra').style.display = '';
		document.getElementById('extra_child').style.display = '';
		document.frm_booking.child_open.value=1;
	}else if((child<=max_child) && adult_open==0){
		document.getElementById('extra').style.display = 'none';
		document.getElementById('extra_child').style.display = 'none';
		document.frm_booking.child_open.value=0;
	}else if((child<=max_child) && adult_open!=0){
		document.getElementById('extra_child').style.display = 'none';
		document.frm_booking.child_open.value=0;
	}
	//else if((adult<=sum_man) && (child<=max_child)){
	//	document.getElementById('extra').style.display = 'none';
	//}
		
}
