jQuery(document).ready(function() {  
	jQuery('#overlayer').click(function() { 
	chiudiPopup();
	})
});

function controllaform(){
	f = document.forms[0];
	
	if (f.nome.value=='' || f.nome.value==null) {
		alert('Per favore, inserisca un nome valido.');
		f.nome.value= '';
		f.nome.focus();  
		return false;
	}
	else if (f.cognome.value=='' || f.cognome.value==null) {
		alert('Per favore, inserisca un cognome valido.');
		f.cognome.value= '';
		f.cognome.focus();  
		return false;
	}
	else if (f.mail.value=='' || f.mail.value==null) {
		alert('Per favore, inserisca una mail valida.');
		f.mail.value= '';
		f.mail.focus();  
		return false;
	}
	
	else if (echeck(f.mail.value)==false){
		f.mail.value='';
		f.mail.focus();
		return false;
	}
	
	else if (f.descrizione.value=='' || f.descrizione.value==null) {
		alert('Per favore, inserisca una descrizione.');
		f.descrizione.value= '';
		f.descrizione.focus();  
		return false;
	}
	
	return true;
}



function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   alert("Indirizzo email non valido.");
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Indirizzo email non valido.");
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Indirizzo email non valido.");
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		alert("Indirizzo email non valido.");
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Indirizzo email non valido.");
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		alert("Indirizzo email non valido.");
		return false;
	}		
	if (str.indexOf(" ")!=-1){
		alert("Indirizzo email non valido.");
		return false;
	}
	return true;
}

function  chiudiPopup(){
	jQuery('#overlayer').css('display','none');
	jQuery('#container_popup').css('display','none');
}

function apriCambiaLingua(){
	apriOverlayer();
	jQuery('#container_popup').css('display','inline');
}


function apriOverlayer() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		}
		else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
		
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} 
	else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} 
	else {
		pageWidth = windowWidth;
	}
	document.getElementById('overlayer').style.width = pageWidth+'px';
	document.getElementById('overlayer').style.height = pageHeight+'px';
	document.getElementById('overlayer').style.display = 'block';
}

