var elementTimer=[];
function getElement(id) {
return document.getElementById ? document.getElementById(id) :
document.all ? document.all(id) : null;
}

function findElement(e,i,execute) {	
	if (i==null && e!=null) {
		var ltimer;
		i=elementTimer.length;
		var tobj=new Object();
		tobj.timer=ltimer;
		tobj.elementname=e;
		tobj.finished=false;
		tobj.execute=execute;
		tobj.testTimes=0;
		elementTimer[i]=tobj;
	} 

	for (var j=0;j<elementTimer.length;j++) {
		if ((!elementTimer[j].finished) && (elementTimer[j].testTimes<5)) {
			elementTimer[j].testTimes+=1;
			clearTimeout(elementTimer[j].ltimer);
			var obj=getElement(elementTimer[j].elementname);
			if (obj==null) {
					elementTimer[j].ltimer=setTimeout("findElement()",1000);
			} else {	
				elementTimer[j].finished=true;
		
				elementTimer[j].execute.call(this,obj);
			}
		}
	}
}
var _b,_s,_on,afield,tof,cbox;

function startclock(a) {
	_on=true;
	if (afield==null) {
		afield=a;
	}
	if (tof!=null) {
		clearTimeout(tof)
	}
	var clockbox=document.forms[0][afield];
	if (clockbox!=null) {
		tof=null;
		cbox=clockbox;
		clocksetup(clockbox);
		clockrepeat(clockbox,startTime);
	} else {
		tof=setTimeout("startclock()",1000);
	}
}

function clockrepeat(obj,st) {
	if (obj!=null) {
		_b=obj;
		_s=st;
	}
	if (tof!=null) {
		clearTimeout(tof)
	}
	if (_on) {
		clocktick(_b,_s);
	} else {
		_s=new Date(_s.getFullYear(),_s.getMonth(),_s.getDate(),_s.getHours(),_s.getMinutes(),_s.getSeconds()+1);
		
	}
	tof=setTimeout("clockrepeat()",1000);
}

function clocking(o) {
if (cbox!=null) {
if (!_on) {
	clocksetup(cbox)
	_s=startTime;
}
_on=!_on;
o.value=(_on) ? "pause" : "start";
}}
function clocksetup(box) {

if (box.value!="") {

var sp=box.value.split(":");
sp[0]=sp[0].substring(0,1)==0? sp[0].substring(1,2) : sp[0];
sp[1]=sp[1].substring(0,1)==0? sp[1].substring(1,2) : sp[1];
sp[2]=sp[2].substring(0,1)==0? sp[2].substring(1,2) : sp[2];
var hrs=parseInt(sp[0]);
var min=parseInt(sp[1]);
var sec=parseInt(sp[2]);
today=new Date();
today.setHours(today.getHours()-hrs)
today.setMinutes(today.getMinutes()-min)
today.setSeconds(today.getSeconds()-sec)
time=box.value;
} else {
today = (new Date());
time="00:00:00";
}
//the_day = (new Date(day)).getTime();
startTime = today;
box.value=time;
return startTime;

}

function clocktick(box,startTime) {
var time,days,hours,mins,secs,out;
out="";
time=new Date()-startTime;
days = (time - (time % 86400000)) / 86400000;
time = time - (days * 86400000);
hours = (time - (time % 3600000)) / 3600000;
time = time - (hours * 3600000);
mins = (time - (time % 60000)) / 60000;
time = time - (mins * 60000);
secs = (time - (time % 1000)) / 1000;
if(hours < 10) out = out+"0";
out = out+hours+":";
if(mins < 10) out = out+"0";
out = out+mins+":";
if(secs < 10) out = out+"0";
out = out+secs;
box.value=out;
}

//* clear a box*
function checkAndClear(obj) {

	if (obj.value=='search') {
		obj.value="";
	}
}