/***************************************************************************
 * 
 * JavaScript utility functions.
 * 
 * Functions for showing and hiding blocks can be found at
 * http://www.xs4all.nl/~ppk/js/blockinvi.html but note these
 * do not operate correctly on Opera. The code is free to use
 * - see http://www.xs4all.nl/~ppk/js/
 *
 ****************************************************************************/
 
/**
 * Expand/collapse utility. Takes the id of some block element such as a div. 
 * This is toggled between expanded and collapsed with the "display" CSS
 * property. The function expects there to be an img element which has the same
 * id with "_img" appended. It toggles the image between the open and closed
 * images.
 *
 * @param baseId     the id of the element to expand
 * @param openImage  the URL of the image which means "expand this"
 * @param closeImage the URL of the image which means "contract this"
 */
function expand(baseId, openImage, closeImage) {
    
    // Get the expando element.
    var expandoElement = new getElement(baseId);
    var imgElement     = new getElement(baseId + "_img");
    
    // Toggle the display state of the element.
    var display = expandoElement.style.display == "none" ? "block" : "none";
    expandoElement.style.display = display;

    // Toggle the image.
    if (display == "none") {
        imgElement.obj.src = openImage;    
    }
    else {
        imgElement.obj.src = closeImage;
    }    
}

/**
 * Gets the named element in a browser-neutral way. The returned object has
 * two properties, "obj" and "style". You can use it like this:
 *
 * var myImage = new getElement("foo");
 * myImage.obj.src = "...";
 * myImage.style.display = "none";
 *
 * @param name the id of the target element
 */
function getElement(name) {
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    }
    else if (document.all) {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    }
    else if (document.layers) {
        this.obj = document.layers[name];
        this.style = document.layers[name];
    }
}

/**
 * Toggles the state of all the checkboxes with the given name, in the given form.
 *
 * @param form the form element
 * @param name the name of the checkbox to toggle
 */
function toggleAll(form, name) {
    var first  = true;
    
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "checkbox" && form.elements[i].name.indexOf(name) == 0) {
            if (first) {
                var select = !form.elements[i].checked;
                first = false;
            }
            
            form[i].checked = select;
        }
    }
}

/**
 * Selects or deselects all the checkboxes with the given name, in the
 * given form.
 *
 * @param form   the form element
 * @param name   the name of the checkbox to toggle
 * @param select true if the controls should be checked, false to uncheck
 */
function selectAll(form, name, select) {
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "checkbox" && form.elements[i].name.indexOf(name) == 0) {
            form[i].checked = select;
        }
    }
}

// This code is based on ideas from:
// http://www.alistapart.com/articles/pngopacity/
// http://www.howtocreate.co.uk/alpha.html
// http://webfx.eae.net/dhtml/pngbehavior/pngbehavior.html

/**
 * Writes a PNG icon - supporting Alpha transparency (even on IE5.5+).
 *
 * @param strPath the image path
 * @param strAlt the alternative text
 */
function pngIcon(strPath, strAlt) {   
    if (pngAlpha) {
        document.write('<span style="position:relative;left:0px;top:0px;width:16px;height:16px;"><span style="position:absolute;left:0px;top:0px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'\');"><img style="filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="'+strPath+'" width="16" height="16" alt="'+strAlt+'"></span></span>');
    }
    else {
        document.write('<img src="'+strPath+'" width="16" height="16" alt="'+strAlt+'"/>');
    }
}

/**
 * Writes a PNG icon - supporting Alpha transparency (even on IE5.5+).
 *
 * @param strPath the image path
 * @param strAlt the alternative text
 * @param onClick the on click function
 */
function pngIconOnClick(strPath, strAlt, onClick) {   
    if (pngAlpha) {
        document.write('<span style="position:relative;left:0px;top:0px;width:16px;height:16px;" onclick="'+onClick+'"><span style="position:absolute;left:0px;top:0px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'\');"><img style="filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="'+strPath+'" width="16" height="16" alt="'+strAlt+'"></span></span>');
    }
    else {
        document.write('<img src="'+strPath+'" width="16" height="16" alt="'+strAlt+'" onclick="'+onClick+'"/>');
    }
}

/**
 * Writes a PNG link - supporting Alpha transparency (even on IE5.5+).
 *
 * @param strPath the image path
 * @param imageWidth the image width
 * @param imageHeight the image height
 * @param strAlt the alternative text
 */
function png(strPath, intWidth, intHeight, strAlt) {   
    if (pngAlpha) {
        document.write('<span style="position:relative;left:0px;top:0px;width:'+intWidth+'px;height:'+intHeight+'px;"><span style="position:absolute;left:0px;top:0px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'\');"><img style="filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="'+strPath+'" width="'+intWidth+'" height="'+intHeight+'" alt="'+strAlt+'"></span></span>');
    }
    else {
        document.write('<img src="'+strPath+'" width="'+intWidth+'" height="'+intHeight+'" alt="'+strAlt+'"/>');
    }
}

/**
 * Writes a PNG link - supporting Alpha transparency (even on IE5.5+).
 *
 * @param strID the ID for the image element
 * @param strPath the image path
 * @param imageWidth the image width
 * @param imageHeight the image height
 * @param strAlt the alternative text
 */
function pngID(strID, strPath, intWidth, intHeight, strAlt) {   
    if (pngAlpha) {
        document.write('<span style="position:relative;left:0px;top:0px;width:'+intWidth+'px;height:'+intHeight+'px;"><span style="position:absolute;left:0px;top:0px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'\');"><img style="filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" src="'+strPath+'" width="'+intWidth+'" height="'+intHeight+'" alt="'+strAlt+'" id="'+strId+'"></span></span>');
    }
    else {
        document.write('<img src="'+strPath+'" width="'+intWidth+'" height="'+intHeight+'" alt="'+strAlt+'" id="'+strId+'"/>');
    }
}

// If IE5.5+ on Win32 then display PNGs with AlphaImageLoader
if ((browser.isIE55 || browser.isIE6up) && browser.isWin32) {
    var pngAlpha = true;
}

