// Code in this file relies on code in file "core.js"

//  Return whether a string contains any non-blank characters
function is_empty(str)
{
	var pat = new RegExp("^[ \t]*$");
	return pat.test(str);
}

//  Trim a string (delete leading and trailing spaces)
function trim(str)
{
	return str.replace(/^\s*/,"").replace(/\s*$/,"");
}

//	Add or delete the indicated class to/from all objects in the document with the indicated IDs
function add_class( cName /* , ... */ )
{
	for ( var i = 1; i < arguments.length; ++i ) {
		var item = document.getElementById(arguments[i]);
		if ( item != undefined ) Core.addClass( item, cName );
	}
}

function del_class( cName /* , ... */ )
{
	for ( var i = 1; i < arguments.length; ++i ) {
		var item = document.getElementById(arguments[i]);
		if ( item != undefined ) Core.removeClass( item, cName );
	}
}

//	Show or hide items based on their IDs
function show_item(/* eid, ... */) {
	var mustAdjust = false;
	for ( var eNum = 0; eNum < arguments.length; ++eNum ) {
		var item = document.getElementById(arguments[eNum]);
		item.style.visibility="visible";
		item.style.display = "";
		if ( item.tagName == "TR" || item.tagName == "TH" || item.tagName == "TABLE" ) {
			mustAdjust = true;
		}
	}
	if ( mustAdjust ) adjustLayout();
}
function hide_item(/* eid, ... */) {
	var mustAdjust = false;
	for ( var eNum = 0; eNum < arguments.length; ++eNum ) {
		var item = document.getElementById(arguments[eNum]);
		item.style.visibility = 'hidden';
		item.style.display = 'none';
		if (item.tagName == "TR" || item.tagName == "TH" || item.tagName == "TABLE" )  {
			mustAdjust = true;
		}
	}
	if ( mustAdjust ) adjustLayout();
}

function clear_textarea(fldId)
{
	var item;
	try {
		item = document.getElementById(fldId);
	}
	catch(c) {return;}		// If not a valid ID, do nothing
	
	var areas = item.getElementsByTagName("textarea");
	for ( var a = 0; a < areas.length; ++a ) areas[a].innerHTML = '';
}

function print_formatted_page(page) {
	var newWindow=window.open(page);
	newWindow.focus();
	var r=confirm("Print this report?");
	if (r) window.print();
}

function in_array( ary, item )	// Is item in array ary?
{
	for ( var el = 0; el < ary.length; ++el  ) if ( item == ary[el] ) return true;
	return false;
}

function changeBtnValue(buttonId, text) {
	var button = document.getElementById(buttonId);
	if (button.value) button.value = text;
}

//	Dynamically change the height of the various "wrapper" structures to match the tallest
//	content, so the footer will float correctly under the columns.

function adjustLayout()
{
  var nCol = 3;	// 3-column layout is the default
  // Get natural heights of the contents of the various wrapper structures
  var cHeight = xHeight("content");
  var lHeight = xHeight("navbar");
  var maxHeight = Math.max(lHeight, cHeight);

  if (document.getElementById("content").parentNode.id == "contentwrapper2col" ) nCol = 2;

  // Assign maximum height to all columns
  switch (nCol) {
    case 2:
      xHeight("contentwrapper2col", maxHeight); // cHeight = xHeight("contentwrapper2col");
      xHeight("navwrapper",         maxHeight); // lHeight = xHeight("navwrapper");
      break;

    case 3:
	  var rContent = document.getElementById("rightwrapper").getElementsByTagName("DIV")[0];
	  var rHeight = xHeight(rContent.id);
      maxHeight = Math.max(maxHeight, rHeight);
      xHeight("contentwrapper", maxHeight); // cHeight = xHeight("contentwrapper");
      xHeight("navwrapper",     maxHeight); // lHeight = xHeight("navwrapper");
      xHeight("rightwrapper",   maxHeight); // rHeight = xHeight("rightwrapper");
      break;
  }

  // Show the footer
  xShow("footer");
}

window.onload=function()
{
  xAddEventListener(window, "resize", adjustLayout, false);
  adjustLayout();
}