if(typeof(Controls) == 'undefined')
	var Controls = {};
if(typeof(Controls.UI) == 'undefined')
	Controls.UI = {};
if(typeof(Controls.UI.Calendar) == 'undefined'){
	Controls.UI.Calendar = {};
	
	Controls.UI.Calendar._currentSelectedDate = null;
	Controls.UI.Calendar._openedContainer = null;
	
	Controls.UI.Calendar.GetCalendarObject = function(){
		var Element = document.getElementById("Calendar");
		
		if(Controls.UI.Calendar.CalculateCoordinatesTo && Element.parentNode != Controls.UI.Calendar.CalculateCoordinatesTo){
			Controls.UI.Calendar.CalculateCoordinatesTo.appendChild(Element);
		}
//		if(Element.parentNode.tagName != "body"){
//			var Body = document.getElementsByTagName("body");
//			Body[0].appendChild(Element);
//		}
		
		return Element;
	}
	
	Controls.UI.Calendar.HideCalendar = function(){
		Controls.UI.Calendar.HideCurrentOpenedList();
		var oCalendar = Controls.UI.Calendar.GetCalendarObject();
		if(oCalendar)
			oCalendar.style.display = "none";
	}
	
	Controls.UI.Calendar.Calendar_Click = function(sender, e){
		Controls.UI.Calendar.HideCurrentOpenedList();
		Utilities.Events.CancelBubble(e);
	}
	
	Controls.UI.Calendar.HideCurrentOpenedList = function(){
		if(Controls.UI.Calendar._openedContainer != null){
			Controls.UI.Calendar._openedContainer.style.display = "none";
			Controls.UI.Calendar._openedContainer = null;
		}
	}
	
	Controls.UI.Calendar.SetToday = function(){
		var Field = document.getElementById(window.sOutputName);
		var now = new Date();
		//Field.value = now.getDate() +" " + Controls.UI.Calendar.Months[now.getMonth()] +" "+ now.getFullYear(); 
		Field.value = Controls.UI.Calendar.Months[now.getMonth()] +", "+ now.getDate()+" "+ now.getYear();
		if(Field.fireEvent)
			Field.fireEvent('onchange');
		var oCalendar = Controls.UI.Calendar.GetCalendarObject();
		oCalendar.style.display = "none";
	}

	Controls.UI.Calendar.ShowCalendar = function(oCaller, sOutputName, Evnt){
		window.sOutputName = sOutputName; 
		
		var oCalendar = Controls.UI.Calendar.GetCalendarObject();

		//	if(oCalendar.style.display == ""){
		//		oCalendar.style.display = "none";
		//		return;
		//	}

		oCalendar.style.display = "";
		
		var sOutputDate = document.getElementById(sOutputName).value;
		var nDate = null;
		if(sOutputDate != ""){
			//var arMatch = sOutputDate.match("[0-9]{1,2} ("+Controls.UI.Calendar.Months.join("|")+") [0-9]{4}");
			var arMatch = sOutputDate.match("("+Controls.UI.Calendar.Months.join("|")+"),? ([0-9]{1,2}) ([0-9]{4})");
			
			if(arMatch && arMatch[0].length == sOutputDate.length){
				nDate = arMatch[2];
			}else{
				var CurrentDate = new Date();
				//sOutputDate = CurrentDate.getDate()+" "+Controls.UI.Calendar.Months[CurrentDate.getMonth()]+" "+CurrentDate.getYear();
				nDate = CurrentDate.getDate();
				sOutputDate = Controls.UI.Calendar.Months[CurrentDate.getMonth()] +", "+ CurrentDate.getDate()+" "+ CurrentDate.getYear();
				document.getElementById(sOutputName).value = sOutputDate;
			}

			arDateParts = sOutputDate.replace(",", "").split(" ");
			for (var i=0; i < Controls.UI.Calendar.Months.length; i++){
				if(Controls.UI.Calendar.Months[i] == arDateParts[0]){
					var nMonthInt = i;
					break; 
				} 
			}
			
			Controls.UI.Calendar.ListBoxItemSelect("YearPicker", arDateParts[2], arDateParts[2]);
			Controls.UI.Calendar.ListBoxItemSelect("MonthPicker", Controls.UI.Calendar.Months[nMonthInt], nMonthInt);
		}
		
		var nMonth = document.getElementById("MonthPicker_Hidden").value;
		var nYear = document.getElementById("YearPicker_Hidden").value;

		Controls.UI.Calendar._currentSelectedDate = new Date(nYear, nMonth, nDate);
		Controls.UI.Calendar.RedrawTable(new Date(nYear, nMonth, 1));
		
		//	var DEBUG = document.createElement("DIV");
		//	DEBUG.style.border = "1px solid red";
		//	DEBUG.style.backgroundColor = "white";
		//	DEBUG.style.zIndex = 1000;
		//	DEBUG.style.position = "absolute";
		//	DEBUG.style.top = 10;
		//	DEBUG.style.left = 10;
		//	DEBUG.style.padding = 10;
		//	document.body.appendChild(DEBUG);
		
		arCoordinates = Utilities.DOM.GetCoordinates(oCaller, false, Controls.UI.Calendar.CalculateCoordinatesTo);
		var nLeft = arCoordinates[0];
		var nTop = arCoordinates[1] + oCaller.offsetHeight;
		
		//nLeft -= (oCalendar.offsetWidth - oCaller.offsetWidth - 2);
		//nTop += (oCaller.offsetHeight + 2);
		//nTop += 20;

		oCalendar.style.top = nTop + "px";
		oCalendar.style.left = nLeft + "px";
		
		if(typeof(Evnt.stopPropagation) != "undefined")
			Evnt.stopPropagation();
		if(typeof(Evnt.cancelBubble) != "undefined")
			Evnt.cancelBubble = true;
	}

	Controls.UI.Calendar.RedrawTable = function(CurrentDate, selectedDate){	
		document.getElementById("Row6").style.display="none";

		var Year = CurrentDate.getFullYear();
		var Month = CurrentDate.getMonth();
		var Day = CurrentDate.getDate();
		var DateString = CurrentDate.toLocaleString();
		var DaysInMonth = new Date(Year, Month, 31).getDate();
		var LastDay = (DaysInMonth < 31) ? (31 - DaysInMonth) : 31;  

		CurrentDate.setDate(1);
		var DayOfWeek = CurrentDate.getDay();
		var StartTD = document.getElementById("Day"+ DayOfWeek);

		if((DayOfWeek == 6 && LastDay > 30) || (DayOfWeek == 0 && LastDay > 29))
			document.getElementById("Row6").style.display="";
		
		Controls.UI.Calendar.CreateLink(StartTD, 1, Month , Year);
		CurrentTD = StartTD;
		var nDay;
		var trCount = 2;
		
		//alert(Controls.UI.Calendar._currentSelectedDate);
		
		for(nDay = 2; nDay < LastDay + 1; nDay++){
			if(CurrentTD.id == "Day0" || CurrentTD.id == "RowEnd"){
				CurrentTD = document.getElementById("Row" + trCount).firstChild;
				trCount++;
			}else{
				CurrentTD  = CurrentTD.nextSibling;
			}
			
			if(CurrentTD.tagName != 'TD')
				CurrentTD = CurrentTD.nextSibling;
			
			Controls.UI.Calendar.CreateLink(CurrentTD, nDay, Month, Year);	
		}
	}

	Controls.UI.Calendar.ClearTable = function(){
		CurrentTD = document.getElementById("Row1").firstChild;
		nRow = 2;
		for(nCount = 1; nCount < 41; nCount++){
			if(CurrentTD.tagName != 'TD')
				CurrentTD = CurrentTD.nextSibling;
			CurrentTD.innerHTML = "";//"&nbsp;";
			if(CurrentTD.id == "Day0" || CurrentTD.id == "RowEnd"){
				CurrentTD = document.getElementById("Row" + nRow).cells[0];
				nRow++;
			}else{
				CurrentTD = CurrentTD.nextSibling;
			}
		}
	}

	Controls.UI.Calendar.IsSelectedDate = function(nDate, nMonth, nYear){
		return (Controls.UI.Calendar._currentSelectedDate != null && Controls.UI.Calendar._currentSelectedDate.getFullYear() == nYear && Controls.UI.Calendar._currentSelectedDate.getMonth() == nMonth && Controls.UI.Calendar._currentSelectedDate.getDate() == nDate)
	}
	
	Controls.UI.Calendar.Cell_MouseOver = function(sender, e){
		sender.style.backgroundColor = "#15188C";
		sender.style.color = "#FFFFFF";
	}
	
	Controls.UI.Calendar.Cell_MouseOut = function(sender, e){
		//var date = sender.getAttribute("DateObject");
		var date = sender.DateObject;
		if(date != null && Controls.UI.Calendar.IsSelectedDate(date.getDate(), date.getMonth(), date.getFullYear())){
			sender.style.backgroundColor = "#888888";
			sender.style.color = "white";
		}else{
			sender.style.backgroundColor = "";
			sender.style.color = ""; 
		}
	}
	
	Controls.UI.Calendar.CreateLink = function(CurrentTD, nDay, nMonth, nYear){
		//var sDate = nDay +" " + Controls.UI.Calendar.Months[nMonth] +" "+ nYear; 
		var sDate = Controls.UI.Calendar.Months[nMonth] +", "+ nDay +" " + nYear; 
		//CurrentTD.innerHTML = "<div name='"+ sOutputName +"' style='display: none;'>"+ sDate +"</div><a href='javascript:void(0);' class='CalendarLink' onclick='document.getElementById(\""+ sOutputName +"\").value=\""+ sDate +"\"; document.getElementById(\""+ sOutputName +"\").fireEvent(\"onchange\"); document.getElementById(\"Calendar\").style.display=\"none\";' title='"+ sDate +"'>" + nDay +"</a>";
		CurrentTD.setAttribute("OutputName", window.sOutputName);
		//CurrentTD.setAttribute("DateObject", new Date(nYear, nMonth, nDay));
		CurrentTD.DateObject = new Date(nYear, nMonth, nDay);
		CurrentTD.setAttribute("Date", sDate);
		CurrentTD.style.cursor = "pointer";
		CurrentTD.title = sDate;
		CurrentTD.innerHTML = nDay;
		//alert(Controls.UI.Calendar._currentSelectedDate +" == "+ new Date(nYear, nMonth, nDay));
		if(Controls.UI.Calendar.IsSelectedDate(nDay, nMonth, nYear)){
			CurrentTD.style.backgroundColor = "#888888";
			CurrentTD.style.color = "white";
		}
	}

	Controls.UI.Calendar.Months= new Array();			
	Controls.UI.Calendar.Months[0] = "Jan";
	Controls.UI.Calendar.Months[1] = "Feb";
	Controls.UI.Calendar.Months[2] = "Mar";
	Controls.UI.Calendar.Months[3] = "Apr";
	Controls.UI.Calendar.Months[4] = "May";
	Controls.UI.Calendar.Months[5] = "Jun";
	Controls.UI.Calendar.Months[6] = "Jul";
	Controls.UI.Calendar.Months[7] = "Aug";
	Controls.UI.Calendar.Months[8] = "Sept";
	Controls.UI.Calendar.Months[9] = "Oct";
	Controls.UI.Calendar.Months[10] = "Nov";
	Controls.UI.Calendar.Months[11] = "Dec";	


	//////////////  LISTBOX /////////////////////////////////////////////////////
	Controls.UI.Calendar.ShowListBoxContainer = function(ObjectName){
		var ListBoxInput = document.getElementById(ObjectName);
		var ListBoxContainer =  document.getElementById(ObjectName + "_Container");
		// Positioning
		Controls.UI.Calendar.HideCurrentOpenedList();
		ListBoxContainer.style.display = "";
		ListBoxContainer.style.top = (ListBoxInput.offsetTop + ListBoxInput.offsetHeight) + "px";
		ListBoxContainer.style.left = (ListBoxInput.offsetLeft) + "px";
		Controls.UI.Calendar._openedContainer = ListBoxContainer;
	}

	Controls.UI.Calendar.ListBoxItemSelect = function(ObjectName, ItemName, ItemValue){
		var ListBoxInput = document.getElementById(ObjectName);
		var ListBoxContainer = document.getElementById(ObjectName + "_Container");
		var ListBoxHiddenInput = document.getElementById(ObjectName + "_Hidden");

		ListBoxInput.value = ItemName;
		ListBoxHiddenInput.value = ItemValue;
		ListBoxContainer.style.display = "none";
		Controls.UI.Calendar._openedContainer = null;
		Controls.UI.Calendar.ClearTable(); 

		Controls.UI.Calendar.RedrawTable(new Date(document.getElementById("YearPicker_Hidden").value, document.getElementById("MonthPicker_Hidden").value, 1));
	}

	Controls.UI.Calendar.ListBoxOver = function(Object){
		Object.style.textDecoration = "underline";
	}

	Controls.UI.Calendar.ListBoxOut = function(Object){
		Object.style.textDecoration = "none";
	}
}
