﻿function changeCss(theClass, element, value)
{
    var cssRules;

    var added = false;
    for (var S = 0; S < document.styleSheets.length; S++)
    {

        if (document.styleSheets[S]['rules'])
        {
            cssRules = 'rules';
        } else if (document.styleSheets[S]['cssRules'])
        {
            cssRules = 'cssRules';
        } else
        {
            //no rules found... browser unknown
        }

        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++)
        {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass)
            {
                if (document.styleSheets[S][cssRules][R].style[element])
                {
                    document.styleSheets[S][cssRules][R].style[element] = value;
                    added = true;
                    break;
                }
            }
        }
        if (!added)
        {
            try
            {
                document.styleSheets[S].insertRule(theClass + ' { ' + element + ': ' + value + '; }', document.styleSheets[S][cssRules].length);

            } catch (err)
            {
                try { document.styleSheets[S].addRule(theClass, element + ': ' + value + ';'); } catch (err) { }

            }

            //if(document.styleSheets[S].insertRule){
            //document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
            //} else if (document.styleSheets[S].addRule) {
            //document.styleSheets[S].addRule(theClass,element+': '+value+';');
            //}
        }
    }
}


function childNodeIndex()
{
    var Index;
//    if (navigator.appName.toLowerCase() == 'netscape')
//        Index = 1;
//    else
//        Index = 0;

    if (navigator.userAgent.toLowerCase().indexOf('mozilla/5.0', 0) == 0)
        Index = 1;
    else
        Index = 0;

    return Index;
}

function descer()
{
    var descer = document.getElementById('descer');

    setScrollPosition(0, getScrollSize()['height']);
    descer.style.display = 'none';
}

function getInnerSize()
{
    var myWidth = 0, myHeight = 0;

    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    return { "width": myWidth, "height": myHeight };
}

function getScrollPosition()
{
    var scrOfX = 0, scrOfY = 0;

    if (typeof (window.pageYOffset) == 'number')
    {
        //Netscape compliant 
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant 
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode 
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    return { "top": scrOfY, "left": scrOfX };
}

function getScrollSize()
{
    var scrW = 0, scrH = 0;

    scrW = document.body.scrollWidth - getInnerSize()["width"];
    scrH = document.documentElement.scrollHeight - getInnerSize()["height"];

    return { "height": scrH, "width": scrW };
}

function IsNumeric(sText)
{
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++)
    {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1)
        {
            IsNumber = false;
        }
    }

    return IsNumber;
}

function posicionaBotoes()
{
    try
    {
        var scrollY = getScrollPosition()['top'];
        var scrollH = getScrollSize()['height'];
        var windowH = getInnerSize()['height'];

        var subir = document.getElementById('subir');
        var descer = document.getElementById('descer');

        if (scrollY > 20)
        {
            subir.style.display = 'block';
            subir.style.top = scrollY + 'px';
        }
        else
        {
            subir.style.display = 'none';
        }

        if (scrollY < (scrollH - 50))
        {
            descer.style.display = 'block';
            descer.style.top = (scrollY + windowH - 20) + 'px';
        }
        else
        {
            descer.style.display = 'none';
        }

        //document.title = scrollY + ' / ' + scrollH;
    }
    catch (err)
    {

    }
}

function SetUniqueRadioButton(nameregex, current)
{
    re = new RegExp(nameregex);

    for (i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio')
        {
            if (re.test(elm.name))
            {
                elm.checked = false;
            }
        }
    }

    current.checked = true;
}

function setScrollPosition(scrOfX, scrOfY)
{
    if (window.scrollTo)
    {
        window.scrollTo(scrOfX, scrOfY);
    }
}

function subir()
{
    var subir = document.getElementById('subir');

    setScrollPosition(0, 0);
    subir.style.display = 'none';
}



function trim(stringToTrim)
{
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function ltrim(stringToTrim)
{
    return stringToTrim.replace(/^\s+/, "");
}

function rtrim(stringToTrim)
{
    return stringToTrim.replace(/\s+$/, "");
}


function validatorFckEditor(src, args)
{
    alert(src);
}
