
function GestLed(NomImg,Img,ValBarra) {
	if (navigator.appVersion.substring(0,1) >= 3)
		document.images[NomImg].src=eval(Img + ".src");
	window.status=ValBarra; 
}
		

function ControllaDigit(virgola, punto, old, meno) {
	
	var kc = event.keyCode;
    
	if (old == null) {
	    old = true;
	}
	
	if (virgola == null) {
	    virgola = false;
	}
	
	if (punto == null) {
	    punto = false;
	}
	
	if (meno == null) {
	    meno = false;
	}
	if (kc >= 96 && kc <=105 && old) {    		//* Tastierino numerico
		kc -= 48;
	}

	var di = (
			kc == 39 ||					//* Tasti cursore
			kc == 37 ||
			(kc >=48 && kc <=57) ||     //* Numeri
			kc == 9 ||                  //* Tab
			kc == 13 ||                 //* Invio
			kc == 35 ||                 //* Fine
			kc == 36 ||                 //* Inizio
			kc == 8 ||               
			(kc == 44 && virgola) ||    //* virgola           
			(kc == 46 && punto)   ||    //* Punto
			(kc == 45 && meno)          //* meno
		);
	
	if (! di) {
		event.returnValue = false;
	} 
}


function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
	if (fromString == "") {
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		
		// Find a string that doesn't exist in the inputString to be used
		// as an "inbetween" string
		while (midString == "") {
			for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
				if (fromString.indexOf(tempMidString) == -1) {
				midString = tempMidString;
				i = midStrings.length + 1;
				}
			}
	} // Keep on going until we build an "inbetween" string that doesn't exist
	// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
	while (temp.indexOf(fromString) != -1) {
		var toTheLeft = temp.substring(0, temp.indexOf(fromString));
		var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
		temp = toTheLeft + midString + toTheRight;
	}
	// Next, replace the "inbetween" string with the "toString"
	while (temp.indexOf(midString) != -1) {
		var toTheLeft = temp.substring(0, temp.indexOf(midString));
		var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
		temp = toTheLeft + toString + toTheRight;
	}
	} // Ends the check to see if the string being replaced is part of the replacement string or not
	return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function controllaNumero(n, min, max, messaggio) {
	if (min != null && n < min) {
		if (messaggio != null && messaggio != "") {
			alert(messaggio);
		}
		n = min;
	}
	if (max != null && n > max) {
		if (messaggio != null && messaggio != "") {
			alert(messaggio);
		}
		n = max;
	}
	return n;
}

function InteroDaCampo(obj) {
	try {
		var ris = parseInt(replaceSubstring(replaceSubstring(obj.value,".",""),",",""));
		if (ris == null || isNaN(ris)) {
			ris = 0;
		}
		return ris;
	} catch (e) {
		return 0;
	}
}

function FormatoCampoIntero(obj) {

	var v		= 0;
	var ris		= "";
	var ris1	= "";
		
	v = InteroDaCampo(obj); //parseInt(replaceSubstring(replaceSubstring(obj.value,".",""),",",""));

	if (isNaN(v) || v < 0) {
		v = 0;
	}
	
	while (v > 0) {
		if (ris != "") {
			ris = "." + ris;
		}
		ris1 = (v % 1000).toString();
		v = Math.floor(v / 1000);
		if (v > 0) {
			switch (ris1.length) {
				case 1:
					ris1 = "00" + ris1;
					break;
				case 2:
					ris1 = "0" + ris1;
					break;
			}
		}
		ris = ris1 + ris;
	}
	
	if (ris == "") {
		ris = "0";
	}
		
	obj.value = ris;	
	
}

function FormatoCampo(nomeItem) {

	var l_prezzo = 0;
		
	l_prezzo = parseFloat(replaceSubstring( replaceSubstring(nomeItem.value,".",""),",","."));

	if (l_prezzo > 0) {
		nomeItem.value = toEuro(l_prezzo);
	} else {
		nomeItem.value = "0,00";
	}
		
}

function FormatoCampoInv(nomeItem) {			
	var l_prezzo = 0;
		
	l_prezzo = parseFloat(replaceSubstring( replaceSubstring(nomeItem,".",""),",","."));

	if (l_prezzo > 0) {
		return l_prezzo;
	} else {
		return  "0,00";
	}
		
}

//2005-03-22 
function formattaKm(Km) { 

	aNumber = new Number(Km); 
	amtStr = aNumber.toString(); 

	if ((pos = amtStr.indexOf(".")) == -1) {
		amtStr = amtStr; 
		pos = amtStr.length; 
	} 

	// simone Euro = amtStr.substring(0, pos-1).toString(); 
	KmPercorsi = amtStr.substring(0, pos).toString(); 
	
	if (pos > 3) {
		KmPercorsi = amtStr.substring(0,pos-3) + "." + amtStr.substring(pos-3,pos);
	}
	
	return KmPercorsi; 
}


function toEuro(amount) { 

	amount = Math.round(amount*100)/100; 

	aNumber = new Number(amount); 
	amtStr = aNumber.toString(); 

	if ((pos = amtStr.indexOf(".")) == -1) {
		amtStr = amtStr; 
		pos = amtStr.length; 
	} 

	Euro = amtStr.substring(0, pos).toString(); 
	
	if (pos > 3) {
		Euro = amtStr.substring(0,pos-3) + "." + amtStr.substring(pos-3,pos);
	}
	
	cents = amtStr.substring(pos+1, amtStr.length); 
	if (cents.length == 0) { 
		outStr = Euro + "," + "00"; 
	} else if (cents.length == 1) { 
		outStr = Euro + "," + cents + "0"; 
	} else { 
		outStr = Euro + "," + cents; 
	}
	
	return outStr; 
}

function seleziona(campo) {
	if (campo != null) {
		campo.value = replaceSubstring(campo.value, ".", "");
		campo.select();
	}
}

function VisualizzaErrori(ListaErrori){

	//* GZ 2005_04_18 * Modificato per creare un Alert invece di un pop-up
	alert("Correggere i seguenti errori:\n\n" + ListaErrori);
	
}

function trim(str) {
	return str.replace(/^\s*|\s*$/g, "");
}

//**********************************************************************
//* 2005-05-11 GZ * Aggiunti per semplificare l'accesso ai campi html
//**********************************************************************

//* True se il valore del oggetto html è uguale al valore passato
function ctrCampo(campo, val, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		if (obj.tagName.toUpperCase() == "SPAN") {
			return (obj.innerText == val);
		} else if (obj.type != null){
			if (obj.type.toUpperCase() == "RADIO") {
				return obj.checked;
			} else {
				var v = obj.value;
				if (v != null) {
					return (v == val);
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}

//* Restituisce il value dell'oggetto html
function valCampo(campo, frm, def) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		if (obj.tagName.toUpperCase() == "SPAN") {
			return obj.innerText;
		} else if (obj.type != null) {
			if (obj.type.toUpperCase() == "RADIO" || obj.type.toUpperCase() == "CHECKBOX") {
				return obj.checked;
			} else {
				var v = obj.value;
				if (v != null) {
					return v;
				} else {
					return def;
				}
			}
		} else {
			return def;
		}
	} else {
		return def;
	}
}

//* Rende visibile / invisibile un campo
function hidCampo(campo, val, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		if (val) {
			obj.style.visibility = "hidden";
		} else {
			obj.style.visibility = "visible";
		}
	}
}

//* Imposta il value dell'oggetto html
function setCampo(campo, val, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo); 
	}
	if (obj != null) {
		if (obj.tagName.toUpperCase() == "SPAN") {
			obj.innerText = val;
		} else if (obj.type != null) {
			if (obj.type.toUpperCase() == "RADIO" || obj.type.toUpperCase() == "CHECKBOX") {
				obj.checked = val;
			} else {
				obj.value = val;
			}
		}
	}
}

function exiCampo(campo, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		return true;
	} else {
		return false;
	}
}

//* Abilita / disabilita un campo
function abiCampo(campo, a, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		obj.disabled = !a;
	}
}

//* Imposta il campo "ReadOnly"
function rdnCampo(campo, a, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		obj.ReadOnly = a;
	}
}

//* Cambia la classe CSS di un campo
function cssCampo(campo, css, frm) {
	var obj;
	if (frm == null) {
		obj = document.getElementById(campo);
	} else {
		obj = frm.document.getElementById(campo);
	}
	if (obj != null) {
		obj.className = css;
	}
}

//* Calcola la string per piazzare un PopUp al centro dello schermo..
function centerScreenPopUp(wPU, hPU) {

	try {
		var x = window.screenLeft; 
		var y = window.screenTop; 
		var w = window.screen.availWidth; 
		var h = window.screen.availHeight; 

		var cntx = x + Math.round((w - wPU) / 2); 
		var cnty = y + Math.round((h - hPU) * .4);
						
		return "left=" + cntx + ",top=" + cnty + ",width=" + wPU + ",height=" + hPU; 
		
	} catch (e) {
		return "left=0,top=0,width=" + wPU + ",height=" + wPU; 
	}

}

//* Apre un PopUp centrandolo nello schermo e dandogli il fuoco....
function ApriPopUp(url, w, h) {
	var popupWin;
	popupWin = window.open(url, "_blank", centerScreenPopUp(w, h)); 
	popupWin.focus();
}

//* Crea una passoword da un alfabeto
function CreaPassword(lunghezzaPassword, alfabeto) {

    if (alfabeto == null) {
        alfabeto    = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-";
    }
    if (lunghezzaPassword == null) {
        lunghezzaPassword    = 8;
    }
    
    var password    = "";
    var random      = 0;
    
    for(var i = 0; i < lunghezzaPassword; i++) {
        random      = Math.floor(Math.random() * (alfabeto.length + 1));
        password    += alfabeto.charAt(random);
    }

    return password;

}        

//* Controlla se un valore passato come parametro è un numero
function IsNumeric(valore, negativi, interi)
{   
    var m_negativi    = 0;
    var m_interi      = 1;
    
    if(negativi != null) {
       m_negativi = negativi; 
    }
    
    if(interi != null) {
        m_interi = interi;
    }
    
    CaratteriValidi = "0123456789";
    
    if(m_negativi == 1) {
        CaratteriValidi += "-";
    }
    
    if(m_interi == 0) {
        CaratteriValidi += ",";
    }
    
    if (valore == null || valore.length < 1) {
        return false;
    }
    
    //* Controllo se ci sono numeri dopo il separatore decimale
    if(valore.substr(valore.length -1, 1) == ",") {
        return false;
    }
    
    for (var i = 0; i < valore.length; i++) {
        var ch  = valore.substr(i, 1);
        var a   = CaratteriValidi.indexOf(ch, 0);
        
        if (a == -1) {
            return false;
        }
        
        //* Controllo se è già presente un separatore decimale
        if(ch == ",") {
            if(valore.indexOf(ch, 0) != i) {
                return false;
            }
        }
    }
    
    return true;
}

