/*
	script per la validazione dei form lato client

	tipi di campo gestiti:
	testo (1)
	area testo (2)
	intero (3)
	decimale (4)
	data (5)
	scelta singola (6)
	checkbox (10)

	ogni campo da verificare deve essere inserito nell'array aCampi come oggetto Campo:

	aCampi[aCampi.length]=new Campo(nomeCampo, descrizioneCampo, tipo, lughezza, obbligatorio);



*/
function Campo(sNome, sDescr, idTipo, lunghezza, bObblig){
	this.nome=sNome;
	this.descrizione=sDescr;
	this.tipo=idTipo;
	this.lunghezza=lunghezza;
	this.obbligatorio=bObblig;
}
function verificaForm (theForm)
{
	var i=0;
	var myEl;
	var bOK;
	for (i=0; i<aCampi.length; i++)
	{
		myEl = aCampi[i];

		if (! controllaValidita(theForm, myEl))
		{
			alert("Il valore di '" + myEl.descrizione + "' non è valido");
			return(false);
		}
		if (! controllaObbligatorieta(theForm, myEl))
		{
			alert("Occorre valorizzare il campo '" + myEl.descrizione + "'");
			return(false);
		}
		if (! controllaLunghezza(theForm, myEl))
		{
			if(myEl.tipo==3)
			{
				alert("Il valore di '" + myEl.descrizione + "' non può superare 2^31-1")
			}
			else
			{
				alert("Limitare la lunghezza del campo '" + myEl.descrizione + "' a " + myEl.lunghezza + " caratteri.");
			}
			return(false);
		}
	}
	return(true);
}

function controllaFloat(sTxt)
{
	var sep_decimale = '.';
	//separatore di lista per campi select e file



	if (sTxt != '')
	{
		var nVirgola1 = sTxt.indexOf(sep_decimale);
		var nVirgola2 = sTxt.lastIndexOf(sep_decimale);
		if (nVirgola1 != nVirgola2)
		{
			return(false);
		}
		else
		{
			var sControllo = "0123456789" + sep_decimale;
			return(allowInString(sTxt, sControllo));
		}
	}
	return(true);
}

function controllaIntero(sTxt)
{
	if (sTxt != '')
	{
		return(allowInString(sTxt, '0123456789'));
	}
	return(true);
}
function allowInString (InString, RefString)  {
        if(InString.length==0) return (false);
        for (Count=0; Count < InString.length; Count++)  {
                TempChar= InString.substring (Count, Count+1);
                if (RefString.indexOf (TempChar, 0)==-1)
                        return (false);
        }
        return (true);
}

        function trim(inputString) {
           // Removes leading and trailing spaces from the passed string. Also removes
           // consecutive spaces and replaces it with one space. If something besides
           // a string is passed in (null, custom object, etc.) then return the input.
           if (typeof inputString != "string") { return inputString; }
           var retValue = inputString;
           var ch = retValue.substring(0, 1);
           while (ch == " ") { // Check for spaces at the beginning of the string
              retValue = retValue.substring(1, retValue.length);
              ch = retValue.substring(0, 1);
           }
           ch = retValue.substring(retValue.length-1, retValue.length);
           while (ch == " ") { // Check for spaces at the end of the string
              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; // Return the trimmed string back to the user
        }

function controllaValidita(myForm, objElem)
{
	var myElForm = eval(myForm.name + "." + objElem.nome);
	var valore = myElForm.value;
	if (objElem.tipo == 3){
		return(controllaIntero(valore));
	}else if (objElem.tipo == 4){
		return(controllaFloat(valore));
	}else if (objElem.tipo == 5){
		return(controllaData(valore));
	}else{
       	return true;
    }
}

function controllaObbligatorieta(myForm, objElem)
{
	var myElForm = eval(myForm.name + "." + objElem.nome);

	var i;
	if (objElem.obbligatorio)
	{
		if (objElem.tipo != 6 && objElem.tipo != 10)
		{
			var valore = myElForm.value;
			return(valore != '');
		}
		else if(objElem.tipo == 6)
		{
			i = myElForm.selectedIndex;
			return (i>0);
		}else if(objElem.tipo == 10){
			return (myElForm.checked);
		}

	}else{
		return(true);
	}
}

function controllaLunghezza(myForm, objElem)
{
	var myElForm = eval(myForm.name + "." + objElem.nome);
	var valore = myElForm.value;
	if ((objElem.tipo == 1) || (objElem.tipo == 2))
	{
		return(valore.length <= objElem.lunghezza);
	}
	else if(objElem.tipo == 3)
	{
		 //return(valore < 2147483648); //2^31-1
		 return true;
	}
	else if(objElem.tipo == 4)
	{
		 return(true);
	}
	else
	{
		return(true);
	}
}

function controllaData(sData)
{
	var dData = sData ;
	var sFormat;
	var bOK;
	bOK = true;
	if (dData != "")
	{
		var Ret = dateConv(dData);
		if (Ret.length < 3)
		{
			bOK = false;
		}
		else
		{
			sFormat=new Date(Ret[2], Ret[1]-1, Ret[0]);
			if (sFormat.getDate()!=parseInt(Ret[0], 10) || (sFormat.getMonth()+1)!= parseInt(Ret[1], 10) || sFormat.getFullYear() != parseInt(Ret[2], 10))
			{
				bOK = false;
			}
			if ( bOK )
			{
				dData = sFormat.getDate()+"/"+(sFormat.getMonth()+1)+"/"+sFormat.getFullYear();
			}
			else
			{
				dData = ""
			}
		}
	}
	else
	{
		bOK = true;
	}
	return (bOK);
}

function dateConv (InString)
/*** controlla validità data nel formato dd/mm/aaaa ***/
{
	var sSep = "/";
	var arrData = InString.split(sSep);
	bOK = (arrData.length==3);
	if(bOK)
	{
		bOK = (arrData[2].length==4)
		if (bOK)
		{
			bOK = (isNumberString(arrData[0]) && isNumberString(arrData[1]) && isNumberString(arrData[2]))
		}
	}
	if ( bOK )
		return(arrData);
	else
		return(new Array(InString));
}

function isNumberString (InString)  {
        if(InString.length==0)
                return (false);
        RefString="1234567890";
        for (Count=0; Count < InString.length; Count++)  {
                TempChar= InString.substring (Count, Count+1);
                if (RefString.indexOf (TempChar, 0)==-1)
                        return (false);
        }
        return (true);
}