//
// common.js
// - contains all common javascript functions
//
// last updated: 01/23/2007
// modified by : Slava Gurgov
//

// trim() removes all leading and trailing whitespace
// in a specified string "str"
function trim(str)
{
  if (str == null || str == "") return "";
  return str.replace(/^\s*/,"").replace(/\s*$/,"");
}

// addLoadEvent() takes a function and adds it
// to the onLoad event of the page
//
// function can be called in following ways:
// 1) addLoadEvent(
//      function() {
//	        // add code here
//	    }
//   );
//
// - or -
//
// 2) addLoadEvent(functionName);
//
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload)
				oldonload();
			func();
		}
	}
}

// sortMultiDimArray() sorts a multi-dimensional array
// by the "column" specified
function sortMultiDimArray(multiDimArray, column)
{
	for (var i=0; i < multiDimArray.length-1; i++)
	{
		for (var j=0; j < multiDimArray.length-1-i; j++)
		{
			if (multiDimArray[j+1][column].toLowerCase() < multiDimArray[j][column].toLowerCase())
			{
				var tmp = multiDimArray[j];
				multiDimArray[j] = multiDimArray[j+1];
				multiDimArray[j+1] = tmp;
			}
		}
	}
	return multiDimArray;
}

// window_open() extends the function of the JS window.open()
// but adds the feature of letting user know if the popup was blocked
function window_open(page, name, properties)
{
  var win = window.open(page, name, properties);
  if (!win) {
    alert("The requested page cannot open in a new window. Please check your popup settings.");
  }
  return win;
}

// makeSure() checks if user selected an item from a select object
// if no item(s) were selected user is asked to select to continue
// otherwise a confirm box is displayed to confirm before proceeding
function makeSure(list)
{
  if (list.selectedIndex < 0) {
    alert("Please select an item from the list before submitting");
    return false;
  } else {
    return confirm("Are you sure you want to delete the selected item?");
  }
}

// selectAll() selects or deselects all items in a list box (select object)
function selectAll(myList, onOff)
{
  for (var i=0; i < myList.length; i++) {
    myList.options[i].selected = onOff;
  }
}

// checkAll() checks or unchecks all checkboxes
function checkAll(myList, onOff)
{
  for (var i=0; i < myList.length; i++) {
    myList[i].checked = onOff;
  }
}

// show() shows the specified layer
function show(layerName)
{
  document.getElementById(layerName).style.display = "block";
}
// hide() hides the specified layer
function hide(layerName)
{
	document.getElementById(layerName).style.display = "none";
}

function validationResult(errors)
{
    if (errors.length > 0)
    {
        var msg = "Please fix the following errors:\n";
        for (var i=0; i < errors.length; i++)
            msg += "    - "+errors[i]+"\n";
        alert(msg);
        return false;
    }
    return true;
}

// isChecked() checks if provided list has any/all items checked
// returns cuont of checked items
// list   - radio, checkbox
// option - any, all
function isChecked(list, option)
{
	var items = 0;
	if (!list.length) // take care of case when there is only one checkbox
		return (list.checked ? 1 : 0);
    
    for (var i=0; i < list.length; i++)
		if (list[i].checked)
			items++;

	if (option == "all")
		return (items == list.length);

	return items;
}


// isSelected() checks if provided list has any/all items selected
// returns count of selected items
// list   - listbox
// option - any, all
function isSelected(list, option)
{
	var items = 0;

	for (var i=0; i < list.options.length; i++)
		if (list.options[i].selected)
			items++;

	if (option == "all")
		return (items == list.length);

	return items;
}
