function checkForm(form) {
   form.domain.required = true;
   form.domain.requiredError = 'You did not enter a name to be searched.';
   form.domain.numeric = true;
   form.domain.numericError = 'At least one alphabetical character must be used.';
   form.domain.validchars = true;
   form.domain.validcharsError = 'Characters allowed are [a-z], [0-9] and [-].  Domains must not start or end with a dash [-].  Do not include extensions .ph, .com.ph, .net.ph and .org.ph when searching for a domain name.';
   form.domain.maxlength = 58;
   form.domain.maxlengthError = 'Domain name must not exceed 62 characters (including the extension).';
   form.domain.minlength = 3;
   form.domain.minlengthError = 'Domain name must have at least 3 characters (not including the extension).';            
   var errors = getErrors(form);
   if (errors.length > 0) {
      var errorMessage = ((errors.length > 1) ? 's' : '');
      for (var errorIndex = 0; errorIndex < errors.length; errorIndex++) {
         errorMessage += errors[errorIndex] + '\n';
      }
      alert(errorMessage);
      return false;
   }
   // no errors: return true
   return true;
}	


function getErrors(form) {
   var errors = new Array();
   var element = form.elements[0];
   element.value = trimWhitespace(element.value)
         
   // required element
   if (element.required  && element.value == '') {
      errors[errors.length] = element.requiredError;
   }
         
   // maximum length
   else if (element.maxlength && isValidLength(element.value, 0, element.maxlength) == false) {
      errors[errors.length] = element.maxlengthError;
   }

   // minimum length
   else if (element.minlength && isValidLength(element.value, element.minlength, Number.MAX_VALUE) == false) {
      errors[errors.length] = element.minlengthError;
   }
         
   // valid characters
   else if (element.validchars && isValidChars(element.value) == false) {
      errors[errors.length] = element.validcharsError;
   }

   // numbers only
   else if (element.numeric && isNumeric(element.value) == false) {
      errors[errors.length] = element.numericError;
   }

   // pattern (validchars, startend, numeric)
//         else if (element.pattern) {
//           if (  (element.pattern.toLowerCase() == 'validchars' && isAlphanumeric(element.value) == false) ||
//                  (element.pattern.toLowerCase() == 'numeric' && isNumeric(element.value) == false) ) {
//                  errors[errors.length] = element.patternError;
//            }
//         }
   
   return errors;
}

// 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 string contains valid domain characters
function isValidChars(string) {
   if (string.search) {
      if (string.search(/^[a-z0-9]+([a-z0-9\-])*([a-z0-9])+$/) != -1) return true;
   }
   return false;
}

// Check that a string contains only numbers
function isNumeric(string) {
   if (string.search) {
      if (string.search(/^[0-9]+$/) != -1) return false;
   }
   return true;
}

// 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;
}

// 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;
}
