function controlla(elemento, testo, lunghezza) {
	
	campo = document.getElementById(elemento); 
	
	nuovo = testo.replace(",",".");
	campo.value = nuovo;
	
	var ok = false;
	var inserito = false;
	
	for(i=0; i<lunghezza; i++) {
		var lettera = nuovo.charAt(i);
		
		if(lettera == ",")
		{
			if(inserito == true)
			{
				campo.value = '';
			}else{
				inserito = true;
			}
		}
		
		if(lettera == ",")
		{
			if(inserito == true)
			{
				campo.value = '';
			}else{
				inserito = true;
			}
		}
		
		if (lettera == " ") {
			campo.value = '';
			break;
		}
		
		if(lettera != "," && lettera != "." && lettera != "" && lettera != " ")
		{
			is_numero(elemento, lettera);
		}
	}
}

function is_numero(elemento, numero){
	campo = document.getElementById(elemento); 
	if (!/^([0-9])*$/.test(numero))
	{
		campo.value = '';
	}
}

function is_valid_quantita(elemento, numero){
	campo = document.getElementById(elemento); 
	if (!/^([0-9])*$/.test(numero))
	{		
		campo.value = '0';
		campo.focus();
		alert("Il valore che hai inserito non è conforme!");
	}
	if(campo.value == "" || campo.value == " ")
	{
		campo.value = '0';	
	}
}

function roundTo(elemento, numero)
{
	campo = document.getElementById(elemento); 
	var numeroArray = numero.split(".");
	var formattazione = "";
	
	for(i=0; i<numeroArray[0].length; i++) {
		var lettera = numeroArray[0].charAt(i);
		if(lettera == " ")
		{
			campo.value = '';
			break;
		}
	}
	
	if(!numeroArray[0])
	{
		numeroArray[0] = "0";
	}	
	
	if(!numeroArray[1])
	{
		campo.value = numeroArray[0] + "." + "00";
	}
	
	if (/^([0-9])*$/.test(numeroArray[0]))
	{
		campo.value = numeroArray[0] + ".00";
	}
	
	if (numero.indexOf(".") != (-1))
	{
	    if (numeroArray[1].length == 1)
	    {
	        numero += 0;
	        campo.value = numero;
	    }
	    
	    if (numeroArray[1].length > 1)
	    {
	    	formattazione = numeroArray[1].substring(0, 2);
	    	numero = numeroArray[0] + "." + formattazione;
	    	campo.value = numero;
	    }
	}
}

/*
function calcola(id)
{
	valoreName = document.getElementById(id);
	numero = valoreName.value;
	
	pezziTotaleCampo = document.getElementById('totalPezzi');
	
	valore = pezziTotaleCampo.value +
}
*/

function trim(id, stringa)
{
	while (stringa.substring(0,1) == ' ')
	{
		stringa = stringa.substring(1, stringa.length);
	}
	while (stringa.substring(stringa.length-1, stringa.length) == ' ')
	{
		stringa = stringa.substring(0,stringa.length-1);
	}
	
	inputText = document.getElementById(id);
	inputText.value = stringa;
}

function controlloCampiInvioModulo()
{
	
	document.forms[0].nomeprod.style.background = "";
	document.forms[0].peso.style.background = "";
	document.forms[0].tot.style.background = "";
	document.forms[0].prezzo1.style.background = "";
	document.forms[0].iva.style.background = "";
	document.forms[0].breve.style.background = "";
	
	
	if (document.forms[0].nomeprod.value == ""){
		alert("Nome del prodotto non valido!");
		document.forms[0].nomeprod.style.background = "#FF5B5B";
		document.forms[0].nomeprod.focus();
		return false;
	}
	
	if (document.forms[0].peso.value == ""){
		alert("Il peso del prodotto non può essere uguale a 0 (zero)");
		document.forms[0].peso.style.background = "#FF5B5B";
		document.forms[0].peso.focus();
		return false;
	}
	
	if (document.forms[0].tot.value == "0.00"){
		alert("Il listino al pubblico non è valido! (0.00€)");
		document.forms[0].tot.style.background = "#FF5B5B";
		document.forms[0].tot.focus();
		return false;
	}
	
	if (document.forms[0].prezzo1.value == "0.00"){
		alert("Il listino non è valido! (0.00€)");
		document.forms[0].prezzo1.style.background = "#FF5B5B";
		document.forms[0].prezzo1.focus();
		return false;
	}
	
	if (document.forms[0].iva.value == "0"){
		alert("Valore per il campo IVA non valido! (0%)");
		document.forms[0].iva.style.background = "#FF5B5B";
		document.forms[0].iva.focus();
		return false;
	}
	
	if (document.forms[0].breve.value == ""){
		alert("La descrizione breve è obbligatoria");
		document.forms[0].breve.style.background = "#FF5B5B";
		document.forms[0].breve.focus();
		return false;
	}
	
}

function salva(id,mode,text)
{
	elemento = document.getElementById(id);

	if(mode == 2)
	{
		document.cookie = "sel3="+escape(text);
		return true;
	}
	
	if(mode == 3)
	{
		document.cookie = "rte1="+escape(text);
		return true;
	}
	
	if(mode == 4)
	{
		if(elemento.checked)
		{
			document.cookie = "statochk=1";
		}else{
			document.cookie = "statochk=0";
		}
		return true;
	}
	
	if(elemento.value != "")
	{
		document.cookie = id+"="+escape(elemento.value);
	}
	return true;
}

function svuotaModulo()
{
	/*
	document.getElementById('filtroImmagine').value = 1;
	document.getElementById("selezioneck").selectedIndex = 0;
	*/
	//document.getElementById('nomemodulo').value = '';
	/*
	document.getElementById('codice').value = '';
	document.getElementById('prezzoda').value = '0.00';
	document.getElementById('prezzoa').value = '0.00';
	document.getElementById("categorie").selectedIndex = 0;
	document.getElementById("marche").selectedIndex = 0;
	document.getElementById("checkbox").selectedIndex = 1;
	*/
	return true;
}