/**********************************************************
* CREATE_REQUEST()
* @todo crea una richesta XMLHttp che vada bene per ogni browser
**********************************************************/
function createRequest()
{
	if (typeof XMLHttpRequest != 'undefined')
	{
			return new XMLHttpRequest();	// nel caso di browser che NON sono IE
	}
	else if(window.ActiveXObject)
	{
			var versione=["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","Microsoft.XMLHttp"];
			for (var i=0; i<versione.lenght; i++)
			{
				try
				{
					var req = new ActiveXObject(versione[i]);
					return req;
				}
				catch (oError)
				{}
			}
	}
	throw new Error ('XMLHttp Object could be created to go ahead');				
	
}

function createRequestObject()
{
	if (typeof XMLHttpRequestObject != 'undefined')
	{
		return new XMLHttpRequestObject();
	}
}


/********************************************************************************
* PREPARA_DATI(form)
* @todo prende i valori dei campi del form nome_form e li 
* converte in stringa utile al passaggio dei dati
* per ajax
* @param form prende in input un object form da cui  verranno letti i dati
* @return stringa string con i dati concatenati pronti per l'invio
********************************************************************************/
function prepara_dati(form)
{
	var form = window.document.forms[form];
	var stringa = "";
  	var numeroElementi = form.elements.length;  
  	for(var i = 0; i < numeroElementi-1; i++)
	{
    	if(i < numeroElementi-2)
		{
      		stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
		}
		else
		{
		  stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
		}  
	}
	return stringa;
	
}

/******************************************************************************************
 * INVIA_MODULO()
 * 
 * modulo richiama il modulo specifico NOT Null
 * azione l'azione che si vuole compiere
 * guest eventuali altre varibili da passare. Viene passata una stringa gia formattata
 * 		del tipo "&var1=val1&var2=val2". Puo essere null
 ******************************************************************************************/
function invia_modulo(modulo,azione,guest)
{
	guest += '&nav=a'; //aggiungo questa stringa per avvisare che c'e' una chiamata ajax
	var richiesta = createRequest();	
	var livello =window.document.getElementById(modulo+'_cont');
	livello.innerHTML='<img src="img/loading.gif" />';
	richiesta.open('GET','modules/'+modulo+'/main.php?mod='+modulo+'&a='+azione+guest,true);
	richiesta.onreadystatechange = function ()
	{
		if(richiesta.readyState ==4)
		{
			if(richiesta.status==200)
			{
				//tutto ok posso procedere
				livello.innerHTML= richiesta.responseText;
			}
			else
			{
				livello.value = 'errore';
			}
		}
	};
	richiesta.send(null);
}



/**********************************************************
* INVIA_DATI()
* @todo legge il form di login 
* e manda i dati per il controllo di accesso
**********************************************************/
function invia_dati(modulo,path)
{
	var richiesta = createRequest();	
	var dati = prepara_dati(modulo);
	var livello = window.document.getElementById(modulo+'_cont');
	richiesta.open('POST',path ,true);
	richiesta.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	richiesta.onreadystatechange = function ()
	{
		if (richiesta.readyState!=4)
		{
			livello.innerHTML = '<img src="img/loading.gif">';	
		}
		else if(richiesta.readyState ==4)
		{
			if(richiesta.status==200)
			{
				//tutto ok posso procedere
				//CARICO I MODULI DEL LOGIN
				livello.innerHTML= richiesta.responseText;
			}
			else
			{
				livello.value = 'errore';
			}
		}
	};
	richiesta.send(dati);
}



/******************************************************************************************
 * APRI_GALLERY()
 * 
 * modulo richiama il modulo specifico NOT Null
 * azione l'azione che si vuole compiere
 * guest eventuali altre varibili da passare. Viene passata una stringa gia formattata
 * 		del tipo "&var1=val1&var2=val2". Puo essere null
 ******************************************************************************************/
function apri_gallery(tipo)
{
	var guest = '&nav=a'; //aggiungo questa stringa per avvisare che c'e' una chiamata ajax
	var richiesta = createRequest();	
	var livello =window.document.getElementById('gallery_cont');
	richiesta.open('GET','_inc_gallery.php?gallery='+tipo+guest,true);
	richiesta.onreadystatechange = function ()
	{
		if(richiesta.readyState ==4)
		{
			if(richiesta.status==200)
			{
				//tutto ok posso procedere
				livello.innerHTML= richiesta.responseText;
			}
			else
			{
				livello.value = 'errore';
				evalJS(livello);
			}
		}
	};
	richiesta.send(null);
}


/*
* permettere di eseguire script js anche dopo chiamate ajax
*/
function evalJS(elementDOM)
{
	var scripts = elementDOM.getElementsByTagName('script');
	var code
	for (var i=0; i< scripts.length; i++)
	{
		code = scripts[i].innerHTML ? scripts[i].innerHTML : scripts[i].text ? scripts[i].text : scrpts[i].textContent;
		try
		{
			eval(code);
		}
		catch(e)
		{
			alert(e);
		}
	}

}
/********************************** CALENDARIO PER LA PRENOTAZIONE ******************/
function apri_calendario(parametro, mese, anno)
{
	//controllo prima di tutto che l'altro calendario non sia gia aperto. Nel caso lo chiudo.
	if (parametro=='dal') { window.document.forms['prenotazione'].dal.style.border='1px solid #DDDDDD;'; if (window.document.getElementById('cal_al')!=null) chiudi_calendario('al','','') }
	if (parametro=='al') { window.document.forms['prenotazione'].al.style.border='1px solid #DDDDDD;'; if (window.document.getElementById('cal_dal')!=null) chiudi_calendario('dal','','') }	
	var target='';
	var contenitore='';
	var data_cont='';
	var path = 'modules/prenotazioni/mese.php?nav=a&parametro='+parametro;
	if ((mese!='')&&(anno!=''))
		path = 'modules/prenotazioni/mese.php?nav=a&parametro='+parametro+'&m='+mese+'&y='+anno;
	var richiesta = createRequest();
	
	//se il calendario non e' stato ancora aperto allora creo il contenitore
	if (window.document.getElementById('cal_'+parametro)==null)
	{
		target = window.document.forms['prenotazione'];
		contenitore = window.document.createElement('div');
		contenitore.id='cal_'+parametro;
		contenitore.className = 'calendario';
		// diamo il posizionamento rispetto all'input
		if (parametro=='dal')
			data_cont = target.dal;
		else if (parametro =='al')
			data_cont = target.al;	
		contenitore.style.top = (data_cont.offsetTop+30)+'px';
		target.appendChild(contenitore);
	}
	//altrimenti significa che sto navigando tra i mesi 
	else
	{
		contenitore = window.document.getElementById('cal_'+parametro);
	}
	contenitore.innerHTML='<img src="img/loading.gif" />';
	richiesta.open('GET',path,true);
	richiesta.onreadystatechange = function ()
	{
		if(richiesta.readyState ==4)
		{
			if(richiesta.status==200)
			{
				contenitore.innerHTML= richiesta.responseText;
			}
			else
			{
				contenitore.value = 'errore';
			}
		}
	};
	richiesta.send(null);

}

function chiudi_calendario(parametro, giorno, mese, anno)
{
	var data_cont='';
	var contenitore = window.document.getElementById('cal_'+parametro);
	var target = window.document.forms['prenotazione'];
	target.removeChild(contenitore);
	
	if ((giorno!='')&&(mese!='')&&(anno!=''))
	{
		if (parametro=='dal')
			data_cont = target.dal;
		else if (parametro =='al')
			data_cont = target.al;
		
		data_cont.value= giorno+'/'+mese+'/'+anno;
	}
}


function check_date(form)
{
	var check = false;
	var dal = form.dal.value;
	var al = form.al.value;
	
	//controllo che non siano vuote
	if ((dal!='')&&(al!=''))
	{
		check =true;
	}
	else
	{
		check=false;
		if (dal=='') form.dal.style.border='1px solid #FF0000';
		if (al=='') form.al.style.border='1px solid #FF0000';
	}
	
	//controllo che non siano uguali al value iniziale
	if ((dal!='dal')&&(al!='al'))
	{
		check = true;
	}
	else
	{
		check=false;
		if (dal=='dal') form.dal.style.border='1px solid #FF0000';
		if (al=='al') form.al.style.border='1px solid #FF0000';
	}
	
	//controllo che siano date
	if ((dal.match(/[a-zA-Z]/)==null)&&(al.match(/[a-zA-Z]/)==null))
	{
		//splitto le date per il controllo sulla correttezza delle date stesse
		dal = dal.split("/");
		al = al.split("/");
		

		//lunghezza dell'array DEVE essere 3 (giorno mese anno)
		if ((dal.length==3)&&(al.length==3))
		{
			data_dal = new Date(dal[2], dal[1], dal[0]);
			data_al = new Date(al[2], al[1], al[0]);
			
			if (data_dal < data_al )
			{
				check = true;
			}
			else
			{
				check=false;
				form.al.style.border='1px solid #FF0000';
			}
		}
		else
		{
			check=false;
			if (dal.length!=3) form.dal.style.border='1px solid #FF0000';
			if (al.length!=3) form.al.style.border='1px solid #FF0000';
		}
	}
	else
	{
		check = false;
		if (dal.match(/[a-zA-Z]/)!=null) form.dal.style.border='1px solid #FF0000';
		if (al.match(/[a-zA-Z]/)!=null) form.al.style.border='1px solid #FF0000';
	}
	if (check==true)
	{
		form.submit();
	}
}
/********************************** END CALENDARIO *********************************/

/***************************FUNZIONI PER LA PRENOTAZIONE**********************/
function calcola_stanze(n_persone,tot, camera, camere)
{
	
	var min = 1;
	var max = n_persone;
	if (n_persone > tot)
	{
		max =tot;
	}
	var target = window.document.getElementById('n_stanze');
	if (window.document.forms['prenotazione'].n_stanze!=null)
	{
		target.removeChild(window.document.forms['prenotazione'].n_stanze);	
	}
	var stanze = window.document.createElement('select');
	stanze.name = 'n_stanze';
	stanze.options.add(new Option('n. '+camere ,'0'));
	if (n_persone > 5)
	{
		min =2;
	}
	for (i = min ; i<=max; i ++)
	{
		if (i==1)
		{
			stanze.options.add(new Option('1 '+camera, '1'));
		}
		else
		{
			stanze.options.add(new Option(i+' '+camere ,i));
		}
	}	
	target.appendChild(stanze);
}


function calcola_postiletto(box, n_persone,n_stanze)
{
	var subtotale =0;
	var count =0;
	var totale ='';
	var form = window.document.forms['prenotazione_2'];
	var dimensione = form.length;
	var postiletto = box.name.split('_');
	postiletto = postiletto[1];
	if (box.checked==true)
	{
		form['persone_'+postiletto].disabled=false;
		for (i=0; i<dimensione; i++)
		{
			if (form[i].type=='checkbox')
			{
				if ((form[i].checked==true)&&(form[i].name!=box.name))
				{
					count ++;
					subtotale += parseInt(form[i].value);
					
				}
			}
			//attivo il select a fianco
		}
		if (count == n_stanze)
		{
			//alert('numero massimo di stanze raggiunto');
			//disattiva gli altri
			for (i=0; i<dimensione; i++)
			{
				box.checked=false;
				if ((form[i].type=='checkbox')&&(form[i].checked==false))
				{
					form[i].disabled=true;
				}
			}
			form['bottone_submit'].disabled=false;
		}
		else if (count<n_stanze)
		{
			count ++;
			totale = subtotale + parseInt(box.value);		
			if (count == n_stanze)
			{
				//alert('numero massimo di stanze raggiunto');
				//disattiva gli altri
				for (i=0; i<dimensione; i++)
				{
					if ((form[i].type=='checkbox')&&(form[i].checked==false))
					{
						form[i].disabled=true;
					}
				}
				form['bottone_submit'].disabled=false;
			}
			else if ((totale >= n_persone)&&(count<n_stanze))
			{
				//alert('come cazzo scegli');
				//disattiva gli altri
				for (i=0; i<dimensione; i++)
				{
					if ((form[i].type=='checkbox')&&(form[i].checked==false)&&(count==n_stanze))
					{
						form[i].disabled=true;
					}
				}
			}
			else
			{
				//alert ('puo scegliere ancora');
			}
		}
		else
		{
			alert('numero di posti letto sufficienti');
			//disattiva gli altri
			for (i=0; i<dimensione; i++)
			{
				if ((form[i].type=='checkbox')&&(form[i].checked==false)&&(count==n_stanze))
				{
					form[i].disabled=true;
				}
			}
			form['bottone_submit'].disabled=false;
		}
	}
	else
	{
	 	//alert('riattiva gli altri');
	 	//disattiva gli altri
		for (i=0; i<dimensione; i++)
		{
			if ((form[i].type=='checkbox')&&(form[i].disabled==true))
			{
				form[i].disabled=false;
			}
		}
		form['bottone_submit'].disabled=true;
		form['persone_'+postiletto].selectedIndex='0';
		form['persone_'+postiletto].disabled=true;
	}
}


function calcola_capienze(select, n_persone)
{
	var count =0;
	var totale ='';
	var form = window.document.forms['prenotazione_2'];
	var dimensione = form.length;
	var postiletto = select.name.split('_');
	postiletto = postiletto[1];
	for (i=0; i<dimensione; i++)
	{
		if (form[i].type.substring(0,6)=='select')
		{
			count = count + parseInt(form[i].options[form[i].selectedIndex].value);
		}
	}

	if(count>n_persone)
	{
		count = count - parseInt(select.options[select.selectedIndex].value);
		rimanenti = n_persone - count;
		select.selectedIndex= rimanenti;
	}
}

/********************************
*
* check del form prenotazione_2
*
*********************************/
function check_prenotazione (form,step)
{
	var check = true;
	switch (step)
	{
		case '2':
			for (i=0; i<form.length; i++)
			{
				if ((form[i].type!='hidden')&&(form[i].type!='textarea'))
				{
					if (form[i].value=='')
					{
						check=false;
					}	
				}
			}
		break;	
	}
	if (check == true)
	{
		form.submit();
	}

}

















/**************************************************************************
 * MOSTRA_SCHEDA (tipo , id) - POP UP !!!
 * 
 * apre un livello tipo pop-up con la scheda dell'oggetto
 * 
 * @param tipo tipologia dell'oggetto da caricare (ricambio, news, evento, ..)
 * @param id id dell'oggetto
 ***************************************************************************/
function mostra_scheda(tipo, id, target )
{
	if (window.document.getElementById('scheda'))
	{
		
		window.document.body.removeChild(window.document.getElementById('scheda'));
	}
		//creo un livello fittizio che copra il body in modo da per disabilitare gli altri link
		var oscurante = window.document.createElement('div');
		oscurante.id='oscurante';
		oscurante.style.width = window.document.body.offsetWidth+"px";
		oscurante.style.height = window.document.body.offsetHeight+"px";
		//creo il livello dove sara contenuto il contenitore per la scheda
		var  scheda = window.document.createElement('div');
		scheda.id= 'scheda';
		//posizioni X e Y per il posizionamento del livello sul body;
		//il posizionamento verticale è calcolato in base all'altezza della finestra del browser
		//alert(document.documentElement.scrollTop);
		if(navigator.appName.indexOf("Microsoft")!=-1)
		{
			scheda.style.top=window.document.body.scrollTop+(window.document.body.offsetHeight/2)-100+'px';
			scheda.style.left=(window.document.body.offsetWidth)/2-300+'px';
		}
		else if (navigator.userAgent.indexOf("Firefox")!=-1)
		{
			scheda.style.top=document.documentElement.scrollTop+(window.innerHeight/2)-100+'px';
			scheda.style.left=(window.document.body.offsetWidth)/2-300+'px';
		}
		else
		{
			scheda.style.top=window.document.body.scrollTop+(window.innerHeight/2)-100+'px';
			scheda.style.left=(window.document.body.offsetWidth)/2-300+'px';
		}
		
		//sapn con la X per chiudere
		var chiudi = window.document.createElement('span');
		chiudi.id ='chiudi';
		chiudi.innerHTML = '';
		chiudi.onclick =function()
		{
			window.document.body.removeChild(oscurante); 
			window.document.body.removeChild(scheda); 
		};
		var contenitore = window.document.createElement('div');
		contenitore.id='contenitore_testo';
		window.document.body.appendChild(oscurante);
		window.document.body.appendChild(scheda);
		scheda.appendChild(chiudi);
		scheda.appendChild(contenitore);
		contenitore.innerHTML='<span style="text-alignment:center"><img src="img/loading.gif"></span>';
		//faccio la chiamata AJAX per riempire la scheda
		var req = createRequest();
		req.open('GET',target,true);
		req.onreadystatechange = function ()
		{
			if (req.readyState==4)
			{
				if(req.status==200)
				{
					
					contenitore.innerHTML=req.responseText;
				}
			}
		};
		req.send(null);
}



/*******************************************************************************************************
 * carica banner
 * 
 * @todo una volta che uno si logga deve scomparire il modulo della registrazione 
 * e comparire quello dei banner
 *********************************************************************************************************/
function carica_banner()
{
	var target = window.document.getElementById('banner_registrati');
	target.innerHTML = "<img src='img/loading.gif'>";
	var req = createRequest();
	req.open('GET', 'include/inc_rec.php?rec=ok', true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				target.innerHTML= req.responseText;
			}
			else 
			if (req.status ==500)
			{
				target.innerHTML= "errore";
			}	
	};
	req.send(null);
	
}

/******************************************************************************
* CARICA_MODULI(id del livello target, nome della cartella del modulo)
* prende i valori dei campi del form nome_form e li 
* converte in stringa utile al passaggio dei dati
* per ajax
* @param id_livello
* @param nome_cartella
*******************************************************************************/
function carica_moduli(id_livello, nome_cartella)
{
	var contenitore = window.document.getElementById(id_livello);
	contenitore.innerHTML = '<img src="img/loading.gif">';	
	var req_moduli = createRequest();
	req_moduli.open('GET',nome_cartella+'/main.php?status=login',true);
	req_moduli.onreadystatechange = function ()
	{
		if(req_moduli.readyState ==4)
		{
			if(req_moduli.status==200)
			{
				//tutto ok posso procedere
				contenitore.innerHTML= req_moduli.responseText;
			}
			else
			{
				contenitore.innerText = 'errore';
			}
		}
	};
	req_moduli.send(null);
	
}


/*************************************************************
 *  MOSTRA_LISTA_RICAMBI (id_categoria)
 *  funzione che carica la lista dei ricambi di una categoria
 *  AJAX
 * @param id_categoria
 **************************************************************/
function mostra_lista_ricambi(id_categoria,init,max_num)
{
	var contenitore = window.document.getElementById('modulo_catalogo');
	contenitore.innerHTML = '<img src="img/loading.gif">';	
	var req = createRequest();
	req.open('GET','catalogo/main.php?show=ric&id='+id_categoria+'&init='+init+'&max='+max_num,true);
	req.onreadystatechange = function()
	{
		if(req.readyState == 4)
		{
			if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
			if (req.status == 500)
			{
				contenitore.innerHTML= "ERRORE 500";
			}
		}
	
	};
	req.send(null);
}

/*********************************************************
 *  MOSTRA_LISTA_NEWS (init,max_num)
 *  funzione che carica la lista dei ricambi di una categoria
 *  AJAX
 * @param init indice d inizio per la query
 * @param max_num numero massimo di record per pagina
 *******************************************************/
function mostra_lista_news(init,max_num)
{
	var contenitore = window.document.getElementById('modulo_news');
	contenitore.innerHTML = '<img src="img/loading.gif">';	
	var req = createRequest();
	req.open('GET','news/lista.php?init='+init+'&max='+max_num,true);
	req.onreadystatechange = function()
	{
		if(req.readyState == 4)
		{
			if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
			if (req.status == 500)
			{
				contenitore.innerHTML= "ERRORE 500";
			}
		}
	
	};
	req.send(null);
}

/*******************************************************************
 *  MOSTRA_LISTA_EVENTI (init,max_num)
 *  funzione che carica la lista dei ricambi di una categoria
 *  AJAX
 * @param init indice d inizio per la query
 * @param max_num numero massimo di record per pagina
 *****************************************************************/
function mostra_lista_eventi(init,max_num)
{
	var contenitore = window.document.getElementById('modulo_eventi');
	contenitore.innerHTML = '<img src="img/loading.gif">';	
	var req = createRequest();
	req.open('GET','eventi/lista.php?init='+init+'&max='+max_num,true);
	req.onreadystatechange = function()
	{
		if(req.readyState == 4)
		{
			if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
			if (req.status == 500)
			{
				contenitore.innerHTML= "ERRORE 500";
			}
		}
	
	};
	req.send(null);
}






/**********************************************************************************
 * SEARCH_CAT
 * 
 ***********************************************************************************/
function ricerca(id_cat, id_mod, id_telaio,id_anno,seriale, init,max)
{
	var contenitore = window.document.getElementById("modulo_search");
	var target = window.document.getElementById("form_ricerca");
	var risultati = window.document.getElementById("search_result");
	target.innerHTML = '<img src="img/loading.gif"> Updating search fields..';
	risultati.innerHTML = '<img src="img/loading.gif"> Updating results..';
	var req = createRequest();
	req.open ('GET','search/main.php?cat='+id_cat+'&mod='+id_mod+'&telaio='+id_telaio+'&anno='+id_anno+'&seriale='+seriale+'&init='+init+'&max='+max,true);
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
		}
	};
	req.send(null);
}



/**************************************************************************
 * AUTOCOMP(testo)
 *
 * autocompletamento nel form di ricerca
 * a testo libero
 ***************************************************************************/
function autocomp(testo)
{
	var contenitore = window.document.getElementById("modulo_autocomp");
	if (testo=='')
	{
		contenitore.style.visibility='hidden';
	}
	else if (testo!='')
	{
		contenitore.style.visibility='visible';
	}
	contenitore.innerHTML = '<img src="img/loading.gif">';
	var req = createRequest();
	req.open ('GET','search/banner.php?testo='+testo,true);
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
			else 
			if (req.status ==500)
			{
				contenitore.innerHTML= 'problemi di caricamento';
			}
		}
	};
	req.send(null);
	
}

function chiudi_autocomp()
{
	var contenitore = window.document.getElementById("modulo_autocomp");
	contenitore.style.visibility='hidden';
	window.document.search.keyword.value='';
}
/*********************************************************FUNZIONI DELLA VETRINA ***********************************************************/

/***********************************************************************
 * aggiorna vetrina
 * @todo aggiorna la vetrina dei prodotti nella index
 * @param azione decide tra vetrina ultimi arrivi e i piu venduti
 ***********************************************************************/
function aggiorna_vetrina(azione)
{
	var contenitore = window.document.getElementById('colonna_sx');
	window.document.getElementById('blocco_vetrina').innerHTML = '<img src="img/loading.gif">';
	var req = createRequest();
	req.open('GET','vetrina/inc_vetrina.php?vetrina='+azione);
	req.onreadystatechange = function()
	{
		if (req.readyState == 4)
		{
			if(req.status==200)
			{
				contenitore.innerHTML=req.responseText;
			}
			else if (req.status==500)
			{
				contenitore.innerHTML='Ci sono stati problemi di caricamento.<br/ >Ricarica la pagina e riprova';
			}
		}
	};
	req.send(null);
}



/*********************************************************FUNZIONI DEL CARRELLO ***********************************************************/


/************************************************
* AGGIUNGI RICAMBIO
*
* @todo prende in input l'id del pezzo di ricambio
* @param azione
* @param id_prod
* @param quantita
*************************************************/
function aggiorna_carrello(azione,id_prod,quantita)
{
	var contenitore = window.document.getElementById('cerca_bottom');
	contenitore.innerHTML = '<div id="img_carrello"><img src="img/carrello.jpg" alt="Carrello" width="23" height="20" /></div><div id="testo"><img src="img/loading.gif"></div>';
	var req = createRequest();
	req.open('GET', 'carrello/banner.php?action_prod='+azione+'&id_prod='+id_prod+'&q='+quantita, true);
	req.onreadystatechange =function()
	{
		
		if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
			else 
			if (req.status ==500)
			{
				contenitore.innerHTML= 'problemi di caricamento';
			}	
	};
	req.send(null);
	if(id_prod!=0)
	{
		mostra_ricambio_aggiunto(azione,id_prod,quantita);
	}
}

/************************************************
* AGGIUNGI RICAMBIO DA SEARCH
*
* prende in input l'id del pezzo di ricambio
* e anche i dati della vespa
* modello telaio e anno
*
*************************************************/
function aggiorna_carrello_search(azione,id_prod,quantita,mod,telaio,anno)
{
	if(id_prod!=0)
	{
		mostra_ricambio_aggiunto(id_prod,quantita);	
	}
	var contenitore = window.document.getElementById('cerca_bottom');
	contenitore.innerHTML = '<div id="img_carrello"><img src="img/carrello.jpg" alt="Carrello" width="23" height="20" /></div><div id="testo"><img src="img/loading.gif"></div>';
	var req = createRequest();
	req.open('GET', 'carrello/banner.php?action_prod='+azione+'&id_prod='+id_prod+'&q='+quantita+'&mod='+mod+'&telaio='+telaio+'&anno='+anno, true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				contenitore.innerHTML= req.responseText;
			}
			else 
			if (req.status ==500)
			{
				contenitore.innerHTML= 'problemi di caricamento';
			}	
	};
	req.send(null);
	
}

/********************************************
 * Mostra_ricambio_aggiunto - POP UP
 * 
 * crea un livello popup che avvisa quando 
 * un ricambio viene aggiunto al carrello
 * 
 *********************************************/
	
function mostra_ricambio_aggiunto(azione,id,quantita)
{
	// LIVELLO POP UP
	var popup = window.document.createElement('div');
	popup.id='popup_carrello';
	popup.style.position='fixed';
	popup.style.top='10px';
	popup.style.right='10px';
	//faccio la chiamata AJAX per riempire la scheda
	var req = createRequest();
	req.open('GET','vetrina/inc_scheda.php?tipo=prod_aggiunto&id='+id+'&q='+quantita+'&azione='+azione,true);
	req.onreadystatechange = function ()
	{
		if (req.readyState==4)
		{
			if(req.status==200)
			{				
				popup.innerHTML=req.responseText;
			}
		}
	};
	req.send(null);
	window.document.body.appendChild(popup);
	var tempo = setTimeout("$('div#popup_carrello').fadeOut('slow')",2000);
	if ((azione =='mod')||(azione=='canc'))
	{
		var pausa =setTimeout("location.href='carrello.php'",500);
	}
	
}


/*
 * 
 */
function aggiorna_cartalogo(azione,id_prod,quantita)
{
	var contenitore = window.document.getElementById('cerca_bottom');
	contenitore.innerHTML = '<div id="img_carrello"><img src="img/carrello.jpg" alt="Carrello" width="23" height="20" /></div><div id="testo"><img src="img/loading.gif"></div>';
	var req = createRequest();
	req.open('GET', 'carrello/banner.php?action_prod='+azione+'&id_prod='+id_prod+'&q='+quantita, true);
	req.onreadystatechange =function()
	{
		if ((req.status ==200)||(req.status==0))
			{
				contenitore.innerHTML= req.responseText;
			}
			else 
			if (req.status ==500)
			{
				contenitore.innerHTML= 'problemi di caricamento';
			}	
	};
	req.send(null);
	if(id_prod!=0)
	{
		mostra_ricambio_aggiunto(azione,id_prod,quantita);
	}
}

/************************************************
* CHECK CARTACEO
*
* controlla che l'utnete voglia o meno il catalogo
* cartaceo
*
*************************************************/
function check_cartaceo(box,sped)
{
	if(box.checked)
	{
		aggiorna_cartalogo('add',2055,1);
		location.href='?action_carrello=check_out&cartaceo=1&spedizione='+sped;
	}
	else
	{
		
		aggiorna_cartalogo('canc',2055,1);
		location.href='?action_carrello=check_out&cartaceo=0&spedizione='+sped;
	}
}


/************************************************
* CHECK DAT FATTURAZIONE
*
*  
*************************************************/
function check_dati_fatturazione(box)
{
	var contenitore = window.document.getElementById('dati_fatturazione');
	if (box.checked)
	{
		contenitore.style.visibility='hidden';
		contenitore.style.display='none';
	}
	else 
	{
		contenitore.style.visibility='visible';
		contenitore.style.display='block';
	}
}

function check_vespa(record)
{
	var target = window.document.getElementById("target_check_vespa");
	target.innerHTML = "<img src='img/loading.gif'>";
	var result = record;
	var req = createRequest();
	req.open('GET', 'carrello/inc_check_vespa.php?value='+result, true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				target.innerHTML= req.responseText;
			}
			else 
			if (req.status ==500)
			{
				target.innerHTML= "errore";
			}	
	};
	req.send(null);
}

/*
 * manda il modulo di check del modello nel carrello via ajax
 */
function invia_check (telaio, seriale)
{
	var target = window.document.getElementById("target_check_vespa");
	if ((telaio==0)||(seriale==''))
	{}
	else
	{
		target.innerHTML = "<img src='img/loading.gif'>";
		var req = createRequest();
		req.open('GET', 'carrello/inc_check_vespa.php?value=0&telaio='+telaio+'&seriale='+seriale, true);
		req.onreadystatechange =function()
		{
			if (req.status ==200)
				{
					target.innerHTML= req.responseText;
				}
				else 
				if (req.status ==500)
				{
					target.innerHTML= "errore";
				}	
		};
		req.send(null);
	}

}

/**************************************************************
 * 
 * INVIA VESAP SEARCH (telaio, seriale)
 * manda il modulo di di ricerca nel vespa search via ajax
 ***************************************************************/
function invia_vespa_search (telaio, seriale)
{
	var target = window.document.getElementById("vespa_search");
	if ((telaio==0)||(seriale==''))
	{}
	else
	{
		target.innerHTML = "<img src='img/loading.gif'>";
		var req = createRequest();
		req.open('GET', 'tech/inc_vespa_search.php?value=0&telaio='+telaio+'&seriale='+seriale, true);
		req.onreadystatechange =function()
		{
			if (req.status ==200)
				{
					target.innerHTML= req.responseText;
				}
				else if (req.status ==500)
				{
					target.innerHTML= "errore";
				}	
		};
		req.send(null);
	}

}

/****************************************************************************************************************************
 * 
 * APRI SCHEDA TECNICA (id_telaio, id_modello, id_annata)
 * manda il modulo di di ricerca nel vespa search via ajax
 *****************************************************************************************************************************/
function apri_scheda_tecnica (id_telaio, id_modello, id_annata)
{
		
	var target = window.document.getElementById("scheda_tecnica");
	target.innerHTML = "<img src='img/loading.gif'>";
	var req = createRequest();
	req.open('GET', 'tech/inc_scheda_tecnica.php?open=tecnica&telaio='+id_telaio+'&modello='+id_modello+'&annata='+id_annata, true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				target.innerHTML= req.responseText;
			}
			else if (req.status ==500)
			{
				target.innerHTML= "errore";
			}	
	};
	req.send(null);
	
}

function apri_impianto (impianto)
{
		
	var target = window.document.getElementById("scheda_tecnica");
	target.innerHTML = "<img src='img/loading.gif'>";
	var req = createRequest();
	req.open('GET', 'tech/inc_scheda_tecnica.php?open=impianto&impianto='+impianto, true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				target.innerHTML= req.responseText;
			}
			else if (req.status ==500)
			{
				target.innerHTML= "errore";
			}	
	};
	req.send(null);
	
}
//test.php?action_carrello=check_out&step=4
function invia_ordine(form)
{
	
	var richiesta = createRequest();	
	var dati = prepara_dati(form);
	var livello = window.document.getElementById('modulo_carrello');
	richiesta.open('POST','carrello/main.php?action_carrello=check_out&step=4' ,true);
	richiesta.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	richiesta.onreadystatechange = function ()
	{
		if (richiesta.readyState!=4)
		{
			livello.innerHTML = '<img src="img/loading.gif"> Loading ...';	
		}
		else if(richiesta.readyState ==4)
		{
			if(richiesta.status==200)
			{
				livello.innerHTML= richiesta.responseText;
			}
			else
			{
				livello.value = 'errore';
			}
		}
	};
	richiesta.send(dati);
}




