//<![CDATA[

	// Svuota i campi del form che contengono i codice gomme quando ci si clicca dentro
	function clearTxt(el) {

		if (el.value == 'Codice/Dimensione') {
		
			el.value = '';
		}
	}
	
	// General il codice delle gomme a partire dalla scelta delle select
	function makeCode(value, select1, select2, select3) {
		
		var objLarghezza = document.getElementById(select1)
		var objPercAltezza = document.getElementById(select2)
		var objDiametro = document.getElementById(select3)
				
		document.getElementById(value).value = objLarghezza.value + objPercAltezza.value + objDiametro.value;
	}

	// Switcha tra form moto e form auto
	function switchFormField(el) {

		// Prendo i 2 campi
		var auto = document.getElementById('conteiner_campi_ricerca_auto');
		var marchi_auto = document.getElementById('conteiner_marchi');
		var moto = document.getElementById('conteiner_ricerca_moto');
		var marchi_moto = document.getElementById('conteiner_marchi_moto');
		
		if (el.id == 'pulsante1') {
		
			moto.style.display = 'none';
			auto.style.display = 'block';
			marchi_moto.style.display = 'none' ;
			marchi_auto.style.display = 'block' ;
			document.formRicercaProdottiMoto.reset();
			document.getElementById('tipoFormIdAuto').value = 'si';
			document.getElementById('tipoFormIdMoto').value = 'no';
		}
		else if (el.id == 'pulsante2') {

			auto.style.display = 'none';
			moto.style.display = 'block';
			marchi_moto.style.display = 'block' ;
			marchi_auto.style.display = 'none' ;
			document.ricercaProdotti.reset();
			document.getElementById('tipoFormIdAuto').value = 'no';
			document.getElementById('tipoFormIdMoto').value = 'si';
		}

		return false;
	}

//##########################################################################
//############### GESTORE EVENTI ###########################################

	if (window.addEventListener) {
		window.addEventListener("load", checkEvent, false);
	}
	else if (window.attachEvent) {
		window.attachEvent("onload", checkEvent);
	}
	else {
		window.onload = checkEvent;
	}
	
	function checkEvent() {

		linkMenuAuto = document.getElementById('linkOpenCloseMenu');
		linkMenuMoto = document.getElementById('linkOpenCloseMenuMoto');
		
		// Menu del form auto
		if (linkMenuAuto.addEventListener) {
			linkMenuAuto.addEventListener("click", closeOpenMenuSetup, false);
		}
		else if (linkMenuAuto.attachEvent) {
			linkMenuAuto.attachEvent("onclick", closeOpenMenuSetup);
		}
		else {
			linkMenuAuto.onclick = closeOpenMenuSetup;
		}
		
		// Menu del form moto
		if (linkMenuMoto.addEventListener) {
			linkMenuMoto.addEventListener("click", closeOpenMenuSetup, false);
		}
		else if (linkMenuMoto.attachEvent) {
			linkMenuMoto.attachEvent("onclick", closeOpenMenuSetup);
		}
		else {
			linkMenuMoto.onclick = closeOpenMenuSetup;
		}

		// Blocco il pulsante invio
		/*if (document.addEventListener) {
			document.addEventListener("keydown", bloccaInvio, false);
		}
		else if (document.attachEvent) {
			document.attachEvent("onkeydown", bloccaInvio);
		}
		else {
			document.onkeydown = bloccaInvio;
		}*/
	}

//##########################################################################
//##########################################################################
	
	// Blocco la propagazione del pulsante invio per evitare il submit del form
	/*function bloccaInvio(e) {

		var unicode = e.keyCode ? e.keyCode : e.charCode;

		// Se è stato premuto il tasto "invio"
		if (unicode == 13) {

			// Controllo se si sta verificando un submit dopo la pressione del tanto invio


			document.onsubmit = stopSubmit;
		}
	}
	
	function stopSubmit(evnt) {
		
	
		qta = checkQta('text_scheda_qta');

		// Il campo di cui abbiamo preso il valore ESISTE
		if (qta !== false && qta <= 0) {
		
			alert('Non puoi ordinare ' + qta + ' prodotti');
		
			if (evnt.preventDefault) {
				evnt.preventDefault();
			}
			else if (evnt.cancelBubble != null) {
				evnt.cancelBubble = true;
			}

			return false;
		}
		else {
		
			document.getElementById('formSchedaProdotto').submit();
		}
	}*/

//##########################################################################
//############### GESTORE MENU ALLUNGABILI #################################

	function closeOpenMenuSetup(evnt) {

		if (evnt.preventDefault) {
			evnt.preventDefault();
		}
		else if (evnt.cancelBubble != null) {
			evnt.cancelBubble = true;
		}
	
		if (evnt.target) targ = evnt.target;
		else if (evnt.srcElement) targ = evnt.srcElement;
	
		if (targ.id == 'buttonOpenCloseCategoryAuto') {
		
			id = 'conteiner_lista_categorie_auto';
			contentDiv = document.getElementById(id);
			numEl = document.getElementById('linkOpenCloseMenu').rel;
		}
		else if (targ.id == 'buttonOpenCloseCategoryMoto') {
		
			id = 'conteiner_lista_categorie_moto';
			contentDiv = document.getElementById(id);
			numEl = document.getElementById('linkOpenCloseMenuMoto').rel;
		}

		contentDiv.style.display = 'block';
		
		incremento = new Number(8);
		altezzaRiga = new Number(21);

		maxHeight = new Number(altezzaRiga) * new Number(numEl);
		minHeight = new Number(1);
		
		if (getCurrentHeight(contentDiv) < maxHeight) {
		
			if (targ.id == 'buttonOpenCloseCategoryAuto') {
				document.getElementById('buttonOpenCloseCategoryAuto').src = '../new_images/button_chiudi_categorie.gif';
			}
			else if (targ.id == 'buttonOpenCloseCategoryMoto') {
				document.getElementById('buttonOpenCloseCategoryMoto').src = '../new_images/button_chiudi_categorie.gif';
			}
			
			allunga(maxHeight, incremento, id);
		}
		else {
		
			if (targ.id == 'buttonOpenCloseCategoryAuto') {
				document.getElementById('buttonOpenCloseCategoryAuto').src = '../new_images/burron_show_category.gif';
			}
			else if (targ.id == 'buttonOpenCloseCategoryMoto') {
				document.getElementById('buttonOpenCloseCategoryMoto').src = '../new_images/burron_show_category.gif';
			}
		
			accorcia(minHeight, incremento, id);
		}
		
		return false;
	}
	
	function freezeHeight (contentDiv, maxHeight) {

		contentDiv.style.height = maxHeight + 'px';
	}
	
	function getCurrentHeight(contentDiv) {
	
		var regEx = /[0-9]*/;
		height = contentDiv.style.height.match(regEx);
		
		return height;
	}
	
	function allunga(maxHeight, incremento, id) {

		contentDiv = document.getElementById(id);
		
		contentDiv.style.height = (new Number(getCurrentHeight(contentDiv)) + new Number(incremento)) + 'px';
		
		var currentValue = new Number(getCurrentHeight(contentDiv));
		
		if (currentValue < maxHeight) {
		
			if (currentValue + incremento > maxHeight) {
				
				freezeHeight(contentDiv, maxHeight);
			}
			else {
				setTimeout("allunga(maxHeight, incremento, id)", 10);
			}
		}
		else {
			freezeHeight(contentDiv, maxHeight);
		}
	}
	
	function accorcia(minHeight, incremento, id) {
	
		contentDiv = document.getElementById(id);

		contentDiv.style.height = (new Number(getCurrentHeight(contentDiv)) - new Number(incremento)) + 'px';
		
		var currentValue = new Number(getCurrentHeight(contentDiv));
		
		if (currentValue > minHeight) {
		
			if (currentValue - incremento < minHeight) {
				
				freezeHeight(contentDiv, minHeight);
				contentDiv = document.getElementById(id).style.display = 'none';
			}
			else {
				setTimeout("accorcia(minHeight, incremento, id)", 10);
			}
		}
		else {
			freezeHeight(contentDiv, minHeight);
		}
	}
	
//############### FINE GESTORE MENU ALLUNGABILI ############################
//##########################################################################

	// Esegue il submit della pagina per ordinare i prodotti se la quantità è maggiore di 0
	function validaQta () {
		
		qta = checkQta('text_scheda_qta');
		
		if (qta > 0) {
		
			document.getElementById('formSchedaProdotto').submit();
		}
		else {
		
			alert('Non puoi ordinare ' + qta + ' prodotti');
		}
	}
	
	// Prende la quantità
	function checkQta (fieldId) {
	
		if (document.getElementById(fieldId)) {
		
			obj = document.getElementById(fieldId);
			return new Number(obj.value);
		}
		else {
		
			// Ritorna false SE NON ESISTE il campo di cui si vuole il valore
			return false;
		}
	}

	function validatePagina(nomeForm,numeroPagine) {
			
		if (nomeForm == 'setPagina') {

			inputPagina = document.forms['setPagina'].elements['pagina'].value;
		}

		if (nomeForm == 'setPaginaDown') {
			
			inputPagina = document.forms['setPaginaDown'].elements['pagina'].value;
		}

		if (inputPagina > numeroPagine || inputPagina <= "0" || isNaN(inputPagina) == true)  {
			
			alert('pagina inserita non valida')
			return false;
		}
	}
	
	function cercaGommista() {
	
		var selectProvincia = document.getElementById('provinciaGommista');
		var random = document.getElementById('random_provincia').value;
		
	    if (selectProvincia != null)
	    {
	        idProvincia = selectProvincia.options[selectProvincia.selectedIndex].value;
	        
	        // non devo effettuare la ricerca se non è stata selezionata alcuna provincia
	        if (idProvincia == 106 || idProvincia == -1)
	        {
	            alert('Occorre selezionare una provincia per poter effettuare la ricerca');
	            return;   
	        }
	        window.location.href = 'index.php?pagina=lista_gommisti_club&provincia=' + idProvincia + '&rand='+random;
	    }
	}

	function goToPreventivo()
	{
		document.forms['form_carrello'].action = "../index.php?pagina=preventivo";
		document.forms['form_carrello'].submit();
	}
	
	function validateQta(oggetto)
	{
		id = 'qta' + '_'+oggetto ;
		text = document.getElementById(id) ;
		valore = text.value;
		if (valore <= "0" || isNaN(valore) == true)
		{
			alert('Quantit&grave; inserita non valida') ;
		}
		else
		{
			return true ;
		}
	}
	function validateQtaAll()
	{
		submit = true ;
		
		for(elemento in document.form_carrello)
		{
			if (elemento.indexOf('qta') != -1) {
				
				
				
				
				id = elemento.replace('qta[','');
				id = parseInt(id);
				id = 'qta' + '_'+id ;
				text = document.getElementById(id) ;
				valore = text.value;
				if (valore <= 0 || isNaN(valore) == true)
				{
					submit = false ;
					break;
				}
			}
		}
		
		if(submit == false)
		{
			alert('Il carrello non verra aggiornato') ;
			 document.form_carrello.action = '../index.php?pagina=carrello_utente&aggiorna=no' ;
			 return false ;
		}
	}
	function destinazioneDiversa(prog,email,riferimento) {
	
		window.location.href = "../index.php?pagina=ordine&prog_dest_div="+prog+"&email="+email+"&riferimento="+riferimento;
	}
				
	function checkEmail() {
			
		var email = document.getElementById('emailOrdine');
		   
		 var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		document.forms['form_ordine'].elements['conferma_ordine'].value = '';
		if (!filter.test(email.value)) {
		 
		  alert('Please provide a valid email address');
		  email.focus
		  return false;
		}
   		document.forms['form_ordine'].elements['conferma_ordine'].value = 1;
		document.forms['form_ordine'].action ='../index.php?pagina=ordine&ok=1';
		document.forms['form_ordine'].submit();
	}

	function validaFormStep1() {
			
		piva = ControllaPIVA();
							
		if (checkEmailRegistrazione() && piva === true) {
									
			document.getElementById('formStep1').submit();
		}
		else 
		{

			alert('Inserire una partita iva valida');
		}
	}
					
function checkEmailRegistrazione() {
					
	var email = document.getElementById('mail');
							
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
								
	if (!filter.test(email.value)) {
					
		alert('Inserire un indirizzo mail valido');
		email.focus ;
		return false;
	}
							
	return true;
}
						
function ControllaPIVA() 
{
					
	pi = document.getElementById('piva').value;
					
	if (pi == '') {
							
		return false;
	}
					
	if(pi.length != 11) {
							
		return "La lunghezza della partita IVA non ?\n" +
		"corretta: la partita IVA dovrebbe essere lunga\n" +
		"esattamente 11 caratteri.\n";
	}
					
	validi = "0123456789";
					
	for (i = 0; i < 11; i++) {
								
		if( validi.indexOf( pi.charAt(i) ) == -1 ) {
			return "La partita IVA contiene un carattere non valido `" +
			pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
		}
	}
					
	s = 0;
							
	for( i = 0; i <= 9; i += 2 ) {
						
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	}
					
	for( i = 1; i <= 9; i += 2 ) {
						
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
						
		if( c > 9 ) {
			c = c - 9;
		}
								
		s += c;
	}
					
	if ( ( 10 - s% 10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) ) {
							
		return "La partita IVA non è valida:\n" + "il codice di controllo non corrisponde.\n";
	}
					
	return true;
}

function validaForm(jTipoLogin){

	if(document.forms['FRM_DivCli'].elements['field_15'].value == '')
	{
		alert('La partita iva e\' un campo obbligatorio') ;
		document.forms['FRM_DivCli'].elements['field_15'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['field_27'].value == '')
	{
		alert('Il Codice fiscale dell\'azienda  e\' un campo obbligatorio') ;
		document.forms['FRM_DivCli'].elements['field_27'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['field_25'].value == '')
	{
		alert('I\'email dell\'azienda  e\' un campo obbligatorio') ;
		document.forms['FRM_DivCli'].elements['field_25'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['field_1'].value == '')
	{
		alert('Il nome e\' un campo obbligatorio') ;
		document.forms['FRM_DivCli'].elements['field_1'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['field_2'].value == '')
	{
		alert('Il cognome e\' un campo obbligatorio') ;
		document.forms['FRM_DivCli'].elements['field_2'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['field_17'].value == '')
	{
		alert('La password e\' un campo obbligatorio') ;
		document.forms['FRM_DivCli'].elements['field_17'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['pw2'].value != document.forms['FRM_DivCli'].elements['field_17'].value)
	{
		alert('Le due password immesse non coincidono') ;
		document.forms['FRM_DivCli'].elements['pw2'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['field_19'].value == '')
	{
		alert('E\' necessario immettere la risposta segreta') ;
		document.forms['FRM_DivCli'].elements['field_19'].focus();
		return;
	}
	if(document.forms['FRM_DivCli'].elements['Risposta2'].value != document.forms['FRM_DivCli'].elements['field_19'].value)
	{
		alert('Le due risposte segrete immesse non coincidono') ;
		document.forms['FRM_DivCli'].elements['Risposta2'].focus();
		return;
	}
	
	document.forms['FRM_DivCli'].submit();
}
function CheckDatiObbligatori(azione)
{

    if ((document.getElementById('NomeClienteP2c').value == null) || (document.getElementById('NomeClienteP2c').value == ''))
    {
        alert('Il campo NOME è obbligatorio');
        return false;   
    }
    if ((document.getElementById('cognomeClienteP2C').value == null) || (document.getElementById('cognomeClienteP2C').value == ''))
    {
        alert('Il campo COGNOME è obbligatorio');
        return false;   
    }

	if(document.forms['FRM_CART'].elements['privacy'].checked == false)
	{
		return false;
	}
	
	document.forms['FRM_CART'].submit();
		
}
function isEmail() 
{
	var email = document.getElementById('email');
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if (filter.test(email.value)) 
	{
		return true;
	}
	else
	{
		return false ;
	}
	
}
