<!-- hide from non-supported browsers
// dx/includes/dxutil2.js
// DX Library - Edit Utilities, numbers, alphanumeric,phone,zip,strip,reformat

// emptyok assumed false in all routines

// DX_phone (TEXTFIELD theField [, BOOLEAN emptyOK==false])
// Check that string theField is a valid Phone.
// Reformats to (xxx) xxx-xxxx
function DX_phone (theField) {  
 if (isEmpty(theField))
  if (DX_phone.arguments.length == 1) return false;
  else return (DX_phone.arguments[1] == true);
 var Delimiters = "()- ";
 var s = stripCharsInBag(theField, Delimiters);
 if (isInteger(s) && s.length == 10) {
  s = reformat(s,"(", 3, ") ", 3, "-", 4);
  return s;
 }
 else {
  return false;
 }
}

// DX_zip (TEXTFIELD theField [, BOOLEAN emptyOK==false])
// Check that string theField is a valid International Zip Code.
function DX_zip(theField) {  
    
    // reg expression to test for
    var regstr = /^[a-zA-Z0-9- ]+$/;
    
    // is it empty AND does it fail reg ex AND is it 9 chars max?
    if (!isEmpty(theField) && regstr.test(theField) && 
        theField.toString().length <= 9){
        //if (DX_zip.arguments.length == 1) return (false);
        //else return (DX_zip.arguments[1] == true);
        //var Delimiters = "- ";
        //var s = stripCharsInBag(theField, Delimiters);
        //if (isInteger(s) && (s.length == 5 || s.length == 9)) {
        //if (isInteger(s) && (s.length == 5 || s.length == 9)) {
        //if (s.length == 9) s = reformat(s,"", 5, "-", 4);
        
        // simply check to ensure that zip/postal code
        // is an alphanumeric value, or a space value, or can have dashes        
        return theField; 
    }
    else {
        return false;   
    }
}

//----------
// isEmpty (STRING s) 
// Check whether string s is empty.
function isEmpty(s) {
 return ((s == null) || (s.length == 0))
}

//----------
// stripCharsInBag (STRING s, STRING bag)
// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag) {   
 var i;var returnString = "";
 // Search through string's characters one by one.
 // If character is not in bag, append to returnString.
 for (i = 0; i < s.length; i++) {   
  // Check that current character isn't whitespace.
  var c = s.charAt(i);
  if (bag.indexOf(c) == -1) returnString += c;
 }
 return returnString;
}

//----------
// reformat (TARGETSTRING, STRING s, INTEGER, STRING, INTEGER ... )       
// Handy function for arbitrarily inserting formatting characters
// or delimiters of various kinds within TARGETSTRING.
// reformat takes one named argument, a string s, and any number
// of other arguments.  The other arguments must be integers or
// strings.  These other arguments specify how string s is to be
// reformatted and how and where other strings are to be inserted
// into it.
// reformat processes the other arguments in order one by one.
// * If the argument is an integer, reformat appends that number 
//   of sequential characters from s to the resultString.
// * If the argument is a string, reformat appends the string
//   to the resultString.
function reformat (s) {   
 var arg; var sPos = 0;
 var resultString = "";
 for (var i = 1; i < reformat.arguments.length; i++) {
  arg = reformat.arguments[i];
  if (i % 2 == 1) resultString += arg;
  else {
   resultString += s.substring(sPos, sPos + arg);
   sPos += arg;
  }
 }
 
 return resultString;
}


//----------
// isAlphanumeric (STRING s [, BOOLEAN emptyOK==false])
// Returns true if string s is English letters
// (A .. Z, a..z) and numbers only.
function isAlphanumeric (s) {
 var i;
 if (isEmpty(s))
  if (isAlphanumeric.arguments.length == 1) return (false);
  else return (isAlphanumeric.arguments[1] == true);
 // Search through string's characters one by one
 // until we find a non-alphanumeric character.
 // When we do, return false; if we don't, return true.
 for (i = 0; i < s.length; i++) { 
  // Check that current character is number or letter.
  var c = s.charAt(i);
  if (! (isLetter(c) || isDigit(c) ) )
    return false;
 }
 // All characters are numbers or letters.
 return true;
}

//----------
// Returns true if character c is an English letter (A .. Z, a..z).
function isLetter (c) {
 return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

//----------
// Returns true if character c is a digit (0 .. 9)
function isDigit (c) {
 return ((c >= "0") && (c <= "9"))
}

//----------
// isInteger (STRING s [, BOOLEAN emptyOK==false])
// Returns s if all characters in string s are numbers or false if not.
// Allows + or - in first position
function isInteger (s) { 
 if (isEmpty(s))
  if (isInteger.arguments.length == 1) return (false);
  else return (isInteger.arguments[1] == true);
 var i;
 for (i = 0; i < s.length; i++) {   
  var c = s.charAt(i);
  if ((i == 0) && ((c == "+") || (c == "-")))
  {}
  else {
   if ((c < "0") || (c > "9")) return false;
  }
 }
 return s;
}


//----------
// isNonnegativeInteger (TEXTFIELD theField [, BOOLEAN emptyOK==false])
// Check that string theField is a valid integer >=0.
// removes commas and spaces
function isNonnegativeInteger (theField) {  
 if (isEmpty(theField))
  if (isNonnegativeInteger.arguments.length == 1) return (false);
  else return (isNonnegativeInteger.arguments[1] == true);
 var Delimiters = ", ";
 var s = stripCharsInBag(theField, Delimiters)
 if (s.length == 0) return false;
 if (!isInteger(s) || (parseInt(s)<0)) return false;
 else return s;
}

//----------
// isIntegerInRange (TEXTFIELD theField, INTEGER a, INTEGER b [, BOOLEAN emptyOK==false])
// isIntegerInRange returns true if theField is an integer 
// within the range of integer arguments a and b, inclusive.
// removes commas and spaces
function isIntegerInRange (theField, a, b, emptyOK) {
 if (isEmpty(theField))
  if (isIntegerInRange.arguments.length < 4) return (false);
  else return (isIntegerInRange.arguments[3] == true);  
 var Delimiters = ", ";
 var s = stripCharsInBag(theField, Delimiters)
 if (s.length == 0) return false;
 if (!isInteger(s) || parseInt(s) < a || parseInt(s) > b) return false;
 return s;
}

//----------
// isFloat (TEXTFIELD theField, INTEGER decimals [, BOOLEAN emptyOK==false])
// Check that string theField is a valid float number >=0.
// decimals indicates max number of digits to right of decimal point.
// removes $, commas and spaces
function isFloat (theField, decimals, emptyOK) {  
 if (isEmpty(theField))
  if (isFloat.arguments.length < 3) return (false);
  else return (isFloat.arguments[2] == true);
 var Delimiters = "$, ";
 var s = stripCharsInBag(theField, Delimiters);
 if (s.length == 0) return false;
 var seenDecimalPoint = false;
 var rightcount=0;
 for (i = 0; i < s.length; i++) {   
  // Check that current character is number.
  var c = s.charAt(i);
  if ((i == 0) && ((c == "+") || (c == "-")))
  {}
  else {
   if ((c == ".") && !seenDecimalPoint) seenDecimalPoint = true;
   else {
    if (!isDigit(c)) return false;
    if (seenDecimalPoint == true) {
     rightcount++;
     if (rightcount > decimals) return false;
    }
   }
  }
 }
 // All characters are numbers.
 return s;
}


//----------
// isFloatInRange (TEXTFIELD theField, INTEGER a, INTEGER b, INTEGER decimals [, BOOLEAN emptyOK==false])
// isFloatInRange returns true if theField is a valid float number 
// within the range of integer arguments a and b, inclusive.
// decimals indicates max number of digits to right of decimal point.
// removes $, commas and spaces (in isFloat)
function isFloatInRange (theField, a, b, decimals, emptyOK) {
 if (isEmpty(theField))
  if (isFloatInRange.arguments.length < 5) return false;
  else return (isFloatInRange.arguments[4] == true);  
 var s = isFloat(theField, decimals);
 if (!s || parseFloat(s) < a || parseFloat(s) >b) return false;
 return s;
}

// -->

