var sMaskSet = 'aAlLn?'
var sUAscii = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
var bStarting = true;

function doKeyDown(e, textbox, sMask) {
 // trap and cancel keys that are not appropriate
 var iKeyCode = 0;  // collect key code
 if (window.event) iKeyCode = window.event.keyCode;
 else if (e) iKeyCode = e.which;
 if (iKeyCode == 32 || iKeyCode == 39 || iKeyCode == 35
 || iKeyCode == 8 || iKeyCode == 9)
  return true;    // space left end backspace tab
 if (iKeyCode < 47)  // non-printable character
  return false;
}

function doKeyPress(e, textbox, sMask) {

 window.status = '';
 var iKeyCode = 0;  // collect key code
 if (window.event) iKeyCode = window.event.keyCode;
 else if (e) iKeyCode = e.which;

 // check if mask already filled, and not backspace
 var iLength = textbox.value.length;
 if ((iLength == sMask.length) && (iKeyCode != 8))
  return false;

 // get mask character for this position in textbox
 var sMaskChar = sMask.charAt(iLength);

 // see if it's a special character
 if (sMaskSet.indexOf(sMaskChar) > -1) {

  // masked character required
  switch (sMaskChar) {

   case 'a':  // any alphanumeric character
    if ((iKeyCode > 47 && iKeyCode < 58)
    || (iKeyCode > 64 && iKeyCode < 91)
    || (iKeyCode > 96 && iKeyCode < 123))
     return true
    else return false;

   case 'A':  // uppercase alphanumeric character
    if ((iKeyCode > 47 && iKeyCode < 58)
    || (iKeyCode > 64 && iKeyCode < 91))
     return true
    else if (iKeyCode > 96 && iKeyCode < 123) {
     textbox.value += sUAscii.charAt(iKeyCode - 97);
     return false;
    }
    else return false;

   case 'l':  // any letter
    if ((iKeyCode > 64 && iKeyCode < 91)
    || (iKeyCode > 96 && iKeyCode < 123))
     return true
    else
     return false;

   case 'L':  // uppercase letter
    if (iKeyCode > 64 && iKeyCode < 91)
     return true
    else if (iKeyCode > 96 && iKeyCode < 123) {
     textbox.value += sUAscii.charAt(iKeyCode - 97);
     return false;
    }
    else return false;

   case 'n':  // any numeric character
    if (iKeyCode > 47 && iKeyCode < 58)
     return true
    else return false;
   case '?':  // any character
    return true;

   default: return false;
  }
 }
 else
  return true;
}

function doKeyUp(e, textbox, sMask) {
 if (bStarting != true) {
   var iKeyCode = 0;  // collect key code
   if (window.event) iKeyCode = window.event.keyCode;
   else if (e) iKeyCode = e.which;
   if (iKeyCode < 47 && iKeyCode != 32) return;
 }
 // check if next mask characters are literals
 // and add to text box if they are
 while ((textbox.value.length < sMask.length) &&
 (sMaskSet.indexOf(sMask.charAt(textbox.value.length)) == -1)) {
  textbox.value += sMask.charAt(textbox.value.length);
 }
 var sNext;
 if (textbox.value.length == sMask.length)
  sNext = 'Complete'
 else
  switch (sMask.charAt(textbox.value.length)) {
   case 'a':
    sNext = 'Expecting any alphanumeric character (0-9,A-Z,a-z)';
    break;
   case 'A':
    sNext = 'Expecting an uppercase alphanumeric char (0-9,A-Z)';
    break;
   case 'l':
    sNext = 'Expecting any letter (A-Z, a-z)';
    break;
   case 'L':
    sNext = 'Expecting an uppercase letter (A-Z)';
    break;
   case 'n':
    sNext = 'Expecting any numeric character (0-9)';
    break;
   case '?':
    sNext = 'Expecting any character';
    break;
   default: sNext = '';
  }
 window.status = sNext;
}

function doFocus(e, textbox, sMask) {
 bStarting = true;
 doKeyUp(e, textbox, sMask);
 bStarting = false;
}

function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

// Convert the number to a string
var value_string = rounded_value.toString()

// Locate the decimal point
var decimal_location = value_string.indexOf(".")

// Is there a decimal point?
if (decimal_location == -1) {

// If no, then all decimal places will be padded with 0s
decimal_part_length = 0

// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {

// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}

// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length

if (pad_total > 0) {

// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++) 
value_string += "0"
}
return value_string
}
