var defaultEmptyOK = false;

var whitespace = " \t\n\r";
var nameDelimiters = "-_. ";
var idDelimiters = "_";
var htmlDelimiters = '!@#$%&*()-_+=[]:;"<>,./?';
var validHtmlChars = whitespace + htmlDelimiters;
var urlDelimiters = "/-_.";

function isEmpty(s) {
    return ((s == null) || (s.length == 0))
}

function isWhitespace(s) {
    var i;

    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false; // not whitespace
    }
    return true;
}

function stripCharsInBag (s, bag){
    var i;
    var 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;
}

function warnInvalid (theField,s){
    theField.focus()
    theField.select()
    alert(s)
    return false
}

function makeTitleCase(s){
  if (isEmpty(s)) 
     if (makeTitleCase.arguments.length == 1) return defaultEmptyOK;
     else return (makeTitleCase.arguments[1] == true);
  count = 1;
  ws = 0;
  s = s.charAt(0).toUpperCase()+s.substring(1,s.length);
  while (count < s.length){
    if (isWhitespace(s.charAt(count)) || (s.charAt(count) == ".") || (s.charAt(count) == "-")) ws = 1;
    else if ((ws == 1) && (isLetter(s.charAt(count)))){
      s = s.substring(0,count)+s.charAt(count).toUpperCase()+s.substring(count+1,s.length);
      ws = 0;
    }
    count++;
  }
  return s;
}

function isLetter (c){
   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == "*") )
}

function isDigit (c){
   return ((c >= "0") && (c <= "9"))
}

function isAlphabetic (s){
    var i;
    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isLetter(c))
        return false;
    }
    return true;
}

function isAlphanumeric (s){
    var i;
    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }
    return true;
}

function checkName(theField,fieldName,emptyOK){
    if (checkName.arguments.length == 2) emptyOK = defaultEmptyOK;
    if (isEmpty(theField.value)) return emptyOK;
    else {
        if (!isAlphabetic(stripCharsInBag(theField.value,nameDelimiters))){
          warnInvalid(theField,fieldName+" contains invalid characters");
          return false;
        }
        else {
          theField.value = makeTitleCase(theField.value);
          return true;
        }
    }
}

function checkId(theField,fieldName,emptyOK){
    if (checkId.arguments.length == 2) emptyOK = defaultEmptyOK;
    if (isEmpty(theField.value)) return emptyOK;
    else {
    	if (!isLetter(theField.value.charAt(0))) {
          warnInvalid(theField,"The first letter of "+fieldName+" should be alphabetic");
          return false;
    	}
        if (!isAlphanumeric(stripCharsInBag(theField.value,idDelimiters))){
          warnInvalid(theField,fieldName+" contains invalid characters");
          return false;
        }
        else {
          theField.value = theField.value.toUpperCase();
          return true;
        }
    }
}

function checkDescription(theField,fieldName,emptyOK){
    if (checkDescription.arguments.length == 2) emptyOK = defaultEmptyOK;
    if (isEmpty(theField.value)) return emptyOK;
    else {
        if (!isAlphanumeric(stripCharsInBag(theField.value,nameDelimiters))){
          warnInvalid(theField,fieldName+" contains invalid characters");
          return false;
        }
        else {
          theField.value = makeTitleCase(theField.value);
          return true;
        }
    }
}

function checkHtmlDesc(theField,fieldName,emptyOK){
    if (checkHtmlDesc.arguments.length == 2) emptyOK = defaultEmptyOK;
    if (isEmpty(theField.value)) return emptyOK;
    else {
        if (!isAlphanumeric(stripCharsInBag(theField.value,validHtmlChars))){
          warnInvalid(theField,fieldName+" contains invalid characters");
          return false;
        }
        else {
          return true;
        }
    }
}

function checkUrl(theField,fieldName,emptyOK){
    if (checkUrl.arguments.length == 2) emptyOK = defaultEmptyOK;
    if (isEmpty(theField.value)) return emptyOK;
    else {
        if (!isAlphanumeric(stripCharsInBag(theField.value,urlDelimiters))){
          warnInvalid(theField,fieldName+" contains invalid characters");
          return false;
        }
        else {
          return true;
        }
    }
}

function formatCurrency(num,htmlobject) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
   num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
   cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  htmlobject.innerText= '$' + num + '.' + cents + ((sign)?'':'-');
}

function formatCurrency2(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
   num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
   cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  document.write('$' + num + '.' + cents + ((sign)?'':'-'));
}

function formatCurrency3(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
   num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();
  if(cents<10)
   cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  formattedval = ('$' + num + '.' + cents + ((sign)?'':'-'));
  return formattedval;
}

function formatCount(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
   num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num+0.50000000001);
  num = Math.floor(num).toString();
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  //htmlobject.innerText= num + ((sign)?'':'-');
  formattednum = num + ((sign)?'':'-');
  document.write(formattednum);
}

function formatCount2(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
   num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num+0.50000000001);
  num = Math.floor(num).toString();
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
  num = num.substring(0,num.length-(4*i+3))+','+
  num.substring(num.length-(4*i+3));
  //htmlobject.innerText= num + ((sign)?'':'-');
  formattednum = num + ((sign)?'':'-');
  return formattednum;
}

   
   function ValidateDate(DateValue,DateFmt) {
   // This function validates any 8-digit date with no separators
   // in MMDDYYYY, DDMMYYYY, or YYYYMMDD format.  The date range
   // supported is January 1, 1 through December 31, 9999
   // The function returns true if the date is valid, false if not
   
    if (DateFmt == 'mdy') {
     var year = DateValue.substr(4,4);
     var month = DateValue.substr(0,2);
     var day = DateValue.substr(2,2);
    }
    else if (DateFmt == 'dmy') {
     var year = DateValue.substr(4,4);
     var month = DateValue.substr(2,2);
     var day = DateValue.substr(0,2);
    }
    else {
     var year = DateValue.substr(0,4);
     var month = DateValue.substr(4,2);
     var day = DateValue.substr(6,2);
    }
    var DaysInMonths = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    var month_idx = month - 1;
    var divisor = 4;
    if (month < 1 || month > 12) return false;
    if (year == 0000) return false;
    if (year == 1582 && month == 10 && (day >= 5 && day <= 14)) return false; 
    if (year.substr(2,2) == 00) divisor = 400;
    if (year < 1600) divisor = 4;
    var remainder = year % divisor;
    if (remainder == 0) DaysInMonths[1] = 29;
    if (day < 1 || day > DaysInMonths[month_idx]) return false;
    return true;
   }
   
   function removeLeadingBlanks(value) { //This is the same as the trimL function below but it's still in use
    var returnvalue = "";
    for(count = 0; count < value.length-1 && value.charAt(count) == " "; count++);
    returnvalue = value.substr(count);
    if (returnvalue.length == 1 && returnvalue == " ") {
     return '';
    }
    else {
     return returnvalue; 
    }
   }
   
   function trim(value) {
    returnvalue = trimL(value);
    returnvalue = trimR(returnvalue);
    return returnvalue;
   }
   
   function trimL(value) {
    var returnvalue = "";
    for(count = 0; count < value.length-1 && value.charAt(count) == " "; count++);
    returnvalue = value.substr(count);
    if (returnvalue.length == 1 && returnvalue == " ") {
     return '';
    }
    else {
     return returnvalue; 
    }
   }
   
   function trimR(value) {
    var returnvalue = "";
    for(count = value.length-1; count > -1 && value.charAt(count) == " "; count--);
    returnvalue = value.substr(0,count+1);
    if (returnvalue.length == 1 && returnvalue == " ") {
     return '';
    }
    else {
     return returnvalue; 
    }
   }
   
   function removeLeadingZeros(value) {
    var returnvalue = "";
    for(count = 0; count < value.length-1 && value.charAt(count) == "0"; count++);
    returnvalue = value.substr(count);
    if (returnvalue.length == 1 && returnvalue == "0") {
     return '';
    }
    else {
     return returnvalue; 
    }
   }   

   // Allow only digits and slashes ('/')
   function CheckDateKey(eventObj) { 
    if (CheckControlKey(eventObj) || (eventObj.keyCode >= 96 && eventObj.keyCode <= 105) || ((eventObj.keyCode > 47 && eventObj.keyCode < 58) && eventObj.shiftKey == true) || eventObj.keyCode == 111 || eventObj.keyCode == 222) {
     return true;
    }
    else {
     return false;
    }
   }  

   // Allow only digits
   function CheckNumericKey(eventObj) { 
     //alert('CheckControlKey is ' + CheckControlKey(eventKey));
    if (!CheckControlKey(eventObj)) {
     //alert('Key pressed is ' + eventKey + ' - Shift is ' + ShiftKey);
     if ((eventObj.keyCode >= 96 && eventObj.keyCode <= 105) || ((eventObj.keyCode > 47 && eventObj.keyCode < 58) && eventObj.shiftKey != true)) {
      return true;
     }
     else {
      return false;
     }
    }
    else {
     return true;
    }
   }  

   // Allow only digits and decimal point
   function CheckAmountKey(eventObj) { 
    if (!CheckControlKey(eventKey)) {
     if ((eventObj.keyCode >= 96 && eventObj.keyCode <= 105) || eventObj.keyCode == 110 || eventObj.keyCode == 190 || ((eventObj.keyCode > 47 && eventObj.keyCode < 58) && eventObj.shiftKey != true)) {   
      return true;
     }
     else {
      return false;
     }
    }
    else {
     return true;
    }
   }

   // Allow only digits and dash (57393 is Opera's code for the minus (-) key on the numeric keypad)
   function CheckPhoneKey(eventObj) { 
    //alert('KeyCode=' + eventObj.keyCode + ' ShiftKey=' + eventObj.keyshiftKey);
    if (!CheckControlKey(eventObj)) {
     if ((eventObj.keyCode >= 96 && eventObj.keyCode <= 105) || eventObj.keyCode == 57393 || ((eventObj.keyCode == 189 || eventObj.keyCode == 109) && eventObj.shiftKey != true) || ((eventObj.keyCode > 47 && eventObj.keyCode < 58) && eventObj.shiftKey != true)) {   
      return true;
     }
     else {
      return false;
     }
    }
    else {
     return true;
    }
   }

   // Is the key pressed a control, function, or editing key?
   function CheckControlKey(eventObj) { 
    if ((eventObj.keyCode >= 0 && eventObj.keyCode <= 31) || (eventObj.keyCode >= 33 && eventObj.keyCode <= 46) || (eventObj.keyCode >= 91 && eventObj.keyCode <= 93) || (eventObj.keyCode >= 112 && eventObj.keyCode <= 145)) {   
     return true;
    }
    else {
     return false;
    }
   }

   function checkEmailAddr (emailStr) {

    /* The following variable tells the rest of the function whether or not
       to verify that the address ends in a two-letter country or well-known
       TLD.  1 means check it, 0 means don't. */

    var checkTLD=1;

    /* The following is the list of known TLDs that an e-mail address must end with. */

    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

    /* The following pattern is used to check if the entered e-mail address
       fits the user@domain format.  It also is used to separate the username
       from the domain. */

    var emailPat=/^(.+)@(.+)$/;

    /* The following string represents the pattern for matching all special
       characters.  We don't want to allow special characters in the address. 
       These characters include ( ) < > @ , ; : \ " . [ ] */

    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

    /* The following string represents the range of characters allowed in a 
     username or domainname.  It really states which chars aren't allowed.*/

    var validChars="\[^\\s" + specialChars + "\]";

    /* The following pattern applies if the "user" is a quoted string (in
       which case, there are no rules about which characters are allowed
       and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
       is a legal e-mail address. */

    var quotedUser="(\"[^\"]*\")";

    /* The following pattern applies for domains that are IP addresses,
       rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
       e-mail address. NOTE: The square brackets are required. */

    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

    /* The following string represents an atom (basically a series of non-special characters.) */

    var atom=validChars + '+';

    /* The following string represents one word in the typical username.
       For example, in john.doe@somewhere.com, john and doe are words.
       Basically, a word is either an atom or quoted string. */

    var word="(" + atom + "|" + quotedUser + ")";

    /* The following pattern describes the structure of the user */

    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

    /* The following pattern describes the structure of a normal symbolic
       domain, as opposed to ipDomainPat, shown above. */

    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

    /* Finally, let's start trying to figure out if the supplied address is valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
       different pieces that are easy to analyze. */

    var matchArray=emailStr.match(emailPat);

    if (matchArray==null) {

     /* Too many/few @'s or something; basically, this address doesn't
      even fit the general mold of a valid e-mail address. */

     return "Email address seems incorrect (check @ and periods)";
    }
    var user=matchArray[1];
    var domain=matchArray[2];

    /* Start by checking that only basic ASCII characters are in the strings (0-127). */

    for (i=0; i<user.length; i++) {
     if (user.charCodeAt(i)>127) {
      return "Ths username contains invalid characters.";
     }
    }
    for (i=0; i<domain.length; i++) {
     if (domain.charCodeAt(i)>127) {
      return "Ths domain name contains invalid characters.";
     }
    }

    /* See if "user" is valid */

    if (user.match(userPat)==null) {

     /* user is not valid */
     return "The username doesn't seem to be valid.";
    }

    /* if the e-mail address is at an IP address (as opposed to a symbolic
       host name) make sure the IP address is valid. */

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {

     /* this is an IP address */
   
     for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
       return "Destination IP address is invalid!";
      }
     }
     return '';
    }

    /* Domain is symbolic name.  Check if it's valid. */
 
    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
     if (domArr[i].search(atomPat)==-1) {
      return "The domain name does not seem to be valid.";
     }
    }

    /* domain name seems valid, but now make sure that it ends in a
       known top-level domain (like com, edu, gov) or a two-letter word,
       representing country (uk, nl), and that there's a hostname preceding 
       the domain or country. */

    if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
     return "The address must end in a well-known domain or two letter " + "country.";
    }

    /* Make sure there's a host name preceding the domain. */

    if (len<2) {
     return "This address is missing a hostname!";
    }

    /* If we've gotten this far, everything's valid! */
    return '';
   }
