/*
* Utility Functions
*
*/

/*
* Function to take a standard javascript event object and return the target that triggered the event.
* Pre-Condtion: An javascript event must exist.
* Post-Condition: The target that triggered the event is returned.
*
* @param        e - standard javascript event
* @return		targ - the target that triggered the event.
* @author       Seisan Consulting   2-16-2006
*/
function getEventCurrentTarget(e){
	var targ
	if (!e) var e = window.event
	if (e.currentTarget) targ = e.currentTarget
	else if (e.srcElement) targ = e.srcElement
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode
	return targ;
}

/*
* Function to take a standard javascript event object and return the physical element that triggered the event.
* Pre-Condtion: An javascript event must exist.
* Post-Condition: The physical element that triggered the event is returned.
*
*
* @param        e - standard javascript event.
* @return		targ - Returns the physical element that triggered the event.
* @author       Seisan Consulting   2-16-2006
*/
function getEventElement(e){
	var targ
	if (!e) var e = window.event
	if (e.target) targ = e.target
	else if (e.srcElement) targ = e.srcElement
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode
	return targ;
}

/*
* Function to get the x value of the physical element clicked.
* Pre-Condtion: An javascript event must exist.
* Post-Condition: The x value of the physical element clicked is returned.
*
* @param        e - a standard javascript event
* @return		event.offsetX - the x value of the physical element clicked.
* @author       Seisan Consulting   2-16-2006

function getImageXfromLeft(e) {
  if (!browserCheck.ie) return e.layerX;
  else return event.offsetX;
}


* Function to get the y value of the physical element clicked.
* Pre-Condtion: An javascript event must exist.
* Post-Condition: The y value of the physical element clicked is returned.
*
*
* @param        e - a standard javascript event
* @return		e.layerY - The y value of the physical element clicked.
* @author       Seisan Consulting   2-16-2006

function getImageYfromTop(e) {
  if (!browserCheck.ie) return e.layerY;
  else return event.offsetY;
}


* Function to get the x value of the location clicked in the browser client.
* Pre-Condtion: An javascript event must exist.
* Post-Condition: The x value of the location clicked in the browser client is returned.
*
* @param        e - a standard javascript event
* @return 		event.clientX + scrollx - x location clicked in the browser client.
* @author       Seisan Consulting   2-16-2006

function getClientXfromLeft(e) {
	var scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset;
	if (!browserCheck.ie) return e.clientX + scrollx;
	else return event.clientX + scrollx;
}


* Function to get the y value of the location clicked in the browser client.
* Pre-Condtion: An javascript event must exist.
* Post-Condition: The y value of the location clicked in the browser client is returned.
*
* @param        e - s standard javascript event
* @return 		event.clientY + scrolly - the y value of the location clicked in the browser client.
* @author       Seisan Consulting   2-16-2006

function getClientYfromTop(e) {
	 var scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
	if (!browserCheck.ie) return e.clientY + scrolly;
	else return event.clientY + scrolly;
}

/*
* Function to append an option to a select statement.
* Pre-Condition: The select statement must exist.
* Post-Condition: The option is added to the select statement passed to the function.
*
* @param        select - the name of the select statement to be appended.
* @param		option - the option to be appeneded to the select statement.
* @return		select - the select statement with the new option appended.
* @author       Seisan Consulting   2-16-2006
*/
function appendOptionToSelect(select, option){
	if(browserCheck.ie){
		var num = select.options.length;
		select.options[num] = option;
	}
	else {
		select.appendChild(option);
	}
	return select;
}


/*
* Function that creates an option for a select statement.
* Post-Condition: An option is created with the text and value pair.
* @param        value - a value of a option
* @param		text - the text of the option
* @return		opt - the completed option statement
* @author       Seisan Consulting   2-16-2006
*/
function getSelectOption(value, text){
	var opt;
	if(browserCheck.ie){
		opt = new Option(text,value);
	}
	else {
		opt = document.createElement("option");
		opt.value = value;
		opt.text = text;
	}
	return opt;
}


/*
* Function to get an XML Request object.
* Pre-Condition: The client browser must support XML transfers
* Post-Condition: The correct XML request object is returned based on the client browser
*
* @return		An XML Rquest Object
* @author       Seisan Consulting   2-16-2006

function getHTTPRequest(){
	if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
	else {
		alert("You browser doesn't support xml transfers, you cannot view this!");
	}
}

/*
* Function to retrieve an XML Document.
* Post-Condition: The XML document is returned.
*
* @return		xmlDoc - an XML document
* @author       Seisan Consulting   2-16-2006

function getXMLDocument() {
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject){
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument){
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else{
		alert('Your browser cannot handle this script');
	}
	return xmlDoc;
}

/*
* Function to retrieve a node from an XML document.
* Pre-Condition: The XMl node must exist.
* Post-Condition: An array of values are returned from the node specified.
*
* @param        xmlDoc - the XML document the node is retrieved from.
* @param		str - the node requested.
* @return		An Array of the node values.
* @author       Seisan Consulting   2-16-2006

function getNode(xmlDoc, str){
	//gets node from xmlDoc, node tree is split by '/'
	try{
		var arr = str.split('/');
		return getNodeViaArray(xmlDoc.responseXML.documentElement, 0, arr);
	}
	catch(exc){
		return null;
	}
}

/*
* Function that recursively finds an XML node at the end of an array.
* Pre-Condition: The XML node exists.
* Post-Condition: If found an XML node is returned.
*
* @param        node - element currently at
* @param		num - number of the element in the current array
* @param 		arr - array containing xml node names
* @return		If found an XML node, if not null is returned.
* @author       Seisan Consulting   2-16-2006

function getNodeViaArray(node, num, arr){
	//recursive function finds node at the end of the arr array
	if(num == arr.length){
		return node;
	}
	else if(arr[num] == ""){
		return getNodeViaArray(node, num+1, arr);
	}
	else if(arr[num] == node.nodeName){
		return getNodeViaArray(node, num+1, arr);
	}
	else {
		var children = node.childNodes;
		for(i=0; i < children.length;i++){

			if(children[i].nodeName == arr[num]){
				return getNodeViaArray(children[i], num+1, arr);
			}
		}
		return null;
	}
}

/*
* Function to check whether an object is in an array.
* Post-Condition: A boolean value is returned evaluating if the object is found in the array.
*
* @param        needle - an object
* @param		haystack - an array
* @return 		Boolean value holding whether the object is found in the array or not.
* @author       Seisan Consulting   2-16-2006

function isIn(needle, haystack){
	for(var i=0; i < haystack.length; i++){
		if(haystack[i].match(needle) != null)
			return true;
	}
	return false;
}

/*
* Function to some of the children from a node with the exception of those specified as exempt.
* Pre-Condition: The node is an html element
* Post-Condition: All child elements are removed from the node
*
* @param        node - node to which the children are to be removed from
* @param		exempt - children exceptions - not to be removed
* @author       Seisan Consulting   2-16-2006

function removeSomeChildren(node, exempt){
	if(!node){
		return;
	}

	var len = node.childNodes.length;

	for(var i = 0; i < len; i++){
		try{
			if(!isIn(node.childNodes[i].id, exempt)){
				node.removeChild(node.childNodes[i]);
			}
		}
		catch(ex){}
	}

}

/*
* Function to remove all children from a node.
* Pre-Condition: The node is an html element.
* Post-Condition: All children are removed from the node.
*
* @param        node - node to have all children removed from.
* @author       Seisan Consulting   2-16-2006
*/
function removeAllChildren(node){
	if(!node){
		return;
	}

	var len = node.childNodes.length;

	for(var i = 0; i < len; i++){
		try{
			node.removeChild(node.childNodes[i]);
		}
		catch(ex){}
	}

	node.innerHTML = "";

}

/*
* Function to create a web cookie.
* Pre-Condition: The String passed is in the format yyyy-mm-dd hh:mm:ss.
* Post-Condition: A javascript Date object is created and returned.
*
* @param        str - a formatted string containing a date and time
* @return		date - a standard javascript date object
* @author       Seisan Consulting   2-16-2006

function odbcCanonicalToJSDate(str){
	var year = str.substring(0,4);
	var month = str.substring(5,7);
	var day = str.substring(8,10);
	var hours = str.substring(11,13);
	var minutes = str.substring(14,15);
	var seconds = str.substring(17,19);

	var date = new Date();
	date.setDate(parseInt(day));
	date.setMonth(parseInt(month-1));
	date.setFullYear(parseInt(year));
	date.setHours(parseInt(hours));
	date.setMinutes(parseInt(minutes));
	date.setSeconds(parseInt(seconds));
	return date;

}

/*
* Function to display all member functions of an object through standard output.
* Pre-Condition: A web cookie is created with the name "name", a value of "value" and set to expire on the time specified in "days" converted to UTC.
* Post-Condition: All member functions are displayed of the object passed in.
*
* @param        obj - the object that contains member functions.
* @param		parent - second parameter.
* @author       Seisan Consulting   2-16-2006

function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof obj[i] == "object") {
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}

/*
* Function to read a CSS type Stylesheet.
* Pre-Condition: Information exists in a CSS type Stylesheet
* Post-Condition: A string is returned displaying information in a CSS Stylesheet.
*
* @param        oElm - element
* @param		strCssRule - callback function
* @return 		strValue - value of the information read from the stylesheet.
* @author       Seisan Consulting   2-16-2006

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}


/*
* Function retrieve the value of the inner window height of the client browser.
* Post-Condition: The value of the inner window height of the client browser is returned.
*
* @return		myHeight - value of the height of the inner window of the client browser.
* @author       Seisan Consulting   2-16-2006

function getBrowserWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

/*
* Function retrieve the value of the inner window wifth of the client browser.
* Post-Condition: The value of the inner window width of the client browser is returned.
*
* @return		myWidth - value of the width of the inner window of the client browser.
* @author       Seisan Consulting   2-16-2006

function getBrowserWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

/*
* Function to format a number passed into Currency.
* Pre-Condition: The value passed into the function must be a valid number.
* Post-Condition: The number is converted and displayed as currency in dollars and cents.
*
* @param        num - a value to be converted into currency
* @return		A string that contains the formatted value of the currency.
* @author       Seisan Consulting   2-16-2006

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

/*
* Function to change the visibility status of an html element.
* Pre-Condition; The element must be an html element.
* Post-Condition: The visibility status of the html element is switched based on the initial status of the element.
*
* @param        element - an html element
* @param		status - visibility status
* @author       Seisan Consulting   2-16-2006

function changeElementVisibility(element, status)
{
	if (document.layers)
	{
		if(status == "visible")
			status = "show";
		else
			status = "hide";
		element.visibility = status;
	}
	else if (document.all || document.getElementById)
	{
		element.style.visibility = status;
	}
}



* Function to build an html input element.
* Pre-Condition: none
* Post-Condition: An html input element is constructed based on the type, name and value.
*
* @param        type - input type
* @param		name - input name
* @param		value - input value
* @return       input - an html input element
* @author       Seisan Consulting   2-16-2006
*/
function createInputElement(type, name, value){
	if(browserCheck.ie)
		return document.createElement("<input type=\"" + type + "\" name=\"" + name + "\" value=\"" + value + "\"/>");
	else {
		input = document.createElement("input");
		input.type = type;
		input.name = name;
		input.value = value;
		return input;
	}
}
