/**
 * @author busetto
 */
function checkName(e,nameField,submit){

	if (nameField.value==''){
		if (submit){
			alert("Please insert a Name");
			if (e.stopPropagation){
				e.preventDefault();
				e.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			return false;
		}
		clearError(nameField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert a Name";
		nameField.parentNode.insertBefore(err,nameField.nextSibling);
		return false;
	}
	else {
			clearError(nameField);
		}
}


function checkSurname(e,surnameField,submit){
	
	if (surnameField.value==''){
		if (submit){
			alert("Please insert a Surname");
			if (e.stopPropagation){
				e.preventDefault();
				e.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			return false;
		}
		clearError(surnameField);
		surnameField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert a Surname";
		surnameField.parentNode.insertBefore(err,surnameField.nextSibling);
		return false;
	}
	else {
		clearError(surnameField);
	}
}

function checkAddress(e,addressField,submit){
	
	if (addressField.value==''){
		if (submit){
			alert("Please insert a valid address");
			if (e.stopPropagation){
				e.preventDefault();
				e.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			return false;
		}
		clearError(addressField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert a valid address";
		addressField.parentNode.insertBefore(err,addressField.nextSibling);
		return false;
	}
	else {
			clearError(addressField);
		}
}

function checkAffiliation(e,affiliationField,submit){
	
	if (affiliationField.value==''){
		if (submit){
			alert("Please insert an affiliation");
			if (e.stopPropagation){
				e.preventDefault();
				e.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			return false;
		}
		clearError(affiliationField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert an affiliation";
		affiliationField.parentNode.insertBefore(err,affiliationField.nextSibling);
		return false;
	}
	else {
			clearError(affiliationField);
		}
}


function checkCity(e,cityField,submit){
	
	if (cityField.value==''){
		if (submit){
			alert("Please insert a city");
			if (e.stopPropagation){
				e.preventDefault();
				e.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			return false;
		}
		clearError(cityField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert a city";
		//seleziono l'elemento successivo a nameField
		cityField.parentNode.insertBefore(err,cityField.nextSibling);
		return false;
	}
	else {
			clearError(cityField);
		}
}

function checkCountry(e,countryField,submit){
	
	if (countryField.value==''){
		if (submit){
			alert("Please insert a Country");
			if (e.stopPropagation){
				e.preventDefault();
				e.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			return false;
		}
		clearError(countryField);
		countryField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert a Country";
		countryField.parentNode.insertBefore(err,countryField.nextSibling);
		return false;
	}
	else {
		clearError(countryField);
	}
}

function checkPostcode(postcodeField,submit){
	if (submit){
		alert("Please insert a Postcode");
		return false;
	}
	if (postcodeField.value==''){
		clearError(postcodeField);
		postcodeField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Please insert a Postcode";
		postcodeField.parentNode.insertBefore(err,postcodeField.nextSibling);
		return false;
	}
	/*else if (isNaN(postcodeField.value)){
		clearError(postcodeField);
		postcodeField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML=" Postcode must be a number";
		postcodeField.parentNode.insertBefore(err,postcodeField.nextSibling);
		return false;
	}*/
	else {
		clearError(postcodeField);
	}
}

function checkPrivacy(evnt,privacyField){
		if (!privacyField.checked) {
			alert('You must agree the privacy');
			if (evnt.stopPropagation){
				evnt.preventDefault();
				evnt.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			
		}
	
}

function checkStatutes(evnt,statutesField){
		if (!statutesField.checked) {
			alert('You must agree the Statutes');
			if (evnt.stopPropagation){
				evnt.preventDefault();
				evnt.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
			
		}
	
}

function checkMail(e){
	var pattern = /.+@.+\.\w{2}/;
	var emailField = document.getElementById("email");
	if (emailField.value.match(pattern)) {
		return true;
	}
	else {
		clearError(emailField);
		emailField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML="Please insert a valid email";
		alert("Please insert a valid email");
		emailField.parentNode.insertBefore(err,emailField.nextSibling);
		if (e.stopPropagation){
			e.preventDefault();
			e.stopPropagation();
		}
		else {
			window.event.cancelBubble=true;
			window.event.returnValue=false;
		}
	}
}

function clearError(field){
	if (field.nextSibling){
	var errorDiv=field.nextSibling;
	if (errorDiv){
		var td=errorDiv.parentNode;
		td.removeChild(errorDiv);
		field.style.borderColor="#000000";

	}
	}
}

/*****************************************************************************************
/* Imposto gli hanlder per Internet Explorer
 * 
 *****************************************************************************************/

function setupEventIE(){
	var errorElement=document.getElementById("risultato");
	//seleziono l'elemento contenente input field del nome
	var name=document.getElementById("name");
	//selezioni l'elemento contenente l'input field del cognome
	var surname=document.getElementById("surname");
	var address=document.getElementById("address");
	var city=document.getElementById("city");
	var affiliation=document.getElementById("affiliation");
	var postcode=document.getElementById("postcode");
	var country=document.getElementById("country");
	var privacy=document.getElementById("privacy");
	var statutes=document.getElementById("statutes");
	
	//aggiungo gli eventi ai vari field.Nitare l'uso delle closure
	name.attachEvent("onblur",function(e){return checkName(e,name,false); });
	name.attachEvent("onchange",function(){return clearError(this);});
	surname.attachEvent("onblur",function(e){return checkSurname(e,surname,false);});
	surname.attachEvent("onchange",function(){return clearError(this);});
	country.attachEvent("onblur",function(e){return checkCountry(e,country,false); });
	country.attachEvent("onchange",function(){return clearError(this);});
	address.attachEvent("onblur",function(e){return checkAddress(e,address,false); });
	address.attachEvent("onchange",function(){return clearError(this);});
	city.attachEvent("onblur",function(e){return checkCity(e,city,false); },false);
	city.attachEvent("onchange",function(){return clearError(this);});
	affiliation.attachEvent("onblur",function(e){return checkAffiliation(e,affiliation,false); });
	affiliation.attachEvent("onchange",function(){return clearError(this);});
	//postcode.attachEvent("onblur",function(){return checkPostcode(postcode); });
	//postcode.attachEvent("onchange",function(){return clearError(this);});
	
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkName(e,name,true);
	});
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkSurname(e,surname,true);
	});
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkCountry(e,country,true);
	});
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkAddress(e,address,true);
	});
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkCity(e,city,true);
	});
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkAffiliation(e,affiliation,true);
	});
	document.forms[0].attachEvent("onsubmit",function(e){return checkPrivacy(e,privacy);});
	document.forms[0].attachEvent("onsubmit",function(e){
		return checkStatutes(e,statutes);
	});
	document.forms[0].attachEvent("onsubmit",checkMail);
}

/*****************************************************************************************
/* Imposto gli hanlder per Mozilla/Gecko
 * 
 *****************************************************************************************/
function setupEventMoz(evnt){
	var errorElement=document.getElementById("risultato");
	//seleziono l'elemento contenente input field del nome
	var name=document.getElementById("name");
	//selezioni l'elemento contenente l'input field del cognome
	var surname=document.getElementById("surname");
	var address=document.getElementById("address");
	var city=document.getElementById("city");
	var affiliation=document.getElementById("affiliation");
	var postcode=document.getElementById("postcode");
	var country=document.getElementById("country");
	var privacy=document.getElementById("privacy");
	var statutes=document.getElementById("statutes");
	
	//aggiungo gli eventi ai vari field.Nitare l'uso delle closure
	name.addEventListener("blur",function(e){return checkName(e,name,false); },false);
	name.addEventListener("change",function(){
		return clearError(this);
	},false);
	surname.addEventListener("blur",function(e){return checkSurname(e,surname,false);},false);
	surname.addEventListener("change",function(){
		return clearError(this);
	},false);
	country.addEventListener("blur",function(e){return checkCountry(e,country,false); },false);
	country.addEventListener("change",function(){
		return clearError(this);
	},false);
	address.addEventListener("blur",function(e){return checkAddress(e,address,false); },false);
	address.addEventListener("change",function(){
		return clearError(this);
	},false);
	city.addEventListener("blur",function(e){return checkCity(e,city,false); },false);
	city.addEventListener("change",function(){
		return clearError(this);
	},false);
	affiliation.addEventListener("blur",function(e){return checkAffiliation(e,affiliation,false); },false);
	affiliation.addEventListener("change",function(){
		return clearError(this);
	},false);
	/*postcode.addEventListener("blur",function(){return checkPostcode(postcode); },false);
	postcode.addEventListener("change",function(){
		return clearError(this);
	},false);*/
	document.forms[0].addEventListener("submit",function(e){
		return checkName(e,name,true);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkSurname(e,surname,true);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkCountry(e,country,true);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkAddress(e,address,true);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkCity(e,city,true);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkAffiliation(e,affiliation,true);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkPrivacy(e,privacy);
	},false);
	document.forms[0].addEventListener("submit",function(e){
		return checkStatutes(e,statutes);
	},false);
	document.forms[0].addEventListener("submit",checkMail,false);
	
	evnt.preventDefault();
}

/******Selezione del setup in base al browser**********/

if (window.attachEvent){
	window.attachEvent("onload",setupEventIE);
}
else {
	window.addEventListener("load",setupEventMoz,false);
}