function ordinaOra()
{
	new Ajax.Updater('moduloOrdine', '/shop.php',
		{
			method: 'get',
			parameters: {action: 'mostraModuloOrdine'}
		}
		);
}

function impostaCodiceSconto()
{
	valore = document.getElementById('codice_sconto').value;
	new Ajax.Updater('contenutoCarrello', '/shop.php',
		{
			method: 'get',
			parameters: {action: 'impostaCodiceSconto', codiceSconto: valore}
		}
		);
}

function eliminaCodiceSconto()
{
	valore = document.getElementById('codice_sconto').value;
	new Ajax.Updater('contenutoCarrello', '/shop.php',
		{
			method: 'get',
			parameters: {action: 'eliminaCodiceSconto', codiceSconto: valore}
		}
		);
}


function cambiaModalitaPagamento(valore)
{
	new Ajax.Updater('infoPagamento', '/shop.php',
		{
			method: 'get',
			parameters: {action: 'cambiaModalitaPagamento', modalita: valore}
		}
		);

}

function inserisciContenutoFormRegistrazione(valore)
{
	new Ajax.Updater('contenutoFormRegistrazione', '/lar.php',
		{
			method: 'get',
			parameters: {action: 'inserisciContenutoFormRegistrazione', val: valore}
		}
		);

}

function inserisciContenutoFormCambioMNT(valore)
{
	new Ajax.Updater('contenutoFormCambioMNT', '/lar.php',
		{
			method: 'get',
			parameters: {action: 'inserisciContenutoFormCambioMNT', val: valore}
		}
		);

}

function inviaLAR(email, confermaEmail)
{
		new Ajax.Request('/lar.php',
		  {
		    method:'get',
		    parameters: {action: 'inviaLAR', val1: email, val2: confermaEmail},
		    onSuccess: esitoInvioLar
		  });
}

function esitoInvioLar(transport)
	{
	alert(transport.responseText);
	document.getElementById('email').parentNode.style.color = 'black';
	document.getElementById('email').style.color = 'black';
	document.getElementById('conferma_email').parentNode.style.color = 'black';
	document.getElementById('conferma_email').style.color = 'black';
	document.getElementById('invioLAR').style.color = 'black';
	switch(transport.responseText)
		{
		case 'match':
			document.getElementById('invioLAR').style.color = 'red';
			document.getElementById('invioLAR').innerHTML = '<b>Attenzione: </b> gli indirizzi email che hai inserito non combaciano!';
		break;

		case 'true':
			alert('invio');
		break;

		default:
			Campi = transport.responseText.split(',');
			for(i=0; i<Campi.length;i++)
				{
				document.getElementById(Campi[i]).parentNode.style.color = 'red';
				document.getElementById(Campi[i]).style.color = 'red';
				}
		break;
		}
	}

function cambioStato(valore, IdStato, nomeInput, valoreInput, IdProv)
	{
	if(valore == 1)
		{
		document.getElementById(IdStato).innerHTML = '';
		}
	else
		{
		document.getElementById(IdStato).innerHTML = '<input type="text" name="' + nomeInput + '" value="' + valoreInput + '" />';
		document.getElementById(IdProv).selectedIndex = 0;
		}
	}

function confermaOrdine()
{
	moduloCarrello = document.getElementById('formModuloOrdine');

	var moduloOk = true;

	for (i = 0; i < moduloCarrello.elements.length; i++)
		{
			if ((moduloCarrello.elements[i].alt == 'required') || (moduloCarrello.elements[i].type == 'select-one'))
				{
				if (moduloCarrello.elements[i].value == '')
					{
					moduloOk = false;
					document.getElementById(moduloCarrello.elements[i].id).className = 'errore';
					document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = 'lblErrore';
					}
				else
					{
					document.getElementById(moduloCarrello.elements[i].id).className = '';
					}
				}
		}
	// 26 Gennaio 2009 - Modifica obbligatoriet�
	// Controlliamo se � stata specificata la partita IVA per le aziende
	if (document.getElementById('id_AZIENDA').value != '' && document.getElementById('id_PARTITA_IVA').value == '')
		{
		alert('per azienda serve partita IVA');
		}

	// Check sull'accettazione delle condizioni
	if (document.getElementById('accetto').checked == '')
		{
		alert('E\' necessario accettare le condizioni contrattuali prima di procedere');
		moduloOk = false;
		}


	if (moduloOk)
		moduloCarrello.submit();
}


function aggiungiAlCarrello()
{
	moduloCarrello = document.getElementById('carrello');

	var moduloOk = true;

	for (i = 0; i < moduloCarrello.elements.length; i++)
	{
	if ((moduloCarrello.elements[i].alt != '') && (moduloCarrello.elements[i].alt != undefined))
		{
			var altDaControllare = moduloCarrello.elements[i].alt.split(',');

			switch (altDaControllare[0])
			{
				case 'domain':
				if  (!(moduloCarrello.elements[i].value.match(/^[A-Za-z0-9\-]{3,}$/)))
					{
					moduloOk = false;
					document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = 'lblErrore';
					}
				else
					document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = '';

				break;

				case 'check':
				if (!(moduloCarrello.elements[i].checked))
					{
					document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = 'lblErrore';
					moduloOk = false;
					}
				else
					document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = '';
				break;

				case 'numero':
				var minimo = altDaControllare[1];
				var massimo = altDaControllare[2];
				if (moduloCarrello.elements[i].value != '')
					{
					if ((moduloCarrello.elements[i].value < minimo) || (moduloCarrello.elements[i].value > massimo))
						{
						moduloOk = false;
						document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = 'lblErrore';
						}
					else
						document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = '';
					}
				else
					{
					document.getElementById('lbl_' + moduloCarrello.elements[i].id).className = '';
					}

				break;

				case 'radio':
				radios = document.getElementsByName(moduloCarrello.elements[i].name);
				var unaSelezionata = false;
				for (var c = 0; c < radios.length; c++)
					{
					if (radios[c].checked)
						unaSelezionata = true;
					}

				if (!(unaSelezionata))
					{
					for (var c = 0; c < radios.length; c++)
						{
						document.getElementById('lbl_' + radios[c].id).className = 'lblErrore';
						}
					}
				else
					{
					for (var c = 0; c < radios.length; c++)
						{
						document.getElementById('lbl_' + radios[c].id).className = '';
						}

					}
				break;
			}

		}

	}

	if(!(moduloOk))
		{
		document.getElementById('errore_modulo').innerHTML = 'Ci sono alcuni errori che &egrave; necessario correggere. <br/>Controlla i campi in rosso!';
		document.getElementById('errore_modulo').style.display = 'block';
		}
	else
		{
		document.getElementById('errore_modulo').style.display = 'none';
		if (confirm('Vuoi procedere e aggiornare il carrello?'))
			{
			moduloCarrello.submit();
			}
		}

;
}

function validaModuloOrdine( modulo )
{
var moduloOk = true;

for (i = 0; i < modulo.elements.length; i++)
	{
	// Solo i campi aggiuntivi sono obbligatori e solo se hanno un alt specificato
	if ((modulo.elements[i].alt != '') && (modulo.elements[i].alt != undefined))
		{
			switch (modulo.elements[i].alt)
			{
				case 'domain':
					if (!(dominioValido(modulo.elements[i].value)))
						{
						modulo.elements[i].className='Errore';
						moduloOk = false;
						}
					else
						{
						modulo.elements[i].className='';
						}
					break;

				default:
				alert('dato sconosciuto...');
				moduloOk = false;
				break;
			}

		}
	}
return moduloOk;
}

function modificaCambioRegistrar(posizione, val)
{
	new Ajax.Updater('contenutoCarrello', '/shop.php',
		{
			method: 'get',
			parameters: {action: 'modificaCambioRegistrar', oggetto: posizione, valore: val}
		}
		);
}

function togliCarrello(posizione)
{
	if (confirm('Attenzione: tutti gli eventuali prodotti in special associati al prodotto verranno eliminati! Vuoi continuare?'))
		new Ajax.Updater('contenutoCarrello', '/shop.php',
			{
				method: 'get',
				parameters: {action: 'elimina', oggetto: posizione}
			}
			);
}

function ricalcolaTotaliTemporaneo()
{
	var totale = 0;
	for (i = 0; i < document.forms.riepilogoAggiunte.elements.length; i++)
		{
			var el = document.forms.riepilogoAggiunte.elements[i];
			if (el.id.match(/prezzo/))
				{
				totale += (el.value * 1.00);
				}

		}
	document.getElementById('totaleOrdine').value = totale;
}

function verificaDominio( dominio, tld, CategoriaProdotto, codiceProdotto )
{
	var nomeDominio = trim(document.getElementById(dominio). value);
	var tldScelto = document.getElementById(tld);

	// Elimina l'eventuale tld messo qui!'
	nomeDominio = nomeDominio.replace(/\..*/, '');
	document.getElementById(dominio). value = nomeDominio;	
	
	tld = tldScelto.options[tldScelto.selectedIndex].text;

	nomeDominio = nomeDominio + tld;
	if (nomeDominio.match(/^[A-Za-z0-9\-]{3,}\.([A-Za-z\.]{2,})$/))
	{
		document.getElementById('errore_modulo').style.display = 'none';

		// Procediamo
		document.getElementById('pulsanteVerifica').innerHTML = '<img src="/images/whoisworking.gif" alt="attendere, prego"/> Verifica in corso, attendere...';
		new Ajax.Request('/whois.php',
		  {
		    method:'get',
		   parameters: {domain: nomeDominio, cat: CategoriaProdotto, id: codiceProdotto},
		    onSuccess: whoisOk,

		    onFailure: function(){ alert('Impossibile eseguire il whois...') }
		  });

		document.getElementById('inserisci').disabled = '';
		document.getElementById('inserisci').innerHTML=	'<img src="/images/continua.png" alt="Metti nel carrello" />';
		document.getElementById('id_nomeDominioEsteso').value = nomeDominio;
		return true;
	}
	else
	{
		document.getElementById('errore_modulo').innerHTML='Il valore ' + nomeDominio + ' non &egrave; un nome di dominio valido';
		document.getElementById('errore_modulo').style.display = 'block';
		return false;
	}
}


function whoisOk( transport )
{
      var response = transport.responseText || "";

	var risultato = response.split('|');

      // alert("Success! \n\n" + response);
      if (risultato[0] == 'Ok')
      	{
      	document.getElementById('risultatoWhois').className = 'dominioDisponibile';
      	document.getElementById('risultatoWhois').innerHTML = 'Complimenti! Il dominio che hai scelto &egrave; disponibile!';
      	if (document.getElementById('cosaFareDominio') != null)
	      	document.getElementById('cosaFareDominio').innerHTML = '';
      	}
      else
      	{
      	document.getElementById('risultatoWhois').className = 'dominioNonDisponibile';
      	document.getElementById('risultatoWhois').innerHTML = 'Siamo spiacenti ma il dominio che hai indicato &egrave; gi&agrave; registrato. <br/><br/><a onclick="mnRis()">Verifica i dati dell\'intestatario</a><br/><br/> <b>Se il dominio &egrave; intestato a te, scegli in fondo al modulo una delle opzioni.</b><br/><br/><div id="id_outputWhois" style="display:none" class="outputWhois">' + risultato[3] + '</div>';
      	if (document.getElementById('cosaFareDominio') != null)
	      	document.getElementById('cosaFareDominio').innerHTML = '<b>Il dominio che hai scelto &egrave; gi&agrave; registrato</b><br/><br/><p>&Egrave; quindi necessario che tu confermi che tale dominio ti appartiene e che decida se desideri trasferirlo a noi oppure lasciarlo in carico al fornitore attuale.<br/><br/></p><input style="width:15px; height:15px"  alt="check" name="confermoTitolaritaDominio" id="id_confermoTitolaritaDominio" type="checkbox" />&nbsp;<label for="id_confermoTitolaritaDominio" id="lbl_id_confermoTitolaritaDominio">Confermo che il dominio &egrave; intestato a me oppure a societ&agrave; da me controllata.</label><br/><br/> Desidero inoltre:<br/><br/>	<input style="width:15px; height:15px" alt="radio" id="id_cambioRegistrar_1" name="cambioRegistrar" value="Si" type="radio" /><label for="id_cambioRegistrar_1" id="lbl_id_cambioRegistrar_1">Che il dominio venga trasferito al registrar di BluePine Technology S.n.c.</label><br/><div id="divMantieneRegistrar"><br/><input style="width:15px; height:15px" id="id_cambioRegistrar_2" name="cambioRegistrar" value="No" type="radio" /><label for="id_cambioRegistrar_2" id="lbl_id_cambioRegistrar_2">Lasciare il mio dominio in carico all\'attuale registrar, mi occuper&ograve; personalmente di effettuare tutte le modifiche necessarie ai nameserver.</div>';
      	}

	var tldScelto = document.getElementById('id_nomeDominio_tld');

	tld = tldScelto.options[tldScelto.selectedIndex].text;

	new Ajax.Request('/shop.php',
		{
		method: 'get',
		parameters: {action:'cercaspecialdomini', tld:tld, cat: risultato[2], id: risultato[1]},
		onSuccess:riempiSpecialDomini
		}
		);

	document.getElementById('pulsanteVerifica').innerHTML = '<button type="button" style="border:none; background-color: #DFF9D9" onclick="verificaDominio(\'id_nomeDominio\' , \'id_nomeDominio_tld\', \'' + risultato[2] + ' \', \'' + risultato[1] + '\' )"><img src="/images/verifica_dominio.png" alt="Verifica Dominio" /></button>';

}

function visualizzaMantieneRegistrar(valore)
{
	if (valore)
		{
			document.getElementById('divMantieneRegistrar').style.display = 'none';
			document.getElementById('id_cambioRegistrar_1').checked = 'checked';
		}
	else
			document.getElementById('divMantieneRegistrar').style.display = 'block';
}

function riempiSpecialDomini( transport )
{
      var response = transport.responseText || "";
      document.getElementById('domain_special').innerHTML = response;

      if (response != '')
      	{
			document.getElementById('divMantieneRegistrar').style.display = 'none';
			document.getElementById('id_cambioRegistrar_1').checked = 'checked';
      	}

}

function mnRis()
{
	if (document.getElementById('id_outputWhois').style.display == 'block')
		document.getElementById('id_outputWhois').style.display = 'none';
	else
		document.getElementById('id_outputWhois').style.display = 'block';

}

function validaForm()
	{
	Invia = 1;

	if(document.getElementById("oggetto").selectedIndex == 0)
		{
		document.getElementById("oggetto").className = "Errore";
		Invia = 0;
		}
	else
		document.getElementById("oggetto").className = "";

	if(document.getElementById("nome").value == "")
		{
		document.getElementById("nome").className = "Errore";
		Invia = 0;
		}
	else
		document.getElementById("nome").className = "";

	if ( (document.getElementById("email").value == "") || (!document.getElementById('email').value.match(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/)) )
		{
		document.getElementById("email").className = "Errore";
		Invia = 0;
		}
	else
		document.getElementById("email").className = "";

	if(document.getElementById("telefono").value == "")
		{
		document.getElementById("telefono").className = "Errore";
		Invia = 0;
		}
	else
		document.getElementById("telefono").className = "";

	if(document.getElementById("testo").value == "")
		{
		document.getElementById("testo").className = "Errore";
		Invia = 0;
		}
	else
		document.getElementById("testo").className = "";


	if(Invia == 1)
		document.getElementById("modulo_contatti").submit();
	else
		{
		document.getElementById("campo_verifica").style.display = 'block';
		document.getElementById("campo_verifica").innerHTML = "I campi in rosso sono obbligatori.";
		}

	}

function trim(stringa)
{
	while (stringa.substring(0,1) == ' ')
	{
	stringa = stringa.substring(1, stringa.length);
	}
	while (stringa.substring(stringa.length-1, stringa.length) == ' ')
	{
	stringa = stringa.substring(0,stringa.length-1);
	}
	return stringa;
}

function scegliTemplate ( numero )
{
	if (confirm('Confermi la scelta del template n. ' + numero + '?'))
		{
			window.location='/?action=acquista_soluzione&w=' + numero;
		}
}

function dedicatiCalcolaTotali(idDedicato)
{
	if (document.getElementById('SMS').checked)
		fSMS = document.getElementById('SMS').value;
	else
		fSMS = '';
			
	if (document.getElementById('Backup').checked)
		fBackup = document.getElementById('Backup').value;
	else
		fBackup = '';

	if (document.getElementById('CPanel').checked)
		fCPanel = document.getElementById('CPanel').value;
	else
		fCPanel = '';

	var fContratto = document.getElementById('contratto').value;
	var fBanda	= document.getElementById('banda').value;
	var fOs = document.getElementById('os').value;
	new Ajax.Request('/server_dedicati.php',
		  {
		   method:'post',
		   parameters: {SMS: fSMS, Backup: fBackup, CPanel : fCPanel, id: idDedicato, Contratto: fContratto, banda: fBanda, os: fOs},
		   onSuccess: function (transport)
			    {
			    		
			    		document.getElementById('PrezzoFinale').innerHTML = transport.responseText;
			    },
		   onFailure: function() { alert('Errore calcolando il prezzo... riprova fra qualche istante!'); }
	    
		  });	

}

function costoAccessorio(idAccessorio, toUpdate)
{
	new Ajax.Request('/server_dedicati.php',
		  {
		   method:'post',
		   parameters: {ida: idAccessorio},
		   onSuccess: function (transport)
			    {			    	
			    			
			    		document.getElementById(toUpdate).innerHTML = transport.responseText;
			    },
		   onFailure: function() { alert('Errore...'); }

		    
		  });	

}

function acquista_dedicato(ItemsInCarrello)
{
	if (ItemsInCarrello > 0)
		if (!(confirm('Il carrello contiene dei prodotti; inserendo il server dedicato questi prodotti verranno rimossi. Vuoi continuare?')))
			return;
	window.location = '/acquista_dedicato.php';
}
