/**
 * Functions to show/hide html elements
 *
 * 
 * 
 * 
 * 
 * @copyright   Silicon Mechanics
 * @license     LGPL
 *
 * @package     SiMech
 * @subpackage  Javascript
 *
/**/

// Toggle the visibility of an element
// This is used in FOUR places, mostly in SANform land.
    var toggle_vis_cache = new Array();
    function toggle_vis(field, vis) {
        var e = get_element(field);
    // Get the display type
        var display = get_display(field);
    // Show
        if (display == 'none') {
            e.style.display = toggle_vis_cache[field] ? toggle_vis_cache[field] : (vis ? vis : default_display(field));
            e.style.visibility  = 'visible';
        }
    // Hide
        else {
            toggle_vis_cache[field] = vis ? vis : display;
            e.style.display         = 'none';
            e.style.visibility      = 'hidden';
        }
    }

// Set the visibility of an element
// This is used in ONE place.
    function set_vis(field, vis) {
        var e = get_element(field);
    // Get the display type
        var display = get_display(field);
        if(vis == true && display == 'none') {
            e.style.display         = 'block';
            e.style.visibility      = 'visible';
        }
        if(vis == false && display == 'block') {
            e.style.display         = 'none';
            e.style.visibility      = 'hidden';
        }
    }

// Make a reasonable attempt to determine whether or not a particular field is visible
// Returns the field's "display" parameter
// This is used only in the SANform code.  Twice.  GARGH.
    function get_display(field) {
        var e = get_element(field);
    // Display assigned directly to the element
        if (e.style.display)
            return e.style.display;
    // Unknown display type -- Make a reasonable effort to look it up in the stylesheet rules
        var classes = e.className.split(/\s+/);
        var found   = false;
        if (document.styleSheets){
            for (var i=0;i<document.styleSheets.length;i++) {
                var sheet = document.styleSheets[i];
                var rules = (typeof sheet.cssRules != 'undefined') ? sheet.cssRules
                                : ((typeof sheet.rules != 'undefined') ? sheet.rules : null);
                if (rules) {
                    for (var j=0;j<rules.length;j++) {
                        var rule = rules[j];
                    // No display rule given -- skip ahead early
                        if (!rule || !rule.style || !rule.style.display)
                            continue;
                    // Grab the selectors and scan through them for id or rudimentary class name matches
                        var selectors = rule.selectorText.split(/\s*,\s*/);
                        for (var k=0;k<selectors.length;k++) {
                            var str = selectors[k];
                        // See if this is a matching id-based class
                            var match = new RegExp('^#'+field+'$');
                            if (str.match(match)) {
                                return rule.style.display;
                            }
                        // Nope -- scan through this field's classnames for a match
                            else {
                                for (var l=0;l<classes.length;l++) {
                                    match = new RegExp('^\.'+classes[l]+'$');
                                    if (str.match(match)) {
                                        return rule.style.display;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    // Return the default
        return default_display(field);
    }

// Return the default display type of a particular field (as we assume things should be displayed)
// This is only used from get_display, thus only used by the SANform code.
    function default_display(field) {
        var e = get_element(field)
    // Return special display types for certain tags
        var tag = e.tagName.toLowerCase();
        switch (tag) {
            case 'tr':
                if (browser.is_gecko || browser.is_khtml || browser.is_opera)
                    return 'table-row';
                return 'block';
            case 'td':
                if (browser.is_gecko || browser.is_khtml || browser.is_opera)
                    return 'table-cell';
                return 'block';
            case 'a', 'span':
                return 'inline';
            default:
                return 'block';
        }
    }


// Update the text for a span element with the text from a form element id of id
// This is used in FOUR places
    function update_text(id) {
        var text = get_element(id + '_text');
        if (!text)
            return;
        var x    = get_element(id);
        if(x.options && typeof(x.selectedIndex) != "undefined")
            text.innerHTML = x.options[x.selectedIndex].text;
        else
            text.innerHTML = value(id);
    // Cleanup
        if (text.innerHTML.length)
            text.innerHTML = text.innerHTML.replace(/\n/g, '<br />');
        else
            text.innerHTML = '&nbsp;';
    }

// Update span elements with a yes or a no depending on the value of a checkbox.
// This is used in ONE place.  Sigh.
    function update_yes_no(id) {
        var text = get_element(id + '_text');
        var x    = get_element(id);
        if(x.checked) {
            text.innerHTML = 'Yes';}
        else {
            text.innerHTML = 'No'; }
    }

