var dateTimes = new Array();

function addDateTime(DispArea, Destination, BtnClicked)
{
	for(i=0; i<dateTimes.length;i++)
	{
		if(dateTimes[i].DispArea == DispArea)
			return;
	}
	dateTimes[dateTimes.length] = new DateTime;
	dateTimes[dateTimes.length-1].init((dateTimes.length-1), DispArea, Destination, BtnClicked);
	return;
}
// define a DateTime class
function DateTime () 
{
    // add some properties to our DateTime
    this.DispArea		= '' ;
    this.Destination	= '' ;
    this.BtnClicked		= '';
    this.ObjectPos		= 0;
    this.Calen;			//Reference to a calendar object
    this.Clck;			//Reference to a clock object

    // initialize the member function references
    // for the class prototype
    if (typeof(_datetime_prototype_called) == 'undefined')
    {
       _datetime_prototype_called = true;
       DateTime.prototype.init = init ;
       DateTime.prototype.Print = Print ;
       DateTime.prototype.Select = Select ;
    }

    // define the clock's methods
	function init(ObjectPos, DispArea, Destination, BtnClicked)
	{
		this.ObjectPos		= ObjectPos;
		this.DispArea		= DispArea;
		this.Destination	= Destination;
		this.BtnClicked		= BtnClicked;
		
		calObjDisp = DispArea+"calObj";
		clkObjDisp = DispArea+"clkObj";
		
		document.getElementById(DispArea).innerHTML = ""+
								"<table><tr><td align=\"center\" id=\""+calObjDisp+"\"></td></tr>"+
								"<tr><td align=\"center\" id=\""+clkObjDisp+"\"></td></tr>" +
								"<tr><td align=\"center\"><a href=\"javascript:dateTimes["+this.ObjectPos+"].Select()\">V&aelig;lg</a></td></tr></table>";
		
		elements = document.getElementById(this.Destination).value.split(' ');
		if(elements.length == 0)
			elements[0] = "Ikke sat";
		if(elements.length < 2)
			elements[1] = "Ikke sat";	

///It won't use a previous selected date at this point			
		this.Calen = calendars.length;	
		calendars[this.Calen] = new Calendar;
		calendars[this.Calen].onlyCal = false;
		calendars[this.Calen].init((this.Calen), calObjDisp, this.Destination, this.BtnClicked);
		
		this.Clck = clocks.length;
		clocks[this.Clck] = new Clock;
		clocks[this.Clck].onlyClock = false;
		clocks[this.Clck].init((this.Clck), clkObjDisp, this.Destination, this.BtnClicked);
		
		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 Select()
	{
		writer = "";
		//First we add the clock
		for(i=0;i<clocks[this.Clck].timeArray.length;i++)
			writer += clocks[this.Clck].timeArray[i];
		//Then we add the date and year
		writer +=	" "+ this.Print(calendars[this.Calen].originalDate.getDate(),2) + "-" + 
					this.Print((calendars[this.Calen].originalDate.getMonth()+1),2) + "-" + 
					this.Print(calendars[this.Calen].originalDate.getFullYear(),4);
		document.getElementById(this.Destination).value = writer;
		this.BtnClicked.value = "Ret";
		for(i=0;i<dateTimes.length;i++)
			if(dateTimes[i].DispArea == this.DispArea)	//Then delete item
			{
				for(s = i; s<dateTimes.length-1;s++)
					dateTimes[i] = dateTimes[i+1];
				dateTimes.length = dateTimes.length - 1;
				break;
			}
		document.getElementById(this.DispArea).innerHTML = "";
		document.getElementById(this.DispArea).style.visibility = 'hidden';
	}
}
