// JavaScript Document

function resetForm() {
	var formCollection=document.getElementsByName("options");
	//alert(document.getElementsByTagName("summary").value)
	for (f=0; f<formCollection.length; f++) {
		//alert(formCollection[f].value);
		formCollection[f].checked = false;
	}
}
function checkProdFamily (id) {
		
		//controllo che tipo di prodotto è
		idObjType = id.substr(0,7);
		
		//cerco di capire se nel carrello c'è un prodotto della stessa famiglia
		var summaryBag=document.getElementById("myBag").childNodes;
		//alert(document.getElementsByTagName("summary").value)
		for (s=0; s<summaryBag.length; s++) {
			//var splitArray = summaryBag[s].childNodes[4].value.split('#')
			//recupero il VALUE dell'INPUT HIDDEN relativo ad ogni riga nel carrello
			//cartHidden = summaryBag[s].childNodes[3].value;
			cartHidden = summaryBag[s].childNodes[2].value;
			//lo devo splittare per arrivare a recuperare solo l'ID
			var cartArray = cartHidden.split('#')
			var id_cart = cartArray[1];
			//alert(id_cart)
				objType = id_cart.substr(0,7);
				//alert("CONFRONTO i PRODOTTI -> "+idObjType+" vs "+objType)
				//if (objType == 'package') {
				if (idObjType == 'package' && objType == 'package' || idObjType == 'startup' && objType == 'startup') {
					//c'è gia un'altro pacchetto della stessa famiglia nel carrello
					//alert("BINGO")
					//alert("CONFRONTO gli ID -> "+id+" vs "+id_cart)
					
					if (id != id_cart) {
					//alert("checkProdFamily (remove) --> inCart_"+id_cart)
						//lo rimuovo dalla sessione
						cartSession('remove',id_cart,'39')
						//lo rimuovo dal carrello
						//alert("Lo rimuovo dal carrello 1")
						removeItem(id_cart);
						
						//quando cambio totalmente prodotto nel carrello (da hosting a Server V o D) allora tolgo tutte le opzioni 
						emptyCart();
					} else {
						//alert("Questo prodotto è già presente nel carrello.")
						
					    keepInCart = 1;
						//alert("checkpoint 2 -> "+keepInCart)
					}
					
					//questa parte non lo utilizzo più potrei anche toglierla
					/*if (document.getElementById(id_cart)) {
						document.getElementById(id_cart).checked = false;
					}
					if (document.getElementById(id_cart+'_value')) {
						//vuole dire che ha un campo VALUE associato e lo disabilito
						document.getElementById(id_cart+'_value').disabled = true;
					}*/	
				}
		}
		
}
function cartSession (act,id,row) { //id=quanto devo rimuovere un elemento o cartrow=quando devo aggiungere un elemento 
	//alert("ROW -> "+row)
	//alert("cartsession ACT ->"+act+" ID -> "+id)
//$('#msgbox').toggle();
	//domain = document.getElementById("domain").value+'.'+document.getElementById("tld").value;
		$.ajax({
				type: 'GET',
				url: 'cartSession.php',
				//data: 'domain='+document.getElementById("domain").value+'.'+document.getElementById("tld").value,
				//data: 'act='+act+'&id='+escape(id),
				data: 'act='+act+'&id='+encodeURIComponent(id)+'&row='+row,
				dataType: 'xml',
				success: onSuccess
			  });
		return false;
}

function addToCart(label,id,qta,price,extra_label,method) { //PARAM -> label | id | qta 0/1 | price | sub descrizione | frequenza di pagamento
		
		//alert("LABEL->"+label+"\n\rID->"+id+"\n\rQTA->"+qta+"\n\rPRICE->"+price+"\n\rEXTRA_LABEL->"+extra_label+"\n\rMETHOD->"+method)
		//inizializzo una var per non cancellare questo articolo dal carrello e dalla sessione
		keepInCart = 0;
		
		//alert("checkpoint 1 -> "+keepInCart)
		checkProdFamily(id,keepInCart)
		//alert("checkpoint 3 -> "+keepInCart)

		if (document.getElementById('inCart_'+id)) {
			
			//alert("addToCart (gia nel carrello con lo stesso id) ("+id+")")
			//GIA ESISTE NEL CARRELLO
			//controllo se la quantita è cambiata, in tal caso devo aggiornare il prezzo
			//il valore di qta (quantità) la recupero della select associata all'opzione [id]_value
			//devo controllare che l'articolo non esista già nel carrello e se esiste, avviso
			//alert(qta)
			if (document.getElementById(id+'_value')) {
				//vuole dire che ha un campo VALUE associato e lo disabilito
				document.getElementById(id+'_value').disabled = true;
			}
			//tolgo il check dal prodotto, se esiste ancora nel carrello (potrebbe essere un dominio nella TEMP_ LIST
			if (document.getElementById(id)) {
				document.getElementById(id).checked = false;
				//controllo se è un dominio dalla TEMP_LIST
				/*objType = id.substr(0,7);
				if (objType == 'package') {
					//devo cambiare l'etichetta aggiungi/rimuovi
					document.getElementById(id).innerHTML = "Aggiungi";
				}*/
			}
			//alert("keepInCart -> "+keepInCart)
			if (keepInCart == 0) {
				//keepInCart viene valorizzata in checkProdFamily
				//lo rimuovo dalla sessione
				//cartSession('remove',id) // commentato perché la rimozione della sessione è fatta da removeId
				//lo rimuovo dal carrello
				//alert("Lo rimuovo dal carrello 2")
				removeItem(id);
			}
			
		} else {
			
			if (qta == 0) {
				
				//vuole dire che ha un campo per il valore aggiuntivo
				if (document.getElementById(id+'_value')) {
					
					document.getElementById(id+'_value').disabled = false;
					//e devo chekkare il check, se è presente nella pagina :-)
					if (document.getElementById(id)) {
						document.getElementById(id).checked = true;
					}
				
				//alert("DEVO CAPIRE CHE ELEMENTO STO TRATTANDO -> "+document.getElementById(id+'_value').nodeName)
				
				formTaget = document.getElementById(id+'_value').nodeName
				if (formTaget == "INPUT") {
					//potrebbe essere una quantita ma anche un valore
					input_value = document.getElementById(id+'_value').value;
					
					if (isNaN(input_value)) {
						//non è una quantita per calcolare il prezzo
						//quindi prendo il prezzo che passa alla funzione
						price = Number(price);
						//devo capire se si tratta di un terzo_livello
						//alert(id)
						isThirdLevel = id.substr(0,20); //da terzolivello_dominio1 estraggo solo terzolivello_dominio
						//alert("DEBUG SUBSTR -> "+isThirdLevel)
						if (isThirdLevel == 'terzolivello_dominio') {
							//è un terzo livello
							trdld = input_value;
						}
						
					} else {
						price = Number(price*input_value);
						//qta = input_value;
					}
					qta = input_value;
					//alert(input_value)
				} else {
					//SELECT
					//il prezzo è dato dalla quantità e lo recupero dal value della select splittando qta#prezzo
					//ne recupero il valore
					qta_array = document.getElementById(id+'_value').value;
					var qtaArray = qta_array.split('#')
					var qta = qtaArray[0];
					var price = qtaArray[1];
					//recupero il label della select
					var num = document.getElementById(id+'_value').selectedIndex;
					extra_label = document.getElementById(id+'_value').options[num].text;
				}
				
				//alert(document.getElementById('myBag').value)
				}
			} else {
				
				//vuole dire che ha un campo per il valore aggiuntivo
				if (document.getElementById(id+'_value')) {
					document.getElementById(id+'_value').disabled = false;
					
					//RISELEZIONO L'ITEM 
					document.getElementById(id+'_value').selectedIndex = qta-1;
				}
				//e devo chekkare il check, se è presente nella pagina :-)
				if (document.getElementById(id)) {
					document.getElementById(id).checked = true;
				}
			}
			//bonifico la variabile
			price = Number(price)
			price = price.toFixed(2)
			
			//controllo se è un dominio dalla TEMP_LIST
			/*objType = id.substr(0,7);
			if (objType == 'package') {
				//devo cambiare l'etichetta aggiungi/rimuovi
				document.getElementById(id).innerHTML = "Rimuovi";
			}*/
			
			
			//aggiungo al carrello
			createDivWithText(label,id,qta,price,extra_label,method);
			//aggiungo alla sessione
			cartRow = label+"#"+id+"#"+qta+"#"+price+"#"+extra_label+"#"+method
			cartSession('add',cartRow,'207')
		}
		
		//aggiorno la riga TOTALE
		calculateCartTotal();
}

function checkExceptions () {
		//devo capire se c'è un hosting. In tal caso se nel carrello è presente un dominio devo settare il prezzo del PRIMO dominio a 0
	//alert(splitArray[0]) ID_ITEM
	//alert(splitArray[0].substr(0,7)); ID_ITEM FAMILY
	//checkFamily = id.substr(0,7);
	isHosting = false;
	isCpanel = false;
	isOSWin = false;
	isSQL = false;
	isVpn = false;
	setFreeDomain = false;
	var summaryBag=document.getElementById("myBag").childNodes;
	
	//controllo che ci sia un determinato prodotto
	for (l=0; l<summaryBag.length; l++) {
		//var splitArray = summaryBag[l].childNodes[3].value.split('#')
		var splitArray = summaryBag[l].childNodes[2].value.split('#')
		subStr_hosting = splitArray[1].substr(0,8);
		//controllo che ci sia un HOSTING
		if (subStr_hosting == 'packageH') {
			//OK c'è un hosting nel carrello!!!
			//alert("PRESENZA DI HOSTING")
			isHosting = true;	
		}
		//controllo se è presente CPANEL nel carrello 
		if (splitArray[1] == 'option_cpanel') {
			isCpanel = true;
		}
		//controllo se è presente WINDOWS  nel carrello
		if (splitArray[1] == 'option_os') {
			isOSWin = true;
		}
		//controllo se è presente SQL  nel carrello
		if (splitArray[1] == 'option_sql') {
			isSQL = true;
		}
		//controllo se è presente VPN nel carrello
		if (splitArray[1] == 'option_vpn') {
			isVpn = true;
			//alert("VPN");
		}
		if (isOSWin) {
			//OK c'è una licenza WIN nel carrello
			//alert("PRESENZA WINDOWS")
			//in tal caso devo disattivare il CHECKBOX cPANEL e rimuoverlo da carrello se presente
			if (document.getElementById('option_cpanel')) {
				document.getElementById('option_cpanel').disabled = true;
			}
			if (isCpanel) {
				//devo rimuovere cpanel dalla sessione e dal carrello
				//lo rimuovo dalla sessione
				cartSession('remove','option_cpanel','265')
				//lo rimuovo dal carrello
				removeItem('option_cpanel');
			}
			//se WIN è nel carrello posso attivare SQL
			if (document.getElementById('option_sql')) {
				document.getElementById('option_sql').disabled = false;
			}
			//02/02/2011 - gestisco l'opzione VPN. se c'è WIN la abilito perché è OPZIONALE
			/*if (document.getElementById('option_vpn')) {
				document.getElementById('option_vpn').disabled = false;
			}*/
		} else {
			if (document.getElementById('option_cpanel')) {
				document.getElementById('option_cpanel').disabled = false;
			}
			if (document.getElementById('option_sql')) {
				document.getElementById('option_sql').disabled = true;
			}
			if (isSQL) {
				//devo rimuovere cpanel dalla sessione e dal carrello
				//lo rimuovo dalla sessione
				cartSession('remove','option_sql','288')
				//lo rimuovo dal carrello
				removeItem('option_sql');
			}
			//02/02/2011 - gestisco l'opzione VPN. se c'è LINUX e non c'è CPANEL è obbligatoria
			if (isCpanel) {
				//alert('cPANEL presente tolgo la VPN e la rendo opzionale');
				//document.getElementById('option_vpn').checked = false;
				//document.getElementById('option_vpn').disabled = false;
				//document.getElementById('option_vpn').disabled = true;
					//lo rimuovo dalla sessione
					//cartSession('remove','option_vpn','298')
					//lo rimuovo dal carrello
					//removeItem('option_vpn');
					//document.getElementById('option_vpn').disabled = false;
			} else {
				//alert("cPANEL NON presente dovrei aggiungere la VPN");
				if (isVpn == false) {
					//isVpn = true //qui non ci dovrei più entrare
					//alert("A -> "+isVpn)
					//alert("cPANEL non c'è e la VPN non ancora, la aggiungo");
					//addToCart('Tunnel VPN','option_vpn','-1','5','Connessione protetta','importo mensile')
					//document.getElementById('option_vpn').checked = true;
					//document.getElementById('option_vpn').disabled = true;
				} else {
					//isVpn = false
					//alert("B -> "+isVpn)
					//alert("cPANEL non c'è e la VPN adesso si. non faccio niente.");
				}
				//addToCart('Tunnel VPN','option_vpn','-1','5','Connessione protetta','importo mensile')
			}
		}
		
		//controllo se nel carrello è presente il BACKUP AUTOMATICO o option_snapshot. Se si, devo calcolare per quanto spazio
		if (document.getElementById('option_backup') ) {
		//if (splitArray[1] == 'option_backup') {
			//alert("SONO VIVO E SONO QUI")
			if (document.getElementById('option_backup').checked==true) {
				hdd_space = 0;
				hdd_qta = 0;
				if (splitArray[1] == 'packageV1' ) {
					hdd_space = 50;
				} else if (splitArray[1] == 'packageV2') {
					hdd_space = 100;
				} else if (splitArray[1] == 'packageV3') {
					hdd_space = 200;
				}
				if (document.getElementById('option_hdd')) {
					if (document.getElementById('option_hdd').checked==true) {
						array_hdd_qta = document.getElementById('option_hdd_value').value.split('#')
						hdd_qta = Number(array_hdd_qta[0]);
					}
				}
				if (hdd_space!=0) {
					total_hdd = hdd_qta+hdd_space;
					total_price_bk = 5+(0.5*total_hdd);
					//alert(total_price_bk)
					document.getElementById("inCart_option_backup_price_value").innerHTML = "€ "+total_price_bk.toFixed(2);
					document.getElementById("hidden_option_backup").value = "Backup Automatico#option_backup#-1#"+total_price_bk.toFixed(2)+"#Frequenza giornaliera#importo mensile";
					//alert(splitArray[1]+" -> "+hdd_space)
					//lo rimuovo dalla sessione
					//cartSession('remove','option_backup','285')
					cartSession('add',"Backup Automatico#option_backup#-1#"+total_price_bk.toFixed(2)+"#Frequenza giornaliera#importo mensile",'339')
					
				}
			}
		}
		//fine check backup
		//controllo se nel carrello è presente il option_snapshot. Se si, devo calcolare per quanto spazio
		if (document.getElementById('option_snapshot') ) {
			//alert("SONO VIVO E SONO QUI")
			if (document.getElementById('option_snapshot').checked==true) {
				hdd_space = 0;
				hdd_qta = 0;
				if (splitArray[1] == 'packageV1' ) {
					hdd_space = 50;
				} else if (splitArray[1] == 'packageV2') {
					hdd_space = 100;
				} else if (splitArray[1] == 'packageV3') {
					hdd_space = 200;
				}
				if (document.getElementById('option_hdd')) {
					if (document.getElementById('option_hdd').checked==true) {
						array_hdd_qta = document.getElementById('option_hdd_value').value.split('#')
						hdd_qta = Number(array_hdd_qta[0]);
					}
				}
				if (hdd_space!=0) {
					total_hdd = hdd_qta+hdd_space;
					total_price_bk = 0.5*total_hdd;
					//alert(total_price_bk)
					document.getElementById("inCart_option_snapshot_price_value").innerHTML = "€ "+total_price_bk.toFixed(2);
					document.getElementById("hidden_option_snapshot").value = "Snapshot#option_snapshot#-1#"+total_price_bk.toFixed(2)+"#Backup System#importo mensile";
					//alert(splitArray[1]+" -> "+hdd_space)
					//lo rimuovo dalla sessione
					//cartSession('remove','option_backup','285')
					cartSession('add',"Snapshot#option_snapshot#-1#"+total_price_bk.toFixed(2)+"#Backup System#importo mensile",'324')
					
				}
			}
		}
		//fine check backup
		//devo controllare che non aggiunga, in caso di V3 nel carrello, più di 6 processori
		if (document.getElementById('option_cpu') || document.getElementById('option_vcpu') ) {
		if (splitArray[1]=='packageV3') {
				//alert("DEBUG 332")
			if (document.getElementById('option_vcpu').checked==true && document.getElementById('option_cpu').checked==true) {
				selectedVCPU = document.getElementById('option_vcpu_value').value.split('#');
				selectedCPU = document.getElementById('option_cpu_value').value.split('#');
				//alert('selectedVCPU -> '+selectedVCPU[0]+" //// "+'selectedCPU -> '+selectedCPU[0]);
				CPUSelected = Number(selectedVCPU[0])+Number(selectedCPU[0]);
				if (CPUSelected > 6) {
					alert("Non puoi aggiungere più di 6 CPU/vCPU")
					//document.getElementById('option_cpu_value').selectedIndex=1;
					//document.getElementById('option_vcpu_value').selectedIndex=1;
					//lo rimuovo dalla sessione
					cartSession('remove','option_cpu','344')
					//lo rimuovo dal carrello
					removeItem('option_cpu');
				}
				
				//split('#')
				//document.getElementById('option_cpu_value').selectedIndex=0;
				//document.getElementById('option_vcpu_value').selectedIndex=0;
			}
		}
		}
		//fine controllo CPU / VCPU
	}
	
	var summaryBag2=document.getElementById("myBag").childNodes;
	
	for (s=0; s<summaryBag2.length; s++) {
		var splitArray2 = summaryBag2[s].childNodes[2].value.split('#')
		subStrId2 = splitArray2[1].substr(0,7);
		if (subStrId2 == 'dominio' && isHosting == true && setFreeDomain == false || subStrId2 == 'trasfer' && isHosting == true && setFreeDomain == false) {
			document.getElementById("inCart_"+splitArray2[1]+'_price_value').innerHTML = "€ 0.00";
			
			//updArray = new Array(summaryBag2[s].childNodes[3].value);
			
			document.getElementById("hidden_"+splitArray2[1]).value = splitArray2[0]+"#"+splitArray2[1]+"#"+splitArray2[2]+"#0.00#"+splitArray2[4]+"#"+splitArray2[5];
			//<input value="dominio1_fdsaddfasd#1#9.00#fdsaddfasd.it" id="hidden_dominio1_fdsaddfasd" name="dominio1_fdsaddfasd" type="hidden">
			//DEVO AGGIORNAMRE ANCHE LA SESSIONE
			cartSession('add',splitArray2[0]+"#"+splitArray2[1]+"#"+splitArray2[2]+"#0.00#"+splitArray2[4]+"#"+splitArray2[5],'323')
			setFreeDomain = true;
		}
	
	//alert("isHosting -> "+isHosting)
	
	
	}

}

function createDivWithText(label,id,qta,price,extra_label,method) {
	//createDiv.setAttribute('onclick',"javascript:removeAllChild('id"+idUser+"');");
	//createDiv.style.cssText = "font-family: Verdana, Arial, Helvetica, sans-serif"
	
	
	createDiv_label_box = document.createElement("div");
	createDiv_label_box.setAttribute('id',"inCart_"+id+"_label_box");
	createDiv_label_box.setAttribute('className',"cartItem_label_box");
	createDiv_label_box.setAttribute('class',"cartItem_label_box");
	//createDiv_label.appendChild(document.createTextNode(label));
	
	createDiv_label = document.createElement("div");
	createDiv_label.setAttribute('id',"inCart_"+id+"_label");
	createDiv_label.setAttribute('className',"cartItem_label");
	createDiv_label.setAttribute('class',"cartItem_label");
	createDiv_label.appendChild(document.createTextNode(label));
	
	createDiv_details = document.createElement("div");
	createDiv_details.setAttribute('id',"inCart_"+id+"_details");
	createDiv_details.setAttribute('className',"cartItem_details");
	createDiv_details.setAttribute('class',"cartItem_details");
	//createDiv_details.appendChild(document.createTextNode("<ul><li>spazio</li></ul>"));
	
	/*createDiv_value = document.createElement("div");
	createDiv_value.setAttribute('id',"inCart_"+id+"_value");
	createDiv_value.setAttribute('className',"cartItem_value");
	createDiv_value.setAttribute('class',"cartItem_value");*/
	if (extra_label) {
		//molto probabilemnte è un nome a dominio, dovrei metterlo insieme a LABEL
		//alert(extra_label)
				
		//createDiv_label.appendChild(document.createTextNode(extra_label));
			isThirdLevel = id.substr(0,20); //da terzolivello_dominio1 estraggo solo terzolivello_dominio
			if (isThirdLevel == 'terzolivello_dominio') {
				//è un terzo livello
				//trdld = input_value;
				extra_label = trdld+"."+extra_label
				
				createDiv_details.appendChild(document.createTextNode(extra_label));
				
			} else {
				createDiv_details.appendChild(document.createTextNode(extra_label));
			}
	} else {
		//se quantita è -1 vuol dire che non la devo visualizzare
		if (qta == '-1') {
			createDiv_details.appendChild(document.createTextNode('qta 1'));
		} else {
			createDiv_details.appendChild(document.createTextNode(qta));
		}
		
	}
	
	/*createDiv_price = document.createElement("div");
	createDiv_price.setAttribute('id',"inCart_"+id+"_price");
	createDiv_price.setAttribute('className',"cartItem_price");
	createDiv_price.setAttribute('class',"cartItem_price");
	createDiv_price.appendChild(document.createTextNode("€ "+price));*/
	
	createDiv_price = document.createElement("div");
	createDiv_price.setAttribute('id',"inCart_"+id+"_price");
	createDiv_price.setAttribute('className',"cartItem_price");
	createDiv_price.setAttribute('class',"cartItem_price");
	//createDiv_price.appendChild(document.createTextNode("€ "+price));
	
	createDiv_price_label = document.createElement("div");
	createDiv_price_label.setAttribute('id',"inCart_"+id+"_price_label");
	createDiv_price_label.setAttribute('className',"cartItem_price_label");
	createDiv_price_label.setAttribute('class',"cartItem_price_label");
	//createDiv_price_label.appendChild(document.createTextNode(extra_label));
	createDiv_price_label.appendChild(document.createTextNode(method));
	
	createDiv_price_value = document.createElement("div");
	createDiv_price_value.setAttribute('id',"inCart_"+id+"_price_value");
	createDiv_price_value.setAttribute('className',"cartItem_price_value");
	createDiv_price_value.setAttribute('class',"cartItem_price_value");
	createDiv_price_value.appendChild(document.createTextNode("€ "+price));
	
	/*createDiv_note = document.createElement("div");
	createDiv_note.setAttribute('id',"inCart_"+id+"_note");
	createDiv_note.setAttribute('className',"cartItem_note");
	createDiv_note.setAttribute('class',"cartItem_note");
	createDiv_note.appendChild(document.createTextNode("mese"));*/

	createDiv_hidden = document.createElement("input");
	//createDiv_hidden.setAttribute('name', "summary");
	createDiv_hidden.setAttribute('name', id);
	createDiv_hidden.setAttribute('id', "hidden_"+id);
	createDiv_hidden.setAttribute('type', "hidden");
	//extra_label contiene il nome a nominio (per adesso)
	//alert("qta -> "+qta)
	createDiv_hidden.setAttribute('value', label+"#"+id+"#"+qta+"#"+price+"#"+extra_label+"#"+method);
	
	createDiv_removeItem = document.createElement("a");
	//createDiv_removeItem.setAttribute('href', "#");
	//createDiv_removeItem.setAttribute('onclick', "removeItem('"+id+"');");
	//createDiv_removeItem.setAttribute('href', "javascript:addToCart('"+label+"','"+id+"','"+qta+"','"+price+"','"+extra_label+"');");
	createDiv_removeItem.setAttribute('href', "javascript:removeItem('"+id+"');");
	//createDiv_removeItem.setAttribute('onclick', "return confirm('Sei sicuro di voler rimuovere "+label+" dal carrello?');");
	createDiv_removeItem.setAttribute('id',"removeItem_link_"+id);
	createDiv_removeItem.setAttribute('className',"cartItem_button");
	createDiv_removeItem.setAttribute('class',"cartItem_button");
	createDiv_removeItem.setAttribute('style',"display:none");
	//createDiv_removeItem.appendChild(document.createTextNode("Elimina"));
	
	createDiv_removeItem_image = document.createElement("img");
	//createDiv_removeItem_image.setAttribute('src', "images/trash.gif");
	createDiv_removeItem_image.setAttribute('src', "/products/images/trash.gif");
	createDiv_removeItem_image.setAttribute('border', "0");
	
	createDiv_br = document.createElement("br");
	createDiv_br.setAttribute('class',"clear");
	
	createDiv_id = document.createElement("div");
	createDiv_id.setAttribute('id',"inCart_"+id);
	createDiv_id.setAttribute('className',"cartRow");
	createDiv_id.setAttribute('class',"cartRow");
	
	//createDiv_id.setAttribute('style',"display: none;");
	//createDiv_id.appendChild(document.createTextNode());
	
	document.getElementById("myBag").appendChild(createDiv_id);
	document.getElementById("inCart_"+id).appendChild(createDiv_label_box);
	document.getElementById("inCart_"+id+"_label_box").appendChild(createDiv_label);
	document.getElementById("inCart_"+id+"_label_box").appendChild(createDiv_details);
	
	document.getElementById("inCart_"+id).appendChild(createDiv_price);
	document.getElementById("inCart_"+id+"_price").appendChild(createDiv_price_label);
	document.getElementById("inCart_"+id+"_price").appendChild(createDiv_price_value);
	
	//document.getElementById("inCart_"+id).appendChild(createDiv_removeItem);
	document.getElementById("inCart_"+id+"_price").appendChild(createDiv_removeItem);
	document.getElementById("removeItem_link_"+id).appendChild(createDiv_removeItem_image);
	document.getElementById("inCart_"+id+"_price").appendChild(createDiv_br);
	
	//devo capire quando valorizzare questo campo: per adesso solo per le caratteristiche dei package
	if (extra_label == 'get_detail') {
		//vuol dire che il prodotto richiede una descrizione maggiore
		
		//la stampa delle caratteristiche devo sostituirla con un sistema serio
		if (id == 'packageH1')  {
			document.getElementById("inCart_"+id+"_details").innerHTML="<ul><li>Spazio web 10 GB</li><li>Email illimitate</li><li>Traffico illimitato</li></ul>";
		} else if (id == 'packageV1') {
			document.getElementById("inCart_"+id+"_details").innerHTML="<ul><li>HDD 50 GB</li><li>RAM 1 GB</li><li>1 vCPU Dedicata</li><li>Cloud</li><li>Startup Gratuito</li></ul>";
		} else if (id == 'packageV2') {
			document.getElementById("inCart_"+id+"_details").innerHTML="<ul><li>HDD 100 GB</li><li>2 GB RAM Dedicata</li><li>2 vCPU</li><li>Cloud</li><li>Startup Gratuito</li></ul>";
		} else if (id == 'packageV3') {
			document.getElementById("inCart_"+id+"_details").innerHTML="<ul><li>Dedicato virtuale</li><li>1 CPU e 1 vCPU Dedicata</li><li>4 GB RAM Dedicata</li><li>Storage 200 GB</li><li>Cloud</li><li>Startup &euro; 500,00</li></ul>";
		} else if (id == 'packageD1') {
			document.getElementById("inCart_"+id+"_details").innerHTML="<ul><li>1 DELL PowerEdge</li><li>Storage 2 TB</li><li>RAM 16 GB</li><li>Startup &euro; 580,00</li></ul>";
		}
		
	} else {
		document.getElementById("inCart_"+id+"_details").innerHTML=extra_label;
	}
	//document.getElementById("inCart_"+id).appendChild(createDiv_note);
	
	document.getElementById("inCart_"+id).appendChild(createDiv_hidden);
	//document.getElementById("inCart_"+id).appendChild(createDiv_br);
	
	//faccio il fadeIn del DIV
	//alert(id)
	if (id == "startupV3" || id == "startupD1") {
		// NON MOSTRO IL DIV
		//alert("Non aggiungo")
		//alert("Non mostro startupV3")
	} else {
		startFadeId('#inCart_'+id);
	}
}

function removeItem(id) {
	//alert("REMOVE_ITEM -> "+id)
	//prima di rimuovere, mi assicuro che esista nel carrello
	//var nodeTarget = document.getElementById("inCart_"+id);
	//alert(d.firstChild.parentNode.getAttribute("id"))
	
	//se esiste, lo cancello
	if (document.getElementById("inCart_"+id)) {
		
		//tolgo il check dal prodotto, se esiste ancora nel carrello (potrebbe essere un dominio nella TEMP_ LIST
		if (document.getElementById(id)) {
			document.getElementById(id).checked = false;
		}

		//controllo se ci sono eventuali SELECT associati e nel caso lo disabilito
		if (document.getElementById(id+"_value")) {
			document.getElementById(id+"_value").disabled = true;
			document.getElementById(id+"_value").selectedIndex=0;
		}

		//startFadeOut(nodeTarget);
		//document.getElementById("myBag").removeChild(nodeTarget.firstChild.parentNode);
		//nodeTarget.firstChild.parentNode.remove();
		
		function callBackRemoveNode() {
			//alert("chiamata")
			//$(nodeTarget.firstChild.parentNode).remove();
			if (document.getElementById("inCart_"+id)) {
				//lo rimuovo dalla sessione
				cartSession('remove',id,'633')
				//lo rimuovo dal carrello
				document.getElementById("myBag").removeChild(document.getElementById("inCart_"+id).firstChild.parentNode);
			} else {
				//alert("Si e\' verificato un errore durante la rimozione dal carrello. Non trovo l'elemento "+id+" nel carrello.")
			}
			//e ricalcolo il totale
			calculateCartTotal();
		}
		//removeNode()
		
		//$('#inCart_'+id).fadeOut("slow", removeNode(nodeTarget));
		$('#inCart_'+id).fadeOut("slow",callBackRemoveNode);
		
	}
}
function updateItem(id,price) {
	//alert(id)
	var nodeTarget = document.getElementById("inCart_"+id);
	//alert(d.firstChild.parentNode.getAttribute("id"))
	elementArray = document.getElementById('hidden_'+id).value;
	var elementSplitted = elementArray.split('#')
	var method = elementSplitted[5];
	
	//prima di aggiornare, mi assicuro che esista nel carrello
	if (nodeTarget) {
		//alert("devo aggiornare "+id)
		//lo rimuovo dalla sessione
			//cartSession('remove',id,'535')
		//prima lo rimuovo 
		//document.getElementById("myBag").removeChild(nodeTarget.firstChild.parentNode);
		//poi lo creo di nuovo
		//addToCart(label,id,qta,price,extra_label)
		//qta,price,extra_label
		//se lo aggiorna, vuol dire che ha un campo VALUE associato, o sbaglio?
		//alert("DEVO CAPIRE CHE ELEMENTO STO TRATTANDO -> "+document.getElementById(id+'_value').nodeName)
				
				formTaget = document.getElementById(id+'_value').nodeName
				input_value = document.getElementById(id+'_value').value;
				
				if (formTaget == "INPUT") {
					
					//trdld = 0;
					if (isNaN(input_value)) {
						//non è una quantita per calcolare il prezzo
						//quindi prendo il prezzo che passa alla funzione
						//price = Number(price);
						//devo capire se si tratta di un terzo_livello
					
						isThirdLevel = id.substr(0,20); //da terzolivello_dominio1 estraggo solo terzolivello_dominio
						//alert("DEBUG SUBSTR -> "+isThirdLevel)
						if (isThirdLevel == 'terzolivello_dominio') {
							price = Number(price);
							price = price.toFixed(2);
							//è un terzo livello
							//trdld = input_value;
							//recupero il dominio TLD
							domain = document.getElementById(id).value;
							//innerHTML
							document.getElementById("inCart_"+id+'_price_label').innerHTML = input_value;
							document.getElementById("inCart_"+id+'_price_value').innerHTML = "€ "+price;
						}
					} else {
						price = Number(price*input_value);
						price = price.toFixed(2);
						
						//innerHTML
						//document.getElementById("inCart_"+id+'_value').innerHTML = input_value;
						document.getElementById("inCart_"+id+'_price_label').innerHTML = input_value;
						document.getElementById("inCart_"+id+'_price_value').innerHTML = "€ "+price;
					}
					alert("DEBUG 1->"+input_value)
					qta = input_value;
				} else {
					//SELECT
					//il prezzo è dato dalla quantità e lo recupero dal value della select splittando qta#prezzo
					//ne recupero il valore
					//qta_array = document.getElementById(id+'_value').value;
					qta_array = document.getElementById(id+'_value').value;
					var qtaArray = qta_array.split('#')
					var qta = qtaArray[0];
					var price = qtaArray[1];
					//var extra_label_select = qtaArray[2];
					price = Number(price)
					price = price.toFixed(2);
					//recupero il label della select
					var num = document.getElementById(id+'_value').selectedIndex;
					extra_label = document.getElementById(id+'_value').options[num].text;
					
					//innerHTML
					//document.getElementById("inCart_"+id+'_price_label').innerHTML = input_value;
					document.getElementById("inCart_"+id+'_details').innerHTML = extra_label;
					document.getElementById("inCart_"+id+'_price_value').innerHTML = "€ "+price;
				}
				label=document.getElementById("inCart_"+id+'_label').innerHTML;
				cartRow = label+"#"+id+"#"+qta+"#"+price+"#"+extra_label+"#"+method
				//cartRow = id+"#"+qta+"#"+price+"#"+extra_label+"#"+method
				//document.getElementById("hidden_"+id).value = id+"#"+qta+"#"+price;
				document.getElementById("hidden_"+id).value = cartRow;
				// lo rimetto in sessione dopo averlo modificato
				//alert(cartRow)
				cartSession('add',cartRow,'608')
				
				/*$('#inCart_'+id).effect("highlight", {
										color: "#ffffcc"
										}, 3000);
				//$('#ciao').effect("highlight", {}, 3000);*/
				
				//alert("TARGET -> hidden_"+id+" VALUE -> "+id+"#"+qta+"#"+price)

		//e ricalcolo il totale
		calculateCartTotal();
	}
}

function doCartSummary() {
	//var summaryBag=document.getElementsByName("summary"); // BUG IN IE
	var summaryBag=document.getElementById("myBag").childNodes;
	//alert(document.getElementsByTagName("summary").value)
	for (s=0; s<summaryBag.length; s++) {
		//var splitArray = summaryBag[s].childNodes[4].value.split('#')
		alert(summaryBag[s].childNodes[2].value)
	}
}

function emptyCart() {
	var summaryBag = document.getElementById("myBag").childNodes;
	var bagLength = summaryBag.length; 
	
	if (bagLength > 0) {
	
		for (f=0; f<bagLength; f++) {
			//document.getElementById("myBag").removeChild(summaryBag[s].firstChild.parentNode)
			
			
			var getId = summaryBag[f].childNodes[2].value.split('#')
			//var gotId = getId[0];
			var gotId = getId[1];
	
			//document.getElementById("myBag").removeChild(summaryBag[0].firstChild.parentNode)
			//alert(gotId)
			removeItem(gotId)
			//lo rimuovo dalla sessione
			cartSession('remove',gotId,'650')
		}
	} else {
		alert("Il carrello è vuoto.")	
	}
	calculateCartTotal();
}

function calculateCartTotal() {
	
	//resetto
	document.getElementById("myBag_status").innerHTML = '';
	
	//chiamo la funzione che controlla il contenuto del carrello ed applica le accezioni
	checkExceptions();
	//alert("chiamata calculateCartTotal")
	//var summaryBag=document.getElementsByName("summary"); // BUG IN IE
	var summaryBag=document.getElementById("myBag").childNodes;
	//alert("DEBUG ->" +summaryBag[0].childNodes[4].value)
	//calcolo solo se il numero di articoli nel carrello è > 0
	if (summaryBag.length>0) {
		//mostro la riga di TOTALE e il CARRELLO
		//document.getElementById("myBag_box").style.display = "block";
		//$("myBag_box:hidden:first").fadeIn("slow");
		startFadeId('#myBag_box');
		
		var price = 0;
		var price_startup = 0;
		for (s=0; s<summaryBag.length; s++) {
			//var splitArray = summaryBag[s].value.split('#')
			var splitArray = summaryBag[s].childNodes[2].value.split('#')
			//alert("AGGIUNGO AL TOTALE -> "+splitArray[3])
			//mi interessa solo il prezzo per ogni riga che nell'array è indice 2
			
			cartHidden = summaryBag[s].childNodes[2].value;
			//lo devo splittare per arrivare a recuperare solo l'ID
			var cartArray = cartHidden.split('#')
			var id_cart = cartArray[1];
			//alert(id_cart)
			
			if (id_cart == "startupV3" || id_cart == "startupD1") {
				price_startup += Number(splitArray[3]);
			} else {
				price += Number(splitArray[3]);
			}
			
		}
		if (price_startup == 0) {
			document.getElementById("summaryPrice_startup").innerHTML = "Gratis";
		} else {
			document.getElementById("summaryPrice_startup").innerHTML = "€ "+price_startup.toFixed(2);
		}
		
		document.getElementById("summaryPrice").innerHTML = "€ "+price.toFixed(2);
		
		//alert()
	} else {
		//alert("Il carrello e\' vuoto")
		//nascondo la riga di TOTALE e il CARRELLO
		//document.getElementById("myBag_box").style.display = "none";
		//startFadeOut('#myBag_box');
		//alert('Il carrello e\' vuoto')
		document.getElementById("myBag_status").innerHTML = 'Il carrello e\' vuoto';
		//resetto anche la riga del totale
		var price = 0;
		document.getElementById("summaryPrice").innerHTML = "€ "+price.toFixed(2);
	}
}
 
	/*$(document.body).click(function () {
		$("#myBag_box:hidden:first").fadeIn("slow");
	});*/

	function startFadeId(target) {
		$(target+":hidden:first").fadeIn("slow");
	};
	
	function startFadeOut(target) {
		$(target).fadeOut("fast");
	};
