function number_format(a, b, c, d) {
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i + f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}

function pausecomp(millis) {
	//alert(millis);

	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); }
	while(curDate-date < millis);
} 

function roundTo(decimalpositions)
{
    var i = this * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}
Number.prototype.roundTo = roundTo; 


function updateValoriRiga(codRiga, quantita, prezzoListino, sconto, prezzoNetto, quantitaold) {
	
	var elementQuantita = 'quantita' + codRiga;
	var elementPrezzoListino = 'prezzolistino' + codRiga;
	var elementSconto = 'sconto' + codRiga;
	var elementPrezzoNetto = 'prezzonetto' + codRiga;
	var elementMargine = 'margine' + codRiga;
	var elementMarginePercentuale = 'marginepercentuale' + codRiga;
	var elementPrezzoRivenditore = 'prezzorivenditore' + codRiga;
	var elementQuantitaOld = 'quantitaold' + codRiga;
	
	/*alert('CodRiga = ' + codRiga);
	alert('Quantita = ' + quantita);
	alert('Prezzo Listino = ' + prezzoListino);
	alert('Sconto = ' + sconto);
	alert('Prezzo Netto = ' + prezzoNetto);*/

	//Controlli vari sui campi --------------------------------------------------
	if ($(elementQuantita).value == '') {
		alert('Attenzione! Il campo Quantità non può essere vuoto');
		exit;
	}
	if ($(elementPrezzoListino).value == '') {
		alert('Attenzione! Il campo Prezzo listino non può essere vuoto');
		exit;
	}
	if ($(elementSconto).value == '') {
		alert('Attenzione! Il campo Sconto non può essere vuoto');
		exit;
	}
	if ($(elementPrezzoNetto).value == '') {
		alert('Attenzione! Il campo Prezzo netto non può essere vuoto');
		exit;
	}
	
	if (isNaN($(elementQuantita).value)) {
		alert('Attenzione! Il campo Quantità non è numerico');
		exit;
	}
	if (isNaN($(elementPrezzoListino).value)) {
		alert('Attenzione! Il campo Prezzo listino non è numerico');
		exit;
	}
	if (isNaN($(elementSconto).value)) {
		alert('Attenzione! Il campo Sconto non è numerico');
		exit;
	}
	if (isNaN($(elementPrezzoNetto).value)) {
		alert('Attenzione! Il campo Prezzo netto non è numerico');
		exit;
	}
	//---------------------------------------------------------------------------
	 
	if (prezzoListino != null && prezzoListino != '') {
		var newPrezzoNetto = (parseFloat(prezzoListino) - ((parseFloat(prezzoListino) * $(elementSconto).value) / 100)) * $(elementQuantita).value;
		//alert(newPrezzoNetto);
		node = $(elementPrezzoNetto);
		node.value = newPrezzoNetto.roundTo(2);
		//node.value = number_format(newPrezzoNetto, ',', '.');
		
		var newMargine = newPrezzoNetto - parseFloat($(elementPrezzoRivenditore).innerHTML);
		Element.update(elementMargine, newMargine.roundTo(2));
		var newMarginePerc = (newMargine * 100) / newPrezzoNetto;
		Element.update(elementMarginePercentuale, newMarginePerc.roundTo(2));
		return;
	}
	
	if (sconto != null && sconto != '') {
		var newPrezzoNetto = ($(elementPrezzoListino).value - (($(elementPrezzoListino).value * sconto) / 100)) * $(elementQuantita).value;
		//alert(newPrezzoNetto);
		node = $(elementPrezzoNetto);
		node.value = newPrezzoNetto.roundTo(2);
		
		var newMargine = newPrezzoNetto - $(elementPrezzoRivenditore).innerHTML;
		Element.update(elementMargine, newMargine.roundTo(2));
		var newMarginePerc = (newMargine * 100) / newPrezzoNetto;
		Element.update(elementMarginePercentuale, newMarginePerc.roundTo(2));
		return;
	}
	
	if (quantita != null && quantita != '') {
	
		newPrezzoNetto = ($(elementPrezzoNetto).value / quantitaold) * quantita;
		//alert('Quantita OLD: ' + quantitaold + ' Prezzo Netto: ' + $(elementPrezzoNetto).value + ' New Prezzo Netto: ' + newPrezzoNetto);
		node = $(elementPrezzoNetto);
		node.value = newPrezzoNetto.roundTo(2);
		
		var newPrezzoRivenditore = ($(elementPrezzoRivenditore).innerHTML / quantitaold) * quantita;
		Element.update(elementPrezzoRivenditore, newPrezzoRivenditore.roundTo(2));
		
		var newMargine = newPrezzoNetto - newPrezzoRivenditore;
		Element.update(elementMargine, newMargine.roundTo(2));
		var newMarginePerc = (newMargine * 100) / newPrezzoNetto;
		Element.update(elementMarginePercentuale, newMarginePerc.roundTo(2));
		$(elementQuantitaOld).innerHTML = quantita;
		return;
		
	}
	
	if (prezzoNetto != null && prezzoNetto != '') {
	
		newSconto = ((($(elementPrezzoListino).value * $(elementQuantita).value) - prezzoNetto) * 100) / ($(elementPrezzoListino).value * $(elementQuantita).value);
		node = $(elementSconto);
		node.value = newSconto.roundTo(2);
		
		var newMargine = prezzoNetto - $(elementPrezzoRivenditore).innerHTML;
		Element.update(elementMargine, newMargine.roundTo(2));
		var newMarginePerc = (newMargine * 100) / prezzoNetto;
		Element.update(elementMarginePercentuale, newMarginePerc.roundTo(2));
		return;
		
	}	
	

}

function fade(objId) {
	$(objId).fade({duration: 6.0});
	$(objId).show();
}
