/**********************************************************
* 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();
	}
}


/*
* 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);
		}
	}

}

/********************************************************************************
* 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;
	
}


/**********************************************************
* LOGIN()
* @todo se non si e' loggati richiama l'inclusione
* del modulo di login
**********************************************************/
function login ()
{
	var richiesta = createRequest();	
	//var livello = $("#session_div");
	var livello =window.document.getElementById('session_div');
	richiesta.open('GET','include/login.php',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
* 
* una volta effettuato il login carica gli altri moduli
**********************************************************/
function invia_dati()
{
	var richiesta = createRequest();	
	var dati = prepara_dati('login');
	var livello = window.document.getElementById('modulo_login');
	richiesta.open('POST','include/login.php?login=ok' ,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)
			{
				livello.innerHTML= richiesta.responseText;
				
				//controllo per il redirect all'avviso in caso di account non agiornato
				if (richiesta.responseText.indexOf("status non aggiornato")>-1)
				{
					mostra_scheda('aggiornamento_status',0);
				}
				//controllo per il redirect all'avviso in caso di ditta non confermata
				if (richiesta.responseText.indexOf("ditta non confermata")>-1)
				{
					mostra_scheda('conferma_ditta',0);
				}
				//carico i moduli 
				aggiorna_carrello('add',0, 1); 
				carica_banner();
				
				//caricamento modulo solo se in pagina catalogo
				if (window.document.getElementById('modulo_catalogo')!=null)
				{
					logga_catalogo();
				}
				//carica_carrello_dx();
				
				//caricamento modulo solo se in index
				if (window.document.getElementById('blocco_vetrina')!=null)
				{
					aggiorna_vetrina('');
				}
				var percorso = window.location.pathname;
				var pagina = percorso.substring(percorso.lastIndexOf('/') + 1); 
				if ((pagina =='index.php')||(pagina =='catalogo.php')||(pagina =='search.php')||(pagina =='news.php')||(pagina =='eventi.php'))
				{
					//logga_carrello_dx();
				}
			}
			else
			{
				livello.value = 'errore';
			}
		}
	};
	richiesta.send(dati);
}

/*******************************************************************************************************
 * 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);
}



function mostra_ricambio(id)
{
	var contenitore = window.document.getElementById('modulo_catalogo');
	contenitore.innerHTML = '<img src="img/loading.gif">';	
	var req = createRequest();
	req.open('GET','catalogo/main.php?show=scheda&id='+id,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);
}



/**************************************************************************
 * 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)
{
	if (window.document.getElementById('scheda'))
	{
		window.document.body.removeChild(window.document.getElementById('scheda'));
		window.document.body.removeChild(window.document.getElementById('oscurante'));
	}
		//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';
		
		cap = 0;
		if (parseInt(window.document.body.offsetHeight)==0) 
		{
			cap=screen.height;
		}
		oscurante.style.width = window.document.body.offsetWidth+"px";
		oscurante.style.height = (window.document.body.offsetHeight+cap)+"px";
		oscurante.style.position = "fixed";
		//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','vetrina/inc_scheda.php?tipo='+tipo+'&id='+id,true);
		req.onreadystatechange = function ()
		{
			if (req.readyState==4)
			{
				if(req.status==200)
				{
					
					contenitore.innerHTML=req.responseText;
					evalJS(contenitore);
				}
			}
		};
		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_dx');
	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('carrello_top');
	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);
		//aggiorna_banner_carrello();
	}
}


function aggiorna_banner_carrello()
{
	var contenitore = window.document.getElementById('modulo_carrello_dx');
	contenitore.innerHTML = '<img src="img/loading.gif" width="20px" />';
	var req = createRequest();
	req.open('GET', 'carrello/banner_dx.php?ajax=true', 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);
}

/************************************************
* 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);
		aggiorna_banner_carrello();
	}
	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='29px';
	//alert(window.document.getElementById('carrello_top').offsetLeft+20);
	var pos = window.document.getElementById('carrello_top').offsetLeft;
	popup.style.left=pos+'px';
	//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.getElementById('carrello_top').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,scheda, ssid)
{	
	var richiesta = createRequest();	
	var dati = prepara_dati(form);
	var livello = window.document.getElementById('modulo_carrello');
	richiesta.open('POST','carrello/main.php?ajax=true&'+ssid ,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;
				if (scheda=='true')
				{
					mostra_scheda ('pagamento',0);
				}
			}
			else
			{
				livello.value = 'errore';
			}
		}
	};
	richiesta.send(dati);
}


function invia_vespa_club( regione)
{
	var target = window.document.getElementById("vespa_club");
	target.innerHTML = "<img src='img/loading.gif'>";
	var req = createRequest();
	req.open('GET', 'register/inc_vespa_club.php?regione='+regione, true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				target.innerHTML= req.responseText;
			}
			else if (req.status ==500)
			{
				target.innerHTML= "errore";
			}	
	};
	req.send(null);
	
}



function logga_catalogo()
{
	var target = window.document.getElementById("modulo_catalogo");
	target.innerHTML = "<img src='img/loading.gif'>";
	var req = createRequest();
	req.open('GET', 'catalogo/main.php?nav=ajax&show=cat', true);
	req.onreadystatechange =function()
	{
		if (req.status ==200)
			{
				target.innerHTML= req.responseText;
				setTimeout("self.location.reload(true)",1);
			}
			else if (req.status ==500)
			{
				target.innerHTML= "errore";
			}	
	};
	req.send(null);
}

/*
function logga_carrello_dx()
{
	if (window.document.getElementById('modulo_news')!=null)
		var target_news = window.document.getElementById('modulo_news');
	if (window.document.getElementById('modulo_eventi')!=null)
		var target_eventi = window.document.getElementById('modulo_eventi');
	var target = window.document.getElementById('colonna_dx');
	target.removeChild(target_news);
	target.removeChild(target_eventi);
	
	var carrello_dx = window.document.createElement('div');
	carrello_dx.id = 'modulo_carrello_dx';
	carrello_dx.className='blocco';
	
	target.appendChild(carrello_dx);
	
	aggiorna_banner_carrello();
}
*/

/*************************************************************************************
* function ESPANDI
*
**************************************************************************************/
function espandi(tipo)
{
	if (!(isNaN(tipo))) tipo = 'address';
	switch(tipo)
	{
		case 'cart':
			//chiude gli altri
			if ($('#sospesi').css("display")=="block") { $('#sospesi').hide(); }
			if ($('#preventivi').css("display")=="block") { $('#preventivi').hide(); }
			
			if ($('#carrello_popup').css("display")=="block")
			{
				$('#carrello_popup').hide();
			}
			else
				$('#carrello_popup').show();
		break;
		
		case 'sospesi':
			//chiude gli altri
			if ($('#carrello_popup').css("display")=="block") { $('#carrello_popup').hide(); }
			if ($('#preventivi').css("display")=="block") { $('#preventivi').hide(); }
			
			if ($('#sospesi').css("display")=="block")
			{
				$('#sospesi').hide();
			}
			else
				$('#sospesi').show();			
		break;
		
		case 'preventivi' :
			if ($('#carrello_popup').css("display")=="block") { $('#carrello_popup').hide(); }
			if ($('#sospesi').css("display")=="block") { $('#sospesi').hide(); }
			
			if ($('#preventivi').css("display")=="block")
			{
				$('#preventivi').hide();
			}
			else
				$('#preventivi').show();
		break;
		
		// espande il livello di inserimento nuovo indirizzo in _step2
		case 'new':
			window.document.getElementById('ind_saved').style.visibility ='visible';
			window.document.getElementById('ind_saved').style.display ='block';
		break;
		
		// nasconde il form di inserimento di un nuovo indirizzo quando si sceglie un indirizzo tra i salvati
		case 'address' :
			if (window.document.getElementById('ind_saved').style.visibility =='visible')
			{
				window.document.getElementById('ind_saved').style.visibility ='hidden';
				window.document.getElementById('ind_saved').style.display= 'none';
			}
		break;
		
		case 'err_form' :
			
			window.document.getElementById('err_form').style.visibility ='visible';
			window.document.getElementById('err_form').style.display ='block';
			
			setTimeout("$('#err_form').hide('slow')",2000);
		break;
		
	}
}


/************************************************************************************
* visualizza_indirizzo_spedizione
*
* permette la visaulizzazione dell'indirizzo di spediazione in check out step 3
*************************************************************************************/
function visualizza_indirizzo_spedizione(value)
{
	switch(value)
	{
		case 'default':
			if (window.document.getElementById('new').style.visibility=='visible')
			{
				window.document.getElementById('new').style.visibility='hidden';
				window.document.getElementById('new').style.display='none';
				
			}
			window.document.getElementById('default').style.visibility='visible';
			window.document.getElementById('default').style.display='block';
		break;
		
		case 'new':
			if (window.document.getElementById('default').style.visibility=='visible')
			{
				window.document.getElementById('default').style.visibility='hidden';
				window.document.getElementById('default').style.display='none';
			}
			window.document.getElementById('new').style.visibility='visible';
			window.document.getElementById('new').style.display='block';
		break;
		
	}
}


/**********************************
*
* invia form
*
* -> form : nome del form 
* ->target: livello target
* ->URL : indirizzo url dello script
*
***********************************/
function invia_form(form,target,URL)
{
	var check = controlla_campi(form);
	if (check)
	{
	var richiesta = createRequest();	
	var dati = prepara_dati(form);
	var livello = window.document.getElementById(target);
	richiesta.open('POST',URL ,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)
			{
				livello.innerHTML= richiesta.responseText;
				//evalJS(livello);
			}
			else
			{
				livello.value = 'errore';
			}
		}
	};
	richiesta.send(dati);
	}
	else
	{
		espandi('err_form');
	}
}


/**************************************************
*
* function is_empty(record)  boolean
*
* controllo del valore di un input di un form
* se il campo e' vuoto colora i bordi di rosso
* se non e' vuoto li rimette default
* var record -> e' l'oggetto input del form
* ritorna TRUE se vuoto
*
***************************************************/
function is_empty(record)
{
	var check = true;
	if (record.value=="")
	{
			check= true;
			record.style.borderColor='#FF0000';
	}
	else if (record.value != "")
	{
		check = false;
		record.style.borderColor='#DDDDDD';
	}
	return check;
}



/**********************************************************
* controlla_campi
*
* fa il check di inserimento dei campi nei form.
* -> form : nome del form da controllare
*************************************************************/
function controlla_campi(form)
{
	var check  =true;
	switch(form)
	{		
		case 'aggiungi_indirizzo':
			if (is_empty(window.document.forms[form].nome)) check =false;
			if (is_empty(window.document.forms[form].cognome)) check =false;
			if (is_empty(window.document.forms[form].indirizzo)) check =false;
			if (is_empty(window.document.forms[form].citta)) check =false;
			if (is_empty(window.document.forms[form].provincia)) check =false;
			if (is_empty(window.document.forms[form].cap)) check =false;
		break;
	}
	return check;
}
