function insertOrder(TID, tipo)
{		
	var totaleordineXPAY = document.getElementById('AMOUNT').value;
			
	totaleordineXPAY = totaleordineXPAY.replace(".", "");
	
	var ZERO = totaleordineXPAY.length;
	
	if(ZERO == 1)	{ totaleordineXPAY = "00000000"+totaleordineXPAY; }
	if(ZERO == 2)	{ totaleordineXPAY = "0000000"+totaleordineXPAY; }
	if(ZERO == 3)	{ totaleordineXPAY = "000000"+totaleordineXPAY; }
	if(ZERO == 4)	{ totaleordineXPAY = "00000"+totaleordineXPAY; }
	if(ZERO == 5)	{ totaleordineXPAY = "0000"+totaleordineXPAY; }
	if(ZERO == 6)	{ totaleordineXPAY = "000"+totaleordineXPAY; }
	if(ZERO == 7)	{ totaleordineXPAY = "00"+totaleordineXPAY; }
	if(ZERO == 8)	{ totaleordineXPAY = "0"+totaleordineXPAY; }
	if(ZERO == 9)	{ totaleordineXPAY = totaleordineXPAY; }
	
	var email = document.getElementById('email_').value;
	
	document.getElementById('EMAIL').value = email;
	
	var email_xpay = email;
	
	document.getElementById('AMOUNT').value = totaleordineXPAY;
	
	var mac_order = "WEB_000000011084" + TID + totaleordineXPAY +"97801.00LAUT"+email_xpay+"QVAVIODPOUOBSVXQLKGZC";
	
	document.getElementById('MAC').value = SHA1(mac_order);
	
	var nome = document.getElementById('nome').value;
	var cognome = document.getElementById('cognome').value;
	var indirizzo = document.getElementById('indirizzo').value;
	var citta = document.getElementById('citta').value;
	var provincia = document.getElementById('provincia').value;
	var cap = document.getElementById('cap').value;
	var nazione = document.getElementById('nazione').value;
	var telefono = document.getElementById('telefono').value;
	var codfisc = document.getElementById('cf').value;
	var piva = document.getElementById('piva').value;
	var fattura = document.getElementById('fattura').checked;
	var note = document.getElementById('note').value;
	var spese_spedizione = document.getElementById('SPESE_SPEDIZIONE').value;
	var totale_peso = document.getElementById('TOTALE_PESO').value;
	var totale_ordine = document.getElementById('IMPORTO_TOTALE').value;
	
	if(document.getElementById('SCONTI_SPECIALI') == null)
	{
		var sconti_speciali = "0.00";
	}
	else
	{
		var sconti_speciali = document.getElementById('SCONTI_SPECIALI').value;	
	}
	
	if(nome == '')		{	alert('Inserire il nome'); return false;	}
	if(cognome == '')	{	alert('Inserire il cognome'); return false;	}
	if(email == '')		{	alert('Inserire la email'); return false;	}
	if(indirizzo == '')	{	alert('Inserire indirizzo'); return false;	}
	if(citta == '')		{	alert('Inserire cittą'); return false;		}
	if(provincia == '')	{	alert('Inserire provincia'); return false;	}
	if(cap == '')		{	alert('Inserire cap'); return false;		}
	if(telefono == '')	{	alert('Inserire telefono'); return false;	}
	
	CreateXmlHttpObj();
	
	var selObjDEST = document.getElementById('destinazione');
	var selIndexDEST = selObjDEST.selectedIndex;	
	var destinazione = selObjDEST.options[selIndexDEST].value;
	
	var selObjPAGAMENTO = document.getElementById('pagamento');
	var selIndexPAGAMENTO = selObjPAGAMENTO.selectedIndex;	
	var pagamento = selObjPAGAMENTO.options[selIndexPAGAMENTO].value;
	
	var requestUrl = "../AJAX/insertOrder.php";
	
	var params = "TID=" + encodeURIComponent(TID) + 
				 "&DESTINAZIONE=" + encodeURIComponent(destinazione) + 
				 "&PAGAMENTO=" + encodeURIComponent(pagamento) + 
				 "&NOME=" + encodeURIComponent(nome) + 
				 "&COGNOME=" + encodeURIComponent(cognome) + 
				 "&EMAIL=" + encodeURIComponent(email) + 
				 "&INDIRIZZO=" + encodeURIComponent(indirizzo) + 
				 "&CITTA=" + encodeURIComponent(citta) + 
				 "&PROVINCIA=" + encodeURIComponent(provincia) + 
				 "&CAP=" + encodeURIComponent(cap) + 
				 "&NAZIONE=" + encodeURIComponent(nazione) + 
				 "&TELEFONO=" + encodeURIComponent(telefono) + 
				 "&CODFISC=" + encodeURIComponent(codfisc) + 
				 "&PIVA=" + encodeURIComponent(piva) + 
				 "&FATTURA=" + encodeURIComponent(fattura) +
				 "&NOTE=" + encodeURIComponent(note) +
 				 "&SPESE_SPEDIZIONE=" + encodeURIComponent(spese_spedizione) + 
				 "&SCONTI_SPECIALI=" + encodeURIComponent(sconti_speciali) + 
				 "&TOTALE_PESO=" + encodeURIComponent(totale_peso) + 
 				 "&TOTALE_ORDINE=" + encodeURIComponent(totale_ordine);
					 
	XmlHttpObj.open("POST", requestUrl,  true);

	XmlHttpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	XmlHttpObj.setRequestHeader("Content-length", params.length);
	XmlHttpObj.setRequestHeader("Connection", "close");

	XmlHttpObj.onreadystatechange = function ()
	{
		if(XmlHttpObj.readyState == 4)
		{
			if(XmlHttpObj.status == 200)
			{
				if(XmlHttpObj.responseText == 'KO')
				{
					return false;
				}
				else
				{
					if(tipo == 'Y')
					{
						document.formX.submit();
					}
					else if(tipo == 'X')
					{
						window.location = 'ordine_confermato.php?TID='+TID;
					}
				}
			}
			else 
			{ 
				alert("CONTROLLO : problem retrieving data from the server, status code: "  + XmlHttpObj.status); 
			}
		}
	}
	
	XmlHttpObj.send(params);
}
