// general values
// note: could make static variable for this if using objects...
var previousLength  = null;

function autoFormatPhone(formField, areaCodePrefix, areaCodeSuffix, threeFourSeparator)
{
  // lock out NS4
  if (!document.layers)
  {
    // assign default values if nothing passed in
    var areaCodePrefix        = (areaCodePrefix == null) ? '(' : areaCodePrefix;
    var areaCodeSuffix        = (areaCodeSuffix == null) ? ') ' : areaCodeSuffix;
    var threeFourSeparator    = (threeFourSeparator == null) ? '-' : threeFourSeparator;
    
    // local values
    var groupLengths    = new Array(3, 3, 4);
    var phonePieces     = new Array();
    var thisInput       = null;
    var regNumberFirst  = new RegExp("^[0-9]");
    
    thisInput = formField.value;
    
    if (thisInput.length == areaCodePrefix.length && thisInput == areaCodePrefix)
    {
      // no action taken on area code prefix
      previousLength = areaCodePrefix.length; 
      return true;
    } 
    else if (thisInput.length > previousLength && (regNumberFirst.test(thisInput) || (areaCodePrefix.length > 0 && thisInput.substr(0, areaCodePrefix.length) == areaCodePrefix)))
    {
      // length has incrased (no action taken on backspace) and value begins with a number or area code prefix (other entries not formatted)
      thisInput = thisInput.replace(/[. ()-\/]/gi, '');
      
      // separate string into groups
      phonePieces[0] = thisInput.substr(0, groupLengths[0]);
      phonePieces[1] = thisInput.substr(groupLengths[0], groupLengths[1]);
      phonePieces[2] = thisInput.substr(groupLengths[0] + groupLengths[1], groupLengths[2]);
      
      // prefix for area code
      if (thisInput.length > 0) { phonePieces[0] = areaCodePrefix + phonePieces[0]; }
      
      // suffix for area code, add length of prefix
      if (phonePieces[0].length == groupLengths[0] + areaCodePrefix.length) { phonePieces[0] += areaCodeSuffix; }
      
      // add separator for last seven numbers
      if (phonePieces[1].length == groupLengths[1]) { phonePieces[1] += threeFourSeparator; }
      
      // assign to field
      formField.value = phonePieces.join('');
    }
    previousLength = formField.value.length
  }
}

function autoFormatDate(formField, dateSeparator, yearLength, monthLength, dayLength)
{
  /*
  note: this function MUST have month and day lengths preset. year can be two or four
        preset needed since a user typing "1" could mean "1" or "10" if no preset
        so "1/21/00" and "12/1/00" become confusing

  note: in the future, allow specific date entry (dd/mm/yy, mm/dd/yy, etc)
  */
  // lock out NS4
  if (!document.layers)
  {
    // assign default values if nothing passed in
    var dateSeparator   = (dateSeparator == null) ? '/' : dateSeparator;
    var yearLength      = (yearLength == null || (yearLength != 2 && yearLength != 4)) ? 2 : yearLength;
    var monthLength     = (monthLength == null || (monthLength != 1 && monthLength != 2)) ? 2 : monthLength;
    var dayLength       = (dayLength == null || (dayLength != 1 && dayLength != 2)) ? 2 : dayLength;
    
    // local values
    var datePieces      = new Array();
    var finalDate       = null;
    var thisInput       = null;
    var regNumberFirst  = new RegExp("^[0-9]");
    
    thisInput = formField.value;
    
    if (thisInput.length > previousLength && regNumberFirst.test(thisInput))
    {
      // length has incrased (no action taken on backspace) and value begins with a number (other entries not formatted)
      // filter out unwanted characters
      thisInput = thisInput.replace(/[^0-9]/gi, '');
      
      // note: this function currently assumes mm/dd/yy entry
      if (1)
      {
        // separate string into groups
        datePieces[0] = thisInput.substr(0, monthLength);
        datePieces[1] = thisInput.substr(monthLength, dayLength);
        datePieces[2] = thisInput.substr(monthLength + dayLength, yearLength);
        
        // first separator
        if (datePieces[0].length == monthLength) { datePieces[0] += dateSeparator; }
        
        // add second separator
        if (datePieces[1].length == dayLength) { datePieces[1] += dateSeparator; }
        
        // add year
        finalDate = datePieces.join('');
      }
      
      // assign to field
      formField.value = finalDate;
    }
    previousLength = formField.value.length
  }
}

function autoFormatMoney(formField, monetarySymbol, maxDecimalPlaces)
{
  /*
  note: this function MUST put the monetary symbol BEFORE the number
        as further entry into the field is at the end, where the symbol should not be
  */
  // lock out NS4
  if (!document.layers)
  {
    // assign default values if nothing passed in
    var monetarySymbol          = (monetarySymbol == null) ? '$' : monetarySymbol;
    var maxDecimalPlaces          = (maxDecimalPlaces == null) ? 2 : maxDecimalPlaces;

    // local values
    var addCommas       = true;
    var groupLength     = 3;
    var moneyPieces     = new Array();
    var thisInput       = null;
    var regNumberFirst  = new RegExp("^[0-9]");
    var tempMoneyPieces = '';
    var commaPosition   = null;
    
    thisInput = formField.value;
    
    if (thisInput.length == monetarySymbol.length && thisInput == monetarySymbol)
    {
      // no action taken on money symbol if symbol suppose to be before
      previousLength = monetarySymbol.length; 
      return true;
    } 
    else if (regNumberFirst.test(thisInput) || (monetarySymbol.length > 0 && thisInput.substr(0, monetarySymbol.length) == monetarySymbol))
    {
      // value begins with a number or monetary prefix (other entries not formatted)
      thisInput = thisInput.replace(/[^0-9.]/gi, '');
      
      // note: need to detect if string has only one decimal
      if (1)
      {
        if (thisInput.indexOf('.') > 0)
        {
          // split string at decimal
          moneyPieces = thisInput.split(".");
        
          // ensure only certain amount of decimals
          if (moneyPieces[1].length > maxDecimalPlaces) { moneyPieces[1] = moneyPieces[1].substr(0, maxDecimalPlaces); }
          
          moneyPieces[1] = '.' + moneyPieces[1];
        }
        else
        {
          // put whole string into one-element array (using .join later)
          moneyPieces[0] = thisInput;
        }

        if (addCommas && moneyPieces[0].length > groupLength)
        {
          // add commas if number is over 999 and "addcommas" flag is true
          commaPosition = moneyPieces[0].length % groupLength;
          
          // first group if incomplete (i.e. does not divide equally into groupLength
          if (commaPosition > 0)
          {
            tempMoneyPieces = moneyPieces[0].substr(0, commaPosition) + ',';
          }
          
          // add a comma to the end of every group of numbers except the last
          do
          {
            tempMoneyPieces += moneyPieces[0].substr(commaPosition, groupLength) + ',';
            
            // catch for 4 or 5 digits adding comma at end
            if (moneyPieces[0].length <  2 * groupLength) { tempMoneyPieces = tempMoneyPieces.substr(0, tempMoneyPieces.length - 1); }
            
            commaPosition += groupLength;
          } 
          while (commaPosition < (moneyPieces[0].length - groupLength));
          
          moneyPieces[0] = tempMoneyPieces +  moneyPieces[0].substr(commaPosition, groupLength);;
        }
      }
      
      formField.value = monetarySymbol + moneyPieces.join('');
    }
  }
}

function autoFormatSSN(formField, ssnSeparator)
{
  // lock out NS4
  if (!document.layers)
  {
    // assign default values if nothing passed in
    var ssnSeparator   = (ssnSeparator == null) ? '/' : ssnSeparator;
    
    // local values
    var ssnPieces       = new Array();
    var groupLengths    = new Array(3, 2, 4);
    var thisInput       = null;
    var regNumberFirst  = new RegExp("^[0-9]");
    
    thisInput = formField.value;
    
    if (thisInput.length > previousLength && regNumberFirst.test(thisInput))
    {
      // length has incrased (no action taken on backspace) and value begins with a number (other entries not formatted)
      thisInput = thisInput.replace(/[^0-9]/gi, '');
      
      // separate string into groups
      ssnPieces[0] = thisInput.substr(0, groupLengths[0]);
      ssnPieces[1] = thisInput.substr(groupLengths[0], groupLengths[1]);
      ssnPieces[2] = thisInput.substr(groupLengths[0] + groupLengths[1], groupLengths[2]);

      // add separators        
      if (ssnPieces[0].length == groupLengths[0]) { ssnPieces[0] += ssnSeparator; }
      if (ssnPieces[1].length == groupLengths[1]) { ssnPieces[1] += ssnSeparator; }
      
      formField.value = ssnPieces.join('');
    }
    
    previousLength = formField.value.length
  }
}

