/* =========================================================================

JavaScript Source File -- Created with SAPIEN Technologies PrimalScript 3.0

NAME: ToolBox.js

AUTHOR: Jeffrey Janner , PolyDyne Software Inc.
DATE  : 1/23/2004

COMMENT: 
	Set of generalized routines for doing common functions.
	Many parts lifted in whole or in part from "JavaScript & DHTML
	Cookbook" by Danny Goodman.  Published by O'Reilly & Associates, Inc.
	
COPYRIGHTS:
	Copyright © 2004 PolyDyne Software Inc.
	Some parts Copyright ©2003 Danny Goodman.

============================================================================ */

/* Funtions to open a Form in a Window without all the excess buttons and menus */
function openForm(formName) {
  var windowFeatures, formURL;
  if (formName.length > 0) {
    if (formName.charAt(0) == "/") {
      formURL = formName;
    } else {
      formURL = "/forms/" + formName;
    }
    if (screen.availWidth < 825) {
      windowFeatures = "width=" + (screen.availWidth - 12) + ", ";
    } else {
      windowFeatures = "width=825, ";
    }
    if (screen.availHeight < 700) {
      windowFeatures = windowFeatures + "height=" + (screen.availHeight - 76) + ", ";
    } else {
      windowFeatures = windowFeatures + "height=700, ";
    }
    if ((screen.availWidth < 850) || (screen.availHeight < 700)) {
      windowFeatures = windowFeatures + "top=0, left=0, ";
    } else {
      windowFeatures = windowFeatures + "top=10, left=50, ";
    }
    windowFeatures = windowFeatures + "status=yes, toolbar=no, menubar=no, location=no, ";
    windowFeatures = windowFeatures + "resizable=yes, scrollbars=yes";
    // go to the Login page
    window.open(formURL, "_blank", windowFeatures);
  }
}

/* Function to biuld a breadcrumb trail and load it into the breadcrumb element */
function createBreadCrumbs(Texts,Links) {
  var bc = "";
  var separator = " &raquo; "
  var textClass = new Array();
  var tdBreadCrumbs = document.getElementById("breadcrumbs");
  textClass[0] = "underlinepurple";
  textClass[1] ="underlinegreen";
  textClass[2] = "underlinepurple";
  if (Texts.length > 0) {
    j=0
    for (i=0; i < Texts.length; i++) {
      link = Links[i];
      text = Texts[i];
    if (text.length > 0) {
        if (i > 0) bc += separator;
        if (link != null) {
          bc += '<a href="' + link + '" class="' + textClass[j] + '">';
          bc += text;
          bc += '</a>'
          j += 1;
        } else {
          bc += text;
        }
      }
    }
  }
  if (bc.length > 0) tdBreadCrumbs.innerHTML = bc;
}

/* Function to left-pad a number to two digits */
function LZ(x) {return(x<0||x>9?"":"0")+x}

/* Function to verify that an entered email address conforms to RFCs */
function isValidEmail(emailAddress) {
  if (typeof emailAddress != "string") { return false; }
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(emailAddress);
}

/*-----------------------------------*/
/* String Trim Functions             */
/*-----------------------------------*/

// remove leading whitespace 
String.prototype.lTrim = function () {
return this.replace(/^\s*/,"");
}

// remove trailing whitespace
String.prototype.rTrim = function () {
return this.replace(/\s*$/,"");
}

// remove leading and trailing whitespace
String.prototype.etrim = function () {
return this.replace(/^\s+|\s+$/g,"");
}

// remove all redundant whitespace
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,"").replace(/\s+/g," ");
} 

/* Function to break a number into 3-digit grouping by inserting commas.
   Works with integers or floating point numbers.  Number must passed in its
   string representation. 
*/
function formatCommas(numString) {
  var decpoint, isFloat, intPart, decPart, re;
  decpoint = numString.indexOf(".")
  isFloat = (decpoint != -1) ? true : false;
  if (isFloat) {
    intPart = numString.substring(0,decpoint);
    decPart = numString.substring(decpoint,numString.length);
  } else {
    intPart = numString;
    decPart = "";
  }
  re = /(-?\d+)(\d{3})/;
  while (re.test(intPart)) {
    intPart = intPart.replace(re, "$1,$2");
  }
  return intPart + decPart;
}

/* Function to remove all commas from a string.  Normally used to strip commas from
   the string representation of a number prior to passing it to parseInt or parseFloat.
*/
function stripCommas(numString) {
  var re = /,/g;
  return numString.replace(re,"");
}

/* Function to allow only numbers to be entered in a textbox.
*/
function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        // alert(charCode + "Enter numerals only in this field.");
        return false;
    }
    return true;
}
