<!--

function tChecker () {
  this.vals = new Array ();
  this.count = 0;

  this.modi = new Array ('O', 'E', 'N', 'I', 'C');
  this.special = new Array ('L', 'V');
  this.nomi = new Array ();
  this.nomi [0] = 'Attenzione: specificare un valore per i campi';
  this.nomi [1] = 'Attenzione: i seguenti campi devono contenere un indirizzo e-mail';
  this.nomi [2] = 'Attenzione: i seguenti campi devono contenere un valore numerico';
  this.nomi [3] = 'Attenzione: i seguenti campi devono contenere un numero intero';
  this.nomi [4] = 'Attenzione: è necessario selezionare i seguenti campi';

  this.add = add;
  this.validator = validator;
  this.check_uno = check_uno;
  this.check_enne = check_enne;
  this.check_special = check_special;
  this.isEmail = isEmail;
  this.checkMaker = checkMaker;
}



function add (objName, objDesc, checks) {
  var tempo = ',' + checks + ',', desc = '';
  while (tempo.indexOf (', ') >= 0) tempo = tempo.replace (', ', ',');
  while (tempo.indexOf (' ,') >= 0) tempo = tempo.replace (' ,', ',');
  for (var ct = 0; ct < this.modi.length; ++ct) tempo = tempo.replace (',' + this.modi [ct] + ',', ',');
  for (var ct = 0; ct < this.special.length; ++ct) {
    r = new RegExp ("," + this.special [ct] + "[^,]+,", "");
    while (r.test (tempo)) tempo = tempo.replace (r, ',');
  }
  tempo = tempo.substr (1);
  while (tempo.length > 0) {
    pos = tempo.indexOf (',');
    desc += " '" + tempo.substr (0, pos) + "' ";
    tempo = tempo.substr (pos + 1);
  }

  if (desc.length > 0) window.alert ("Code Error: tChecker.add - modalità " + desc + " non contemplata/e");
  else {
    tempo = ',' + checks + ',';
    while (tempo.indexOf (', ') >= 0) tempo = tempo.replace (', ', ',');
    while (tempo.indexOf (' ,') >= 0) tempo = tempo.replace (' ,', ',');
    tempo = tempo.replace (/==/g, '=');
    tempo = tempo.replace (/=/g, '==');
    tempo = tempo.replace (/>==/g, '>=');
    tempo = tempo.replace (/<==/g, '<=');
    tempo = tempo.replace (/!==/g, '!=');
    tempo = tempo.replace (/<>/g, '!=');
    tempo = tempo.substr (1, tempo.length - 2);
    this.vals [this.count] = objName;
    this.vals [this.count + 1] = objDesc;
    this.vals [this.count + 2] = tempo;
    this.count += 3;
  }
}



function validator (theForm, chMode) {

  if (this.count == 0) return true;

  chMode = chMode.toLowerCase ();
  if ((chMode != '1') && (chMode != 'n')) {
    window.alert ("Code Error: l' ordine di comparazione '" + chMode + "' non è supportato.");
    return false;
  }

  for (var ct = 0; ct < this.count; ct += 3) {
    if (!theForm [this.vals [ct]]) {
      window.alert ("Code Error: il campo '" + this.vals [ct] + "' non esiste all' interno della form specificata.");
      return false;
	}
  }

  if (chMode == '1') {
    if (!this.check_uno (theForm)) return false;
  } else {
    if (!this.check_enne (theForm)) return false;
  }
  return true;

}



function check_uno (theForm) {

  for (ct = 0; ct < this.count; ct += 3) {
    for (im = 0; im < this.modi.length; ++ im) {
      valore = ',' + this.vals [ct + 2] + ',';
      chiave = ',' + this.modi [im] + ',';
      if ((valore.indexOf (chiave) >= 0) && ((chiave == ',O,') || (chiave == ',C,') || (theForm [this.vals [ct]].value.length > 0))) {
        if (!this.checkMaker (theForm [this.vals [ct]], this.modi [im])) {
          window.alert (this.nomi [im] + "\n\n - " + this.vals [ct + 1]);
          theForm [this.vals [ct]].focus ();
          return false;
        }
      }
    }
    if (!this.check_special (theForm, ct)) return false;
  }
  return true;

}



function check_enne (theForm) {

  var res = true, lista = '', chi = '';
  for (im = 0; im < this.modi.length; ++ im) {

    for (ct = 0; ct < this.count; ct += 3) {
      valore = ',' + this.vals [ct + 2] + ',';
      chiave = ',' + this.modi [im] + ',';
      if ((valore.indexOf (chiave) >= 0) && ((chiave == ',O,') || (chiave == ',C,') || (theForm [this.vals [ct]].value.length > 0))) {
        if (!this.checkMaker (theForm [this.vals [ct]], this.modi [im])) {
          lista += "\n - " + this.vals [ct + 1];
          res = false;
          if (chi.length == 0) chi = this.vals [ct];
        }
      }
    }

    if (!res) {
      window.alert (this.nomi [im] + "\n" + lista);
      theForm [chi].focus ();
      return false;
    }
  }

  for (ct = 0; ct < this.count; ct += 3) {
    if (!this.check_special (theForm, ct)) return false;
  }
  return true;

}



function check_special (theForm, ct) {

  if ((theForm [this.vals [ct]].type == "select-one") || (theForm [this.vals [ct]].type == "select-multiple")) {
    if (theForm [this.vals [ct]].selectedIndex == -1) value = '';
    else value = theForm [this.vals [ct]].options [theForm [this.vals [ct]].selectedIndex].value;
  } else value = theForm [this.vals [ct]].value;

  if (value.length > 0) {

    valore = ',' + this.vals [ct + 2] + ',';
    r = new RegExp (",.,", "");
    while (r.test (valore)) valore = valore.replace (r, ',');

    valore = valore.substr (1);
    while (valore.length > 0) {

      v1 = valore.substr (0, 1);
      v2 = valore.substr (1, valore.indexOf (',') - 1);
      valore = valore.substr (valore.indexOf (',') + 1);

      switch (v1) {
        case 'L':
          if (!eval ('value.length' + v2 + ';')) {
            v2 = v2.replace ('!=', '<>');
            v2 = v2.replace ('==', '=');
            window.alert (mes = "Attenzione: la lunghezza del campo " + this.vals [ct + 1] + " deve essere " + v2 + " caratteri");
            theForm [this.vals [ct]].focus ();
            return false;
          }
          break;
        case 'V':
          if (!eval ('value' + v2 + ';')) {
            v2 = v2.replace ('!=', '<>');
            v2 = v2.replace ('==', '=');
            window.alert (mes = "Attenzione: il valore del campo " + this.vals [ct + 1] + " deve essere " + v2 + "");
            theForm [this.vals [ct]].focus ();
            return false;
          }
          break;
      }
    }
  }
  return true;

}



function checkMaker (theObj, modo) {

  if ((theObj.type == "select-one") || (theObj.type == "select-multiple")) {
    if (theObj.selectedIndex == -1) value = '';
    else value = theObj.options [theObj.selectedIndex].value;
  } else value = theObj.value;

  switch (modo) {
    case 'O': return (value.length != 0);
    case 'E': return (this.isEmail (value));
    case 'N':

      if (value == '0') return true;
      value = value.replace (',', '.');
      value = value.replace (/(0*)([^0]*.*)/, "$2");
      return (parseFloat (value).toString () == value) || (parseFloat (value).toString () == ('0' + value));

    case 'I':

      if (value == '0') return true;
      value = value.replace (',', '.');
      value = value.replace (/(0*)([^0]*.*)/, "$2");
      return (parseInt (value).toString () == value);

    case 'C': return (theObj.checked);
  }
  return true;
}



function isEmail (email) {
  var result = false;
  result = result || (email.indexOf ('.') <= 0);
  result = result || (email.indexOf ('..') >= 0);
  result = result || (email.indexOf ('@') <= 0);
  result = result || (email.indexOf ('@') != email.lastIndexOf ('@'));
  result = result || (email.indexOf ('.@') >= 0);
  result = result || (email.indexOf ('@.') >= 0);
  result = result || (email.lastIndexOf ('.') <= (email.indexOf ('@') + 1));
  result = result || (email.lastIndexOf ('.') == (email.length - 1));
  return !result;
}

// -->
