﻿(function() {

    var FORMS = sbs.register("sbs.forms");

    /*
    sbs.forms.addEvent

    args
    1 - node
    *required*
    the element to add an event handler to
    
    2 - type
    *required*
    the event type, WITHOUT 'on' before it (ex: 'click')
    
    3 - func
    *required*
    the function for the event handler to invoke
    
    4 - isPrimary
    *optional* (false if other events present) (true if this is first event)
    if isPrimary is set to true, or there are no other events present,
    then this event handler will return its value to the DOM object,
    allowing for postbacks to be cancelled, etc.
    */
    FORMS.addEvent = function(node, type, func, isPrimary) {
        if (isPrimary || !node["on" + type]) {
            node["on" + type] = func;
        }
        else if (node.addEventListener) {
            node.addEventListener(type, func, false);
        }
        else if (node.attachEvent) {
            node.attachEvent("on" + type, func);
        }

        /*
        if (node.addEventListener) {
        node.addEventListener(type, func, false);
        }
        else if (node.attachEvent) {
        node.attachEvent("on" + type, func);
        }
        else {
        node["on" + type] = func;
        }
        */
    };

    /*
    sbs.forms.getAspElement

    args
    1 - divId
    *required*
    the ID of the div surrounding the asp.net server control
    
    2 - doc
    *optional* (document)
    the document that the elements exist in
    
    returns
    if element found: the first DOM object contained inside the div with attribute "divid" = argument "divId"
    else: null
    */
    FORMS.getAspElement = function(divId, doc) {
        if (doc == null) doc = document;
        var element = doc.getElementById(divId);

        for (var i = 0, j = element.childNodes.length; i < j; i++) {
            var childNode = element.childNodes[i];
            if (childNode
			&& childNode.attributes
			&& childNode.attributes['divid']
			&& childNode.attributes['divid'].value == divId)

                return childNode;
        }

        return null;
    };

})();