// <script>
var fV = {
  init: function() {
    for (var i in validationSet) {
      if ( $(i) ) {
        var formField = $(i);
        base.addEvent(formField, 'blur', fV.checkValid, false);

        /* if (!formField.form.validateSubmit) {
          base.addEvent(formField.form, 'submit', fV.checkValidSubmit, false);
          formField.form.onsubmit = fV.checkSubmit; // Safari
          formField.form.validateSubmit = true;
        } */
      }
    }
  },

  checkValidSubmit: function(e) {
    var frm = window.event ? window.event.srcElement : e ? e.target : null;
    if (!frm) return;
    var errText = [];

    for (var i = 0; i < frm.elements.length; i++) {
      if (frm.elements[i].id && validationSet[frm.elements[i].id]) {

        var failedE = fV.handleValidity(frm.elements[i]);

        var errDisplay = document.getElementById('error_' + frm.elements[i].id);

        if (failedE && errDisplay) {
          errDisplay.innerHTML =
              validationSet[failedE.id]['error'];
          errDisplay.parentNode.className = 'error';
        }
        if (!failedE && errDisplay) {
          errDisplay.innerHTML = '';
          errDisplay.parentNode.className = 'error';
        }

        if (failedE) {
          var labels = document.getElementsByTagName('label');
          errText[errText.length] = validationSet[failedE.id]['error'];
          for (var j = 0; j < labels.length; j++) {
            if (labels[j].htmlFor == failedE.id) {
              errText[errText.length - 1] +=
                  ' (field \'' + labels[j].firstChild.nodeValue + '\')';
            }
          }
        }
      }  /* end 'if' */
    } /* end 'for' */

    if (errText.length > 0) {
      alert(/*'Bitte beheben Sie die folgenden Fehler und wählen Sie erneut "weiter":\n' +*/
          errText.join('\n'));
      frm.submitAllowed = false;
      if (e && e.stopPropagation && e.preventDefault) {
        e.stopPropagation();
        e.preventDefault();
      }
      if (window.event) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        return false;
      }
    } else {
      frm.submitAllowed = true;
    }
  },

  checkSubmit: function() {
    if (this.attachEvent) return true;
    return this.submitAllowed;
  },

  checkValid: function(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;

    var failedE = fV.handleValidity(target);

    var errDisplay = document.getElementById('error_' + target.id);

    if (failedE && errDisplay) {
      errDisplay.innerHTML = validationSet[failedE.id]['error'];
      Element.addClassName(errDisplay.parentNode, 'error' );
      failedE.focus();
    }
    if (failedE && !errDisplay) {
      alert(validationSet[failedE.id]['error']);
    }
    if (!failedE && errDisplay) {
      errDisplay.innerHTML = '';
      Element.removeClassName(errDisplay.parentNode, 'error');
    }
  },

  handleValidity: function(field) {
    if (!field.value) {
      return null;
    }
    var re = validationSet[field.id]['regexp'];
    if (!field.value.match(re)) {
      return field;
    } else {
      return null;
    }
  }
}

base.addEvent(window, 'load', fV.init, false);