<!--

/*
 * OSAS Net, LLC
 * 
 * Created: April 28, 2007
 * Author: Anthony Sprague
 *
 * Maintenance Log:
 * When          | Who            | Description
 * --------------|----------------|-------------------------------------------------------
 *               |                |
 *
 */

/**
 * JavaScript class that provides utility methods to be used globally across all pages.
 */
 
// enumeration for the different debugging levels.
var DebugLevels = {
	NONE: -1,
	TRACE: 0,
	INFO: 1,
	WARNING: 2,
	EXCEPTION: 3			
}

// Constants
var HEIGHT_BUFFER = 45;
var PIXEL = "px";
var REQUIRED = "required";
var OPEN_IMAGE = "images/imgTreeOpen.gif";
var CLOSE_IMAGE = "images/imgTreeClose.gif";
var DEFAULT_DEBUG_LEVEL = DebugLevels.EXCEPTION;

// Used to indicate the level of output to display to the screen.
var debugLevel = DebugLevels.NONE;

// Used to access the utility functions.
var utilityScript = new Object();

/**
 * --------------- UTILITY FUNCTIONS ---------------
 */


// This function displays a debug message if the debug boolean is turned on.
utilityScript.debug = function(msg, level) {
	if (debugLevel > DebugLevels.NONE) {	
		if (level >= debugLevel) {
			alert(msg);
		}
	}
}

// This function displays a formatted message.
utilityScript.message = function(msg, level, scrpt, funct, lineNumber) {
	try {
		var debugMessage = "********** " + this.getDebugLevelDisplay(level) + " **********\n";
		var obj = new Object();
		if (this.isObject(msg)) {
			obj = msg;
		} else {
			if (this.isPopulated(msg)) {
				obj.MESSAGE = msg;
			}
		}
		if (this.isPopulated(scrpt)) {
			obj.SCRIPT = scrpt;
		}
		if (this.isPopulated(funct)) {
			obj.FUNCTION = funct;
		}
		if (this.isPopulated(lineNumber)) {
			obj.LINE_NUMBER = lineNumber;
		}
		var infoMsg = this.toString(obj);
		if (this.isPopulated(infoMsg)) {
			debugMessage += infoMsg;
		}
		this.debug(debugMessage, level);
	} catch (e) {}
}

// This function displays a formatted exception message.
utilityScript.exception = function(e, scrpt, funct, lineNumber) {
	this.message(e, DebugLevels.EXCEPTION, scrpt, funct, lineNumber);
}

// This function displays a formatted warning message.
utilityScript.warning = function(msg, scrpt, funct, lineNumber) {
	this.message(msg, DebugLevels.WARNING, scrpt, funct, lineNumber);
}

// This function displays a formatted info message.
utilityScript.info = function(msg, scrpt, funct, lineNumber) {
	this.message(msg, DebugLevels.INFO, scrpt, funct, lineNumber);
}

// This function displays a formatted trace message.
utilityScript.trace = function(msg, scrpt, funct, lineNumber) {
	this.message(msg, DebugLevels.TRACE, scrpt, funct, lineNumber);
}

// This function returns the display name of the given element.
utilityScript.getDebugLevelDisplay = function(level) {
	var displayName = "";
	if (level == DebugLevels.TRACE) {
		displayName = "Trace";
	} else if (level == DebugLevels.INFO) {
		displayName = "Information";
	} else if (level == DebugLevels.WARNING) {
		displayName = "Warning";
	} else if (level == DebugLevels.EXCEPTION) {
		displayName = "Exception";
	} else if (level == DebugLevels.NONE) {
		displayName = "None";
	}
	return displayName;
}

// This function formats a exception message specifically for the utility script.
utilityScript.utilException = function(e, funct) {
	this.exception(e, "utilities.js", funct);
}

// This function formats a warning message specifically for the utility script.
utilityScript.utilWarning = function(msg, funct) {
	this.warning(msg, "utilities.js", funct);
}

// This function formats a info message specifically for the utility script.
utilityScript.utilInfo = function(msg, funct) {
	this.info(msg, "utilities.js", funct);
}

// This function formats a trace message specifically for the utility script.
utilityScript.utilTrace = function(msg, funct) {
	this.trace(msg, "utilities.js", funct);
}

// This function converts an object into an array of objects if necessary.
utilityScript.makeArray = function(obj) {
	this.utilTrace("start", "makeArray");
	var objArray = new Array();
	try {
		if (this.isArray(obj)) {
			objArray = obj;
		} else {
			if (obj) {
				objArray[0] = obj;
			}    	
	  	}
	} catch (e) {
		this.utilException(e, "makeArray");
	}
  	return objArray;
}

// This function replaces all occurrences of one value with another and returns the modified string.
utilityScript.replaceAll = function(str, repl, wth) {
	this.utilTrace("start", "replaceAll");
	var result = "";
	try {
		result = str.replace(new RegExp(repl, "g"), wth);
	} catch (e) {
		e.STR = str;
		e.REPL = repl;
		e.WTH = wth;
		this.utilException(e, "replaceAll");
	}
  	return 
}

// function to resize container2 if the container1 height is greater than the actual workspace height.
utilityScript.matchSize = function(container1, container2, heightBuffer) {
	this.utilTrace("start", "matchSize");
	var heightDifference = 0 + PIXEL;
	if (!heightBuffer) {
		heightBuffer = HEIGHT_BUFFER;
	}
	try {
		container1 = this.getElement(container1);
		container2 = this.getElement(container2);
	} catch (e) {
		e.STEP = "Getting container1 and container2.";
		this.utilException(e, "matchSize");
	}
	if (container1 && container2 && container1.clientHeight && container2.clientHeight) {
		try {
			var container1Height = container1.clientHeight;
			var container2Height = container2.clientHeight;
		} catch (e) {
			e.STEP = "Getting container1Height and container2Height.";
			this.utilException(e, "matchSize");
		}
		if (this.isGreaterThan(container1Height, container2Height)) {
			try {
				heightDifference = (container1Height - container2Height) + heightBuffer;
				var newHeight = container2Height + heightDifference + PIXEL;
				this.utilInfo("setting container2.style.height=" + newHeight, "matchSize");
				container2.style.height = newHeight;
			} catch (e) {
				e.STEP = "Setting container2.style.height.";
				e.container1Height = container1Height;
				e.container2Height = container2Height;
				e.heightDifference = heightDifference;
				e.newHeight = newHeight;
				this.utilException(e, "matchSize");
			}
		}		
	}	
	return heightDifference;
}

// This function displays a formatted error message if the debug boolean is turned on.
utilityScript.toString = function(obj) {
	var result = "";
	try {
		if (obj) {
			if (this.isString(obj)) {
				result = "\n" + obj;
			} else {
				for (var prop in obj) {
					result += "\n" + prop + ":\n" + obj[prop] + "\n";		
				}
			}			
		}
	} catch (e) {}
	return result;
}

// This method returns the anchor given the href.
utilityScript.findLinkByHref = function(href) {
	var link;
	for (var i = 0; utilityScript.isLessThan(i, document.links.length); i++) {
		if (document.links[i].href == href) {
			link = document.links[i];
			break;
		}
	}
	return link;
}

// This method returns the frame given the src.
utilityScript.findFrameByStyleClass = function(styleClass) {
	var frame;
	for (var i = 0; utilityScript.isLessThan(i, parent.document.frames.length); i++) {
		if (parent.document.frames[i].className == styleClass) {
			frame = parent.document.frames[i];
			break;
		}
	}
	return frame;
}

// This method returns the parent of the given element.
utilityScript.getParent = function(elem) {
	var parentElem;
	try {
		if (this.isIE()) {
			parentElem = elem.parentElement;
		} else {
			parentElem = elem.parentNode;
		}
	} catch (e) {}
	return parentElem;
}

/**
 * --------------- DOCUMENT FUNCTIONS ---------------
 */


// This function tries to set focus to the first input field in the first form.
utilityScript.setFocus = function() {
	this.utilTrace("start", "setFocus");
	var frm = document.forms[0];
  	try {    
    	if (frm) {
      		var elems = this.makeArray(frm.elements);
      		for (var i = 0; i < elems.length; i++) {
        		if (this.setElementFocus(elems[i])) {
          			break;
        		}
      		}
    	} else {
      		if (canvas) {
        		canvas.focus();
      		}
    	}
  	}
  	catch (e) {
  		this.utilException(e, "setFocus");
  	}
}

/**
 * --------------- FORM FUNCTIONS ---------------
 */


// This function is the clears enabled/visible elements of the given form
utilityScript.clearForm = function(frm) {
	this.utilTrace("start", "clearForm");
	if (!frm) {
		frm = document.forms[0];
	}
	if (frm) {
		var elems = this.makeArray(frm.elements);
		for (var i = 0; utilityScript.isLessThan(i, elems.length); i++) {
			if (this.isVisibleFormElement(elems[i])) {  
	            try {
	              	if (elems[i].disabled || elems[i].readOnly) {
	                	continue;
	              	}
	              	this.clearElement(elems[i]);
	            } catch(e) {
	            	e.INDEX = i;
	            	this.utilException(e, "clearForm");
	            }            
			}
		}
		this.setFocus();
	}
}

// This function is the clears enabled/visible elements of the given form
utilityScript.validateFormRequired = function(frm) {
	this.utilTrace("start", "validateFormRequired");
	var reqErrorsOccured = false;
	if (!frm) {
		frm = document.forms[0];
	}
	if (frm) {
		var elems = this.makeArray(frm.elements);
		var failedElems = "";
		var firstFailedElem;
		for (var i = 0; utilityScript.isLessThan(i, elems.length); i++) {
			if (this.isVisibleFormElement(elems[i])) {  
	            try {
	              	if (this.isElementRequired(elems[i]) && this.isElementBlank(elems[i])) {
	              		reqErrorsOccured = true;
	              		failedElems += "\n" + this.getElementDisplayName(elems[i]);
	              		if (!firstFailedElem) {	              			
							firstFailedElem = elems[i];
						}
	              	}
	            } catch(e) {
	            	e.INDEX = i;
	            	this.utilException(e, "validateFormRequired");
	            }            
			}
		}
		if (reqErrorsOccured) {
			var message = "Please provide a value for the following required fields:\n";
			message += failedElems;
			alert(message);
			if (firstFailedElem) {
				this.setElementFocus(firstFailedElem);
			}
		}
	}
	return !reqErrorsOccured;
}

// populates all non-checked checkboxes with value of "off".  without this if a checkbox
// isnt checked then it will not post.      
utilityScript.populateEmptyCheckboxes = function(frm) {
	if (!frm) {
		frm = document.forms[0];
	}
	if (frm) {
		var elems = this.makeArray(frm.elements);
		for (var i = 0; this.isLessThan(i, elems.length); i++) {
			if (elems[i].type == "checkbox") {  
	            try {
	              	if (elems[i].disabled || elems[i].readOnly) {
	                	continue;
	              	}
	              	if (!elems[i].checked) {
	              	  elems[i].checked=true;
	              	  elems[i].value="off";
	              	}
	            } catch(e) {	            	
	            }            
			}
		}
	}
}
      
/**
 * --------------- ELEMENT FUNCTIONS ---------------
 */


// This function returns an element on the page. If the parameter is an object with
// a valid tag name, then the parameter is an element and it is returned. Otherwise,
// the parameter is assumed to be an element name and a search is done for that element.
utilityScript.getElement = function(elem) {
	var result = elem;
	try {
		if (elem && !elem.tagName) {
			result = document.getElementById(elem);
			if(document.getElementById&&document.getElementById(elem)) {
				result = document.getElementById(elem);
			} else if(document.all&&document.all(elem)) {
				result = document.all(elem);
			} else if(document.layers&&document.layers[elem]) {
				result = document.layers[elem];
			}
		} 
	} catch (e) {
		this.utilException(e, "getElement");
	}
	return result;
}

// function to set the focus on the given form element.  Returns a boolean indicating
// success or failure.
utilityScript.setElementFocus = function(elem) {
	this.utilTrace("start", "setElementFocus");
	var result = true;
	elem = this.getElement(elem);
	if (elem) {
		try {
			elem.focus();
			elem.select();
		} catch (e) {
			this.utilException(e, "setElementFocus");
			result = false;
     	}
  	}
  	return result;
}

// This function disables the given element. If the parameter is an object with
// a valid tag name, then the parameter is an element and it is returned. Otherwise,
// the parameter is assumed to be an element name and a search is done for that element.
utilityScript.disableElement = function(elem) {
	this.utilTrace("start", "disableElement");
	elem = this.getElement(elem);
	try {
	  	if (elem != null) {
	    	if (elem.type == "password" || elem.type == "text" || elem.type == "textarea") {
	      		elem.readOnly = true;
	      		elem.className = "fielddisabled";
	    	} else if (elem.type == "select-one" || elem.type == "select-multiple") {
	      		elem.disabled = true;
	      		elem.className = "fielddisabled";
	    	} else {
	      		if (elem.type) {
	        		elem.disabled = true;
	      		} else {
	        		elem.disabled = true;
	        		if (elemType == "a") {
	          			elem.href="#";
	        		}
	      		}
	    	}
	  	}
	} catch (e) {
		this.utilException(e, "disableElement");
	}
}

// This function enables the given element. If the parameter is an object with
// a valid tag name, then the parameter is an element and it is returned. Otherwise,
// the parameter is assumed to be an element name and a search is done for that element.
utilityScript.enableElement = function(elem) {
	this.utilTrace("start", "enableElement");
  	elem = this.getElement(elem);
  	try {
	  	if (elem != null) {
	    	if (elem.type == "password" || elem.type == "text" || elem.type == "textarea") {
	    		elem.readOnly = false
	      		elem.className = "";
	    	} else if (elem.type == "select-one" || elem.type == "select-multiple") {
	      		elem.disabled = false;
	      		elem.className = "";
	    	} else {
	      		if (elem.type) {
	        		elem.disabled = false;
	      		} else {
	        		elem.disabled = false;
	      		}
	    	}
	  	}
	} catch (e) {
		this.utilException(e, "enableElement");
	}
}

// This function clears the element value
utilityScript.clearElement = function(elem) {
	this.utilTrace("start", "clearElement");
	try {
		if (elem && elem.type) {
			this.utilInfo("elem.id=" + elem.id + " | elem.type: " + elem.type + " | elem.value: " + elem.value, "clearElement");
			if (elem.type == "text") {
				if (elem.name.toLowerCase().indexOf("date") != -1 ) {
					elem.value = "  /  /    ";
                } else {
                  	elem.value = "";
                }
			} else if (elem.type == "textarea") {
				elem.value = "";
			} else if (elem.type == "select-one") {
				//elem.value = "";
			}
		}
	} catch (e) {
		this.utilException(e, "clearElement");
	}
}

// This function shows the given element.
utilityScript.toggleShowHideElement = function(elem, elemImg) {
	this.utilTrace("start", "toggleShowHideElement");
	var isShow = true;
  	elem = this.getElement(elem);
  	if (elemImg) {
  		elemImg = this.getElement(elemImg);
  	}
  	try {
	  	if (elem) {
	  		if (this.isElementVisible(elem)) {
	  			isShow = false;
	  			this.hideElement(elem);
	  			if (elemImg && elemImg.src) {
	  				elemImg.src = OPEN_IMAGE;
	  			}
	  		} else {
		  		isShow = true;
	  			this.showElement(elem);
	  			if (elemImg && elemImg.src) {
	  				elemImg.src = CLOSE_IMAGE;
	  			}
	  		}
	  	}
	} catch (e) {
		this.utilException(e, "showElement");
	}
	return isShow;
}

// This function shows the given element and all parent elements
utilityScript.showElementHierarchy = function(elem) {
	this.utilTrace("start", "showElementHierarchy");
  	elem = this.getElement(elem);
  	try {
	  	while (this.getParent(elem) != null) {
	  		if (elem.style.display == "none") {
	  			this.showElement(elem);
	  		}
	    	elem = this.getParent(elem);
	  	}
	} catch (e) {
		this.utilException(e, "showElementHierarchy");
	}
}

// This function shows the given element.
utilityScript.showElement = function(elem) {
	this.utilTrace("start", "showElement");
  	elem = this.getElement(elem);
  	try {
	  	if (elem && elem.style) {
	    	elem.style.display = "block";
	  	}
	} catch (e) {
		this.utilException(e, "showElement");
	}
}

// This function hides the given element.
utilityScript.hideElement = function(elem) {
	this.utilTrace("start", "hideElement");
  	elem = this.getElement(elem);
  	try {
	  	if (elem && elem.style) {
	    	elem.style.display = "none";
	  	}
	} catch (e) {
		this.utilException(e, "hideElement");
	}
}

// This function returns the display name of the given element.
utilityScript.getElementDisplayName = function(elem) {
	this.utilTrace("start", "getElementDisplayName");
	var displayName = "";
	elem = this.getElement(elem);
	if (elem) {
		if (this.isPopulated(elem.title)) {
			displayName = elem.title;
		} else {
			displayName = elems.id;
		}
	}
	return displayName;
}

// This function returns a boolean indicating whether the given element is a number.
utilityScript.validateElementsNumeric = function(elems) {
	this.utilTrace("start", "validateElementsNumeric");
	elems = this.makeArray(elems);
	var firstFailedElem;
	var failedElems = "";
	var numErrorsOccured = false;
	for (var i = 0; utilityScript.isLessThan(i, elems.length); i++) {
		var elem = this.getElement(elems[i]);
		var elemValue = elem.value;
		if (this.isPopulated(elemValue) && !this.isNumber(elemValue)) {
			numErrorsOccured = true;
			failedElems += "\n" + this.getElementDisplayName(elem);
			if (!firstFailedElem) {
				firstFailedElem = elem;
			}
			this.utilInfo("Not number.  elem.id=" + elem.id + " | elemvalue=" + elemValue, "validateElementsIsNumber");	
		}
	}  	
  	if (numErrorsOccured) {
		var message = "Please provide a numeric value for the following fields:\n";
		message += failedElems;
		alert(message);
		if (firstFailedElem) {
			this.setElementFocus(firstFailedElem);
		}
	}
	return !numErrorsOccured;
}

// checks if the value of the given element is blank
utilityScript.isElementBlank = function(elem) {	
	this.utilTrace("start", "isElementBlank");
	var result = true;
	elem = this.getElement(elem);
	if (elem && elem.type) {
		try {            
			if (elem.type == "text" || elem.type == "textarea" || elem.type == "select-one") {
				result = this.isBlank(elem.value);
			} else {
				result = this.isBlank(elem.value);
			}
		} catch (e) {
			this.utilException(e, "isElementBlank");
			result = true;
		}
	}
	return result;
}

// This function determines if the given element is populated
utilityScript.isElementPopulated = function(elem) {
    return !this.isElementBlank(elem);
}

// This function returns a boolean indicating whether the given element is visible.
utilityScript.isElementVisible = function(elem) {
	this.utilTrace("start", "isElementVisible");
  	elem = this.getElement(elem);
  	if (!elem && !elem.style) {
  		return false;
  	}
  	try {
	  	while (elem.style && elem.style.display != "none" && this.getParent(elem)) {
	    	elem = this.getParent(elem);
	  	}
	  	if (!elem || !elem.style) {
	  		return true;
	  	}
	} catch (e) {
		this.utilException(e, "isElementVisible");
		return false;
	}
  	return (elem.style.display != "none");
}

// This function returns a boolean indicating whether the given element is a visible form element.
utilityScript.isVisibleFormElement = function(elem) {
	this.utilTrace("start", "isVisibleFormElement ");	
	var result = false;
	try {
		result = elem && (elem.type == "text" || elem.type == "textarea" || elem.type == "select-one" || elem.type == "checkbox" || elem.type == "radio");  
	} catch (e) {
		this.utilException(e, "isVisibleFormElement");
	}
	return result;
}

// This function returns a boolean indicating whether the given element is required.
utilityScript.isElementRequired = function(elem) {
	this.utilTrace("start", "isElementRequired");
  	elem = this.getElement(elem);  	
  	return elem && this.isPopulated(elem.alt) && elem.alt == REQUIRED;
}

// This function returns a boolean indicating whether the given element is a number.
utilityScript.isElementNumber = function(elem) {
	this.utilTrace("start", "isElementNumber");
  	elem = this.getElement(elem);
  	return this.isNumber(elem);
}


/**
 * --------------- COMPARISON FUNCTIONS ---------------
 */


// This function determines if the given parameter is blank
utilityScript.isBlank = function(val) {
    return val == undefined || val == null || val == "";
}

// This function determines if the given parameter is populated
utilityScript.isPopulated = function(obj) {
    return !this.isBlank(obj);
} 

// This function determines if the given parameter is an array
utilityScript.isArray = function(obj) {
  return this.isObject(obj) && (obj.constructor == Array || obj.length && obj.length > 0);
}

// This function determines if the given parameter is a boolean
utilityScript.isBoolean = function(obj) {
  return typeof obj == 'boolean';
}

// This function determines if the given parameter is a function
utilityScript.isFunction = function(obj) {
    return typeof obj == 'function';
}

// This function determines if the given parameter is null
utilityScript.isNull = function(obj) {
    return typeof obj == 'object' && !a;
}

// This function determines if the given parameter is a number
utilityScript.isNumber = function(obj) {
	var result = true;
	for (var i = 0; utilityScript.isLessThan(i, obj.length); i++) {
        var c = obj.charAt(i);
        if ((c < "0") || (c > "9")) {
        	result = false;
        	break;
        }
    }
    return result;
}

// This function determines if the given parameter is a object
utilityScript.isObject = function(obj) {
    return (obj && typeof obj == 'object') || this.isFunction(obj);
}

// This function determines if the given parameter is a string
utilityScript.isString = function(obj) {
    return typeof obj == 'string';
}

// This function determines if the given parameter is undefined
utilityScript.isUndefined = function(obj) {
    return typeof obj == 'undefined';
} 

// This function returns a boolean indicating whether the first number is greater than
// the second number. This isn't really necessary to help with parsing errors but is here
// to be consistent with the 'less than' functions.
utilityScript.isGreaterThan = function(num1, num2) {
  	return num1 > num2;
}

// This function returns a boolean indicating whether the first number is greater than
// or equal to the second number. This isn't really necessary to help with parsing errors
// but is here to be consistent with the 'less than' functions.
utilityScript.isGreaterThanEqual = function(num1, num2) {
  	return num1 >= num2;
}

// This function returns a boolean indicating whether the first number is less than
// the second number. This helps with parsing errors when using the < in a for loop
// for example.
utilityScript.isLessThan = function(num1, num2) {
  	return num1 < num2;
}

// This function returns a boolean indicating whether the first number is less than
// or equal to the second number. This helps with parsing errors when using the < in
// a for loop for example.
utilityScript.isLessThanEqual = function(num1, num2) {
  	return num1 <= num2;
}

utilityScript.isIE = function() {
	return navigator.userAgent.indexOf("MSIE") >= 0;
}

utilityScript.isFirefox = function() {
	return navigator.userAgent.indexOf("Firefox") >= 0;
}

//-->