function dateToString(date)
{
	var str='';
	var month=date.getMonth()+1;
	var day=date.getDate();
	month=month.toString();
	day=day.toString();
	if(month.length<=1) month='0'+month;
	if(day.length<=1) day='0'+day;
	str=date.getFullYear()+"-"+month+"-"+day;
	return str;
}

function checkRoomStatus(roomIds,roomIdsTxt)
{   
	var roomsStatussTxt=document.getElementById('roomStatus').value;
	var roomsStatuss=Array();
	var roomStatus=Array();
	var status=Array();
	var date=Array();
	var arrivalDate=new Date(document.myForm.a_year.value, document.myForm.a_month.value-1, document.myForm.a_date.value);
	var departureDate=new Date(arrivalDate);
	departureDate.setDate(departureDate.getDate()+parseInt(document.getElementById('nbNight').value)-1);
	var staBeginDate;
	var staEndDate;
	var staLen;
	var str='';
	var isJoinRoomTypeName=isShow=false;
	var roomIdsLen=roomIds.length;
	roomsStatuss=roomsStatussTxt.split('|||');
	var len=roomsStatuss.length;
	for(var k=0;k<roomIdsLen;k++)
	{
			for(var i=0;i<len;i++)
			{
					roomStatus=roomsStatuss[i].split('||');
					if(roomIds[k]==roomStatus[0])
					{   
							staLen=roomStatus.length;
							for(var j=1;j<staLen;j++)
							{
									status=roomStatus[j].split('|');
									date=status[0].split('-');
									staBeginDate=new Date(date[0],date[1]-1,date[2]);
									date=status[1].split('-');
									staEndDate=new Date(date[0],date[1]-1,date[2]);
									if(!(Date.parse(staBeginDate)>Date.parse(departureDate)||Date.parse(staEndDate)<Date.parse(arrivalDate)))
									{
											isJoinRoomTypeName=isShow=true;
											str=str+' from ';
											if (Date.parse(staBeginDate)<Date.parse(arrivalDate)) 
												 str=str+dateToString(arrivalDate);
											else str=str+dateToString(staBeginDate);
											str=str+' to ';
											if (Date.parse(staEndDate)<Date.parse(departureDate))    
													 str=str+dateToString(staEndDate);
											else     str=str+dateToString(departureDate);
											str=str+' Probably '+status[2]+'\n               ';
									}
							}
					}
			}
			if(isJoinRoomTypeName)
				str='    '+roomIdsTxt[k]+' '+str+'\n';
				isJoinRoomTypeName=false;
	}
	str=str+"    Press \"Ok\" to book anyway, we suggest you fill \"Second Choice Hotel\", \"Cancel\" to choose other rooms";
	if(isShow)
	return confirm(str,'cantonfair');
}

function showRoom()
{
	var nbR = document.myForm.room_quantity.value;
	for ( var  i = 1 ; i <= 10; i ++ )
	{
		document.getElementById("Room"+i).style.display="none";
	}
	for ( var  i = 1 ; i <= nbR; i ++ )
	{
		document.getElementById("Room"+i).style.display="";
	}
}

function bookRoom(roomId)
{
	bookform.scrollIntoView(true);  
	var rooms=Array();
	rooms=document.getElementsByName('room_id[]');
	for(var i=0;i<rooms.length;i++) 
	rooms[i].value=roomId;
}

function updateCheckOut(checkInD,checkInM,checkInY,nbNights,printD,printM,printY){
echoCheckOutD = document.getElementById(printD);
echoCheckOutM = document.getElementById(printM);
echoCheckOutY = document.getElementById(printY); 
ScheckOut = document.getElementById(print.value);  
if ((checkInD.value!=null) || (checkInM.value!=null) || (checkInY.value!=null)){
var thedate = new Date();
thedate.setFullYear(checkInY.value, checkInM.value-1, checkInD.value);
if (thedate.getDate() != checkInD.value) {
thedate.setMonth(thedate.getMonth()-1);
var replaceInD;
if(thedate.getDate()<10) 
replaceInD= thedate.getDate();
else replaceInD=thedate.getDate();
checkInD.value=replaceInD;
checkInM.value=thedate.getMonth()+1;
checkInY.value=thedate.getFullYear();
}

thedate.setDate(thedate.getDate()+parseInt(nbNights.value));
months = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
echoCheckOutD.innerHTML = thedate.getDate();
echoCheckOutM.innerHTML = months[thedate.getMonth()];
echoCheckOutY.innerHTML = thedate.getFullYear();
return 1;
}
}

function buildOpt(values,texts) 
{
	var valuesT = values.split("|");
	var textsT = texts.split("|");
	for (var i=0;i<valuesT.length;i++) {
		document.writeln('<option value="' + valuesT[i] + '">' + textsT[i] + '<\/option>');
	}
}

function CheckSubmit()
{ 
	var today = new Date();
	var selectTime=new Date();
	var room=Array()
	var roomIds=Array();
	var roomIdsTxt=Array();
	selectTime.setFullYear(document.myForm.a_year.value, document.myForm.a_month.value-1, document.myForm.a_date.value);
	if(selectTime<today)
	{
		alert("你输入的日期不能小于今天.");
		return false;
	} 
	today.setDate(today.getDate()+1); 
	if(selectTime<today)
	{
		alert("如果你想今天入住,请必须联系我们,我们将优先处理你的预定,电话: +86-20-3765-3678");
	} 
	if(document.myForm.nbNight.options[document.myForm.nbNight.selectedIndex].innerText=="30+") 
		alert("如果你想预定30天以上,请电话联系我们: +86-20-3765-3678");

	room=document.getElementsByName('room_id[]');
	var k=0;
	var roomQuantity=document.getElementById('room_quantity').value;
	for(var i=0;i<roomQuantity;i++)
	{
		for(var j=0;j<=i;j++)
		{
			if(room[i].value==room[j].value&&j<i) break;
			if(j==i)  
			{
				roomIds[k]=room[j].value;
				roomIdsTxt[k]=room[j].options[room[j].selectedIndex].text;
				k=k+1;
			}
		}
	}
	return checkRoomStatus(roomIds,roomIdsTxt);
}

function setMaxGuest(obj)
{
	var room=document.getElementsByName("room_id[]");
	var Max=document.getElementsByName("MaxGuest[]"); 
	for(var i=0;i<room.length;i++) 
	if(obj == room[i])
	{
		Max[i].length=1;
	    Max[i].options[0] = new Option(1,1);
		var len=document.getElementById("roomMaxguest"+(obj.selectedIndex+1)).value;
		Max[i].options.length=0;
		for(var j=1;j<=len;j++)
		{
			Max[i].options[Max[i].options.length] = new Option(j,j);
		}
	}
} 
