// JavaScript Document
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


//          Detect Reservation Form Function
//Date function

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please select a valid day")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please select a valid month")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please select a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please select a valid date")
		return false
	}
	
	return true
}


/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str){
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox){
	while (textBox.value.length > 0 && isNaN(textBox.value)){
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

//Function format day
function getday(day){
	var day = eval(day);
	if(day <= 9){
		day = "0" + day;
	}
	return day;
}

//Function get sort month name by number of month
function getsortmonthname(month){
	var month = eval(month);
	if (month == 1)
    	month = "Jan";
	if (month == 2)
        month = "Feb";
	if (month  == 3)
        month = "Mar";
	if (month  == 4)
        month = "Apr";
	if (month  == 5)
        month = "May";
	if (month  == 6)
        month = "Jun";
	if (month == 7)
        month = "Jul";
	if (month  == 8)
        month = "Aug";
	if (month  == 9)
        month = "Sep";
	if (month  == 10)
        month = "Oct";
	if (month == 11)
        month = "Nov";
	if (month  == 12)
        month = "Dec";
	return month;
}

//Function get month name by number of month
function getmonthname(month){
	var month = eval(month);
	if (month == 1)
    	month = "January";
	if (month == 2)
        month = "February";
	if (month  == 3)
        month = "March";
	if (month  == 4)
        month = "April";
	if (month  == 5)
        month = "May";
	if (month  == 6)
        month = "June";
	if (month == 7)
        month = "July";
	if (month  == 8)
        month = "August";
	if (month  == 9)
        month = "September";
	if (month  == 10)
        month = "October";
	if (month == 11)
        month = "November";
	if (month  == 12)
        month = "December";
	return month;
}

//Function get Day in month list
function daylist(tday){
	var tday = eval(tday);
	for(var i=1; i<=31; i++){
		var selected = (i==tday)? "SELECTED":"";
		document.write("<option value='" + getday(i) + "' " + selected + ">" + getday(i) + "</option>");
	}
}

//Function get Month in year list
function monthlist(smonth){
	var smonth = eval(smonth);
	for(var i=1; i<=12; i++){
		var selected = (i==smonth)? "SELECTED":"";
		document.write("<option value='" + getday(i) + "' " + selected + ">" + getmonthname(i) + "</option>");
	}
}

//Function get Year list
function yearlist(year){
	var year = eval(year);
	
	var curentDate = new Date();
//	var tday = curentDate.getDate();
//	var smonth = curentDate.getMonth()+1;
	var startyear = curentDate.getFullYear();
	
	for(var y=startyear; y<=2023; y++){
		var selected = (y==year)? "SELECTED":"";
		document.write("<option value='" + y + "' " + selected + ">" +  y + "</option>");
	}
}

//Function get Month, Year list
function monthyearlist(year, month){
	var year = eval(year);
	var month = eval(month);
	
	var curentDate = new Date();
	var tday = curentDate.getDate();
	var smonth = curentDate.getMonth()+1;
	var fyear = curentDate.getFullYear();
	
	var startyear = fyear;//fyear-1;
	var minusval = 12-smonth;
	
	for(var y=startyear; y<=(fyear); y++){ //fyear+2
		startmonth = (y==startyear)? smonth:1;
		endmonth = (y==(startyear+2))? (12-minusval):12;
		
		for(var m=startmonth; m<=endmonth; m++){
			var selected = ((y==year) && (m==month))? "SELECTED":"";
			document.write("<option value='" + y + "-" + getday(m) + "' " + selected + ">" + getsortmonthname(m) + " " + y + "</option>");
		}
	}
	if(smonth>1){
		for(var y=fyear+1; y<=fyear+1; y++){ //fyear+2
			for(var m=1; m<smonth; m++){
				var selected = ((y==year) && (m==month))? "SELECTED":"";
				document.write("<option value='" + y + "-" + getday(m) + "' " + selected + ">" + getsortmonthname(m) + " " + y + "</option>");
			}
		}
	}
}

//Function formart checkout date
function formartdate(form) {
	for (var i = 0; i < form.arr_y.length; i++) {
      if (form.arr_y.options[i].selected)
        var fyear = form.arr_y.options[i].text;
    }
	
	var checkinDate = new Date(fyear,form.arr_m.selectedIndex,form.arr_d.selectedIndex+1);
	
	var checkoutDate = new Date(checkinDate.getTime()+(1000*60*60*24*1));
	var out_d = checkoutDate.getDate();
	var out_m = checkoutDate.getMonth();
	var out_y = checkoutDate.getFullYear();
	
    form.dep_d.options[out_d-1].selected=1;
	form.dep_m.options[out_m].selected=1;
	for (var i = 0; i < form.dep_y.length; i++){		
      	if(form.dep_y.options[i].text == out_y){
			form.dep_y.options[i].selected=1;
		}
    }	
}

//Weekday Array
var wdArray = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")

//Set day in week of arrival and departure date
function setWkd(form){
//	var form = document.frmtourbooking;
	var dep_dV = form.dep_d.value;
    var checkinDate = new Date(form.arr_y.value, (form.arr_m.value-1), form.arr_d.value);
	if(dep_dV != ""){
	    var checkoutDate = new Date(form.dep_y.value, (form.dep_m.value-1), form.dep_d.value);
	}

    form.inWd.value = "(" + wdArray[checkinDate.getDay()] + ")";
    if(dep_dV){
		form.outWd.value = "(" + wdArray[checkoutDate.getDay()] + ")";
	}
}

//Function detect arrival and departure date
function checkvaliddate(form){
	// IN Date
    var fyear = form.arr_y.value;
	var inDate = new Date (Date.UTC(fyear,(form.arr_m.value-1),form.arr_d.value));
	var inDtVal = inDate.getTime();
	
	// OUT Date
    var outyear = form.dep_y.value;
	var outDate = new Date (Date.UTC(outyear,(form.dep_m.value-1),form.dep_d.value));
	var outDtVal = outDate.getTime();

	var today = new Date();
	var Cyear = today.getFullYear();
	var Cmonth = today.getMonth();
	var Cday = today.getDate();
	var today = new Date(Date.UTC(Cyear,Cmonth,Cday));
	
	var cdate = today.getTime();//+(1000*60*60*24*1);  //(1000*60*60*24*1);
	
	if (inDtVal < cdate) //(inDtVal <= cdate)
	{
		alert("Check-in date must be from current date!"); //3 day ahead of current date
		form.CheckInD.focus();
		return false;

	}
	else
	{
		var num = Math.round((inDtVal-cdate)/(1000*60*60*24));
		if (num<0)
		{
			alert("Can not make reservation in the past");
			return false;
		}
		else{
			if((outDtVal-inDtVal)<=0){
				alert("Invalid check-out date, please try again");
				form.CheckOutD.focus();
				return false;
			}
			else{
				var length = Math.round((outDtVal-inDtVal)/(1000*60*60*24));
				if(length < 1){
					alert("Please choose a new check-out.");
					form.CheckOutD.focus();
					return false;
				}
				else
					return true;

			}
		}
	}
}

//Function detect arrival date
function checkSelectedDate(month, day, year){
	monthV = month.value;
	dayV = day.value;
	yearV = year.value;
	if(monthV != "" && dayV != "" && yearV != "")
		if(!isDate(monthV+"/"+dayV+"/"+yearV)){
			day.focus();
			return false;
		}
		else return true;
}

//Function calculate the number of night
function CountDays(form, VarLocationNew, DropdownType) {
//	var form = document.frmtourbooking;
	
	//Calculate the check in date
	if (DropdownType == 'CheckInM') {
		form.arr_m.value = VarLocationNew;
	}
	else if (DropdownType == 'CheckInY') { form.arr_y.value = VarLocationNew; }
	else if (DropdownType == 'CheckInD') { form.arr_d.value = VarLocationNew; }
	
	//Calculate the check out date
	if (DropdownType == 'CheckOutM') {
		form.dep_m.value = VarLocationNew;
	}
	else if (DropdownType == 'CheckOutY') { form.dep_y.value = VarLocationNew; }
	else if (DropdownType == 'CheckOutD') { form.dep_d.value = VarLocationNew; }
	
	
	var dep_dV = form.dep_d.value;
	//If value of departure day is not null so we calculate the number of nights
	if(dep_dV != ""){
		if (DropdownType == 'CheckOutY') {
			form.dep_y.value = VarLocationNew;
		}
		else if (DropdownType == 'CheckOutM') { form.dep_m.value = VarLocationNew; }
		else if (DropdownType == 'CheckOutD') { form.dep_d.value = VarLocationNew; }
	
		var min = form.arr_m.value-1;
		var din = form.arr_d.value;
		var yin = form.arr_y.value;
		
		var mout = form.dep_m.value-1;
		var dout = form.dep_d.value;
		var yout = form.dep_y.value;
		
		var h  = 0;
		var n  = 0;
		var s  = 0;
		
		var checkinDate  = new Date(yin,min,din,h,n,s);
		var checkoutDate = new Date(yout,mout,dout,h,n,s);
	
		checkinTime  = checkinDate.getTime();
		checkoutTime = checkoutDate.getTime();
		TotDaysMillisec = checkoutTime - checkinTime;
		if (TotDaysMillisec >0) {
			form.nonights.value = Math.round(TotDaysMillisec / (1000 * 60 * 60 *24));
		} else {
			form.nonights.value = 0;
		}
	}
}

//Function detect form hotel booking
function detectfrmhotelbook(frm){
/*	alert(frm.Name.value);
	alert(frm.Email.value);
	return false;
*/
	var roomType = frm.Room;
	var bedType = frm.BedType;
	
	var roomChecked = false;
	var bedChecked = false;
	
	for(var r=0; r<roomType.length; r++){
		if(roomType[r].checked){
			roomChecked = true;
			break;
		}
	}
	
	for(var b=0; b<bedType.length; b++){
		if(bedType[b].checked){
			bedChecked = true;
			break;
		}
	}
	
	if(frm.Name.value == ""){
		alert("Please enter your full name!");
		frm.Name.focus();
		return false;
	}
	else
		if(frm.Email.value == ""){
			alert("Please enter your email address!");
			frm.Email.focus();
			return false;
		}
		else
			if(frm.Email.value.indexOf ('@',0) == -1 || frm.Email.value.indexOf ('.',0) == -1) {
				alert ("The Email field requires a \"@\" and a \".\" be used. \n\nPlease re-enter your Email address.");
				frm.Email.select();
				frm.Email.focus();
				return false;
			}
			else
				if(frm.NoGuests.value=="" || frm.NoGuests.value<1){
					alert("Please enter number of Guest(s)");
					frm.NoGuests.select();
					frm.NoGuests.focus();
					return false;
				}
				else
					if(frm.NoRooms.value=="" || frm.NoRooms.value<1){
						alert("Please enter number of Room(s)");
						frm.NoRooms.select();
						frm.NoRooms.focus();
						return false;
					}
					else
						if(!roomChecked){
							alert("Please select room type");
							roomType[0].focus();
							return false;
						}
						else
							if(!bedChecked){
								alert("Please select bed type");
								bedType[0].focus();
								return false;
							}
							else
								if(!checkvaliddate(frm)){
									return false;
								}
								else return true;
}

//Function detect form tour booking
function detectfrmtourbook(frm){
	var roomType = frm.Room;
	var packageType = frm.Package;
	
	var roomChecked = false;
	var packageChecked = false;
	
	for(var r=0; r<roomType.length; r++){
		if(roomType[r].checked){
			roomChecked = true;
			break;
		}
	}
	
	for(var b=0; b<packageType.length; b++){
		if(packageType[b].checked){
			packageChecked = true;
			break;
		}
	}
	
	if(frm.Name.value == ""){
		alert("Please enter your full name!");
		frm.Name.focus();
		return false;
	}
	else
		if(frm.Email.value == ""){
			alert("Please enter your email address!");
			frm.Email.focus();
			return false;
		}
		else
			if(frm.Email.value.indexOf ('@',0) == -1 || frm.Email.value.indexOf ('.',0) == -1) {
				alert ("The Email field requires a \"@\" and a \".\" be used. \n\nPlease re-enter your Email address.");
				frm.Email.select();
				frm.Email.focus();
				return false;
			}
			else
				if(frm.NoGuests.value=="" || frm.NoGuests.value<1){
					alert("Please enter number of Guest(s)");
					frm.NoGuests.select();
					frm.NoGuests.focus();
					return false;
				}
				else
					if(frm.NoRooms.value=="" || frm.NoRooms.value<1){
						alert("Please enter number of Room(s)");
						frm.NoRooms.select();
						frm.NoRooms.focus();
						return false;
					}
					else
						if(!roomChecked){
							alert("Please select room type");
							roomType[0].focus();
							return false;
						}
						else
							if(!packageChecked){
								alert("Please select pcakge type");
								packageType[0].focus();
								return false;
							}
							else
								if(!checkvaliddate(frm)){
									return false;
								}
								else return true;
}
//End