var SessionTimeout;

/*****************************************************************
* Name:		Logout
* Purpose:	Log user off  the system
* Created:	2/13/2004
*****************************************************************/
function Logout() {
	window.location.href="logout.aspx";
}

/*****************************************************************
* Name:		Cancel
* Purpose:	Redirects user to menu page after prompt.
* Created:	2/13/2004
*****************************************************************/
function Cancel() {
	var strMsg = "You will lose any unsaved data. Click OK if you wish to continue.";
	if (confirm(strMsg)) window.location.href="menu.aspx";
}

/*****************************************************************
* Name:		Menu
* Purpose:	Redirects user to menu page without prompt.
* Created:	2/13/2004
*****************************************************************/
function Menu() {
	window.location.href="menu.aspx";
}

/*****************************************************************
* Name:		AppTimeout
* Purpose:	Runs the code in the 1st parameter after  
*			MILLISECONDS_TO_TIMEOUT time has passed.
* Created:	2/13/2004
*****************************************************************/
function AppTimeout() {
	var MINS_TO_TIMEOUT = 30;
	var MILLISECONDS_TO_TIMEOUT = MINS_TO_TIMEOUT * 60 * 1000;
	SessionTimeout = setTimeout("Logout()",MILLISECONDS_TO_TIMEOUT);
}

/*****************************************************************
* Name:		ClearTimeout
* Purpose:	Reset Timeout for user.
* Created:	4/20/2004
*****************************************************************/
function ClearTimeout() {
	AppTimeout();							//used to define SessionTimeout
	window.clearTimeout(SessionTimeout);	//Clear SessionTimeout
	AppTimeout();							//Start a new SessionTimeout
}

/*****************************************************************
* Name:		SetFocus
* Purpose:	Set form focus to the first appropriate field.
* Created:	4/20/2004
*****************************************************************/
function SetFocus() {
	var bolDone = false;
	
	//Loop through form looking for a item to set focus on
	for (i=0;i<document.forms(0).elements.length;i++) {
		//Continue as long as we have not found an item
		if (!bolDone) {
			/************************************
			* Select any element above the default case
			* and set the focus to that element
			* then we are done.
			***************************************/
			switch (document.forms(0).elements(i).type) { 
				case "select-one":		//Combobox
				case "submit" :			//input type=submit
				case "button" :			//button
				case "checkbox" :		//checkbox
				case "file":			//input type=file
				case "text":			//input type=text
					//Only try to select focus when the control is enabled
					if(!document.forms(0).elements(i).disabled) {
						document.forms(0).elements(i).focus();
						bolDone = true;
					}
					break;
				default : 
					//Do Nothing 
			} 
		}
	}
}

/****************************************************************
* Name:		trim
* Purpose:	basic trim function added to the String object
****************************************************************/
String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, ""); 
}

/****************************************************************
* Name:		padL
* Purpose:	Pads to left of a string.  Added to the String object
****************************************************************/
String.prototype.padL = function (nLength, sChar) {
  var sreturn = this;
  while (sreturn.length < nLength) {
    sreturn = String(sChar) + sreturn; }
  return sreturn; }

/****************************************************************
* Name:		date_onkeydown
* Purpose:	This is basically a mask for entering dates 
*			into a text field.  Additionally there is a hot key
*			'Shift-D' that automaticlly enters the current date.
****************************************************************/
function date_onkeydown() {
	if (window.event.srcElement.readOnly) return;
	
	var key_code = window.event.keyCode;
	var oElement = window.event.srcElement;
	
	if (window.event.shiftKey && String.fromCharCode(key_code) == "D") {
		var d = new Date();
		oElement.value = String(d.getMonth() + 1).padL(2,"0")+"/"+String(d.getDate()).padL(2,"0")+"/"+d.getFullYear();
		window.event.returnValue = 0;
	}
	if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
		if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
			if (key_code > 95) key_code -= (95-47);
			oElement.value = oElement.value.replace(/[mdy]/, String.fromCharCode(key_code));
		}
		
		if (key_code == 8) {
			if (!oElement.value.match(/^[mdy0-9]{2}\/[mdy0-9]{2}\/[mdy0-9]{4}$/))
				oElement.value = "mm/dd/yyyy";
				oElement.value = oElement.value.replace(/([mdy\/]*)[0-9]([mdy\/]*)$/, function ($0, $1, $2) {
				var idx = oElement.value.search(/([mdy\/]*)[0-9]([mdy\/]*)$/);
			if (idx >= 5) {
				return $1 + "y" + $2; }
			else if (idx >= 2) {
				return $1 + "d" + $2; }
			else { return $1 + "m" + $2; } } );
		window.event.returnValue = 0; }
	}
	
	if (key_code != 9) {
		event.returnValue = false; }		  
}

/****************************************************************
* Name:		time_onkeydown
* Purpose:	This is basically a mask for entering times 
*			into a text field.  Additionally there is a hot key
*			'Shift-T' that automaticlly enters the current time.
****************************************************************/
function time_onkeydown2() {
	if (window.event.srcElement.readOnly) return;
	
	var key_code = window.event.keyCode;
	var oElement = window.event.srcElement;
	
	if (window.event.shiftKey && String.fromCharCode(key_code) == "T") {
		var d = new Date();
		if(d.getHours() > 12) {
			oElement.value = String(d.getHours()-12).padL(2,"0")+":"+String(d.getMinutes()).padL(2,"0");
		} else {
			oElement.value = String(d.getHours()).padL(2,"0")+":"+String(d.getMinutes());
		}
		window.event.returnValue = 0;
	}
	
	if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
		if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
			if (key_code > 95) key_code -= (95-47);
			oElement.value = oElement.value.replace(/[hm]/, String.fromCharCode(key_code));
		}
		
		if (key_code == 8) {
			if (!oElement.value.match(/^[hm0-9]{2}\:[hm0-9]{2}$/))
				oElement.value = "hh:mm";
				oElement.value = oElement.value.replace(/([hm\:]*)[0-9]([hm\:]*)$/, function ($0,$1,$2) {
				var idx = oElement.value.search(/([hm\:]*)[0-9]([hm\:]*)$/);
			if (idx >= 2) {
				return $1 + "m" + $2; }
			else { return $1 + "h" + $2; } } );
		window.event.returnValue = 0; }
	}
	
	if (key_code != 9) {
		event.returnValue = false; }		  
}

/****************************************************************
* Name:		time_onkeydown
* Purpose:	This is basically a mask for entering times 
*			into a text field.  Additionally there is a hot key
*			'Shift-T' that automaticlly enters the current time.
****************************************************************/
function time_onkeydown() {
	if (window.event.srcElement.readOnly) return;
	
	var key_code = window.event.keyCode;
	var oElement = window.event.srcElement;
	
	if (window.event.shiftKey && String.fromCharCode(key_code) == "T") {
		var d = new Date();
		if(d.getHours() > 12) {
			oElement.value = String(d.getHours()-12).padL(2,"0")+":"+String(d.getMinutes()).padL(2,"0")+ " PM";
		} else {
			oElement.value = String(d.getHours()).padL(2,"0")+":"+String(d.getMinutes()).padL(2,"0")+" AM";
		}
		window.event.returnValue = 0;
	}
	
	if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
		if ((key_code > 47 && key_code < 58) || (key_code > 95 && key_code < 106)) {
			if (key_code > 95) key_code -= (95-47);
			oElement.value = oElement.value.replace(/[hm]/, String.fromCharCode(key_code));
		}
		
		if(key_code == 80 || key_code == 65 || key_code == 77) {
			oElement.value = oElement.value.replace(/[a]/, String.fromCharCode(key_code));
		}
		
		if (key_code == 8) {
			if (!oElement.value.match(/^[hm0-9]{2}\:[hm0-9]{2} [aAMP]{2}$/))
				oElement.value = "hh:mm aa";
				oElement.value = oElement.value.replace(/([hma \:]*)[0-9APM]([hma \:]*)$/, function ($0,$1,$2) {
				var idx = oElement.value.search(/([hma \:]*)[0-9AMP]([hma \:]*)$/);
			if (idx >= 5) {
				return $1 + "a" + $2; }
			if (idx >= 2) {
				return $1 + "m" + $2; }
			else { return $1 + "h" + $2; } } );
		window.event.returnValue = 0; }
	}
	
	if (key_code != 9) {
		event.returnValue = false; }		  
}

/****************************************************************
* Name:		is_valid_date
* Purpose:	Checks to see if a string is a valid date in the
*			format MM/DD/YYYY.
****************************************************************/

function is_valid_date(dateStr) {
	var re = /^\d{2}\/\d{2}\/\d{4}$/;
	if (re.test(dateStr) == false) { 
		return false;
	}
	var parts = dateStr.split("/");
	var dt = new Date(parseFloat(parts[2]), parseFloat(parts[0])-1, parseFloat(parts[1]), 0, 0, 0, 0);
	if (parseFloat(parts[1]) != dt.getDate()) { 
		return false;
	}
	if (parseFloat(parts[0])-1 != dt.getMonth()) { 
		return false;
	}
	return true;
}

/****************************************************************
* Name:		is_valid_time
* Purpose:	Checks to see if a string is a valid time.
****************************************************************/
function is_valid_time(value) {
	var hasMeridian = false;
	var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
	if (!re.test(value)) {
		return false;
	}
	if (value.toLowerCase().indexOf("p") != -1) {
		hasMeridian = true;
	}
	if (value.toLowerCase().indexOf("a") != -1) {
		hasMeridian = true;
	}
	var values = value.split(":");
	if ((parseFloat(values[0])< 0)||(parseFloat(values[0])>23)) {
		return false;
	}
	if (hasMeridian) {
		if ((parseFloat(values[0])<1)||(parseFloat(values[0])>12)) {
			return false;
		}
	}
	if ((parseFloat(values[1])<0)||(parseFloat(values[1])>59)) {
		return false;
	}
	if (values.length > 2) {
		if ((parseFloat(values[2])<0)||(parseFloat(values[2])>59)) {
			return false;
		}
	}
	return true;
}

/****************************************************************
* Name:		typeAheadInfo
* Purpose:	Global storage object for type-ahead info, including
*           reset() method. Used by TypeAhead function, below. 
* Added:    BLC 08/06/2004
****************************************************************/
var typeAheadInfo = {last:0, 
                     accumString:"", 
                     delay:1200,
                     timeout:null, 
                     reset:function() {this.last=0; this.accumString=""}
                    };

/****************************************************************
* Name:		TypeAhead
* Purpose:	Attached as needed to select element's onkeydown
*           event to provide type-ahead selection capability.
* Added:    BLC 08/06/2004
* Note:     The typeAheadInfo object (above) is a dependency.
****************************************************************/
function TypeAhead() {
   // limit processing to IE event model supporter; don't trap Ctrl+keys
   if (window.event && !window.event.ctrlKey) {
      // timer for current event
      var now = new Date();
      // process for an empty accumString or an event within [delay] ms of last
      if (typeAheadInfo.accumString == "" || now - typeAheadInfo.last < typeAheadInfo.delay) {
         // make shortcut event object reference
         var evt = window.event;
         // get reference to the select element
         var selectElem = evt.srcElement;

         // get typed character keyboard (NOT ASCII) value
         var charCode = evt.keyCode;

		 if ((charCode > 95) && (charCode < 106)) {
  			// HACK: Re-map keypad numbers to the number characters
			// (because fromCharCode does not map them correctly).
			charCode -= 48;
		 }

         // get the ASCII character, converted to uppercase
         // if it is a letter.
         var newChar =  String.fromCharCode(charCode).toUpperCase();
        
		 //Self-service confessional debugging.
		 //alert ("Code: " + charCode);
		 
		 if (charCode == 13) {
			// Redirect Enter key to click on the "Submit" button.
			ClickSubmitButton();
		 
		 //} else if (charCode == 9) {

			// Tab key			
			
		 } else if (((newChar < "A") || (newChar > "Z"))
		     && ((newChar < "0") || (newChar > "9"))
		     && (newChar != " ")) { 
			
			// stop any previous timeout timer
			clearTimeout(typeAheadInfo.timeout);
			// store current event's time in object 
			typeAheadInfo.last = now;
			// reset typeAhead properties in [delay] ms unless cleared beforehand
			typeAheadInfo.timeout = setTimeout("typeAheadInfo.reset()", typeAheadInfo.delay);
         } else {
			// append new character to accumString storage
			typeAheadInfo.accumString += newChar;
			// grab all select element option objects as an array
			var selectOptions = selectElem.options;
			// prepare local variables for use inside loop
			var txt, nearest;
			// look through all options for a match starting with accumString
			for (var i = 0; i < selectOptions.length; i++) {
				// convert each item's text to uppercase to facilitate comparison
				// (use value property if you want match to be for hidden option value)
				txt = selectOptions[i].text.toUpperCase();
				// record nearest lowest index, if applicable
				nearest = (typeAheadInfo.accumString > 
						txt.substr(0, typeAheadInfo.accumString.length)) ? i : nearest;
				// process if accumString is at start of option text
				if (txt.indexOf(typeAheadInfo.accumString) == 0) {
				// stop any previous timeout timer
				clearTimeout(typeAheadInfo.timeout);
				// store current event's time in object 
				typeAheadInfo.last = now;
				// reset typeAhead properties in [delay] ms unless cleared beforehand
				typeAheadInfo.timeout = setTimeout("typeAheadInfo.reset()", typeAheadInfo.delay);
				// visibly select the matching item
				selectElem.selectedIndex = i;
				// prevent default event actions and propagation
				evt.cancelBubble = true;
				evt.returnValue = false;
				// exit function
				return false;   
				}            

			} // ((newChar < "A") || (newChar > "Z"))
         }
         // if a next lowest match exists, select it
         if (nearest != null) {
            selectElem.selectedIndex = nearest;
         }
      } else {
         // not a desired event, so clear timeout
         clearTimeout(typeAheadInfo.timeout);
      }
      // reset global object
      typeAheadInfo.reset();
   }
   return true;
}

/*****************************************************************
* Name:		ClickSubmitButton
* Purpose:	Click the submit button on the current form.
* Added:    BLC 08/09/2004
*****************************************************************/
function ClickSubmitButton() {
    var i; // Loop counter
	
	//Loop through form looking for a item to set focus on
	for (i=0;i<document.forms(0).elements.length;i++) {
		//Continue as long as we have not found an item
		if (document.forms(0).elements(i).type == "submit") {
			// Engage the submit element only if enabled
			if(!document.forms(0).elements(i).disabled) {
				document.forms(0).elements(i).click();
			}
			break;
		}
	}	
}

