﻿/* initialize some variables */
var calendarstate = 'CLOSED';
var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var dropdownYearMonth;
var dropdownDay;
var updateJS = 0;

// we validate against dates like 30 feb, 31 iun etc
function ValidateDay(monthCtrlId, dayCtrlId, changeDay, uniqueKeyword) {
	//alert(date_validator + " " + typeof (date_validator));
	if (date_validator == null || typeof (date_validator) == 'undefined')
		return true;

	var lCalendar = new CalendarFlight(uniqueKeyword);

	var lYearIndex;
	if (dayCtrlId == 'depDayFlight_' + uniqueKeyword)
		lYearIndex = lCalendar.GetStartDate().getFullYear();
	else
		lYearIndex = lCalendar.GetEndDate().getFullYear();

	lYearIndex = lYearIndex - (new Date()).getFullYear();

	var dayCtrl = document.getElementById(dayCtrlId);
	var monthCtrl = document.getElementById(monthCtrlId);

	var lSelDay = dayCtrl.options[dayCtrl.selectedIndex].value;

	if (Number(lSelDay) > date_validator[lYearIndex][monthCtrl.options[monthCtrl.selectedIndex].value]) {
		if (changeDay == 1) {
			dayCtrl.selectedIndex = date_validator[lYearIndex][monthCtrl.options[monthCtrl.selectedIndex].value] - 1;
		}
		else {
			return false;
		}
	}
	return true;
}

// make sure that the start date is less than the end date
function ValidateDateChronology(uniqueKeyword) {
	var lCalendar = new CalendarFlight(uniqueKeyword);

	if (lCalendar.GetStartDate() >= lCalendar.GetEndDate())
		return false;

	return true;
}

// make sure the selection is not in the past
function ValidateDateSelection(uniqueKeyword) {
	var lCalendar = new CalendarFlight(uniqueKeyword);
	var lToday = new Date();
	if (lCalendar.GetStartDate() < lToday)
		return false;

	return true;
}

// we set the return date to be always 8 days after the departure date
function ChangeRetDate(uniqueKeyword) {
	var lCalendar = new CalendarFlight(uniqueKeyword);

	if (lCalendar.GetStartDate() < lCalendar.GetEndDate())
		return;

	var lRetDate = lCalendar.GetStartDate();

	lRetDate.setDate(lRetDate.getDate() + 8);

	var lDayCombo = document.getElementById('retDayFlight_'+uniqueKeyword);
	var lMonthCombo = document.getElementById('retMonthFlight_'+uniqueKeyword);

	lDayCombo.options[lRetDate.getDate() - 1].selected = true;

	for (i = 0; i < lMonthCombo.options.length; i++)
		if (lMonthCombo.options[i].value == lRetDate.getMonth())
		lMonthCombo.options[i].selected = true;
}

var CalendarFlight = function(uniqueKeyword) 
{ 
	this.UniqueKeyword = uniqueKeyword 
};

CalendarFlight.prototype.GetStartDate = function() {
	var lToday = new Date();
	var lYear = lToday.getFullYear();
	
	var lDayCombo = document.getElementById(('depDayFlight_' + this.UniqueKeyword));
	var lDay = lDayCombo.options[lDayCombo.selectedIndex].value;

	var lMonthCombo = document.getElementById(('depMonthFlight_' + this.UniqueKeyword));
	var lMonth = lMonthCombo.options[lMonthCombo.selectedIndex].value.substring(4, 2);

	if (lMonth.indexOf(0) == 0) {
		lMonth = lMonth.substring(1);
	}
	if ((lMonth < lToday.getMonth()))
		lYear += 1;

	var lSelDate = new Date();
	lSelDate.setFullYear(lYear, lMonth, lDay);
	return lSelDate;
}

CalendarFlight.prototype.GetEndDate = function() {
	var lToday = new Date();
	var lYear = lToday.getFullYear();

	var lDayCombo = document.getElementById(('retDayFlight_' + this.UniqueKeyword));
	var lDay = lDayCombo.options[lDayCombo.selectedIndex].value;

	var lMonthCombo = document.getElementById(('retMonthFlight_' + this.UniqueKeyword));
	var lMonth = lMonthCombo.options[lMonthCombo.selectedIndex].value.substring(4,2);

	if (lMonth.indexOf(0) == 0) {
		lMonth = lMonth.substring(1);
	}
	
	if ((lMonth < lToday.getMonth()))
		lYear += 1;

	var lSelDate = new Date();
	lSelDate.setFullYear(lYear, lMonth, lDay);
	return lSelDate;
}

// gets the end date in the form 'ddMMyyyy'
CalendarFlight.prototype.GetEndDateString = function() {
	return this.GetDateString(this.GetEndDate());
}

// gets the start date in the form 'ddMMyyyy'
CalendarFlight.prototype.GetStartDateString = function() {
	return this.GetDateString(this.GetStartDate());
}

CalendarFlight.prototype.GetDateString = function(date) {
	var lDay = date.getDate();
	if (lDay < 10)
		lDay = '0' + lDay;

	var lMonth = (date.getMonth() + 1);
	if (lMonth < 10)
		lMonth = '0' + lMonth;

	var lYear = date.getFullYear();

	return '' + lDay + lMonth + lYear;
}

function InitDatesFlights(uniqueKeyword) {
	var lToday = new Date();
	lToday.setDate(lToday.getDate() + 14);
	selectDateFlight(lToday.getDate(), lToday.getMonth() + 1, lToday.getFullYear(), 'depDayFlight_' + uniqueKeyword,
		'depMonthFlight_' + uniqueKeyword, uniqueKeyword);

	lToday.setDate(lToday.getDate() + 14);
	selectDateFlight(lToday.getDate(), lToday.getMonth() + 1, lToday.getFullYear(), 'retDayFlight_' + uniqueKeyword,
		'retMonthFlight_' + uniqueKeyword, uniqueKeyword);
}

function selectDateFlight(selected_day, selected_month, selected_year, day_field, month_field, uniqueKeyword) {

	// show the dropdowns
	document.getElementById(('retDayFlight_' + uniqueKeyword)).style.visibility = 'visible';
	document.getElementById(('retMonthFlight_' + uniqueKeyword)).style.visibility = 'visible';

	if (selected_month > 12) {
		selected_month = 1;
		selected_year += 1;
	}

	if ((day_field == ("depDayFlight_" + uniqueKeyword)) || (month_field == ("depMonthFlight_" + uniqueKeyword))) {
		var mon_element = document.getElementById(("depMonthFlight_" + uniqueKeyword));
		document.getElementById(("depDayFlight_" + uniqueKeyword)).options[selected_day - 1].selected = true;

		for (i = 0; i < mon_element.options.length; i++)
			if (mon_element.options[i].value == createMonthValue(selected_month, selected_year)) {
			mon_element.options[i].selected = true;
		}

		if (typeof (mon_element.onchange) == "function") {
			mon_element.onchange();
		}

	} else if ((day_field == ("retDayFlight_" + uniqueKeyword)) || (month_field == ("retMonthFlight_" + uniqueKeyword))) {
		var mon_element = document.getElementById(("retMonthFlight_" + uniqueKeyword));
		document.getElementById(("retDayFlight_" + uniqueKeyword)).options[selected_day - 1].selected = true;
	
		for (i = 0; i < mon_element.options.length; i++)
			if (mon_element.options[i].value == createMonthValue(selected_month, selected_year))
			mon_element.options[i].selected = true;
	}
}

function createMonthValue(month, year) {
	if (month <= 9) {
		return year + '0' + month;
	}
	return year + '' + month; 
}

/* draw calendar */
function showCalendar(type, y, m, yd, dd, ujs, leftOffset, topOffset, uniqueKeyword) {
	var h = '';
	/* set dropdowns if they are set */
	if (yd && dd) {
		dropdownYearMonth = yd;
		dropdownDay = dd;
		updateJS = ujs;
	}

	/* had to do this crazy hack because parseInt() doesn't work as expected */
	m = m + '';
	if (m.substr(0, 1) == '0') {
		m = m.substr(1, 1);
	}

	y = parseInt(y);
	m = parseInt(m);

	if (type != 'UPDATE') {
		if (type == calendarstate) {
			return closeCalendar();
		} else {
			calendarstate = type;
		}
	}
	if (window.attachEvent) h += '<iframe style="position:absolute; width:447px;height:216px;background-color:#FFFFFF;z-index:10;"></iframe>';
	h += '<table border="0" cellpadding="0" cellspacing="0" class="calheader">';
	/* Print close button, that closes the calendar window */

	h += '<tr>';
	h += '<td width="50%" valign="top">' + drawMonth(1, y, m, uniqueKeyword, yd, dd, leftOffset, topOffset) + '</td>';
	h += '<td width="50%" valign="top">' + drawMonth(2, y, m + 1, uniqueKeyword, yd, dd, leftOffset, topOffset) + '</td>';
	h += '</tr>';

	/* Print close button, that closes the calendar window */

	h += '</table>';

	var divob = document.getElementById(('calendardiv_' + uniqueKeyword));
	divob.innerHTML = h;
	divob.style.top = topOffset+'px';
	divob.style.left = leftOffset+'px';

	setVisibilityCalendarFC('calendardiv_' + uniqueKeyword, 1);
}

/* close calendar */
function closeCalendar(uniqueKeyword) {
	calendarstate = 'CLOSED';
	setVisibilityCalendarFC('calendardiv_' + uniqueKeyword, 0);
}

/*
* function drawMonth(position, y, m)
*
* - position = This is either 1 (left) or 2 (right). Don't alter.
* - y = Year
* - m = Month
*
*/
function drawMonth(position, y, m, uniqueKeyword, yd, dd, leftOffset, topOffset) {
	var h = '';
	var cells = 0;
	var today_dob = new Date();

	/* Let's do a sanity check. This works nicely if someone enters for example month 13, which would become 1, and year increased by 1 also */
	var dob = new Date(y, m - 1, 1);
	m = dob.getMonth() + 1;
	y = dob.getFullYear();

	/* Start table */
	h = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';

	/* Print the header row with month name */
	h += '<tr class="scalheader1">';

	/* Print the 'previous' button (for left calendar only) */
	if (position == 1) {
		var plink_y = y;
		var plink_m = m;

		if (plink_m == 1) {
			plink_y--;
			plink_m = 12;
		}
		h += '<td>&nbsp;</td>';
		h += '<td align="center" class="scalheader1"><a style="color: black;" href="javascript://" onClick="showCalendar(\'UPDATE\', ' + plink_y + ', ' +
			(plink_m - 1) + ',\'' + yd + '\',\'' + dd + '\',1,' + leftOffset + ',' + topOffset + ',\'' + uniqueKeyword + '\');"><img src="../shared/images2/datepicker_prev-inactive.png"></a></td>';
	}

	/* Print month name */
	h += '<td colspan="5" align="center" class="scalheader1">' + monthNames[m - 1] + ' ' + y + '</td>';

	/* Print the 'next' button (for right calendar only) */
	if (position == 2) {
		var mdiff = (y * 12 + m) - (today_dob.getFullYear() * 12 + today_dob.getMonth() + 1) + 2;
		/* if the 'next month' is further than 15 months from today.. let's not show it :) */
		if (mdiff > 15) {
			h += '<td>&nbsp;</td>';
		} else {
		h += '<td align="center" class="scalheader1 scalheader1-next"><a style="color: black;" href="javascript://" onClick="showCalendar(\'UPDATE\', ' + y + ', ' + (m) + ',\'' + yd + '\',\'' + dd + '\',1,' + leftOffset + ',' + topOffset + ',\'' + uniqueKeyword + '\');"><img src="../shared/images2/datepicker_next-inactive.png"></a></td>';
		h += '<td align="center"><a href="javascript://" onclick="closeCalendar(\'' + uniqueKeyword + '\');" class="closebutton"><img src="../shared/images2/datepicker_close-inactive.png"></a></td>';
		}
	}

	h += '</tr>';

	/* Print day names */
	h += '<tr class="scalheader2">';

	for (var i = 0; i < dayNames.length; i++) {
		h += '<td align="center" width="14%" class="scalheader2">' + dayNames[i] + '</td>';
	}

	h += '</tr>';

	/* Print the calendar */
	h += '<tr class="scalcell">';

	/* Fill empty cells in the beginning of the 'first week of month' if necessary. For example 1st day of a month can be wednesday */
	for (var i = 0; i < getStartDay(dob); i++) {
		h += '<td>&nbsp;</td>';
		cells++;
	}

	/* Check how many days there are in this month. Also checks for leap year */
	var daysInThisMonth = getDaysInThisMonth(y, m);

	/* Print the day numbers of the week */
	for (var i = 1; i <= daysInThisMonth; i++) {
		dob.setDate(i);
		cells++;

		var mStr = m + '';
		if (mStr.length == 1) {
			mStr = '0' + mStr;
		}

		var onClickStr = 'updateCalendar(\'' + y + '\',\'' + mStr + '\',\'' + i + '\',\'' + uniqueKeyword + '\');'; 

		if (dob.getFullYear() == today_dob.getFullYear() && dob.getDate() == today_dob.getDate() && dob.getMonth() == today_dob.getMonth()) {
			/* today */
			h += '<td align="center" style="color: black;"><b>' + i + '</b></td>';
		} else if (dob.getTime() < today_dob.getTime()) {
			/* days before today are not selectable */
			h += '<td align="center" style="color: gray;">' + i + '</td>';
		} else {
			h += '<td align="center"><a href="javascript://" style="color: black;" onClick="' + onClickStr + '">' + i + '</a></td>';
		}

		/* next week row */
		if (!(cells % 7)) {
			h += '</tr><tr class="scalcell">';
		}
	}

	/* Add some empty cells to tidy the table */
	if (cells < 42) {
		var cells2 = 42 - cells;
		for (var i = 0; i < cells2; i++) {
			cells++;
			h += '<td align="center">&nbsp;</td>';

			if (!(cells % 7)) {
				h += '</tr><tr class="scalcell">';
			}
		}
	}

	/* close table */
	h += '</tr></table>';

	return h;
}

function updateCalendar(y, mStr, i, uniqueKeyword) {
	document.getElementById(dropdownYearMonth).value = (y + mStr);
	document.getElementById(dropdownDay).value = i;
	closeCalendar(uniqueKeyword); 
}

/* Get position of the day in a week */
function getStartDay(d) {
	var startDay = d.getDay();

	if (startDay == 0) {
		startDay = 7;
	}

	return startDay - 1;
}

/* Amount of days in a month */
function getDaysInThisMonth(y, m) {
	/* leap year check for february */
	if (m == 2) {
		if (!(y % 400) || (!(y % 4) && y % 100)) {
			return 29;
		} else {
			return 28;
		}
	} else {
		return daysInMonth[m - 1];
	}
}

/* set visibility of an element. */
function setVisibilityCalendarFC(obname, state) {
	var ob = document.getElementById(obname);
	if (ob != null) {
		if (state == 1) {
			ob.style.display = '';
			ob.style.visibility = 'visible';
		} else {
			ob.style.display = 'none';
			ob.style.visibility = 'hidden';
		}
	}
}

