<!-- Begin
/****************************************************************************************
'* Javascript Time Picker                                                               *
'*--------------------------------------------------------------------------------------*
'* Abstract: Popup window allowing user to select time of day                           *
'* Last Update: 2-28-06																	*
*****************************************************************************************/
var timePickerDivID = "timePicker";
var selHours = 0;
var selMinutes = 0;
var selAP = 0;

function displayTimePicker(timeFieldName)
{
  var targetTimeField = document.getElementsByName(timeFieldName).item(0);

  // get incoming time field and try to parse it
  getFieldTime(targetTimeField.value);
 
  displayBelowThisObject = targetTimeField;
  
  var x = displayBelowThisObject.offsetLeft;
  var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight + 1;
  
  // deal with elements inside tables and such
  var parent = displayBelowThisObject;
  while (parent.offsetParent) {
    parent = parent.offsetParent;
    x += parent.offsetLeft;
    y += parent.offsetTop;
  }
  
  drawTimePicker(targetTimeField, x, y);
}


function drawTimePicker(targetTimeField, x, y)
{

  // the timePicker table will be drawn inside of a <div> with an ID defined by the
  // global timePickerDivID variable. If such a div doesn't yet exist on the HTML
  // document we're working with, add one.
  if (!document.getElementById(timePickerDivID)) {
    // don't use innerHTML to update the body, because it can cause global variables
    // that are currently pointing to objects on the page to have bad references
    //document.body.innerHTML += "<div id='" + timePickerDivID + "' class='dpDiv'></div>";
    var newNode = document.createElement("div");
    newNode.setAttribute("id", timePickerDivID);
    newNode.setAttribute("class", "dpDiv");
    newNode.setAttribute("style", "visibility: hidden;");
    document.body.appendChild(newNode);
  }
  
  // move the timePicker div to the proper x,y coordinate and toggle the visiblity
  var pickerDiv = document.getElementById(timePickerDivID);
  pickerDiv.style.position = "absolute";
  pickerDiv.style.left = x + "px";
  pickerDiv.style.top = y + "px";
  pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
  pickerDiv.style.zIndex = 10000;
  
  // draw the timePicker table
  refreshTimePicker(targetTimeField.name);
}

function refreshTimePicker(timeFieldName)
{

  var crlf = "\r\n";
  var TABLE = "<table class='dpTable'>" + crlf;
  var xTABLE = "</table>" + crlf;
  var TR = "<tr class='dpTR'>";
  var TR = "<tr class='dpDayTR'>";
  var xTR = "</tr>" + crlf;
  var TD = "<td class='dpTD'>";
  var TD_title = "<td class='dpTitleTD'>";
  var TD_buttons = "<td class='dpButtonTD'>";
  var xTD = "</td>" + crlf;
  var DIV_title = "<div class='dpTitleText'>";
  var DIV_selected = "<div class='dpDayHighlight'>";
  var xDIV = "</div>";
  
  // start generating the code for the calendar table
  var html = TABLE;

  html += TR + "<td colspan='2'>";
  html += "<center><select name='sHours' class='textbox' style='width:45;'>";
  html += "<option ";
  if (selHours == 12) { 
	html += "SELECTED"
  }
  html += " value='12'>12"; 
  for (var i = 1; i < 12; i++){
	html += "<option "
	if (selHours == i) { 
		html += "SELECTED"
	}
	html += " value='"
	var hString = "00" + (i);
	hString = hString.substring(hString.length - 2);
	html += hString + "'>" + i;
  }
  html += "</select>";

  html += "&nbsp;:&nbsp;<select name='sMinutes' class='textbox' style='width:45;'>";
  if (selMinutes < 8) 
	{ html += "<option SELECTED value=00>00<option value=15>15<option value=30>30<option value=45>45"; }
  else if (selMinutes > 7 && selMinutes < 22)
	{ html += "<option value=00>00<option SELECTED value=15>15<option value=30>30<option value=45>45"; }
  else if (selMinutes > 21 && selMinutes < 38)
	{ html += "<option value=00>00<option value=15>15<option SELECTED value=30>30<option value=45>45"; }
  else
	{ html += "<option value=00>00<option value=15>15<option value=30>30<option SELECTED value=45>45"; }
  html += "</select>";

  html += "&nbsp;&nbsp;<select name='sAP' class='textbox' style='width:45;'><option";
  if (selAP == 0) {
	html += " SELECTED value='AM'>AM";
	html += "<option value='PM'>PM";
  }
  else {
	html += " value='AM'>AM";
	html += "<option SELECTED value='PM'>PM";
  }
  html += "</select>";


  html += "&nbsp;&nbsp;<button class='dpButton' onClick='updateTimeField(\"" + timeFieldName + "\",\"" + "OK" + "\");'>OK</button>";
  html += "</center>" + xTD + xTR;

  // show comment line
  html += TR + "<td colspan='2'><center>Time rounded to 15 mins</center>" + xTD + xTR

  var sNow = new Date();

  // add buttons at bottom of frame
  html += TR;
  var nowString = getTimeString(sNow,1); // round time
  html += TD_title + DIV_title + nowString + xDIV + xTD;
  html += TD + "<center>";
  html += "<button class='dpButton' onClick='updateTimeField(\"" + timeFieldName + "\",\"" + nowString + "\");'>Now</button>";
  html += "&nbsp;&nbsp;&nbsp<button class='dpButton' onClick='updateTimeField(\"" + timeFieldName + "\");'>Cancel</button>";
  html += "</center>" + xTD + xTR;
  
  // and finally, close the table
  html += xTABLE;
  
  document.getElementById(timePickerDivID).innerHTML = html;
}


function updateTimeField(timeFieldName, sTime)
{
  if (sTime == "OK") {
	var hField = document.getElementsByName("sHours").item(0);
	var mField = document.getElementsByName("sMinutes").item(0);
	var apField = document.getElementsByName("sAP").item(0);
	sTime = hField.value + ":" + mField.value + " " + apField.value;
  }
  var targetTimeField = document.getElementsByName(timeFieldName).item(0);
  if (sTime) {
    if (sTime == "None") {
       targetTimeField.value = "";
       }
    else {
       targetTimeField.value = sTime;
       }
  }
  document.getElementById(timePickerDivID).style.visibility = "hidden";
  targetTimeField.focus();
  
}

/** Convert a JavaScript Date object to a string, 
based on the dateFormat and dateSeparator
variables at the beginning of this script library. */

function getTimeString(timeVal,roundTime)
{
  var nHour = timeVal.getHours();
  var ap = "AM";
  if (nHour > 11) {
	nHour = nHour-12;
	ap = "PM";
  } 
  if (nHour == 0) {nHour=12;}

  var nMinute = timeVal.getMinutes()

  // fix minutes if we are to adjust time
  if (roundTime == 1) {
	if (nMinute < 8) 
		{ nMinute = 0; }
	else if (nMinute > 7 && nMinute < 22)
		{ nMinute = 15; }
	else if (nMinute > 21 && nMinute < 38)
		{ nMinute = 30; }
	else
		{ nMinute = 45; }
  }

  var hString = "00" + nHour;
  var mString = "00" + nMinute;
  hString = hString.substring(hString.length - 2);
  mString = mString.substring(mString.length - 2);
  
  return hString + ":" + mString + " " + ap;
}


// Get field from calling form and try to parse it into HH:MM AM/PM
function getFieldTime(timeString)
{

  var delim;
  
  selHours = 0;
  selMinutes = 0;
  selAP = 0; // default to AM

  // Trim beginning and trailing spaces from string and force to uppercase
  timeString = LTrim(RTrim(timeString));
  timeString = timeString.toUpperCase();

  // See if AM OR PM is Present
  if (timeString.indexOf("PM") > 0) { selAP = 1; }

  // Get Hours
  delim = timeString.indexOf(":");
  if (delim == -1) { delim = timeString.indexOf("."); }

  if (delim > 0) {
	selHours = timeString.substring(0,delim);
	selHours = selHours * 1; // convert to num
	selMinutes = timeString.substring(delim+1,delim+3);
	selMinutes = selMinutes * 1; // convert to num
	if (selHours < 0 || selHours > 23) { selHours = 0; }
	if (selMinutes < 0 || selMinutes > 59) { selMinutes = 0; }
	if (selHours > 12) { // convert from 24 hour to 12 hour time
		selHours = selHours - 12;
		selAP = 1;
		if (selHours == 0) { selHours = 12; }
		}
	}

  return;
}


function LTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
     var j=0, i = s.length;
     while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
           j++;
     s = s.substring(j, i);
  }
  return s;
}


function RTrim(str)
{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
     var i = s.length - 1;
     while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
           i--;
     s = s.substring(0, i+1);
  }
  return s;
}

// End -->
