var clocks = new Array();

function addClock(DispArea, Destination, BtnClicked)
{
	for(i=0; i<clocks.length;i++)
	{
		if(clocks[i].DispArea == DispArea)
			return;
	}
	clocks[clocks.length] = new Clock;
	clocks[clocks.length-1].init((clocks.length-1), DispArea, Destination, BtnClicked);
	return;
}

// define a Clock class
function Clock () 
{
    // add some properties to our clock
    this.DispArea		= '' ;
    this.Destination	= '' ;
    this.BtnClicked		= '';
    this.ObjectPos		= 0;
    this.timeArray		= new Array();
    this.onlyClock		= true;

    // initialize the member function references
    // for the class prototype
    if (typeof(_clock_prototype_called) == 'undefined')
    {
       _clock_prototype_called = true;
       Clock.prototype.showClock = showClock ;
       Clock.prototype.init = init ;
       Clock.prototype.Format = Format ;
       Clock.prototype.Print = Print ;
       Clock.prototype.Change = Change ;
       Clock.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;
		dest = document.getElementById(this.Destination);
		currentTime = new Date();
		str = this.Print(currentTime.getHours(),2) +":"+this.Print(currentTime.getMinutes(),2);
		this.timeArray = this.Format(dest.value) ? dest.value.split('') : str.split('');
		
		document.getElementById(this.DispArea).innerHTML = this.showClock();	
		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})$/.exec(time) == null ); }
    
    function showClock()
    {
		digits = "style=\"font-size:1.2em; background-color:white;\"";				//Style formatting of digits
		pointers = "style=\"text-decoration:none; color:black;\"";
		content = "<table width=\"150px\" class=\"datetable\"><tr>" + 
				"<td align=center "+digits+">"+this.timeArray[0]+"</td>"+
				"<td align=center "+digits+">"+this.timeArray[1]+"</td>"+
				"<td align=center>	<a href=\"javascript:clocks["+this.ObjectPos+"].Change('hour', 1);\" "+pointers+">&uarr;</a><br/>" +
				"		<a href=\"javascript:clocks["+this.ObjectPos+"].Change('hour',-1);\" "+pointers+">&darr;</a></td>" +
				"<td align=center "+digits+">"+this.timeArray[3]+"</td>"+
				"<td align=center "+digits+">"+this.timeArray[4]+"</td>"+
				"<td align=center>	<a href=\"javascript:clocks["+this.ObjectPos+"].Change('minute', 1);\" "+pointers+">&uarr;</a><br/>" +
				"		<a href=\"javascript:clocks["+this.ObjectPos+"].Change('minute',-1);\" "+pointers+">&darr;</a></td></tr>";
		if(this.onlyClock)
				content += "<tr><td class=choosebutton colspan=\"6\"><a href=\"javascript:clocks["+this.ObjectPos+"].Select();\">V&aelig;lg</a></td></tr>";
				
		content += "</table>";
		return content;
    }
    
    function Change(element, change)
	{
		switch(element)
		{
			case 'hour': 
						hours = (this.timeArray[0] == "0") ? parseInt(this.timeArray[1]) : parseInt(this.timeArray[0]+this.timeArray[1]);
						hours = hours + change;
						hours = (hours < 0) ? 24+hours : hours;
						hours = this.Print(hours%24,2).split('');
						this.timeArray[0] = hours[0];
						this.timeArray[1] = hours[1];
						break;
			default:	
						min = (this.timeArray[3] == "0") ? parseInt(this.timeArray[4]) : parseInt(this.timeArray[3]+this.timeArray[4]);
						min = min + change;
						min = (min < 0) ? 60+min : min;
						min = this.Print(min%60,2).split('');
						this.timeArray[3] = min[0];
						this.timeArray[4] = min[1];
						break;
		}
		document.getElementById(this.DispArea).innerHTML = this.showClock();
	}
	
	function Select()
	{
		writer = "";
		for(i=0;i<this.timeArray.length;i++)
			writer += this.timeArray[i];
		document.getElementById(this.Destination).value = writer;
		this.BtnClicked.value = "Ret";
		for(i=0;i<clocks.length;i++)
			if(clocks[i].DispArea == this.DispArea)	//Then delete item
			{
				for(s = i; s<clocks.length-1;s++)
					clocks[i] = clocks[i+1];
				clocks.length = clocks.length - 1;
				break;
			}
		document.getElementById(this.DispArea).style.visibility = 'hidden';
	}
}
