SHOW_ERROR=true;
SUPPRESS_ERROR=false;
IE = false;
// Format dollar amount with dollar sign.  If show_error is true, display error message; otherwise, just return false.
// E.g., <input type=text name="dollar" onchange="formatMoney(this,SHOW_ERROR);">
function formatMoney(fld, show_error) {
  var amt=fld.value.ltrim().rtrim();
  var moneyChars="0123456789";
  var cents="00";
  var formatAmt="";
  var errorDesc="";

// If there is a dollar sign, remove it.
  if (amt.charAt(0) == "$") amt = amt.substring(1,amt.length).ltrim();

// Check for a decimal, and set a flag if there is one in the right place.
  if (amt.indexOf(".") != -1 ) {
    if (amt.indexOf(".") == amt.lastIndexOf(".") && amt.indexOf(".") >= amt.length-3) {
      if (amt.indexOf(".") < amt.length-1) cents = amt.substring(amt.indexOf(".")+1, amt.length);
      if (cents.length==1) cents=cents+"0";
      amt = amt.substring(0,amt.indexOf("."));
    } else
      errorDesc = "Decimal placement error";
  }

// Remove any commas.
  if (errorDesc.length == 0) {
    while (amt.indexOf(",") != -1) {
      amt = amt.substring(0,amt.indexOf(","))+amt.substring(amt.indexOf(",")+1, amt.length);
    }
  }

// Now, check for any invalid characters.  There should not be any characters other than digits.  
  for (var i=0; i<amt.length && errorDesc.length==0; ++i) {
    if (moneyChars.indexOf(amt.charAt(i)) == -1) {
      errorDesc = "Dollar amount contains at least one illegal character.";
    }
  }

  if (errorDesc.length==0) {
// The dollar amount now contains only digits, so we can format them.
    if (amt.length == 0) amt="0";
    formatAmt = "."+cents;
    while (amt.length > 3) {
      formatAmt = ","+amt.substring(amt.length-3,amt.length)+formatAmt;
      amt=amt.substring(0,amt.length-3);
    }
    formatAmt = "$"+amt+formatAmt;
//    formatAmt = amt+""+formatAmt;
  } else {
    if (show_error) {
      alert(errorDesc);
      fld.focus();
    } else {
      return false;
    }
  }
  fld.value = formatAmt;
  return true;
}


// Verify that this is a valid ABA number.
function valABA(fld, show_error) {
  var errorDesc="";
  var vABA=fld.value;

  if (vABA.length != 9) {
    errorDesc = "ABA# must be contain exactly nine digits.";
  } else {
    var w = new Array(3,7,1,3,7,1,3,7);	
    var Sum = 0;
    for (j=0; j < w.length; j++) Sum += w[j] * (vABA.charAt(j)- '0');
    var CheckDigit = 10 - (Sum%10);
    if (CheckDigit == 10) CheckDigit = 0;
    if (vABA.charAt(vABA.length-1)-'0' != CheckDigit) {
      errorDesc = "   Invalid check digit in the F.I. Routing number.\n";
    }
  }
  if (errorDesc.length!=0) {
    if (show_error) {
      alert(errorDesc);
      fld.focus();
    } else {
      return false;
    }
  }
  return true;
}

function checkDate (dateVal) {
   if (/^(\d{2})([\/\.-]?)(\d{2})(\2)(\d{2}|\d{4})$/.test(dateVal) ||
      /^(\d{1,2})([\/\.-])(\d{1,2})(\2)(\d{2}|\d{4})$/.test(dateVal)) {
      this.month   = RegExp.$1 * 1;
      this.date    = RegExp.$3 * 1;
      this.year    = RegExp.$5 * 1;
      this.isValid = true;
   } else {
      this.isValid = false;
   } // end if-else
   return (this.isValid);
} // end fun checkDate


// Verify that this is a valid quantity (non-negative and no decimals).
// e.g. <INPUT type="text" name="quantity" onchange="valQuantity (this, 'Number of Foo');">
function valQuantity (fld, desc, minZeroFlag, emptyOK) {
var minZeroFlag; // true if 0 okay, false sets min val to 1
var errorDesc = '';
var testAmt = String (fld.value);
   if (emptyOK && fld.value == '') return;
   while (testAmt.charAt (0) == '0' && testAmt.length > 1)
      testAmt = testAmt.substring (1, testAmt.length);
   if (!testAmt.isNumeric () || testAmt == '')
      errorDesc = 'You have entered an invalid number. Please try again.';
   if (errorDesc.length == 0)
      if (!minZeroFlag && parseInt (testAmt) <= 0)
         errorDesc = 'Please enter a value greater than zero.';
   if (errorDesc.length > 0) {
      fld.focus ();
      fld.select ();
      alert (desc + ': ' + errorDesc);
      validationFLAG = false;
      if (IE) setIEFieldFocus (fld);
      return (false);
   } // end if
   return (true);
} // end fun valQuantity

function valUserName (fld, desc, len) {
   var len;
   var reBegSpaces = /(^\s)/;
   var reEndSpaces = /(\s$)/;
   if (arguments.length <= 2) len = 4; 
   if (reBegSpaces.test(fld.value) || reEndSpaces.test(fld.value)) {
      fld.focus();
      fld.select();
      alert (desc + ": " + " This field must not begin with a space nor end with a space. Please try again");
      validationFLAG = false;
      if (IE) setIEFieldFocus (fld);
      return (false);
   } else {                      
      return (true);
   } // end if-else
} // end fun valUserName


function valDate(f, desc, begin, end, emptyOK,valBusDate) {
var dateFlag, errorDesc = '';
   if (2 > arguments.length || arguments.length > 6) return;  
 
   if (arguments.length == 3 && String(begin).length < 8) emptyOK = begin;
   if (emptyOK && f.value == '') return true;

   dateFlag = new checkDate(f.value);

   if (dateFlag.isValid){
      var M = dateFlag.month;
      var D = dateFlag.date;
      var Y = dateFlag.year;
      if (Y < 100)
         Y = (Y <= 50) ? Y + 2000 : Y + 1900;
      if (M < 1 || M > 12) {
         errorDesc = " Invalid month.";
      } else {
         var ld = (Y % 400 == 0 || Y % 100 != 0 && Y % 4 == 0) ? "9" : "8";
         var maxdays = "312" + ld + "31303130313130313031";
         if (D < 1 || D > maxdays.substr ((M - 1) * 2, 2))
            errorDesc = " Invalid date.";
      } // end if-else
      if (errorDesc.length == 0) {
         // Date is valid; now let's check on the range.


      var nDate = new Date(Y,(parseInt(M)-1),D);
         if ((arguments.length > 2) && (begin != true)) {
            if (nDate < begin || nDate > end) {
               errorDesc = "Date (" + parseDateOb2Str(nDate) + ") is outside of the valid range (" + parseDateOb2Str(begin) + " - " + parseDateOb2Str(end) + ").";
            }
         }
      }
      if (errorDesc.length == 0) {
         // Date is valid and in range; format it.
         f.value =  parseDateOb2Str(nDate);       
      } // end if
   } else {
      errorDesc = "You have entered an invalid date. Please re-enter the date in an accepted format, e.g. 01/01/2000 for January 1, 2000.";
   } // end if-else

  //holiday string must exist for this to run
   if (errorDesc.length == 0)
   {
      if (valBusDate)
      {
       if(isWkEndOrHoliday(nDate))
           errorDesc = 'The date you have chosen is a weekend or other Federal non-processing day. Please choose a regular business day for your date.';
      }
   }               


   if (errorDesc.length > 0) {
      f.focus();
      f.select();
      alert(desc + ": " + errorDesc);
      validationFLAG = false;
      if (IE) setIEFieldFocus (f);
      return (false);
   } // end if
	return (true);
} // end fun valDate


//parses date object to standard display form "01/01/2001"
function parseDateOb2Str(dateOb)
//# returns date as a string
{
	var sMonth = String(dateOb.getMonth() + 1);
	var sDate = String(dateOb.getDate());
	var sYear = String(dateOb.getFullYear());
	
	if(sMonth.length == 1) sMonth = '0' + sMonth;
	if(sDate.length == 1) sDate = '0' + sDate;
	
	var newDate = sMonth + '/' + sDate + '/' + sYear;
        return   String(newDate);
}

// Validate and format phone number.
// e.g. <INPUT type="text" name="phoneNum" onchange="valPhone(this, 'Foo Phone');">
function valPhone (f, desc, emptyOK) {
   if (emptyOK && f.value == "") return;
   if (/^\((\d{3})\)\s?(\d{3})[\s|\.|-](\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d+))?$/.test(f.value) ||
      /^(\d{3})[\s|\/|\.|-]?(\d{3})[\s|\.|-]?(\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d+))?$/.test(f.value)) {
      if (RegExp.$7) f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3+"X"+RegExp.$7;
      else f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3;
   } else {
      f.focus ();
      f.select ();
	
	  var x10tionMesg;
	  if (desc == "Fax Number") {
		  x10tionMesg = "";
	  } else {
		  x10tionMesg = "\n If you wish to enter an extension, place it at the end of the main telephone number, and precede it with an 'X'.";
	  }	
	
      alert (desc + ": " + " You have entered an invalid number.  The number must be 10 digits in length and contain the 3 digit area code.  Please re-enter, formatting will be completed automatically.\n " + x10tionMesg);

      validationFLAG = false;
      if (IE) setIEFieldFocus (f);
      return (false);
   } // end if-else
   return (true);
} // end fun valPhone


// Verify that the email address is valid.
// e.g. <INPUT type="text" name="email" onchange="valEmail(this, 'Foo Email');">
function valEmail (fld, desc, emptyOK) {
var arrayOfEmails = new Array ();
var arrayIndex    = 0;
var errorDesc     = '';
var reEmail       = new RegExp ();
var reSplit       = new RegExp ();
   reEmail = /^(\w+[\.-])*\w+@(\w+[\.-])*\w+\.\w{2,3}$/;
   reSplit = /;\s*/;
   if (emptyOK && fld.value == '') return;
   arrayOfEmails = String (fld.value).split (reSplit);
   while (errorDesc.length == 0 && arrayIndex < arrayOfEmails.length) {   
      if (!reEmail.test (arrayOfEmails [arrayIndex]))
         if (arrayOfEmails.length > 1) errorDesc = ' The list of email addresses is formatted incorrectly. Addresses must be in the correct format and separated by a single semicolon (\';\'). Please check individual addresses and try again.';
         else errorDesc = ' You have entered an invalid email address. Please try again.';
      arrayIndex++;
   } // end while
   if (errorDesc.length > 0) {
      fld.focus ();
      fld.select ();
      alert (desc + ': ' + errorDesc);
      validationFLAG = false
      if (IE) setIEFieldFocus (fld);
      return (false);
   } // end if
   return (true);
} // end fun valEmail


// Verify that a field has content and is of a minimum length
// e.g. <INPUT type="text" name="userName" onchange="valMinLength(this, 4, 'Foo Name');">
function valMinLength (f, minlength, desc, emptyOK) {
   if (emptyOK && f.value == '') return;
   if (f.value.length < minlength) {
      f.focus();
      f.select();
      if (minlength > 1) alert (desc + ': At least '  + minlength + ' characters are required. Please try again.');
      else alert (desc + ': This value may not be left blank. Please try again.');
      validationFLAG = false;
      return (false);
   }
   return (true);
} // end fun valMinLength

function convertToUpper (theField) {
var theField;
      if (theField.type.toLowerCase () == 'text')
         theField.value = theField.value.toUpperCase ();
   return;
} // end fun convert
