//for Timer functions:
// st = start time hour; et = end time hour in military (these must match the hours used in the div tag)
// w = approx width of table
// h = height of graphic vertical ruler (see img height in table within div)- this must be precise so that popup time matches mouse position
// pw = approx width of popup; ph = approx height of popup
// dh = used to fine tune the mapping between mouse position and popup time
// truevert and truehoriz are for setting the top or left parm of the table to a fixed value - if you need to position the table precisely. Set to -1 to not use these parms. Can be set individually.
// x and y are used to position table and popup
// px positions the popup horizontally
// field is the name of the time field to be populated

//Set the variables in the JS Header of your form, outside of any functions.
//EXAMPLE: var st=6, et=18, w=120, h=194, pw=40, ph=12, dh=-5, truevert=-1, truehoriz=-1, x, y, px, field;
//====================================================================

function pickTime(f) {
	//Called when time icon next to time field is clicked.
	y = event.clientY - (h / 2);
	x = event.clientX - (w / 2);

	//If table would be too close to edge, move it away a bit.
	if (x > document.body.clientWidth - 30 - w) x = document.body.clientWidth - 50 - w;
	if (x < 30) x = 30;
	if (y > document.body.clientHeight - 30 - h) y = document.body.clientHeight - 60 - h;
	if (y < 30) y = 30;

	//ck and set precise top and/or left positions
	if (truehoriz != -1) x = truehoriz;
	if (truevert != -1) y = truevert;
	
	//adjust coordinates for scrolling documents
	x += document.body.scrollLeft;
	y += document.body.scrollTop;
	
	//Set time picker dialog position and make it visible
	timer.style.left = x;
	timer.style.top = y;
	timer.style.visibility = 'visible';
	
	//Used by getTime
	px = 0;
	field = f;
}
//====================================================================

function getTime(ret) {
	//This function is used by the time picker div
	
	//Capture mouse vertical position
	y = event.offsetY;
	
	//Compute time relative to mouse position and hour range in div
	t = st + ((et - st) * (y + dh) / h);
	
	//Compute minute, round to nearest 15 minutes
	dt = (t - parseInt(t)) * 60;
	dt = dt - (dt % 15);
	if(dt <= 0) dt = '00';
	
	//Compute hour
	t = parseInt(t);
	
	if(parseInt(t) < 12) {
		ap = " AM"; // IF t is less then 12 it is an AM time.
	} else {
		ap = " PM"}
		
	//sets time to 12 hour clock not 24 hour clock. Max time is 11:45 PM.
	if(t > 23) {
		t = '11';
		dt = '45'; }
	
	if(t > 12) t = t - 12; 

	//If time is less than 0100, represent as 12:xx AM. 
	if(t < 1) t = 12; 	
	
	//Pad with leading zero if nec.
	if(t < 10) t = '0' + t;
	
	if (ret) {
		//If getTime is called with an arg of True, set the field to the computed time and close (hide) div		
		document.forms[0].elements[field].value = t + ':' + dt + ap;
		closePicker();
		return;
	}
	
	//If function is called without the True arg, just show computed time in popup
	tpop.innerHTML = t + ':' + dt + ret;
	if (!px) px = parseInt(timer.style.left) + (w / 2) + pw - 15;
	tpop.style.left = px;
	tpop.style.top = (event.clientY + document.body.scrollTop) - (ph / 2);
	tpop.style.visibility = 'visible';
}
//====================================================================

function closePicker(m) {
	//This function is used by the time picker div & by the getTime function
	tpop.style.visibility = 'hidden';
	if (!m) timer.style.visibility = 'hidden';
}
