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('index.html');
		
		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;
	}
}

var isun = 'Sun';
var imon = 'Mon';
var itue = 'Tue';
var iwed = 'Wed';
var ithu = 'Thu';
var ifri = 'Fri';
var isat = 'Sat';
// Calendar Close button
var icalendarclose = 'CLOSE';

// Loading message
var iLoading = 'One moment please,<br/>your request is being processed';

// Not Available
var inotavailable = 'Not available';
var irateinclusions = 'What’s included in this package?';
var irateconditions = 'Rate Conditions';
var iresultnotfound = 'There are no results that match your search.';

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 OpenCondition(i)
{
    newWindow(bokingsiteurl + '/reserve/conditions.aspx?hotelcode=' + curHotelCode + '&rateid=' + arrRate[i][4] + '&periodid=' + arrRate[i][5],'RateCondition',660,685);
}
function builRateGrid() {
    var str = '';
    var strnum = '';
    var count = 0;
    var row = new Array();
    var col = new Array();
    
	if (arrRate) {
		if (arrRate.length > 0)
		{
			for (var i = 0;i < arrRate.length;i++)
			{			
				if ((hotelBrand != "IBI") || (i != 0)) {			
					str += '<table border="0" cellspacing="0" cellpadding="0" class="raterow"><tr><td width="260">';
					str += '<div class="leftbox">';
					str += '<div class="ratename">' + arrRate[i][1];
					if (arrRate[i][6])
						str += '<div class="inclusion"><a href="javascript:OpenCondition(' + i + ')">' + irateconditions + '</a></div>';
					str += '</div>';
					str += '<div class="condition"><a href="javascript:OpenCondition(' + i + ')">' + irateconditions + '</a></div>';
					str += '</div>'
					str += '<div class="rightbox">';
					str += '<div class="currency">' + arrRate[i][3] + '</div>';
					str += '</div>'
					str += '</td>';
					str += '<td valign="top">';
					str += '<div class="leftline"></div>';
					for (var j = 0;j < 4;j++)
					{
						strnum = (i+1) + '' + (j+1);
						if (arrAvailable[i][j] && ((arrRate[i][4] != 8) || (arrDAF[j] > 0))) {
	
							str += '<div class="rate">';
							str += '<label id="lbl' + strnum + '" for="chk' + strnum + '">' + arrPrice[i][j] + arrTaxSign[j];
							if (arrMLOS[i][j] > 1)
								str += '*';
							str += '</label>';
						} else {
							str += '<div class="rateDis">';
							if (arrIsClosed[i][j] && ((arrRate[i][4] != 8) || (arrDAF[j] > 0))) {
								if (arrRate[i][4] != 8)
									str += '<label>' + arrPrice[i][j] + arrTaxSign[j] + '</label>';
								else
									str += '<label>' + inotavailable + '</label>';
							} else
								str += '<label>' + inotavailable + '</label>';
						}
						str += '</div>';
					}
					str += '</td>';
					str += '</tr>';
					str += '</table><div class="clear"></div>\n';
				}
			}
		}
	}else {
		str = '<p>' + iresultnotfound + '</p>';
	}
    
    document.getElementById("DateBox").innerHTML = str;
}