AdapterUtils = {
    getEventElement: function(event) {
        return event.target || event.srcElement;
    },
    
    trim: function(string) {
	    return string.replace(/^\s+|\s+$/g,"");
    },
    
    canHaveClass: function (element) {
        return ((element != null) && (element.className != null));
    },

    hasAnyClass: function (element) {
        return (AdapterUtils.canHaveClass(element) && (element.className.length > 0));
    },

    hasClass: function (element, specificClass) {
        return (AdapterUtils.hasAnyClass(element) && (element.className.indexOf(specificClass) > -1));
    },

    addClass: function (element, classToAdd) {
        if (AdapterUtils.hasAnyClass(element))
        {
            if (!AdapterUtils.hasClass(element, classToAdd))
            {
                element.className = element.className + " " + classToAdd;
            }
        }
        else if (AdapterUtils.canHaveClass(element))
        {
            element.className = classToAdd;
        }
    },

    addClassUpward: function (startElement, stopParentClass, classToAdd) {
        var elementOrParent = startElement;
        while ((elementOrParent != null) && (!AdapterUtils.hasClass(elementOrParent, topmostClass)))
        {
            AdapterUtils.addClass(elementOrParent, classToAdd);
            elementOrParent = elementOrParent.parentNode;
        }    
    },

    swapClass: function (element, oldClass, newClass) {
        if (AdapterUtils.hasAnyClass(element))
        {
            element.className = element.className.replace(new RegExp(oldClass, "gi"), newClass);
        }
    },

    swapOrAddClass: function (element, oldClass, newClass) {
        if (AdapterUtils.hasClass(element, oldClass))
        {
            AdapterUtils.swapClass(element, oldClass, newClass);
        }
        else
        {
            AdapterUtils.addClass(element, newClass);
        }
    },

    removeClass: function (element, classToRemove) {
        AdapterUtils.swapClass(element, classToRemove, "");
    },

    removeClassUpward: function (startElement, stopParentClass, classToRemove) {
        var elementOrParent = startElement;
        while ((elementOrParent != null) && (!AdapterUtils.hasClass(elementOrParent, topmostClass)))
        {
            AdapterUtils.removeClass(elementOrParent, classToRemove);
            elementOrParent = elementOrParent.parentNode;
        }    
    },

    isEnterKey: function () {
        var retVal = false;
        var keycode = 0;
        if ((typeof(window.event) != "undefined") && (window.event != null))
        {
            keycode = window.event.keyCode;
        }
        else if ((typeof(e) != "undefined") && (e != null))
        {
            keycode = e.which;
        }
        if (keycode == 13)
        {
            retVal = true;
        }
        return retVal;
    },
    
    htmlEncode: function(str) {	    
	    str = str.replace(/&/ig, "&amp;");
	    str = str.replace(/</ig, "&lt;");
	    str = str.replace(/>/ig, "&gt;");
	    str = str.replace(/\x22/ig, "&quot;");
	    return str;
    },
    
    htmlDecode: function(str) {	    	    
	    str = str.replace(/\x26lt\x3B/ig, "<");
	    str = str.replace(/\x26gt\x3B/ig, ">");
	    str = str.replace(/\x26quot\x3B/ig, "\"");
	    str = str.replace(/\x26amp\x3B/ig, "&");
	    return str;
    },
    
    escape: function(text) {
        if (!arguments.callee.sRE) {
            var specials = [
              '/', '.', '*', '+', '?', '|',
              '(', ')', '[', ']', '{', '}', '\\'
            ];
            arguments.callee.sRE = new RegExp(
              '(\\' + specials.join('|\\') + ')', 'g'
            );
        }
        return text.replace(arguments.callee.sRE, '\\$1');
    },
    
     getAttribute: function(elem, attrName) {
        var nodeValue = null;
        
        if (elem.getAttribute) {        
            nodeValue = elem.getAttribute(attrName);
        } else {
            var attr = elem.attributes.getNamedItem(attrName);
            if (attr != null)
                nodeValue = attr.nodeValue;
        }
        
        if (nodeValue != null)
            return nodeValue;
        else
            return "";            
    }
};

AdapterUtils.userAgent = navigator.userAgent;    
AdapterUtils.versionOffset = AdapterUtils.userAgent.indexOf("MSIE");
AdapterUtils.isIE = AdapterUtils.versionOffset >= 0;
AdapterUtils.fullVersionIE = AdapterUtils.isIE ? parseFloat(AdapterUtils.userAgent.substring(AdapterUtils.versionOffset+5, AdapterUtils.userAgent.length)): "";
AdapterUtils.majorVersionIE = AdapterUtils.isIE ? parseInt('' + AdapterUtils.fullVersionIE) : "";
AdapterUtils.isPreIE7 = AdapterUtils.majorVersionIE < 7;

