function PreparaDati(nome_form){
  var stringa = "";
  for (i=0; i<document.forms.length;i++) {
	  if (document.forms[i].name==nome_form) var frm = document.forms[i];
  }
  
  if (frm==null) var frm = document.forms[0];
  var numeroElementi = frm.elements.length;
  
  for(var i = 0; i < numeroElementi; i++){
	//if(frm.elements[i].type=="checkbox" && frm.elements[i].checked==true) frm.elements[i].value="1";
	//controllo se il campo non è fleggato lo metto a vuoto
	if(frm.elements[i].type=="checkbox" && frm.elements[i].checked==false){ frm.elements[i].value="";	}
    if(i < numeroElementi-1){
      stringa += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value)+"&";
    }else{
      stringa += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value);
    }  
  }
  return stringa;
}


function ajaxRequest(method,page,element,form,func){
 var xmlhttp;
 var el=document.getElementById(element);
 
  xmlhttp= new XMLHttp();
  xmlhttp.open(method,page,true);
  
  if ( method == 'POST' ) {
	  
  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.setRequestHeader('Cache-Control', 'no-cache'); // if XML code rather than a static XML file (IE)
  uristring = PreparaDati(form);
 }else{
	 uristring="";
 }
 
  xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4){
			if (el!=null){
				if(el.tagName!="INPUT"){
					el.innerHTML =xmlhttp.responseText;
				}else{
					el.value =xmlhttp.responseText;
				}
			}
			if(func) func.call();
			
		}
	}
	xmlhttp.send(uristring);
}

function XMLHttp() {
 var request = false ;
 if ( window.XMLHttpRequest ) {
  request = new XMLHttpRequest() ;
 } else if ( window.ActiveXObject ) {
  try {
   request = new ActiveXObject ( 'Msxml2.XMLHTTP' ) ;
  } catch ( e ) {
   try {
    request = new ActiveXObject ( 'Microsoft.XMLHTTP' ) ;
   } catch ( e ) {}
  }
 }
 if ( !request ) alert( 'Could not create an XMLHttpRequest istance.' );
 return request ;
}

function add(){
	ajaxRequest('POST','form_prodotto','process.php','carrello');
	setTimeout("ajaxRequest('GET','form-scheda','process.php?action=carrello_small','carrello_count')",1000);
	setTimeout("ajaxRequest('GET','form-scheda','process.php?action=totale_prodotti','totale_prodotti')",1000);
	setTimeout("ajaxRequest('GET','form-scheda','process.php?action=totale_spedizione','totale_spedizione')",1000);
	setTimeout("ajaxRequest('GET','form-scheda','process.php?action=totale_carrello','totale_carrello')",1000);
}

function aggiorna_carrello(id,div){
	ajaxRequest('POST','form','process.php?id='+id,div);
	setTimeout("ajaxRequest('GET','form','process.php?action=update_quantita','carrello')",1000);
	setTimeout("ajaxRequest('GET','spedizione','process.php?action=totale_carrello','totale_carrello')",1000);
	void(0);
}

function aggiorna_qta(id,div,qta){
	if(qta==0){
		elimina_carrello(id,div);
	}else{
		ajaxRequest('POST','process.php',div,'form',function(){ ajaxRequest('GET','process.php?action=update_quantita','carrello_count','',''); });
	}
}

function elimina_carrello(id,div){
	ajaxRequest('GET','process.php?action=delete&id='+id,div,'',function(){ ajaxRequest('GET','process.php?action=update_quantita','carrello_count','',''); });
}

function aggiorna_spedizione(id){
	ajaxRequest('GET','process.php?action=aggiorna_spedizione&id='+id,'','',function(){ ajaxRequest('GET','process.php?action=update_cart','carrello_big','',''); });
}

function aggiorna_pagamento(id){
	ajaxRequest('GET','process.php?action=aggiorna_pagamento&id='+id,'','',function(){ ajaxRequest('GET','process.php?action=update_cart','carrello_big','',''); });
}
