/**
 * @author busetto
 */
function checkName(nameField){
	if (nameField.value==''){
		clearError(nameField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML="Please insert a Name";
		//seleziono l'elemento successivo a nameField
		nameField.parentNode.insertBefore(err,nameField.nextSibling);
		return false;
	}
	else {
			clearError(nameField);
		}
}

function checkInstitute(instituteField){
	if (instituteField.value==''){
		clearError(instituteField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML="Please insert a Name institute";
		//seleziono l'elemento successivo a nameField
		instituteField.parentNode.insertBefore(err,instituteField.nextSibling);
		return false;
	}
	else {
			clearError(instituteField);
		}
}

function checkSurname(surnameField){
	if (surnameField.value==''){
		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(addressField){
	if (addressField.value==''){
		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 checkAcronym(acronymField){
	if (acronymField.value==''){
		clearError(acronymField);
		err=document.createElement('div');
		err.style.display="inline";
		err.style.color="red";
		err.setAttribute("id","err");
		err.innerHTML="Please insert an acronym";
		acronymField.parentNode.insertBefore(err,acronymField.nextSibling);
		return false;
	}
	else {
			clearError(acronymField);
		}
}


function checkCity(cityField){
	if (cityField.value==''){
		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(countryField){
	if (countryField.value==''){
		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.nextSibling);
		return false;
	}
	else {
		clearError(countryField);
	}
}

function checkPostcode(postcodeField){
	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');
			evnt.preventDefault();
			evnt.stopPropagation();
		}
	
}

function checkStatutes(evnt,statutesField){
		if (!statutesField.checked) {
			alert('You must agree the Statutes');
			evnt.preventDefault();
			evnt.stopPropagation();
		}
	
}

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 checkUrl(e){
	//var pattern = /http:\/\/.+\..+\..{2}/;
	//var pattern = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	var pattern = /[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3,}\.[A-Za-z]{2,}/;
	
	var urlField = document.getElementById("url");
	if (urlField.value.match(pattern)) {
		return true;
	}
	else {
		clearError(urlField);
		urlField.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 url";
		urlField.parentNode.insertBefore(err,urlField.nextSibling);
		alert("Please insert a valid url");
		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";
		}
	}
}


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 institute=document.getElementById("institute");
	var acronym=document.getElementById("acronym");
	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(){return checkName(name); });
	name.attachEvent("onchange",function(){
		return clearError(this);
	});
	surname.attachEvent("onblur",function(){return checkSurname(surname);});
	surname.attachEvent("onchange",function(){
		return clearError(this);
	});
	country.attachEvent("onblur",function(){return checkCountry(country); });
	country.attachEvent("onchange",function(){
		return clearError(this);
	});
	address.attachEvent("onblur",function(){return checkAddress(address); });
	address.attachEvent("onchange",function(){
		return clearError(this);
	});
	city.attachEvent("onblur",function(){return checkCity(city); });
	city.attachEvent("onchange",function(){
		return clearError(this);
	});
	acronym.attachEvent("onblur",function(){return checkAcronym(acronym); });
	acronym.attachEvent("onchange",function(){
		return clearError(this);
	});
	institute.attachEvent("onblur",function(){return checkInstitute(institute); });
	institute.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 checkPrivacy(e,privacy);
	});
		document.forms[0].attachEvent("onsubmit",function(e){
		return checkStatutes(e,statutes);
	});
	document.forms[0].attachEvent("onsubmit",checkMail);
	document.forms[0].attachEvent("onsubmit",checkUrl);
}

/*****************************************************************************************
/* 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 institute=document.getElementById("institute");
	var acronym=document.getElementById("acronym");
	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(){return checkName(name); },false);
	name.addEventListener("change",function(){
		return clearError(this);
	},false);
	surname.addEventListener("blur",function(){return checkSurname(surname);},false);
	surname.addEventListener("change",function(){
		return clearError(this);
	},false);
	country.addEventListener("blur",function(){return checkCountry(country); },false);
	country.addEventListener("change",function(){
		return clearError(this);
	},false);
	address.addEventListener("blur",function(){return checkAddress(address); },false);
	address.addEventListener("change",function(){
		return clearError(this);
	},false);
	city.addEventListener("blur",function(){return checkCity(city); },false);
	city.addEventListener("change",function(){
		return clearError(this);
	},false);
	acronym.addEventListener("blur",function(){return checkAcronym(acronym); },false);
	acronym.addEventListener("change",function(){
		return clearError(this);
	},false);
	institute.addEventListener("blur",function(){return checkInstitute(institute); },false);
	institute.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 checkPrivacy(e,privacy);
	},false);
		document.forms[0].addEventListener("submit",function(e){
		return checkStatutes(e,statutes);
	},false);
	document.forms[0].addEventListener("submit",checkMail,false);
	document.forms[0].addEventListener("submit",checkUrl,false);
	
	evnt.preventDefault();
}


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

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