var startCal;

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)
}

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.selectDate = new Date();
		
		config.autoHide = false;
		config.closeOnSelect = true;
		config.callBack = function (iDay,iMonth,iYear) {
							selectDate(iDay,iMonth,iYear);
						};
		
		startCal = new Calendar('startCal',config);
	}
}

Calendar.addEvent(window,"load",CalendarLoad);

function selectDate(iDay,iMonth,iYear) {
    document.frmBook.cmbDay.options[iDay - 1].selected = true;
	++iMonth;
	for (var i = 0;i < document.frmBook.cmbMonthYear.options.length;i++) {
		//alert(parseInt(document.frmBook.cmbYear.options[i].value) + 2000);
		var selectMonthYear = iMonth + '/' + iYear;

	    if (document.frmBook.cmbMonthYear.options[i].value == selectMonthYear) {
			document.frmBook.cmbMonthYear.options[i].selected = true;
		}
	}
}

function submitBooking(frm) {
	if (isValidDate(frm.cmbDay.options[frm.cmbDay.selectedIndex].value + '/' + frm.cmbMonthYear.options[frm.cmbMonthYear.selectedIndex].value)) {
		var arrMY = frm.cmbMonthYear.options[frm.cmbMonthYear.selectedIndex].value.split('/');
		
		var year = arrMY[1];
		var month = arrMY[0];
		
		if (frm.action.search(/accorhotels\.com/gi) != -1) {
			frm.jour_arrivee.value = frm.cmbDay.options[frm.cmbDay.selectedIndex].value;
			frm.mois_arrivee.value = month;
			frm.annee_arrivee.value = year;
		} else {
			frm.date.value = frm.cmbDay.options[frm.cmbDay.selectedIndex].value + '/' + frm.cmbMonthYear.options[frm.cmbMonthYear.selectedIndex].value;
		}

		return (true);
	}else {
		alert('Arrival date is invalid. Please choose another.');
	
		return false;
	}
}