var numrow = "";

function removeElement($divNum) {
	
  var olddiv = document.getElementById('C'+$divNum);
  document.form1.removeChild(olddiv);
  Recalculatotcarrello();
}

function create_div_dynamic(divNum, valore, $descrizione){
	
	var tmpdivNum = new String(divNum);
	var t1 = new String(numrow);
	var tmpValore = new String(valore);
	tmpValore = tmpValore.replace(",", ".")
	tmpValore = parseFloat(tmpValore);
	
	dv = document.createElement('div'); 
	dv.setAttribute('id','C'+divNum);  
	dv.className="BoxCarr"; 
	var divHTML = '';
	divHTML = "<div style='height:71px; width:584px;'>"
	divHTML += "<table width='584px' border='0' cellspacing='0' cellpadding='0'>"
	divHTML += "<tr style=' height:71px;'>"
	divHTML += "<td width='95' align='center'>"
	divHTML += "<img src='../images/buono_sconto.jpg' alt='Buono sconto'>"
	divHTML += "<input type='hidden' name='codart' id='codart' value='' >  "
	divHTML += "</td>"
	divHTML += "<td width='174' >"+ $descrizione +"</td>"
	divHTML += "<td width='87'></td>"
	divHTML += "<td width='119' align='right' style='font-size:14px; font-weight:bold;'></td>"
	divHTML += "<td width='109' bgcolor='#FAFAFA' align='right' style='font-size:18px; font-weight:bold;'>"
	divHTML += "&euro;."
	divHTML += "<span id='totaleriga"+divNum+"' style='margin-right:10px;'> " 
	divHTML += formatCurrency(tmpValore*-1,2)
	divHTML += "</span>"
	divHTML += "</td>"
	divHTML += "</tr>"
	divHTML += "</table>"
	divHTML += "</div>"	
	divHTML += "<div style=' margin:2px; height:20px; background-color:#EEEEEE; font-size:10px;'>"
	divHTML += "<div style='margin-left:10px; float:left; height:20px;'></div>"
	divHTML += "<div style='float:right;'>"
	//divHTML += "<a href=\"javascript:void(0)\" onclick=\"removeElement('S"+divNum+"');\">"
	
	divHTML += "<a href=\"javascript:void(0)\" onclick=\"AjaxAddSconto('../funzioni/delete_cart.asp?cdrow="+divNum+"');\">"
	
	divHTML += "<img src='../images/carrello/elimina_prodotto.gif' alt='Elimina prodotto dal carrello' border='0' style='margin-top:2px;' >"
	divHTML += "</a>"
	divHTML += "</div>"
	divHTML += "</div>"

	numrow += ','+tmpdivNum
	dv.innerHTML=divHTML;
	document.getElementById('form1').appendChild(dv);
	Recalculatotcarrello();

}

function DisactRow ($check, row){
	if (document.getElementById($check).checked==true){
		document.getElementById('rowqta'+row).style.display='block';
		document.getElementById('totaleriga'+row).style.display='';
		document.getElementById('dispaitotriga'+row).style.display='block';
		
	}else{
		document.getElementById('rowqta'+row).style.display='none';
		document.getElementById('dispaitotriga'+row).style.display='none';
		document.getElementById('totaleriga'+row).style.display='none';
	}
	Recalculatotcarrello();
}

function DisRow ($check, row){
	document.getElementById($check).checked=false;
	document.getElementById('rowqta'+row).style.display='none';
	document.getElementById('dispaitotriga'+row).style.display='none';
	Recalculatotcarrello();
}



function ToggleVert($WhichDiv, $AccName, $MinHeight, $MaxHeight) {
	var $CurrHeight = parseInt(document.getElementById($WhichDiv).style.height);
	if ($CurrHeight < ($MaxHeight - 10)) {
		VertScale($WhichDiv,$MaxHeight, 1);
		document.getElementById($AccName).src = "../images/carrello/ind_accessori_on.gif";
		document.getElementById($WhichDiv).display = "";
	} else {
		VertScale($WhichDiv,$MinHeight, 0);
		document.getElementById($AccName).src = "../images/carrello/ind_accessori_off.gif";
	}
	
}

function VertScale($WhichDiv, $EndHeight, $CClose) {
	var $StartHeight = parseInt(document.getElementById($WhichDiv).style.height);
	document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
	document.getElementById($WhichDiv).style.overflow = 'hidden'; // set the overflow to hidden
	for( var i = 0 ; i <= 50 ; i++ ) {
		$CurrHeight = ($StartHeight - ((($StartHeight - $EndHeight) / 50) * (i-1)));
		setTimeout( 'setHeight(\''+$WhichDiv+'\',' + $CurrHeight + ')' , 8 * i );
	}	
}

function setHeight( $WhichDiv, value ) {
	if (value > 5) {
	document.getElementById($WhichDiv).style.height = parseInt(value) + "px";
	}
	
	if (value < 5) {
		document.getElementById($WhichDiv).display = "none";
		document.getElementById($WhichDiv).style.height=0;
	}
}


function onlynum(e){
if (document.layers){ 
   if (e.which){
	  //if(e.which!=46 && e.which!=8 && (e.which<48 || e.which>57))
	  if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}
	}else if(e.keyCode){
	  //if(e.keyCode!=46 && e.keyCode!=8 && (e.keyCode<48 || e.keyCode>57))
	  if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}
	}
   return true;
}else if (document.all){
	    if(e.keyCode!=8 && e.keyCode!=0 && (e.keyCode<48 || e.keyCode>57)){e.returnValue=false;return false;}
	   /*if(!((e.keyCode>=48&&e.keyCode<=57)||(e.keyCode>=96&&e.keyCode<=105)||(e.keyCode==8))){
	e.returnValue=false; 
   }*/
}else if (document.getElementById){
	   if (e.which){
	  //if(e.which!=46 && e.which!=8 && (e.which<48 || e.which>57))
	  if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}
	}else if(e.keyCode){
	  //if(e.keyCode!=46 && e.keyCode!=8 && (e.keyCode<48 || e.keyCode>57))
	  if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}
	}
   return true;
}
	

   
      /*

   */
   }

function RecalculaRow(row, opr){
	var qta =  document.getElementById('qta'+row+'').value;
	var disp =  document.getElementById('disp'+row+'').value;
	tmpqta = new String(qta);
	tmpqta = tmpqta.replace(",", ".")
	tmpqta = parseFloat(tmpqta);
	
	tmpdisp = new String(disp);
	tmpdisp = tmpdisp.replace(",", ".")
	tmpdisp = parseFloat(tmpdisp);
	
	if (opr == "add"){
		if (tmpqta+1 > tmpdisp) {
			alert("Attenzione!!! Non è possibile inserire quantità maggiori della disponibilità in magazzino.");
			tmpqta = tmpdisp;
		}else{
			tmpqta = tmpqta + 1;
		}
	}else{
		if (tmpqta - 1 <=0 ){
		}else {
			tmpqta = tmpqta - 1;
		}
	}
	var prezzo =  document.getElementById('prezzo'+row+'').innerHTML;
	tmpprezzo = new String(prezzo);
	tmpprezzo = tmpprezzo.replace(",", ".")
	tmpprezzo = parseFloat(tmpprezzo);
	document.getElementById('qta'+row+'').value = tmpqta;
	var totriga = new String(tmpqta*tmpprezzo);
	totriga = totriga.replace(".", ",");
	document.getElementById('totaleriga'+row+'').innerHTML = formatCurrency(tmpqta*tmpprezzo, 2);
	Recalculatotcarrello();
}

function RecalculaQta(qta,row){
	var prezzo =  document.getElementById('prezzo'+row+'').innerHTML;
	tmpprezzo = new String(prezzo);
	tmpprezzo = tmpprezzo.replace(",", ".")
	tmpprezzo = parseFloat(tmpprezzo);
	//var totriga = new String(qta*tmpprezzo);
	//totriga = totriga.replace(".", ",");
	document.getElementById('totaleriga'+row+'').innerHTML = formatCurrency(qta*tmpprezzo, 2);
	Recalculatotcarrello();
}

function ResettaQta(qta,row){
	var disp =  document.getElementById('disp'+row+'').value;
	
	tmpdisp = new String(disp);
	tmpdisp = tmpdisp.replace(",", ".")
	tmpdisp = parseFloat(tmpdisp);
	
	if (qta > tmpdisp) {
		document.getElementById('qta'+row+'').value = tmpdisp;
		alert("Attenzione!!! Non è possibile inserire quantità maggiori della disponibilità in magazzino.");
		qta = tmpdisp;
	}
	
	if (qta !=''){ 
	if ((qta < 0) ||(qta==0)){ 
		qta = 1; 
		if (testIsValidObject(document.getElementById('totaleriga'+row+''))==true){
			document.getElementById('totaleriga'+row+'').value = qta;
		}
	}
	RecalculaQta(qta, row);
	}
}


function formatCurrency(num, dec, sepm, sepd){
	if(dec == undefined || dec == "") dec = 2;
	if(sepm == undefined || sepm == "") sepm = " "; sepm = sepm.substr(0,1);
	if(sepd == undefined || sepd == "") sepd = ","; sepd = sepd.substr(0,1);
	
	var d = Math.pow(10,dec);
	
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
		
	num = Math.floor(num * d + 0.50000000001);
	cents = num % d;
	num = Math.floor(num / d).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1+i))/3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + sepm + num.substring(num.length - (4 * i + 3));
	return (((sign)?'':'-') + '' + num + sepd + cents);
}

function testIsValidObject(objToTest) {
	if (objToTest == null || objToTest == undefined) {
		return false;
	}
		return true;
}

function Recalculatotcarrello(){
	var t1 = new String(numrow);
	var arrlinker = t1.split(",");
	var part_num=0;	
	if (testIsValidObject(document.getElementById('totcarrello'))==true){
		var tmpQtaCarr = 0;
		//for( var i = 1 ; i < numrow ; i++ ) {
		while (part_num < arrlinker.length){
			var check = testIsValidObject(document.getElementById('checkbox'+arrlinker[part_num]+''));
			if (check == false){
					tmpQtaCarr += SommaRighe(arrlinker[part_num]);
			}else{
				if(document.getElementById('checkbox'+arrlinker[part_num]+'').checked==true){
					tmpQtaCarr += SommaRighe(arrlinker[part_num]);
				}
			}
		 part_num+=1;
		}
		document.getElementById('totcarrello').innerHTML = formatCurrency(tmpQtaCarr, 2);
	}
}

function SommaRighe(i){
	tmpqta = 0;
	if (testIsValidObject(document.getElementById('totaleriga'+i+''))==true){
		qta =  document.getElementById('totaleriga'+i+'').innerHTML;
		tmpqta = new String(qta);
		tmpqta = tmpqta.replace(" ", "")
		tmpqta = tmpqta.replace(".", "")
		tmpqta = tmpqta.replace(",", ".")
		tmpqta = parseFloat(tmpqta);
	}
	
	
	return tmpqta
}

