<!--

function tChecker (lang) {

  if (lang == null) lang = 'IT';
  lang = lang.toLowerCase ();

  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.msg = new Array ();

  this.load_it = tChecker_load_it;
  this.load_en = tChecker_load_en;

  if (lang == 'en') this.load_en ();
  else this.load_it ();

  this.add = tChecker_add;
  this.validator = tChecker_validator;
  this.check_uno = tChecker_check_uno;
  this.check_enne = tChecker_check_enne;
  this.check_special = tChecker_check_special;
  this.isEmail = tChecker_isEmail;
  this.checkMaker = tChecker_checkMaker;
}



function tChecker_load_it () {

  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.msg [0] = 'Attenzione: la lunghezza del campo ';
  this.msg [1] = ' deve essere ';
  this.msg [2] = ' caratteri';
  this.msg [3] = 'Attenzione: il valore del campo ';
  this.msg [4] = ' deve essere ';
  this.msg [5] = '';

}



function tChecker_load_en () {

  this.nomi [0] = 'Attention: specify a value for the following field/s';
  this.nomi [1] = 'Attention: the following field/s have to contain a valid e-mail address';
  this.nomi [2] = 'Attention: the following field/s have to contain a numerical value';
  this.nomi [3] = 'Attention: the following field/s have to contain an integer value';
  this.nomi [4] = 'Attention: it is compulsory to select the following field/s';

  this.msg [0] = 'Attention: the length of the field ';
  this.msg [1] = ' have to be ';
  this.msg [2] = ' letters';
  this.msg [3] = 'Attention: the value of the field ';
  this.msg [4] = ' have to be ';
  this.msg [5] = '';

}



function tChecker_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 tChecker_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 tChecker_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]);
          if ((theForm [this.vals [ct]].type != 'hidden') && (theForm [this.vals [ct]].disabled != true)) {
            if (theForm [this.vals [ct]].type) theForm [this.vals [ct]].focus ();
            else if (theForm [this.vals [ct]].length > 0) theForm [this.vals [ct]][0].focus ();
          }
          return false;
        }
      }
    }
    if (!this.check_special (theForm, ct)) return false;
  }
  return true;

}



function tChecker_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);
      if ((theForm [chi].type != 'hidden') && (theForm [chi].disabled != true)) {
        if (theForm [chi].type) theForm [chi].focus ();
        else if (theForm [chi].length > 0) theForm [chi][0].focus ();
      }
      return false;

    }
  }

  for (ct = 0; ct < this.count; ct += 3) if (!this.check_special (theForm, ct)) return false;
  return true;

}



function tChecker_check_special (theForm, ct) {

  tmpObj = theForm [this.vals [ct]];
  if ((tmpObj.type == "select-one") || (tmpObj.type == "select-multiple")) {

    if (tmpObj.selectedIndex == -1) value = '';
    else value = tmpObj.options [tmpObj.selectedIndex].value;

  } else if (!tmpObj.type) {

    for (i = 0; i < tmpObj.length; ++ i) if (tmpObj [i].checked) value = tmpObj [i].value;

  } else value = tmpObj.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 = this.msg [0] + this.vals [ct + 1] + this.msg [1] + v2 + this.msg [2]);
            if ((theForm [this.vals [ct]].type != 'hidden') && (theForm [this.vals [ct]].disabled != true)) theForm [this.vals [ct]].focus ();
            return false;
          }
          break;

        case 'V':

          if (!eval ('value' + v2 + ';')) {
            v2 = v2.replace ('!=', '<>');
            v2 = v2.replace ('==', '=');
            window.alert (mes = this.msg [3] + this.vals [ct + 1] + this.msg [4] + v2 + this.msg [5]);
            if ((theForm [this.vals [ct]].type != 'hidden') && (theForm [this.vals [ct]].disabled != true)) theForm [this.vals [ct]].focus ();
            return false;
          }
          break;

      }
    }
  }
  return true;

}



function tChecker_checkMaker (theObj, modo) {

  value = "";
  if ((theObj.type == "select-one") || (theObj.type == "select-multiple")) {

    if (theObj.selectedIndex == -1) value = '';
    else value = theObj.options [theObj.selectedIndex].value;

  } else if (!theObj.type) {

    for (i = 0; i < theObj.length; ++ i) if (theObj [i].checked) value = theObj [i].value;

  } else value = theObj.value;

  switch (modo) {

    case 'O': return (value.length != 0);
    case 'E': return (this.isEmail (value));
    case 'N':
    
      if (value.substr (0, 1) == '-') value = value.substr (1);
      nums = 0, dots = 0, other = 0;
      while ((value.length > 0) && (other == 0) && (dots <= 1)) {

        car = value.substr (0, 1);
        value = value.substr (1);
        if ((car >= '0') && (car <= '9')) ++ nums;
        else if ((car == '.') || (car == ',')) ++ dots;
        else ++ other;

      }

      return (other == 0) && (dots <= 1) && (nums > 0);

    case 'I':

      if (value.substr (0, 1) == '-') value = value.substr (1);
      nums = 0, other = 0;
      while ((value.length > 0) && (other == 0)) {

        car = value.substr (0, 1);
        value = value.substr (1);
        if ((car >= '0') && (car <= '9')) ++ nums;
        else ++ other;

      }

      return (other == 0) && (nums > 0);

    case 'C': return (theObj.checked);

  }
  return true;

}



function tChecker_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;
}

// -->

