//--------------------------------------Date Show---------------------------------------//

function calculateJD(calendarDate) {
   cdDate = new Date(calendarDate)
   year = cdDate.getFullYear()          
   if (year < 1000 ) {year+=1900}  
   //year = 1900+cdDate.getFullYear()
   month = cdDate.getMonth() + 1 
   day = cdDate.getDate()
   hour = cdDate.getHours()
   min = cdDate.getMinutes()
   sec = cdDate.getSeconds()
   univTime = hour+(min/60)+(sec/3600)
   if ((100*year+month-190002.5) >= 0) {sign = 1}
      else {sign = -1}
   with (Math) {
      part1 = 367 * year
      part2 = floor((7*(year+floor((month+9)/12)))/4)
      part3 = day+floor((275*month)/9)
      part4 = 1721013.5+(univTime/24)
      part5 = 0.5*sign
      jd = part1-part2+part3+part4-part5+0.5
   }
   return jd
}

function calculateCD(julianDate) {
   with (Math) {
      X = parseFloat(julianDate)+0.5
      Z = floor(X)
      F = X - Z
      Y = floor((Z-1867216.25)/36524.25)
      A = Z+1+Y-floor(Y/4)
      B = A+1524
      C = floor((B-122.1)/365.25)
      D = floor(365.25*C)
      G = floor((B-D)/30.6001)
      month = (G<13.5) ? (G-1) : (G-13)
      year = (month<2.5) ? (C-4715) : (C-4716)
      month -= 1 // month in JavaScript is from 0 to 11
      UT = B-D-floor(30.6001*G)+F
      day = floor(UT)
      UT -= floor(UT)
      UT *= 24;
      hour = floor(UT)
      UT -= floor(UT)
      UT *= 60
      minute = floor(UT)
      UT -= floor(UT)
      UT *= 60
      second = round(UT)
   }
   cdDate = new Date(Date.UTC(year, month, day, hour, minute, second))
   return cdDate;
   //return cdDate.toGMTString()
}

function Date_change(formName, dayadd, mode){
	
	theForm = formName;
	
	today = new Date();
	todayDay = today.getDate();
	todayMonth = today.getMonth()+1;
	todayYear = today.getFullYear();
	
	aryDay = ["", "31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];

	if(dayadd == ''){
		dayadd = 1;		
	}
	if(mode == 0){
		if(parseInt(theForm.monthfrom.value) == 2 &&  parseInt(theForm.yearfrom.value)%4 != 0){
			daysinmonth = 28;
		}else{
			daysinmonth = aryDay[theForm.monthfrom.value];
		}
		if(parseInt(theForm.dayfrom.value) > parseInt(daysinmonth)){
			theForm.dayfrom.value = daysinmonth;
		}	
		if(parseInt(theForm.monthfrom.value) < parseInt(todayMonth) && parseInt(theForm.yearfrom.value) <= parseInt(todayYear)){
			theForm.yearfrom.value = parseInt(todayYear) + 1;
		}else if(parseInt(theForm.monthfrom.value) == parseInt(todayMonth) && parseInt(theForm.dayfrom.value) < parseInt(todayDay)){
			theForm.dayfrom.value = todayDay;
			theForm.yearfrom.value = todayYear;
		}
	    if(parseInt(theForm.monthfrom.value) == parseInt(todayMonth) - 1 && parseInt(theForm.dayfrom.value) == parseInt(daysinmonth)){
			alert('For booking 1 year or more in advance,\nplease contact our sales department +66-76-248459');
			theForm.dayfrom.value = todayDay;
			theForm.monthfrom.value = todayMonth;
			theForm.yearfrom.value = todayYear;
		}
		if(parseInt(theForm.monthfrom.value) >= parseInt(todayMonth)){
			theForm.yearfrom.value = todayYear;
		}else if(parseInt(theForm.monthfrom.value) < parseInt(todayMonth)){
			theForm.yearfrom.value = parseInt(todayYear) + 1;
		}

		newDate = nextDate(theForm.dayfrom.value, theForm.monthfrom.value, theForm.yearfrom.value, dayadd);
		
		newDay = newDate.getDate()
		newMonth = newDate.getMonth() + 1;	
		newYear = newDate.getFullYear();

		theForm.dayend.value = newDay;
		theForm.monthend.value = newMonth;
		theForm.yearend.value = newYear;
		
		if(parseInt(theForm.monthend.value) == parseInt(todayMonth) && parseInt(theForm.dayend.value)  <= parseInt(todayDay)){
			//theForm.dayfrom.value == parseInt(daysinmonth) - 1;
			//theForm.yearfrom.value = todayYear + 1;
			Date_change(theForm, '', '0');
		}

		//alert(theForm.dayfrom.value+'/'+theForm.monthfrom.value +'/'+theForm.yearfrom.value + ':' + theForm.dayend.value+'/'+theForm.monthend.value +'/'+theForm.yearend.value+'-->'+theForm.yearfrom.value);
	}else if(mode == 1){		
		if(parseInt(theForm.monthend.value) == 2 &&  parseInt(theForm.yearend.value)%4 != 0){
			daysinmonth = 28;
		}else{
			daysinmonth = aryDay[theForm.monthend.value];
		}
		if(parseInt(theForm.dayend.value) > parseInt(daysinmonth)){
			theForm.dayend.value = daysinmonth;
		}	
		if(parseInt(theForm.monthend.value) == parseInt(theForm.monthfrom.value) && parseInt(theForm.dayend.value) <= parseInt(theForm.dayfrom.value)){
			Date_change(theForm, '', '0');
		}
		if(parseInt(theForm.monthend.value) < parseInt(theForm.monthfrom.value) && parseInt(theForm.yearend.value) <= parseInt(theForm.yearfrom.value)){
			theForm.yearend.value = parseInt(theForm.yearfrom.value) + 1;
		}
		if(parseInt(theForm.monthend.value) < parseInt(todayMonth) && parseInt(theForm.yearend.value) <= parseInt(todayYear)){
			theForm.yearend.value = parseInt(todayYear) + 1;
		}else if(parseInt(theForm.monthend.value) == parseInt(todayMonth) && parseInt(theForm.dayend.value) <= parseInt(todayDay)){
			Date_change(theForm, '', '0');
		}
		
		if(parseInt(theForm.monthend.value) >= parseInt(todayMonth) && parseInt(theForm.yearend.value) > parseInt(todayYear)){
			theForm.yearend.value = todayYear;
		} 
		
		if(parseInt(theForm.monthend.value) >= parseInt(todayMonth)){
			theForm.yearend.value = todayYear;
		}else if(parseInt(theForm.monthend.value) < parseInt(todayMonth)){
			theForm.yearend.value = parseInt(todayYear) + 1;
		}
		//alert(theForm.yearfrom.value + ' / ' + theForm.yearend.value);
		if(parseInt(theForm.yearend.value) < parseInt(theForm.yearfrom.value)){
			alert('For booking 1 year or more in advance,\nplease contact our sales department +66-76-248459');
			Date_change(theForm, '', '0');
		}
	}
	if(parseInt(theForm.monthfrom.value) >= parseInt(todayMonth)){
		theForm.yearfrom.value = todayYear;
	}else if(parseInt(theForm.monthfrom.value) < parseInt(todayMonth)){
		theForm.yearfrom.value = parseInt(todayYear) + 1;
	}
	if(parseInt(theForm.monthend.value) >= parseInt(todayMonth)){
		theForm.yearend.value = todayYear;
	}else if(parseInt(theForm.monthend.value) < parseInt(todayMonth)){
		theForm.yearend.value = parseInt(todayYear) + 1;
	}

}

function nextDate(dd, mm, yyyy, dayadd){
	
	if(dd.value <= 9){
		new_day = "0" + dd;
	}else{
		new_day = "" + dd;
	}
	if(mm <= 9){
		new_month = "0" + mm;
	}else{
		new_month = "" + mm;
	}
	new_year = yyyy;

	JD = calculateJD(new_month + "/" + new_day + "/" + new_year);
	newJD = JD + parseInt(dayadd);
	newDate = calculateCD(newJD);
	
	return newDate;	

}

function show_day(dayadd, mode){
	
	theForm = document.form_booking;
	gMonth = '';
	
	today = new Date();
	todayDay = today.getDate()
	todayYear = today.getFullYear();
	todayMonth = today.getMonth() + 1;
	aryDay = ["", "31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
	aryMonth = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	flag = 0;	
	
	if(gMonth != ''){
		todayMonth = gMonth;
	}
	
	newDate = nextDate(todayDay, todayMonth, todayYear, dayadd);

	todayDay = newDate.getDate()
	todayYear = newDate.getFullYear();
	todayMonth = newDate.getMonth() + 1;	
	
	gDay = parseInt(todayDay);	
	gMonth = parseInt(todayMonth);	
	gYear = parseInt(todayYear);
	
	if(gMonth == 2 && gYear%4 != 0){
		daysinmonth = 28;
	}else{
		daysinmonth = aryDay[gMonth];
	}	
	
	if(mode == 0){
		selectday = '<select name="dayfrom" id="dayfrom" onChange="return Date_change(document.form_booking, \'\', \'0\');">\n';
		selectdayend = '</select>\n';		
		selectmonth = '<select name="monthfrom" id="monthfrom" onChange="return Date_change(document.form_booking, \'\', \'0\');">\n';
		selectmonthend = '</select>\n';
	}else{
		selectday = '<select name="dayend" id="dayend" onChange="return Date_change(document.form_booking, \'\', \'1\');">\n';
		selectdayend = '</select>\n';		
		selectmonth = '<select name="monthend" id="monthend" onChange="return Date_change(document.form_booking, \'\', \'1\');">\n';
		selectmonthend = '</select>\n';
	}
	
	document.write(selectday);
	//for(i=1; i<=daysinmonth; i++){
	for(i=1; i<=31; i++){
		
		if(gDay){
			if(i <= 9){
				str_day = "0" + i;
			}else{
				str_day = "" + i;
			}
			if(i == gDay){
				document.write('<option value = "' + i + '" selected>' + str_day + '</option>\n');
			}else{
				document.write('<option value = "' + i + '">' + str_day + '</option>\n');
			}
		}	
	}
	document.write(selectdayend);
	document.write(selectmonth);
	

	for(i=0; i<12; i++){
		monthValue = parseInt(todayMonth) + parseInt(i);
		if(monthValue > 12){
			monthValue = parseInt(monthValue) % 12;
			flag++;
			if(flag == 1){
				gYear = parseInt(gYear)+1;				
			}	
		}
		if(i == 1){
			fYear = gYear;
		}
		monthName = aryMonth[monthValue];
		if(monthValue == gMonth){
			document.write('<option value = "' + monthValue + '" selected>' + monthName + " " + gYear + '</option>\n');
		}else{
			document.write('<option value = "' + monthValue + '">' + monthName + " " + gYear + '</option>\n');
		}
	}	

	document.write(selectmonthend);
	if(mode == 0){
		selectyear = '<input name="yearfrom" type="hidden" id="yearfrom" value="'+ fYear +'">';
	}else{
		selectyear = '<input name="yearend" type="hidden" id="yearend" value="'+ fYear +'">';
	}
	document.write(selectyear);
	
}

function show_day2(dayadd, mode){
	
	theForm = document.form_booking; 
	gMonth = '';
	
	today = new Date();
	todayDay = today.getDate()
	todayYear = today.getFullYear();
	todayMonth = today.getMonth() + 1;
	aryDay = ["", "31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
	aryMonth = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	flag = 0;	
	
	if(gMonth != ''){
		todayMonth = gMonth;
	}
	
	newDate = nextDate(todayDay, todayMonth, todayYear, dayadd);

	todayDay = newDate.getDate()
	todayYear = newDate.getFullYear();
	todayMonth = newDate.getMonth() + 1;	
	
	gDay = parseInt(todayDay);	
	gMonth = parseInt(todayMonth);	
	gYear = parseInt(todayYear);
	
	if(gMonth == 2 && gYear%4 != 0){
		daysinmonth = 28;
	}else{
		daysinmonth = aryDay[gMonth];
	}	
	
	if(mode == 0){
		selectday = '<select name="dayfrom" id="dayfrom" onChange="return Date_change(document.form_booking, \'\', \'0\');">\n';
		selectdayend = '</select>\n';		
		selectmonth = '<select name="monthfrom" id="monthfrom" onChange="return Date_change(document.form_booking, \'\', \'0\');">\n';
		selectmonthend = '</select>\n';
	}else{
		selectday = '<select name="dayend" id="dayend" onChange="return Date_change(document.form_booking, \'\', \'1\');">\n';
		selectdayend = '</select>\n';		
		selectmonth = '<select name="monthend" id="monthend" onChange="return Date_change(document.form_booking, \'\', \'1\');">\n';
		selectmonthend = '</select>\n';
	}
	
	document.write(selectday);
	//for(i=1; i<=daysinmonth; i++){
	for(i=1; i<=31; i++){
		
		if(gDay){
			if(i <= 9){
				str_day = "0" + i;
			}else{
				str_day = "" + i;
			}
			if(i == gDay){
				document.write('<option value = "' + i + '" selected>' + str_day + '</option>\n');
			}else{
				document.write('<option value = "' + i + '">' + str_day + '</option>\n');
			}
		}	
	}
	document.write(selectdayend);
	document.write(selectmonth);
	

	for(i=0; i<12; i++){
		monthValue = parseInt(todayMonth) + parseInt(i);
		if(monthValue > 12){
			monthValue = parseInt(monthValue) % 12;
			flag++;
			if(flag == 1){
				gYear = parseInt(gYear)+1;				
			}	
		}
		if(i == 1){
			fYear = gYear;
		}
		monthName = aryMonth[monthValue];
		if(monthValue == gMonth){
			document.write('<option value = "' + monthValue + '" selected>' + monthName + " " + gYear + '</option>\n');
		}else{
			document.write('<option value = "' + monthValue + '">' + monthName + " " + gYear + '</option>\n');
		}
	}	

	document.write(selectmonthend);
	if(mode == 0){
		selectyear = '<input name="yearfrom" type="hidden" id="yearfrom" value="'+ fYear +'">';
	}else{
		selectyear = '<input name="yearend" type="hidden" id="yearend" value="'+ fYear +'">';
	}
	document.write(selectyear);
	
}