// c.marinelli
var ie = (navigator.appName.indexOf('Microsoft Internet Explorer')>-1); 

// permette inserimento di cifre da 0 a 9 e della virgola --------------
// viene impedito l'inserimeno di una seconda virgola ------------------
// viene impedito l'inserimento di pių di due cifre dopo la virgola ----

function KeyPressImporto(importo,e) {
	var k;
	var valore = importo.value;
	if(ie) {
	 	k = e.keyCode;
	} else {
	 	k = e.which;
	}
	
	if( (k > 47 && k < 58) || (k == 44) || (k == 46)){
	
		// Impediamo di digitare due volte la virgola
		if (k == 44 || k == 46){
			if (importo.value.split(",").length == 2)return false;
 		}
 	}else {
		if(k != 8 && k !=0) return false;
	}
}

// toglie gli zeri iniziali e inserisce i punti delle migliaia ------------- 
function KeyUpImporto(textinput,e) {
	var k;
	if(ie) {
	 	k = e.keyCode;
	} else {
	 	k = e.which;
	}
	
	// consentito inserimento del punto dal tasterino numerico, trasformato in virgola
	var val = textinput.value;
	var dtArray = val.split(",");
	if (k == 110){ // punto tasterino numerico
		if (dtArray.length < 2){
			val = val.substring(0, val.length-1);
			val = val + ',';
			textinput.value = val;
		}
	}
	
	
	if (k !=188 && k != 37 && k !=110) {
		
		
		valore=textinput.value.replace(/\./g,"");
	
		
		// Conserva i centesimi
		centesimi="";
		
		if (valore.indexOf(",")>-1) {
			espr=valore.split(",");
			valtomod=valore.substring(0,valore.indexOf(","));
			centesimi=valore.substring(valore.indexOf(",")+1,valore.length+1);
			if (centesimi.length > 2){ centesimi = centesimi.substring(0,2); } 
			valore=valtomod;
		}
		
		// Toglie gli zeri iniziali
		
		if (valore.length !=1 && valore.substr(0,1)=='0') {
			valore=valore.substr(1,valore.length-1);
		}
		
		temp="";
		// Aggiungi i punti
		while (valore.length>0) {
			if (valore.length>3) {
				temp="."+valore.substr(valore.length-3,3)+temp;
				valore=valore.substr(0,valore.length-3);
			} else {
				temp=valore+temp;
				valore="";
			}
		}
		if (centesimi!="") {
		 	temp=temp+","+centesimi;
		}
		
		textinput.value=temp;
		
	}
}


function ctrlFormatoEuro(elemento){
	if ( elemento.indexOf(",") == elemento.length - 2)  
    	return true;
    else
    	return false; 
 	
}

function formattaEuro(elemento){
	   
	   //if (ctrlFormatoEuro(elemento))
	   //		return elemento;
	   		
	    var temp = elemento.replace(".",",");
	    var arrayDot = temp.split(",");
	  
	    if (arrayDot){
	        if (arrayDot.length == 1)
	            temp = temp + ',00';
	        if (arrayDot.length == 2) {
	            if ( arrayDot[1] > 2) {
	                temp = arrayDot[0] + ',' + arrayDot[1].substr(0,2);
	            }else if (arrayDot[1] == 1) {
	                temp = arrayDot[0] + ',' + arrayDot[1] + '0';
	            }else
	            	temp = arrayDot[0] + ',' + arrayDot[1];
	        }
	           
	    }
	    
	    // aggiunto 08/09/2006 c.marinelli
	    if (temp.substr(0,1) == '-'){
	        temp = formattaEuroMigliaia(temp.substr(1));
	    	temp = '-' + temp;
	    }else
	    	temp = formattaEuroMigliaia(temp);
	     
	    return temp;
	}

function formattaEuroMigliaia(valore){
	
	if (valore.indexOf(",")>-1) {
		espr=valore.split(",");
		valtomod=valore.substring(0,valore.indexOf(","));
		centesimi=valore.substring(valore.indexOf(",")+1,valore.length+1);
		if (centesimi.length > 2){ centesimi = centesimi.substring(0,2); } 
		valore=valtomod;
	}
		
	// Toglie gli zeri iniziali
	
	if (valore.length !=1 && valore.substr(0,1)=='0') {
		valore=valore.substr(1,valore.length-1);
	}
	
	temp="";
	// Aggiungi i punti
	while (valore.length>0) {
		if (valore.length>3) {
			temp="."+valore.substr(valore.length-3,3)+temp;
			valore=valore.substr(0,valore.length-3);
		} else {
			temp=valore+temp;
			valore="";
		}
	}
	if (centesimi!="") {
	 	temp=temp+","+centesimi;
	}

	return temp;
}


function KeyPressDouble(importo,e, maxDecimal) {
	var k;
	var valore = importo.value;
	if(ie) {
	 	k = e.keyCode;
	} else {
	 	k = e.which;
	}
	if (maxDecimal == '')
		maxDecimal = 2;
	if( (k > 47 && k < 58) || (k == 44) || (k == 46)){
	
		// Impediamo di digitare due volte la virgola k = 46 tasterino numerico
		if (k == 44 || k == 46){
			if (importo.value.split(",").length == 2)
				return false;
			
 		}else{
 			var  impArray = valore.split(",");
 			if (impArray.length == 2){
	 			if (impArray[1].length == maxDecimal){
	 				return false;
	 			}
 			}
 		}
 		
 	}else {
		if(k != 8 && k !=0) return false;
	}
}


// toglie gli zeri iniziali e inserisce i punti delle migliaia ------------- 
function KeyUpDouble(textinput,e) {
	var k;
	if(ie) {
	 	k = e.keyCode;
	} else {
	 	k = e.which;
	}
	
	// consentito inserimento del punto dal tasterino numerico, trasformato in virgola
	var val = textinput.value;
	var dtArray = val.split(",");
	if (k == 110){ // punto tasterino numerico
		if (dtArray.length < 2){
			val = val.substring(0, val.length-1);
			val = val + ',';
			textinput.value = val;
		}
	}
	
    // Toglie gli zeri iniziali
    var temp;
	if (k !=188 && k != 37 && k != 46 && k != 39 && k !=110) {
		var dtArray1 = val.split(",");
		if (dtArray1.length == 2){
			temp = dtArray1[0];
			if (temp.length > 1 && temp.substr(0,1)=='0') {
				temp = temp.substr(1,temp.length-1);
			}
			val = temp + ',' + dtArray1[1];
		}else{	
			if (val.length > 1 && val.substr(0,1)=='0') {
				val=val.substr(1,val.length-1);
			}
		}
		textinput.value= val;
	}
	
	
}


//----permette scrittura solo numeri da 0 a 9 -----------------------
function KeyPressOnlyNum(e) {

var k;
if(ie) {
 	k = e.keyCode;
} else {
 	k = e.which;
}
if((k < 48) || (k > 57)) 
	{
		// Serve per abilitare le operazioni di modifica cancella	per browser diversi da IE
		if(k != 8 && k !=0) return false;				
	}
}
//-------------------------------------------------------------------------------------------


//-------------//-------------------  CONTROLLO CODICE FISCALE E PARTITA IVA ---------------//-----------------//

// --------- controllo se la stringa č solo composta da numeri
function isOnlyNumber(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
//----->>>>>>>>>>>>>>>>>>>>>>>>>>> controlli per la data >>>>>>>>>>>>>>>>>>>>>>>>>>>><
//----permette scrittura solo numeri da 0 a 9 pių lo "/"  -----------------------
function KeyPressOnlyDate(e) {
var k;
if(ie) {
 	k = e.keyCode;
} else {
 	k = e.which;
}
if((k < 47) || (k > 57)) 
	{
		// Serve per abilitare le operazioni di modifica cancella	per browser diversi da IE
		if(k != 8 && k !=0) return false;				
	}
}
//_____________________________________________________
var bordo = ''
var bordoColor = '';
function attivaField(objField){
	objField.style.backgroundColor = '#ECFFFF';
}
function disattivaField(objField){
	objField.style.backgroundColor = '';
	
}


function disabilitaCampo(campo){
	campo.readOnly = true;
	campo.style.backgroundColor="#E8E8E8";
	campo.style.color="#E8E8E8";
	
}
function abilitaCampo(campo){
	campo.readOnly = false;
	campo.style.backgroundColor="#FFFFFF";
	campo.style.color="#000000";
	
}

			function assegnaXMLHttpRequest() {
				var XHR = null,
			    browserUtente = navigator.userAgent.toUpperCase();
				if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
			  		XHR = new XMLHttpRequest();
			 	else if(
			  		window.ActiveXObject &&
			  		browserUtente.indexOf("MSIE 4") < 0
			 		) {
					if(browserUtente.indexOf("MSIE 5") < 0)
						XHR = new ActiveXObject("Msxml2.XMLHTTP");
			  		else
			   			XHR = new ActiveXObject("Microsoft.XMLHTTP");
			 	}
			 	return XHR;
			} 
