﻿if (!Common)
{
    var Common = {};
}

Common.insertNewWindowLinks = function(strAttribute, strAttributeValue)
{
    if (!strAttribute)
    {
        var strAttribute = "rel";
    }

    if (!strAttributeValue)
    {
        var strAttributeValue = "external";
    }

    var arrLinks = document.getElementsByTagName("a");

    for (var nIndex = 0; nIndex < arrLinks.length; ++nIndex)
    {
        if (arrLinks[nIndex].getAttribute("href") && arrLinks[nIndex].getAttribute(strAttribute) == strAttributeValue)
        {
            arrLinks[nIndex].target = "_blank";
        }
    }
}


Common.setFormFieldValue = function(strElementID, strValue)
{
    $get(strElementID).value = strValue;
}

Common.getIntegerFromStyleValue = function(strValue)
{
    var nPXIndex = strValue.indexOf("px");
    if (nPXIndex != -1)
    {
        strValue = strValue.substring(0, nPXIndex);
    }

    var nValue = parseInt(strValue);

    if (!nValue)
    {
        return 0;
    }
    else
    {
        return nValue;
    }
}

Common.getHexFromStyleValue = function(strValue)
{
    var nHashIndex = strValue.indexOf("#");
    if (nHashIndex != -1)
    {
        strValue = strValue.substring(nHashIndex + 1);
    }

    var nRGBIndex = strValue.toLowerCase().indexOf("rgb");
    if (nRGBIndex != -1)
    {
        var strRGBValue = strValue.substring(strValue.indexOf("(") + 1, strValue.indexOf(")"));
        var arrRGBValue = strRGBValue.split(", ");

        strValue = Common.getHexFromDecimal(arrRGBValue[0], 2)
            + Common.getHexFromDecimal(arrRGBValue[1], 2)
            + Common.getHexFromDecimal(arrRGBValue[2], 2);
    }

    return strValue;
}

Common.getHexFromDecimal = function(nValue, nNumberOfHexDigits)
{
    if (!nNumberOfHexDigits)
    {
        nNumberOfHexDigits = nValue.length;
    }

    var strValue = parseInt(nValue).toString(16);

    while (strValue.length < nNumberOfHexDigits)
    {
        strValue = "0" + strValue;
    }

    return strValue;
}

Common.getStyleAttribute = function(elmElement, strCSSStyleName)
{
    var strValue = "";

    if (elmElement.currentStyle)
    {
        var strIEStyleName = "";
        var bCapitalise = false;

        var strFinalWord = "";
        var strFirstWords = "";

        for (var nIndex = 0; nIndex < strCSSStyleName.length; ++nIndex)
        {
            //do this with a split
            if (strCSSStyleName.charAt(nIndex) == "-")
            {
                bCapitalise = true;
            }
            else
            {
                if (bCapitalise)
                {
                    strFinalWord = strCSSStyleName.substring(nIndex);
                    strFirstWords = strIEStyleName;

                    strIEStyleName += strCSSStyleName.charAt(nIndex).toUpperCase();

                    bCapitalise = false;
                }
                else
                {
                    strIEStyleName += strCSSStyleName.charAt(nIndex);
                }
            }
        }

        strValue = elmElement.currentStyle[strIEStyleName];
    }
    else if (window.getComputedStyle)
    {
        strValue = document.defaultView.getComputedStyle(elmElement, null).getPropertyValue(strCSSStyleName);
    }

    return strValue;
}

Common.clearFileUploadBox = function(elmFileInput)
{
    var elmFileInputCleared;

    if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
    {
        elmFileInputCleared = elmFileInput.cloneNode(false);
    }
    else
    {
        elmFileInputCleared = document.createElement("input");

        for (var nIndex = 0; nIndex < elmFileInput.attributes.length; nIndex++)
        {
            if (elmFileInput.attributes[nIndex].nodeName.toLowerCase() != "value")
            {
                elmFileInputCleared.setAttribute(elmFileInput.attributes[nIndex].nodeName, elmFileInput.attributes[nIndex].nodeValue);
            }
        }
    }

    elmFileInput.parentNode.insertBefore(elmFileInputCleared, elmFileInput);

    elmFileInput.parentNode.removeChild(elmFileInput);
}

Common.getParentElementWithByIDEnd = function(elmCurrent, strIDPostfix)
{
    for (; ; )
    {
        elmCurrent = elmCurrent.parentNode;

        var strID = elmCurrent.id;

        if (strID != undefined && strID != "undefined")
        {
            if (strID.length >= strIDPostfix.length)
            {
                if (strID.substring(strID.length - strIDPostfix.length) == strIDPostfix)
                {
                    return elmCurrent;
                }
            }
        }
    }

    return null;
}

Common.decodeURI = function(strURI)
{
    var regexPlusSymbolEncodedSpace = new RegExp("\\+", "gi")
    strURI = strURI.replace(regexPlusSymbolEncodedSpace, "%20");

    strURI = decodeURI(strURI);

    var regexPlusSymbolEncodedSpace = new RegExp("%2f", "ig")
    strURI = strURI.replace(regexPlusSymbolEncodedSpace, "/");

    var regexPlusSymbolEncodedSpace = new RegExp("%3d", "ig")
    strURI = strURI.replace(regexPlusSymbolEncodedSpace, "=");

    var regexPlusSymbolEncodedSpace = new RegExp("%3a", "ig")
    strURI = strURI.replace(regexPlusSymbolEncodedSpace, ":");

    var regexPlusSymbolEncodedSpace = new RegExp("%3b", "ig")
    strURI = strURI.replace(regexPlusSymbolEncodedSpace, ";");

    return strURI;
}

Common.hasVisibleHTMLContent = function(strValue)
{
    strValue = strValue.toLowerCase();

    if (strValue.indexOf("<img") != -1)
    {
        return true;
    }

    var regexHTMLTags = new RegExp("(<([^>]+)>)|&nbsp;| |\n|\r|\t", "ig");
    strValue = strValue.replace(regexHTMLTags, "");
    strValue = strValue.trim();

    return strValue.length > 0;
}

Common.getElementsByClassName = function(strClassName, elmParentNode)
{
    if (!elmParentNode)
    {
        elmParentNode = document.getElementsByTagName("body")[0];
    }

    var arrElements = [];
    var regexClassName = new RegExp("\\b" + strClassName + "\\b");
    var arrAllElements = elmParentNode.getElementsByTagName("*");

    for (var nIndex = 0; nIndex < arrAllElements.length; ++nIndex)
    {
        if (regexClassName.test(arrAllElements[nIndex].className))
        {
            arrElements.push(arrAllElements[nIndex]);
        }
    }

    return arrElements;
}

Common.encodeLineBreaksToHTML = function(strValue)
{
    var regexNewlines = new RegExp("\n", "gi");
    strValue = strValue.replace(regexNewlines, "<br />");

    return strValue;
}

String.prototype.trim = function()
{
    var regexTrim = new RegExp("^\s+|\s+$", "gi");
    strValue = strValue.replace(regexTrim, "");
}

if (!Common.Form)
{
    Common.Form = {};
}

Common.Form.interceptEnterKey = function(elmInput, elmAction)
{
    elmInput.onkeyup = function(e)
    {
        var nCharacterCode;

        if (window.event)
        {
            nCharacterCode = window.event.keyCode
        }
        else
        {
            nCharacterCode = e.which;
        }

        if (nCharacterCode == 13)
        {
            var bSubmitForm = false;

            if (elmAction.click)
            {
                elmAction.click();
            }

            if (elmAction.type)
            {
                if (elmAction.type.toLowerCase() == "submit" || elmAction.type.toLowerCase() == "image")
                {
                    if (document.forms[0].onsubmit)
                    {
                        document.forms[0].onsubmit();
                    }

                    bSubmitForm = true;
                }
            }

            if (bSubmitForm)
            {
                document.forms[0].submit();
            }
            else
            {
                if (window.event)
                {
                    window.event.returnValue = false;
                }
                else
                {
                    e.preventDefault();
                }
            }
        }
    };
}

Common.invokeAction = function(elmElement)
{
    if (elmElement.onclick)
    {
        eval(elmElement.onclick);
    }

    if (elmElement.href)
    {
        var strHRef = elmElement.href;

        if (strHRef.trim().toLowerCase().indexOf("javascript:") == 0)
        {
            strHRef = strHRef.trim().substring("javascript:".length);

            strHRef = Common.decodeURI(strHRef);

            eval(strHRef);
        }
        else
        {
            if (elmElement.target)
            {
                switch (elmElement.target.toLowerCase())
                {
                    case "_top":
                        window.top = strHRef;
                        break;
                    case "_parent":
                        window.parent = strHRef;
                        break;
                    case "_self":
                        window.location = strHRef;
                        break;
                    case "_blank":
                        window.open(strHRef);
                        break;
                    default:
                        window.frames[elmElement.target].location = strHRef;
                        break;
                }
            }
            else
            {
                window.location = strHRef;
            }
        }
    }

    if (elmElement.type)
    {
        var strType = elmElement.type.toLowerCase();

        if (strType == "submit" || strType == "image")
        {
            var bSubmit = true;

            if (document.forms[0].onsubmit)
            {
                if (document.forms[0].onsubmit.trim().indexOf("return") == 0)
                {
                    eval("bSubmit = " + document.forms[0].onsubmit.trim().substring("return".length));
                }
                else
                {
                    eval(document.forms[0].onsubmit);
                }
            }

            if (bSubmit)
            {
                document.forms[0].submit();
            }
        }
    }
}

