//  ###############################
//	==>>  DIV INFO
//  ###############################
function MostraLayer(nomelayer)
{
	document.getElementById(nomelayer).style.visibility = 'visible';
	document.getElementById(nomelayer).style.display = 'block';
}

function NascondiLayer(nomelayer)
{
	document.getElementById(nomelayer).style.visibility = 'hidden';
	document.getElementById(nomelayer).style.display = 'none';
}

function ControllaCampoObbligatorio (stringa)
{
	if (stringa == '' || stringa.replace(/\s*/gi,'') == '' || stringa.replace(/\x13\x10*/gi,'') == '' )
		return false;
	else
		return true;
}

function verEmail(stringa)
{
   if (stringa.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
   else
		return false;
}

function ControllaForm(f, lingua)
{
	var controlla;
	var msgErrore = '';
	
	switch(lingua)
	{
		case 'ita':
			var tit_msg_errore = "Si sono verificati i seguenti errori:\n\n";
			var msg_errore1 = ": è obbligatorio e non può essere vuoto.\n";
			var msg_errore2 = ": inserire un indirizzo email valido.\n";
			break;
		case 'eng':
			var tit_msg_errore = "Errors found:\n\n";
			var msg_errore1 = ": is required and can\'t be empty.\n"
			var msg_errore2 = ": insert a valid e-mail address.\n"
			break;
	}
	
	for(var i=0 ; i < f.elements.length ; i++)
	{
		controlla = f.elements[i].name.substring(f.elements[i].name.length-1);
		if (controlla != '0')
		{
			switch (controlla)
			{
				case '1':
					if (!ControllaCampoObbligatorio(f.elements[i].value))
						msgErrore += "- " + FormattaAlert(f.elements[i].name.substring(0,f.elements[i].name.length-2)) + msg_errore1;
					break;
				case '2':
					if (!verEmail(f.elements[i].value))
						msgErrore += "- " + FormattaAlert(f.elements[i].name.substring(0,f.elements[i].name.length-2)) + msg_errore2;
					break;
			}
		}
	}
	
	
	if (msgErrore != '')
	{
		alert(tit_msg_errore + msgErrore);
		return false;
	}
	else
	{
		return true;
	}
}

function FormattaAlert(nome)
{
	var testo = nome;
	testo = testo.replace("§"," ");
	testo = testo.replace("[a]","à");
	testo = testo.replace("[e]","è");
	testo = testo.replace("[i]","ì");
	testo = testo.replace("[o]","ò");
	testo = testo.replace("[u]","ù");

	return testo;
}

function verificaQta(f, elementi_form, lingua)
{
	var trovato_errore = false
	switch(lingua)
	{
		case 'ita':
			var tit_msg_errore = "Si sono verificati i seguenti errori:\n\n";
			var msg_errore1 = "Le quantità sono obbligatorie, non possono essere pari a zero e/o devono essere in formato numerico.";
			break;
		case 'eng':
			var tit_msg_errore = "Errors found:\n\n";
			var msg_errore1 = "Quantity are required, can't be equal to 0 and/or must be in numeric format."
			break;
	}
		
	if (elementi_form > 1)
		for ( j = 0; j < elementi_form; j++ )
			if (isNaN(f.qta[j].value))
			{
				trovato_errore = true;
			}
			else
			{
				if (f.qta[j].value < 1)
				{
					trovato_errore = true;
				}
			}
	else
			if (isNaN(f.qta.value))
			{
					trovato_errore = true;
			}
			else
			{
				if (f.qta.value < 1)
				{
					trovato_errore = true
				}
			}

	if (trovato_errore)
	{
		alert(tit_msg_errore + msg_errore1);
		return false;
	}
	else
	{
		f.submit()
	}
}

function verEmail(stringa) {
   if (stringa.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
   return true;
   else return false;
}

function ControllaDati(f, lingua)
{
	switch(lingua)
	{
		case 'ita':
			var tit_msg_errore = "Si sono verificati i seguenti errori:\n\n";
			var msg_errore1 = "- Username: è obbligatorio e non può essere vuoto.\n";
			var msg_errore2 = "- Password: è obbligatorio e non può essere vuoto.\n";
			var msg_errore3 = "- Password: deve essere di almeno 8 caratteri.\n";
			var msg_errore4 = "- Conferma password: è obbligatorio e non può essere vuoto.\n";
			var msg_errore5 = "- Conferma password: deve essere di almeno 8 caratteri.\n";
			var msg_errore6 = "- La password deve essere ripetuta correttamente.\n";
			var msg_errore7 = "- Nome: è obbligatorio e non può essere vuoto.\n";
			var msg_errore8 = "- Cognome: è obbligatorio e non può essere vuoto.\n";
			var msg_errore8bis = "- Codice fiscale: è obbligatorio e non può essere vuoto.\n";
			var msg_errore9 = "- Indirizzo: è obbligatorio e non può essere vuoto.\n";
			var msg_errore10 = "- Città: è obbligatorio e non può essere vuoto.\n";
			var msg_errore11 = "- CAP: è obbligatorio e non può essere vuoto.\n";
			var msg_errore12 = "- Telefono: è obbligatorio e non può essere vuoto.\n";
			var msg_errore13 = "- e-mail: inserire un indirizzo e-mail valido.\n";
			var msg_errore14 = "- Nazione: è obbligatorio.\n";
			var msg_errore15 = "- E\' necessario selezionare una provincia italiana.\n";
			break;
		case 'eng':
			var tit_msg_errore = "Errors found:\n\n";
			var msg_errore1 = "- Username: is required and can't be empty.\n";
			var msg_errore2 = "- Password: is required and can't be empty.\n";
			var msg_errore3 = "- Password: must be at least 8 characters.\n";
			var msg_errore4 = "- Confirm password: is required and can't be empty.\n";
			var msg_errore5 = "- Confirm password: must be at least 8 characters.\n";
			var msg_errore6 = "- Repeat password correctly.\n";
			var msg_errore7 = "- Name: is required and can't be empty.\n";
			var msg_errore8 = "- Surname: is required and can't be empty.\n";
			var msg_errore8bis = "- Fiscal code: is required and can't be empty.\n";
			var msg_errore9 = "- Address: is required and can't be empty.\n";
			var msg_errore10 = "- City: is required and can't be empty.\n";
			var msg_errore11 = "- ZIP Code: is required and can't be empty.\n";
			var msg_errore12 = "- Phone: is required and can't be empty.\n";
			var msg_errore13 = "- e-mail: insert a valid e-mail address.\n";
			var msg_errore14 = "- Nation: is required.\n";
			var msg_errore15 = "- You must select an italy province.\n";
			break;
	}

	var msgErrore = '';
	var user = f.username.value;
	var pass = f.password.value;
	var pass2 = f.confermapassword.value;
	var nome = f.nome.value;
	var cognome = f.cognome.value;
	var codicefiscale = f.codicefiscale.value;
	var indirizzo = f.indirizzo.value;
	var citta = f.citta.value;
	var cap = f.cap.value;
	var telefono = f.telefono.value;
	var email = f.mail.value;
	var prov = f.provstato;
	var naz = f.nazione;

	if (!ControllaCampoObbligatorio(user))
		msgErrore += msg_errore1;
	if (!ControllaCampoObbligatorio(pass))
		msgErrore += msg_errore2;
	else
	{
		if (pass.length < 8)
			msgErrore += msg_errore3;
	}
	if (!ControllaCampoObbligatorio(pass2))
		msgErrore += msg_errore4;
	else
	{
		if (pass2.length < 8)
			msgErrore += msg_errore5;
	}
	if (pass2 != pass)
		msgErrore += msg_errore6;
	if (!ControllaCampoObbligatorio(nome))
		msgErrore += msg_errore7;
	if (!ControllaCampoObbligatorio(cognome))
		msgErrore += msg_errore8;
	if (!ControllaCampoObbligatorio(codicefiscale))
		msgErrore += msg_errore8bis;
	if (!ControllaCampoObbligatorio(indirizzo))
		msgErrore += msg_errore9;
	if (!ControllaCampoObbligatorio(citta))
		msgErrore += msg_errore10;
	if (!ControllaCampoObbligatorio(cap))
		msgErrore += msg_errore11;
	if (!ControllaCampoObbligatorio(telefono))
		msgErrore += msg_errore12;
	if (!verEmail(email))
		msgErrore += msg_errore13;
	if(naz.options[naz.selectedIndex].value == "0")
		msgErrore += msg_errore14;
	if(naz.options[naz.selectedIndex].value == "3" && prov.options[prov.selectedIndex].value == "0")
		msgErrore += msg_errore15;
	
	if (msgErrore != '')
	{
		alert(tit_msg_errore + msgErrore);
		return false;
	}
	else
	{
		return true;
	}
}

function ControllaRicercaCatalogo(f, lingua)
{
	var msgErrore = '';
	var campolibero = f.k.value;

	switch (lingua)
	{
		case 'ita':
			tit_msg_errore = "Sono stati rilevati i seguenti errori:\n\n";
			msg_errore1 = "- Inserire una parola chiave di almeno 3 caratteri.\n";
			break;
		case 'eng':
			tit_msg_errore = "Errors found:\n\n";
			msg_errore1 = "- Insert a key word more than 3 characters.\n";
			break;
	}
	if (!campolibero.match(/^.{3,}$/))
		msgErrore += msg_errore1;

	if (msgErrore != '')
	{
		alert(tit_msg_errore + msgErrore);
		return false;
	}
	else
	{
		return true;
	}
}