//  ###############################
//	==>>  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';
}


//  ###############################
//	==>>  CONTROLLI
//  ###############################
function Controlla(valore, obbligatorio){
	if (obbligatorio){
		if ( (valore == "") || (valore.replace(/\s*/gi,'') == '') || (valore.replace(/\x13\x10*/gi,'') == '') )
			return false;
		else
			return true;
	}else{
		if ( (valore != "") && ( (valore.replace(/\s*/gi,'') == '') || (valore.replace(/\x13\x10*/gi,'') == '') ) )
			return false;
		else
			return true;
	}
}

function ValidaEmail(indirizzo){
   if (indirizzo.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
   else
		return false;
}


//  ###############################
//	==>>  LOGIN
//  ###############################
function CheckLogin(f)
{
	var errMsg = '';
	var user=f.user.value;
	var pass=f.pass.value;
	var livello=f.livello;
	var tmpLivello = 0;
	titolo_errore = "Si sono verificati i seguenti errori:\n\n";
	errore1 = "- Selezionare un tipo di accesso.\n";
	errore2 = "- Username deve essere di 4-8 caratteri alfanumerici (a-z, A-Z, 0-9).\n";
	errore3 = "- Username deve essere di 8 caratteri alfanumerici (a-z, A-Z, 0-9).\n";
	errore4 = "- Password deve essere di 8 caratteri alfanumerici (a-z, A-Z, 0-9).\n";
	
	if(livello.options[livello.selectedIndex].value == "")
		errMsg += errore1;
	else{
		tmpLivello = parseInt(livello.options[livello.selectedIndex].value);
		switch(tmpLivello){
			case 1:
				if (!ValidaAccesso(user, tmpLivello))
					errMsg += errore2;
				break;
			case 2:
				if (!ValidaAccesso(user, tmpLivello))
					errMsg += errore3;
				break;
		}
		if (!ValidaAccesso(pass, 2))
			errMsg += errore4;
	}

	if (errMsg != ''){
		errMsg = titolo_errore + errMsg;
		alert(errMsg);
		return false;
	}
	return true;
}

function ValidaAccesso(valore, tipo)
{
	if(tipo==1){
		if (!valore.match(/^[a-zA-Z0-9]{4,8}$/))
			return false;
		else
			return true;
	}else if (tipo==2){
		if (!valore.match(/^[a-zA-Z0-9]{8}$/))
			return false;
		else
			return true;
	}
}

function CheckRecuperoDati(f)
{
	var errMsg = '';
	var tipodato = f.tipodato;
	var valore = f.valore.value;

	titolo_errore = "Si sono verificati i seguenti errori:\n\n";
	errore1 = "- Username deve essere di 4-8 caratteri alfanumerici (a-z, A-Z, 0-9).\n";
	errore2 = "- Inserire un\'indirizzo e-mail valido.\n";

	if (tipodato[0].checked)
		if (!ValidaAccesso(valore, 1))
			errMsg += errore1;
	if (tipodato[1].checked)
		if (!ValidaEmail(valore))
			errMsg += errore2;

	if (errMsg != ''){
		errMsg = titolo_errore + errMsg;
		alert(errMsg);
		return false;
	}
	return true;
}


//  ###############################
//	==>>  PROFILO
//  ###############################
function Profilo(f)
{
	var errMsg = '';
	var user = f.user.value;
	var pass = f.pass.value;
	var nome = f.nome.value;
	var mail = f.mail.value;
	
	titolo_errore = "Si sono verificati i seguenti errori:\n\n";
	errore1 = "- Username deve essere di 4-8 caratteri alfanumerici (a-z, A-Z, 0-9).\n";
	errore2 = "- Password deve essere di 8 caratteri alfanumerici (a-z, A-Z, 0-9).\n";
	errore3 = "- Nome è obbligatorio e non può essere vuoto.\n";
	errore4 = "- Inserire un\'indirizzo e-mail valido.\n";

	if (!ValidaAccesso(user, 1))
		errMsg += errore1;
	if (!ValidaAccesso(pass, 2))
		errMsg += errore2;
	if (!Controlla(nome, true))
		errMsg += errore3;
	if (!ValidaEmail(mail))
		errMsg += errore4;
		
		
	if (errMsg != ''){
		alert(titolo_errore+errMsg);
		return false;
	}else
		return true;
}


//  ###############################
//	==>>  DATI
//  ###############################
function Dati(f){
	var errMsg = '';
	var ragionesociale = f.ragionesociale.value;
	var indirizzo = f.indirizzo.value;
	var numerocivico = f.numerocivico.value;
	var cap = f.cap.value;
	var citta = f.citta.value;
	var idprovincia = f.idprovincia;
	var telefono = f.telefono.value;
	var fax = f.fax.value;
	var partitaiva = f.partitaiva.value;
	var www = f.www.value;
	var mail = f.mail.value;
	
	titolo_errore = "Si sono verificati i seguenti errori:\n\n";
	errore1 = "- Ragione sociale è obbligatorio e non può essere vuoto.\n";
	errore2 = "- Indirizzo è obbligatorio e non può essere vuoto.\n";
	errore3 = "- Civico è obbligatorio e non può essere vuoto.\n";
	errore4 = "- CAP è obbligatorio e non può essere vuoto.\n";
	errore5 = "- Città è obbligatorio e non può essere vuoto.\n";
	errore6 = "- Provincia è obbligatorio e non può essere vuoto.\n";
	errore7 = "- Telefono è obbligatorio e non può essere vuoto.\n";
	errore8 = "- FAX è obbligatorio e non può essere vuoto.\n";
	errore9 = "- Partita IVA è obbligatorio e non può essere vuoto.\n";
	errore10 = "- Sito web è obbligatorio e non può essere vuoto.\n";
	errore11 = "- Inserire un\'indirizzo e-mail valido.\n";

	if (!Controlla(ragionesociale, true))
		errMsg += errore1;
	if (!Controlla(indirizzo, true))
		errMsg += errore2;
	if (!Controlla(numerocivico, true))
		errMsg += errore3;
	if (!Controlla(cap, true))
		errMsg += errore4;
	if (!Controlla(citta, true))
		errMsg += errore5;
	if (idprovincia.options[idprovincia.selectedIndex].value == "")
		errMsg += errore6;
	if (!Controlla(telefono, true))
		errMsg += errore7;
	if (!Controlla(fax, true))
		errMsg += errore8;
	if (!Controlla(partitaiva, true))
		errMsg += errore9;
	if (!Controlla(www, true))
		errMsg += errore10;
	if (!ValidaEmail(mail))
		errMsg += errore11;
		
	if (errMsg != ''){
		alert(titolo_errore+errMsg);
		return false;
	}else
		return true;
}

function OrdinaRicerca(f, ordine){
	f.filtro_ordine.value = ordine;
	f.submit();
}

function Corso_Elimina(f){
	var corso = f.corso.value;
	
	titolo_errore = "Confermi l\'operazione?\n\n";
	errore1 = "- Eliminazione del corso:\n" + corso +"\n\nL\'accesso web verrà eliminato!\nEventuali file associati verranno eliminati dal server!";

	if (!confirm(titolo_errore + errore1))
		return false;
	else
		f.submit()
}

function Corso_Blocca(f){
	var corso = f.corso.value;
	
	titolo_errore = "Confermi l\'operazione?\n\n";
	errore1 = "- Disattivazione accesso web del corso:\n" + corso;

	if (!confirm(titolo_errore + errore1))
		return false;
	else
		f.submit()
}

function Corso_Sblocca(f){
	var corso = f.corso.value;
	
	titolo_errore = "Confermi l\'operazione?\n\n";
	errore1 = "- Attivazione accesso web del corso:\n" + corso;

	if (!confirm(titolo_errore + errore1))
		return false;
	else
		f.submit()
}

function Corso(f){
	var errMsg = '';
	var nome = f.nome.value;
	var fine_gg = f.fine_gg;
	var fine_mm = f.fine_mm;
	var fine_aaaa = f.fine_aaaa;
	
	titolo_errore = "Confermi l\'operazione?\n\n";
	errore1 = "- Nome è obbligatorio e non può essere vuoto.\n";
	errore2 = "- Inserire una data di termine valida.\n";

	if (!Controlla(nome, true))
		errMsg += errore1;
	if (!VerificaData(fine_gg.options[fine_gg.selectedIndex].value + "/" + fine_mm.options[fine_mm.selectedIndex].value + "/" + fine_aaaa.options[fine_aaaa.selectedIndex].value))
		errMsg += errore2;

	if (errMsg != ''){
		alert(titolo_errore+errMsg);
		return false;
	}else
		return true;
}

function VerificaData(strData){
	var regex = new RegExp("[/-]");
	var data = strData.split(regex);
	var nbJours = new Array('',31,28,31,30,31,30,31,31,30,31,30,31);
	var result = true;
	
	if (data['1'] == "" || data['2'] == "" || data['3'] == "")
		result = false;
	
	if ( data['2']%4 == 0 && data['2']%100 > 0 || data['2']%400 == 0 )
		nbJours['2'] = 29;
	
	if( isNaN(data['2']) )
		result=false;
	
	if ( isNaN(data['1']) || data['1'] > 12 || data['1'] < 1 )
		result=false;
	
	if ( isNaN(data['0']) || data['0'] > nbJours[Math.round(data['1'])] || data['0'] < 1 )
		result=false;
	
	return result;
}