/* SOURCE FILE: registration.js */

/* validate user registration */


function check_email(email) {
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (filter.test(email)) {
        return true;
    } else {
        return false;
    }
}


function isDigit(string) {
	 if (! string) return false;
	 var Chars = "0123456789 ";

	 for (var i = 0; i < string.length; i++) {
		 if (Chars.indexOf(string.charAt(i)) == -1)
	  return false;
	 }
	 return true;
}

function check_radio_metodo_pagamento() {
	var ischecked
	for (i=0;i<document.regform.metodo_pagamento.length;i++){
		if (document.regform.metodo_pagamento[i].checked==true){
			ischecked=i
			return true
			break
		}
	}
	return false;
}

function check_radio_metodo_spedizione() {
	var ischecked
	for (i=0;i<document.regform.metodo_spedizione.length;i++){
		if (document.regform.metodo_spedizione[i].checked==true){
			ischecked=i
			return true
			break
		}
	}
	return false;
}


function toggleLayer(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"block";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"block";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"block";
}
}

function toggleLayer2(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
var style2 = document.getElementById(whichLayer).style;
style2.display = style2.display? "":"none";
}
else if (document.all)
{
// this is the way old msie versions work
var style2 = document.all[whichLayer].style;
style2.display = style2.display? "":"none";
}
else if (document.layers)
{
// this is the way nn4 works
var style2 = document.layers[whichLayer].style;
style2.display = style2.display? "":"none";
}
}

function check_is_societa() {
	if(document.regform.is_societa.value=="P") {
		document.regform.piva.disabled=true;
                document.getElementById('id_piva').style.backgroundColor  = "#ccc"; 
		document.regform.n_dipendenti.disabled=true;
                document.getElementById('id_n_dipendenti').style.backgroundColor  = "#ccc"; 
		document.regform.anno_inizio_attivita.disabled=true;
                document.getElementById('id_anno_inizio_attivita').style.backgroundColor  = "#ccc"; 
		document.regform.contatto.disabled=true;
                document.getElementById('id_contatto').style.backgroundColor  = "#ccc"; 
		document.regform.categoria.disabled=true;
                document.getElementById('categoria').style.backgroundColor  = "#ccc"; 
		toggleLayer2('visura');
		return true;
	}
	if(document.regform.is_societa.value=="S") {
		document.regform.piva.disabled=false;
                document.getElementById('id_piva').style.backgroundColor  = "#fff"; 
		document.regform.n_dipendenti.disabled=false;
                document.getElementById('id_n_dipendenti').style.backgroundColor  = "#fff"; 
		document.regform.anno_inizio_attivita.disabled=false;
                document.getElementById('id_anno_inizio_attivita').style.backgroundColor  = "#fff"; 
		document.regform.contatto.disabled=false;
                document.getElementById('id_contatto').style.backgroundColor  = "#fff"; 
		document.regform.categoria.disabled=false;
                document.getElementById('categoria').style.backgroundColor  = "#fff"; 
		toggleLayer('visura');
		return true;
	}
}


function validate()
{

if(document.regform.is_societa.value=="") {
	alert("Devi scegliere tra Privato o Societa'");
	return false;
}
if(document.regform.ragione_sociale.value=="") {
	alert("Nome/Ragione Sociale e' obbligatorio");
	return false;
}
if(document.regform.piva.value=="" && document.regform.cf.value=="") {
	alert("Cod. Fiscale o P.IVA sono obbligatori");
	return false;
}

if(document.regform.is_societa.value=="P") {
	if(document.regform.piva.value=="" && document.regform.cf.value=="") {
		alert("Cod. Fiscale obbligatorio");
		return false;
	}
	if(document.regform.piva.value!="") {
		if(!isDigit(document.regform.piva.value)) {
			alert("P.IVA deve contenere solo numeri");
			return false;
		}
	}
	if( document.regform.cf.value.length  != 16 ) {
			alert("Cod.Fiscale non corretto");
			return false;
	}
}


if(document.regform.is_societa.value=="S") {
	if(document.regform.piva.value=="" || document.regform.cf.value=="") {
		alert("Cod. Fiscale e P.IVA sono obbligatori");
		return false;
	}
	if(document.regform.piva.value!="") {
		if(!isDigit(document.regform.piva.value)) {
			alert("P.IVA deve contenere solo numeri");
			return false;
		}
	}
}

if(document.regform.indirizzo.value=="") {
	alert("Indirizzo e' obbligatorio");
	return false;
}
if(document.regform.cap.value=="") {
	alert("CAP e' obbligatorio");
	return false;
}
if(document.regform.citta.value=="") {
	alert("Citta' e' obbligatorio");
	return false;
}
if(document.regform.provincia.value=="") {
	alert("Provincia e' obbligatorio");
	return false;
}
if(document.regform.cod_paese.value=="") {
	alert("Paese e' obbligatorio");
	return false;
}
if(document.regform.cod_paese.value=="IT" && document.regform.cod_regione.value=="") {
	alert("Regione e' obbligatorio");
	return false;
}
if(!check_email(document.regform.email.value)) {
	alert("Indirizzo email non e' valido");
	return false;
}
if(document.regform.tel.value=="") {
	alert("Telefono e' obbligatorio");
	return false;
}
if(document.regform.password1.value=="") {
	alert("Password mancante");
	return false;
}
if(document.regform.password1.value!=document.regform.password2.value) {
	alert("Password non coincidenti");
	return false;
}

if(!check_radio_metodo_pagamento()) {
	alert('Metodo di pagamento non specificato');
	return false;
}
if(!check_radio_metodo_spedizione()) {
	alert('Metodo di spedizione non specificato');
	return false;
}


if(document.regform.is_societa.value=="S") {
	if(document.regform.categoria.value=="") {
		alert("Categoria non selezionata");
		return false;
	}
	if(document.regform.anno_inizio_attivita.value=="") {
		alert("Anno inizio attivita' e' obbligatorio");
		return false;
	}
	if(document.regform.n_dipendenti.value=="") {
		alert("Numero di dipendenti e' obbligatorio");
		return false;
	}
}

if(!document.regform.consenso.checked) {
	alert("Devi accettare le condizioni di tutela della Privacy");
	return false;
}
if(!document.regform.check_trattamento.checked) {
	alert("Devi accettare le condizioni di trattamento dei dati personali");
	return false;
}
if(!document.regform.check_condizioni.checked) {
	alert("Devi accettare le condizioni di vendita");
	return false;
}

return true;

}
