function isEmail(str) {
  // are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}


function CheckForm() {
		f = document.forms[0];
		var sAlert = '';
		
		if (f.elements['txtNome'].value  == '') {
			sAlert+=((sAlert!='')?'\n':'')+'Il campo Nome è obbligatorio';
		};
		if (f.elements['txtCognome'].value  == '') {
			sAlert+=((sAlert!='')?'\n':'')+'Il campo Cognome è obbligatorio';
		};
		
		if (f.elements['txtEmail'].value == '') {
			sAlert+=((sAlert!='')?'\n':'')+'Il campo Email è obbligatorio';
		}else{	
			if (isEmail(f.elements['txtEmail'].value) == false) {
				sAlert+=((sAlert!='')?'\n':'')+'Formato e-mail non valido.';	
			};
		};

		if (f.elements['txtPassword'].value == '') {
			sAlert+=((sAlert!='')?'\n':'')+'Il campo Password è obbligatorio';
		};

		if (!f.elements['checkbox'].checked) {
			sAlert+=((sAlert!='')?'\n':'')+'Deve essere accetto il regolamento';
		};
		if (!f.elements['checkbox2'].checked) {
			sAlert+=((sAlert!='')?'\n':'')+'Deve essere accettata l\'informativa sulla privacy';
		};
		
		
		if (sAlert!='') {
			alert(sAlert);
			return false;
		}else{
			return true;
		};	
}

function CheckEmail() {
		f = document.forms[0];
		var sAlert = '';
		
		if (f.elements['txtEmail'].value == '') {
			sAlert+=((sAlert!='')?'\n':'')+'Il campo Email è obbligatorio';
		}else{	
			if (isEmail(f.elements['txtEmail'].value) == false) {
				sAlert+=((sAlert!='')?'\n':'')+'Formato e-mail non valido.';	
			};
		};		
		
		if (sAlert!='') {
			alert(sAlert);
			return false;
		}else{
			return true;
		};	
}


function SetChecked(val,chkName) {
	dml=document.forms[0];
	len = dml.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++) {
		if (dml.elements[i].name==chkName) {
			dml.elements[i].checked=val;
		}
	}
}

function goToPage(page, operation) {
	document.forms[0].action=page + "?operation=" + operation;
	document.forms[0].submit();
}

function CancUtente(page, IdUtente, Nome, Cognome) {
	var msg = window.confirm("Sei sicuro di voler cancellare utente " + Nome + " " + Cognome + "?");
	if(msg){
		document.forms[0].action=page + "?operation=cancella&IdUtente=" + IdUtente + "&UtentiAbilitati=" + document.forms[0].UtentiAbilitati.value;
		document.forms[0].submit();
	}
}

function ConfirmAbilDis() {
	
	if(document.forms[0].operation.value == "Abilita"){
		operation = "abilitare";
	}else{
		operation = "disabilitare";
	}
	var msg = window.confirm("Sei sicuro di voler " + operation + " utenti?");
	if(msg){
		document.forms[0].action="ListaUser.php?UtentiAbilitati=" + document.forms[0].UtentiAbilitati.value;
		document.forms[0].submit();
	}else{
		document.forms[0].operation.value = '';
	}
}

function ConfirmAbilDisVoti() {
	
	if(document.forms[0].operation.value == "Abilita"){
		operation = "abilitare";
	}else{
		operation = "disabilitare";
	}
	var msg = window.confirm("Sei sicuro di voler " + operation + " utenti?");
	if(msg){
		document.forms[0].action="ListaUser_voti.php?UtentiAbilitati=" + document.forms[0].UtentiAbilitati.value;
		document.forms[0].submit();
	}else{
		document.forms[0].operation.value = '';
	}
}

function VerLogIn(UtenteLoggato, ValVoto, Pagina) {
	if(!UtenteLoggato){
		alert ("Per votare è necessario fare il login.");
	} else {
		location.href = Pagina + "?" + ValVoto;
	}
}

