/*	
* WEBSOL VALIDATION CODE
* COPYRIGHT 2003 WEBSOL LIMITED. NO PART OF THIS SOURCECODE MAY BE REPRODUCED IN ANY FORM WITHOUT THE WRITTEN PERMISSION OF WEBSOL LTD 
*
* CREATED BY: NEJG 18/MAR/2004
* LAST MODIFIED BY: NEJG 13/SEP/2004
* PATH: /JS/NCAL.JS
* DESCRIPTION: THIS FILE IS USED THROUGHOUT THE SYSTEM FOR CALENDAR INPUT.
*/
/*****************************************************************************************************/
/*----------------------------START OF CALENDAR FUNCTIONS AND VARIABLES-----------------------------*/	
/*****************************************************************************************************/	
	var ctidCounter = 0;
	var ctCalArr = new Array();
	var myWidth = 0, myHeight = 0;
	var scrOfX = 0, scrOfY = 0;
	function getWindowSize() {
		getScrollXY()
		if( typeof( window.innerWidth ) == 'number' ) 
		{
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if(document.documentElement &&	( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
		{
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		//window.alert( 'Width = ' + myWidth );
		//window.alert( 'Height = ' + myHeight );
	}
function getScrollXY() {
	scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement &&
		( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
	}
}

	function createCal(inputBox){
			if (typeof(inputBox) =="string") inputBox = document.getElementById(inputBox);
			if (inputBox.id == "") inputBox.id = "AUTOGENID" + ctidCounter++;
			if(!inputBox.calendar)
			{
				//Declare and Create the elments of the Calendar div
				var CalendarDiv, calendarMonthTable, calendarDayTable, calendarYearTable, calendarYearSelect, calendarYearButton, clearButton
				
				CalendarDiv = document.createElement("DIV");
				CalendarDiv.style.position = "relative";
				CalendarDiv.style.zIndex  = 50;
				CalendarDivIFrame = document.createElement("IFRAME");
				CalendarDivIFrame.style.display = "none";
				CalendarDivIFrame.style.position = "absolute";
				CalendarDivIFrame.style.borderWidth  = 0;
				CalendarDivIFrame.style.left = 0;
				CalendarDivIFrame.style.top = 0;
				CalendarDivIFrame.style.zIndex  = 49;
				
				
				calendarMonthTable = document.createElement("TABLE");
				calendarDayTable = document.createElement("TABLE");
				calendarYearTable = document.createElement("TABLE");
				calendarTable = document.createElement("TABLE");
				
				calendarMonthTable.className = "calendar";
				calendarDayTable.className = "calendar";
				calendarYearTable.className = "calendar";
				calendarTable.className = "calendar";/**/
				
				calendarYearSelect = document.createElement("SELECT");
				calendarYearButton = document.createElement("INPUT");
				calendarYearButton.type = "Button"
				
				clearButton = document.createElement("INPUT");
				clearButton.type = "Button"
				
				CalendarDiv.id = inputBox.id + "Calendar";
				CalendarDiv.className = "calendar";
				CalendarDiv.style.display = "none";
				CalendarDiv.style.position = "absolute";
				
				calendarMonthTable.id = inputBox.id + "CalendarMonthTable";
				calendarDayTable.id = inputBox.id + "CalendarDayTable";
				calendarYearTable.id = inputBox.id + "CalendarYearTable";
				
				calendarDayTable.width = "100%";
				calendarYearTable.width = "100%";
				
				calendarYearSelect.id = inputBox.id + "CalendarYearSelect";
				calendarYearSelect.className =  "calendarSelect";
				calendarYearSelect.onchange  = new Function("setCalYear(\""+ inputBox.id +"\")"); 
				
				calendarYearButton.value = "Done";
				calendarYearButton.onclick =  new Function("hideCalendar()");
				calendarYearButton.className = "CalendarButton"
				
				clearButton.value = "Clear";
				clearButton.onclick =  new Function("clearCalendar(\"" + inputBox.id + "\")"); 
				clearButton.className = "CalendarButton"
				
				//Assemble the Div
				inputBox.parentNode.appendChild(CalendarDiv);
				inputBox.parentNode.appendChild(CalendarDivIFrame);
				CalendarDiv.appendChild(calendarTable);
				
				calendarTable.insertRow(-1);
				calendarTable.rows[0].insertCell(-1);
				calendarTable.rows[0].cells[0].appendChild(calendarMonthTable);
				calendarTable.insertRow(-1);
				calendarTable.rows[1].insertCell(-1);
				calendarTable.rows[1].cells[0].appendChild(calendarYearTable);
				calendarYearTable.insertRow(-1);
				calendarYearTable.rows[0].insertCell(-1);
				calendarYearTable.rows[0].cells[0].align = "center";
				calendarYearTable.rows[0].cells[0].appendChild(calendarYearSelect);
				calendarYearTable.rows[0].insertCell(-1);
				calendarYearTable.rows[0].cells[1].align = "center";
				calendarYearTable.rows[0].cells[1].appendChild(calendarYearButton);
				var buttonSpace = document.createElement("SPAN")
				buttonSpace.innerHTML = ("&nbsp;&nbsp;&nbsp;");
				calendarYearTable.rows[0].cells[1].appendChild(buttonSpace);
				calendarYearTable.rows[0].cells[1].appendChild(clearButton);
				calendarTable.insertRow(-1);
				calendarTable.rows[2].insertCell(-1);
				calendarTable.rows[2].cells[0].appendChild(calendarDayTable);
				
				calendarTable.rows[0].cells[0].className = "calendar";
				calendarTable.rows[1].cells[0].className = "calendar";
				calendarTable.rows[2].cells[0].className = "calendar";
				calendarYearTable.rows[0].cells[0].className = "calendar";
				calendarYearTable.rows[0].cells[1].className = "calendar";
				/**/
				
				inputBox.calendar = new calObject(inputBox, CalendarDiv, CalendarDivIFrame, calendarMonthTable, calendarYearSelect, calendarDayTable);
				ctCalArr.push(inputBox.calendar)
				CalendarDiv.style.top = inputBox.offsetTop// - 50
			}
			showCalendar(inputBox.id);		
	}
	function calObject(inputBox, calDiv, calDivIF, calMonth, calYear, calDay){
		this.inputB = inputBox;
		var tempDate = new RegExp(  /([0123]?[0-9]{1})\W([01]?[0-9]{1})\W(\d{2,4})/ );//
		tempDate.test(inputBox.value)
		if(tempDate.test(inputBox.value))
		{
			this.currDate = new Date(RegExp.$3,  RegExp.$2 - 1, RegExp.$1);
		}else{
			this.currDate = new Date()
		}
		this.cal = calDiv;
		this.calIF = calDivIF;
		this.calM = calMonth;
		this.calY = calYear;
		this.calD = calDay;
		this.currDay = this.currDate.getDate();
		this.currMonth = this.currDate.getMonth();
		this.currYear = this.currDate.getFullYear();
		this.show = function(){this.cal.style.display = "inline";this.calIF.style.display = "inline";}
		this.hide = function(){this.cal.style.display = "none"; this.calIF.style.display = "none"}
		var oOption;
		for(var i = 1900; i<= (new Date().getFullYear() + 1); i++){
			oOption = document.createElement("OPTION");
			this.calY.appendChild(oOption);
			oOption.innerHTML = oOption.value = i;
			if(i == this.currYear) oOption.selected = true;
		}
		this.calM.insertRow(-1);
		this.calM.insertRow(-1);
		for(var i=0;i<this.calM.rows.length;i++)for(var j=0;j<6;j++)this.calM.rows[i].insertCell(-1);
		var tdtable = this.calM.getElementsByTagName("TD");
		for (var i=0; i< tdtable.length; i++)
		{
			tdtable[i].onclick = new Function("setCalMonth(\"" + this.inputB.id + "\", " + i + ")")
			tdtable[i].innerHTML = intToMonth(i);
			tdtable[i].className = tdtable[i].innerHTML == intToMonth(this.currMonth)?"currMonth":"othMonth";
		}
	}

	function setCalYear(calInputId){
		var inputBox = document.getElementById(calInputId);
		inputBox.calendar.currYear = inputBox.calendar.calY.options[inputBox.calendar.calY.selectedIndex].value;
		arrange(calInputId);
	}

	function setCalMonth(calInputId, monthIndex){
		var inputBox = document.getElementById(calInputId);
		inputBox.calendar.currMonth = monthIndex;
		arrange(calInputId);
	}

	function setCalDay(calInputId, dayIndex){
		var inputBox = document.getElementById(calInputId);
		inputBox.calendar.currDay = dayIndex;
		arrange(calInputId);
	}
	
	function showCalendar(calInputId)
	{
		getWindowSize();
		var tempDisplay = document.getElementById(calInputId).calendar.cal.style.display;
		for(vartheCals in ctCalArr){
			ctCalArr[vartheCals].hide();
		}
		arrange(calInputId);
		
		if (tempDisplay == "none")
		{
			var tepCalendar = document.getElementById(calInputId).calendar;
			tepCalendar.show();
			if(myWidth < tepCalendar.cal.offsetWidth || myHeight < tepCalendar.cal.offsetHeight)
			self.resizeTo(myWidth < tepCalendar.cal.offsetWidth ? tepCalendar.cal.offsetWidth : myWidth+10,
				myHeight < tepCalendar.cal.offsetHeight ? tepCalendar.cal.offsetHeight+30 : myHeight+30);
			getWindowSize();
			/***********************************************************************
			//Un-comment for use in Netaca
			***********************************************************************/
			//*
			getWindowSize();
			tepCalendar.cal.style.left = ((myWidth - tepCalendar.cal.offsetWidth)/2) + scrOfX;
			tepCalendar.cal.style.top = ((myHeight - tepCalendar.cal.offsetHeight)/2) + scrOfY;
			//*/
			tepCalendar.calIF.style.left = tepCalendar.cal.offsetLeft;
			tepCalendar.calIF.style.top = tepCalendar.cal.offsetTop;
			tepCalendar.calIF.style.width = tepCalendar.cal.offsetWidth;
			tepCalendar.calIF.style.height = tepCalendar.cal.offsetHeight;
			tepCalendar.calIF.style.display = "inline";
		}
	}
	
	function hideCalendar()
	{
		for(vartheCals in ctCalArr){
			ctCalArr[vartheCals].hide();
		}
	}
	
	function clearCalendar(inputToClear){
		document.getElementById(inputToClear).value = "";
		hideCalendar();
	}
	
	function arrange(calInputBoxId)
	{
		var cal = document.getElementById(calInputBoxId).calendar;
		cal.currDate.setMonth(cal.currMonth);
		cal.currDate.setDate(cal.currDay);
		cal.currDate.setYear(cal.currYear);
		if (cal.currMonth != cal.currDate.getMonth())
		{
			cal.currDay = 1;
			cal.currDate.setDate(cal.currDay);
			cal.currDate.setMonth(cal.currMonth);
		}
		var ttable = cal.calD;
		var tdtable = cal.calM;
		for(var i=0;i<tdtable.rows.length;i++)
		{
			for(var j=0;j<6;j++)
			{
				tdtable.rows[i].cells[j].className = tdtable.rows[i].cells[j].innerHTML == intToMonth(cal.currMonth)?"currMonth":"othMonth";
			}
		}
		while(ttable.rows.length>0) ttable.deleteRow(0);
		ttable.insertRow(-1);
		for(var j = 0; j<7; j++)
		{
			ttable.rows[0].insertCell(-1);
			ttable.rows[0].cells[j].className = "days";
		}
		ttable.rows[0].cells[0].innerHTML = "M";
		ttable.rows[0].cells[1].innerHTML = "T";
		ttable.rows[0].cells[2].innerHTML = "W";
		ttable.rows[0].cells[3].innerHTML = "Th";
		ttable.rows[0].cells[4].innerHTML = "F";
		ttable.rows[0].cells[5].innerHTML = "S";
		ttable.rows[0].cells[6].innerHTML = "Su";
		dayCount = new Date(cal.currDate);
		dayCount.setDate(1);
		dayCount = 2 - dayCount.getDay();
		if(dayCount > 1) dayCount -= 7;
		for(var j = 0; j<7; j++)
		{
			ttable.rows[0].cells[j].className = "days";
		}
		var mLength = monthLength(cal.currDate);
		for(var i = 1; dayCount <= mLength;i++)
		{
			ttable.insertRow(-1);
			var trow = ttable.rows[ttable.rows.length-1];
			for(j = 0; j<7; j++)
			{
				trow.insertCell(-1);
				var tdata = trow.cells[trow.cells.length-1];
				tdata.innerHTML = (dayCount > 0 && dayCount <= mLength)? dayCount:"";
				tdata.className = (dayCount == cal.currDay)?"currDay":"othDay";
				
				tdata.onclick = (dayCount > 0 && dayCount <= mLength)?
					new Function("setCalDay(\"" + cal.inputB.id + "\", " + dayCount + ")")
					:
					function()
					{}
				dayCount++;
			}
		}
		cal.inputB.value = (cal.currDay < 10?"0":"") + cal.currDate.getDate() + "/" + (cal.currMonth < 9?"0":"") + (cal.currDate.getMonth()+1) + "/" + cal.currYear;
		cal.calIF.style.left = cal.cal.offsetLeft;
		cal.calIF.style.top = cal.cal.offsetTop;
		cal.calIF.style.width = cal.cal.offsetWidth;
		cal.calIF.style.height = cal.cal.offsetHeight;
	}
	
	

	function intToMonth(month){
		month = month%12;
		switch(month){
			case 0:
				return "Jan";
			case 1:
				return "Feb";
			case 2:
				return "Mar";
			case 3:
				return "Apr";
			case 4:
				return "May";
			case 5:
				return "Jun";
			case 6:
				return "Jul";
			case 7:
				return "Aug";
			case 8:
				return "Sep";
			case 9:
				return "Oct";
			case 10:
				return "Nov";
			case 11:
				return "Dec";
		}
	}
	
	function monthToInt(month){
		switch(month){
			case "Jan":
				return 0;
			case "Feb":
				return 1;
			case "Mar":
				return 2;
			case "Apr":
				return 3;
			case "May":
				return 4;
			case "Jun":
				return 5;
			case "Jul":
				return 6;
			case "Aug":
				return 7;
			case "Sep":
				return 8;
			case "Oct":
				return 9;
			case "Nov":
				return 10;
			case "Dec":
				return 11;
		}
	}
	
	function monthLength(dateM)
	{
		switch(dateM.getMonth()){
			case 0:
			case 2:
			case 4:
			case 6:
			case 7:
			case 9:
			case 11:
				return 31;
			case 3:
			case 5:
			case 8:
			case 10:
				return 30
			case 1:
				return (dateM.getFullYear()%4==0)? 29:28;
				return 30;
		}
	}
/*****************************************************************************************************/
/*-----------------------------END OF CALENDAR FUNCTIONS AND VARIABLES------------------------------*/	
/*****************************************************************************************************/	
