document.write("<script type='text/javascript' src='/layouts/JavaScriptLanguageTexts.aspx?language=" + currentlanguage + "'></script>");

var calendars = new Array();

function addCalendar(DispArea, Destination, BtnClicked)
{
	for(i=0; i<calendars.length;i++)
	{
		if(calendars[i].DispArea == DispArea)
		{
			calendars[i] = new Calendar;
			calendars[i].init(i, DispArea, Destination, BtnClicked);
			return;
		}
	}
	calendars[calendars.length] = new Calendar;
	calendars[calendars.length-1].init((calendars.length-1), DispArea, Destination, BtnClicked);
	return;
}

// define a Calendar class
function Calendar () 
{
    // add some properties to our Calendar
    this.DispArea		= '' ;
    this.Destination	= '' ;
    this.BtnClicked		= '';
    this.ObjectPos		= 0;
    this.currentDate	= new Date();
    this.originalDate	= new Date();
    this.onlyCal		= true;

    // initialize the member function references
    // for the class prototype
    if (typeof(_calendar_prototype_called) == 'undefined')
    {
       _calendar_prototype_called = true;
       Calendar.prototype.showCal = showCal ;
       Calendar.prototype.init = init ;
       Calendar.prototype.Format = Format ;
       Calendar.prototype.Print = Print ;
       Calendar.prototype.Change = Change ;
       Calendar.prototype.Select = Select ;
       Calendar.prototype.Link = Link ;
       Calendar.prototype.Mark = Mark ;
    }

    // define the clock's methods
	function init(ObjectPos, DispArea, Destination, BtnClicked)
	{
		this.ObjectPos		= ObjectPos;
		this.DispArea		= DispArea;
		this.Destination	= Destination;
		this.BtnClicked		= BtnClicked;

		//We first check if a date already has been selected
		if(this.Format(document.getElementById(this.Destination).value))				//A date has been selected before
		{
			var arr_date = document.getElementById(this.Destination).value.split('.');	//Splits the date with "-"
			//Creates new Date with (year, month, date)
			this.currentDate = new Date(arr_date[2], arr_date[1]-1, arr_date[0] );
		}
		else //This is the first time a date is chosen
			this.currentDate = new Date();
		
		this.originalDate = new Date(this.currentDate);
		//Now the date has been set to a correct date either today or previous selected date
		document.getElementById(this.DispArea).innerHTML = this.showCal();
		document.getElementById(this.DispArea).style.visibility = 'visible';
	}
	
	//Function to ensure that a number is printed with a specific number of digits
	function Print(number, digits)
	{
		number = number + "";											//Conversion to force the number to be recognized as a string
		for(i=0;i < (digits-number.length); i++) number = "0"+number;	//Adds leading zeros to fulfill the required number of digits
		return number;
	}
	
	function Format( time ) 
	{ return  !( /^(\d{2})\.(\d{2})\.(\d{4})$/.exec(time) == null ); }
	

    
    function showCal()
    {
    	//The beginning of the table is setup here
		cont = "<table class=\"datetable\" rules=\"cols\">";
		
		
			
		cont +=		"<tr><td colspan=\""+Days.length+"\"><table class=dateHeaderTable width=\"100%\"><tr>" + 
					"<td align=left><table class=dateHeader align=left><tr>" +					
						"<td><a title=\"G&aring; en m&aring;ned tilbage\" href=\"javascript:calendars["+this.ObjectPos+"].Change(-1)\">&#171;</a></td>"+
						"<td align=center width=\"70\">"+Months[this.currentDate.getMonth()]+" </td>" + 
						"<td><a title=\"G&aring; en m&aring;ned frem\" href=\"javascript:calendars["+this.ObjectPos+"].Change(1)\">&#187;</a></td>"+
					
					"</tr></table></td>" +
					"<td align=right><table class=dateHeader align=right><tr>" +
					
					"<td><a title=\"G&aring; et &aring;r tilbage\" href=\"javascript:calendars["+this.ObjectPos+"].Change(-12)\">&#171;</a></td>"+
					"<td align=center width=\"35\">"+this.currentDate.getFullYear()+"</td>" + 
					"<td><a title=\"G&aring; et &aring;r frem\" href=\"javascript:calendars["+this.ObjectPos+"].Change(12)\">&#187;</a></td>"+
					
					"</tr></table></td>" +
					
					"</tr></table></td></tr><tr>";
		//All the days are added from the array
		for(i=0;i<Days.length;i++)
			cont += "<td class=datetabledays>"+Days[i]+"</td>";
		cont += "</tr>";
		
		//Then all the days must be written
		//Find first day of the month it will be set on the scale 0 - 6 is configured because Date has sunday as 0
		//and in our case it should be 6
		firstDay = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), 1);
		firstDay = (firstDay.getDay()+Days.length-1)%Days.length;
		
		//dP contains the amount of days in the previous month
		dP = new Date(this.currentDate.getFullYear(), this.currentDate.getMonth()-1, 1);
		dP = isLeapYear(dP.getFullYear()) ? daysMonthLeap[dP.getMonth()] : daysMonth[dP.getMonth()];

		//Countdays counts the days written
		countDays = 1;
		DaysFromPrevious = firstDay;
		//We find the number of days in the current month
		noDays = isLeapYear(this.currentDate.getFullYear()) ? daysMonthLeap[this.currentDate.getMonth()] : daysMonth[this.currentDate.getMonth()];
		//We create an integer to count the days from the next month
		extras = 1;

		//We  make a special style for all the dates that does not belong to this month
		//with "visibility:hidden;" in the tag, they would not be shown
		style = "style=\"color:#666666;\"";
		
		row = 0;
		
		//We then make a row as long as we have not yet written all the days of the month
		while(countDays <= noDays)
		{
			cont += "<tr>";
			//The we add a cell for each day available
			for(i=0;i<Days.length;i++)
			{
				cont += "<td class=dateCell>";
				if(row ==0)	//First Row
					cont += (DaysFromPrevious > 0) ? "<a href=\"javascript:calendars["+this.ObjectPos+"].Mark("+(dP-DaysFromPrevious +1)+",-1)\" "+style+">"+(dP-DaysFromPrevious-- +1)+"</a>" : this.Link(countDays++);
				else
				{
					if((countDays+6) > noDays) //Last row
					{
						cont += (countDays > noDays) ? "<a href=\"javascript:calendars["+this.ObjectPos+"].Mark("+extras+",1)\" "+style+">"+(extras++)+"</a>" : this.Link(countDays++);
						continue;
					}
						cont += this.Link(countDays++);
				}
				cont += "</td>";
			}
			cont += "</tr>";
			row++;
		}
		if(this.onlyCal && false) //set to false since it should be selected when marking a date
			cont += "<tr><td class=choosebutton colspan=\""+Days.length+"\"><a href=\"javascript:calendars["+this.ObjectPos+"].Select();\">V&aelig;lg</a></td></tr>";

		if(this.onlyCal)
			cont += "<tr><td class=close colspan=\""+Days.length+"\"><a href=\"javascript:calendars["+this.ObjectPos+"].Select();\">luk vindue</a></td></tr>";
			
		cont += "</table>";
		return cont;
    }
    
    function Change(change)
	{
		this.currentDate.setMonth(this.currentDate.getMonth() + change);
		document.getElementById(this.DispArea).innerHTML = this.showCal();
	}
	
	//When a date is selected the currentdate is change so a new date is marked
	function Mark(dato, monthFromCurrent)
	{
	
		this.currentDate.setMonth(this.currentDate.getMonth()+monthFromCurrent);
		this.currentDate.setDate(dato);
		this.originalDate = new Date(this.currentDate);
	//	document.getElementById(this.DispArea).innerHTML = this.showCal();		
	
		this.Select();
	}
	
	function Select()
	{
		result = this.Print(this.originalDate.getDate(),2)+"."+this.Print((this.originalDate.getMonth()+1),2)+"."+this.Print(this.originalDate.getFullYear(),4);
		document.getElementById(this.Destination).value = result;
		this.BtnClicked.value = currentlanguage == "en" ? "Edit" : "Ret";

/*
		for(i=0;i<calendars.length;i++)
			if(calendars[i].DispArea == this.DispArea)	//Then delete item
			{
				for(s = i; s<calendars.length-1;s++)
					calendars[i] = calendars[i+1];
				calendars.length = calendars.length - 1;
				break;
			}
*/
//Don't delete just render invisible
		document.getElementById(this.DispArea).style.visibility = 'hidden';
	}
	
	//Function which sets a link given by a specific date. This is done to ensure that the formatting is the same
	function Link(dato)
	{
		b = this.originalDate; c = this.currentDate;
		extra = (dato == b.getDate() && c.getMonth() == b.getMonth() && c.getFullYear() == b.getFullYear()) ? "background-color:#B31B34;color:#ffffff;font-weight:bold;" : "";
		return "<a href=\"javascript:calendars["+this.ObjectPos+"].Mark("+dato+",0)\" style=\""+extra+"\">" + dato +"</a>";
	}
}

//Function to test whether a year is leap year or not
function isLeapYear(year) 
{
	if ((year/4)   != Math.floor(year/4))   return false;
	if ((year/100) != Math.floor(year/100)) return true;
	if ((year/400) != Math.floor(year/400)) return false;
	return true;
}