window.addEvent('domready', function(){
	
	
	
	if($('invia_accredito')){
		$('invia_accredito').addEvent('click',function(){
			
			var nome = $('nome').value;
			var cognome = $('cognome').value;
			var eta = $('eta').value;
			var id_citta = $('salone_citta').value;
			var sesso = null;
			var prov = $('provincia').value;
			
			for (counter = 0; counter < $('modulo_accredito').sesso.length; counter++){ 
		       if ($('modulo_accredito').sesso[counter].checked){ 
		        	sesso = $('modulo_accredito').sesso[counter].value; 
		       } 
		    } 
			//alert(sesso);
			var citta = $('citta').value;
			var email = $('email').value;
			var cell = $('cell').value;
			var operatore = null;
			for (z = 0; z < $('modulo_accredito').operatore.length; z++){ 
		       if ($('modulo_accredito').operatore[z].checked){ 
		        	operatore = $('modulo_accredito').operatore[z].value; 
		       } 
		    }
		    var data_partecipazione = "0";
			for (t = 0; t < $('modulo_accredito').data_partecipazione.length; t++){ 
		       if ($('modulo_accredito').data_partecipazione[t].checked){ 
		        	data_partecipazione = $('modulo_accredito').data_partecipazione[t].value; 
		       } 
		    } 
			var privacy_int = "0";
			for (y = 0; y < $('modulo_accredito').pubb_interna.length; y++){ 
		       if ($('modulo_accredito').pubb_interna[y].checked){ 
		        	privacy_int = $('modulo_accredito').pubb_interna[y].value; 
		       } 
		    } 
			var privacy_ext = "0";
			for (q = 0; q < $('modulo_accredito').pubb_esterna.length; q++){ 
		       if ($('modulo_accredito').pubb_esterna[q].checked){ 
		        	privacy_ext = $('modulo_accredito').pubb_esterna[q].value; 
		       } 
		    } 
			//alert (privacy_int + ' - ' + privacy_ext);
			var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
			
			if(nome == "" || nome == "undefined"){
				alert('Il campo nome e\' obbligatorio');
				$('nome').focus();
				return false;
			}
			else if(cognome == "" || cognome == "undefined"){
				alert('Il campo cognome e\' obbligatorio');
				$('cognome').focus();
				return false;
			}
			else if(isNaN(eta) || eta == "" || eta == "undefined"){
				alert('Il campo eta\' deve essere un numero positivo');
				$('eta').focus();
				return false;
			}
			else if(citta == "" || citta == "undefined"){
				alert('Il campo citta\' e\' obbligatorio');
				$('citta').focus();
				return false;
			}
			else if(prov == "" || prov == "undefined"){
				alert('Il campo provincia e\' obbligatorio');
				$('provincia').focus();
				return false;
			}
			else if (!email_reg_exp.test(email) || email == undefined || email == "") {
				alert('Inserire una email valida.');
				$('email').focus();
				return false;
			}
            else if(cell == "" || cell == "undefined"){
				alert('Il campo cellulare e\' obbligatorio');
				$('cell').focus();
				return false;
			}
			else if(data_partecipazione == "0"){
				alert('Compilare il campo relativo alla data di partecipazione.');
				return false;
			}
			else if(privacy_int == "0" || privacy_ext == "0"){
				alert('Compilare il campo relativo al trattamento dei dati.');
				return false;
			}
			else{
				var invia_commento = new Request.HTML({	
										url: './sql/memorizza_accredito.php', 
										onComplete: function(tree, elements, html, js) {

											if(html.match('error')){
												
												alert('ATTENZIONE: registrazione fallita, riprovare.');
												alert(html);
											}else{
												window.location.href = "accredito_ok.php?data_partecipazione=" + data_partecipazione + 
												"&nome=" + nome + 
												"&cognome=" + cognome + "&id_citta=" + id_citta;
											}
										}
									});
				invia_commento.post({nome : nome, cognome : cognome, eta : eta, id_citta : id_citta, prov : prov, sesso : sesso, citta : citta, email : email, cell : cell, operatore : operatore, data_partecipazione : data_partecipazione, privacy_int : privacy_int, privacy_ext : privacy_ext});
				return true;
			}
			
		});
		
	}
	
	
});

