




<!--

var formValidated="";
var formObjects	= new Array();

function resetAll() {
    formObjects	= new Array(0);
}

function checkObject()
{
 var f=eval("document."+this.form+"."+this.HTMLname);
 if (this.mandatory && this.validateObject(f, true))
  return "non hai indicato " + this.label ;
 if (!this.mandatory && this.validateObject(f, true))
  return "";
 return this.validateObject(f, false);
}

function trim(inputString)
{
   inputString = String(inputString);
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue;
}


// riceve una stringa. se è vuota torna true, se contiene qualcosa di valido torna false
function isEmpty(stringa)
{
    if (stringa==null || stringa =='')
      return true;

    stringa = trim(stringa);
    if (stringa==null || stringa =='')
      return true;
    else
      return false;
}

function isString(f, checkEmpty)
{
    var errMsg = "";
    var str = f.value;
    if (checkEmpty) return (str == "");

    if ((this.limitA != null && str.length < this.limitA) || (this.limitB != null && str.length > this.limitB))
    {
        if (this.limitA == this.limitB)
            errMsg = this.label + "  deve essere di  " + this.limitA + " caratteri";
        else if (this.limitB == null && this.limitA != null)
            errMsg = this.label + "  deve essere di almeno  " + this.limitA + " caratteri";
        else if (this.limitA == null && this.limitB != null)
            errMsg = this.label + "  deve essere al massimo di  " + this.limitB + " caratteri";
        else
            errMsg = this.label + "  deve essere costituito da  " + this.limitA + " a " + this.limitB + " caratteri";
     }
    if (this.maxRows!=null)
    {
       arr = str .split("\r");
       if (arr.length>this.maxRows)
          errMsg = this.label + "  deve essere al massimo di  " + this.maxRows + "  righe";
    }
    return errMsg;
}

function isEmail(f, checkEmpty)
{
    var email = f.value;
    if (checkEmpty) return (email == "");
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
        return "";
    return this.label + "  non e\' un e-mail valida";
}

function isChecked(chk, checkEmpty)
{
    if (!checkEmpty) return "";
    var indefinita;
    var errMsg = "non hai indicato  "  + this.label;
    if (chk.length == indefinita)
    {
        if (chk.checked)
            errMsg="";
    }
    else
    {
        for (var i=0;i<chk.length;i++)
        {
            if (chk[i].checked)
            {
                errMsg="";
                break;
            }
        }
    }
    return errMsg;
}

function isNumber(f, checkEmpty)
{
    var num = f.value;

    if (checkEmpty) return (num == "");
    if (num.substring(0, 1) == "," || num.substring(num.length - 1, num.length) == "," ||
        num.substring(0, 1) == "." || num.substring(num.length - 1, num.length) == ".")
        return this.label + "  deve essere un valore numerico";
    num = num.replace(",", ".");
    if (isNaN(num))
        return this.label + "  deve essere un valore numerico";

    var numPart = num.split(".");
    var errMsg = "";

    if (this.limitA != null && numPart[0].length > this.limitA)
        errMsg = this.label + "  deve avere al massimo  " + this.limitA + "  cifre intere";
    if (this.limitB != null && numPart.length > 1 && numPart[1].length > this.limitB)
    {
        if (this.limitB ==0)
            errMsg += (errMsg == ""?"":"\n- ") + this.label + "  deve essere un numero intero";
        else
            errMsg += (errMsg == ""?"":"\n- ") + this.label + "  deve avere al massimo  " + this.limitB + " cifre decimali";
    }

    return errMsg;
}


//riceve un numero (volendo anche sotto forma di stringa). sostituisce tutte le virgole con il punto e restituisce un Float
function commaToDot(num)
{
    if (num==null)
        return num;
    var stringa = num.toString();
    var pos = stringa.indexOf(",");
    while ( pos != -1 )
    {
        stringa = stringa.slice(0,pos)+"."+stringa.slice(pos+1);
        pos = stringa.indexOf(",", pos+1);
    }
    return parseFloat(stringa);
}


function isNumberWithLimit(f, checkEmpty)
{
    var errMsg="";

    var num=f.value;
    if (checkEmpty) return (num=="");
    if (num.substring(0,1)=="," || num.substring(num.length-1,num.length)=="," ||
        num.substring(0,1)=="." || num.substring(num.length-1,num.length)==".")
        return this.label + " deve essere un valore numerico";

    num=num.replace(",",".");
    var num1 = num;
    if (isNaN(num))
     return this.label + " deve essere un valore numerico";
    if (checkEmpty) return (num=="");

    var num=commaToDot(f.value);
    var minimo = commaToDot(this.limitC);
    var massimo = commaToDot(this.limitD);

    var numPart=num1.split(".");
    if (this.limitA!=null && numPart[0].length>this.limitA)
        errMsg = this.label + "  deve avere al massimo  " + this.limitA + "  cifre intere";
    if (this.limitB!=null && numPart.length>1 && numPart[1].length>this.limitB)
    {
        if (this.limitB ==0)
            errMsg += (errMsg == ""?"":"\n- ") + this.label + "  deve essere un numero intero";
        else
            errMsg += (errMsg == ""?"":"\n- ") + this.label + "  deve avere al massimo  " + this.limitB + " cifre decimali";
    }

    // 0= limite minimo escluso - limite massimo escluso
    // 1= limite minimo incluso - limite massimo escluso
    // 2= limite minimo escluso - limite massimo incluso
    // 3= limite minimo incluso - limite massimo incluso
    if (!isEmpty(this.limitC))
    {
      //limite minimo incluso
      if (this.incType==1 || this.incType==3)
      {
        if (num<minimo)
          errMsg=(errMsg==""?"":"\n- ")+this.label+" deve essere maggiore o uguale a "+this.limitC+"!";
       }
       else if (num<=minimo)
       errMsg=(errMsg==""?"":"\n- ")+this.label+" deve essere maggiore a "+this.limitC+"!";

    }

    if (!isEmpty(this.limitD))
    {
         //limite massimo incluso
        if (this.incType==2 || this.incType==3)
        {
            if (num>massimo)
                errMsg+=(errMsg==""?"":"\n- ")+this.label+" deve essere minore o uguale a "+this.limitD+"!";
        }
        else if (num>=massimo)
          errMsg+=(errMsg==""?"":"\n- ")+this.label+" deve essere minore a "+this.limitD+"!";
    }
    return errMsg;
}


function FormObject(form, type, HTMLname, label, limitA, limitB, limitC, limitD, mandatory, validationFunction, initValue, incType,maxRows)
{
    this.form = form;
    this.type = type;
    this.HTMLname = HTMLname;
    this.initValue = initValue;
    this.label = label;
    this.limitA = limitA;
    //lim inf / dim max INT / date from
    this.limitB = limitB;
    //lim max / dim max DEC / date to
    this.limitC = limitC;
    //lim inf / dim max INT / date from
    this.limitD = limitD;
    //lim max / dim max DEC / date to
    this.maxRows = maxRows;
    this.mandatory = mandatory;
    this.checkObject = checkObject;
    this.incType = incType;
    // 0= limite minimo escluso - limite massimo escluso
    // 1= limite minimo incluso - limite massimo escluso
    // 2= limite minimo escluso - limite massimo incluso
    // 3= limite minimo incluso - limite massimo incluso
    this.validateObject = eval(validationFunction);
}


function defineCheck(formName, name, label, mandatory) {
 var initValue=eval("document."+formName+"."+name);
 var initCheck = new Array();
 for (var i=0;i<initValue.length;i++)
  initCheck[i]=initValue[i].checked;
 formObjects[formObjects.length]=new FormObject(formName,"CHECK",name,label,null,null,null,null,mandatory, "isChecked", initCheck, null,null);
}

function defineNumber(formName, name, label, maxInt, maxDec, mandatory) {
    formObjects[formObjects.length] = new FormObject(formName, "NUMBER", name, label, maxInt, maxDec, null, null, mandatory, "isNumber", eval("document." + formName + "." + name + ".value"), null,null);
}
function defineString(formName, name, label, min, max, mandatory) {
    formObjects[formObjects.length] = new FormObject(formName, "STRING", name, label, min, max, null, null, mandatory, "isString", eval("document." + formName + "." + name + ".value"), null,null);
}
function defineText(formName, name, label, min, max, mandatory,maxRows) {
    formObjects[formObjects.length] = new FormObject(formName, "STRING", name, label, min, max, null, null, mandatory, "isString", eval("document." + formName + "." + name + ".value"), null,maxRows);
}
function defineEmail(formName, name, label, mandatory) {
    formObjects[formObjects.length] = new FormObject(formName, "EMAIL", name, label, null, null, null, null, mandatory, "isEmail", eval("document." + formName + "." + name + ".value"), null,null);
}
function defineNumberWithLimit(formName, name, label, maxInt, maxDec, da, a, mandatory, incType) {
 formObjects[formObjects.length]=new FormObject(formName,"NUMBERLIM",name,label,maxInt, maxDec,da,a,mandatory, "isNumberWithLimit", eval("document."+formName+"."+name+".value"), incType,null);
}

function removeLast()
{
  var formObjects2 = new Array(formObjects.length-1);
  for (i=0; i<formObjects.length-1;i++)
    formObjects2[i] = formObjects[i];

  formObjects = new Array(formObjects2.length);
  for (i=0; i<formObjects2.length;i++)
    formObjects[i] = formObjects2[i];
}

function validate(formName)
{
    var errorMsg = "";
    var focus = false;

    if (formObjects.length > 0)
    {
        for (i = 0; i < formObjects.length; i++)
        {
            if (formObjects[i].form == formName)
            {
                res = formObjects[i].checkObject();
                if (res!="")
                {
                  if (errorMsg=="")
                    setFocus(formObjects[i]);
                  errorMsg = errorMsg + "- " + res + "\n";
                }
            }
        }
    }
    if (errorMsg != "") {
        alert("Attenzione:\n" + errorMsg);
        formValidated = "";
        return false;
    } else {
        formValidated = formName;
        return true;
    }
}


function setFocus(f) {
    try {
        f.focus();
    }
    catch (e) {
    }
}




//-->