﻿var validate={
	
	trim: function(stringa){
		while (stringa.substring(0,1) == ' '){
			stringa = stringa.substring(1, stringa.length);
		}
		while (stringa.substring(stringa.length-1, stringa.length) == ' '){
			stringa = stringa.substring(0,stringa.length-1);
		}
		return stringa;
	},
	
	validateContatti: function(){
		var nome=document.getElementById("nome");
		var societa=document.getElementById("societa");
		var email=document.getElementById("email");
		var testo=document.getElementById("testo");
		var feedback=document.getElementById("feedback");
		var action=document.getElementById("actionForm");
		
		nome.style.borderColor="#000";
		societa.style.borderColor="#000";
		email.style.borderColor="#000";
		testo.style.borderColor="#000";

		
		var errors=0;
		var mailError=0;
		
		Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		
		if(this.trim(nome.value)=="" || this.trim(nome.value)==null || this.trim(nome.value)=="undefined"){
			nome.style.borderColor="#F00";
			errors++;
		}
		if(this.trim(societa.value)=="" || this.trim(societa.value)==null || this.trim(societa.value)=="undefined"){
			societa.style.borderColor="#F00";
			errors++;
		}
		if(this.trim(email.value)=="" || this.trim(email.value)==null || this.trim(email.value)=="undefined"){
			email.style.borderColor="#F00";
			errors++;
		}else{
			if(!Filtro.test(email.value)){
				email.style.borderColor="#F00";
				mailError=1;
			}
		}
		if(this.trim(testo.value)=="" || this.trim(testo.value)==null || this.trim(testo.value)=="undefined"){
			testo.style.borderColor="#F00";
			errors++;
		}
		
		if(errors>0 && mailError==0){
			if(errors==1){
				feedback.innerHTML="<span class=\"red\">Completare il campo evidenziato</span>";
			}else{
				feedback.innerHTML="<span class=\"red\">Completare i campi evidenziati</span>";
			}
		}else if(mailError==1){
			feedback.innerHTML="<span class=\"red\">L'email inserita non è valida</span>";
		}else{
			feedback.innerHTML="Attendere prego...";
			action.submit()
		}		
	}
}

function open_w(title,img,h,w){
 	msg=open('','','width='+(w+10)+',height='+(h+10)+', top='+(screen.height/2-(h/2+10))+', left='+(screen.width/2-(w/2+10))+', toolbar=no,directories=no,menubar=no');
 	msg.document.write('<HEAD><TITLE>'+title+'</TITLE></HEAD><body topmargin=5 leftmargin=5 rightmargin=5 bottommargin=5>');
   	msg.document.write('<center><a href=# onclick=\"window.close();\"><img border=0 src='+img+' alt=\"'+title+'\"></a></center>');
}
