// Name: isEmpty
// Description: Returns true if the string is empty
// --------------------------------------------------
function isEmpty(str){
	return (str == null) || (str.length == 0);
}

// Name: removeSpaces
// Description: 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;
}

// Name: isNumeric
// Description: 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;
}

// Name: isValidZipcode
// Description: Check that a US zip code is valid
// --------------------------------------------------
function isValidZipcode(zipcode) {
   zipcode = removeSpaces(zipcode);
   if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
   if ((zipcode.length == 5 || zipcode.length == 9) && !isNumeric(zipcode)) return false;
   if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
   return true;
}

// Name: isValidEmail
// Description: Check that an email address is valid based on RFC 821 (?)
// --------------------------------------------------
function isValidEmail(address) {
   if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
   }
   
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

// Name: errorAlert
// Description: Given an array, pops up a javascript alert message with the
//  contents formatted nicely.
// --------------------------------------------------
function errorAlert(errors) {
  if (errors.length > 0 ) {
    var errorMessage = 'The form was not submitted due to the following problem' + ((errors.length > 1) ? 's' : '') + ':\n\n';
    for (var errorIndex = 0; errorIndex < errors.length; errorIndex++) {
      errorMessage += '* ' + errors[errorIndex] + '\n';
    }
    errorMessage += '\nPlease fix ' + ((errors.length > 1) ? 'these' : 'this') + ' problem' + ((errors.length > 1) ? 's' : '') + ' and resubmit the form.';
    alert(errorMessage);
    return true;
  }
  return false;
}
