/**
	@Hotel search date display.
	@2004-06-7
	@author Dingzhener

*/
//Get date.
var nowDate = new Date();					//get now date
var currentYear = nowDate.getYear();			//get now year
var nextYear = currentYear+1;				//get next year
var currentMonth = nowDate.getMonth()+1;		//get now month
var currentDay = nowDate.getDate();			//get now day

//Month array.
monthName = new Array(13);
monthName[1]="Jan.";
monthName[2]="Feb.";
monthName[3]="Mar.";
monthName[4]="Apr.";
monthName[5]="May.";
monthName[6]="Jun.";
monthName[7]="Jul.";
monthName[8]="Aug.";
monthName[9]="Sep.";
monthName[10]="Oct.";
monthName[11]="Nov.";
monthName[12]="Dec.";

//Day of month array.
dayofMonth = new Array(13);
dayofMonth[1]=31;
dayofMonth[2]=28;
dayofMonth[3]=31;
dayofMonth[4]=30;
dayofMonth[5]=31;
dayofMonth[6]=30;
dayofMonth[7]=31;
dayofMonth[8]=31;
dayofMonth[9]=30;
dayofMonth[10]=31;
dayofMonth[11]=30;
dayofMonth[12]=31;

if((dayofMonth[currentMonth] - currentDay) <= 1){	
	currentDay=currentDay+1;
	if(currentDay>=dayofMonth[currentMonth])currentDay=currentDay-dayofMonth[currentMonth];
	currentMonth=currentMonth+1;	
	if(currentMonth>12) currentMonth=currentMonth-12;
	
}
//function show the year.
function ShowYear(){
if(currentMonth<6){
	document.write("<option selected value="+currentYear+">"+currentYear+"</option>");}
else{
	document.write("<option selected value="+currentYear+">"+currentYear+"</option>");
	document.write("<option value="+nextYear+">"+nextYear+"</option>");
	}
}

//Function show the month
function ShowMonth(){
for(j=1;j<=12;j++){	
if(j<10){
			if(currentMonth==j){
				document.write("<option selected value=0"+j+">"+monthName[j]+"</option>");
			}
			else document.write("<option value=0"+j+">"+monthName[j]+"</option>");
		
		}
else{
			if(currentMonth==j){
				document.write("<option selected value="+j+">"+monthName[j]+"</option>");
			}
			else document.write("<option value="+j+">"+monthName[j]+"</option>");
		}
	
}
	
}

//Function show the day
function ShowDay(m){
	if(m==1){
	for(i=1;i<=dayofMonth[currentMonth];i++){
		if(i<10){
			if(i==(currentDay+1)) document.write("<option selected value=0"+i+">"+i+"</option>");
			else document.write("<option value=0"+i+">"+i+"</option>");
	}
		else{
			if(i==(currentDay+1)) document.write("<option selected value="+i+">"+i+"</option>");
			else document.write("<option value="+i+">"+i+"</option>");
	}
	
	
	}}
	if(m==2){
	for(i=1;i<=dayofMonth[currentMonth];i++){
		if(i<10){
			if(i==(currentDay+2)) document.write("<option selected value=0"+i+">"+i+"</option>");
			else document.write("<option value=0"+i+">"+i+"</option>");
	}
		else{
			if(i==(currentDay+2)) document.write("<option selected value="+i+">"+i+"</option>");
			else document.write("<option value="+i+">"+i+"</option>");
	}
	
	
	}}
	
	
}

//Function to relate .
function relatePages(category){
		
		if(category.name == "arrmonth"){
			var mo = document.serchhotel.arrmonth.value;
			var pagesMenu = document.serchhotel.arrday;
		}
		if(category.name == "depmonth"){
			var mo = document.serchhotel.depmonth.value;
			var pagesMenu = document.serchhotel.depday;
		}
		
		
		mo = mo - 12 + 12;
		
		
		//Clear the old option
		for(var i = pagesMenu.options.length;i >= 0;i--){
			pagesMenu.options[i] = null;
		}
		
		//Add the new pages
		for(var i = 1;i<=dayofMonth[mo];i++){
			if(i<10){
			pagesMenu.options[i] = new Option(i,'0'+i);}
		else pagesMenu.options[i] = new Option(i,i);
		}
		
}



function Check()
{
		ayear = document.serchhotel.arryear.value;
		amonth = document.serchhotel.arrmonth.value - 1;

		aday = document.serchhotel.arrday.value;
		dyear = document.serchhotel.depyear.value;
		dmonth = document.serchhotel.depmonth.value - 1;

		dday = document.serchhotel.depday.value;
		//if(ayear!=""&&amonth!=""&&aday!=""&&dyear!=""&&dmonth!=""&&dday!="")
		//alert ("well!");
		var adate = new Date(ayear,amonth,aday);
		var a = adate.getTime();
		var ddate = new Date(dyear,dmonth,dday);
		var b = ddate.getTime();
		
		if (document.serchhotel.city.value=="")
		{
			alert ("Please choose the city name!");
			document.serchhotel.city.focus();
			return false;
		}
		if (a > b)
		{
		alert ("Please check the date!");
		document.serchhotel.arryear.focus();
		return false;
		}
		if(compareDate(amonth,aday,ayear,dmonth,dday,dyear)){
			alert ("Dear user,You are submitting a reservation order with more than 20 nights! Such order forms will be handled with special procedures. Please send your requests to us via http://www.chinats.com/mail.htm to make the reservation. Thank you for your understanding!");
			return false;
		}

		if(chkValid(amonth,aday,ayear,dmonth,dday,dyear)) 
		{
			return true;
		}
		else return false;			
}

//Function to check nights more zhan 20.
function compareDate(tmp1,tmp2,tmp3,tmp4,tmp5,tmp6) {
	var tmpdate1 = new Date(tmp1+'/'+tmp2+'/'+tmp3);
	var tmpdate2 = new Date(tmp4+'/'+tmp5+'/'+tmp6);

	if ((tmpdate2.valueOf() - tmpdate1.valueOf()) >= 20*24*60*60*1000)
	{
		return true;
	}
else
	{
		return false;
	}
}


function chkValid(mm,dd,yy,mmm,ddd,yyy)
	{

		var date2 = new Date(yy,mm,dd);
		var dd2 = date2.getDate();
		var mm2 = date2.getMonth();
		var yy2 = date2.getFullYear();


		if(dd==dd2 && mm==mm2 && yy==yy2)
		{
		var date3 = new Date(yyy,mmm,ddd);
		var dd3 = date3.getDate();
		var mm3 = date3.getMonth();
		var yy3 = date3.getFullYear();	
		if(ddd==dd3 && mmm==mm3 && yyy==yy3)
		return true;	
		else
		{
		alert("Invalid Date (departure date)!");
		document.serchhotel.depday.focus();
		return false;
		}		
		}	
		else
		{
		alert("Invalid Date (arrivel date)!");
		//alert(document.serchhotel.arrmonth.value);
		alert(mm);
		alert(mm2);
		document.serchhotel.arrday.focus();
		return false;
		}
		
		
			
}
	
	

