/****************************************************************/
/*	Module : Calendar											*/
/*	Programmer : Dan Brunner									*/
/*  Date : 15/1/2001											*/
/****************************************************************/


/****************************************************************/
/*							Variables							*/
/****************************************************************/
var daysInMonth		  = new Array(31,-1,31,30,31,30,31,31,30,31,30,31);
var monthNamesHeb	  = new Array("ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר");
var monthNamesEng	  = new Array("January","February","March","April","May","June","July","August","Spetember","October","November","December");
var daysNamesHeb	  = new Array("שבת","ו","ה","ד","ג","ב","א");
var daysNamesEng	  = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var labelsHeb		  = new Array("חודש","שנה");
var labelsEng		  = new Array("Month","Year");
var selectedCellObj	  = null;
var selectedCellStyle = "";
var destObject		  = null;
var FORWARD			  = 1;
var BACKWARD		  = 2;
var HEBREW			  = "Heb";
var ENGLISH			  = "Eng";
var MONTH			  = 0;
var YEAR			  = 1;
var _3D_SKIN		  = "3D";
var _2D_SKIN		  = "2D";
var PLAIN_SKIN		  = "";

var parentRef		  = null;
var calendarLang	  = HEBREW;
var outputDelimeter	  = "/"; // The delimeter in the output result
var skin			  = _2D_SKIN;

var selectedMonth;
var selectedYear;
var selectedDay;
var shownYear;
var shownMonth;
/****************************************************************/
/*							Aux Methods							*/
/****************************************************************/

/*
Name : setSelectVal
Input: a select object,value 
Desc : Sets the selected value in the list to the element whose
		value is the given value.
		if the given value is null than the first elments is selected
		if the given element is null or there is no element in the 
		given element with the given value than false is returned, otherwise
		true is returned.		
*/
function setSelectVal(elementObj,value){
	result = false;
	count = 0;
	if (elementObj!=null && elementObj.length > 0)
		if (value==null){
			elementObj.selectedIndex = 0;
			result = true;
		}
		else{
			tempLength = elementObj.length;
			for (j=0;j<tempLength;j++)
				if (elementObj.options[j].value==value){
					elementObj.selectedIndex = j;
					result = true;	
				}
		}			
	return result;
}

/*
Name : LeapYear
Input : Calculates the number of days in february
*/

function LeapYear(intYear) {
if (intYear % 100 == 0) {
	if (intYear % 400 == 0) { 
		return true; 
	}
}
else {
	if ((intYear % 4) == 0) { 
		return true; 
	}
}
return false;
}

/****************************************************************/
/*							Methods								*/
/****************************************************************/

/*
Name : getDaysInMonth
Input: month and year
Desc : Calculates the number of days in the given month in the
		given years
*/

function getDaysInMonth(monthIndex,yearIndex){
	result = daysInMonth[monthIndex];
	if (result == -1){
		if (LeapYear(yearIndex))
			result = 29;
		else
			result = 28;
	}
	return result;	
}

/*
Name : initCalendar
Input: 
Desc : Initialize the calendar to the current day, and make the year
		and month lists point to the current date
*/

function initCalendar(){
	var currentDate = new Date();
	var delimeteres = new Array("-","/","."," ");
	var setDate = false;
	if (destObject!=null){
		if (isDate(destObject.value)){
			for (i=0;i<delimeteres.length;i++)
				if (destObject.value.indexOf(delimeteres[i])>-1)
					dataArray = destObject.value.split(delimeteres[i]);
			if (dataArray.length==3){
				selectedYear  = dataArray[2];
				selectedMonth = dataArray[1] - 1;
				selectedDay   = dataArray[0];
				setDate = true;
			}
		}
	}
	if (!setDate){
		selectedYear  = currentDate.getYear();
		selectedMonth = currentDate.getMonth();
		selectedDay   = currentDate.getDate();
	}
	setOkAlt();
	setSelectVal(document.calendar.Year,selectedYear);
	setSelectVal(document.calendar.Month,selectedMonth);
	createCalendar(selectedYear,selectedMonth);
}

/*
Name : createCalendar
Input: year and month
Desc : creates the calendar according to a given month and year.
		There are 3 stages when creating the calendar:
		1. Creating the previous month.
		2. Creating the current month.
		3. Creating the next month.
*/

function createCalendar(_Year,_Month){
	shownYear		=_Year;
	shownMonth		= _Month;
	_currentDate	= new Date(_Year,_Month,1);
	_Day			= _currentDate.getDay();
	_daysInMonth	= getDaysInMonth(_Month,_Year);
	
	// display prev month
	if (_Month==0){
		prevMonth = 11;
		prevYear  = _Year - 1;
	}else{
		prevMonth = _Month - 1;
		prevYear  = _Year;
	}
	_prevDaysInMonth = getDaysInMonth(prevMonth,prevYear);
	
	if (_Day>0){
		for (i=0;i<_Day;i++){
			currentID = _Day - 1 - i;
			currentRow = 0;
			if (prevMonth == selectedMonth && prevYear==selectedYear && (_prevDaysInMonth-i) == selectedDay){
				eval("Day" + currentID + "" + currentRow + ".className = '" + skin + "selectedDay'");
				selectedCellObj	  = eval("Day" + currentID + "" + currentRow);
				selectedCellStyle = skin + "prevMonth";
			}
			else
				eval("Day" + currentID + "" + currentRow + ".className = '" + skin + "prevMonth'");
			eval("Day" + currentID + "" + currentRow + ".innerHTML = '" + (_prevDaysInMonth-i)+"'");
			if (skin==_3D_SKIN)
				eval("Day" + currentID + "" + currentRow + ".vAlign = 'top'");
			else
				eval("Day" + currentID + "" + currentRow + ".vAlign = 'middle'");
		}
	}
	
	// display currentMonth
	for (i=0;i<_daysInMonth;i++){
		currentID = (_Day + i)%7;
		currentRow = Math.floor((_Day + i) / 7);
		if (shownMonth == selectedMonth && shownYear==selectedYear && (i+1) == selectedDay){
				eval("Day" + currentID + "" + currentRow + ".className = '" + skin + "selectedDay'");
				selectedCellObj	  = eval("Day" + currentID + "" + currentRow);
				selectedCellStyle = skin + "currentMonth";
			}
			else
				eval("Day" + currentID + "" + currentRow + ".className = '" + skin + "currentMonth'");

		eval("Day" + currentID + "" + currentRow + ".innerHTML = '" + (i+1)+"'");
		if (skin==_3D_SKIN)
			eval("Day" + currentID + "" + currentRow + ".vAlign = 'top'");
		else
			eval("Day" + currentID + "" + currentRow + ".vAlign = 'middle'");
	}
	
	// display nextMonth
	nextMonth=shownMonth + 1;
	nextYear=shownYear
	if (shownMonth==11){
		nextMonth=0
		nextYear = shownYear + 1;
	}
	
	j = 1;
	for (i=(_daysInMonth+_Day);i<42;i++){
		currentID = i%7;
		currentRow = Math.floor(i / 7);
		if (nextMonth == selectedMonth && nextYear==selectedYear && (j) == selectedDay){
				eval("Day" + currentID + "" + currentRow + ".className = '" + skin + "selectedDay'");
				selectedCellObj	  = eval("Day" + currentID + "" + currentRow);
				selectedCellStyle = skin + "nextMonth";
			}
			else
				eval("Day" + currentID + "" + currentRow + ".className = '" + skin + "nextMonth'");

		eval("Day" + currentID + "" + currentRow + ".innerHTML = '" + (j++) +"'");
		if (skin==_3D_SKIN)
			eval("Day" + currentID + "" + currentRow + ".vAlign = 'top'");
		else
			eval("Day" + currentID + "" + currentRow + ".vAlign = 'middle'");
	}
}

/*
Name : updateCalendar
Input: 
Desc : Updates the calendar to the values in the year and month
		lists
*/

function updateCalendar(){
	_Year = parseInt(document.calendar.Year.value);
	_Month = parseInt(document.calendar.Month.value);
	createCalendar(_Year,_Month);
}

/*
Name : constructSelectedDate
Input: a table cell object
Desc : Creates the string representation of the selected cell.
		the current format is : year , month(by name) day
		the string is placed above the list row
		the special cases that are checked are wether the selected
		cell is from the previous month or the next month, if so we need
		to calculate the month and year.
*/

function constructSelectedDate(cellObj){
	if (cellObj.className == (skin + "selectedDay"))
		return true;
	_Year = parseInt(document.calendar.Year.value);
	_Month = parseInt(document.calendar.Month.value);
	_Day = cellObj.innerText;
	
	displayYear = _Year;
	displayMonth = _Month;
	
	switch(cellObj.className){
	case skin + "nextMonth" : 
					  if (_Month==11){
						 displayMonth = 0;
						 displayYear = _Year + 1;
						 }
					  else
						displayMonth = _Month + 1;
					  break;
	case skin + "prevMonth" : 
						if (_Month==0){
							displayMonth = 11;
							displayYear = _Year - 1;
						}
						else
							displayMonth = _Month - 1;
						break;
	}
	
	selectedYear  = displayYear;
	selectedMonth = displayMonth;
	selectedDay   = parseInt(_Day);
	
	_monthName = eval('monthNames' + calendarLang)[displayMonth];
	firstCell = displayYear;
	secondCell= _Day;
	if (calendarLang==ENGLISH){
		firstCell = _Day;
		secondCell= displayYear;
	}
	// creates the display format
	resultHTML = "<TABLE border=0 cellspaccing=0 cellpadding=0><TR>";
	resultHTML +="<TD class=selectedDayText>&nbsp;" + firstCell + "&nbsp;</TD>";
	resultHTML +="<TD class=selectedDayText>&nbsp;" + _monthName + "&nbsp;</TD>";
	resultHTML +="<TD class=selectedDayText>&nbsp;" + secondCell + "&nbsp;</TD>";
	resultHTML +="</TR></TABLE>";
	//selectedDate.innerHTML = resultHTML;	
	if (selectedCellObj!=null){
		selectedCellObj.className = selectedCellStyle;
	}
	selectedCellObj = cellObj;
	selectedCellStyle = cellObj.className;
	cellObj.className = skin + "selectedDay";
	setOkAlt();
}

/*
Name : showCalendar
Input: The object that the final date value should be placed into
Desc : The method displays the calendar object in the coordinates where
		the mouse was pressed.
*/

function showCalendar(destObj){
	destX = event.clientX +  document.body.scrollLeft;
	destY = event.clientY +  document.body.scrollTop;
	calendarObj.style.top = destY;
	calendarObj.style.left = destX;
	initCalendar();	
	destObject = destObj;
	calendarObj.style.display = "";
}

/*
Name : setSelectedDate
Input: The cell that was double clicked
Desc : The method creates the final value and its tranfered to the 
		object that was given in the showCalendar method.
*/

function setSelectedDate(cellObj){
	_Year = parseInt(document.calendar.Year.value);
	_Month = parseInt(document.calendar.Month.value);
	if (cellObj==null && selectedCellObj==null){
		cancel();
		return false;
	}else
		cellObj = selectedCellObj;
	_Day = cellObj.innerText;
	displayYear = _Year;
	displayMonth = _Month;
	currentClassName  = cellObj.className;
	if (cellObj.className == (skin + "selectedDay"))
		currentClassName = selectedCellStyle;
	switch(currentClassName){
	case skin + "nextMonth" : 
					  if (_Month==11){
						 displayMonth = 0;
						 displayYear = _Year + 1;
						 }
					  else
						displayMonth = _Month + 1;
					  break;
	case skin + "prevMonth" : 
						if (_Month==0){
							displayMonth = 11;
							displayYear = _Year - 1;
						}
						else
							displayMonth = _Month - 1;
						break;
	}
	
	result = _Day + outputDelimeter + (displayMonth+1) + outputDelimeter + displayYear;
	
	if (destObject!=null){
		destObject.value = result;
		destObject=null;
		if (parentRef == null)	
			calendarObj.style.display = "none";
		else{
			parentRef.style.display = "none";
			parentRef = null;
		}
			
	}
}

/*
Name : cancel
Input: 
Desc : remove the calendar from the screen (make it invisible)
*/

function cancel(){
	destObject=null;
	if (parentRef == null)	
		calendarObj.style.display = "none";
	else{
		parentRef.style.display = "none";
		parentRef = null;
	}
}
/*
Name : jumpMonth
Input: mode : FORWARD or BACKWARD
Desc : The method transfers the calendar to the next or previous month
*/

function jumpMonth(mode){
	var jumpToMonth,jumpToYear;
	_Year = parseInt(document.calendar.Year.value);
	_Month = parseInt(document.calendar.Month.value);
	jumpToYear = _Year;
	if (mode==FORWARD){
		if (_Month==11){
			jumpToMonth = 0;
			jumpToYear = _Year + 1;
		}
		else
			jumpToMonth = _Month + 1;
	}else{
			if (_Month==0){
			jumpToMonth = 11;
			jumpToYear = _Year - 1;
		}
		else
			jumpToMonth = _Month - 1;
	}
	createCalendar(jumpToYear,jumpToMonth);
	setSelectVal(document.calendar.Year,jumpToYear);
	setSelectVal(document.calendar.Month,jumpToMonth);
}

/*
Name : jumpYear
Input: mode : FORWARD or BACKWARD
Desc : The method transfers the calendar to the next or previous year
*/

function jumpYear(mode){
	var jumpToMonth,jumpToYear;
	_Year = parseInt(document.calendar.Year.value);
	_Month = parseInt(document.calendar.Month.value);
	jumpToMonth = _Month;
	(mode==FORWARD)?jumpToYear=_Year+1:jumpToYear=_Year-1;
	createCalendar(jumpToYear,jumpToMonth);
	setSelectVal(document.calendar.Year,jumpToYear);
	setSelectVal(document.calendar.Month,jumpToMonth);
}

/*
Name : createMonthList
Input: 
Desc : Create the month list
*/
function createMonthList(){
	for (i=1;i<=12;i++)
		document.write("<OPTION VALUE=" + (i-1) +  ">" + eval("monthNames" + calendarLang+"[i-1]"));	
}

/*
Name : createYearList
Input: 
Desc : Create the year list
Modified: 21.08.2006 - make the year strat from 1999
*/
function createYearList(){
	currentDate = new Date();
	currentYear = currentDate.getYear();
	//for (i=currentYear - 5 ;i<=currentYear + 5;i++)
	for (i=1999 ;i<=currentYear + 5;i++)
		document.write("<OPTION VALUE=" + i + ">" + i);
}

function setLocation(){
	destX = event.clientX +  document.body.scrollLeft;
	destY = event.clientY +  document.body.scrollTop;
	calendarObj.style.top = destY;
	calendarObj.style.left = destX;
}

function setCalendarHeader(headerStyle){
	var sizeFixStart = "";
	var sizeFixEnd	 = "";
	calendarDays = eval("daysNames" + calendarLang);
	if (calendarLang == ENGLISH){
		sizeFixStart = "<font size=-3>";
		sizeFixEnd	 = "</font>";
	}
	for (i=0;i<7;i++){
		document.write("<TH class="+ headerStyle +">"+ sizeFixStart + calendarDays[i]+ sizeFixEnd + "</TH>");
	}
}

function setCalendarBody(bodyStyle){
	for (j=0;j<=5;j++){
		document.write("<TR>");
		if (calendarLang == HEBREW)
			for (i=6;i>=0;i--){
				startNum = new String(i);
				document.write("<TD id='Day"+ startNum + j + "' class=" + bodyStyle + " onClick=constructSelectedDate(this) onDblClick=setSelectedDate(this)>&nbsp;</TD>");
			}
		else
			for (i=0;i<=6;i++){
				startNum = new String(i);
				document.write("<TD id='Day"+ startNum + j + "' class=" + bodyStyle + " onClick=constructSelectedDate(this) onDblClick=setSelectedDate(this)>&nbsp;</TD>");
			}
		document.write("</TR>");
	}
}

function setCalendarLabel(location){
	document.write(eval('labels' + calendarLang + "[" + location + "]"));
}

function setSkin(skinType){
	if (skinType!=skin){
		skin = skinType;
		createCalendar(shownYear,shownMonth);
		if (skinType==_2D_SKIN)
			document.all._2dSkinBtn.src = "../images/ICalendar/rightUpperButtonFill.gif";
		else
			document.all._2dSkinBtn.src = "../images/ICalendar/rightUpperButton.gif";
		
		if (skinType==_3D_SKIN)
			document.all._3dSkinBtn.src = "../images/ICalendar/rightMiddleButtonFill.gif";
		else
			document.all._3dSkinBtn.src = "../images/ICalendar/rightMiddleButton.gif";
		
		if (skinType==PLAIN_SKIN)
			document.all.plainSkinBtn.src = "../images/ICalendar/rightBottomButtonFill.gif";
		else
			document.all.plainSkinBtn.src = "../images/ICalendar/rightBottomButton.gif";
		
		if (selectedCellObj!=null){
			if (selectedCellStyle.indexOf(_2D_SKIN)>-1 || selectedCellStyle.indexOf(_3D_SKIN)>-1){
				selectedCellStyle = skin + selectedCellStyle.substring(2,selectedCellStyle.length);
			}
			else
				selectedCellStyle = skin + selectedCellStyle;
				
			selectedCellObj.className = skin + "selectedDay";	
		}
		
		document.all.selectedHelp.className = skin + "selectedDay";
		document.all.currentHelp.className  = skin + "currentMonth";
		document.all.nextHelp.className		= skin + "nextMonth";
		document.all.prevHelp.className		= skin + "prevMonth";
	}
}

function toggleHelp(){
	if (document.all.help.style.display==""){
		document.all.help.style.display="none";
		document.all.mainCalendarArea.style.display="";
	}else{
		document.all.help.style.display=""
		document.all.mainCalendarArea.style.display="none";
	}

}

function setOkAlt(){
	document.all.OKButton.alt = selectedDay + outputDelimeter + (selectedMonth+1) + outputDelimeter + selectedYear;
}
