function test1() {
  alert("T E S T 1");
}

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. Instead,
// please just point to my URL to ensure the most up-to-date versions
// of the files. Thanks.
// ===================================================================

//-------------------------------------------------------------------
// Trim functions
//   Returns string with whitespace trimmed
//-------------------------------------------------------------------
function LTrim(str) {
  for (var i=0; str.charAt(i)==" "; i++);
  return str.substring(i,str.length);
}

function RTrim(str) {
  for (var i=str.length-1; str.charAt(i)==" "; i--);
  return str.substring(0,i+1);
}

function Trim(str) {
  return LTrim(RTrim(str));
}

// Remove all spaces from a string
function removeSpaces(string) {
  var newString = '';
  for (var i = 0; i < string.length; i++) 
    if (string.charAt(i) != ' ') newString += string.charAt(i);
  return newString;
}

//-------------------------------------------------------------------
// isNull(value)
//   Returns true if value is null
//-------------------------------------------------------------------
function isNull(val) {
  if (val == null) return true;
  return false;
}

//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------
function isBlank(val) {
  if (val == null) return true;
  for (var i=0; i < val.length; i++) 
    if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n")) return false;
  return true;
}

//-------------------------------------------------------------------
// isInteger(value)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isInteger(val) {
  for (var i=0; i < val.length; i++) 
    if (!isDigit(val.charAt(i))) return false;
  return true;
}

//-------------------------------------------------------------------
// isFloat(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isFloat(val) {
  var dp = false;
  for (var i=0; i < val.length; i++) {
    if (!isDigit(val.charAt(i))) { 
      if (val.charAt(i) == '.') {
        if (dp == true) { return false; } // already saw a decimal point
        else { dp = true; }
      }
      else return false; 
    } // end if
    } // end for
  return true;
}

//-------------------------------------------------------------------
// isReal(value)
//   Returns true if value is an integer or a floating point number
function isReal(value) {
  if (isInteger(value) || isFloat(value)) return true;
  else return false;
}
  
//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num) {
  var string="1234567890";
  if (string.indexOf(num) != -1) return true;
  return false;
}

//--------------------------------------------------------------------
// isUnderlen(value, len)
//   Returns true if string value is not longer than "len"
//   with optional alert if false
//--------------------------------------------------------------------
function isUnderLen(val, maxlen) {
  var msg;
  val = val+"";
  if (val.length <= maxlen) return true;
  else {
    if (!isBlank(msg)) alert(msg);
    return false;
  }
}


//-------------------------------------------------------------------
// isMonth(string)
//   Returns true if string is either a full month name or a month
//   abbreviation.
//-------------------------------------------------------------------
function isMonth(val) {
  val = val+"";
  val = val.toLowerCase();
  if ((val=="jan") || (val=="feb") || (val=="mar") || (val=="apr") || (val=="may") || (val=="jun") ||
      (val=="jul") || (val=="aug") || (val=="sep") || (val=="oct") || (val=="nov") || (val=="dec")) {
      return true;
  }
  if ((val=="january") || (val=="february") || (val=="march") || (val=="april") || (val=="may") ||
      (val=="june") || (val=="july") || (val=="august") || (val=="september") || (val=="october") ||
      (val=="november") || (val=="december")) {
        return true;
  }
  return false;
}

//-------------------------------------------------------------------
// isStateAbbr(string)
//   Returns true if string is a US state abbreviation or one of 
//   the canadian provinces
//-------------------------------------------------------------------
function isStateAbbr(val) {
  if (val.length != 2) { return false; }
  val = val+"";
  if (val.charAt(0) == ' ' || val.charAt(1) == ' ') { return false; }
  if (isUSStateAbbr(val)) { return true; }
  if (isCanadianStateAbbr(val)) { return true; }
  return false;
}

//-------------------------------------------------------------------
// isUSStateAbbr(string)
//   Returns true if string is a US State Abbreviation
//-------------------------------------------------------------------
function isUSStateAbbr(val) {
  val = val+"";
        if (val.length != 2) { return false; }
        if (val.charAt(0) == ' ' || val.charAt(1) == ' ') { return false; }
  var string="AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA PR RI SC SD TN TX UT VA VI VT WA WI WV WY";
  if (string.indexOf(val.toUpperCase()) != -1) {
    return true;
  }
  return false;
}

//-------------------------------------------------------------------
// isCanadianStateAbbr(string)
//   Returns true if string is a Canadian State (Province) Abbreviation
//------------------------------------------------------------------
function isCanadianStateAbbr(val) {
  val = val+"";
        if (val.length != 2) { return false; }
        if (val.charAt(0) == ' ' || val.charAt(1) == ' ') { return false; }
  var string="AB BC EI MB NB NF NS NT NU ON PQ SK YK";
  if (string.indexOf(val.toUpperCase()) != -1) {
    return true;
  }
  return false;
}

//-------------------------------------------------------------------
// setNullIfBlank(input_object)
//   Sets a form field to "" if it isBlank()
//-------------------------------------------------------------------
function setNullIfBlank(obj) {
  if (isBlank(obj.value)) {
    obj.value = "";
  }
}

//-------------------------------------------------------------------
// setFieldsToUpperCase(input_object)
//   Sets value of form field toUpperCase() for all fields passed
//-------------------------------------------------------------------
function setFieldsToUpperCase() {
  for (var i=0; i<arguments.length; i++) {
    var obj = arguments[i];
    obj.value = obj.value.toUpperCase();
  }
}

//-------------------------------------------------------------------
// disallowBlank(input_object[,message[,true]])
//   Checks a form field for a blank value. Optionally alerts if 
//   blank and focuses
//-------------------------------------------------------------------
function disallowBlank(obj) {
  var msg;
  var dofocus;
  if (arguments.length>1) { msg = arguments[1]; }
  if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
  if (isBlank(obj.value)) {
    if (!isBlank(msg)) alert(msg);
    if (dofocus) {
      obj.select();
      obj.focus();
    }
    return true;
  }
  return false;
}

//-------------------------------------------------------------------
// disallowModify(input_object[,message[,true]])
//   Checks a form field for a value different than defaultValue. 
//   Optionally alerts and focuses
//-------------------------------------------------------------------
function disallowModify(obj) {
  var msg;
  var dofocus;
  if (arguments.length>1) { msg = arguments[1]; }
  if (arguments.length>2) { dofocus = arguments[2]; } else { dofocus=false; }
  if (getInputValue(obj) != getInputDefaultValue(obj)) {
    if (!isBlank(msg)) alert(msg);
    if (dofocus) {
      obj.select();
      obj.focus();
    }
    setInputValue(obj,getInputDefaultValue(obj));
    return true;
  }
  return false;
}

//-------------------------------------------------------------------
// isChanged(input_object)
//   Returns true if input object's state has changed since it was
//   created.
//-------------------------------------------------------------------
function isChanged(obj) {
  if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
    for (var i=0; i<obj.length; i++) {
      if (obj[i].checked != obj[i].defaultChecked) { return true; }
    }
    return false;
  }
  if ((obj.type=="text") || (obj.type=="hidden") || (obj.type=="textarea"))
    return (obj.value != obj.defaultValue);
  if (obj.type=="checkbox")
    return (obj.checked != obj.defaultChecked);
  if (obj.type=="select-one") { 
    if (obj.options.length > 0) {
      var x=0;
      for (var i=0; i<obj.options.length; i++) {
        if (obj.options[i].defaultSelected) { x++; }
      }
      if (x==0 && obj.selectedIndex==0) return false;
      for (var i=0; i<obj.options.length; i++) {
        if (obj.options[i].selected != obj.options[i].defaultSelected) {
          return true;
    }}}
    return false;
  }
  if (obj.type=="select-multiple") {
    if (obj.options.length > 0) {
      for (var i=0; i<obj.options.length; i++) {
        if (obj.options[i].selected != obj.options[i].defaultSelected) {
          return true;
    }}}
    return false;
  }
  // return false for all other input types (button, image, etc)
  return false;
}

//-------------------------------------------------------------------
// getInputValue(input_object)
//   Get the value of any form input field
//   Multiple-select fields are returned as comma-separated values
//   (Doesn't support input types: button,file,password,reset,submit)
//-------------------------------------------------------------------
function getInputValue(obj) {
  if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
    for (var i=0; i<obj.length; i++) {
      if (obj[i].checked == true) { return obj[i].value; }
    }
    return "";
  }
  if (obj.type=="text") return obj.value;
  if (obj.type=="hidden") return obj.value;
  if (obj.type=="textarea") return obj.value;
  if (obj.type=="checkbox") {
    if (obj.checked == true) {
      return obj.value;
    }
    return "";
  }
  if (obj.type=="select-one") { 
    if (obj.options.length > 0) {
      return obj.options[obj.selectedIndex].value; 
    }
    else {
      return "";
  }}
  if (obj.type=="select-multiple") { 
    var val = "";
    for (var i=0; i<obj.options.length; i++) {
      if (obj.options[i].selected) {
        val = val + "" + obj.options[i].value + ",";
    }}
    if (val.length > 0) {
      val = val.substring(0,val.length-1); // remove trailing comma
      }
    return val;
    }
  return "";
}

//-------------------------------------------------------------------
// getInputDefaultValue(input_object)
//   Get the default value of any form input field when it was created
//   Multiple-select fields are returned as comma-separated values
//   (Doesn't support input types: button,file,password,reset,submit)
//-------------------------------------------------------------------
function getInputDefaultValue(obj) {
  if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
    for (var i=0; i<obj.length; i++) {
      if (obj[i].defaultChecked == true) { return obj[i].value; }
    }
    return "";
  }
  if (obj.type=="text") return obj.defaultValue;
  if (obj.type=="hidden") return obj.defaultValue;
  if (obj.type=="textarea") return obj.defaultValue;
  if (obj.type=="checkbox") {
    if (obj.defaultChecked == true) {
      return obj.value;
    }
    return "";
  }
  if (obj.type=="select-one") {
    if (obj.options.length > 0) {
      for (var i=0; i<obj.options.length; i++) {
        if (obj.options[i].defaultSelected) {
          return obj.options[i].value;
    }}}
    return "";
  }
  if (obj.type=="select-multiple") { 
    var val = "";
    for (var i=0; i<obj.options.length; i++) {
      if (obj.options[i].defaultSelected) {
        val = val + "" + obj.options[i].value + ",";
    }}
    if (val.length > 0) {
      val = val.substring(0,val.length-1); // remove trailing comma
    }
    return val;
  }
  return "";
}
  
//-------------------------------------------------------------------
// setInputValue()
//   Set the value of any form field. In cases where no matching value
//   is available (select, radio, etc) then no option will be selected
//   (Doesn't support input types: button,file,password,reset,submit)
//-------------------------------------------------------------------
function setInputValue(obj,val) {
  if ((typeof obj.type != "string") && (obj.length > 0) && (obj[0] != null) && (obj[0].type=="radio")) {
    for (var i=0; i<obj.length; i++) {
      if (obj[i].value == val) { 
        obj[i].checked = true;
      }
      else {
        obj[i].checked = false;
  }}}
  if (obj.type=="text") obj.value = val;
  if (obj.type=="hidden") obj.value = val;
  if (obj.type=="textarea") obj.value = val;
  if (obj.type=="checkbox") {
    if (obj.value == val) obj.checked = true;
    else obj.checked = false;
  }
  if ((obj.type=="select-one") || (obj.type=="select-multiple")) {
    for (var i=0; i<obj.options.length; i++) {
      if (obj.options[i].value == val) {
        obj.options[i].selected = true;
      }
      else {
        obj.options[i].selected = false;
}}}}
  
//-------------------------------------------------------------------
// isFormModified(form_object,hidden_fields,ignore_fields)
//   Check to see if anything in a form has been changed. By default
//   it will check all visible form elements and ignore all hidden 
//   fields. 
//   You can pass a comma-separated list of field names to check in
//   addition to visible fields (for hiddens, etc).
//   You can also pass a comma-separated list of field names to be
//   ignored in the check.
//-------------------------------------------------------------------
function isFormModified(theform, hidden_fields, ignore_fields) {
  if (hidden_fields == null) { hidden_fields = ""; }
  if (ignore_fields == null) { ignore_fields = ""; }
  
  var hiddenFields = new Object();
  var ignoreFields = new Object();
  var i,field;
  
  var hidden_fields_array = hidden_fields.split(',');
  for (i=0; i<hidden_fields_array.length; i++) {
    hiddenFields[Trim(hidden_fields_array[i])] = true;
    }
  var ignore_fields_array = ignore_fields.split(',');
  for (i=0; i<ignore_fields_array.length; i++) {
    ignoreFields[Trim(ignore_fields_array[i])] = true;
    }
  for (i=0; i<theform.elements.length; i++) {
    var changed = false;
    var name = theform.elements[i].name;
    if (!isBlank(name)) {
      var type = theform[name].type;
      if (!ignoreFields[name]) {
        if (type=="hidden" && hiddenFields[name]) {
          changed = isChanged(theform[name]);
        }
        else if (type=="hidden") {
          changed = false;
        }
        else {
          changed = isChanged(theform[name]);
    }}}
    if (changed) { 
      return true;
  }}
  return false;
}

// Form Validation Functions  v1.0
// http://www.dithered.com/javascript/form_validation/index.html
// code by Chris Nott (chris@dithered.com)

// Check that the number of characters in a string is between a max and a min
function isValidLength(string, min, max) {
  if (string.length < min || string.length > max) return false;
  else return true;
}

// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)
function isValidCreditCard(number) {
  number = '' + number;
  if (number.length > 16 || number.length < 13 ) return false;
  else if (getMod10(number) != 0) return false;
  else if (arguments[1]) {
    var type = arguments[1];
    var first2digits = number.substring(0, 2);
    var first4digits = number.substring(0, 4);
    if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&
      (number.length == 16 || number.length == 13 )) return true;
    else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&
      (first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;
    else if (type.toLowerCase() == 'american express' && number.length == 15 && 
      (first2digits == '34' || first2digits == '37')) return true;
    else if (type.toLowerCase() == 'diners club' && number.length == 14 && 
      (first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;
    else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;
    else if (type.toLowerCase() == 'enroute' && number.length == 15 && 
      (first4digits == '2014' || first4digits == '2149')) return true;
    else if (type.toLowerCase() == 'jcb' && number.length == 16 &&
      (first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;
    else return true;
  }
  else return true;
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
  if (address.indexOf('@') < 3) return false;
  var name = address.substring(0, address.indexOf('@'));
  var domain = address.substring(address.indexOf('@') + 1);
  if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;
  if (domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;
  return true;
}

// Check that a US zip code is valid
function isValidZipcode(zipcode) {
  zipcode = removeSpaces(zipcode);
  if (!(zipcode.length == 5) || !isNumeric(zipcode)) return false;
  return true;
}

// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
  if (postalcode.search) {
    postalcode = removeSpaces(postalcode);
    if (postalcode.length == 6 && postalcode.search(/^\w\d\w\d\w\d$/) != -1) return true;
    else if (postalcode.length == 7 && postalcode.search(/^\w\d\w\-d\w\d$/) != -1) return true;
    else return false;
  }
  return true;
}

// Check that a string contains only letters and specific punctuation (period, apostrophe, hyphen)
function isAlphapunc(string, ignoreWhiteSpace) {
  if (string.search) {
    // Not TESTED!
    if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s\.'\-]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z\.'\-]/) != -1)) return false;
  }
  return true;
}

// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
  if (string.search) {
    if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
  }
  return true;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
  if (string.search) {
    if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
  }
  return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
  if (string.search) {
    if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
  }
  return true;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
  var newString  = '';
  var substring  = '';
  beginningFound = false;
  // copy characters over to a new string
  // retain whitespace characters if they are between other characters
  for (var i = 0; i < string.length; i++) {
    // copy non-whitespace characters
    if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
      // if the temporary string contains some whitespace characters, copy them first
      if (substring != '') {
        newString += substring;
        substring = '';
      }
      newString += string.charAt(i);
      if (beginningFound == false) beginningFound = true;
    }
    // hold whitespace characters in a temporary string if they follow a non-whitespace character
    else if (beginningFound == true) substring += string.charAt(i);
  }
  return newString;
}

// Returns a checksum digit for a number using mod 10
function getMod10(number) {
    // convert number to a string and check that it contains only digits
    // return -1 for illegal input
    number = '' + number;
    number = removeSpaces(number);
    if (!isNumeric(number)) return -1;
    // calculate checksum using mod10
    var checksum = 0;
    for (var i = number.length - 1; i >= 0; i--) {
        var isOdd = ((number.length - i) % 2 != 0) ? true : false;
        digit = number.charAt(i);
        if (isOdd) checksum += parseInt(digit);
        else {
            var evenDigit = parseInt(digit) * 2;
            if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);
            else checksum += evenDigit;
        }
    }
    return (checksum % 10);
}

//var PatDict = new Object();
//PatDict.zipPat      = /\d{5}(-\d{4})?/;             // matches zip codes
//PatDict.currencyPat = /\$\d{1,3}(,\d{3})*\.\d{2}/;  // matches $17.23 or $14,281,545.45 or ...
//PatDict.timePat     = /\d{2}:\d{2}/;                // matches 12:34 but also 75:83
//PatDict.timePat2    =/^([1-9]|1[0-2]):[0-5]\d$/;    // matches 5:04 or 12:34 but not 75:83

// V A L I D A T E F O R M
//
// Any Form Input field can have a 'validate' value.
//
// It 3 parts seperated by slashes '/'.
//
//    Part 1: required or not - 'req' = required 'not' or missing = NOT required
//
//    Part 2: field type validation:  'num'                 =   numeric  (integer)             
//                                    'float'           =   floating point  (or integer)
//                                    'alpha'           =   alphabetic
//                                    'alphanum'        =   mixed alpnanumeric or numeric 
//                                    'alphapunc'   =   alpna plus period OK (special for initials?) 
//                                    'CC'                  =   Credit Card Number                   
//                                    'email'           =   legal email address  
//                                    'zip'                 =   US Zip Code                         
//                                    'CPost'           =   Canadian Postal Code'
//                                    'zipost'          =   zip or postal
//
//    Part 3: string to be displayed in an alert box of validation fails
//
//
function validateForm(theForm){      // return true if all is well
  var elArr = theForm.elements;      // get all elements of the form into array
  for(var i = 0; i < elArr.length; i++) with(elArr[i]){ // for each element of the form...
    var v = elArr[i].validator;      // get validator, if any
    if(!v) v = elArr[i].VALIDATOR;   // make it case insensative
    if(!v) continue;
    vFields = v.split('/');          // parse validator fields
    fReq = vFields[0]; fVal = vFields[1]; fErMsg = vFields[2];
    if((fReq == 'req') && ((value == '0') || (value == ''))) {
      alert("Please complete all required fields.");
      elArr[i].focus();
      return false;
    }                       
    if(!fVal) continue;              // no validator property, skip
    if(fVal == 'num') {
      if(!isInteger(value)) {
        if(!fErMsg) alert('Field can only contain numbers.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    if(fVal == 'float') {
      if(!isFloat(value)) {
        if(!fErMsg) alert('Field can only contain numbers and optional "."');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'alpha') {
      if(!isAlphabetic(value)) {
        if(!fErMsg) alert('Field can only contain letters.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'alphanum') {
      if(!isAlphanumeric(value)) {
        if(!fErMsg) alert('Field can only contain letters and numbers.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'alphapunc') {
      if(!isAlphapunc(value, true )) {
        if(!fErMsg) alert('Field can only contain letters and punctuation.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'CC') {
      if(!isValidCreditCard(removeSpaces(value))) {
        if(!fErMsg) alert('Not Valid Credit Card Number.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'email') {
      if(!isValidEmail(value)) {
        if(!fErMsg) alert('Not Valid Email Address.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'zip') {
      if(!isValidZipcode(value)) {
        if(!fErMsg) alert('Not Valid Zip Code.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'CPost') {
      if(!isValidPostalcode(value)) {
        if(!fErMsg) alert('Not Valid Canadian Postal Code.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }   
    else if(fVal == 'zipost') {
      if(!isValidZipcode(value) && !isValidPostalcode(value)) {
        if(!fErMsg) alert('Not Valid Zip/Postal Code.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } } 
    else if(fVal == 'varchar') {
      if(!isUnderLen(value, 255)) {
        if(!fErMsg) alert('Not Valid VarChar field: length is limited to 255 characters.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } } 
    else if(fVal == 'numornone') {
//      if(!isReal(value) && (value != 'NULL')) { ???????????????????????????????????
      if(!isReal(value) && (value != 'None') && (value != 'NULL')) {
        if(!fErMsg) alert('Not a Valid entry for this field.');
        else alert(fErMsg);
        elArr[i].focus();
        return false;
    } }
  } // for  
  return true;
}
//================================================================================
//================================================================================
//================================================================================
//  YET MORE CREDIT CARD STUFF

    function cc_ok(frm,cc_req) {
      //=============================================
      // CREDIT CARD CHECKING========================
      // 
      var good = true;
      if (cc_req) {
        if ((frm.cc_num.value == '') || (frm.cc_num.value == '0')) {
          good = false;
          alert("Credit Card Required.");
        }
        else if ((frm.cc_type.value == '') || (frm.cc_type.value == '0')) {
          good = false;
          alert("Please Select Card Type.");
        }
        else if (frm.cc_exp_year.value.length < 2) {
          good = false;
          alert("Please Select Card Expiration Year.");
        }
        else if ((frm.cc_exp_month.value == '') || (frm.cc_exp_month.value == '0')) {
          good = false;
          alert("Please Select Card Expiration Month.");
        }
        else if (!CheckCardNumber(frm)) {
          good = false; 
          // Alert errror message supplied by CheckCardNumber function
        }
      }
      return good;
    }





var Cards = new makeArray(8);

Cards[0]            = new CardType("MasterCard", "51,52,53,54,55", "16");
var MasterCard      = Cards[0];

Cards[1]            = new CardType("VisaCard", "4", "13,16");
var VisaCard        = Cards[1];

Cards[2]            = new CardType("AmExCard", "34,37", "15");
var AmExCard        = Cards[2];

Cards[3]            = new CardType("DinersClubCard", "30,36,38", "14");
var DinersClubCard  = Cards[3];

Cards[4]            = new CardType("DiscoverCard", "6011", "16");
var DiscoverCard    = Cards[4];

Cards[5]            = new CardType("enRouteCard", "2014,2149", "15");
var enRouteCard     = Cards[5];

Cards[6]            = new CardType("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard         = Cards[6];

var LuhnCheckSum = Cards[7] = new CardType();

/*************************************************************************\
CheckCardNumber(form)
function called when users click the "check" button.
\*************************************************************************/
function CheckCardNumber(form) {
  var tmpyear;
  if (form.cc_num.value.length == 0) {
//    alert("Please enter a Card Number.");
//    form.cc_num.focus();
    return true;
  }
//alert('exp year = |'+form.cc_exp_year.value+'| len = |'+form.cc_exp_year.value.length);
  if (form.cc_exp_year.value.length < 2) {
    alert("Please enter the Expiration Year.");
    form.cc_exp_year.focus();
    return false;
  }
  tmpmonth = form.cc_exp_month.options[form.cc_exp_month.selectedIndex].value;
  tmpyear  = form.cc_exp_year.options[form.cc_exp_year.selectedIndex].value;
  // The following line doesn't work in IE3, you need to change it
  // to something like "(new CardType())...".
  // if (!CardType().isExpiryDate(tmpyear, tmpmonth)) {
//alert("y="+tmpyear+" / m="+tmpmonth);
  if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
    alert("This card has already expired.");
    return false;
  }
// alert(form.cc_num.value);
  form.cc_num.value = removeSpaces(form.cc_num.value);
// alert(form.cc_num.value);
  card = form.cc_type.options[form.cc_type.selectedIndex].value;
    switch (card) {
      case '1':
          card = "VisaCard";
            break;
      case '2':
          card = "MasterCard";
            break;
      case '3':
          card = "AmExCard";
            break;
      case '4':
          card = "DiscoverCard";
            break;
  } // end switch
// alert('aa');
  var retvaltext = (card + ".checkCardNumber(\"" + form.cc_num.value +
                "\", " + tmpyear + ", " + tmpmonth + ");");
// alert(retvaltext);                               
  var retval = eval(card + ".checkCardNumber(\"" + form.cc_num.value +
                "\", " + tmpyear + ", " + tmpmonth + ");");
// alert('bb');
// alert('retval = |'+retval+'|');
  cardname = "";
// alert('cc');
  if (retval) {
    // comment this out if used on an order form
    //    alert("This card number appears to be valid.");
// alert('dd');
    return true;
    }   
  else {
// alert('ee');
    // The cardnumber has the valid luhn checksum, but we want to know which
    // cardtype it belongs to.
    for (var n = 0; n < Cards.size; n++) {
      if (Cards[n].checkCardNumber(form.cc_num.value, tmpyear, tmpmonth)) {
        cardname = Cards[n].getCardType();
        break;
      }
    }
    if (cardname.length > 0) {
      alert("This looks like a " + cardname + " number, not a " + card + " number. Please correct!");
    }
    else {
      alert("This card number is not valid.");
    }
  }
  return false;
}

/*************************************************************************\
Object CardType([String cardtype, String rules, String len, int year, 
                                        int month])
cardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new CardType("Visa", "4", "16");
var AmExCard = new CardType("AmEx", "34,37", "15");
\*************************************************************************/
function CardType() {
  var n;
  var argv = CardType.arguments;
  var argc = CardType.arguments.length;
  
  this.objname = "object CardType";

  var tmpcardtype   = (argc > 0) ? argv[0] : "CardObject";
  var tmprules      = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
  var tmplen        = (argc > 2) ? argv[2] : "13,14,15,16,19";

  this.setCardNumber    = setCardNumber;  // set CardNumber method.
  this.setCardType      = setCardType;    // setCardType method.
  this.setLen           = setLen;         // setLen method.
  this.setRules         = setRules;       // setRules method.
  this.setExpiryDate    = setExpiryDate;  // setExpiryDate method.

  this.setCardType(tmpcardtype);
  this.setLen(tmplen);
  this.setRules(tmprules);
  if (argc > 4)
    this.setExpiryDate(argv[3], argv[4]);

  this.checkCardNumber  = checkCardNumber;  // checkCardNumber method.
  this.getExpiryDate    = getExpiryDate;    // getExpiryDate method.
  this.getCardType      = getCardType;      // getCardType method.
  this.isCardNumber     = isCardNumber;     // isCardNumber method.
  this.isExpiryDate     = isExpiryDate;     // isExpiryDate method.
  this.luhnCheck        = luhnCheck;        // luhnCheck method.
  return this;
}

/*************************************************************************\
boolean checkCardNumber([String cardnumber, int year, int month])
return true if cardnumber pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkCardNumber() {
// alert('checkCardNumber');
  var argv = checkCardNumber.arguments;
  var argc = checkCardNumber.arguments.length;
  var cardnumber = (argc > 0) ? argv[0] : this.cc_num;
  var year = (argc > 1) ? argv[1] : this.year;
  var month = (argc > 2) ? argv[2] : this.month;

  this.setCardNumber(cardnumber);
  this.setExpiryDate(year, month);

  if (!this.isCardNumber())
    return false;
  if (!this.isExpiryDate())
    return false;

  return true;
}
/*************************************************************************\
String getCardType()
return the cardtype.
\*************************************************************************/
function getCardType() {
  return this.cc_type;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
  return this.month + "/" + this.year;
}
/*************************************************************************\
boolean isCardNumber([String cardnumber])
return true if cardnumber pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function isCardNumber() {
  var argv = isCardNumber.arguments;
  var argc = isCardNumber.arguments.length;
  var cardnumber = (argc > 0) ? argv[0] : this.cc_num;
  if (!this.luhnCheck()) {
// alert('isCardNumber A');
    return false;
  }

  for (var n = 0; n < this.len.size; n++)
    if (cardnumber.toString().length == this.len[n]) {
// alert('isCardNumber B');
      for (var m = 0; m < this.rules.size; m++) {
        var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
        if (headdigit == this.rules[m]) {
// alert('isCardNumber C');
          return true;
        } 
      }
// alert('isCardNumber D');
      return false;
    }
// alert('isCardNumber E');
  return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
  var argv = isExpiryDate.arguments;
  var argc = isExpiryDate.arguments.length;

  year = argc > 0 ? argv[0] : this.year;
  month = argc > 1 ? argv[1] : this.month;
  
  if (!isNum(year+""))
    return false; 
  if (!isNum(month+""))
    return false;
  today = new Date();
  expiry = new Date(year, month);
  if (today.getTime() > expiry.getTime())
    return false;
  else
    return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
  argvalue = argvalue.toString();

  if (argvalue.length == 0)
    return false;

  for (var n = 0; n < argvalue.length; n++)
    if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
      return false;

  return true;
}

/*************************************************************************\
boolean luhnCheck([String CardNumber])
return true if CardNumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
  var argv = luhnCheck.arguments;
  var argc = luhnCheck.arguments.length;

  var CardNumber = argc > 0 ? argv[0] : this.cc_num;

  if (! isNum(CardNumber)) {
    return false;
  }

  var no_digit = CardNumber.length;
  var oddoeven = no_digit & 1;
  var sum = 0;

  for (var count = 0; count < no_digit; count++) {
    var digit = parseInt(CardNumber.charAt(count));
    if (!((count & 1) ^ oddoeven)) {
      digit *= 2;
      if (digit > 9)
      digit -= 9;
    }
    sum += digit;
  }
  if (sum % 10 == 0)
    return true;
  else
    return false;
}

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
  this.size = size;
return this;
}

/*************************************************************************\
CardType setCardNumber(cardnumber)
return the CardType object.
\*************************************************************************/
function setCardNumber(cardnumber) {
  this.cc_num = cardnumber;
return this;
}

/*************************************************************************\
CardType setCardType(cardtype)
return the CardType object.
\*************************************************************************/
function setCardType(cardtype) {
  this.cc_type = cardtype;
return this;
}

/*************************************************************************\
CardType setExpiryDate(year, month)
return the CardType object.
\*************************************************************************/
function setExpiryDate(year, month) {
  this.year = year;
  this.month = month;
  return this;
}

/*************************************************************************\
CardType setLen(len)
return the CardType object.
\*************************************************************************/
function setLen(len) {
  // Create the len array.
  if (len.length == 0 || len == null)
    len = "13,14,15,16,19";

  var tmplen = len;
  n = 1;
  while (tmplen.indexOf(",") != -1) {
    tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
    n++;
  }
  this.len = new makeArray(n);
  n = 0;
  while (len.indexOf(",") != -1) {
    var tmpstr = len.substring(0, len.indexOf(","));
    this.len[n] = tmpstr;
    len = len.substring(len.indexOf(",") + 1, len.length);
    n++;
  }
  this.len[n] = len;
  return this;
}

/*************************************************************************\
CardType setRules()
return the CardType object.
\*************************************************************************/
function setRules(rules) {
  // Create the rules array.
  if (rules.length == 0 || rules == null)
    rules = "0,1,2,3,4,5,6,7,8,9";
  
  var tmprules = rules;
  n = 1;
  while (tmprules.indexOf(",") != -1) {
    tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
    n++;
  }
  this.rules = new makeArray(n);
  n = 0;
  while (rules.indexOf(",") != -1) {
    var tmpstr = rules.substring(0, rules.indexOf(","));
    this.rules[n] = tmpstr;
    rules = rules.substring(rules.indexOf(",") + 1, rules.length);
    n++;
  }
  this.rules[n] = rules;
  return this;
}
