// Requires onload.js

var FormCheck = new FormChecker(); 

function FormChecker() {
  this.map = new Object(); 

  this.addMapping = function (form_id) {
    this.map[form_id] = new Object();
    this.map[form_id].checks = new Array();
    this.map[form_id].handler = null;
  }

  this.addCheck = function (form_id, check) {
    if (!this.map[form_id]) { 
      this.addMapping(form_id);
    }
    this.map[form_id].checks.push(check);
    OnloadDispatch.addCall(function(event) {
      document.getElementById(form_id).onsubmit = function () {
        return FormCheck.CheckForm(form_id);
      }
    });
  }

  this.setEventHandler = function (form_id, handler) {
    if (!this.map[form_id]) { 
      this.addMapping(form_id);
    }
    this.map[form_id].handler = handler; 
  }
 
  this.CheckForm = function (form_id) {
    if (!this.map[form_id]) {
      alert('no mapping for: ' + form_id); 	
      return true; 
    }

    var errors = "";
    var error_array = Array();
    var handler = this.map[form_id].handler;
    var checkarray = this.map[form_id].checks;

    for(var i = 0; i < checkarray.length; i++) {
      resp = checkarray[i].check();
      if (resp) {
        errors += resp.message + "\n";
        error_array.push(resp);
      }
    }

    if (handler) {
       handler.clear_errors();
    }
     
    if (errors.length || error_array.length) {
      if (handler) { 
        handler.handle_errors(error_array);
      } else {
        alert(errors);
      }
      return false; 
    }
    
    return true;
  }
}

function CheckFail(el, msg, v) {
  this.element_id = el;
  this.message = msg; 
  this.value = v;
}


function RequireNotNull(e, m) {
  this.element_id = e;
  this.message = m; 

  this.check = function() {
    value = document.getElementById(this.element_id).value;
    if (value) {
      return null;
    } else {
      return new CheckFail(this.element_id, this.message, value);
    }
  }
}

function RequireNotMatch(e, v, m) {
  this.element_id = e;
  this.value = v;
  this.message = m; 

  this.check = function() {
    value = document.getElementById(this.element_id).value;

    if (value.match(this.value)) {
      return new CheckFail(this.element_id, this.message, value); 
    } else {
      return null;

    }
  }
}

function RequireMatch(e, v, m) {
  this.element_id = e;
  this.value = v;
  this.message = m; 

  this.check = function() {
    value = document.getElementById(this.element_id).value;

    if (value.match(this.value)) {
      return null;
    } else {
      return new CheckFail(this.element_id, this.message, value);
    }
  }
}

function RequireDate(e, m) {
  this.element_id = e;
  this.message = m; 

  this.check = function() {
    return RequireMatch(this.element_id, /\d{2}\/\d{2}\/\d{4}/, this.message);
  }  
}

function RequireOr(c1, c2, m) {
  this.check1 = c1; 
  this.check2 = c2;
  this.message = m;
  this.check = function() {
    var r1 = this.check1.check();
    var r2 = this.check2.check();

    if (r1 && r2) {
      values = Array();
      values.push(r1);
      values.push(r2); 
      return values; 
    }
    return null; 
  }
}

function RequireAnd(c1, c2) {
  this.check1 = c1; 
  this.check2 = c2;
  this.check = function() {
    var r1 = this.check1.check();
    var r2 = this.check2.check();

    fails = Array();
    if (r1) {
      fails.push(r1);
    }
    if (r2) {
      fails.push(r2);      
    }
    if (r1 || r2) return fails; 
    return null;
  }
}


function HandleErrorsInDivs() {
  this.divs = new Array();

  this.CheckHandlerErrorDiv = function(error) { 
    if (!error) {
      return;
    }
    if (isArray(error)) {
      for (var i = 0; i < error.length; i++) {
        this.CheckHandlerErrorDiv(error[i]);
      }
      return;
    }
    el = document.getElementById(error.element_id + "_errors");

    if (el) {
      this.divs.push(error.element_id + "_errors");
      el.innerHTML = error.message;    
    } else {
      alert('Webmaster error:  no element ' + error.element_id + '_errors found');
      alert(error.message + '\n' + error.value);
    } 
  }

  this.handle_errors = function(errors) {
    for (var i = 0; i < errors.length; i++) {
      this.CheckHandlerErrorDiv(errors[i]);
    }
  }
 
  this.clear_errors = function() {
    for (var i = 0; i < this.divs.length; i++) {
      document.getElementById(this.divs[i]).innerHTML = "";
    }
    this.divs = new Array();
  }
}



function isArray(obj) {
   if (!obj) return false; 
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}