/**
 *	period_public_periods:  global variable that contains all periods data, including start and end dates, type and days number
 *	period_public_calendar: global variable that contains one year calendar structure, including periods info day by day
 *	period_public_days_divs: this contains all days divs to make a quick access to its contents
 *		(instead of look into all document divs)
 *	period_public_previous_day: this stores the previous day calendar info to know if this is the first or last day into a period
 */
var period_public_periods		= null;
var period_public_calendar		= null;
var period_public_days_divs		= null;
var period_public_previous_day	= null;
var titleBox					= null;

/**
 *	This loads periods info, creates calendar structure, joins into periods info and draws the calendar tables into the module
 */
function periodsPublicInitPeriodsDrawing() {

	//Initializes titleBox container
	titleBox				= new c_FloatingTitleBox();

	//Periods JS structure
	period_public_periods	= new Array();

	//reads and sort periods data
	periodsPublicReadPeriods();
	periodsPublicSortPeriods();

	//joins period_public_calendar data with period_public_periods data
	periodsPublicJoinCalendarPeriods();

	//draws periods on its container
	periodsPublicDrawCalendar();
}


/**
 * Returns the sum of the days to the passed month (included)
 */
function periodsPublicMonthsSumDays( month, year ) {

	month		= parseInt(month,10);
	year		= parseInt(year,10);

	var auxDate	= new c_MyDate('01/01/'+parseInt(year, 10));
	var sum		= 0;

	for (var n=1;n<month+1;n++) {
		sum += auxDate.month_days[n];
	}
	return sum;
}


/**
 * Returns the complete passed months for a number of days
 */
function periodsPublicMonthsPassed( value, year ) {

	value			= parseInt(value,10);
	year			= parseInt(year,10);

	var auxDate		= new c_MyDate('01/01/'+parseInt(year, 10));
	var month_count	= 0;
	var n			= 1;
	while (value>auxDate.month_days[n] && n<13) {
		value -= auxDate.month_days[n];
		month_count++;
		n++;
	}
	return month_count;
}


/**
 * Returns year day number of a passed date (dd/mm/aaaa)
 */
function periodsPublicDate2Number( date ) {

	var day		= parseInt(date.charAt(0) + date.charAt(1), 10);
	var month	= parseInt(date.charAt(3) + date.charAt(4), 10);
	var year	= parseInt(date.charAt(6) + date.charAt(7) + date.charAt(8) + date.charAt(9), 10);

	return periodsPublicMonthsSumDays(month-1, year)+day;
}


/**
 * Returns the day and month string (dd/mm) for a passed numeric year day
 */
function periodsPublicNumber2PeriodDate( value, year ) {

	if (!year) {
		var year	= new c_MyDate().year;
	}

	value			= (parseInt(value,10)>365 ? parseInt(value,10)-365 : parseInt(value,10));
	year			= parseInt(year,10);

	var auxDate		= new c_MyDate('01/01/'+parseInt(year, 10));
	var month		= periodsPublicMonthsPassed(value, year);
	var day			= value - periodsPublicMonthsSumDays(month, year);

	if (day==0)
		day			= auxDate.month_days[month-1];
	else
		month++;

	return (day<10 ? '0'+day : day) + '/' + (month<10 ? '0'+month : month);
}


/**
 *	Reads the periods info from DDBB in the template to a JS structure
 *	A period into the template has the next order:
 *		0: id			: id_period
 *		1: type			: type
 *		2: num_days		: num_days
 *		3: start_date	: start_date
 *		4: end_date		: end_date
 *		5: selected		: selected flag (default false)
 *		5: onMouseOver	: onMouseOver flag (default false)
 */
function periodsPublicReadPeriods() {

	//reset array
	period_public_periods = new Array();

	//gets data structure
	var periods_container = document.getElementById('periods_data');

	for (var n=0;n<periods_container.childNodes.length;n++) {
		if (periods_container.childNodes[n].id && periods_container.childNodes[n].id.indexOf('period')!=-1) {
			var period		= periods_container.childNodes[n];

			var counter		= 0;
			var attributes	= new Array('id', 'type', 'num_days', 'start_date', 'end_date', 'selected', 'onMouseOver');

			period_public_periods[period_public_periods.length] = new Array();

			for (var m=0;m<period.childNodes.length;m++) {
				if (period.childNodes[m].id && period.childNodes[m].id.indexOf('period_attrib')!=-1) {
					var period_attrib	= period.childNodes[m];
					period_public_periods[period_public_periods.length-1][attributes[counter]]  = period_attrib.innerHTML;
					counter++;
				}
			}
			//selected flag
			period_public_periods[period_public_periods.length-1].selected = false;

			//onMouseOver flag
			period_public_periods[period_public_periods.length-1].onMouseOver = false;
		}
	}
}


/**
 *	Sorts the periods JS structure by start_date
 */
function periodsPublicSortPeriods() {

	for (var n=0;n<period_public_periods.length-1;n++) {
		for (var m=0;m<period_public_periods.length-1;m++) {
			if ( parseInt(period_public_periods[m].start_date,10) > parseInt(period_public_periods[m+1].start_date,10) ) {
				var aux = period_public_periods[m];
				period_public_periods[m]	= period_public_periods[m+1];
				period_public_periods[m+1]	= aux;
			}
		}
	}
}


/**
 *	Joins the calendar structure with periods data structure
 *		0: day				: day
 *		1: month			: month
 *		2: year				: year
 *		3: periodId			: periodId
 *		4: periodType		: periodType
 *		5: periodNumDays	: periodNumDays
 *		6: periodStartDate	: periodStartDate
 *		7: periodEndDate	: periodEndDate
 *		8: current			: current
 */
function periodsPublicJoinCalendarPeriods() {

	var date	= new c_MyDate();

	//This creates or resets the JS one year calendar structure
	period_public_calendar	= new c_PeriodYear();

	//for each period
	for (var n=0;n<period_public_periods.length;n++) {
		var aux_start_date		= parseInt(period_public_periods[n].start_date, 10);
		var aux_end_date		= parseInt(period_public_periods[n].end_date, 10);

		var period_length		= 0;
		if (aux_end_date>=aux_start_date) {
			period_length		= aux_end_date - aux_start_date + 1;
		} else {
			//it needs know if current year is a leap one
			var aux_date		= new c_MyDate();

			if (aux_date.month_days[2]=='28')
				period_length	= (aux_end_date + 365) - aux_start_date + 1;
			else
				period_length	= (aux_end_date + 366) - aux_start_date + 1;
		}

		var start_date		= parseInt(periodsPublicNumber2PeriodDate(period_public_periods[n].start_date,date.year).split('/')[0],10);
		var start_month		= parseInt(periodsPublicNumber2PeriodDate(period_public_periods[n].start_date,date.year).split('/')[1],10);
		var current_day		= start_date;
		var current_month	= start_month;

		//for each day into the period
		for (var m=0;m<period_length-1;m++) {
			if (current_day>period_public_calendar.months[0].date.month_days[current_month]) {
			//period arrives to next month, day must be setted to 1 and month to the next one
				current_day		= 1;
				current_month	= (current_month<12 ? current_month + 1 : 1);
			}

			//gets calendar day and sets its period information properties
			period_public_calendar.getDay(current_day, current_month).setPeriod(
				period_public_periods[n].id,
				period_public_periods[n].type,
				period_public_periods[n].num_days,
				period_public_periods[n].start_date,
				period_public_periods[n].end_date );
			current_day++;
		}
	}
}


/**
 *	This returns a period Array if it is found, false else
 */
function periodsPublicSearchPeriodsById( period_id ) {

	for (var n=0;n<period_public_periods.length;n++) {
		if (period_public_periods[n].id==period_id) {
			return period_public_periods[n];
		}
	}
	return false;
}


/**
 *
 */
function periodsPublicResetMonthsContainer() {

	for (var n=1;n<13;n++) {
		var month_div		= document.getElementById('periods_public_calendar_cell_'+n);
		month_div.innerHTML	= '';
	}
}


/**
 *	This switches monthDay and periodNumDays and its codes. InnerHTML value depends on code
 *	Div ID is formed by:
 *		periodDayDiv:periodId:code('D'=day or 'N'=numDays):periodDay/periodNumDays:periodMonth
 */
function periodsPublicSwitchDayContent( divHandle, code ) {

	var parts			= divHandle.id.split(':');
	var aux				= divHandle.innerHTML;

	if (parts[2]=='D') {
		if (code=='D') {
			divHandle.innerHTML	= parts[3];
			divHandle.id		= parts[0] + ':' + parts[1] + ':' + 'N' + ':' + aux + ':' + parts[4] + ':' + parts[5];;

			divHandle.className	= 'periods_public_calendar_cell_highlight';
		} else if (code=='N') {
			//no changes needed
		}
	} else if (parts[2]=='N') {
		if (code=='D') {
			//no changes needed
		} else if (code=='N') {
			divHandle.innerHTML	= parts[3];
			divHandle.id		= parts[0] + ':' + parts[1] + ':' + 'D' + ':' + aux + ':' + parts[4] + ':' + parts[5];

			divHandle.className	= 'periods_public_calendar_cell_period';

		}
	}
}


/**
 *	This removes selected classNames from an single calendar day
 */
function restoreCalendarDay( div_handle ) {

	div_handle.className				= div_handle.startClass;
	div_handle.pair.className			= div_handle.pair.startClass;

	if (div_handle.id.indexOf('periodDayDiv')!=-1) {
		if (div_handle.className.indexOf('periods_public_calendar_cell_busy')==-1 && div_handle.className.indexOf('periods_public_calendar_cell_close_season')==-1) {
			div_handle.onmouseout		= periodPublicDayOnMouseOut;
			div_handle.onmouseover		= periodPublicDayOnMouseOver;
		}
		if (div_handle.pair.className.indexOf('periods_public_calendar_cell_busy')==-1 && div_handle.pair.className.indexOf('periods_public_calendar_cell_close_season')==-1) {
			div_handle.pair.onmouseout	= periodPublicDayOnMouseOut;
			div_handle.pair.onmouseover	= periodPublicDayOnMouseOver;
		}
	} else if (div_handle.className.indexOf('periods_public_calendar_cell_busy')==-1 && div_handle.className.indexOf('periods_public_calendar_cell_close_season')==-1) {
		div_handle.onmouseout			= normalPublicDayOnMouseOut;
		div_handle.onmouseover			= normalPublicDayOnMouseOver;
	}
}


/**
 *	This removes selected classNames from calendar days
 */
function restoreCalendarDays() {

	titleBox.hide();

	for (var x=0;x<period_public_days_divs.length;x++) {
		restoreCalendarDay(period_public_days_divs[x]);
	}
}


/**
 *	From start_div sets className for number days
 */
function selectDaysRange( start_div, num_days, direction, class_name ) {

	var counter		= 0;
	var change_div	= false;
	var isFirst		= true;
	var isLast		= true;

	//ascending
	if (direction==1) {
		for (var x=0;x<period_public_days_divs.length*2;x++) {
			//not possible to reserv ocupated days
			if (period_public_days_divs[x%period_public_days_divs.length].innerHTML!='') { //not empty divs
				if (period_public_days_divs[x%period_public_days_divs.length]==start_div || period_public_days_divs[x%period_public_days_divs.length]==start_div.pair) {
					change_div	= true; //set flag to indicate changing divs
				}
				if (change_div) { //if flag, it highlights pressedPeriod.num_days divs
					if (counter<=num_days) {
						if (period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_busy')!=-1
								&& period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_busy')!=-1) {
							restoreCalendarDays();
							document.getElementById('reservations_start_date').value	= '';
							document.getElementById('reservations_end_date').value		= '';
							return false;
						}
						if (period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_close_season')!=-1
								&& period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_close_season')!=-1) {
							restoreCalendarDays();
							document.getElementById('reservations_start_date').value	= '';
							document.getElementById('reservations_end_date').value		= '';
							return false;
						}
						if (isFirst) { //first day
							period_public_days_divs[x%period_public_days_divs.length].className			= class_name + '_right';
							period_public_days_divs[x%period_public_days_divs.length].onmouseover		= null;
							period_public_days_divs[x%period_public_days_divs.length].onmouseout		= null;

							if (period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_busy')==-1
									&& period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_close_season')==-1) {
								period_public_days_divs[x%period_public_days_divs.length].pair.onmouseover	= function() {
									this.className = 'periods_public_calendar_cell_highlight_left';
								}
								period_public_days_divs[x%period_public_days_divs.length].pair.onmouseout	= function() {
									this.className = this.startClass;
								}
							}
							isFirst = false;
						} else if (counter==num_days) { //last day
							period_public_days_divs[x%period_public_days_divs.length].pair.className	= class_name + '_left';
							period_public_days_divs[x%period_public_days_divs.length].pair.onmouseover	= null;
							period_public_days_divs[x%period_public_days_divs.length].pair.onmouseout	= null;

							if (period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_busy')==-1
									&& period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_close_season')==-1) {

								period_public_days_divs[x%period_public_days_divs.length].onmouseover		= function() {
									this.className = 'periods_public_calendar_cell_highlight_right';
								}
								period_public_days_divs[x%period_public_days_divs.length].onmouseout		= function() {
									this.className = this.startClass;
								}
							}
						} else {
							periodsPublicChangePairClass(period_public_days_divs[x%period_public_days_divs.length], class_name);
							period_public_days_divs[x%period_public_days_divs.length].onmouseout		= null;
							period_public_days_divs[x%period_public_days_divs.length].pair.onmouseout	= null;
						}
						counter++;
					}
				}
			}
		}
	} else { //down to
		for (var x=(period_public_days_divs.length*2)-1;x>=0;x--) {
			if (period_public_days_divs[x%period_public_days_divs.length].innerHTML!='') { //not empty divs
				if (period_public_days_divs[x%period_public_days_divs.length]==start_div || period_public_days_divs[x%period_public_days_divs.length]==start_div.pair) {
					change_div	= true; //set flag to indicate changing divs
				}
				if (change_div) { //if flag, it highlights pressedPeriod.num_days divs
					if (counter<num_days) {
						if (period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_busy')!=-1
								&& period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_busy')!=-1) {
							restoreCalendarDays();
							document.getElementById('reservations_start_date').value	= '';
							document.getElementById('reservations_end_date').value		= '';
							return false;
						}
						if (period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_close_season')!=-1
								&& period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_close_season')!=-1) {
							restoreCalendarDays();
							document.getElementById('reservations_start_date').value	= '';
							document.getElementById('reservations_end_date').value		= '';
							return false;
						}
						if (isLast) { //last day
							period_public_days_divs[x%period_public_days_divs.length].pair.className	= class_name + '_left';

							if (period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_busy')==-1
									&& period_public_days_divs[x%period_public_days_divs.length].className.indexOf('periods_public_calendar_cell_close_season')==-1) {
								period_public_days_divs[x%period_public_days_divs.length].onmouseover		= function() {
									this.className = 'periods_public_calendar_cell_highlight_right';
								}
								period_public_days_divs[x%period_public_days_divs.length].onmouseout		= function() {
									this.className = this.startClass;
								}
							}
							period_public_days_divs[x%period_public_days_divs.length].pair.onmouseover	= null;
							period_public_days_divs[x%period_public_days_divs.length].pair.onmouseout	= null;
							isLast = false;
						} else if (counter==num_days-1) { //first day
							period_public_days_divs[x%period_public_days_divs.length].className			= class_name + '_right';
							period_public_days_divs[x%period_public_days_divs.length].onmouseover		= null;
							period_public_days_divs[x%period_public_days_divs.length].onmouseout		= null;

							if (period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_busy')==-1
									&& period_public_days_divs[x%period_public_days_divs.length].pair.className.indexOf('periods_public_calendar_cell_close_season')==-1) {
								period_public_days_divs[x%period_public_days_divs.length].pair.onmouseover	= function() {
									this.className = 'periods_public_calendar_cell_highlight_left';
								}
								period_public_days_divs[x%period_public_days_divs.length].pair.onmouseout	= function() {
									this.className = this.startClass;
								}
							}
						} else {
							periodsPublicChangePairClass(period_public_days_divs[x%period_public_days_divs.length], class_name);
							period_public_days_divs[x%period_public_days_divs.length].onmouseout		= null;
							period_public_days_divs[x%period_public_days_divs.length].pair.onmouseout	= null;
						}
						counter++;
					}
				}
			}
		}
	}
}


/**
 *	This creates a formated date string (dd/mm/aaaa) from day and month (this calculates current year to know leap years)
 */
function createDateString( day, month ) {

	var aux_date			= new c_MyDate();
	var auxDay				= parseInt(day, 10);
	var auxMonth			= parseInt(month, 10);

	//compare current day to selected day to calculate period year
	var cellMonthDay		= parseInt('' + auxMonth + (auxDay<10 ? '0' + auxDay : auxDay), 10);
	var currentMonthDay		= parseInt('' + aux_date.month + (aux_date.day<10 ? '0' + aux_date.day : aux_date.day), 10);
	var auxYear 			= (cellMonthDay<currentMonthDay ? aux_date.year+1 : aux_date.year);

	return new c_MyDate('' + (auxDay<10 ? '0' + auxDay : auxDay)+'/'+(auxMonth<10 ? '0' + auxMonth : auxMonth)+'/'+auxYear).toString();
}


/**
 *	Configures and shows titleBox
 */
function showTitleBox( div_handle, start_date, end_date, num_days ) {

	//show message box
	titleBox.setXPosition(div_handle, -195);
	titleBox.setYPosition(div_handle, -105);
	titleBox.setMessage('Desde el día '+start_date+' al '+end_date+' se deben reservar '+num_days+' noches o más');
	titleBox.show();
}


/**
 *	Changes div and div.pair className to the passed class looking for left and right pairs
 */
function periodsPublicChangePairClass( div_handle, class_name ) {

	if (div_handle.clientWidth<11) {
		if (div_handle.className.indexOf('cell_highlight')==-1 && div_handle.className.indexOf('cell_busy')==-1 && div_handle.className.indexOf('cell_blue')==-1 && div_handle.className.indexOf('cell_close_season')==-1)
			div_handle.className		= class_name + '_left';
		if (div_handle.pair.className.indexOf('cell_highlight')==-1 && div_handle.pair.className.indexOf('cell_busy')==-1 && div_handle.pair.className.indexOf('cell_blue')==-1 && div_handle.pair.className.indexOf('cell_close_season')==-1)
			div_handle.pair.className	= class_name + '_right';
	} else {
		if (div_handle.pair.className.indexOf('cell_highlight')==-1 && div_handle.pair.className.indexOf('cell_busy')==-1 && div_handle.pair.className.indexOf('cell_blue')==-1 && div_handle.pair.className.indexOf('cell_close_season')==-1)
			div_handle.pair.className	= class_name + '_left';
		if (div_handle.className.indexOf('cell_highlight')==-1 && div_handle.className.indexOf('cell_busy')==-1 && div_handle.className.indexOf('cell_blue')==-1 && div_handle.className.indexOf('cell_close_season')==-1)
			div_handle.className		= class_name + '_right';
	}
}


/**
 *	calendar period day cell onMouseOver function
 */
function periodPublicDayOnMouseOver() {

	periodsPublicChangePairClass(this, 'periods_public_calendar_cell_highlight');
}


/**
 *	calendar period day cell onMouseOut function
 */
function periodPublicDayOnMouseOut() {

	if (this.className.indexOf('periods_public_calendar_cell_blue')==-1) {
		this.className		= this.startClass;
		this.pair.className	= this.pair.startClass;
	}
}


/**
 *	calendar period day cell onClick function
 */
function periodPublicDayOnClick() {

	var start_date_input	= document.getElementById('reservations_start_date');
	var end_date_input		= document.getElementById('reservations_end_date');

	var string_date			= createDateString((this.innerHTML!='' ? this.innerHTML : (this.pair.innerHTML!='-' ? this.pair.innerHTML : this.pair.id)), this.id.split(':')[4]);
	var pressedPeriod		= periodsPublicSearchPeriodsById(this.id.split(':')[1]);

	var markPeriod			= true;

	restoreCalendarDays();

	//this is a close period
	if (pressedPeriod.type=='C') {
		//if days number to the end of the period is smaller than period.num_days
		var day				= parseInt(this.id.split(':')[3],10);
		var month			= parseInt(this.id.split(':')[4],10);
		var current_date	= periodsPublicDate2Number(createDateString(day, month));

		if (pressedPeriod.end_date-current_date<pressedPeriod.num_days) {
			//if days number to the end of the period is smaller than period.num_days
			markPeriod		= false;
		}
	}

	if (start_date_input.value!='') {
		if (end_date_input.value=='') {
			var start_date				= new c_MyDate(start_date_input.value);
			if (start_date.smaller(string_date)) {
				end_date_input.value	= string_date;

				var numeric_start_date	= periodsPublicDate2Number(start_date_input.value);
				var numeric_end_date	= periodsPublicDate2Number(end_date_input.value);

				if (numeric_start_date>numeric_end_date) {
					numeric_end_date += 365;
				}

				//this is a close period and reservation starts inside the period and ends outside it
				if (pressedPeriod.type=='C') {
					if (numeric_end_date-pressedPeriod.start_date>=pressedPeriod.num_days) {
						selectDaysRange(this, numeric_end_date-numeric_start_date+1, -1, 'periods_public_calendar_cell_highlight');
					} else {
						start_date_input.value	= '';
						end_date_input.value	= '';
					}
				} else if (numeric_end_date-numeric_start_date>=pressedPeriod.num_days) { //reservation is fully inside the period
					selectDaysRange(this, numeric_end_date-numeric_start_date+1, -1, 'periods_public_calendar_cell_highlight');
				} else {
					start_date_input.value	= '';
					end_date_input.value	= '';
				}
			} else {
				if (markPeriod) { //open period or close period with enough days
					start_date_input.value	= string_date;
					selectDaysRange(this, pressedPeriod.num_days, 1, 'periods_public_calendar_cell_blue');
				}
			}
		} else {
			start_date_input.value	= string_date;
			end_date_input.value	= '';
			periodPublicDayOnClick.call(this);
		}
	} else {
		if (markPeriod) { //open period or close period with enough days
			start_date_input.value	= string_date;

			//looks for the pressed day and month (month is more difficult)
			var auxDay				= parseInt(this.innerHTML, 10);
			var auxMonth			= this.id.split(':')[4];

			selectDaysRange(this, pressedPeriod.num_days, 1, 'periods_public_calendar_cell_blue');
		}
	}

	//show title box
	if (start_date_input.value=='' || end_date_input.value=='') {
		showTitleBox(this,
					 periodsPublicNumber2PeriodDate(pressedPeriod.start_date),
					 periodsPublicNumber2PeriodDate(pressedPeriod.end_date),
					 pressedPeriod.num_days);
	}
}


/**
 *	calendar normal day cell onMouseOver function
 */
function normalPublicDayOnMouseOver() {

	periodsPublicChangePairClass(this, 'periods_public_calendar_cell_highlight');
}


/**
 *	calendar normal day cell onMouseOut function
 */
function normalPublicDayOnMouseOut() {

	this.className		= this.startClass;
	this.pair.className	= this.pair.startClass;
}


/**
 *	calendar normal day cell onClick function
 */
function normalPublicDayOnMouseClick() {

	var start_date_input	= document.getElementById('reservations_start_date');
	var end_date_input		= document.getElementById('reservations_end_date');

	var string_date			= createDateString((this.innerHTML!='' ? this.innerHTML : (this.pair.innerHTML!='-' ? this.pair.innerHTML : this.pair.id)), this.id);

	restoreCalendarDays();

	if (start_date_input.value=='') {
		start_date_input.value		= string_date;
		periodsPublicChangePairClass(this, 'periods_public_calendar_cell_highlight');
		this.onmouseout				= null;
		this.pair.onmouseout		= null;

	} else if (end_date_input.value=='') {
		var start_date				= new c_MyDate(start_date_input.value);
		if (start_date.smaller(string_date)) {
			end_date_input.value	= string_date;

			var numeric_start_date	= periodsPublicDate2Number(start_date_input.value);
			var numeric_end_date	= periodsPublicDate2Number(end_date_input.value);

			if (numeric_start_date>numeric_end_date) {
				numeric_end_date += 365;
			}

			var start_day			= period_public_calendar.getDay(start_date_input.value.charAt(0)
																  + start_date_input.value.charAt(1),
																	start_date_input.value.charAt(3)
																  + start_date_input.value.charAt(4));

			if (start_day.periodNumDays) {

				if (numeric_end_date-numeric_start_date+1>=start_day.periodNumDays) {
					selectDaysRange(this, numeric_end_date-numeric_start_date+1, -1, 'periods_public_calendar_cell_highlight');
				} else {
					start_date_input.value	= '';
					end_date_input.value	= '';

					//show message box
					titleBox.setXPosition(this, -195);
					titleBox.setYPosition(this, -105);
					titleBox.setMessage('Desde el día ' + periodsPublicNumber2PeriodDate(start_day.periodStartDate)
											+ ' al ' + periodsPublicNumber2PeriodDate(start_day.periodEndDate)
											+ ' se deben reservar ' + start_day.periodNumDays + ' noches o más'	);
					titleBox.show();
				}
			} else {
				selectDaysRange(this, numeric_end_date-numeric_start_date+1, -1, 'periods_public_calendar_cell_highlight');
			}
		} else {
			start_date_input.value	= string_date;
			periodsPublicChangePairClass(this, 'periods_public_calendar_cell_highlight');
			this.onmouseout			= null;
			this.pair.onmouseout	= null;
		}
	} else {
		end_date_input.value		= '';
		start_date_input.value		= string_date;
		periodsPublicChangePairClass(this, 'periods_public_calendar_cell_highlight');
		this.onmouseout				= null;
		this.pair.onmouseout		= null;
	}
}


/**
 *	Sets div properties for a busy day
 */
function setPublicBusyDivProperties( div_handle, div_type, content ) {

	div_handle.innerHTML	= (div_type=='right' ? '-' : '');
	div_handle.id			= content;
	div_handle.className	= 'periods_public_calendar_cell_busy_'+div_type;
	div_handle.title		= document.getElementById('reservations_reservated_day_language').innerHTML;
}


/**
 *	Sets div properties for a close season day
 */
function setPublicCloseDivProperties( div_handle, div_type, content, calendar_day ) {

	div_handle.innerHTML	= (div_type=='right' ? '-' : '');
	div_handle.id			= content;
	div_handle.className	= 'periods_public_calendar_cell_close_season_'+div_type;
	div_handle.title		= 'Del '
							+ periodsPublicNumber2PeriodDate(calendar_day.periodStartDate, calendar_day.year)
							+ ' al '
							+ periodsPublicNumber2PeriodDate(calendar_day.periodEndDate, calendar_day.year)
							+ ' nuestras instalaciones permanecerán cerradas.';
}

/**
 *	Sets div properties for a period day
 *
 *	For periods its must store periodDay/month and periodNumDays. It will be done by storing periodNumDays
 *	into the innerHTML property and periodDay into the id property.
 *	The data into the id property will be composed by:
 *		periodDayDiv:periodId:code('D'=day or 'N'=numDays):periodDay/periodNumDays:periodMonth:periodType
 */
function setPublicPeriodDivProperties( div_handle, div_type, content, calendar_day ) {

	div_handle.innerHTML	= content;

	div_handle.id			= 'periodDayDiv:' + calendar_day.periodId
							+ ':D:' + calendar_day.day + ':' + calendar_day.month
							+ ':' + calendar_day.periodType;

	div_handle.className	= 'periods_public_calendar_cell_period_'+div_type;

	div_handle.title		= 'Del '
							+ periodsPublicNumber2PeriodDate(calendar_day.periodStartDate, calendar_day.year)
							+ ' al '
							+ periodsPublicNumber2PeriodDate(calendar_day.periodEndDate, calendar_day.year)
							+ ' es obligatorio reservar al menos '
							+ calendar_day.periodNumDays
							+ ' noches.';

	//calendar cell onMouseOver function
	div_handle.onmouseover	= periodPublicDayOnMouseOver;

	//calendar cell onMouseOut function
	div_handle.onmouseout	= periodPublicDayOnMouseOut;

	//calendar cell onClick function
	div_handle.onclick		= periodPublicDayOnClick;
}


/**
 *	This is a proxy function for setPublicBusyDivProperties, setPublicCloseDivProperties and setPublicPeriodDivProperties
 */
function setPublicDivProperties( type, div_handle, div_hand, content, calendar_day ) {

	if (type=='B') { //busy days
		setPublicBusyDivProperties(div_handle, div_hand, content);
	} else if (type=='S') { //Close season days
		setPublicCloseDivProperties(div_handle, div_hand, content, calendar_day);
	} else { //period days
		setPublicPeriodDivProperties(div_handle, div_hand, content, calendar_day);
	}
}


/**
 *	Sets div properties for an empty day
 */
function setPublicNormalDivProperties( div_handle, div_type, content, calendar_day ) {

	div_handle.innerHTML	= content;
	div_handle.id			= calendar_day.month;
	div_handle.className	= 'periods_public_calendar_cell_day_'+div_type;

	//calendar cell onMouseOver function
	div_handle.onmouseover	= normalPublicDayOnMouseOver;

	//calendar cell onMouseOut function
	div_handle.onmouseout	= normalPublicDayOnMouseOut;

	//calendar cell onClick function
	div_handle.onclick		= normalPublicDayOnMouseClick;
}


/**
 *	Draws calendar structure
 */
function periodsPublicDrawCalendar() {

	//Resets calendar container
	periodsPublicResetMonthsContainer();

	//Resets days divs store
	period_public_days_divs	= new Array();

	for (var n=1;n<13;n++) {
		var month_div			= document.getElementById('periods_public_calendar_cell_'+n);

		//Create table
		var table				= document.createElement('table');
			table.cellPadding	= 0;
			table.cellSpacing	= 0;
			//table.width			= 120;
			table.border		= 0;
			table.className		= 'periods_public_month_table_' + (navigator.appName == "Microsoft Internet Explorer" ? 'ie' : 'ff');
			month_div.appendChild(table);

		//Create header cells
		var daysTitle			= new Array ('L','M','X','J','V','S','D');
		var row 				= table.insertRow(table.rows.length);

		for (var i=0;i<7;i++) {
			var cell			= row.insertCell(row.cells.length);
				cell.align		= 'center';
				cell.valign		= 'middle';
				cell.colSpan	= 2;
				cell.className	= 'periods_public_cell_header';
				cell.innerHTML	= daysTitle[i];
		}

		//Create rows
		for (var i=0;i<6;i++) {
			var row = table.insertRow(table.rows.length);

			for (var j=0;j<7;j++) {

				//Create row cells
				var leftCell				= row.insertCell(row.cells.length);
					leftCell.width			= 10;
					leftCell.height			= (navigator.appName == "Microsoft Internet Explorer" ? 22 : 20);

				var leftCellDiv				= document.createElement('div');
					leftCellDiv.className	= 'periods_public_calendar_cell_empty';
					leftCell.appendChild(leftCellDiv);

				var rightCell				= row.insertCell(row.cells.length);
					rightCell.width			= 14;
					rightCell.height		= (navigator.appName == "Microsoft Internet Explorer" ? 22 : 20);

				var rightCellDiv			= document.createElement('div');
					rightCellDiv.pair 		= leftCellDiv;
					rightCellDiv.className	= 'periods_public_calendar_cell_empty';
					rightCell.appendChild(rightCellDiv);

					leftCellDiv.pair 		= rightCellDiv;

				//Is a day cell
				if (period_public_calendar.months[n-1].month_table[i][j]) {
					leftCellDiv.className	= 'periods_public_calendar_cell_day_left';
					rightCellDiv.className	= 'periods_public_calendar_cell_day_right';

					var hasPeriod = (period_public_calendar.months[n-1].month_table[i][j].periodId
									&& period_public_calendar.months[n-1].month_table[i][j].periodId!=''
										? true
										: false);

					var calendar_day	= period_public_calendar.months[n-1].month_table[i][j];

					//special days
					if (hasPeriod) {
						if (!period_public_previous_day) {
							if (parseInt(calendar_day.periodStartDate, 10)>parseInt(calendar_day.periodEndDate, 10)) { //this is a period that starts on past year to next year
								setPublicDivProperties(calendar_day.periodType, leftCellDiv, 'left', '', calendar_day);
							} else {
								setPublicNormalDivProperties(leftCellDiv, 'left', '', calendar_day);
							}
						} else if (!period_public_previous_day.periodId) {
							setPublicNormalDivProperties(leftCellDiv, 'left', '', calendar_day);
						} else if (period_public_previous_day.periodId!=calendar_day.periodId) {
							setPublicDivProperties(period_public_previous_day.periodType, leftCellDiv, 'left', '', period_public_previous_day);
						} else {
							setPublicDivProperties(calendar_day.periodType, leftCellDiv, 'left', '', calendar_day);
						}
						setPublicDivProperties(calendar_day.periodType, rightCellDiv, 'right', calendar_day.day, calendar_day);

						//This checks the special case in which a period ends on 01/01
						if (parseInt(calendar_day.periodEndDate, 10)==1) {
							setPublicDivProperties(calendar_day.periodType, period_public_days_divs[0].pair, 'left', '', calendar_day);
							period_public_days_divs[0].pair.startClass	= period_public_days_divs[0].pair.className;
						}

					} else { //day is not into a period
						//If previous day has period inside it self set current leftCellDiv to PeriodDivProperties
						if (period_public_previous_day && period_public_previous_day.periodId) {
							setPublicDivProperties(period_public_previous_day.periodType, leftCellDiv, 'left', '', period_public_previous_day);
						} else {
							setPublicNormalDivProperties(leftCellDiv, 'left', '', calendar_day);
						}
						setPublicNormalDivProperties(rightCellDiv, 'right', calendar_day.day, calendar_day);
					}
					period_public_previous_day								= calendar_day;
					period_public_days_divs[period_public_days_divs.length]	= rightCellDiv;

					//Current day has an outline border
					if (calendar_day.current) {
						leftCellDiv.className	= 'periods_public_calendar_cell_current_left';
						rightCellDiv.className	= 'periods_public_calendar_cell_current_right';
					}

					leftCellDiv.startClass	= leftCellDiv.className;
					rightCellDiv.startClass	= rightCellDiv.className;
				}
			}
		}
	}
}