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

function checkSurname(surnameField){
	if (surnameField.value==''){
		clearError(surnameField);
		surnameField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.setAttribute("style","display:inline;color:red");
		err.setAttribute("id","err");
		err.innerHTML="Please insert a Surname";
//seleziono l'elemento successivo a nameField
		surnameField.parentNode.insertBefore(err,surnameField.nextSibling.nextSibling);
		return false;
	}
	else {
		clearError(surnameField);
	}
}

function checkCountry(countryField){
	if (countryField.value==''){
		clearError(countryField);
		countryField.style.borderColor="#FF0000";
		err=document.createElement('div');
		err.setAttribute("style","display:inline;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 checkPrivacy(evnt,privacyField){
		if (!privacyField.checked) {
			alert('You must agree');
			if (evnt.stopPropagation){
				evnt.preventDefault();
				evnt.stopPropagation();
			}
			else {
				window.event.cancelBubble=true;
				window.event.returnValue=false;
			}
		}
	
}


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

	}
}

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.setAttribute("style","display:inline;color:red");
		err.setAttribute("id","err");
		err.innerHTML="Please insert a valid email";
		emailField.parentNode.insertBefore(err,emailField.nextSibling.nextSibling);
		if (e.stopPropagation){
			e.preventDefault();
			e.stopPropagation();
		}
		else {
			window.event.cancelBubble=true;
			window.event.returnValue=false;
		}
	}
}

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 country=document.getElementById("country");
	var privacy=document.getElementById("privacy");

	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);});
	document.forms[0].attachEvent("onsubmit",function(e){return checkPrivacy(e,privacy);});
	document.forms[0].attachEvent("onsubmit",checkMail);
}
	
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 country=document.getElementById("country");
	var privacy=document.getElementById("privacy");
	
	//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);
	document.forms[0].addEventListener("submit",function(e){
		return checkPrivacy(e,privacy);
	},false);
	document.forms[0].addEventListener("submit",checkMail,false);
	
	evnt.preventDefault();
}


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