<!-- BEGIN
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function checkNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);


  /* Check if the keyed in character is a number
     do you want alphabetic UPPERCASE only ?
     or lower case only just check their respective
     codes and replace the 48 and 57 */

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

function isEmpty(str){
  var r = false;
// alert("IN isEmpty" + str);
  if((str == null) || (str.length == 0))
    r = true;
//alert("IN isEmpty. r = " + r);
  return r;
}

// returns true if the string is a valid email
function isEmail(str){
  if(isEmpty(str)) return false;
  var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
  return re.test(str);
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
  var re = /[^a-zA-Z]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string only contains characters 0-9
function isNumeric(str){
  var re = /[\D]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
  var re = /[^a-zA-Z0-9[:blank:]]/g
  if (re.test(str)) return false;
  return true;
}
// returns true if the string's length equals "len"
function isLength(str, len){
  return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
  return (str.length >= min)&&(str.length <= max);
}
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
  var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
  return re.test(str);
}


// returns true if the string is a valid state for the country
function isValidState(st, ctry){
  var mStates = new Array ('AG','BJ','BS','CH','CI','CL','CP','CU','DF','DG','EM','GJ','GR','HG','JA','MH','MR','NA','NO','OA','PU','QA','QR','SI','SL','SO','TA','TL','TM','VZ','YC','ZT');
  var cStates = new Array ('AB','BC','MB','NB','NL','NS','NT','NU','ON','PE','QC','SK','YT');
  var uStates = new Array ('AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','GU','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MH','MI','MN','MO','MP','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','PR','PW','RI','SC','SD','TN','TX','UT','VA','VI','VT','WA','WI','WV','WY');
  var r = true;
//alert("in isValidState 2 r = " + r + " uStates = gaga" );
  if (ctry == "Mexico"){
    r = false;
    for (k=0; k<mStates.length; k++){
      if (st == mStates[k])
        r = true;
    }
  }

  else if (ctry == "United States"){
    r = false;
    for (i=0; i<uStates.length; i++){
      if (st == uStates[i])
        r = true; 
    }   
  }

  else if (ctry == "Canada"){
    r = false;
    for (j=0; j<cStates.length; j++){
      if (st == cStates[j])
        r = true;
    }
  } 
// No other country should have a state
  else if (st != "XX")
    r = false;
//alert("Leaving isValidState r = " + r + " st = " + st + " ctry = " + ctry);
  return r;
}

// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
  var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
  if (!re.test(str)) return false;
  var result = str.match(re);
  var y = parseInt(result[3]);
  var m = parseInt(result[1]);
  var d = parseInt(result[2]);
  if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
  if(m == 2){
          var days = ((y % 4) == 0) ? 29 : 28;
  }else if(m == 4 || m == 6 || m == 9 || m == 11){
          var days = 30;
  }else{
          var days = 31;
  }
  return (d >= 1 && d <= days);
}
// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
  return str1 == str2;
}
// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
  var re = /[\S]/g
  if (re.test(str)) return false;
  return true;
}
// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
  if (replacement == null) replacement = '';
  var result = str;
  var re = /\s/g
  if(str.search(re) != -1){
    result = str.replace(re, replacement);
  }
  return result;
}

// validate the form
function validateForm(f, newClass, alerttype){
  var errors = '';
  var errorsa = '';
  var i,e,t,n,v,nm,excludeNm;
  var bcntry = null;
  var scntry = null;
//alert("in validateForm");

  for(i=0; i < f.elements.length; i++){
    e = f.elements[i];
    if(e.optional) continue;
    t = e.type;
    n = e.id;
    v = e.value;
    nm = e.name;
//The following fields are NOT required
    if(nm == "BillAddress2" || nm == "ShipAddress2" || nm == "BillCompany" || nm == "ShipCompany" || nm == "BillPhone" || nm == "ShipPhone"){  
      excludeNm = true;
    }
    else excludeNm = false;

//alert("in big for loop. i = " + i + " n = " + n + " t = " + t + " v = " + v);
    if((t == 'text' || t == 'password' || t == 'textarea') && (!excludeNm)){  
// alert("in big if loop");
      if(isEmpty(v)){
        errors += n+errormsg[1]+ '<br>';
//        errors += n+'<br>'+"OOGY";
// alert("isEmpty returned true errors = " + errors);
        errorsa += n+errormsg[1]+'\n';
        e.className=newClass;
        continue;
      }
      else {
        e.className='checkit';
      }

     
      if(e.isPhoneNumber){
        if(!isPhoneNumber(v)){
          errors += n+errormsg[9]+ '<br>';
          errorsa += n+errormsg[9]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isAlphaNumeric){
        if(!isAlphaNumeric(v)){
          errors += n+errormsg[5]+ '<br>';
          errorsa += n+errormsg[5]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isEmail){
        if(!isEmail(v)){
          errors += v+errormsg[6]+ '<br>';
          errorsa += n+errormsg[6]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isLength != null){
        var len = e.isLength;
        if(!isLength(v,len)){
          errors += n+errormsg[7]+ len + '<br>';
          errorsa += n+errormsg[7]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isLengthBetween != null){
        var min = e.isLengthBetween[0];
        var max = e.isLengthBetween[1];
        if(!isLengthBetween(v,min,max)){
          errors += n+errormsg[8] + min + '-' + max + '<br>';
          errorsa += n+errormsg[8] + min + '-' + max + '\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isPhoneNumber){
        if(!isPhoneNumber(v)){
          errors += v+errormsg[9]+ '<br>';
          errorsa += n+errormsg[9]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isDate){
        if(!isDate(v)){
          errors += v+errormsg[10]+ '<br>';
          errorsa += n+errormsg[10]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }

      if(e.isMatch != null){
        if(!isMatch(v, e.isMatch)){
          errors += n+errormsg[11]+ '<br>';
          errorsa += n+errormsg[11]+'\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }
    } // huge if statement
// alert('Made it out of huge if statement.');
// alert("bcntry = " + bcntry + " scntry = " + scntry + " nm = " + nm + " n = " + n);
// get the country name for later use
    if(t.indexOf('select') != -1){
//alert("testing indexOf != -1. Index = " + (t.indexOf('select')) + " nm = " + nm + " value = " + (e.options[e.selectedIndex].value));
      if(nm == "BillCountry"){
        bcntry = e.options[e.selectedIndex].value;
      }
      else if(nm == "ShipCountry"){
        scntry = e.options[e.selectedIndex].value;
      }

    }

    if(t.indexOf('select') != -1){
      var st_nm = e.options[e.selectedIndex].value;
      if(nm == "BillState"){
        if(!isValidState(st_nm,bcntry)){
          errors += st_nm+errormsg[14]+bcntry+'!<br>';
          errorsa += st_nm+errormsg[14]+bcntry+'!\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }
      else if(nm == "ShipState"){
        if(!isValidState(st_nm,scntry)){
          errors += st_nm+errormsg[14]+scntry+'!<br>';
          errorsa += st_nm+errormsg[14]+scntry+'!\n';
          e.className=newClass;
          continue;
        }
        else {
          e.className='checkit';
        }
      }
    }

    if(t == 'file'){
      if(isEmpty(v)){
        errors += n+errormsg[13]+'<br>';
        errorsa += n+errormsg[13]+'\n';
        e.className=newClass;
        continue;
      }
      else {
        e.className='checkit';
      }
    }
  } //for loop

 //alert("Made it out of big for loop");
  div = document.getElementById('errordiv');
//alert("hello 2 div = " + div);
//alert("errors = " + errors);
  if(errors != '') {
	  if(alerttype == '2' || alerttype == '3') {
      alert(errorsa);
      }
	  if(alerttype == '1' || alerttype == '3') {
      return dispErr(errors, div);
      }
  }
  div.style.display="none";
//alert("end of validate function. errors = " + errors);
  return errors == '';
}
/*
dispErr = function(error, divo) {
  divo.style.display="block";
  divo.innerHTML = error;
  return false;
}
*/
/*
The following elements are not validated...

button   type="button"
checkbox type="checkbox"
hidden   type="hidden"
radio    type="radio"
reset    type="reset"
submit   type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

isEmail = true;          // valid email address
isAlpha = true;          // A-Z a-z characters only
isNumeric = true;        // 0-9 characters only
isAlphaNumeric = true;   // A-Z a-z 0-9 characters only
isLength = number;       // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true;    // valid phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true;           // valid date. See "isDate()" comments for the formatting rules
isMatch = string;        // must match string
isValidState = true;     // must be a vaid state for the US, Mexico or Canada
optional = true;         // element will not be validated

alerttype = 0            // no error msg
alerttype = 1            // error msg in div
alerttype = 2            // error msg in alert
alerttype = 3            // error msg in div and alert
*/

//============================

// error msg depends on the language
var errormsg = new Array();
errormsg[0] = 'Select at least one checkbox!';
errormsg[1] = ' is required';
errormsg[2] = ' cannot use the default value!';
errormsg[3] = ' can only contain characters A-Z a-z!';
errormsg[4] = ' can only contain characters 0-9!';
errormsg[5] = ' can only contain characters A-Z a-z 0-9!';
errormsg[6] = ' is not a valid email!';
errormsg[7] = ' character number must be less than ';
errormsg[8] = ' character number must be between ';
errormsg[9] = ' is not a valid US phone number!';
errormsg[10] = ' is not a valid date!';
errormsg[11] = ' does not match!';
errormsg[12] = ' needs an option selected!';
errormsg[13] = ' needs a file to upload!';
errormsg[14] = ' is not a valid state for ';
errormsg[99] = 'All form information will be erased!';
// errormsg[100] = 'Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

// END -->
