
// La funzione check_numeric deve essere chiamata sia sul keypress che sul keyup

// a third optional argument, true o false, tells the function
// whether it must accept negatives values
//
// a fourth argument, optional, tells the function to accept the plus sign anywhere
// in the string
//
// the fifth argument tessl the function to accept the '/' sign. Serve
// per le date.

// 6° argument tells the function to accept the '%' sign. Serve
// per le percentuali.


function check_numeric(event , dec ) 
{
argv=arguments;
argc=arguments.length;
if (argc > 2)
      negatives = argv[2];
else
      negatives = false;

if (argc > 3)
      accept_plus_sign_anywhere = argv[3];
else
      accept_plus_sign_anywhere = false;

if (argc > 4)
      accept_slash = argv[4];
else
      accept_slash = false;

if (argc > 5)
      accept_percent = argv[5];
else
      accept_percent = false;
   

var eventKeycode,valore,campo;
if(document.all) 
        { //it's IE 
        tipo = window.event.type;
	eventKeycode = window.event.keyCode; 
	campo=event.srcElement;
	valore=event.srcElement.value;
	}
else
        {
	tipo = event.type; 
	eventKeycode = event.which; 
	campo=event.target;
	valore=event.target.value;
	//if(eventKeycode == 8)
	//	campo.value=valore.substr(0,valore.length-1);
	}

if (accept_plus_sign_anywhere )
        {
        if(eventKeycode == 43)
                return true;
        }

if (accept_slash )
        {
        if(eventKeycode == 47)
                return true;
        }

if (accept_percent )
        {
        if(eventKeycode == 37)
                return true;
        }
        
if (tipo == "keyup")
	{

        if (accept_percent)
                {
                // se ho giā digitato una percentuale, non accetto ulteriori
                // caratteri dopo la percentuale
                if (valore.indexOf("%") != -1 )
                        {
			ze = valore.length - valore.indexOf("%") - 1;
                        if (ze > 0)
                                campo.value=valore.substr(0,valore.indexOf("%") + 1 );
                        }
                }
        


	// controllo il numero di decimali eventualmente gia' digitati
	// e vedo se non ho superato il massimo
	
	if (dec > 0)
		{
		if (valore.indexOf(".")!= -1)
			{
			num_decimali = valore.length - valore.indexOf(".") - 1;
			if (num_decimali > dec) 
				{
				// tolgo un carattere al termine della stringa
				campo.value=valore.substr(0,valore.length- ( num_decimali - dec ) );
				}
			}
		}
		
	// cambio le virgole in punti
	
	if (valore.indexOf(",")!= -1)
		{
		campo.value=valore.substr(0, valore.indexOf(",") )  +  "."  +  valore.substr(valore.indexOf(",") + 1 , valore.length - valore.indexOf(",") - 1) ;
		}
		
	// se ho digitato un "meno" NON nella posizione iniziale, lo elimino

	if (valore.indexOf("-") > 0)
		{
		campo.value=valore.substr(0, valore.indexOf("-") )  +  valore.substr(valore.indexOf("-") + 1 , valore.length - valore.indexOf("-") - 1) ;
		}

	// se ho digitato un "+" NON nella posizione iniziale, lo elimino

	if (valore.indexOf("+") > 0 && ! accept_plus_sign_anywhere)
		{
		campo.value=valore.substr(0, valore.indexOf("+") )  +  valore.substr(valore.indexOf("+") + 1 , valore.length - valore.indexOf("+") - 1) ;
		}
	
					
	}

if (tipo == "keypress")
	{	
	if(dec==null)
	        dec=2;
		
	if(eventKeycode == 8 || eventKeycode == 9 || eventKeycode == 0)
		{	 	
		// e' un TAB !!
		return true;
		}	

	if(eventKeycode == 10 || eventKeycode == 13 )
		{	 	
		// e' un invio
		return true;
		}	
			
	//if ( eventKeycode == 44)
	//	{
	//	// La virgola deve essere trasformata in punto
	//	//campo.value= valore.substr(0,valore.length-1) + ".";
	//	//alert('ciao');
	//	// Se ho giā un punto nella stringa, semplicemente
	//	// restituisco FALSE
	//	
	//	try
	//		{
	//		if(document.all) 
	//			window.event.keyCode = 46;
	//		else
	//			event.which = 46;
	//		}
	//	catch(e)
	//		{
	//		}
	//	eventKeycode == 46;
	//	}
	if (negatives)
	        {
		// I must accept also the negative sign and the Plus sign
		 esi = ( (eventKeycode < 48 || eventKeycode > 57) && eventKeycode!=46 && eventKeycode != 45 && eventKeycode != 43 && eventKeycode != 44) ;
		}
	else
	        {
		// I must accept only figures and "."
		esi = ( (eventKeycode < 48 || eventKeycode > 57) && eventKeycode!=46 && eventKeycode!=44 );
		}
	if(esi)
		{	
		return false;
		}	
	else if((eventKeycode==46 || eventKeycode==44) && dec==0)
		{
		return false;
		}
	
	if( ( eventKeycode==46 || eventKeycode==44 ) && ( valore.indexOf(".")!=-1 || valore.indexOf(",")!=-1 ) )
		{
		// Nella stringa č gič presente un punto (o virgola). Non posso digitare
		// punti o virgole
		return false;
		}
	
	// --------------------------------------------------------------------------------------------
	// se digito un "meno" o "+", nella stringa non debbono essere presenti altri "meno" o "+"
	// --------------------------------------------------------------------------------------------
	
	if( ( eventKeycode==45 || eventKeycode==43 ) && ( valore.indexOf("-") != -1 || valore.indexOf("+") != -1  ) )
		{
		return false;
		}
	
	
	if((campo.maxLength-1)-dec == valore.length && dec!=0)
		{
		if(eventKeycode==46 || eventKeycode==44)
		        {
			return true;
			}
		else if(valore.indexOf(".")!=-1 || valore.indexOf(",")!=-1 )
		        {
			return true;
			}
		else if(  valore.indexOf(".")!=-1 && (valore.length-valore.indexOf(".")+1>dec) )
			{
			return false;
			}
		else 
			{
			return true;
			}
		}
	}
return true;
}

