UTILITY_LOADED = true;

function isType(obj, typeName) 
{
    var type = new String(typeof(obj));
    return (type.toLowerCase() == typeName.toLowerCase());
}

function isString(obj) 
{
    return isType(obj, "string");
}

function isArray(obj) 
{
    return isType(obj, "array");
}

function isFunction(obj) 
{
    return isType(obj, "function");
}

function getElement(element) 
{
    if (isString(element))
        element = document.getElementById(element);
    return element;
}

function getRandomValue() 
{
    var value = "";
    var dt = new Date();
    value = dt.getMilliseconds();
    value += "" + Math.round(Math.random() * 1000);
    return value;
}

function getBrowserInfo() 
{
    var ua = navigator.userAgent.toLowerCase();
    var info = 
    {
        ie: (ua.indexOf("msie") > -1),
        netscape: (ua.indexOf("netscape") > -1),
        firefox: (ua.indexOf("firefox") > -1),
        safari: (ua.indexOf("safari") > -1),
        chrome: (ua.indexOf("chrome") > -1),
        opera: (ua.indexOf("opera") > -1),
        ie6: (ua.indexOf("msie 6") > -1),
        ie7: (ua.indexOf("msie 7") > -1),
        ie8: (ua.indexOf("msie 8") > -1),
        ie9: (ua.indexOf("msie 9") > -1),
        windows: (ua.indexOf("windows") > -1),
        mac: (ua.indexOf("mac") > -1),
        geckoEngine: (ua.indexOf("gecko") > -1),
        webkitEngine: (ua.indexOf("webkit") > -1),
        standardsMode: (document.compatMode != "BackCompat"),
        ieVersion: 0
    };
    info.ieVersion = (info.ie ? parseInt(ua.split("msie ")[1]) : 0);
    return info;
}

function getDocumentRoot() 
{
    var browser = getBrowserInfo();
    if (browser.ie6)
        return document.body;
    else if (document.documentElement)
        return document.documentElement;
    else
        return document.body;
}

function getPageGeometry(element) 
{
    element = getElement(element);
    var coord = { x: 0, y: 0, width: 0, height: 0 };
    if (element) 
    {
        var root = getDocumentRoot();
        var browser = getBrowserInfo();
        if (element.getBoundingClientRect) 
        {
            var rect = element.getBoundingClientRect();
            coord.x = Math.floor(rect.left);
            coord.y = Math.floor(rect.top);
            coord.width = Math.floor(rect.right - rect.left);
            coord.height = Math.floor(rect.bottom - rect.top);
            if (browser.ie && browser.ieVersion < 8) 
            {
                if (!(browser.ie6 && browser.standardsMode)) 
                {
                    coord.x -= 2;
                    coord.y -= 2;
                }
                if (!browser.standardsMode) 
                {
                    coord.x -= parseInt(root.style.borderLeftWidth);
                    coord.y -= parseInt(root.style.borderTopWidth);                    
                }
            }
            if (root.scrollTop) 
            {
                coord.x += root.scrollLeft;
                coord.y += root.scrollTop;
            }
        }
        else if (element.offsetParent) 
        {
            coord.width = element.offsetWidth;
            coord.height = element.offsetHeight;
            if (!(coord.width && coord.height))
            {
                coord.width = parseInt(element.style.width);
                coord.height = parseInt(element.style.height);
            }
            var obj = element;            
            while (obj) 
            {
                coord.x += obj.offsetLeft;
                coord.y += obj.offsetTop;
                if (browser.geckoEngine || browser.webkitEngine) 
                {
                    coord.x += parseInt(element.style.borderLeftWidth);
                    coord.y += parseInt(element.style.borderTopWidth);
                }
                obj = obj.offsetParent;
            }
            obj = element;
            while (obj.tagName.toLowerCase() != "html") 
            {
                coord.x -= obj.scrollLeft;
                coord.y -= obj.scrollTop;
                obj = obj.parentNode;
            }
            if (element.style.position.toLowerCase() != "fixed") 
            {
                obj = element;
                while (obj && obj.tagName) 
                {
                    coord.x -= parseInt(obj.scrollLeft);
                    coord.y -= parseInt(obj.scrollTop);
                    if (browser.geckoEngine && element.style.overflow != "visible") 
                    {
                        coord.x += parseInt(element.style.borderLeftWidth);
                        coord.y += parseInt(element.style.borderTopWidth);
                    }
                }
            }
            else 
            {
                if (browser.opera) 
                {
                    coord.x -= root.scrollLeft;
                    coord.y -= root.scrollTop;
                }
                else if (browser.geckoEngine || browser.safari) 
                {
                    coord.x += root.scrollLeft;
                    coord.y += root.scrollTop;
                }
            }
            coord.x = Math.floor(coord.x);
            coord.y = Math.floor(coord.y);
        }
    }
    return coord;
}

function getPageXY(element)
{
    var pos = getPageGeometry(element);
    return {x: pos.x, y: pos.y};
}

function setDimensions(element, width, height) 
{
    element = getElement(element);
    if (element) 
    {
        if (width)
            element.style.width = width + (!isNaN(width) ? "px" : "");
        if (height)
            element.style.height = height + (!isNaN(height) ? "px" : "");
    }
}

function setLocation(element, left, top) 
{
    element = getElement(element);
    if (element) 
    {
        if (left)
            element.style.left = left + (!isNaN(left) ? "px" : "");
        if (top)
            element.style.top = top + (!isNaN(top) ? "px" : "");
    }
}

function createElement(tagName, jsonAttributes, jsonStyleAttributes) 
{
    var element = getDocumentRoot().createElement(tagName);
    if (element) 
    {
        for (var attr in jsonAttributes)
            element.setAttribute(attr.name, attr.value);
        for (var attr in jsonStyleAttributes)
            element.style.setAttribute(attr.name, attr.value);
    }
    return element;
}

function getStyleAttribute(obj, name) 
{
    if (obj.getAttribute) 
    {
        var styleAttr = obj.getAttribute("style");
        if (styleAttr && styleAttr.getAttribute)
            return styleAttr.getAttribute(name);
        else
            return "";
    }
    else if (obj.style)
        return obj.style[name];
    else
        return "";
}

function getDimension(value, maxValue) 
{
    if (value && maxValue) 
    {
        valueString = new String(value);
        if (valueString.charAt(valueString.length - 1) == "%") 
        {
            value = parseInt(valueString.substring(0, valueString.length - 2));
            value = Math.max(maxValue * (value / 100), 0);
        }
        else if (valueString.toLowerCase() == "auto")
            value = maxValue;
    }
    else
        value = 0;
    return value;
}

function setStyleAttribute(obj, name, value) 
{
    if (obj.setAttribute) 
    {
        var styleAttr = obj.getAttribute("style");
        if (styleAttr && styleAttr.setAttribute)
            styleAttr.setAttribute(name, value);
        else if (obj.style)
            obj.style[name] = value;
    }
    else if (obj.style)
        obj.style[name] = value;
    else
        CSSStyleNotSupported = true;
}

function setNodeText(obj, text) 
{
    try 
    {
        if (obj.innerText)
            obj.innerText = text;
        if (obj.textContent)
            obj.textContent = text;
        obj.innerHTML = text;
        obj.nodeValue = text;
    }
    finally 
    {
    }
}

function getNodeText(obj) 
{
    if (obj.innerText)
        return obj.innerText;
    else if (obj.textContent)
        return obj.textContent;
    else if (obj.innerHTML)
        return obj.innerHTML;
    else
        return obj.nodeValue;
}

function setClassName(obj, name) 
{
    obj.setAttribute("class", name);
    obj.className = name;
}

function displayElement(obj, makeVisible) 
{
    if (obj) 
    {
        var displayValue = makeVisible ? "block" : "none";
        setStyleAttribute(obj, "display", displayValue);
    }
}

function createElementFromCode(html) 
{
    var element = document.createElement("div");
    setNodeText(element, html);
    return element;
}

function createClone(node) 
{
    var element = document.createElement("div");
    element.appendChild(node.cloneNode());
    return element;
}

function getHttpRequestObject() 
{
    try
    { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } 
    catch (e) {
        try
        { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } 
        catch (e) {
            try
            { return new ActiveXObject("Msxml2.XMLHTTP"); } 
            catch (e) {
                try
                { return new ActiveXObject("Microsoft.XMLHTTP"); } 
                catch (e) {
                    try
                    { return new XMLHttpRequest(); }
                    catch (e) 
                    { return null; }
                }
            }
        }
    }
}

function emptySelectList(field) 
{
    if (isString(field))
        field = document.getElementById(field);
    if (field.type && field.type == "select-one" || field.type == "select") 
    {
        for (var i = field.options.length - 1; i >= 0; i--)
            field.remove(i);
    }
}

function getFieldValue(objField) 
{
    var value = "";
    if (objField.type) 
    {
        switch (objField.type) 
        {
            case "text":
            case "textarea":
                value = objField.value;
                break;
            case "checkbox":
                value = (objField.checked ? "1" : "0");
                break;
            case "select-one":
            case "select":
                if (objField.selectedIndex > -1)
                    value = objField.options[objField.selectedIndex].value;
                break;
            default:
                break;
        }
    }
    return value;
}

function addHandler(element, handler, code) 
{
    if (isString(element))
        element = document.getElementById(element);
    if (element) 
    {
        if (element.getAttribute(handler) == null)
            eval("element." + handler + " = function() { " + code + " };");
        else
            eval("element." + handler + " = function() { " + code + "; " + element.getAttribute(handler) + "};");
    }
}

function findOptionIndex(selectObj, value) 
{
    var s = -1;
    for (var i = 0; i < selectObj.options.length && s == -1; i++) 
    {
        if (selectObj.options[i].value == value)
            s = i;
    }
    return s;
}

function setEventHandler(element, eventName, handler) 
{
    if (element != null) 
    {
        if (element.addEventListener)
            element.addEventListener(eventName, handler, false);
        else if (element.attachEvent)
            element.attachEvent("on" + eventName, handler);
        else
            element[eventName] = handler;
    }
}

// Page specific handlers

function setSearchField(field, checkText, newText) 
{
    if (field && field.value == checkText)
        field.value = newText;
}

function submitSearchOnEnter(targetId, e)
{
    if (typeof (__doPostBack) == "function")
    {
        if (e == null)
            e = event;
        if (e && ((e.keyCode && e.keyCode == 13) || (e.which && e.which == 13)))
            return __doPostBack(targetId);
    }
    return true;
}

function checkVoteChoice(rbListID) 
{
    var selected = false;
    var n = 0;
    var elem = document.getElementById(rbListID + "_0");
    while (!selected && elem != null) 
    {
        n++; 
        selected = elem.checked;
        elem = document.getElementById(rbListID + "_" + n.toString());
    }
    if (!selected)
        alert("You must select a voting choice before submitting.");
    return selected;
}

function showFramePopup(show, divID, iframeID, frameUrl)
{
    var div = document.getElementById(divID);
    var frm = document.getElementById(iframeID);
    if (div)
    {
        div.style.display = (show ? "block" : "none");
        if (show && frm && frm.src && frameUrl)
            frm.src = frameUrl;
    }
}
