
function clsHTML() {
    var me = this;
    this.doctype = "HTML";

    this.title = document.title;

    this.remove = function(strNode) {
        var n = me.get(strNode);
        if (!n) { return; }
        n.parentNode.removeChild(n);
        return;
    };

    //----------------------------------------------
    //  get
    //----------------------------------------------
    //  Description:  finds a HTML node by strNode in the 
    //                current document and returns that node
    //
    //  Parameter 1:  strNode - HTML node ID to find
    //
    //  RETURN:       success(object_node), fail(null)
    //----------------------------------------------
    this.get = function(strNode) {
        var oHTML = document.getElementById(strNode);
        if (!oHTML) { return (null); }
        return (oHTML);
    }

    //----------------------------------------------
    //  append
    //----------------------------------------------
    //  Description:  finds a HTML node by strNode in the 
    //                current document and appends newNode
    //
    //  Parameter 1:  strNode - HTML node ID to find
    //  Parameter 2:  newNode - new html node to append
    //
    //  RETURN:       success(true), fail(false)
    //----------------------------------------------
    this.append = function(strNode, newNode) {
        if (!newNode) { return (false); }
        oHTML = document.getElementById(strNode);
        if (oHTML == null) {
            oHTML = document.getElementsByTagName(strNode);
            if (oHTML.length == 0) { return (false); }

            oHTML = oHTML.item(0);
        }

        oHTML.appendChild(newNode);
        return (true);
    }

    //----------------------------------------------
    //  write
    //----------------------------------------------
    //  Description:  Searches for a strNode in the HTML where id matches
    //                strNode and updates the innerHTML with strData
    //
    //  Parameter 1:  strNode - HTML node ID to find
    //  Parameter 2:  strData - must be well formed HTML
    //
    //  RETURN:       success(true), fail(false)
    //----------------------------------------------
    this.write = function(strNode, strData) {
        if (!(oHTML = me.get(strNode))) { return (false); }
        oHTML.innerHTML = strData;
        return (true);
    };

    //----------------------------------------------
    //  buildSelect
    //----------------------------------------------
    //  Description:  Builds a string containing a well formed HTML selectbox
    //                It will create an <option> node for each item in arrSel
    //
    //  Parameter 1:  strNode - HTML node ID to find
    //  Parameter 2:  arrSel - 2D array,  example arrSel = new Array(Array(name, id), Array(name, id));
    //  Parameter 3:  selID  - id matching the id in arrSel to put focus
    //  Parameter 4:  selID  - the id matching the id in arrSel to put focus
    //
    //  RETURN:       success(true), fail(false)
    //----------------------------------------------
    this.buildSelect = function(strNode, arrSel, selID, selName, onChange) {
        if (onChange) {
            s = "<SELECT id='" + selName + "' name='" + selName + "' onChange=\"javascript:" + onChange + "(this);\">";
        }
        else {
            s = "<SELECT id='" + selName + "' name='" + selName + "' onChange=\"javascript:" + onChange + "(this);\">";
        }
        for (var c = 0; c < arrSel.length; c++) {
            s += "<OPTION id='" + arrSel[c][1] + "' name='" + arrSel[c][1] + "'" + (selID == c ? " SELECTED" : "") + " value='" + arrSel[c][1] + "'>" + arrSel[c][0] + "</OPTION>";
        }
        s += "</SELECT>";

        return (me.write(strNode, s));
    };

    this.insertOption = function(selID, optionID, index, value, text) {
        if (!(oHTML = me.get(selID))) { return (null); }
        if ((oHTML.tagName).toLowerCase() != "select") { return (null); }

        oOption = document.createElement("OPTION");
        oOption.setAttribute("id", optionID);
        oOption.setAttribute("name", optionID);
        oOption.setAttribute("value", value);
        oText = document.createTextNode(text);
        oOption.appendChild(oText);

        if (index < oHTML.childNodes.length) {
            oHTML.insertBefore(oOption, oHTML.childNodes.item(index));
        }
        else {
            oHTML.appendChild(oOption);
        }
        return;
    };

    this.mouseCoords = function(ev) {
        if (ev.pageX || ev.pageY) {
            return { x: ev.pageX, y: ev.pageY }
        }
        return {
            x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            y: ev.clientY + document.body.scrollTop - document.body.clientTop
        }
    };

    this.getSelectedNode = function(strNode) {
        if (!(oHTML = me.get(strNode))) { return (null); }
        if ((oHTML.tagName).toLowerCase() != "select") { return (null); }
        if (oHTML.options.length == 0) { return (null); }
        if (oHTML.selectedIndex == -1) { return (null); }

        return (oHTML.options[oHTML.selectedIndex]);
    };

    this.getSelectedValue = function(strNode) {
        oHTML = me.getSelectedNode(strNode);
        if (!oHTML) { return (null); }
        return (oHTML.value);
    };

    this.getSelectedText = function(strNode) {
        oHTML = me.getSelectedNode(strNode);
        if (!oHTML) { return (null); }
        return (oHTML.innerHTML);
    };

    this.getSelectedID = function(strNode) {
        oHTML = me.getSelectedNode(strNode);
        if (!oHTML) { return (null); }
        return (oHTML.getAttribute("id"));
    };

    this.getSelectedIndex = function(strNode) {
        oHTML = me.getSelectedNode(strNode);
        if (!oHTML) { return (null); }

        return (parseInt(oHTML.getAttribute("id")));
    };

    this.setSelectedValue = function(strNode, value) {
        var s = me.get(strNode);
        if (!s) { return; }
        for (var c = 0; c < s.options.length; c++) {
            if (s.options[c].value == value) {
                s.options[c].selected = true;
                return (true);
            }
        }
        return (false);
    };

    this.setSelectedIndex = function(strNode, index, id) {
        oHTML = me.get(strNode);
        if (!oHTML) { return (false); }

        oOptions = oHTML.getElementsByTagName("option");

        if (!index) {
            for (var c = 0; c < oOptions.length; c++) {
                if (id == oOptions.item(c).value) {
                    index = c;
                    break;
                }
            }
        }

        len = parseInt(oOptions.length);
        if (len > index) {
            oHTML.selectedIndex = index;
            return (true);
        }
        return (false);
    };

    this.getPosition = function(obj) {
        var l = t = 0;
        if (obj.offsetParent != null) {
            l = obj.offsetLeft;
            t = obj.offsetTop;
            while ((obj = obj.offsetParent) != null) {
                l += obj.offsetLeft;
                t += obj.offsetTop;
            }
        }
        return { left: l, top: t };
    };

    this.create = function(type) {
        var node = document.createElement(type);
        node.setAttribute("id", "");
        node.setAttribute("name", "");
        node.setAttribute("style", "");
        node.setAttribute("onmousedown", "");
        var oText = document.createTextNode(" ");
        node.appendChild(oText);
        return (node);
    };

    this.getSelectedText = function() {
        var txt = '';
        if (window.getSelection) {
            txt = window.getSelection();
        }
        else if (document.getSelection) {
            txt = document.getSelection();
        }
        else if (document.selection) {
            txt = document.selection.createRange().text;
        }
        
        return(txt);
    }

} //end class


