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");
		return;
	}
	else if(!to_mail.match(mailRegEx)) {
		window.alert("Indirizzo Destinatario non valido");
		return;
	}
	else if(!subject) {
		window.alert("Campo oggetto vuoto");
		return;
	}
	else if(!body_text) {
		window.alert("Campo messaggio vuoto");
		return;
	}
	else if(privacyDenied.checked) {
		window.alert("Non hai accettato l'informativa sulla privacy");
		return;
	}
	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();
	}
}

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");
		return;
	}
	else if(!lastname) {
		window.alert("Campo 'Cognome' vuoto");
		return;
	}
	/*else if(!_tnes_username)
		window.alert("Campo 'Nickname' vuoto");
	*/
	
	else if(!email.match(mailRegEx)) {
		window.alert("Indirizzo email non valido");
		return;
	}
	else if(privacyDenied.checked) {
		window.alert("Non hai accettato l'informativa sulla privacy");
		return;
	}
	else{
		document.getElementById("_tnes_username").value = email;
		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");
		return;
	}
	else if(!lastname) {
		window.alert("Campo 'Cognome' vuoto");
		return;
	}
	else if(!password) {
		window.alert("Campo 'Password' vuoto");
		return;
	}
	else if(!password_confirm) {
		window.alert("Campo 'conferma Password' vuoto");
		return;
	}
	else if(password_confirm != password) {
		window.alert("Il Campo Password e Conferma Password non sono Uguali");
		return;
	}
	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);
		return;
	}
	else{
		formEl.submit();
	}
}

