// JavaScript Document

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function getX( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function decode(tbl,key,des,val,sup,id_answer)
{
argv=arguments;
argc=arguments.length;

if (argc >= 7)
        id_field = argv[6];
else
        id_field = '';
        
if (val == '')
        {
        document.getElementById(id_answer).value='';
        if (argc >= 7)
                document.getElementById(id_field).style.backgroundColor='';
        return;
        }

x=document.getElementById('decod_bridge_count').value;
y=parseInt(x) + 1;
if (y > 4)
        y = 0;
document.getElementById('decod_bridge_count').value = y;

if (argc >= 7)
        {
        document.getElementById(id_field).style.backgroundColor = 'yellow';
        appo_id_field = id_field;
        }
else
        appo_id_field = "";
//alert('id_answer=' + id_answer + '  id_field = ' + appo_id_field);
document.getElementById('decod_bridge' + x).src='decod_bridge.php?tbl=' + tbl + '&key=' + key + '&des=' + des + '&val=' + val + '&sup' + sup + '&id_answer=' + id_answer + '&id_field=' + appo_id_field;;
}


// 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 chknum(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;
}


function focus_amount(v)
{
if (v)
        {
        while ( v.indexOf(".") != -1 )
                {
                v = v.replace(".","");
                }
        v = v.replace(",",".");
        return(v);
        }
else
        return('');
}

function blur_amount(v)
{
if (v)
        {
        // -------------------------------------------
        // Trasformo dapprima i punti in virgole
        // ------------------------------------------------- 
        
        v = v.replace(".",",");

        // -----------------------
        // formatto il numero
        // -----------------------------
        
        if (v.indexOf(",") == -1)
                {
                // ----------------------
                // non ci sono virgole
                // ----------------------------
                r = '';
                for (i = 0 ; i < v.length ; i++)
                        {
                        r = v.substr(v.length - i - 1,1) + r;
                        if ( (i + 1) % 3 == 0 && i != v.length-1)
                                r = '.' + r;
                        }
                // --------------------------------
                // aggiungo alla fine due zeri decimali
                // -------------------------------------- 
                r = r + ',00';
                }
        else
                {
                // ---------------
                //  ci sono virgole
                // --------------------- 
                r = v.substr(v.indexOf(",") );
                for (i = 0 ; i < v.indexOf(",")  ; i++)
                        {
                        r = v.substr(v.indexOf(",") - i - 1,1) + r;
                        if ((i + 1) % 3 == 0 && i != v.indexOf(",")-1)
                                r = '.' + r;
                        }
                        
                // ----------------------------------------------
                // Se c'è solo una cifra decimale, aggiungo uno zero
                // ------------------------------------------------- 
                
                vi = r.indexOf(","); 
                lu = r.length;
                if (vi == (lu - 2) )
                      r = r + '0';
                }
        return (r);
        }
else
        return('');
}


// ------------------------------------------------------------------------------
// Check data functions
// ----------------------------------------------------------------------------

function chkdate(data)
{
return chkdate_dmy(data);
}

function chkdate_dmy(data)
        {
        if (data.length != 10)
                {
                return (1);  // Lunghezza illegare
                }
        else
                {
                
                // ----------
                // Giorno
                // ----------
                appo = data.substr(0,2);
                appo = appo * 1 ;
                if (appo < 1 || appo > 31)
                        return (2);   // Giorno illegale
                else
                        {
                        // ---------
                        // Mese
                        // ---------
                        appo = data.substr(3,2);
                        appo = appo * 1;
                        if (appo < 1 || appo > 12)
                                return(4);   // mese illegale
                        else
                                {
                                appo = data.substr(6,4);
                                appo = appo * 1;
                                if (appo < 1900 || appo > 2100)
                                        return(5);  // anno illegale
                                else
					{
					// Controllo di nuovo il giorno
					
					giorno = data.substr(0,2);
                        		giorno = giorno * 1;
                        		
					mese = data.substr(3,2);
                        		mese = mese * 1;
					
					anno = data.substr(6,4);
                                	anno = anno * 1;
					
					if (mese == 1 || mese == 3 || mese == 5 || mese == 7 || mese == 8 || mese == 10 || mese == 12)
						{
						max = 31;
						}
					else
						{
						if (mese == 2)
							{
							// verifico se l'anno è bisestile
							if (anno % 4 == 0 && anno % 100 != 0 || anno % 400 == 0)
								{
								max = 29;
								}
							else
								{
								max = 28;
								}
							}
						else
							{
							max = 30;
							}
						}
					if (giorno > max)
						{
						return(6);  // di nuovo giorno illegale
						}				
					}
                                }
                        }
                }

        return(0);
        }

function chkdate_mdy(data)
        {
        if (data.length != 10)
                {
                return (1);  // Lunghezza illegare
                }
        else
                {
                
                // ----------
                // Giorno
                // ----------
                appo = data.substr(3,2);
                appo = appo * 1 ;
                if (appo < 1 || appo > 31)
                        return (2);   // Giorno illegale
                else
                        {
                        // ---------
                        // Mese
                        // ---------
                        appo = data.substr(0,2);
                        appo = appo * 1;
                        if (appo < 1 || appo > 12)
                                return(4);   // mese illegale
                        else
                                {
                                appo = data.substr(6,4);
                                appo = appo * 1;
                                if (appo < 1900 || appo > 2100)
                                        return(5);  // anno illegale
                                else
					{
					// Controllo di nuovo il giorno
					
					giorno = data.substr(3,2);
                        		giorno = giorno * 1;
                        		
					mese = data.substr(0,2);
                        		mese = mese * 1;
					
					anno = data.substr(6,4);
                                	anno = anno * 1;
					
					if (mese == 1 || mese == 3 || mese == 5 || mese == 7 || mese == 8 || mese == 10 || mese == 12)
						{
						max = 31;
						}
					else
						{
						if (mese == 2)
							{
							// verifico se l'anno è bisestile
							if (anno % 4 == 0 && anno % 100 != 0 || anno % 400 == 0)
								{
								max = 29;
								}
							else
								{
								max = 28;
								}
							}
						else
							{
							max = 30;
							}
						}
					if (giorno > max)
						{
						return(6);  // di nuovo giorno illegale
						}				
					}
                                }
                        }
                }

        return(0);
        }

function norm_date(data)
{
return norm_date_dmy(data);
}

function norm_date_mdy(data)
{
return norm_date_dmy(data);
}

function norm_date_dmy(data)
{
if (data.length == 0)
	{
	return '';
	}
if (data.length == 10)
	{
	return data.substr(0,2) + "/" + data.substr(3,2) + "/" + data.substr(6,4);
	}
if (data.length == 8)
      {
      a = data.substr(2,1);
      b = data.substr(5,1);

      if (! ng2_isdigit(a) && ! ng2_isdigit(b) ) 
            {
            x = data.substr(6,2);
            if (x > 50)
                  return data.substr(0,6) + '19' + data.substr(6,2) ;
            else
                  return data.substr(0,6) + '20' + data.substr(6,2) ;
            }
      if (ng2_isdigit(data) )
            return data.substr(0,2) + '/' + data.substr(2,2) + '/' + data.substr(4,4);
      }
if (data.length == 6 && ng2_isdigit(data) )
      {
      x = data.substr(4,2);
      if (x > 50)
            return data.substr(0,2) + '/' + data.substr(2,2) + '/19' + data.substr(4,2);
      else
            return data.substr(0,2) + '/' + data.substr(2,2) + '/20' + data.substr(4,2);
      }    

return data;
   
}

function ng2_isdigit(a)
{
for (i = 0 ; i < a.length ; i++)
        {
        b = a.substr(i,1);
        if (b < '0')
        	{
                return false;
        	}
	if (b > '9')
		{
                return false;
                }
        }
return true;

}

function normalizzapdc(v)
{
if (v.length == 7)
        {
        r = "0" + v.substr(0,1) + ".0" + v.substr(1,1) + "." + v.substr(2,2) + ".0" + v.substr(4,3); 
        return(r);
        }

if (v.length == 10)
        {
        r = v.substr(0,2) + "." + v.substr(2,2) + "." + v.substr(4,2) + "." + v.substr(6,4); 
        return(r);
        }

if (v.length == 13)
        {
        r = v.substr(0,2) + "." + v.substr(3,2) + "." + v.substr(6,2) + "." + v.substr(9,4); 
        return(r);
        }
return(v);

}

function call_help(title,tabella,campi,intestazione,campiricerca,campodarestituire,campodarestituire_decod,sort,filtro,id_campo)
{

argv=arguments;
argc=arguments.length;

if (argc > 10)
        il_risultato_va_aggiunto = argv[10];   /* In genere questo parametro vale '1' o nulla */
else
        il_risultato_va_aggiunto = '';
        
        
visu=window.open('','visu','toolbar=no,status=no, menubar=no, width=800, height=600, scrollbars=yes, top=50, left=100'); 
visu.focus(); 
visu.document.write("<html><head>");
visu.document.write("<link rel='stylesheet' type='text/css' href='hstyle.css' />");
visu.document.write("<meta http-equiv='content-type' content='text/html; charset=iso-8859-16'>");
visu.document.write("</head>");


visu.document.write("<body onload=\"document.getElementById('roo').focus();\" onblur=\"window.close();\">");

visu.document.write("<div class='htitle'>" + title + "</div>");
visu.document.write("<form method='POST' name='form1' action='generichelp.php'>");
visu.document.write("<table border=0 width='100%'' class='htable1'>");
visu.document.write("<tr>");
visu.document.write("<td colspan=2 align='left'  class='htable1'>");
visu.document.write("Radice&nbsp;da&nbsp;ricercare");
visu.document.write("<input type=text size=20 name=help_root id='roo' class='hinput' value=''>&nbsp");
visu.document.write("<input type=submit name='but' class='hbutton' value='Ricerca'>");

//visu.document.write("<input type=hidden name=campodarestituire_decod value='" + campodarestituire_decod + "'>");
visu.document.write("<input type=hidden name=il_risultato_va_aggiunto value='" + il_risultato_va_aggiunto + "'>");
visu.document.write("<input type=hidden name=id_campo value='" + id_campo + "'>");
visu.document.write("<input type=hidden name=filtro value=\"" + filtro + "\">");
visu.document.write("<input type=hidden name=sort value='" + sort + "'>");

visu.document.write("<input type=hidden name=tabella value='" + tabella + "'>");
visu.document.write("<input type=hidden name=title value=\"" + title + "\">");
visu.document.write("<input type=hidden name=campi value='" + campi + "'>");
visu.document.write("<input type=hidden name=intestazione value='" + intestazione + "'>");
visu.document.write("<input type=hidden name=campiricerca value='" + campiricerca + "'>");
visu.document.write("<input type=hidden name=campodarestituire value='" + campodarestituire.replace(/'/g,"&#39;") + "'>");
visu.document.write("<input type=hidden name=campodarestituire_decod value='" + campodarestituire_decod.replace(/'/g,"&#39;") + "'>");
visu.document.write("</td>");
visu.document.write("</tr>");
visu.document.write("</table>");
visu.document.write("</form>");

visu.document.write("</head>");
visu.document.write("</html>");
visu.document.close();

}
