// functions for validating input.
// All return '' on success, an error message on failure
// NOTE that this is not the same as a boolean, and a return of '' will not abort the
// submit.
//
// In general, do not call on empty required fields. Those you want to report as 'missing',
// rather than 'invalid'.
//
// Required includes: none

var validations = {}; // namespace object.

validations.url = function(s) {
  var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
  return regexp.test(s) ? '' : 'invalid url';
};

// NOTE: php pattern has \pL to match the unicode letters (ñ e.g.). We can't do that in js,
// so we can't really have a pattern for everything which should match.

// these pattern pieces match the php ones:
validations._special = '\\.\\,\\+\\*\\!\\#\\$\\^\\&\\|\\?\\:\\=\\~';
validations._wordSlashDash = '\\w\\/\\-';
validations._quoteParensSpace = '\\\'\\(\\)\\ ';
validations._validPassword = '^['+validations._wordSlashDash+validations._special+']{6,32}$';
validations._validPhrase = '^['+validations.wordSlashDash+validations._special+validations._quoteParensSpace+']{2,50}$';
validations._invalidChars = '[\\@\\%\\{\\}\\[\\]\\<\\>\\`\\"\\;]'; // just some known bad ones
//validations._invalidChars = '[^'+validations._wordSlashDash+validations._special+validations._quoteParensSpace+']';

/** private: concatinate invalid chars found in a match */
validations._badChars = function(matches) {
  if (matches == null) { return ''; }
  var invalidChars = '';
  for (var i = 0; i < matches.length; i++) {
    invalidChars += matches[i]==' ' ? '<i>space</i>' : matches[i];
  }
  return invalidChars;
}

validations.tagName = function(s) {
  var tagRE = new RegExp(validations._validPhrase, '');
  if (tagRE.test(s)) { return ''; } // it's ok, we're done
  if (s.length < 2) { return 'Invalid name - must be at least 2 characters long'; }
  if (s.length > 50) { return 'Invalid name - maximum length is 50 characters'; }
  var invalidCharsRE = new RegExp(validations._invalidChars, 'g');
  var matches = s.match(invalidCharsRE);
  if (matches == null) { return ''; } // ok by js. Let php check
  var invalidChars = validations._badChars(matches);
  if (matches.length == 1) { return 'illegal character: ' + invalidChars; }
  return 'illegal characters: ' + invalidChars;
};

// ZAT_TODO: implement
validations.alt = function(s) {
  return '';
};

// other utility functions.

// stick the message in the id_error span.
validations.reportError = function(id, message) {
  var errElem = $(id + '_error');
  if (errElem) { errElem.innerHTML = message; }
};

validations.noError = function(id) {
  var errElem = $(id + '_error');
  if (errElem) { errElem.innerHTML = ''; }
};

/** set focus on first field w/ error
 * return true if such a field found and focus set, false otherwise.
 */
validations.setFocus = function(form) {
  for (var i = 0; i < form.length; i++) {
    var formElem = form.elements[i];
    var errElem = $(formElem.id + '_error');
    if (errElem) {
      var errVal = errElem.innerHTML;
      if (trim(errVal) != '') {
        formElem.focus();
        return true;
      }
    }
  }
  return false;
};

/** clear any errors on form */
validations.clearErrors = function(form) {
  for (var i = 0; i < form.length; i++) {
    validations.noError(form.elements[i].id);
  }
};

/** Check for missing required fields. Set their error messages,
 * and build flash. Returns true if no required fields are missing.
 */
validations.checkRequired = function(form, genericFlash) {
  var missing = [];
  var formElem;
  var value;
  validations.clearErrors(form);
  for (var i = 0; i < form.length; i++) {
    formElem = form.elements[i];
    value = trim(formElem.value);
    if (YAHOO.util.Dom.hasClass(formElem, 'required')
        && value == '' || (formElem.type == 'select-one' && formElem.value == -1)) {
      validations.reportError(formElem.id, 'is required');
      missing.push(formElem.id);
    }
  }
  if (missing.length == 1) {
    validations.buildFlash(genericFlash, missing[0] + ' required');
  } else if (missing.length > 1) {
    validations.buildFlash(genericFlash, 'required fields missing');
  }
  return missing.length == 0 ? true : false;
};

validations._flash = '';

validations.buildFlash = function(generic, specific) {
  if (validations._flash == '') {
    validations._flash = generic + ' - ' + specific;
  } else { validations._flash = generic; }
};

validations.getFlash = function() {
  var flash = validations._flash;
  validations._flash = '';
  return flash;
}

