//==============================================================================
// JavaScript utility functions
//==============================================================================

// format variable as US dollars
function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
    num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num % 100;
  num = Math.floor(num/100).toString();
  if(cents < 10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
  return (((sign)?'':'-') + num + '.' + cents);
}

// remove formatting from a form field value
function stripFormat(theField, stripChars) {
  var formatted = theField.value;
  var unformatted = "";
  for (var i=0; i < formatted.length; i++) {
    if (stripChars.indexOf(formatted.charAt(i)) == -1) {
      unformatted += formatted.charAt(i);
    }
  }
  return unformatted;
}

// find selected radio button in a group
function getRadioButton(radioGroup) {
  for (var i=0; i < radioGroup.length; i++) {
    if (radioGroup[i].checked) { return i; }
  }
  return 0;
}

// extract front of mainStr before searchStr
function getFront(mainStr,searchStr) {
  foundOffset = mainStr.indexOf(searchStr);
  if (foundOffset == -1) {
    return null;
  }
  return mainStr.substring(0,foundOffset);
}

// extract end of mainStr after searchStr
function getEnd(mainStr,searchStr) {
  foundOffset = mainStr.indexOf(searchStr);
  if (foundOffset == -1) {
    return null;
  }
  return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
}

// insert insertStr immediately before searchStr in mainStr
function insertString(mainStr,searchStr,insertStr) {
  var front = getFront(mainStr,searchStr);
  var end = getEnd(mainStr,searchStr);
  if (front != null && end != null) {
    return front + insertStr + searchStr + end;
  }
  return mainStr;
}

// replace searchStr with replaceStr in mainStr
function replaceString(mainStr,searchStr,replaceStr) {
  var front = getFront(mainStr,searchStr);
  var end = getEnd(mainStr,searchStr);
  if (front != null && end != null) {
    return front + replaceStr + end;
  }
  return mainStr;
}

// remove deleteStr from mainStr
function deleteString(mainStr,deleteStr) {
  return replaceString(mainStr,deleteStr,"");
}

// create pop-up window from url
function PopUp(whatURL) { 
	open( whatURL ,"PopUp","scrollbars=yes,status=no,toolbar=no,width=620,height=500,resize=yes");
 }
 
 // create fully controllable pop-up window from url - example file in ISdept dir under EBusiness
 function popWindow(url, windowName, width, height, statusbar, resizable, scrollbars, toolbar, menubar, locationbar, directories) {
  /* defaults if not passed to function */
  if (!windowName) { windowName = "amswebWindow"; }
  if (!width) { width = 700; }
  if (!height) { height = 600; }
  if (!statusbar) { statusbar = "yes"; }
  if (!resizable) { resizable = "yes"; }
  if (!scrollbars) { scrollbars = "yes"; }
  if (!toolbar) { toolbar = "no"; }
  if (!menubar) { menubar = "no"; }
  if (!locationbar) { locationbar = "no"; }
  if (!directories) { directories = "no"; }
	/* open the window with the designated controls */
  wref = window.open (url, windowName, "toolbar=" + toolbar + ",location=" + locationbar +
        ",width=" + width + ",height=" + height + ",directories=" + directories + ",status=" + statusbar + 
        ",scrollbars=" + scrollbars + ",resizable=" + resizable + ",menubar=" + menubar);
  /* throw focus to window */
	if (navigator.appName == "Netscape" || navigator.appVersion.substring(0,1) == '5') {
    wref.focus();
  }
}