/*----------------------------------------------------*/	
/* FUNCTION     : IsDate	                          */
/* DESCRIPTION  : Check if data is a data and		  */
/*				 return true else return false		  */
/* PARAMETERS                                         */
/*     data      : (IN) string to check			      */
/*----------------------------------------------------*/
function IsDate(data)
{
	var dataVect,dataObj;
	
	dataVect=data.split("/")
	if (dataVect.length!=3)
			return false
	else 
		{
			
			if (dataVect[0].length!=1 && dataVect[0].length!=2)
				return false
				
			if (dataVect[1].length!=1 && dataVect[1].length!=2)
				return false
			
			if (dataVect[2].length!=4)
				return false
						
			//oggetto data considera i mesi da 0 a 11
			dataVect[1]-=1
			dataObj=new Date(dataVect[2],dataVect[1],dataVect[0])
			if (dataObj.getDate()==dataVect[0] && dataObj.getMonth()==dataVect[1] && dataObj.getFullYear()==dataVect[2] )
				 return true
			else return false
		}	
}
/*----------------------------------------------------*/	
/* FUNCTION     : CheckOra	                          */
/* DESCRIPTION  : Check if ora is a ora and	    	  */
/*				 return true else return false		  */
/* PARAMETERS                                         */
/*     ora      : (IN) string to check			      */
/*----------------------------------------------------*/
function CheckOra(ora)
{
	var ora;
	
	ora=ora.split(":")
	if (ora.length!=2)
	{
		return false;
	}	
	else 
	{
		//controlla l'ora e accetta  h24
		var appo =ora[0].toString()
		var	appolung=appo.length
		if (appolung !=2)
			return false;
		else
		{
			
			if(Number(appo)>23 || isNaN(appo))
				return false;
		}
		
		//controlla i minuti da 00 a 59
		var appo1 =ora[1].toString()
		var	appo1lung=appo1.length
		if (appo1lung !=2)
			return false;
		else
		{
			
			if(Number(appo1)>59 || isNaN(appo1))
				return false;
		}
	}	
	return true;	
}

/*---------------------------------------------------------------*/	
/* FUNCTION     : CompareDate	     							 */
/* DESCRIPTION  : compare two date							 	 */
/* PARAMETERS												     */
/*	  data1		: data to compare								 */
/*	  data2		: data to compare								 */
/*	RETURN VALUES												 */
/*				: 0  if data1=data2								 */	
/*				: 1  if data1>data2								 */	
/*				: -1 if data1<data2								 */	
/*---------------------------------------------------------------*/

 function CompareDate(data1,data2)
 {
	var Vect1,Vect2;
	var i;
	
	Vect1=data1.split("/")
	Vect2=data2.split("/")
	
	for (i=0;i<Vect1.length;i++)
		Vect1[i]=parseInt(DelZero(Vect1[i]))
	
	for (i=0;i<Vect2.length;i++)
		Vect2[i]=parseInt(DelZero(Vect2[i]))
		
	if 	(Vect1[0]==Vect2[0] && Vect1[1]==Vect2[1] && Vect1[2]==Vect2[2])
		return 0
		
	if 	(Vect1[2]>Vect2[2] || (Vect1[2]==Vect2[2] && Vect1[1]>Vect2[1]) || (Vect1[2]==Vect2[2] && Vect1[1]==Vect2[1] && Vect1[0]>Vect2[0]))
		 return 1
	else return -1		
 }	
/*----------------------------------------------------*/	
/* FUNCTION     : Trim		   	                      */
/* DESCRIPTION  : esegue il trim della stringa		  */
/* PARAMETERS										  */
/*----------------------------------------------------*/
function Trim(str)
{
	while(str.substr(0,1)==' ')
		str=str.substr(1)
		
	while(str.substr(str.length-1)==' ')
		str=str.substr(0,str.length-1)	
	return str
}
/*---------------------------------------------------------------------------*/
/* FUNZIONE        : CheckEmail                                              */
/* DESCRIZIONE        : verifica se il valore passato in input (ValoreInput) */
/*                è un indirizzo email                                       */
/* PARAMETRI        : ValoreInput (stringa)                                  */
/* OUTPUT        : true (è un email), false (non è un email)                 */
/*---------------------------------------------------------------------------*/

function CheckEmail(ValoreInput)
{
var tc = false;
var tp = false;
var pc = -1;
var pp = -1;

        for (var i=0; i<= ValoreInput.length; i++)
                {
                if (ValoreInput.charAt(i) == "@")
                        {
                        tc = true;
                        pc = i;
                        }
                else
                        {
                        if (ValoreInput.charAt(i) == ".")
                                {
                                tp = true;
                                pp = i;
                                }
                        }
                }
        if ((tc && tp) && (pc < pp))
                return true
        else
                return false
}
/*---------------------------------------------------------------------------*/
/* FUNZIONE        : TestLenght                                              */
/* DESCRIZIONE     : verifica se il valore passato in input (ValoreInput)    */
/*                   supera il massimo di caratteri consentiti               */
/* PARAMETRI       : str(stringa), len(lunghezza massima)                    */
/* OUTPUT		   : true(lunghezza consentita), false(stringa troppo lunga) */
/*---------------------------------------------------------------------------*/
function TestLenght(str,len)
{
    
    if (str.length > len)
       return false;
    else
       return true;
}
/*---------------------------------------------------------------------------*/
/* FUNZIONE        : MsgConferma                                             */
/* DESCRIZIONE     : chiede la conferma di un operazione richiesta           */
/* PARAMETRI       : msg(stringa)                                            */
/* OUTPUT		   : true(richiesta confermata),false(richiesta annulata)	 */
/*---------------------------------------------------------------------------*/
function MsgConferma(msg)
{
	if (window.confirm(msg))
		return true;
	else
		return false;	
}
/*---------------------------------------------------------------------------*/
/* FUNZIONE        : InfoMsg                                                 */
/* DESCRIZIONE     : messaggio a video  e focus del campo                    */
/* PARAMETRI       : msg(stringa), campo(campo x il focus)                   */
/* OUTPUT		   : messaggio e focus del campo                           	 */
/*---------------------------------------------------------------------------*/
function InfoMsg(msg, campo)
{
	alert(msg);
	campo.focus();
		
}
/*---------------------------------------------------------------------------*/
/* FUNZIONE        : CampoVal                                                */
/* DESCRIZIONE     : controlla se un campo è valorizzato                     */
/* PARAMETRI       : campo(campo da controllare)                             */
/* OUTPUT		   : true(campo pieno),false(campo vuoto)                 	 */
/*---------------------------------------------------------------------------*/
function CampoVal(campo)
{
	if(campo== null || Trim(campo)== '' || !campo)
		return false
	else
		return true			
}
/*------------------------------------------------------------------------------------*/
/* FUNZIONE        : SaveEl                                                           */
/* DESCRIZIONE     : assegna un valore ad un campo                                    */
/* PARAMETRI       : campo(campo), valore(Valore da assegnare)						  */
/* OUTPUT		   : assegna il valore al campo                         	          */
/*------------------------------------------------------------------------------------*/
function SaveEl(valore, campo)
{
	if(!campo.value)
		campo.value=valore
	else
		campo.value=campo.value+","+valore;	
}

/*------------------------------------------------------------------------------------*/
/* FUNZIONE        : OptionSel                                                        */
/* DESCRIZIONE     : controlla se un campo è selezionato o ceccato                    */
/* PARAMETRI       : campo(campo da controllare), tipo(S select, O checkbox e radio)  */
/* OUTPUT		   : true(campo vuoto),false(campo pieno)                 	          */
/*------------------------------------------------------------------------------------*/
function OptionSel(campo, tipo)
{
	if (tipo =='S')
	{
		var controllo;
		controllo=campo.selectedIndex;
		if(controllo == -1)
			return false;
		controllo=campo[controllo].value;
		if(controllo == null || controllo == "")
			return false
		else
			return true	
	}
	else
	{
		if(campo.checked == false)
			return false
		else
			return true	
	}
}
/*------------------------------------------------------------------------------------*/
/* FUNZIONE        : GetValue                                                         */
/* DESCRIZIONE     : prende il valore di un campo selezionato o ceccato               */
/* PARAMETRI       : campo(campo da controllare), tipo(S select, O checkbox e radio)  */
/* OUTPUT		   : true(campo vuoto),false(campo pieno)                 	          */
/*------------------------------------------------------------------------------------*/
function GetValue(campo)
{
		var controllo;
		controllo=campo.selectedIndex;
		if(controllo == -1)
			return 0;
		controllo=campo[controllo].value;
		if(controllo == null || controllo == "")
			return 0
		else
			return controllo;	
}

/*------------------------------------------------------*/	
/* FUNCTION     : Checkumber(input) 					*/
/* DESCRIPTION  : check if the inout is a  number   	*/
/* PARAMETERS											*/
/*	  num		: number								*/	
/* RETURN VALUES :										*/
/*	               if number is correct else false      */
/*------------------------------------------------------*/
function CheckNum(num)
{
var AppoNumer = 0;

    AppoNumer= Number(num);
    if (isNaN(AppoNumer))
       return false
    else
       return true   
}
/*------------------------------------------------------*/	
/* FUNCTION     : FormatNum								*/
/* DESCRIPTION  : check the a number					*/
/* PARAMETERS											*/
/*	  num		: number								*/	
/*	  PartInt	: max length of part integer			*/
/*	  PartDec   : max length of part decimal			*/	
/* RETURN VALUES :										*/
/*	  number formatted if number is correct else false  */
/*------------------------------------------------------*/
function FormatNum(num,PartInt,PartDec)
{
	
	if (num<"")
		return false
	if (num=="")
		return true
	
	var i=num.indexOf(',');
	var j=num.indexOf('-');
	var segno="";
	
	if(num.indexOf('.')>-1)
		return false
	
	if (j>-1)
		segno=num.substring(0,1)
		
	if(i>-1)// se c'è la virgola
	{	
		if (j>-1)						//se c'è il segno meno
			parteInt=num.substring(1,i)
		else
			parteInt=num.substring(0,i)
			
		parteDec=num.substring(i+1)
		
		if ( parteInt.length<=PartInt && isNaN(parteInt)==false  && 
			 parteDec.length<=PartDec && isNaN(parteDec)==false)

			 return segno + parteInt + '.' + parteDec
		else return false
			
	}
	else //è un numero senza virgola
	{
		if (j>-1) //se c'è il segno meno
			num=num.substring(1)
	
		if (num.length<=PartInt && isNaN(num)==false)
			return segno + num
		else 
			return false
	}
}

//********************************************************************************//
// Nome Funzione: FormatNumber                                                    //
//   Descrizione: Funzione che formatta un numero in base al numero di cifre      //
//                decimali e tipo di arrotondamento passati                       //
//         Input: numero (numero da formattare)                                   //
//                numDecimali (numero di cifre decimali)                          //
//                tipoArr (tipo di arrotondamento)                                //
//                         0 -> Nessun arrotondamento                             //
//                         1 -> Troncamento                                       //
//                         2 -> Arrotondamento                                    //
//                aggZeri (indica se aggiungere gli zeri se non esistono decimali)//
//                      true -> Aggiunge gli zeri                                 //
//                     false -> Non aggiunge gli zeri                             //
//        Output: numFormat (numero formattato o 0 se errore)                     //
//********************************************************************************//
function FormatNumber(numero, numDecimali, tipoArr, aggZeri)
{
	var numFormat;
	var parteIntera=0;
	var parteDecimale=0;
	var valDecSucc=0;
	var valDecArr=0;
	var posVirgola=0;
	
	try
	{
		if (numero == 0)
			return 0;
		//numFormat = Number(numero);
		numFormat = numero;
		//Sostituisce il punto con la virgola
		numFormat = (numFormat.toString()).replace(".", ",");
		//Prende la parte intera
		parteIntera = parseInt(numFormat);
		//Prende la posizione della virgola
		posVirgola = numFormat.search(',');
		
		//Controlla se la virgola è presente
		if(posVirgola > 0)
		{
			//Prende la parte decimale
			parteDecimale = numFormat.substr(posVirgola+1);
			
			//Controlla se la parte decimale è più lunga dei decimali richiesti
			if(parteDecimale.length > numDecimali)
			{
				//Distinzione del tipo di operazione da eseguire sulla parte decimale
				switch(tipoArr)
				{
					case 1:	//Troncamento
					{
						//Tronca alla cifra decimale richiesta
						parteDecimale = parteDecimale.substr(0, numDecimali);
						break;
					}
					case 2:	//Arrotondamento
					{
						//Prende la cifra in base alla quale viene effettuato l'arrotondamento
						valDecSucc = parteDecimale.substr(numDecimali, 1);
						
						//Se la cifra è compresa tra 0 e 4 l'arrotondamento è per difetto
						//se la cifra è compresa tra 5 e 9 l'arrotondamento è per eccesso
						if(valDecSucc >= 0 && valDecSucc <= 4)	//Difetto
							parteDecimale = parteDecimale.substr(0, numDecimali);
						else									//Eccesso
						{
							var arrotondaValore = Number(parteIntera + "." + parteDecimale) + (1/Math.pow(10,numDecimali));
							var spostPunto = Number(String(parseInt(arrotondaValore)).length - String(parteIntera).length);
							parteIntera = parseInt(arrotondaValore);
							parteDecimale = String(arrotondaValore).substr(posVirgola+1+spostPunto,numDecimali);
						}
//						valDecArr = Number(parteDecimale.substr(numDecimali-1, 1)) + 1;

//						if(valDecArr=="10")
//							parteDecimale = (Number(parteDecimale.substr(0, numDecimali-1)) + 1).toString() + 0;
//						else
//							parteDecimale = parteDecimale.substr(0, numDecimali-1) + valDecArr;
					}
				}
			}
			else if(parteDecimale.length < numDecimali)
			{
				//Se la parte decimale è più corta dei decimali richiesti vengono
				//aggiunti tanti zeri in modo che la lunghezza sia quella richiesta
				parteDecimale = AddZeroRight(parteDecimale, numDecimali);
			}
			numFormat =  parteIntera + "," + parteDecimale;
		}
		else
		{
			//Se la parte decimale non esiste viene creata con i zeri
			if(aggZeri)
			{
				parteDecimale = AddZeroRight('0', numDecimali);
				numFormat = parteIntera + "," + parteDecimale;
			}
			else
				numFormat = parteIntera;
		}
		
		//Restiuisce il numero formattato
		return numFormat;
	}
	catch(e)
	{
		return 0;
	}
}


function FormatMigliaia(numero)
{
	var posChar=0;
	var indFormat=0;
	var numFormat='';
	var appoNumFormat='';
	var posVirgola=0;
	var parteIntera=0;
	var parteDecimale=0;
	var segno="";
	
	try
	{
		if(!numero)
			return 0;

		parteIntera = parseInt(numero)
		
		//Prende il segno
		segno = (parteIntera.toString()).substr(0, 1);
		if(segno=="-" || segno=="+")
			parteIntera = Number((parteIntera.toString()).substr(1))
		else
			segno = "";
			
		//Prende la posizione della virgola
		posVirgola = (numero.toString()).search(',');
		
		//Controlla se la virgola è presente
		if(posVirgola > 0)
		{
			//Prende la parte decimale
			parteDecimale = numero.substr(posVirgola+1);
		}

		if((parteIntera.toString()).length>3)
		{
			//Aggiunge i separatori delle migliaia
			for(posChar=(parteIntera.toString()).length-1, indFormat=0; posChar>=0; posChar--, indFormat++)
			{
				if(indFormat==3)
				{
					appoNumFormat += ".";
					indFormat=0;
				}
					
				appoNumFormat += (parteIntera.toString()).substr(posChar, 1);
			}
		
			//Inverte la stringa appoNumFormat assegnandola a parte numFormat
			for(indFormat=(appoNumFormat.toString()).length-1; indFormat>=0; indFormat--)
			{
				numFormat += appoNumFormat.substr(indFormat, 1)
			}
		}
		else
			numFormat = parteIntera;
		
		return segno + numFormat + (parteDecimale?"," + parteDecimale:"");
	}
	catch(e)
	{
		return 0;
	}
}

/*----------------------------------------------------------*/	
/* FUNCTION     : AddZeroRight								*/
/* DESCRIPTION  : Riempie il numero di zeri a destra		*/
/*				  affinchè la lunghezza sia uguale a lentot	*/
/* PARAMETERS												*/
/*     numero   : (IN) number to format						*/
/*	   lentot	: (IN) len of number						*/
/*----------------------------------------------------------*/
function AddZeroRight(numero,lentot)
{
	if (numero.length<lentot)
	{
		for (i=numero.length;i<lentot;i++)
		numero = numero + '0';
		return numero;
	}
	if (numero<="")
		return "0"
	return numero
}

/*----------------------------------------------------*/	
/* FUNCTION     : TxtLostFocus				          */
/* DESCRIPTION  : on lost focus of textbox	set		  */
/*						all character to upper		  */
/*----------------------------------------------------*/
function TxtLostFocus(textbox)
{
	textbox.value=Upper(textbox.value) 
}

/*----------------------------------------------------*/	
/* FUNCTION     : Upper			                      */
/* DESCRIPTION  : Return upperCase of string	      */
/*				  and delete all start and end space  */	
/* PARAMETERS                                         */
/*	  str		: (IN)string to Upper		          */	
/*----------------------------------------------------*/
function Upper(txtBox)
{  
	var str=txtBox.value
	
	if(str)
	{
		while (str.charAt(0)==" ")
			str=str.substr(1)
	
		while (str.charAt(str.length-1)==" ")
			str=str.substr(0,str.length-1)
	
		str=str.toUpperCase()
		txtBox.value=str
	}
}
/*----------------------------------------------------------*/	
/* FUNCTION     : InvalidChar								*/
/* DESCRIPTION  : return true if str have an invalid char	*/
/* PARAMETERS												*/
/*     txt		: (IN) string to check						*/
/*	   lentot	: (IN) string of invalid chars				*/
/*----------------------------------------------------------*/

function InvalidChar(str, chars)
{
	//chars="#|"
	
	if (str)
	{
		for (var i=0;i<chars.length;i++)
		{
			if (str.indexOf(chars.substr(i,1))!=-1)
				return true
		}
	}
	return false
}


/*----------------------------------------------------------*/	
/* FUNCTION     : ControllaCaratteriNonValidi				*/
/* DESCRIPTION  : Check if an input field have an invalid   */
/*				  char										*/	
/* PARAMETERS												*/
/*     form		 : (IN) form to check						*/
/* RETURN VALUES : true if there is an invalid char			*/   
/*----------------------------------------------------------*/

function ControllaCaratteriNonValidi(form)
{
	var elements;
	var i;
	
	if (navigator.appName.indexOf('Explorer')>=0)
	{
		elements=form.all
		for (i=0;i<elements.length;i++)
			if (elements[i].tagName.toUpperCase()=='INPUT' && elements[i].type.toUpperCase()=='TEXT')
				if (InvalidChar(elements[i].value, "#|"))
				{	alert('Carattere non Valido')
					elements[i].focus();
					return true
				}
	}
	else 
	{	
		elements=form.elements 
		for (i=0;i<elements.length;i++)
			if (elements[i].type.toUpperCase()=='TEXT')
				if (InvalidChar(elements[i].value))
				{	alert('Carattere non Valido')
					elements[i].focus();
					return true
				}
	}
	return false
}
/*---------------------------------------------------------------*/	
/* FUNCTION     : DelZero										 */
/* DESCRIPTION  : Delete all char = "0" to the left of str		 */
/* PARAMETERS												     */
/*	  str	: (IN) string to delete								 */
/*---------------------------------------------------------------*/
function DelZero(str)
{
	while(str.charAt(0)=='0')
		str=str.substr(1)
		
	if (str=="")
		return parseInt("0")
	else
		return parseInt(str)
}
//----------------------------------------------------------------//
//                      controlla duplicati                       //
//----------------------------------------------------------------//
function CheckDuplicati(prior, id)
{
	var appo = new Array();
	var pass;
	var n,i;
	appo = prior.split(",");
	n = appo.length;
	for (i = 0 ; i < n ; i ++)
	{
		if (parseInt(appo[i]) == parseInt(id))
		{
			return false;
		}
	}
	
	return true;
}
//----------------------------------------------------------------//
//                      Elimina elemento                          //
//----------------------------------------------------------------//

function EliminaElemento(indicisel, indicecanc)
{
	var campi = '';
	var appo= new Array();
	appo=indicisel.split(",");
	
	for(var i=0; i<appo.length; i++)
	{
		if(Number(appo[i])!= Number(indicecanc))
		{
			if(campi)
				campi = campi + "," +appo[i] ;
			else
				campi = appo[i] ;
		}
		
	}
	return campi;
}
/*----------------------------------------------------*/
/* FUNCTION     : ReplaceChar	                      */
/* DESCRIPTION  : Raplace Oldchar with NewChar in str */
/* PARAMETERS                                         */
/*     str      : (IN) string to replace		      */
/*	   Oldchar	: (IN) char to replace				  */
/*	   NewChar	: (IN) char replaced				  */
/*----------------------------------------------------*/
function ReplaceChar(str,Oldchar,NewChar)
{
	var stringa="";
	var vect;
	var i;
		
	if (str>"")
	{
		str=str.toString()
		if (str.indexOf(Oldchar)==-1)
		 return str
		
		vect=str.split(Oldchar);
		for (i=0;i<vect.length-1;i++)
			stringa+=vect[i] + NewChar;
		stringa+=vect[vect.length-1]
	}	
	return stringa;
}
