// JavaScript Document
function InitRoom(){
	for(var i=0; i<xInitRoom.length;i++){
		var elmRm = GetObjectById("no_room_"+xInitRoom[i][0]+"_"+xInitRoom[i][1]);
		var elmAd = GetObjectById("adults_"+xInitRoom[i][0]+"_"+xInitRoom[i][1]);
		var elmCh = GetObjectById("children_"+xInitRoom[i][0]+"_"+xInitRoom[i][1]);
		var elmIn = GetObjectById("infants_"+xInitRoom[i][0]+"_"+xInitRoom[i][1]);
		if (! (elmRm&&elmAd&&elmCh&&elmIn) ) continue;
		elmRm.selectedIndex = xInitRoom[i][2];
		OnRoomChange(elmRm,xInitRoom[i][0],xInitRoom[i][1]);
		if (xInitRoom[i][3]>10){
			elmAd.selectedIndex = 10;
			OnPersonChange(elmAd,xInitRoom[i][0],xInitRoom[i][1]);	
		}
		elmAd.selectedIndex=xInitRoom[i][3]-1;
		if (xInitRoom[i][4]>10){
			elmCh.selectedIndex = 11;
			OnPersonChange(elmCh,xInitRoom[i][0],xInitRoom[i][1]);	
		}
		elmCh.selectedIndex=xInitRoom[i][4];
		if (xInitRoom[i][5]>10){
			elmIn.selectedIndex = 11;
			OnPersonChange(elmIn,xInitRoom[i][0],xInitRoom[i][1]);	
		}
		elmIn.selectedIndex=xInitRoom[i][5];
	}
}

function OnRoomChange(elm,iCatId,iUnitId){
	var numRoom = parseInt(elm.options[elm.selectedIndex].value);
	var elmAdults  = GetObjectById("adults_"+iCatId+"_"+iUnitId);
	var elmChilren = GetObjectById("children_"+iCatId+"_"+iUnitId);
	var elmInfants = GetObjectById("infants_"+iCatId+"_"+iUnitId);
	
	var bDisable = (numRoom==0);
	if (elmAdults) elmAdults.disabled = bDisable;
	if (elmChilren && elmChilren.options.length>2) elmChilren.disabled = bDisable;
	if (elmInfants && elmInfants.options.length>2) elmInfants.disabled = bDisable;
}

function OnPersonChange(elm, iCatId, iUnitId){
	var iSelVal = elm.options[elm.selectedIndex].value;
	if (iSelVal=="more"){
		elm.remove(elm.selectedIndex);
		FillCBOptions(elm,11,40,11);
	}
}
function UpdateDate(){
	var frm = document.frmRoomRate;
	var today = new Date();
	var dd = today.getDate(); var mm = today.getMonth()+1; var yy = today.getFullYear();
	var res = CompareDate(frm.check_in.value,dd+"/"+mm+"/"+yy,0);
	if (res==0){
		alert("The correct the date value(s). The valid date format is dd/mm/yyyy.");
		frm.check_in.focus();
		return;
	}
	if (res==2){
		alert("Sorry, you can not select a past date.");
		frm.check_in.focus();
		return;
	}

	var res = CompareDate(frm.check_in.value,frm.check_out.value,0);
	if (res==0){
		alert("The correct the date value(s). The valid date format is dd/mm/yyyy.");
		frm.check_in.focus();
		return;
	}
	if (res!=2){
		alert("The check-in date must before check-out date.");
		frm.check_in.focus();
		return;
	}
	frm.action="";
	frm.act.value="reload";
	frm.submit();
}

function RemoveCBOptions(elm){
	var len = elm.options.length;
	for(var i=0;i<len;i++){
		elm.remove(0);	
	}
}

function FillCBOptions(elm,iFrom,iTo,iSelect){
	for(var i=iFrom;i<=iTo;i++){
		if (i==iSelect) AddComboOption(elm,i,i,true);
		else AddComboOption(elm,i,i);
	}
}

function OnSubmitRmBk(){
	var frm = document.frmRoomRate;
	//-- check check-in/check-out dates
	var today = new Date();
	var dd = today.getDate(); var mm = today.getMonth()+1; var yy = today.getFullYear();
	var res = CompareDate(frm.check_in.value,dd+"/"+mm+"/"+yy,0);
	if (res==0){
		alert("The correct the date value(s). The valid date format is dd/mm/yyyy.");
		frm.check_in.focus();
		return(false);
	}
	if (res==2){
		alert("Sorry, you can not select a past date.");
		frm.check_in.focus();
		return(false);
	}

	var res = CompareDate(frm.check_in.value,frm.check_out.value,0);
	if (res==0){
		alert("The correct the date value(s). The valid date format is dd/mm/yyyy.");
		frm.check_in.focus();
		return(false);
	}
	if (res!=2){
		alert("The check-in date must before check-out date.");
		frm.check_in.focus();
		return(false);
	}
	//-- check room-type and capacity
	var bHasBkItem = false;
	for(var id in xRoomCapacity){
		var elmRm = GetObjectById("no_room_"+id);
		//-- check if booked room
		if (!elmRm) continue;
		var noRm = parseInt(elmRm.options[elmRm.selectedIndex].value);
		var noAd = 0; var noCh = 0; var noIn = 0;
		if (noRm>0) {
			bHasBkItem=true;
			//-- if this is on per-person base
			if (xRoomCapacity[id]==0) continue;
			var elmAd = GetObjectById("adults_"+id);
			if (elmAd && !elmAd.disabled && elmAd.selectedIndex!=-1)
				noAd = parseInt(elmAd.options[elmAd.selectedIndex].value);
			var elmCh = GetObjectById("children_"+id);
			if (elmCh && !elmCh.disabled && elmCh.selectedIndex!=-1)
				noCh = parseInt(elmCh.options[elmCh.selectedIndex].value);
			var elmIn = GetObjectById("infants_"+id);
			if (elmIn && !elmIn.disabled && elmIn.selectedIndex!=-1)
				noIn = parseInt(elmIn.options[elmIn.selectedIndex].value);
			//-- check capacity
			if (noAd+noCh+noIn > noRm*parseInt(xRoomCapacity[id])){
				alert("The max capacity of unit is "+xRoomCapacity[id]+" persons.");
				elmRm.focus();
				return(false);
			}
		}
	}
	//-- check if there is any booked room
	if (!bHasBkItem){
		alert("Please select at least one unit.");
		return(false);
	}
	return(true);
}