function findVPosAbsolute(id) {
	var obj = document.getElementById(id);
	var curtop = 0;
	if (obj.offsetParent) {
  		curtop = obj.offsetTop;
  		while (obj = obj.offsetParent) {
   			curtop += obj.offsetTop;
  		}
 	}
 	return curtop;
}

function scorri(id){
	var toppx = findVPosAbsolute(id);
	document.body.scrollTop = toppx - 20;
}

function popup(url) {
 	image1 = new Image();
	image1.src = url;
	height = image1.height + 30;
	width = image1.width + 30;
	newwindow = window.open(url,'vetrine','height=600,width=450'); //'+height+''+width+'
	if (window.focus) {newwindow.focus()}
	return false;
}

function ajax( str, max, php, tid){
 	if (str != 0) {	
	 	document.getElementById('seleziona').style.display = 'none';
	 	document.getElementById(tid).innerHTML='<img src="/images/ajaxloader.gif" width="32" height="32" />';
		kjx(str, max, php, tid);
	} 
	else {
		document.getElementById(tid).innerHTML='Seleziona un prodotto!';
		document.getElementById('subcarrello').disabled = true;
		document.getElementById('subcarrello').style.background = 'url(/html/img/bottone-grande-blu.gif) no-repeat left top';
		/*document.getElementById('subcarrello').style. = '';
		document.getElementById('subcarrello').style. = '';
		document.getElementById('subcarrello').style. = '';*/
	}
}

function quantitaret(){
	document.getElementById('seleziona').style.display = 'block';
	document.getElementById('subcarrello').disabled = false;
	document.getElementById('subcarrello').className = 'metti';
}

function aggiornatotali(numerobuoni, subtotale){
	// DEBUG //	alert(numerobuoni +' '+ subtotale);
	var totalesconti = numerobuoni*8;
	var totale = subtotale - totalesconti;
	document.getElementById('totalesconti').innerHTML = totalesconti.toFixed(2).replace(".",",");
	document.getElementById('totale').innerHTML = totale.toFixed(2).replace(".",",");
	document.getElementById('inputtotale').value = totale.toFixed(2).replace(".",",");
}

function aggiornatotali_nuovo(ceckbox,subtotale){
	// DEBUG //	alert(numerobuoni +' '+ subtotale);
	var perc_sconto = 0;
	var valore_sconto = 0;
	var valore_percentuale = 0;

	array_buoni = document.getElementsByName('buoni[]');
	for (i=0;i<array_buoni.length ;i++ )
	{
		if (array_buoni[i].checked){
			split_buono = array_buoni[i].value.split("#");
			if (split_buono[0] == 1){
				perc_sconto += parseInt(split_buono[1]);
			}else if (split_buono[0] == 0){
				valore_sconto += parseInt(split_buono[1]);
			}
		}
	}
	
	if (perc_sconto > 0)
	{
		valore_percentuale = subtotale * (perc_sconto/100);
	}
	
	totale_sconti = formatFloat(valore_percentuale + valore_sconto,2);
	
	totale_parziale = subtotale - totale_sconti;
	
	totale_nuovo = formatFloat(totale_parziale,2);

	if (totale_nuovo >= 0){
		document.getElementById('totalesconti').innerHTML = totale_sconti.toFixed(2).replace(".",",");
		document.getElementById('totale').innerHTML = totale_nuovo.toFixed(2).replace(".",",");
		document.getElementById('inputtotale').value = totale_nuovo.toFixed(2).replace(".",",");
		document.getElementById('totale_sconto_buoni').value = totale_sconti.toFixed(2);
	}else{
		alert ("Non è possibile utilizzare il seguente buono in quanto il valore supera il totale");
		ceckbox.checked = false;
	}
	

}

function ModificaCarrello(){
	
}

function doRedirect(qta,url,cod) { //funzione con il link alla pagina che si desidera raggiungere
	//alert(url);
	location.href = "/AzioniCarrello/Cambia/"+cod+'-'+url+'/'+qta;
}

function Anteprima(id, path, idzoom, zoompath){
 
	if(document.getElementById){
		x = document.getElementById(id);
		x.innerHTML = '<img src="'+path+'" width="238" height="238" border="0" />';
		y = document.getElementById(idzoom);
		y.href = zoompath;
	}
	
}

function formatFloat(num, precision) {

	//var m = Math.pow(10, precision);

    //return parseInt(num * m, 10) / m;

	numStr = Math.round(num*Math.pow(10,precision))/Math.pow(10,precision);

	return numStr;

}