//CVS:       $Id: util.js,v 1.1 2011/01/13 22:48:45 cvsdevel Exp $
//Title:     util.js
//Version:   1.00
//Copyright: Copyright (c) 2010
//Author:    JS
//Company:   Rhino Internet

/**
 * Contains utility functions.
 *
 * <p>
 * <b>Changelog:</b><pre>
 *  1.00  JS    2011/01/13  created.
 * </pre>
 *
 * @author  JS
 * @version 1.00
 */

/**
 * Prompts the user to confirm a delete operation.
 * 
 * @param name the name of the data to delete, to be displayed to the user.
 * @param plural the plural form of the name.  Optional.  If not provided, then
 *               the name will be used, with an 's' appended. 
 * @param elementName the name of the checkbox list indicating which records
 *                    should be deleted.  Optional.  If not provided, then the
 *                    string 'delete[]' will be used.
 * @return true, if the user confirms the delete; false, if the user cancels the
 *         delete, or if no data objects were selected for deletion.
 */
function promptForDelete(name, plural, elementName)
{
   if (plural == undefined) {
      plural = name + 's';
   }
   if (elementName == undefined) {
      elementName = 'delete[]';
   }
   
   var cbList = document.getElementsByName(elementName);
   var checked = [];
   var msg = '';
   var result = false;
   
   for (var i = 0; i < cbList.length; i++) {
      if (cbList[i].checked) {
         checked.push(cbList[i].value);
      }
   }
   
   if (checked.length == 0) {
      msg = 'You haven\'t selected any '+plural+' to delete.';
      alert(msg);
      result = false;
   } else if (checked.length == 1) {
      msg = 'Are you sure you would like to delete this '+name+'?';
      result = confirm(msg);
   } else {
      msg = 'Are you sure you would like to delete these '+plural+'?';
      result = confirm(msg);
   }
   return result;
}//promptForDelete


/**
 * Combines two functions in a single function delegate.
 * 
 * @param function1 the first function to add to the delegate.
 * @param function2 the second function to add to the delegate.
 * @return the function delegate.  
 **/  
function makeDoubleDelegate(function1, function2)
{
   return function() {
      if (function1) {
         function1();
      }
      if (function2) {
         function2();
      }
   };
}


