var errorMsg="";
var oldValue=new String("");
function setOldValue(obj)
{
	
	oldValue=new String(obj.sourceIndex+"-"+obj.value);
	
}

function debug(messaggo,dove)
{
	if(dove==undefined)
		alert("DEBUG "+messaggo);
	else if(dove=="S")
		window.status="DEBUG "+messaggo;
	else if(dove == "F" )
	{
		var ora=new Date().getTime();
		scriviLog("c:\\clientlog.txt",ora+ " - " +messaggo);	
	}	
}
function scriviLog(documento,dati)
{
	var fso, f, r;
	var ForReading = 1, ForWriting = 2, ForAppending= 8 ;
	try
	{
		fso = new ActiveXObject("Scripting.FileSystemObject");
		
	//	f = fso.CreateTextFile(documento,true);
		if(!fso.FileExists(documento))
		{
			f = fso.CreateTextFile(documento);
			f.close();	
		}
		f = fso.OpenTextFile(documento, ForAppending, true);
		f.WriteLine(dati);
			
		f.Close();
	}
	catch(err)
	{
		window.status=dati;
		return null;
	}
}
function validaCampoData(dObject)
{
	var desc=null;
 	
 	fieldName = dObject.name;
 	if(dObject.parentNode.tagName=="LABEL")
	{
	
		desc=dObject.parentNode.innerHTML;
		desc=desc.substr(0,desc.indexOf("<"));
	}	
	else
	{
		desc=fieldName;
	}
	var campo=dObject.value;
	if(verificaData(campo)==false)
	{
		errorMsg= "Valore "+desc+" non Valido";
		alert(errorMsg);
		var indiceCampo=dObject.sourceIndex+"-";
		if( oldValue.substr(0, indiceCampo.length)==indiceCampo)
			dObject.value=oldValue.substr(indiceCampo.length);
		return false;	
	}	
	return true;
}

function verificaData(campo)
 {
	var bar1=campo.indexOf("/");
	var bar2=campo.indexOf("/",bar1+1);
	var gg=campo.substr(0,bar1);
	var mm=campo.substr(bar1+1,bar2-bar1-1);
	var aa=campo.substr(bar2+1,4);
	var DATA_INI=new  Date(aa , mm-1 , gg);
	var DATA_INS=""+(gg*1)+(mm*1)+aa;
	var DATA_CONF =""+(DATA_INI.getDate())+(DATA_INI.getMonth()+1)+DATA_INI.getFullYear();
	if(DATA_CONF!=DATA_INS )
	{
		return false;
	}
//	object.value=""+(DATA_INI.getDate())+"/"+(DATA_INI.getMonth()+1)+"/"+DATA_INI.getFullYear();
	return true;
 }
 
 function campoData(e)
 {
 	var ardata="0123456789/";
 	controllaImmissione(e,ardata);
 		
 }

 function campoNumerico(e)
 {
 	var arNumeric = "0123456789.";
 	controllaImmissione(e,arNumeric);
 		
 }
 function controllaImmissione(e,caratteri)
 {
 	var bRc = true;
 	if(window.event!=undefined)
 	{
 		e = window.event;
 		myObj = e.srcElement;
 		codice=e.keyCode;
 	}
 	else	
 	{
 		var myObj = e.currentTarget 
 		codice=e.which;
 	
 	}
 		
 	//window.status=e.keyCode+"-"+String.fromCharCode( e.which  ) ;
 		
 	if ("8,9,13,37,38,39,40".indexOf(codice)==-1)
 	{
 		var carImm=String.fromCharCode( codice );
 		if( caratteri.indexOf( carImm ) == -1 )
		{
			bRc = false;
		}	
	}
	if(bRc == false  && window.event==undefined)
		e.preventDefault();
	else 
		e.returnValue=bRc;
	
 }
 
 
 
 
 
 
 function campoNumerico2(event)
 {
 		alert(event);
 		//var event=window.event;
 		var myObj = event.srcElement;
 		if(event.keyCode==44)
			event.keyCode=46;
		var valoreCampo=new String(myObj.value);
		valoreCampo=valoreCampo.replace(',','.')
		var carImm=String.fromCharCode( event.keyCode );
		if( arNumeric.indexOf( carImm ) == -1 )
		{
			bRc = false;
		}
		if(	",.".indexOf(carImm)!=-1 && valoreCampo.indexOf('.')!=-1)
			bRc = false;
		return bRc;	
 }
