var mailRegEx = "^[A-Za-z0-9\._-]+@([0-9a-zA-Z][0-9A-Za-z_-]+\.)+[a-z]{2,4}$";

function formCheck(formId){
	var formEl = document.getElementById(formId);
	var from_mail = document.getElementById("from_mail").value;
	var to_mail = document.getElementById("to_mail").value;
	var subject = document.getElementById("subject").value;
	var body_text = document.getElementById("body_text").value;
	var privacyDenied = document.getElementById("privacyDenied");
	
	if(!from_mail.match(mailRegEx))
		window.alert("Indirizzo Mittente non valido");
	
	else if(!to_mail.match(mailRegEx))
		window.alert("Indirizzo Destinatario non valido");
	
	else if(!subject)
		window.alert("Campo oggetto vuoto");
	
	else if(!body_text)
		window.alert("Campo messaggio vuoto");
	
	else if(privacyDenied.checked)
		window.alert("Non hai accettato l'informativa sulla privacy");
		
	else{
		formEl.submit();
	}
}

function checkPost(formId, formAction){
	var formEl = document.getElementById(formId);
	var from_mail = document.getElementById("email").value;
	var author = document.getElementById("author").value;
	var subject = document.getElementById("subject").value;
	
	if(!from_mail.match(mailRegEx)){
		window.alert("Indirizzo Email non valido");
		return;
		
	}else if(!author){
		window.alert("Campo autore vuoto");
		return;
	}else if(!subject){
		window.alert("Campo oggetto vuoto");
		return;
		
	}else{
		formEl.action = formAction;
		formEl.submit();
	}
	return;
}

function formCheckUser(formId){
	var formEl = document.getElementById(formId);
	var firstname = document.getElementById("firstname").value;
	var lastname = document.getElementById("lastname").value;
	var email = document.getElementById("email").value;
	var _tnes_username = document.getElementById("_tnes_username").value;
	var privacyDenied = document.getElementById("privacyDenied");
	
	if(!firstname)
		window.alert("Campo 'Nome' vuoto");
	
	else if(!lastname)
		window.alert("Campo 'Cognome' vuoto");
	
	else if(!_tnes_username)
		window.alert("Campo 'Nickname' vuoto");
	
	else if(!email.match(mailRegEx))
		window.alert("Indirizzo email non valido");
	
	else if(privacyDenied.checked)
		window.alert("Non hai accettato l'informativa sulla privacy");
		
	else{
		formEl.submit();
	}
}

function formCheckModify(formId){
	var formEl = document.getElementById(formId);
	var firstname = document.getElementById("firstname").value;
	var lastname = document.getElementById("lastname").value;
	var password_confirm = document.getElementById("password_confirm").value;
	var password = document.getElementById("password").value;
	var lastname = document.getElementById("lastname").value;
	
	if(!firstname)
		window.alert("Campo 'Nome' vuoto");
	
	else if(!lastname)
		window.alert("Campo 'Cognome' vuoto");
	
	else if(!password)
		window.alert("Campo 'Password' vuoto");

	else if(!password_confirm)
		window.alert("Campo 'conferma Password' vuoto");
	
	else if(password_confirm != password)
		window.alert("Il Campo Password e Conferma Password non sono Uguali");

	else{
		formEl.submit();
	}
		
	
}

//check della Mail
// riceve i parametri formId nome del form e email id del campo su cui eseguire il check
function formCheckMail(formId,email,msgErr){
	var formEl = document.getElementById(formId);
	var mail = document.getElementById(email).value;
	
	if(!mail.match(mailRegEx))
		window.alert(msgErr);
	else{
		formEl.submit();
	}
}

	function changeContextLabel(elemToId, elemFromId, date_to){
		var elemFrom = document.getElementById(elemFromId);
		var elemTo = document.getElementById(elemToId);
		elemTo.value = "demelloforumthread_"+elemFrom.value.replace(/[ ]+/g, '_') + "_" + date_to;
	}
	
	function changeRedirect(elemToId, elemFromId){
		var elemFrom = document.getElementById(elemFromId);
		var elemTo = document.getElementById(elemToId);
		elemTo.value = elemFrom.value;
	}

