
var Calendar = (function() 
{
	return {		

		Buffer: '',
		TextElement: '', 
		
		MonthNames: new Array('Ianuarie', 'Februarie', 'Martie', 'Aprilie', 'Mai', 'Iunie', 'Iulie', 'August', 'Septembrie', 'Octombrie', 'Noiembrie', 'Decembrie'),
		DayNames: new Array('Du', 'Lu', 'Ma', 'Mi', 'Jo', 'Vi', 'Sa'),
		
		Initialize: function()
		{
			this.InitialDate = new Date();
			this.CurrentMonth = (this.InitialDate.getMonth() + 1);
			this.CurrentYear = this.InitialDate.getFullYear();
			this.CalendarContentName = 'CalendarContent';
			this.CalendarContainerName = 'CalendarContainer';
			this.CalendarContent = DOM.GetElement(this.CalendarContentName);			
			this.CalendarContainer = DOM.GetElement(this.CalendarContainerName);
			
		},
		
		CalendarHeader: function(t, SelectNamePreffix) 
		{
			return '<table cellpadding="0" cellspacing="0">'
					 + '<thead>'
				     + '	<tr>'				     
					 + '		<td style="cursor: pointer" onclick="Calendar.PreviousYear(\'' + SelectNamePreffix + '\');" class="CalendarOptions"><a class="generalwhitetext">&lt;&lt;</a></td>'
					 + '		<td style="cursor: pointer" onclick="Calendar.PreviousMonth(\'' + SelectNamePreffix + '\');" class="CalendarOptions"><a class="generalwhitetext">&lt;</a></td>'
					 + '		<td style="cursor: pointer" onclick="Calendar.HideCalendar();" colspan="3" class="CalendarOptions"><a class="generalwhitetext">X</a></td>'
					 + '		<td style="cursor: pointer" onclick="Calendar.NextMonth(\'' + SelectNamePreffix + '\');" class="CalendarOptions"><a class="generalwhitetext">&gt;</a></td>'
					 + '		<td style="cursor: pointer" onclick="Calendar.NextYear(\'' + SelectNamePreffix + '\');" class="CalendarOptions"><a class="generalwhitetext">&gt;&gt;</a></td>'
					 + '	</tr>'					 
				     + '	<tr>'
					 + '		<td colspan="7" id="CalendarCurrent"><a class="generalbluetext">' + t + '</a></td>'
					 + '	</tr>'
					 + '</thead>'
					 + '<tbody>'
					 + '	<tr>';
					 
		},
		
		CalendarDayRow: function(t) 
		{
			return '<td class="CalendarDays"><a class="generalgreentext">' + t + '</a></td>';
		
		},
		
		CalendarNewWeek: function() 
		{
			return '   </tr>'
				    + '<tr>';
			
		},
		
		CalendarBlankCell: function(colspan) 
		{
			return '<td colspan="' + colspan + '"></td>'
			
		},
		
		CalendarDay: function(d, m, y, SelectNamePreffix) 
		{			
			return '<td class="CalendarNumbers" onclick="Calendar.SelectDay(' + d + ',' + m + ',' + y + ',\'' + SelectNamePreffix + '\');">' + d + '</td>';
		
		},
		
		CalendarFooter: function() 
		{
			return '		    </tr>'
		    		+ '    </tbody>'
			        + '</table>';
			     
		},
		
		CreateCalendar: function(m, y, SelectNamePreffix) 
		{
			this.Buffer = '';
			this.Buffer = this.Buffer + (this.CalendarHeader(this.MonthNames[m - 1] + ' ' + y, SelectNamePreffix));
			
			for (var i = 0; i < 7; i++) 
				this.Buffer = this.Buffer + (this.CalendarDayRow(this.DayNames[i]));
						
			var PointerDate = new Date();
			PointerDate.setMonth(m - 1);
			PointerDate.setFullYear(y);
			PointerDate.setDate(1);
			
			var MonthDays = 0;
			if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) MonthDays = 31;
				else if (m == 4 || m == 6 || m == 9 || m == 11) MonthDays = 30;
					else MonthDays = (y % 4 == 0) ? 29 : 28;
			
			var FirstDay = PointerDate.getDay();
			var FirstLoop = 1;
			
			this.Buffer = this.Buffer + (this.CalendarNewWeek());
			if (FirstDay != 0) 
				this.Buffer = this.Buffer + (this.CalendarBlankCell(FirstDay));
			
			var j = FirstDay;
			
			for (var i = 0; i < MonthDays; i++) 
			{				
				if (j == 0 && !FirstLoop) 
					this.Buffer = this.Buffer + (this.CalendarNewWeek());
				
				this.Buffer = this.Buffer + (this.CalendarDay(i + 1, m, y, SelectNamePreffix));
				
				FirstLoop = 0;
				j++;
				j = j % 7;
				
			}
			
			this.Buffer = this.Buffer + (this.CalendarFooter());
			
			this.CalendarContent.innerHTML = this.Buffer;
			this.Buffer = '';
			
			this.CalendarContainer.scrollIntoView();
			
		},
		
		ShowCalendar: function(t, Bottom, SelectNamePreffix) 
		{
			if (typeof(Bottom) == 'undefined')
				Bottom = 1;
				
			if (typeof(SelectNamePreffix) == 'undefined')
				SelectNamePreffix = 0;
				
			this.TextElement = t;
			
			var ShowDate = new Date();
			this.CurrentMonth = ShowDate.getMonth() + 1;
			this.CurrentYear = ShowDate.getFullYear();
			
			this.CreateCalendar(this.CurrentMonth, this.CurrentYear, SelectNamePreffix);
			
			DOM.SwitchVisibilityElement(this.CalendarContainerName, 1);
			
			var Position = DOM.GetAbsolutePosition(t);
			var LeftWidth = Position[0];
			var TopHeight = 0;
			
			if (Bottom == 1) TopHeight = Position[1] + t.offsetHeight;
				else TopHeight = Position[1] - t.offsetHeight - 170;
				
			this.CalendarContainer.style.left = LeftWidth + 'px';
			this.CalendarContainer.style.top = (TopHeight + 5) + 'px';
			
			this.CalendarContainer.scrollIntoView();
			
		},
		
		HideCalendar: function() 
		{
			DOM.SwitchVisibilityElement(this.CalendarContainerName, 0);
			
		},
		
		NextMonth: function(SelectNamePreffix) 
		{
			this.CurrentMonth++;
			
			if (this.CurrentMonth > 12)
			{
				this.CurrentMonth = 1;
				this.CurrentYear = this.CurrentYear + 1;
				
			}
			
			this.CreateCalendar(this.CurrentMonth, this.CurrentYear, SelectNamePreffix);
			
		},
		
		PreviousMonth: function(SelectNamePreffix) 
		{
			this.CurrentMonth = this.CurrentMonth - 1;
			
			if (this.CurrentMonth < 1)
			{
				this.CurrentMonth = 12;
				this.CurrentYear = this.CurrentYear - 1;
				
			}
			
			this.CreateCalendar(this.CurrentMonth, this.CurrentYear, SelectNamePreffix);
			
		},
		
		NextYear: function(SelectNamePreffix)
		{			
			this.CurrentYear = this.CurrentYear + 1;
			this.CreateCalendar(this.CurrentMonth, this.CurrentYear, SelectNamePreffix);
			
		},
				
		PreviousYear: function(SelectNamePreffix) 
		{
			this.CurrentYear = this.CurrentYear - 1;
			this.CreateCalendar(this.CurrentMonth, this.CurrentYear, SelectNamePreffix);
			
		},
		
		FormatDate: function(d, m, y, format) 
		{			
			if (typeof(format) == 'undefined')
				var format = 'DD-MM-YYYY';
				
			m = this.FirstZero(m);
			d = this.FirstZero(d);
			
			var date = format.replace(/DD/g, d).replace(/MM/g, m).replace(/YYYY/g, y);

			return date;
			
		},
				
		SelectDay: function (d, m, y, SelectNamePreffix) 
		{	
			this.HideCalendar();
			
			if (SelectNamePreffix == 0)
			{
				if (typeof(this.TextElement.value) != 'undefined') this.TextElement.value = this.FormatDate(d, m, y);
					else if (typeof(this.TextElement.innerHTML) != 'undefined') this.TextElement.innerHTML = this.FormatDate(d, m, y);
						else alert(this.FormatDate(d, m, y));
						
			}
			else
			{
				DOM.SetSelectedValue(SelectNamePreffix + 'Day', d);	
				DOM.SetSelectedValue(SelectNamePreffix + 'Month', m);	
				DOM.SetSelectedValue(SelectNamePreffix + 'Year', y);	
				
			}
			
		},
		
		FirstZero: function(number) 
		{
   			return (number < 10) ? '0' + number : number;
   			
  		},
  
  		DatesInterval: function (StartDateValue, EndDateValue, format) 
  		{
  			if (typeof(format) == 'undefined')
				var format = 'DD-MM-YYYY';
				
    		var DateRE    = /^(\d{1,2})-(\d{1,2})-(\d{2,4})$/;
    		var StartDate = null;
    		var EndDate = null;
    		
    		if (StartDateValue && StartDateValue.match(DateRE)) 
    			var StartDate = new Date(RegExp.$3, RegExp.$2 - 1, RegExp.$1);
    
    		if (EndDateValue && EndDateValue.match(DateRE))
      			var EndDate = new Date(RegExp.$3, RegExp.$2 - 1, RegExp.$1);
    
      		var i = 0;
    		var Result = new Array();
    		    		
    		while (StartDate < EndDate) 
    		{
      			Result[i] = this.FormatDate(StartDate.getDate(), StartDate.getMonth() + 1, StartDate.getFullYear(), format);
      			
      			StartDate.setDate(StartDate.getDate() + 1);

      			i = i + 1;
      			
    		}
    
    		return Result;
    
  		}
		
	};
			
})();
