// ATTENDO CHE IL DOCUMENTO VENGA COMPLETAMENTE CARICATO
document.observe("dom:loaded", function() {

	var emailReg = new RegExp("^[a-z0-9]([a-z0-9\.\-_]*)\@([a-z0-9\-_]+)[\.]([a-z]{2,6})$");

	var telReg = new RegExp("^([0-9\+\ \-]{6,24})$");

	var capReg = new RegExp("^([0-9]{5})$");

	/*** CONTATTACI ***/
	// SUBMIT
	if($('frm_contattaci')) {

		//var tipologia_altro = false;

		Event.observe('frm_contattaci', 'submit', function(event){

			// NOME
			$('frm_nome').value = $('frm_nome').value.strip();
			if($('frm_nome').value.blank()){
				alert('Inserire il nome!');
				$('frm_nome').focus();
				Event.stop(event);
				return;
			}

			// COGNOME
			$('frm_cognome').value = $('frm_cognome').value.strip();
			if($('frm_cognome').value.blank()){
				alert('Inserire il cognome!');
				$('frm_cognome').focus();
				Event.stop(event);
				return;
			}

			// INDIRIZZO
			$('frm_indirizzo').value = $('frm_indirizzo').value.strip();

			// CITTA
			$('frm_citta').value = $('frm_citta').value.strip();
			if($('frm_citta').value.blank()){
				alert('Inserire la citta\'!');
				$('frm_citta').focus();
				Event.stop(event);
				return;
			}

			// CAP
			$('frm_cap').value = $('frm_cap').value.strip();
			if($('frm_cap').value != ''){
				if (!capReg.test($('frm_cap').value)) {
					alert('Formato CAP non corretto!');
					$('frm_cap').focus();
					Event.stop(event);
					return;
				}//fine if
			}//fine if

			// PROVINCIA
			if($('frm_provincia').value == ""){
				alert('Scegliere una provincia!');
				$('frm_provincia').focus();
				Event.stop(event);
				return;
			}

			// TELEFONO
			$('frm_telefono').value = $('frm_telefono').value.strip();

			if($('frm_telefono').value != ''){
				if (!telReg.test($('frm_telefono').value)) {
					alert('Formato nro telefono non corretto!');
					$('frm_telefono').focus();
					Event.stop(event);
					return;
				}//fine if
			}//fine if

			// EMAIL
			$('frm_email').value = $('frm_email').value.strip();

			if (!emailReg.test($('frm_email').value)) {
				alert('Formato di email non corretto!');
				$('frm_email').focus();
				Event.stop(event);
				return;
			}//fine if

			//MESSAGGIO
			$('frm_mess').value = $('frm_mess').value.strip();
			if($('frm_mess').value.blank()){
				alert('Inserire il messaggio!');
				$('frm_mess').focus();
				Event.stop(event);
				return;
			}

			// PRIVACY
			legge = $$('input:checked[type="radio"][name="frm_legge"]').pluck('value');
			if(legge != 1){
				alert('Devi dare il consenso al Dlgs 196/2003.!');
				//$('frm_legge').focus();
				Event.stop(event);
				return;
			}

			$('frm_operazione').value = 'ok';

		}// fine funzione
		)// fine observe

	}// fine if

	/*** LOCATOR ***/

	if($('frm_locator')) {

		$('frm_cosa').observe('change', function(){
			if($('frm_cosa').value=='Altro'){
				$('modx').update('<br/><input type="text" id="frm_altracosa" name="frm_altracosa" value="" class="form_stile_input" />');
			}
			else{
				$('modx').update('');
			}});

			Event.observe('frm_locator', 'submit', function(event){

				// NOME
				$('frm_nome').value = $('frm_nome').value.strip();
				if($('frm_nome').value.blank()){
					alert('Inserire il nome!');
					$('frm_nome').focus();
					Event.stop(event);
					return;
				}//fine if

				// COGNOME
				$('frm_cognome').value = $('frm_cognome').value.strip();
				if($('frm_cognome').value.blank()){
					alert('Inserire il cognome!');
					$('frm_cognome').focus();
					Event.stop(event);
					return;
				}

				// EMAIL
				$('frm_email').value = $('frm_email').value.strip();
				if (!emailReg.test($('frm_email').value)) {
					alert('Formato della tua email non corretto!');
					$('frm_email').focus();
					Event.stop(event);
					return;
				}//fine if

				// PROVINCIA
				if($('frm_provincia').value == ""){
					alert('Scegliere una provincia!');
					$('frm_provincia').focus();
					Event.stop(event);
					return;
				}

				// CITTA
				$('frm_citta').value = $('frm_citta').value.strip();
				if($('frm_citta').value.blank()){
					alert('Inserire la citta\'!');
					$('frm_citta').focus();
					Event.stop(event);
					return;
				}

				// prodotto
				if($('frm_cosa').value == ""){
				alert('Scegliere un prodotto!');
				$('frm_cosa').focus();
				Event.stop(event);
				return;
				}

				// PRIVACY
				legge = $$('input:checked[type="radio"][name="frm_legge"]').pluck('value');
				if(legge != 1){
					alert('Devi dare il consenso al Dlgs 196/2003.!');
					//$('frm_legge').focus();
					Event.stop(event);
					return;
				}

				$('frm_operazione').value = 'ok';

			}// fine funzione
			)// fine observe

	}// fine if

});