/************funzione per l'inserimento della rivista nel carrello**********************/

  function makePOSTRequest_add_carrello_prodotto(url, parameters) {
     http_request = false;
	 
     if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
           //http_request.overrideMimeType('text/xml');
           http_request.overrideMimeType('text/html');
        }
     } else if (window.ActiveXObject) { // IE
        try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
           try {
              http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
        }
     }
     if (!http_request) {
        alert('Cannot create XMLHTTP instance');
        return false;
     }
         http_request.onreadystatechange = alertContents_add_carrello_prodotto;
     http_request.open('POST', url, true);
     http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     http_request.setRequestHeader("Content-length", parameters.length);
     http_request.setRequestHeader("Connection", "close");
     http_request.send(parameters);
  }

  function alertContents_add_carrello_prodotto() {
     if (http_request.readyState == 4) {
        if (http_request.status == 200) {

			var result = eval('(' + http_request.responseText + ')');
			
			var azione = result.azione;
			var cod_prod = result.cod_prod;
		    var prodotto = result.attr_prod;
		
		if(azione!=null){
			try{
			//Chiamata google analitycs (verificare libreria google)
            _gaq.push(['_trackEvent', 'Carrello', azione, prodotto]);
			} 
			catch(err){
			//alert("Inserire il codice per Google Analytics");
			}
			
			document.getElementById('aggiungi_prodotto_carrello'+cod_prod).innerHTML = '<span class="aggiungi-ok"> '+ azione + '</span>';
		}
        else{        
		   document.getElementById('aggiungi_prodotto_carrello'+cod_prod).innerHTML = '<span class="aggiungi-ko">prodotto non presente</span>';
		}		
		
        } else {
           alert('Sono sorti problemi nell\'invio dei dati');
		   document.getElementById('aggiungi_prodotto_carrello'+cod_prod).innerHTML = '<span class="aggiungi-ko"> '+ azione + '</span>';
        }
     }
  }

function aggiungi_prodotto(nome_form, sigla_lingua, id_lingua) {

	document.getElementById('aggiungi_prodotto_carrello'+document.forms[nome_form].id_prodotto.value).innerHTML = '<span class="caricamento-prodotto"> </span>';

	qt_min=document.forms[nome_form].qt_min.value;
	if (qt_min<1) { qt_min=1; }
	qt_max=document.forms[nome_form].disponibilita.value;
	if (qt_max<1) { qt_max=100; }
	
	
	if ((eval(document.forms[nome_form].qt.value)>=qt_min) && (eval(document.forms[nome_form].qt.value)<=qt_max)) {
	
		var url
		//url = "";
		url = "?";
		id_lingua=
		url += "lingua="+ document.forms[nome_form].lingua.value;
		//url += "&id_lingua=it" + id_lingua;
		url += "&cod_prod=" + document.forms[nome_form].id_prodotto.value;
		url += "&desc_prod=" + escape(document.forms[nome_form].desc_prod.value);
		url += "&attr_prod=" + escape(document.forms[nome_form].attr_prod.value);
		
		if (document.forms[nome_form].peso.value=="") {
			document.forms[nome_form].peso.value=0;
		}
		url += "&peso=" + document.forms[nome_form].peso.value;
		url += "&quantita=" + document.forms[nome_form].qt.value;
		//url += "&config=" + config;
		//alert(url);
		//aWindow=window.open(url, "aggiungi", "width=430,height=120,scrollbars=no,toolbar=no,menubar=no,status=no,resizable=yes" );

		makePOSTRequest_add_carrello_prodotto('/aggiungi-prodotto-carrello.php', url);

		//document.getElementById('contenitore-menu-carrello').innerHTML = '';
		//'<div id="menu-carrello-dx" class="box-semplice"><a href="ecommerce1.html" ><span>'+testo_carrello_stretto_menu+'</span></a><div class="interruzione"></div></div>';

	} else {
		//alert('Entri ++++');
		
		//se la quantità inserita è minore di 1 esegui la elimina prodotto
		var url
		//url = "";
		url = "?";
		id_lingua=
		url += "lingua="+ document.forms[nome_form].lingua.value;
		//url += "&id_lingua=it" + id_lingua;
		url += "&cod_prod=" + document.forms[nome_form].id_prodotto.value;
		url += "&desc_prod=" + escape(document.forms[nome_form].desc_prod.value);
		url += "&attr_prod=" + escape(document.forms[nome_form].attr_prod.value);
		
		if (document.forms[nome_form].peso.value=="") {
			document.forms[nome_form].peso.value=0;
		}
		url += "&peso=" + document.forms[nome_form].peso.value;
		url += "&quantita=" + document.forms[nome_form].qt.value;
		
	
		//document.getElementById('aggiungi_prodotto_carrello'+document.forms[nome_form].id_prodotto.value).innerHTML ='prodotto eliminato';
		
		//makePOSTRequest_delete_carrello_prodotto('/elimina-prodotto-carrello.php', url);
		makePOSTRequest_add_carrello_prodotto('/aggiungi-prodotto-carrello.php', url);
		
		//alert(testo_qt);
	}
}








