/**
 * @author studio lovotrico.net
 */

function invia_messaggio(){
	if(! controlla_campo('nome',5) ) {
		$('nome').focus();
		alert('Inserire un nome di almeno 5 caratteri.');
		return
	}
	if(! controlla_campo('email',5,true)){
		$('email').focus();
		alert('Inserire un E-mail valido.');
		return;
	}
	var e = $('frm_contatto');
	e.set('send',{
		url: 'contatto.php',
		method: 'post',
		onSuccess: function(x){
			if(x == 1){
				e.reset();
				alert('Messaggio inviato con successo.\nGrazie di averci contattato!\nIl messaggio sara\' evaso nelle prossime 48 ore.');
			} else {
				alert('Impossibile inviare il messaggio.\nProvare pił tardi. Grazie.');
			}
		},
		onFailure: function(){
			alert('Impossibile inviare il messaggio.\nControllare la connessione Internet e riprovare.');
		}
	}).send();
}

function apri_chiudi(e){
	if ($(e).style.display == 'block') 
		$(e).style.display = 'none';
	else 
		$(e).style.display = 'block';
}
function seleziona_utente(id){
	// alert(id);
	var r = new Request({
		onSuccess: function(x){
			oJson = JSON.decode(x);
			$('id').value 	= oJson.id 		|| 0;
			$('nome').value	= oJson.nome 	|| "";
			$('usr').value 	= oJson.usr		|| "";
			$('psw').value 	= oJson.psw		|| "";
			$('email').value= oJson.email	|| "";
			$('indirizzo').value = oJson.indirizzo || "";
			$('telefono').value  = oJson.telefono  || "";
			$('note').value	= oJson.note 	|| "";
			
			$('usr').disabled 	= true;
			$('email').disabled = true;
			
			var livello = $('livello');
			for(i=0;i<livello.length;i++){
				if(livello.options[i].value == oJson.livello){
					livello.options[i].selected = true;
					break;
				}
			}
		},
		onFailure: function(){
			alert('Impossibile eseguire la richiesta');
		},
		onRequest: function(){
			
		},
		onComplete:	function(){
			$('lista-utenti').style.display='none';
		},
		url: 'utenti.php',
		method: 'post'
	}).send('carica-utente=1&id='+id);
}
function checkEmail(valore)
{
  var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
  if (!espressione.test(valore))  return false;
  else  return true;
}
function controlla_campo(element_id,min_length,isEmail){
	var e = $(element_id);
	if (isEmail)return checkEmail(e.value);
	if(e.value.length <  min_length ) return false;
	return true;
}
function controlla_campi_frm_utenti(){
	$('nome-msg').innerHTML	= "";
	$('usr-msg').innerHTML 	= "";
	$('psw-msg').innerHTML 	= "";
	$('email-msg').innerHTML= "";
	
	var l_err = false;
	var e = $('nome');
	if(e.value.length < 5 ){
		$('nome-msg').innerHTML = "Inserire minimo 5 caratteri";
		l_err = 1;
	}
	e = $('usr');
	if(e.value.length < 5 ){
		$('usr-msg').innerHTML = "Inserire minimo 5 caratteri";
		l_err = 1;
	}
	e = $('psw');
	if(e.value.length < 5 ){
		$('psw-msg').innerHTML = "Inserire minimo 5 caratteri";
		l_err = 1;
	}
	e = $('email');
	if(! checkEmail(e.value)) {
		$('email-msg').innerHTML = "Inserire un'indirizzo E-mail valido";
		l_err = 1;
	}
	
	if(!l_err) return true;
	else return false;
	
}
function nuovo_utente(){
	$('id').value 	= 0;
	$('nome').value	= "";
	$('usr').value 	= "";
	$('psw').value 	= "";
	$('email').value= "";
	$('indirizzo').value = "";
	$('telefono').value  = "";
	$('note').value	= "";
	$('livello')[0].selected = true;
	
	$('usr').disabled 	= false;
	$('email').disabled = false;
}
function registra_utente(){
	if(! controlla_campi_frm_utenti()) return;
	
	$('usr').disabled 	= false;
	$('email').disabled = false;
	
	var e = $('frm_utenti');
	e.set('send',{
		url: 'utenti.php',
		method: 'post',
		onSuccess: function(x){
			var oUtente = JSON.decode(x);
			$('id') = oUtente.id;
		},
		onFailure: function(){
			alert('Impossibile salvare');
		},
		onComplete: function(){
			$('progressbar').style.display = 'none';
			$('usr').disabled 	= true;
			$('email').disabled = true;
			$('btn_submit').disabled = false;
			$('frm_utenti').style.visibility = 'visible';
		},
		onRequest: function(){
			$('frm_utenti').style.visibility = 'hidden';
			$('progressbar').style.display = 'inline';
			$('btn_submit').disabled = true;
		}
	}).send();
}
function logout(){
	var r = new Request({
		url: 'login.php',
		method: 'post',
		onSuccess: function(x){
			window.location.reload(true);
		}
	}).send('login=1&u=&p=');
}
function login(){
	var r = new Request({
		url: 'login.php',
		method: 'post',
		onRequest: function(){
			$('frm-login-main').style.display = 'none';
			$('ajax-loader').style.display = 'block';
		},
		onComplete: function(){
			$('frm-login-main').style.display = 'block';
			$('ajax-loader').style.display = 'none';
		},
		onSuccess: function(x){
			var oJson = JSON.decode(x);
			var id = oJson.id || 0;
			if (id == 0) {
				alert('Username e Password non corrispondenti');
			}
			else {
				if ($('redir').value.length > 0) 
					window.location.href = $('redir').value;
				else 
					window.location.href = "index.php";
			}	
		}
	}).send('login=1&u='+$('usr').value+'&p='+$('psw').value);
}
function box_login(){
	var r = new Request({
		url: 'login.php',
		method: 'post',
		onSuccess: function(x){
			var oJson = JSON.decode(x);
			var id = oJson.id || 0;
			if(id == 0)
				alert('Username e Password non corrispondenti');
			else
				window.location.reload(true);
		}
	}).send('login=1&u='+$('username').value+'&p='+$('password').value);
}
