<!--
function validateForm() {
  // This function check the field inputs in the form.
  // The arguments should be listed as following:
  // field name for the first field ,field length for the first field, Required field or not(specified by 'R' if required), and so on
  var i,len,test,fieldObj,fieldName,fieldVal,p,min,max,errors='',args=validateForm.arguments;
  var re=new RegExp (' +', 'gi');
  for (i=0; i<(args.length-2); i+=3) {
    test=args[i+2];
    
    if (test.indexOf('isZip')!=-1) errors+= zipCheck(args[i]);
    if (test.indexOf('isEmail')!=-1) errors+= emailCheck(args[i]);
    if (test.indexOf('isPhone')!=-1) errors+= phoneCheck(args[i]);
    if (test.indexOf('isPassword')!=-1) errors+= passwordCheck(args[i]);
    if (test.indexOf('isInteger')!=-1) errors+= integerCheck(args[i])

    fieldObj=findObj(args[i]);
    if (fieldObj) {
      // fieldObj is a form field object
      
      fieldVal = fieldObj.value;
      
      // Replace more than one spaces with only one space
      fieldVal=fieldVal.replace(re,' ');
      
      // Now on both sides there should have no more than one space. If have one, remove them
      if (fieldVal.charAt(0)==' ') fieldVal=fieldVal.substring(1,fieldVal.length);
      if (fieldVal.charAt(fieldVal.length-1)==' ') fieldVal=fieldVal.substring(0,fieldVal.length-1);
      
      fieldName=fieldObj.name;
      
      if (fieldVal!='') {
        // Check the input length
        len=args[i+1];
        if (len!='') {
          fieldLen = fieldVal.length;
          if (fieldLen>len) errors+='- '+fieldName+' must contain no more than '+len+' characters.\n';
        }
      } else if (test.charAt(0) == 'R') errors += '- '+fieldName+' is required.\n';
    // Reassign the form field
    fieldObj.value=fieldVal;
    }
  }
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.formValidationreturnValue = (errors == '');
}


function zipCheck(fieldName) {
  // This function check if the user entered valid zip code in the field whose name is specified by the first arguent.
    
  var fieldObj, fieldVal,re
  fieldObj = findObj(fieldName);
  if (fieldObj) {
    // Find the field object
    // Check the field value
    
    fieldVal=fieldObj.value;
    
    // Remove all the spaces
    re = new RegExp (' +', 'gi') ;
    fieldVal=fieldVal.replace(re,'');
    
    if (fieldVal.length>8) fieldVal = fieldVal.substr(0,5) + '-' + fieldVal.substr(fieldVal.length-4,4);
    
    // Reassign the form field
    fieldObj.value=fieldVal;
    
    if (fieldVal != '') {
      // The forms of zip code are: 20016, or 20016-2756
      reZip=/^\d{5}(-\d{4})?$/;
      if (!fieldVal.match(reZip)) return ('- '+fieldName+' must contain a valid zip code.\n')
    } 
  }
  return ('');      
}


function emailCheck(fieldName) {
  // This function check if the user entered valid email in the field whose name is specified by the arguent.
    
  var fieldObj, fieldVal,re, reEmail
  fieldObj = findObj(fieldName);
  if (fieldObj) {
    // Find the field object
    // Check the field value
    
    fieldVal=fieldObj.value;
    
    // Remove all the spaces
    re = new RegExp (' +', 'gi') ;
    fieldVal=fieldVal.replace(re,'');
    
    // Reassign the form field
    fieldObj.value=fieldVal;
    
    if (fieldVal != '') {
      // \w: Matches any word character including underscore. Equivalent to '[A-Za-z0-9_]'.
      reEmail=/^\w[\w-]*(\.[\w-]+)*@([\w-]+\.)+\w+$/i; //reEmail = new RegExp('^\w+@(\w+.)+\w+$', 'i') does not work
      if (!fieldVal.match(reEmail)) return ('- '+fieldName+' must contain a valid email address.\n')
    }
  }
  return ('');      
}


function phoneCheck(fieldName) {
  // This function check if the user entered valid phone number in the field whose name is specified by the first arguent.
    
  var fieldObj, fieldVal,re,rePhone
  fieldObj = findObj(fieldName);

  if (fieldObj) {
    // Find the field object
    // Check the field value
    
    fieldVal=fieldObj.value;
    
    // Remove all the non-digit characters
    re = /\D+/g;
    fieldVal=fieldVal.replace(re,'');

    // Reassign the form field
    fieldObj.value = fieldVal;  
    
    if (fieldVal !='') {
      // Check the phone number
      
      if (fieldVal.length!=10) return ('- '+fieldName+' must be a valid phone number if you enter.\n')
        
      // Reassign the form field
      // rePhone = /^\(\d{3}\) \d{4}-\d{3}$/
      fieldObj.value = '(' + fieldVal.substr(0,3) + ') ' + fieldVal.substr(3,4) + '-' + fieldVal.substr(7,3);
    }  
  }
  return ('');      
}


function integerCheck(fieldName) {
  // This function check if the user entered valid integer in the field whose name is specified by the first arguent.
    
  var fieldObj, fieldVal,re, reEmail
  fieldObj = findObj(fieldName);
  if (fieldObj) {
    // Find the field object
    // Check the field value
    
    fieldVal=fieldObj.value;
    
    // Remove all the spaces
    re = new RegExp (' +', 'gi') ;
    fieldVal=fieldVal.replace(re,'');
    
    // Reassign the form field
    fieldObj.value=fieldVal;
    
    if (fieldVal != '') {
      reInt=/^\d+$/;
      if (!fieldVal.match(reInt)) return ('- '+fieldName+' must contain an integer number.\n')
    }
  }
  return ('');      
}


function amountCheck(fieldName, minAmount, decimalN) {
  // This function check if the user entered correct amount number in the field whose name is specified by the first arguent.
  // This number should not be less than the number specified by the second argument.
  // If the number has decimal places, then the number of these places should not be more than the number specified by the third argument.
  
  var fieldObj, fieldVal,re,val,i;
  fieldObj = findObj(fieldName);
  if (fieldObj) {
    // Find the field object
    // Check the field value
    
    fieldVal=fieldObj.value;
    
    // Remove all the spaces
    re = new RegExp (' +', 'gi') ;
    fieldVal=fieldVal.replace(re,'');
    
    // Reassign the form field
    fieldObj.value=fieldVal;
    
    i=fieldVal.indexOf('.');
    if (i!=-1 && (fieldVal.length-1-i)>decimalN) {
      alert('Please do not fill more than ' +decimalN+ ' decimal places.')
      fieldObj.select();
      fieldObj.focus();
      return false;
    }
    val = Number(fieldVal);
    if (isNaN(val)) {
      // Input is not a number
      alert('Please enter only dollar amounts.');
      fieldObj.select();
      fieldObj.focus();
      return false;
    } 
    if (val<minAmount) {
      alert('Plase note:\n'+'Due to collection and processing costs,\n'+'donations less than $' + minAmount + ' cannot be accepted.');
      fieldObj.select();
      fieldObj.focus();
      return false;
    }   
  }
  return true;      
}


function passwordCheck(fieldName) {
  // This function check if the user entered valid password in the field whose name is specified by the arguent.
    
  var fieldObj, fieldVal,rePass
  fieldObj = findObj(fieldName);
  if (fieldObj) {
    // Find the field object
    // Check the field value
    
    fieldVal=fieldObj.value;
    
    if (fieldVal != '') {
      // \w: Matches any word character including underscore. Equivalent to '[A-Za-z0-9_]'.
      rePass=/^\w{6,10}$/;
      if (!fieldVal.match(rePass)) return ('- '+fieldName+' must contain a valid password.\n')
    }
  }
  return ('');      
}


function findObj(n, d) {
  // This function find the object named by the first argument in the document object specified by the second object.
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;
    n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);
  return x;
}
//-->
