var gNow = new Date();
var ggWinCal;
var gCal;

var dates_array = new Array (14);
var codes_array = new Array (14);
var yearMonthsCode = new Array('0','1','2','3','4','5','6','7','8','9','10','11');
var yearMonthsText = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var yearText = new Array("2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019");

var gDaysOfWeekFirstLetterDefault = ["Su","Mo","Tu","We","Th","Fr","Sa"];

Calendar.Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var begin_dates;
var end_dates;
var descriptions;
var users;
var avail;
var index_date;
var contain;

var gIndexValidFlightFirstDay;
var gIndexValidFlightFirstMonth;
var gIndexValidFlightFirstYear;
var gIndexValidFlightLastDay;
var gIndexValidFlightLastMonth;
var gIndexValidFlightLastYear;	

var gDaysOfWeekFirstLetter;
var gPopUpSelectText;
var gPopUpSelectTextDefault = "Select month";

var gPopUpTitleText;
var gPopUpTitleTextDefault = "calendar";

function Calendar(monthSelect, yearSelect, p_WinCal, selIndex) {

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;

	var d = new Date();
	
	if (monthSelect != -1)
		this.gMonth = monthSelect;
	else
		this.gMonth = d.getMonth();
	
	if (yearSelect != -1)
		this.gYear = 2005 + parseInt(yearSelect);
	else
		this.gYear = d.getYear();
	
	this.selIndex = selIndex;
}

Calendar.get_daysofmonth = Get_MonthDays;

function Get_MonthDays(monthNo, yearToShow)
{
	if ((yearToShow % 4) == 0)
	{
		if ((yearToShow % 100) == 0 && (yearToShow % 400) != 0)
			return DOMonth[monthNo];
		
		return lDOMonth[monthNo];
	}
	else
		return DOMonth[monthNo];
}

new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
			
	var vCode 			= "";
	var vHeader_Code 	= "";
	var vData_Code 		= "";

	//vHeader_Code = this.cal_header();
	vCode = vCode + "<form style='margin: 0;' NAME='CalendarForm'>";
	vCode = vCode + "<table width='190' height='100%' border='0' cellspacing='0' cellpadding='0'>";
	vCode = vCode + "<tr align='center'>";
	vCode = vCode + "<td>";
	vCode = vCode + "<table style='background-color: #0F6734;' width='180' cellpadding='0' cellspacing='0' border='0'>";
	vCode = vCode + "<tr>";
	vCode = vCode + "<td colspan='7' height='27'><div align=center valign=middle>&nbsp;&nbsp;<b><font class='common_font'>ٍإسرأ:</font></b>";
	vCode = vCode + "&nbsp;&nbsp;<select style='background-color: #FFFFFF;' name='year_with_monthes'" +  //">";
			"onChange = 'changeForm ();'>";

	for (var i = 0; i < 12; i ++ )
	{
		if (i == this.selIndex)
			vCode = vCode + "<option value =\" " + codes_array[i] + " \" selected><font class='common_font'>" + dates_array[i] + "</font></option>";
		else
			vCode = vCode + "<option value =\" " + codes_array[i] + " \"><font class='common_font'>" + dates_array[i] + "</font></option>";
	}
	vCode = vCode + "</select></div>";
	vCode = vCode + "</td>";
	vCode = vCode + "</tr>";
	
	vData_Code = this.cal_data();	
		
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE></td>";
	vCode = vCode + "</tr>";
	vCode = vCode + "</table>";
	vCode = vCode + "</form>";
	
	return vCode;
}

Calendar.prototype.show = function()
{
	var vCode = "";
	
	this.wwrite ("\<script language=javascript\>");
	this.wwrite ("	function ChangeEvent (event_index)");
	this.wwrite ("	{");
	this.wwrite ("		var curr_event = document.getElementById (\"container_event\");");
	this.wwrite ("		if (curr_event)");
	this.wwrite ("		{");
	this.wwrite ("			a_tag = descriptions [event_index].split (/&lt;a href=/);");
	this.wwrite ("			if (a_tag [1])");
	this.wwrite ("			{");
	this.wwrite ("				href = a_tag [1].split (/&quot;/);");
	this.wwrite ("				text = '';");
	this.wwrite ("				t = href [3].split (/&gt;/);");
	this.wwrite ("				text = text + t [0].split (/&lt;\\/a/);");
	this.wwrite ("				curr_event.innerHTML = a_tag [0] + text.link (href [0]);");
	this.wwrite ("			}");
	this.wwrite ("			else");
	this.wwrite ("				curr_event.innerHTML = descriptions [event_index];");
	this.wwrite ("		}");
	this.wwrite ("	}");
	this.wwrite ("\</script\>");
	this.wwrite ("<div name='container_cal' id='container_cal'>");	
	
	vCode = this.getMonthlyCalendarCode();
	this.wwrite (vCode);

	this.wwrite ("</div>");
}


Calendar.prototype.wwrite = function(wtext)
{
	document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext)
{
	document.write(wtext);
}

Calendar.prototype.cal_header = function() {

	var vCode = "";
	
	vCode = vCode + "<tr align='center' bgcolor='#000066'>";

	//-------------------------------------------
	//COMMENT: Iterate through the days in the week to get the first letter
	//-------------------------------------------
	for(i = 0; i <= 6; i++){	
		vCode = vCode + "<td width='18' height='20' align='center' class='week_day'><b>" + gDaysOfWeekFirstLetter[i] + "</b></td>";
	}
	vCode = vCode + "</tr>";
	return vCode;
}


Calendar.prototype.cal_data = function() {
	//Create a data object
	var vDate = new Date();
	var cur_day = vDate.getDate();
	var cur_month = vDate.getMonth();
	var cur_year = vDate.getYear();

	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);
	var cur_date = vDate;

	var vFirstDay= vDate.getDay();
	var tempDate = new Date();

	tempDate.setMonth(this.gMonth);
	tempDate.setFullYear(this.gYear);
	
	if (vFirstDay ==0){
		vFirstDay = 6
	}else{
		vFirstDay = (vFirstDay-1)
	}

	var vDay=1;
	var vLastDay=Get_MonthDays(parseInt(this.gMonth), this.gYear);
	var vOnLastDay=0;
	var vCode = "";
	
	currentMonth = this.gMonth;
	currentYear = this.gYear;
	
	if (currentMonth == 0){
		prevMonth = 11;	
		yearOfPrevMonth = (this.gYear - 1)
	}else{
		prevMonth = currentMonth - 1;
		yearOfPrevMonth = this.gYear;
	}
	
	
	numberOfDaysInPreviousMonth = Get_MonthDays(prevMonth, yearOfPrevMonth)
	
	vCode = vCode + "<tr>";
		
			for (i=0; i<vFirstDay; i++) {
					vCode = vCode + "<td width='18' height='20' align='center' class='day_other'>&nbsp;</td>";
			}
	var first_week = 0;
	var temp_b;
	var temp_e;
	
	for (j=vFirstDay; j<7; j++) {
	
		//check if current day is a day that the user may fly on				  
		if (	(this.gYear < cur_year)
			 || 
			(	(this.gYear == cur_year) && 
				(this.gMonth < cur_month)
			) 
			||
			(	(this.gYear == cur_year) && 
				(this.gMonth == cur_month) && 
				(this.format_day(vDay) < cur_day)
			)
		)
		{
			vCode = vCode + "<td width='18' height='20' align='center' class='day_prev'>" + vDay  +"</td>";
		}//else allow the day to be selectable
	
		else
		{
			busy = 0;
			var k;
			cur_date.setDate (vDay);
		
			if (begin_dates != null && end_dates != null)
			{
				for (k = 0; k < begin_dates.length; k++)
				{
					temp_b = new Date (parseInt(begin_dates[k]));
					temp_e = new Date (parseInt(end_dates[k]));
					if (	(cur_date.getYear() >= temp_b.getYear() && cur_date.getYear() <= temp_e.getYear()) && 
						(cur_date.getMonth() >= temp_b.getMonth() && cur_date.getMonth() <= temp_e.getMonth()) &&
						(cur_date.getDate() >= temp_b.getDate() && cur_date.getDate() <= temp_e.getDate()) &&
							(news[k] == 0)
					)
					{
						busy = 1;
						break;
					}
				}
			}
			
			vCode = vCode + "<td width='18' height='20' align='center' class='";
			
			if (busy != 0)
			{
				if (first_week < 7)
					first_week++;
				
				vCode = vCode + "day_event' style='";
				
				temp_b = new Date (parseInt(begin_dates[k]));
				temp_e = new Date (parseInt(end_dates[k]));
				
				if (	cur_date.getYear() == temp_b.getYear() &&
					cur_date.getMonth() == temp_b.getMonth() &&
					cur_date.getDate() == temp_b.getDate()
				)
					vCode = vCode + "border-left-color: #00600B; border-left-style: solid; border-left-width: 2px;";
				
				if (	cur_date.getYear() == temp_e.getYear() &&
					cur_date.getMonth() == temp_e.getMonth() &&
					cur_date.getDate() == temp_e.getDate()
				)
					vCode = vCode + "border-right-color: #00600B; border-right-style: solid; border-right-width: 2px;";
				
				vCode = vCode + "' onMouseOver=\"ChangeEvent (" + k + ");\"";
			}
			else
			{
				vCode = vCode + "day_select";
				
				if (first_week < 7)
				{
					vCode = vCode + " first_week' style='";
					
					if (first_week == 0 || j == 0)
						vCode = vCode + "border-left-color: #7EBA67; border-left-style: solid; border-left-width: 3px;";
					
					if (j == 6)
						vCode = vCode + "border-right-color: #7EBA67; border-right-style: solid; border-right-width: 3px;";
				
					first_week++;
				}
				
				vCode = vCode + "'";
			}
			
			vCode = vCode + ">" + vDay + "</TD>";
		}
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";
	var ends = false;
	for (l=2; l<7; l++)
	{
		vCode = vCode + "<TR>";
		
		if ((vLastDay - vDay) < 7)
			ends = true;
		
		for (j=0; j<7; j++)
		{
		
			if (	(this.gYear < cur_year)
				|| 
				(	(this.gYear == cur_year) &&
					(this.gMonth < cur_month)
				) 
				||
				(	(this.gYear == cur_year) && 
					(this.gMonth == cur_month) && 
					(this.format_day(vDay) < cur_day)
				)
			)
			{
				busy = 0;
				var k;
				cur_date.setDate (vDay);
			
				if (begin_dates != null && end_dates != null)
				{
					for (k = 0; k < begin_dates.length; k++)
					{
						temp_b = new Date (parseInt(begin_dates[k]));
						temp_e = new Date (parseInt(end_dates[k]));
						if (	(cur_date.getYear() >= temp_b.getYear() && cur_date.getYear() <= temp_e.getYear()) && 
							(cur_date.getMonth() >= temp_b.getMonth() && cur_date.getMonth() <= temp_e.getMonth()) &&
							(cur_date.getDate() >= temp_b.getDate() && cur_date.getDate() <= temp_e.getDate()) &&
							(news[k] == 0)
						)
						{
							busy = 1;
							break;
						}
					}
				}
				
				vCode = vCode + "<td width='18' height='20' align='center' class='";
				
				if (busy != 0)
				{
					vCode = vCode + "day_event' style='";
					
					temp_b = new Date (parseInt(begin_dates[k]));
					temp_e = new Date (parseInt(end_dates[k]));
					
					if (	cur_date.getYear() == temp_b.getYear() &&
						cur_date.getMonth() == temp_b.getMonth() &&
						cur_date.getDate() == temp_b.getDate()
					)
						vCode = vCode + "border-left-color: #00600B; border-left-style: solid; border-left-width: 2px;";
					
					if (	cur_date.getYear() == temp_e.getYear() &&
						cur_date.getMonth() == temp_e.getMonth() &&
						cur_date.getDate() == temp_e.getDate()
					)
						vCode = vCode + "border-right-color: #00600B; border-right-style: solid; border-right-width: 2px;";
					
					vCode = vCode + "' onMouseOver=\"ChangeEvent (" + k + ");\"";
				}
				else
					vCode = vCode + "day_prev'";
					
				vCode = vCode + ">" + vDay +"</td>";
	
			}//else allow the day to be selectable		
			else
			{
				busy = 0;
				var l;
				cur_date.setDate (vDay);
			
				if (begin_dates != null && end_dates != null)
				{
					for (k = 0; k < begin_dates.length; k++)
					{
						temp_b = new Date (parseInt(begin_dates[k]));
						temp_e = new Date (parseInt(end_dates[k]));
						if (	(cur_date.getYear() >= temp_b.getYear() && cur_date.getYear() <= temp_e.getYear()) && 
							(cur_date.getMonth() >= temp_b.getMonth() && cur_date.getMonth() <= temp_e.getMonth()) &&
							(cur_date.getDate() >= temp_b.getDate() && cur_date.getDate() <= temp_e.getDate()) &&
							(news[k] == 0)
						)
						{
							busy = 1;
							break;
						}
					}
				}
				
				vCode = vCode + "<td width='18' height='20' align='center' class='";
				
				if (busy != 0)
				{
					if (first_week < 7)
						first_week++;
					
					vCode = vCode + "day_event' style='";
					
					temp_b = new Date (parseInt(begin_dates[k]));
					temp_e = new Date (parseInt(end_dates[k]));
					
					if (	cur_date.getYear() == temp_b.getYear() &&
						cur_date.getMonth() == temp_b.getMonth() &&
						cur_date.getDate() == temp_b.getDate()
					)
						vCode = vCode + "border-left-color: #00600B; border-left-style: solid; border-left-width: 2px;";
					
					if (	cur_date.getYear() == temp_e.getYear() &&
						cur_date.getMonth() == temp_e.getMonth() &&
						cur_date.getDate() == temp_e.getDate()
					)
						vCode = vCode + "border-right-color: #00600B; border-right-style: solid; border-right-width: 2px;";
					
					vCode = vCode + "' onMouseOver=\"ChangeEvent (" + k + ");\"";
				}
				else
				{
					if (first_week < 7)
					{
						vCode = vCode + "first_week ";
						first_week++;
					}
					
					vCode = vCode + "day_select' style='";
					
					if (first_week == 1 || j == 0)
						vCode = vCode + "border-left-color: #7EBA67; border-left-style: solid; border-left-width: 3px;";
					
					if (j == 6)
						vCode = vCode + "border-right-color: #7EBA67; border-right-style: solid; border-right-width: 3px;";
					
					if (ends)
						vCode = vCode + "border-bottom-color: #7EBA67; border-bottom-style: solid; border-bottom-width: 3px;";
					
					vCode = vCode + "'";
				}
				
				vCode = vCode + ">" + vDay + "</TD>";
			}
			vDay=vDay + 1;
	
			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}
	
		if (vOnLastDay == 1)
			break;
	}
	
	for (m=1; m<(7-j); m++)
	{
		vCode = vCode + "<td width='18' height='20' align='center' class='";
		
		if (cur_month <= this.gMonth)
		{
			vCode = vCode + "day_select";
			
			if (first_week < 7)
			{
				vCode = vCode + " first_week";
				first_week++;
			}
			
			vCode = vCode + "' style='border-bottom-color: #7EBA67; border-bottom-style: solid; border-bottom-width: 3px;";
			
			if (m == (6 - j))
				vCode = vCode + " border-right-color: #7EBA67; border-right-style: solid; border-right-width: 3px;";
		}
		else
			vCode = vCode + "day_other";
		
		vCode = vCode + "'>&nbsp;</TD>";
	}
	
	vCode = vCode + "</tr>";
	
	return vCode;
}


Calendar.prototype.format_day = function(vday)
{
	return (vday);
}

function Build(monthSelect, yearSelect, selIndex)
{
	var p_WinCal = this;	

	gCal = new Calendar(monthSelect, yearSelect, p_WinCal, selIndex);
	gCal.show();
}


//--------------------------------------------------------------------------------------------------------------
// COMMENT:
// show_calendar
// =============
//
// The function that is called in the jsp page. This function initiates the pop up calendar variables 
// and calls the function to invoke the displaying and building.
//
// @param	0	dayTargetSelect		: The target for the 1st return item: i.e. the day drop down Menu/Select
// @param	1	monthTargetSelect		: The target for 2nd return item. i.e. the month Drop Down Menu/Select
// @param	2	yearTargetSelect		: The target for 3rd return item. i.e. the year Drop Down Menu/Select
//-------------------------------------------------------
function show_calendar() 
{
	yearSelect = -1;
	if (arguments[0] != null)
		yearSelect = arguments[0];
	
	monthSelect = -1;
	if (arguments[1] != null)
		monthSelect = arguments[1];
	
	if (arguments[2] != null)
		selIndex = arguments[2];
	else
		selIndex = gNow.getMonth();

	gDaysOfWeekFirstLetter = gDaysOfWeekFirstLetterDefault;
	
	var mon_i = gNow.getMonth() - 1;
	var yea_i = gNow.getYear() - 2005;
	
	for (i = 0; i < 12; i++)
	{
		dates_array[i] = yearMonthsText[i] + ", " + yearText[yea_i];
		codes_array[i] = yearText[yea_i] + "," + yearMonthsCode[i];
	}

	Build(monthSelect, yearSelect, selIndex);
}

function changeForm()
{
	var index = document.CalendarForm.year_with_monthes.selectedIndex;
	var temp = document.CalendarForm.year_with_monthes.options[index].value.split (/,/);
	var year = temp[0] - 2005;
	var month = temp[1];
	
	if (month != -1)
		gCal.gMonth = parseInt (month);
	else
		gCal.gMonth = d.getMonth();
	
	if (year != -1)
		gCal.gYear = 2005 + parseInt(year);
	else
		gCal.gYear = d.getYear();
	
	gCal.selIndex = index;
	
	new_cal = gCal.getMonthlyCalendarCode ();
	var con = document.getElementById ("container_cal");
	con.innerHTML = new_cal;
	
	var month_fi = new Date (year + 2005, gCal.gMonth, 1);
	/*month_fi.setDate (1);
	month_fi.setMonth (gCal.gMonth);
	month_fi.setYear (year + 2005);*/
	
	var month_la = new Date (year + 2005, gCal.gMonth, Get_MonthDays (gCal.gMonth, gCal.gYear));
	/*month_la.setDate (Get_MonthDays (gCal.gMonth, gCal.gYear));
	month_la.setMonth (gCal.gMonth);
	month_la.setYear (year + 2005);*/
	
	var f_time = month_fi.getTime () - 86300000;
	var l_time = month_la.getTime ();
	
	var ev = document.getElementById ("container_event");
	for (i = 0; i < begin_dates.length; i++)
	{
		if (
			((begin_dates [i] >= f_time) && (begin_dates [i] <= l_time)) || 
			((end_dates [i] >= f_time) && (end_dates [i] <= l_time))
		)
		{
			a_tag = descriptions [i].split (/&lt;a href=/);
			
			if (a_tag [1])
			{
				href = a_tag [1].split (/&quot;/);
    				text = "";
    				t = href [3].split (/&gt;/);
    				text = text + t [0].split (/&lt;\/a/);
    				
	    			ev.innerHTML = a_tag [0] + text.link (href [0]);
    			}
    			else
    				ev.innerHTML = descriptions [i];
    			
    			break;
    		}
	 }
}