/***                                                                        ***\
    utils.js                                  Last Updated: 2005.04.01 (kormoc)
    a random assortment of javascript utility routines
\***                                                                        ***/

    function get_element(id) {
        if (typeof id != 'string') return id;
        if (document.getElementById)
            return document.getElementById(id);
        if (document.all)
            return document.all[id];
        return null;
    }

// Pass in value to change, otherwise it returns the value of the "e" element
    function value(e, new_value) {
        if (typeof e == 'string')
            var e = get_element(e);
        if (!e) return '';
    // A <select>
        if (e.options) {
            if (new_value != null) {
            // This would scan the options and choose the one that matches
            // new_value.  Not used anywhere yet, so no need to hook it up.
            }
            return value(e.options[e.selectedIndex]);
        }
    // Just an html element?  (or in IE, an option element with no value="" specified)
        else if (e.value == null || e.value == '') {
            if (new_value != null)
                e.innerHTML = new_value;
            return e.innerHTML;
        }
    // Form field
        if (new_value != null) {
            e.value = new_value;
        }
        return e.value;
    }

// Image Preloader
    var img_on  = new Array();
    var img_off = new Array();
    function preload_image(id, on, off) {
        img_on[id]      = new Image();
        img_on[id].src  = on;
        if (off) {
            img_off[id]     = new Image();
            img_off[id].src = off;
        }
    }

// Functions to swap on/off states of images
    function on(which) {
        var img = get_element(which);
        img.src=img_on[which].src;
    }
    function off(which) {
        var img = get_element(which);
        img.src=img_off[which].src;
    }

// Window status changer
    function wstatus(str) {
        window.status = str ? str : '';
        return true;
    }

// Confirm deleting something?
    function confirm_task(what, field, value, form, type) {
        if (!field) {
            field = 'delete';
            value = true;
        }
        if (!type) {
            type = 'delete';
        }
        if (confirm("Are you sure you want to " + type + " this " + what + "?  This CANNOT be undone."))
            submit_form(field, value, form);
    }

// Submit a form
    function submit_form(newvar, val, form, confirm_str) {
    // Confirm?
        if (confirm_str && !confirm(confirm_str))
            return;
    // Find the form we want to submit
        form = get_element(form ? form : 'form');
        if (!form)
            form = document.form ? document.form : document.forms[0];
    // Create a new variable?
        if (newvar) {
            var hidden = document.createElement('input');
            hidden.type  = 'hidden';
            hidden.name  = newvar;
            hidden.value = val ? val : 1;
            form.appendChild(hidden);
        }
    // Submit
        form.submit();
    }

// Add a css class to a specified element
    function add_class(id, classname) {
        var field = get_element(id);
    // No field
        if (!field)
            return;
    // Field already has this class, don't bother to add it again
        if (field.className && (new RegExp('\\b'+classname+'\\b')).test(field.className))
            return;
    // Add the class
        if (field.className)
            field.className = field.className + ' ' + classname;
        else
            field.className = classname;
    }

// Remove a css class from a particular element
    function remove_class(id, classname) {
        var field = get_element(id);
        if (!field)
            return;
        field.className = field.className.replace(RegExp('\\b'+classname+'\\s*\\b|\\b\\s*'+classname+'\\b', 'g'), '') ;
    }

// Check/uncheck a checkbox
    function checkbox(id, check) {
        var e = get_element(id);
        if (check)
            e.checked = true;
        else if (check != null)
            e.checked = false;
        else
            e.checked = e.checked ? false : true;
    }

// Pop open a window to a new URL
    function popup_window(url, name, width, height, center) {
        var options = 'menubar=false,status=false,toolbar=false,scrollbars,resizable';
    // Center the new window?
        if (center) {
            var left    = 0;
            var top     = 0;
        // Figure out where this window should be drawn
            if (document.documentElement.clientWidth || document.documentElement.clientHeight) {
                left = parseInt((document.documentElement.clientWidth  - width)  / 2);
                top  = parseInt((document.documentElement.clientHeight - height) / 2);
            }
            else if (document.body.clientWidth || document.body.clientHeight) {
                left = parseInt((document.body.clientWidth  - width)  / 2);
                top  = parseInt((document.body.clientHeight - height) / 2);
            }
            else {
                left = parseInt((window.innerWidth  - width)  / 2);
                top  = parseInt((window.innerHeight - height) / 2);
            }
            if (window.screenLeft || window.screenTop) {
                left += parseInt(window.screenLeft);
                top  += parseInt(window.screenTop);
            }
            else if (window.screenX || window.screenY) {
                left += parseInt(window.screenX);
                top  += parseInt(window.screenY) + parseInt(window.outerHeight) - parseInt(window.innerHeight);
            }
        // Do some error checking - Safari has issues with height/width stuff and multiple monitors
            if (top > 200 && height > 0)
                options += ',top=' + top;
            if (left > 200 && width > 0)
                options += ',left=' + left;
        }
    // Make sure the window isn't too tall/wide
        if (width > screen.availWidth)
            width = screen.availWidth;
        if (height > screen.availHeight)
            height = screen.availHeight;
    // Add the height and width
        if (width > 0)
            options += ',width=' + width;
        if (height > 0)
            options += ',height=' + height;
    // Open a new window, with the new dimensions
        var win = window.open(url, name, options);
        win.focus();
    }

// Round a number to a specified number of decimal places
    function round(num, places, strip) {
    // Default to 2 decimal places, round the number
        places = (!isNaN(places) ? parseInt(places) : 2);
        num = '' + Math.round(num * Math.pow(10, places)) / Math.pow(10, places);
    // No trailing zeroes?
        if (strip || places == 0) return num;
    // Need to add trailing zeroes?
        var i = num.indexOf('.');
        if (i < 0) {
            num = num + '.';
            i = num.length - 1;
        }
        i = num.length - i - 1;
        for(var j=i;j<places;j++) {
            num = num + '0';
        }
        return num;
    }

// Resize a the specified <textarea>
    function resize_textarea(id) {
        var text = get_element(id);
        text.rows = value(id + '_rows');
        text.cols = value(id + '_cols');
    }
