var loginForm;

window.onload = init;

function init() {
	loginForm = document.getElementById('form_reg');
	if (loginForm.privato.checked) {
		loginForm.ragione_sociale.disabled = true;
		loginForm.partita_iva.disabled = true;
	}
	loginForm.onsubmit = function () {
		return canSubmit(this);
	}
	loginForm.e_mail.focus();
}

//Definisce il valore del campo VUOTO
function filled(field) {
	if (field.value == "" || field.value == " " || field.value == null || field.value == 0 || field.value == '00' || field.value == 'anno') {
	//if (field.value == 0 || field.value == null) {
		return false;
	} else {
		return true;
	}
}
	
function canSubmit(form) {
	
	form.e_mail.style.border = '#FFF';
	form.email_conf.style.border = '#FFF';
	form.password.style.border = '#FFF';
	form.conf_pass.style.border = '#FFF';
	form.nome.style.border = '#FFF';
	form.cognome.style.border = '#FFF';
	form.cod_fisc.style.border = '#FFF';
	form.indirizzo.style.border = '#FFF';
	form.ragione_sociale.style.border = '#FFF';
	form.partita_iva.style.border = '#FFF';
	form.civico.style.border = '#FFF';
	form.id_provincia.style.border = '#FFF';
	form.citta.style.border = '#FFF';
	form.cap.style.border = '#FFF';
	form.telefono.style.border = '#FFF';
	form.cellulare.style.border = '#FFF';
	form.privacy.style.border = '#FFF';
	form.privacy2.style.border = '#FFF';
	form.verifica_cod.style.border = '#FFF';
	
	
	
	if (!filled(form.e_mail)) {
		alert("La tua email è obbligatoria.");
		form.e_mail.focus();
		document.getElementById('e_mail').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.email_conf)) {
		alert("E' obbligatorio confermare l'email.");
		form.email_conf.focus();
		document.getElementById('email_conf').style.border = '1px solid #ff9933';
		return false;
	} else if ( !(form.e_mail.value == form.email_conf.value) ){
		alert("Le 2 email non sono uguali");
		form.e_mail.focus();
		document.getElementById('e_mail').style.border = '1px solid #ff9933';
		document.getElementById('email_conf').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.password)) {
		alert("La password è obbligatoria.");
		form.password.focus();
		document.getElementById('password').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.conf_pass)) {
		alert("E' obbligatorio confermare la password.");
		form.conf_pass.focus();
		document.getElementById('conf_pass').style.border = '1px solid #ff9933';
		return false;
	} else if ( !(form.password.value == form.conf_pass.value) ){
		alert("Le 2 password non sono uguali");
		form.password.focus();
		document.getElementById('password').style.border = '1px solid #ff9933';
		document.getElementById('conf_pass').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.nome)) {
		alert("Il tuo Nome è obbligatorio.");
		form.nome.focus();
		document.getElementById('nome').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.cognome)) {
		alert("Il tuo Cognome è obbligatorio.");
		form.cognome.focus();
		document.getElementById('cognome').style.border = '1px solid #ff9933';
		return false;
	}

	for (var i = form.tipologia.length - 1; i > -1; i--) {
		if (form.tipologia[i].checked) { // Se viene selezionato "Azienda"
			var pos = form.tipologia[i].value;
		}
	}
	if (pos && pos=='azienda') {
		if (!filled(form.ragione_sociale)) {
			alert("La Ragione Sociale è obbligatoria.");
			form.ragione_sociale.focus();
			document.getElementById('ragione_sociale').style.border = '1px solid #ff9933';
			return false;
		}
		if (!filled(form.partita_iva)) {
			alert("La P. Iva è obbligatoria.");
			form.partita_iva.focus();
			document.getElementById('partita_iva').style.border = '1px solid #ff9933';
			return false;
		}
		if (!filled(form.cod_fisc)) {
			alert("Il tuo Codice Fiscale è obbligatorio.");
			form.cod_fisc.focus();
			document.getElementById('cod_fisc').style.border = '1px solid #ff9933';
			return false;
		}
	} else if (pos && pos=='privato') {
		// Verifica Cod Fisc
		if (!filled(form.cod_fisc)) {
			alert("Il tuo Codice Fiscale è obbligatorio.");
			form.cod_fisc.focus();
			document.getElementById('cod_fisc').style.border = '1px solid #ff9933';
			return false;
		} else {
			//Verifica codice fiscale
			var cf = document.getElementById('cod_fisc').value;
			var validi, i, s, set1, set2, setpari, setdisp;
			if( cf == '' )  return '';
				cf = cf.toUpperCase();
			if( cf.length != 16 ){
				alert("La lunghezza del codice fiscale non è corretta");
				document.getElementById('cod_fisc').style.border = '1px solid #ff9933';
				return false;
			}
			validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			for( i = 0; i < 16; i++ ){
				if( validi.indexOf( cf.charAt(i) ) == -1 ){
					alert("Il codice fiscale contiene un carattere non valido: " + cf.charAt(i));
					document.getElementById('cod_fisc').style.border = '1px solid #ff9933';
					return false;
				}
			}
			set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
			setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
			s = 0;
			for( i = 1; i <= 13; i += 2 )
				s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
			for( i = 0; i <= 14; i += 2 )
				s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
			if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ){
				alert("Il codice fiscale non è corretto");
				document.getElementById('cod_fisc').style.border = '1px solid #ff9933';
				return false;
			}
		}		
	} else {
		alert("Devi selezionare la tipologia di registrazione (privato/azienda).");
		form.privato.focus();
		document.getElementById('privato').style.border = '1px solid #ff9933';
		return false;
	}

	if (!filled(form.indirizzo)) {
		alert("Il tuo Indirizzo di residenza è obbligatorio.");
		form.indirizzo.focus();
		document.getElementById('indirizzo').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.civico)) {
		alert("Il numero civico è obbligatorio.");
		form.civico.focus();
		document.getElementById('civico').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.id_provincia)) {
		alert("La Provincia di residenza è obbligatoria.");
		form.id_provincia.focus();
		document.getElementById('id_provincia').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.citta)) {
		alert("La tua Città di residenza è obbligatoria.");
		form.citta.focus();
		document.getElementById('citta').style.border = '1px solid #ff9933';
		return false;
	}
	if (form.cap && !filled(form.cap)) {
		alert("Il c.a.p. è obbligatorio.");
		form.cap.focus();
		document.getElementById('cap').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.telefono) & !filled(form.cellulare)) {
		alert("Almeno un numero di telefono è obbligatorio.");
		form.telefono.focus();
		document.getElementById('telefono').style.border = '1px solid #ff9933';
		document.getElementById('cellulare').style.border = '1px solid #ff9933';
		return false;
	}
	if (!(form.privacy2.checked) ) {
		alert("Devi dare il tuo consenso al trattamento dei tuoi dati personali.");
		form.privacy2.focus();
		document.getElementById('privacy2').style.border = '1px solid #ff9933';
		return false;
	}
	if (!(form.privacy.checked) ) {
		alert("Devi leggere ed accettare le nostre condizioni generali.");
		form.privacy.focus();
		document.getElementById('privacy').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.verifica_cod)) {
		alert("Non hai inserito il codice di verifica antirobot in basso nella pagina.");
		form.verifica_cod.focus();
		document.getElementById('verifica_cod').style.border = '1px solid #ff9933';
		return false;
	} else {
		codVer = document.getElementById('verifica_cod');
		c1 = document.getElementById('code').value;
		c2 = document.getElementById('code2').value;
		if (codVer.value != c2 + "R2m" + c1){
			alert("Il codice di verifica antirobot in basso nella pagina è errato.");
			form.verifica_cod.focus();
			codVer.style.border = '1px solid #ff9933';
			return false;
		}
	}

	return true;
}