var startCal;
var endCal;

function isValidDate(val) {
	var strExp;
	return (val.search(/^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/gi) != -1)
}

function dateCompare(iDay1,iMonth1,iYear1,iDay2,iMonth2,iYear2) {
	iDay1 = parseInt(iDay1,10);
	iMonth1 = parseInt(iMonth1,10);
	iYear1 = parseInt(iYear1,10);
	
	iDay2 = parseInt(iDay2,10);
	iMonth2 = parseInt(iMonth2,10);
	iYear2 = parseInt(iYear2,10);
	
	var result = 0;
	if (iYear1 > iYear2) {
		result = 1;
	}else if (iYear1 < iYear2) {
		result = 2;
	}else {
		if (iMonth1 > iMonth2) {
			result = 1;
		}else if (iMonth1 < iMonth2) {
			result = 2;
		}else {
			if (iDay1 > iDay2) {
				result = 1;
			}else if (iDay1 < iDay2){
				result =  2;
			}else {
				result = 3;
			}
		}
	}
	return result;
}

String.prototype.left = function(length) {
	return this.substring(0,length);
}

String.prototype.right = function(length) {
	return this.substring(this.length - length,this.length);
}

function CalendarLoad() {
	if (typeof Calendar == "function") {
		var start = new Date();
		var end = start.valueOf() + (Calendar.OneDay * 365);
		
		var config = new Calendar.Config();
		config.startDate = start;
		config.endDate = new Date(end);
		config.className = 'calendar';
		config.width = 180;
		config.displayPosition = 'top';
	
		config.selectDate = new Date();
		
		config.autoHide = false;
		config.closeOnSelect = true;
		config.callBack = function (iDay,iMonth,iYear) {
							selectStartDate(iDay,iMonth,iYear);
						};
		
		startCal = new Calendar('startCal',config);
		
		var endconfig = new Calendar.Config();
		endconfig.startDate = new Date(start.valueOf()+Calendar.OneDay);
		endconfig.endDate = new Date(end+Calendar.OneDay);
		endconfig.className = 'calendar';
		endconfig.width = 180;
		endconfig.displayPosition = 'top';
	
		endconfig.selectDate = endconfig.startDate;
		
		endconfig.autoHide = false;
		endconfig.closeOnSelect = true;
		endconfig.callBack = function (iDay,iMonth,iYear) {
							selectEndDate(iDay,iMonth,iYear);
						};
		
		endCal = new Calendar('endCal',endconfig);
	}
}

Calendar.addEvent(window,"load",CalendarLoad);

function selectStartDate(iDay,iMonth,iYear) {
    document.frmBook.txtCheckIn.value = (parseInt(iMonth)+1) + '/' + iDay + '/' + iYear
}

function selectEndDate(iDay,iMonth,iYear) {
    document.frmBook.txtCheckout.value = (parseInt(iMonth)+1) + '/' + iDay + '/' + iYear
}

function submitBooking(frm) {
	var arrival,departure;
	var arrStartMY = frm.txtCheckIn.value.split('/');
	var startDay = arrStartMY[1];
	var startMonth = arrStartMY[0];
	var startYear = arrStartMY[2];
	if (isValidDate(startDay + '/' + startMonth + '/' + startYear)) {		
		frm.dateIn.value = startMonth + '/' + startDay + '/' + startYear;
		arrival = new Date(startYear,startMonth,startDay,0,0,0,0);
	}else {
		alert('Arrival date is invalid. Please choose another.');
		return false;
	}
	
	var arrEndMY = frm.txtCheckout.value.split('/');
	var endDay = arrEndMY[1];
	var endMonth = arrEndMY[0];
	var endYear = arrEndMY[2];
	if (isValidDate(endDay + '/' + endMonth + '/' + endYear)) {		
		frm.dateOut.value = endMonth + '/' + endDay + '/' + endYear;
		departure = new Date(endYear,endMonth,endDay,0,0,0,0);
	}else {
		alert('Departure date is invalid. Please choose another.');
		return false;
	}
	if (dateCompare(startDay,startMonth,startYear,endDay,endMonth,endYear) != 2) {
		alert('Departure date is invalid. Please choose another.');
		return false;
	}
	frm.Length.value = (departure.valueOf() - arrival.valueOf())/86400/1000;

	return (true);
}

function newWindow(theURL,winName,width,height) {
  var newWin = window.open(theURL,winName,'scrollbars=yes,width=' + width + ',height=' + height + ',resizable=yes,location=no,menubar=yes');
  newWin.focus();
  return false;
}

function openMap(url) {
	newWindow(url,'digitalmap',780,495);
}