/*
* DOM/HtmlElement Functions
* 
* @author       Seisan Consulting   2-16-2006 
*/


var DOMFunctions = new function() {
    this.getBrowserWidth = function() {
        if (window.innerHeight && window.scrollMaxY) {// Firefox
            return window.innerWidth + window.scrollMaxX;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            return document.body.scrollWidth;
        } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
            return document.body.offsetWidth;
        }
    }

    this.getBrowserHeight = function() {
        if (window.innerHeight && window.scrollMaxY) {// Firefox
            return window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            return document.body.scrollHeight;
        } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
            return document.body.offsetHeight;
        }
    }


    /*
    * 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 
    */
    this.getEventCurrentTarget = function(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 
    */
    this.getEventElement = function(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 
    */
    this.getImageXfromLeft = function(e) {
        if (e.layerX) 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 
    */
    this.getImageYfromTop = function(e) {
        if (e.layerY) 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 
    */
    this.getClientXfromLeft = function(e) {
        var scrollx = (document.all) ? document.body.scrollLeft : window.pageXOffset;
        if (e.clientX) 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 
    */
    this.getClientYfromTop = function(e) {
        var scrolly = (document.all) ? document.body.scrollTop : window.pageYOffset;
        if (e.clientY) 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 
    */
    this.appendOptionToSelect = function(select, option) {
        try {
                var num = select.options.length;
                select.options[num] = option;
        }
        catch (exc) {
            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 
    */
    this.getSelectOption = function(value, text) {
        var opt;
        try {
            opt = new Option(text, value);
        }
        catch (exc) {
            opt = document.createElement("option");
            opt.value = value;
            opt.text = text;
        }
        return opt;
    }

    /*
    * 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 
    */
    this.removeSomeChildren = function(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 
    */
    this.removeAllChildren = function(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 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 
    */
    this.dumpProps = function(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) { this.dumpProps(obj[i], parent + "." + i); } else { this.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 
    */
    this.getStyle = function(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 
    */
    this.getBrowserWindowHeight = function() {
        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 
    */
    this.getBrowserWindowWidth = function() {
        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 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 
    */
    this.changeElementVisibility = function(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 
    */
    this.createInputElement = function(type, name, value) {
        try {
            return document.createElement("<input type=\"" + type + "\" name=\"" + name + "\" value=\"" + value + "\"/>");
        } catch (exc) {
            input = document.createElement("input");
            input.type = type;
            input.name = name;
            input.value = value;
            return input;
        }
    }

    this.setOpacity = function(element, opacity) {
        element.style.opacity = opacity / 100;
        element.style.filter = 'alpha(opacity=' + opacity + ')';
    }


    this.fireEvent = function(obj, evt) {
        var fireOnThis = obj;
        if (document.createEvent) {
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent(evt, true, false);
            fireOnThis.dispatchEvent(evObj);
        }
        else if (document.createEventObject) {
            fireOnThis.fireEvent('on' + evt);
        }
    }
}
