Date.prototype.getMonthName = function() {
   return ['ene','feb','mar','abr','may','jun','jul','ago','sep','oct','nov','dic'][this.getMonth()]; 
}

Date.prototype.daysInMonth = function () {
   return new Date(this.getFullYear(), this.getMonth()+1, 0).getDate();
}

var today = new Date(); 
var tomorrow = new Date();  
tomorrow.setTime(today.getTime() + 24*1000*60*60);
var months_ahead = 24;
var showCal = true;
var title = "Reservas On-line";
var showGuests = true;
var button_text="Buscar";
var form_action = 'http://www.mariterrahotels.com/mitBook.asp';

if ( typeof(showCal) !== 'undefined' && showCal === true ) {
	calendar = new Object();
	cv = new Object();
	cv.nextMonth = "Next month";
	cv.prevMonth = "Previous month";
	cv.closeCalendar = "Close";
	cv.iconpath = "http://bookassist.com/scripts/calendar/";
}
var months = ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];
var days = ['lun','mar','miŽ','jue','vie','s‡b','dom'];  
var days_long = ['lun','mar','miŽ','jue','vie','s‡b','dom'];	
var hotelSelectAndGuideIds;
var hotelNames = [
'Hotel Ampuria Inn',
'Hotel Hostal de La Gloria',
'Hotel Miramar',
'Hotel Sant Roc',
'Chateaux de Bessonies',
'Hotel Era Conte',
'Hotel Vall de Bas',
'Hotel Ronda',
'Hotel El Lagar del Vero',
'Hosteria de Guara',
'Hotel sa Punta de s´estanyol',
'Hotel Masferran',
'Aparthotel Vent de Garbí',
'Hotel Restaurant del Mar'];

var hotelIds = [
212,
235,
153,
343,
1146,
1141,
344,
348,
1346,
1342,
1378,
412,
413,
442];

var guideIds = [
103,	
103,
103,
103,
600,
102,
103,
103,
102,
102,
102,
103,
103,
103];
	

if ( typeof(showCal) !== 'undefined' && showCal === true ) {
	document.write('<scri'+'pt type="text/javascript" src="http://www.bookassist.com/scripts/calendar/cal.js"></scr'+'ipt>');
}
document.write('<div id="bookassist_form">');
document.write('<form name="DatesForm" id="DatesForm" method="get" action="' + form_action);
if ( typeof(target) === 'string' ) {
	document.write('" target="' + target);
}
document.write('">');
document.write('		<span class="search_title">'+title+'</span>');		
document.write(getHotelSelect());
document.write('  		<label class="arrival">Llegada:</label>');
document.write(getDaySelect('inday', today, true));
document.write(getMonthSelect('inmonthyear', today, true, 'inday'));
if ( typeof(showCal) !== 'undefined' && showCal === true ) {
	document.write('<a onclick="popCal(this, \'calendar\', \'in\');" class="callink"><img src="http://www.bookassist.com/scripts/calendar/minical2.gif" width="16" height="18" id="arr_cal_icon" alt="calendar" title="Open calendar" /></a>');
}
document.write('  		<label class="departure">Salida:</label>');
document.write(getDaySelect('outday', tomorrow, false));
document.write(getMonthSelect('outmonthyear', tomorrow, false, 'outday'));
if ( typeof(showCal) !== 'undefined' && showCal === true ) {
	document.write('<a onclick="popCal(this, \'calendar\', \'out\');" class="callink"><img src="http://www.bookassist.com/scripts/calendar/minical2.gif" width="16" height="18" id="dep_cal_icon" alt="calendar" title="Open calendar" /></a>');
}
if(showGuests) {
	document.write('<label class="adults_children" >Adultos/Ni&ntilde;o(s):</label>');
	document.write( getAdultSelect());
	document.write( getChildSelect());
}
document.write('	<input type="hidden" name="guide_id" value="102">');
document.write('	<input type="hidden" name="user_language" value="es">');
document.write('	<input class="date_input" value="'+button_text+'" type="button" onClick="submitIfHotelSelected()">');
document.write('</form>');
document.write('</div>');

document.write('<div id="calendar" class="nav"></div>');

function setDayMonth(year,month,day,type) {
setMonth(year, month, type);
setDay(type + 'monthyear', type + 'day', day);
if ( type == "in" ) setOutDayMonth();
}

function setOutDayMonth() {
var inYearMonthSelected = document.getElementById("inmonthyear").options[document.getElementById("inmonthyear").selectedIndex].value.split("-");
var inDaySelected = document.getElementById("inday").options[document.getElementById("inday").selectedIndex].value;
var outYearMonthSelected = document.getElementById("outmonthyear").options[document.getElementById("outmonthyear").selectedIndex].value.split("-");
var outDaySelected = document.getElementById("outday").options[document.getElementById("outday").selectedIndex].value;
var currInDate = new Date(inYearMonthSelected[0], inYearMonthSelected[1] - 1, inDaySelected);
var currOutDate = new Date(outYearMonthSelected[0], outYearMonthSelected[1] - 1, outDaySelected);
if ( currOutDate.getTime() < currInDate.getTime() + 1000*60*60*24 ) {
	currOutDate = new Date(currInDate.getTime() + 1000*60*60*24);
	var currOutMonth = (currOutDate.getMonth() < 9 ? "0" : "") + (currOutDate.getMonth() + 1);
	setMonth(currOutDate.getFullYear(), currOutMonth, "out");
	setDay('outmonthyear', 'outday', currOutDate.getDate());
}
}

function setMonth(year,month,type) {
var ym = year + "-" + month;
var s = document.getElementById(type +"monthyear");
for ( var i=0; i<s.options.length; i++ ) {
	if ( s.options[i].value == ym ) {
		s.selectedIndex = i;
		break;
	}
}
}

function setDay(monthSelectName, daySelectName, day){
var daySelect = document.getElementById(daySelectName);
var selectedDay;
if ( day != undefined ) {
	selectedDay = day;
}
else {
	selectedDay = daySelect.options[daySelect.selectedIndex].value;
}
var inYearMonthSelected = document.getElementById(monthSelectName).options[document.getElementById(monthSelectName).selectedIndex].value.split("-");
daySelect.options.length = 0;
var calDate = new Date(inYearMonthSelected[0], inYearMonthSelected[1] - 1);
var hassel = false;
for( var i=1; i<=calDate.daysInMonth(); i++ ) {
	var issel = i == selectedDay ? true : false;
	hassel = hassel == false ? issel : hassel;
	if ( i == calDate.daysInMonth() && !(hassel) ) {
		issel = true;
	}
	daySelect.options[daySelect.options.length] = new Option(i, i, false, issel);
}
}

function getMonthSelect(selName, date, showOnChange, daySelectName){
var monthSelect = '<select class="date_select" id="'+selName+'" name="'+selName+'"';
if ( showOnChange ) {	
	monthSelect = monthSelect +' onChange="setDay(\'' + selName + '\',\'' + daySelectName + '\');setOutDayMonth()"';
}
else {
	monthSelect = monthSelect +' onChange="setDay(\'' + selName + '\',\'' + daySelectName + '\')"';
}
monthSelect = monthSelect + '>';
var calDate = new Date(date.getTime());
calDate.setDate(1);
for ( var i=1; i<=24; i++ ) {
	monthSelect += '<option value="' + calDate.getFullYear() + '-' + (calDate.getMonth() < 9 ? '0' : '') + (calDate.getMonth() + 1) + '"' + ((date.getFullYear() == calDate.getFullYear() && date.getMonth() == calDate.getMonth()) ? ' selected' : '') + '">' + calDate.getMonthName() + ' ' + calDate.getFullYear() + '</option>';
	calDate.setTime(calDate.getTime() + 1000*60*60*24*calDate.daysInMonth());
}
monthSelect = monthSelect + '</select>';
return monthSelect;
}

function getDaySelect(selName, date, showOnChange){
var daySelect = '<select class="date_select"  id="'+selName+'" name="'+selName+'" ';
if ( showOnChange ) {
	daySelect = daySelect + ' onChange="setOutDayMonth()"';
}
daySelect = daySelect + '>';
var calDate = new Date(date.getTime());
for( var i=1; i<=calDate.daysInMonth(); i++ ) {
	daySelect = daySelect + '<option value="' + i + '"' + (date.getDate() == i ? ' selected' : '') + '>' + i + '</option>';
}
daySelect = daySelect + ' </select>';
return daySelect;
}

function getAdultSelect() {
var adultSelect = '<select class="date_select" id="ba_adults" name="adults">' +
			'	<option value="1">1</option>' +
			'	<option value="2" selected>2</option>' +
			'	<option value="3">3</option>' +
			'	<option value="4">4</option>' +
			'	<option value="5">5</option>' +
			'	<option value="6">6</option>' +
			'	<option value="7">7</option>' +
			'	<option value="8">8</option>' +
			'	<option value="9">9</option>' +
			'	<option value="10">10</option>' +
			'	<option value="11">11</option>' +
			'	<option value="12">12</option>' +
			'	<option value="13">13</option>' +
			'	<option value="14">14</option>' +
			'	<option value="15">15</option>' +
			'	<option value="16">16</option>' +
			'	<option value="17">17</option>' +
			'	<option value="18">18</option>' +
			'	<option value="19">19</option>' +
			'	<option value="20">20</option>' +
			'</select>';
return adultSelect;
}
			
function getChildSelect() {
var childSelect = '<select class="date_select" id="ba_children" name="children" >' +
		  '    <option value="0" selected>0</option>' +
		  '    <option value="1">1</option>' +
		  '    <option value="2">2</option>' +
		  '    <option value="3">3</option>' +
		  '    <option value="4">4</option>' +
		  '    <option value="5">5</option>' +
		  '    <option value="6">6</option>' +
		  '    <option value="7">7</option>' +
		  '    <option value="8">8</option>' +
		  '    <option value="9">9</option>' +
		  '    <option value="10">10</option>' +
		  '    <option value="11">11</option>' +
		  '    <option value="12">12</option>' +
		  '    <option value="13">13</option>' +
		  '    <option value="14">14</option>' +
		  '    <option value="15">15</option>' +
		  '    <option value="16">16</option>' +
		  '    <option value="17">17</option>' +
		  '    <option value="18">18</option>' +
		  '    <option value="19">19</option>' +
		  '    <option value="20">20</option>' +
		  '</select>';
return childSelect;
}

function getHotelSelect(){
var hotelSelect = '<label>Escoja un hotel</label> <select class="date_select" id="hotel_select" name="hotel_id" onChange="setGuideIdForHotel(this)">';
	hotelSelect = hotelSelect + '<option value="">Seleccionar...</option>';
for( var i=0; i<hotelIds.length; i++ ) {
	hotelSelect = hotelSelect + '<option value="' + hotelIds[i] + '">' + hotelNames[i] + '</option>';
}
hotelSelect = hotelSelect + '</select>';
return hotelSelect;
}

function setGuideIdForHotel(hotelSelect) {
document.DatesForm.guide_id.value= guideIds[hotelSelect.selectedIndex-1];
}
function submitIfHotelSelected(){
	if(document.DatesForm.hotel_id.selectedIndex>0) {
		document.DatesForm.submit();
	}
}