//detect browser:
var isDOM = (document.getElementById) ? true : false;
var isIE4 = (document.all && !isDOM) ? true : false;
var isNS4 = (document.layers) ? true : false;
var other = (!isDOM && !isIE4 && !isNS4 ) ? 1 : 0;
var opera = (navigator.userAgent.indexOf('Opera') != -1);

function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function()
    {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function NewWindow(mypage, myname, w, h, scroll)
{
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
  win = window.open(mypage, myname, winprops)
  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//
// Parse the query string passed with a URL.
// To get a single query string parameter, reference it like getVars[name].
//
function parseGetVars()
{
  var qString = unescape(top.location.search.substring(1));

  // I want to return null is there is no qString.
  var getVars;
  if (qString) {
    getVars = new Array();
    var pairs = qString.split(/\&/);
    for (var i = 0; i < pairs.length; i ++) {
      var nameVal = pairs[i].split(/\=/);
      getVars[nameVal[0]] = nameVal[1];
    }
  }

  return(getVars);
}

// Adds a remove function for the array object.
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

// Adds a removeItem function for the array object.
Array.prototype.removeItem = function(item) {
  // Find the item in the array.  If it doesn't exist, do nothing.
  var ind = this.indexOf(item);
  if (ind >= 0) {
    return this.remove(ind);
  } else {
    return this;
  }
};

function getElementsByClass(searchClass, node, tag)
{
  var classElements = new Array();

  if (node == null)
    node = document;
  if (tag == null)
    tag = '*';

  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  var i, j;
  for (i = 0, j = 0; i < elsLen; i++)
  {
    if (pattern.test(els[i].className))
    {
      classElements[j] = els[i];
      j ++;
    } 
 }
  return classElements;
}

// Function to expand a section to show its descriptive text.
// I need 3 controls to make this work:
//  span (*_text) - encompassing the text to be displayed or hidden
//  img (*_image) - the icon used to show expand/collapse
//  p (*_anchor) - a header text block where we need to set focus
//
// '*' is the progName passed to the function.
//
// If the caller doesn't pass an icon_color, assume I'm not changing
// the img src.  Instead, change the border style from outset to inset.
//
function expandText(progName, icon_color, inputId)
{
  progE = document.getElementById(progName + '_text');
  imgE = document.getElementById(progName + '_image');
  headE = document.getElementById(progName + '_anchor');

  if (progE.style.display == 'block') {
    progE.style.display = 'none';
    if (icon_color) {
      imgE.src = '/images/Icons/icon_expand_' + icon_color + '.png';
    } else {
      imgE.style.borderStyle = 'outset';
    }
  } else {
    progE.style.display = 'block';
    if (icon_color) {
      imgE.src = '/images/Icons/icon_collapse_' + icon_color + '.png';
    } else {
      imgE.style.borderStyle = 'inset';
    }
  }

  // Set the focus to the specified input or the specified anchor.
  if (inputId) {
    document.getElementById(inputId).focus();
  } else if (headE) {
    headE.focus();
  }
}

// This function is used to prevent the ugly box around links when the
// user clicks on them.  It moves the focus to a link that is off the page.
// Call the function from the active link's onClick method.
// You have to add the following to your page for it to work:
// <a href="#" id="fixbox"></a>
//
function noboxLink()
{
  if (opera || other) {
    return false;
  }
  if (isIE4) {
    window.focus();
  }
  if (isDOM) {
    document.getElementById("fixbox").focus();
  }
}

// This function determines if the input is numeric.
//
// Return:
//  TRUE - numeric
//  FALSE - not numeric
//
function isNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber = true;
  var Char;

  for (var i = 0; i < sText.length && IsNumber == true; i ++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
    }
  }
  return(IsNumber);
}

function OLDisNumeric(St) {
  return(/[\%\.\d]/.test(St));
}

//
// This function formats a number (string) with commas.
//
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

//
// Helper function to strip commas from a string.
//
function stripCommas(str)
{
  var ind;
  while ((ind = str.indexOf(',')) > -1) {
    str = str.substring(0, ind) + str.substring(ind+1);
  }

  return(str);
}

// This function determines if the input object is an array.
//
// Return:
//  TRUE - array
//  FALSE - not array
//
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// This function adds a row to a table element.
// The rowIndex is the row after which the caller wants to add the new
// row.  Pass -1 to add the new row to the end of the table.
//
function addTableRow(eTab, rowHtml, cellClass, rowIndex)
{
  // Add a new row to the table.
  var new_row = eTab.insertRow(rowIndex);
  setTableRow(new_row, rowHtml, cellClass);

  return(new_row);
}

// This function replaces a row in the specified table element.
// Each cell (<td></td>) in row_html muse be separated by a \n.
//
function replaceTableRow(eRow, rowHtml, cellClass)
{
  // Delete the current cells in the row.
  var i;
  for (i = eRow.cells.length-1; i >= 0; i --) {
    eRow.deleteCell(i);
  }

  setTableRow(eRow, rowHtml, cellClass);
}

// This function creates cells in the specified row element using
// the specified HTML.  We cannot set the innerHTML attribute for IE,
// so this function parses the HTML for the row and uses DOM to add the row.
// Ref: http://msdn.microsoft.com/en-us/library/ms532998%28VS.85%29.aspx
//
// Each cell (<td></td>) in row_html muse be separated by a \n.
//
function setTableRow(eRow, rowHtml, cellClass)
{
  // Split the new row into cells.
  // Loop to add each cell to the row.
  var cells = rowHtml.split('\n');
  for (i = 0; i < cells.length; i ++) {
    if (cells[i].trim() != '') {
      var new_cell = eRow.insertCell(-1);
      var ind1 = cells[i].indexOf('>');
      var ind2 = cells[i].lastIndexOf('<');
      new_cell.innerHTML = cells[i].substring(ind1+1, ind2);

      // The caller passed a class for each cell.
      new_cell.className = cellClass;
    }
  }
}

// This function deletes a row from the specified table.  The rowId is
// the id attribute of the row the user wants to remove.
//
function removeRow(rowId, eTab)
{
  // Loop over the table rows to find the one to remove.
  for (var i = 0; eTab.rows.length; i ++) {
    if (eTab.rows[i].id == rowId) {
      eTab.deleteRow(i);
      return;
    }
  }
}
