function SelectForm(name,bool)
{
    var object = document.getElementById(name);

    for (var i=0;i<object.options.length;i++) object.options[i].selected = bool;

    return true;
}

function CalculateCoordinate(object)
{
    var dimensions = new Array(0,0);

    object.offsetParent && (dimensions = CalculateCoordinate(object.offsetParent));

    dimensions[0] += object.offsetLeft;
    dimensions[1] += object.offsetTop;

    alert(object.tagName + " " + dimensions.join(", "));

    return dimensions;
}

function openWindow(link,name,features,position,width,height,views,extra)
{
    width       || (width = screen.width);
    height  || (height = screen.height);

    var top = (position == "center" ? (screen.height - height)/2 : (position.match(/^top/) ? 0 : screen.height - height));
    var left = (position == "center" ? (screen.width - width)/2 : (position.match(/left$/) ? 0 : screen.width - width));

    features += (features ? "," : "") + "top=" + top + ",left=" + left + ",width=" + width + ",height=" + height;

    var myWindow = window.open(link,name,features); eval('myWindow && myWindow.' + (extra & 1 ? 'blur' : 'focus') + '();');

    link && SetCookie("cms[" + name + "][views]",++views,(extra & 2 ? -1 : 365*24*3600));

    return myWindow;
}

function PopUp(title,image,width,height)
{
    myWindow = openWindow('',title.replace(/\s+/,''),'','topleft',width,height);

    myWindow.document.write("<HTML>\n<HEAD>\n<TITLE>" + title + "</TITLE>\n</HEAD>\n<BODY>\n<CENTER>\n<A HREF=\"javascript: window.close();\">\n<IMG SRC='" + image + "' BORDER=0>\n</A></CENTER>\n</BODY>\n</HTML>\n");
}

function SetCookie(name,value,expires)
{
    expires += (expires > 0 ? (new Date()).getTime()/1000 : 0);

    document.cookie = name + "=" + value + (expires > 0 ? ";expires=" + expires : "");
}

function ShowProps(object)
{
    var property, content = "";

    for (property in object)
    {
        content += "name: " + property + "; value: " + object[property] + "\n";
    }

    alert (content);
}


function sprintf()
{
    if (!arguments || arguments.length < 1 || !RegExp)
    {
        return;
    }
    var str = arguments[0];
    var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
    var a = b = [], numSubstitutions = 0, numMatches = 0;
    while (a = re.exec(str))
    {
        var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
        var pPrecision = a[5], pType = a[6], rightPart = a[7];

//      alert(a + '\n' + a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

        numMatches++;
        if (pType == '%')
        {
            subst = '%';
        }
        else
        {
            numSubstitutions++;
            if (numSubstitutions >= arguments.length)
            {
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
            }
            var param = arguments[numSubstitutions];
            var pad = '';
                if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
                else if (pPad) pad = pPad;
            var justifyRight = true;
                if (pJustify && pJustify === "-") justifyRight = false;
            var minLength = -1;
                if (pMinLength) minLength = parseInt(pMinLength);
            var precision = -1;
                if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
            var subst = param;
                if (pType == 'b') subst = parseInt(param).toString(2);
                else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
                else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
                else if (pType == 'u') subst = Math.abs(param);
                else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
                else if (pType == 'o') subst = parseInt(param).toString(8);
                else if (pType == 's') subst = param;
                else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
                else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
        }
        str = leftpart + subst + rightPart;
    }
    return str;
}

function ExecuteFlash (source)
{
    document.writeln (source);
}

WindowComplete = false;

function WindowCompleteCheck ()
{
    WindowComplete = true;
}

window.onload = WindowCompleteCheck;

function DisableField(field, object)
{
    var mode        = !object.checked;
    var container   = document.getElementById("visibility[" + field + "]");

    object.offsetParent.className = (mode && object.value == 2 ? "inherited" : "");

    DisableFieldRecursive(container,mode);
}

function DisableFieldRecursive(object,mode,depth)
{
    var children = object.childNodes; depth || (depth = 0);

    for (var i=0;i<children.length;i++)
    {
        DisableFieldRecursive(children[i],mode,depth + 1);

        if (children[i].tagName && children[i].tagName.match(/^(SELECT|TEXTAREA|INPUT)$/i)) {
            if (children[i].tagName == 'TEXTAREA' && children[i].className.match(/codepress/i)) {
                // Check if the window is initialized
                if (children[i].id.match(/_cp$/)) {
                    // Get the new CodePress object
                    var cpId = children[i].id.replace(/_cp$/, '');
                    CodePressEditors[cpId].toggleReadOnly();
                } else {
                    // This is an uninitialzed CodePress editor window, add the readonly class
                    if (mode) {
                        children[i].className += ' readonly-on';
                    } else {
                        children[i].className = children[i].className.replace(/ readonly-on/, '');
                    }
                }
            } else {
                children[i].disabled = mode;
            }
        }
    }
}