// JavaScript Document
includeOnce("/javascript/dom.js");
/*
These files must be included for this to work
if(includeOnce != null){
	includeOnce("/javascript/clock.css");
	includeOnce("/javascript/dom.js");
	includeOnce("/javascript/events.js");
	includeOnce("/javascript/timeparse.js");
}*/

var currentTimeField = null;
var _clock = new Object();

function isTime(s){
	try{
		parseTimeString(s);
	}
	catch(e){
		return false;
	}
	return true;
}

istime = isTime;

function toTime(s){
	try{
		s  = parseTimeString(s);
	}
	catch(e){
		return s;
	}
	returnString = '';
	returnString += s.getHours();
	returnString += ':';
	if((s.getMinutes() < 10)){
		returnString += '0';
	}
	returnString += s.getMinutes();
	return returnString;
}

totime = toTime;

function moveClock(element){
	if($('_clock') == null){
		loadClock();
	}
	currentTimeField = element;
	setClockTime();
	var coors = findPos(currentTimeField);
	$('_clock').style.display = 'inline';
	$('_clock').style.width = currentTimeField.offsetWidth + 'px';
	$('_clock').style.top = (coors[1] + currentTimeField.offsetHeight) + 'px';
	$('_clock').style.left = coors[0] + 'px';
}

function moveClockMask(event){
	moveClock(getElementFromEvent(event));
}

function hideClock(event){
	$('_clock').style.display = 'none';
	currentTimeField = null;
}

function appendClockEvent(element){
	addEvent(element, 'click', moveClockMask);
}

function loadClock(){
	//lookAtAllInputs(document, appendClockEvent);
	theClock = document.createElement('div');
	theClock.className = 'clock';
	_clock.mainClock = theClock;
	hour = document.createElement('select');
	for(i = 0; i < 12; i++){
		the_hour = new Option(i+1, i+1, false, false);
		hour.options[i] = the_hour;
	}
	theClock.appendChild(hour);
	_clock.hour = hour;
	
	theClock.appendChild(document.createTextNode(':'));
	
	minute = document.createElement('select');
	for(i = 0; i < 60; i++){
		the_minute = new Option((((i<10)?'0':'')+i), i, false, false);
		minute.options[i] = the_minute;
	}
	theClock.appendChild(minute);
	_clock.minute = minute;
	
	theClock.appendChild(document.createTextNode(' '));
	
	ampm = document.createElement('select');
	ampm.options[0] = new Option('AM','AM', false, false);
	ampm.options[1] = new Option('PM','PM', false, false);
	theClock.appendChild(ampm);
	_clock.ampm = ampm;
	
	theClock.setAttribute('id', '_clock');
	theClock.style.display = 'none';
	theClock.style.position = 'absolute';
	document.body.appendChild(theClock);
	
	disappearingClock = function(event){
		//Alert('disappearingContainer<br>');
		theClock = document.getElementById('_clock');
		
		if(!theClock){
			return;
		}
		
		if(!isAncestorOf(theClock,getElementFromEvent(event))){
			hideClock();
		}
	};
	
	addEvent(document, 'mousedown', disappearingClock);
	addEvent(ampm, 'change', setFieldTime);
	addEvent(minute, 'change', setFieldTime);
	addEvent(hour, 'change', setFieldTime);
}

function setClockTime(){
	if(currentTimeField == null || !_clock.hour){
		return;
	}
	
	if(!isTime(currentTimeField.value)){
		_clock.hour.options[11].selected = true;
		_clock.minute.options[0].selected = true;
		_clock.ampm.options[0].selected = true;
		return;
	}
	
	time = parseTimeString(currentTimeField.value);
	
	index = (time.getHours() % 12) - 1;
	
	if(index == -1){
		index = 11;
	}
	
	_clock.hour.options[index].selected = true;
	_clock.minute.options[time.getMinutes()].selected = true;
	_clock.ampm.options[(time.getHours() > 11)?1:0].selected = true;;
}

function setFieldTime(){
	if(currentTimeField == null || !_clock.hour){
		return;
	}
	
	string = (_clock.hour.selectedIndex+1) + ':' + _clock.minute.selectedIndex + ' ' + _clock.ampm.options[_clock.ampm.selectedIndex].value;
	
	currentTimeField.value = toTime(string);
}
