/**
 * utils.inc.js
 *
 * Created on 06/11/2006
 *
 * Copyright (c) 2006 by CRP - Gabriel Lippmann
 *
 * Contains various Javascript utility functions.
 *
 * @author Pierre Post
 * @since 1.0
 */

/* --- Modifications - Start
   --- Modifications - End */

/**
 * Displays a dialog box that asks the user to 
 * confirm the loading of a specific URL.
 * @param message The message to display
 * @param url The URL to load
 */
function confirmLink(message, url) {
    if (confirm(message)) {
        window.location = url;
    }
}

/**
 * Toggles the visibility of the popup div with the
 * specified identifier on or off.
 * @param id The identifier of the popup div
 */
function togglePopup(id) {
    // get popup element
    var popup_div = document.getElementById(id);
    if (popup_div != null) {
        // check if popup is currently visible or not
        if (popup_div.style.display == "none") {
            popup_div.style.display = "inline";
        } else {
            popup_div.style.display = "none";
        }
    } else {
        alert("Popup " + id + " could not be found");
    }
}

/**
 * Empty Javascript method.
 */
function noop() {
}


/**
 * Outputs a link to an email address without being visible in the source.
 * @param name    The email address name
 * @param domain  The email address domain
 * @param display The text to display for the email link (optional)
 */
function writeMail(name, domain, display) {
    displayed = (typeof(display)=="undefined") ? name + "@" + domain : display;
    document.write('<a href="mailto:' + name + '@' + domain + '">' + displayed + '</a>');
}

/**
 * Sets the checked state of all checkboxes whose names start with the given
 * prefix.
 * @param form_id    The identifier of the form
 * @param is_checked TRUE if all checkboxes should be checked, FALSE otherwise
 * @param prefix     The name prefix of the checkboxes
 */
function setCheckBoxesAll(form_id, is_checked, prefix) {
    var form = document.getElementById(form_id);
    if (form != null) {
    	// loop through form elements
    	for (var i = 0; i < form.length; i++) {
    	    var e = form.elements[i];
    	    // search checkboxes
    	    if (e.type == "checkbox" && e.name.substring(0, prefix.length) == prefix) {
    	    	e.checked = is_checked;
    	    }
    	}
    } else {
    	alert("Form " + id + " could not be found");
    }
}
