﻿  function SelectDate(calendarElementId, elementId, dateString)
  {
    var element = document.getElementById(elementId);
    element.value = dateString;
    
    CloseCalendar(calendarElementId);
  }
  
  function CloseCalendar(calendarElementId)
  {
    var calendarElement = document.getElementById(calendarElementId);
    calendarElement.parentNode.removeChild(calendarElement);
  }

  function ShowElement(elementId)
  {
    if (document.getElementById(elementId) != null)
    {
      document.getElementById(elementId).style.visibility = 'visible';
    }
  }

  /* PopupCalendar class */
  function PopupCalendar(returnToElementId, dateFormat, startDate, elementToHideId)
  {
    this.Today = new Date().getDatePart();

    var returnToElement = document.getElementById(returnToElementId);

    if(startDate)
    {
      this.SelectedDate = startDate;
    }
    else if(returnToElement.value.length > 0)
    {
      var dateAr = returnToElement.value.split('-');
      var dateMonth = (dateAr[1] > 0) ? dateAr[1] -1 : 0;
      this.SelectedDate = new Date(dateAr[2],dateMonth,dateAr[0]);
    }
    else
    {
      this.SelectedDate = this.Today;
    }

    this.SelectedMonth = this.SelectedDate.getMonth();
    this.SelectedYear = this.SelectedDate.getFullYear();

    this.ReturnToElementId = returnToElementId;
    this.ElementToHideId = elementToHideId;
    this.DateFormat = dateFormat;
    this.SetUniqueId();

    this.HideElement();
    this.ShowCalendar();
  }

  PopupCalendar.prototype.SetUniqueId = function()
  {
    do
    {
      this.UniqueId = 'CalendarTable'+Math.round(Math.random()*1000000);
    } 
    while(document.getElementById(this.UniqueId));
  }

  PopupCalendar.prototype.HideElement = function()
  {
    if (document.getElementById(this.ElementToHideId) != null)
    {
      document.getElementById(this.ElementToHideId).style.visibility = 'hidden';
    }
  }

  PopupCalendar.prototype.ShowCalendar = function()
  {
    document.body.appendChild(this.CreateCalendarDomObject());
  }
  
  PopupCalendar.prototype.CreateCalendarDomObject = function()
  {
    var offset = new Offset(document.getElementById(this.ReturnToElementId));
    var table = document.createElement('table');
    table.className = 'CalendarTable';
    table.id = this.UniqueId;
    table.style.top = offset.getPixelsY();
    table.style.left = offset.getPixelsX();
    
    table.appendChild(this.CreateCalendarTableHeader());
    table.appendChild(this.CreateCalendarTableBody());
    
    return table;
  }
  
  PopupCalendar.prototype.CreateCalendarTableHeader = function()
  {
    var thead = document.createElement('thead');

    var trYear = this.CreateElement(thead, 'tr');

    this.CreateElement(trYear, 'td',null ,'<a href="javascript:void(0)" onclick="CloseCalendar(\''+this.UniqueId+'\');new PopupCalendar(\''+this.ReturnToElementId+'\',\''+this.DateFormat+'\',new Date('+(this.SelectedDate.getFullYear()-1)+','+this.SelectedDate.getMonth()+'),\''+this.ElementToHideId+'\')"><img src="/images/pil_venstre.gif"/></a>');
    
    var yearNameTd = this.CreateElement(trYear, 'td',"MonthName",this.SelectedDate.getFullYear());
    yearNameTd.colSpan = 6;
    
    this.CreateElement(trYear, 'td',null ,'<a href="javascript:void(0)" onclick="CloseCalendar(\''+this.UniqueId+'\');new PopupCalendar(\''+this.ReturnToElementId+'\',\''+this.DateFormat+'\',new Date('+(this.SelectedDate.getFullYear()+1)+','+this.SelectedDate.getMonth()+'),\''+this.ElementToHideId+'\')"><img src="/images/pil_hoejre.gif"/></a>');


    var trMonth = this.CreateElement(thead, 'tr');

    this.CreateElement(trMonth, 'td',null ,'<a href="javascript:void(0)" onclick="CloseCalendar(\''+this.UniqueId+'\');new PopupCalendar(\''+this.ReturnToElementId+'\',\''+this.DateFormat+'\',new Date('+this.SelectedDate.getFullYear()+','+(this.SelectedDate.getMonth()-1)+'),\''+this.ElementToHideId+'\')"><img src="/images/pil_venstre.gif"/></a>');
    
    var monthNameTd = this.CreateElement(trMonth, 'td',"MonthName",this.SelectedDate.getMonthName());
    monthNameTd.colSpan = 6;
    
    this.CreateElement(trMonth, 'td',null ,'<a href="javascript:void(0)" onclick="CloseCalendar(\''+this.UniqueId+'\');new PopupCalendar(\''+this.ReturnToElementId+'\',\''+this.DateFormat+'\',new Date('+this.SelectedDate.getFullYear()+','+(this.SelectedDate.getMonth()+1)+'),\''+this.ElementToHideId+'\')"><img src="/images/pil_hoejre.gif"/></a>');
    
    var tr = this.CreateElement(thead, 'tr');

    this.CreateElement(tr, 'td', 'WeekColumn','Uge');

		for	(i=1; i<8; i++)	
		{
		  this.CreateElement(tr, 'td', 'DayColumn',new Date().getDayName(i==7?0:i));
		  
		  if(i==7)
		    break;
		}
		
		return thead;
  } 
  
  PopupCalendar.prototype.CreateElement = function(createOn, tagName, cssClass, innerHTML)
  {
    if(!tagName)
      throw new Error("You cannot create an element without a tagname");
  
    var element = document.createElement(tagName);
    
    if(createOn)
      createOn.appendChild(element);
    
    if(cssClass)
      element.className = cssClass;
    
    if(innerHTML)
      element.innerHTML = innerHTML;
      
    return element;
  }
  
  PopupCalendar.prototype.CreateCalendarTableBody = function()
  {
    var startDate = this.SelectedDate.getMonthPart();
    var numberOfDaysInMonth = startDate.getNumberOfDaysInMonth();
  
    var tbody = document.createElement('tbody');
    
    var tr = this.CreateElement(tbody,'tr');
    
    this.CreateElement(tr,'td','WeekColumn',startDate.getWeekNumber());

		for	( var i=1; i<=startDate.getDay();i++ )
		{
		  this.CreateElement(tr,'td',null,"&nbsp;");
		}
    
		for	( var day=1; day<=numberOfDaysInMonth; day++ )
		{
		  var thisDate = this.SelectedDate.getWithDate(day);

      var td = this.CreateElement(tr,'td','DayColumn'+this.GetDayStyle(thisDate));
      
      var a = this.CreateElement(td,'a',null, day);
      a.href = 'javascript:SelectDate(\''+this.UniqueId+'\',\''+this.ReturnToElementId+'\', \''+thisDate.format(this.DateFormat)+'\');ShowElement(\''+this.ElementToHideId+'\')';

			if (thisDate.getDay() == 0) 
			{ 
				var tr = this.CreateElement(tbody,'tr');
				
				if (day<numberOfDaysInMonth)
				{
				  var nextDate = this.SelectedDate.getWithDate(day+1);
				  this.CreateElement(tr,'td','WeekColumn',nextDate.getWeekNumber());
				}
			}
		}
    
    return tbody;
  }

  PopupCalendar.prototype.GetDayStyle = function(date) 
  {
		var style=""
		if (date.valueOf() == this.SelectedDate.valueOf())
		{ 
		  style+=" SelectedDate"
		}
    
		if (date.valueOf() == this.Today.valueOf())
		{ 
		  style += " Today"
		}
		else if	(date.getDay() == 0)
		{ 
		  style += " Sunday";
		}
		
		return style;
  }



