
//cerca classi
function getElementsByClassName(classname){
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = document.all?document.all:document.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}
function createnode(arc){
	newimg=document.createElement('span');
	newimg.className="controllo-form";
	newimg.innerHTML='<strong>Campo Necessario !</strong>';
	arc.appendChild(newimg);	
}
function checkMail(x){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)){
		return true;
	}else{
		return false;
	}
}
function controllo(gg){
	err=0;
	chan=getElementsByClassName("controllo-form");
	for(z=1;z<=chan.length;z++){
		node=chan[z-1];
		removed=node.parentNode.removeChild(node);
	}
	merr="";
  	reqfields=gg.required.value.split(',');
	for(i=0;i<reqfields.length;i++){
		if(document.getElementById(reqfields[i])){
			f=document.getElementById(reqfields[i]);
			if(reqfields[i]=="privacy-c"){
				if(f.checked==false){
					arc="Acconsenti al trattamento dei dati personali";
					merr+="\n"+arc;
					err++;
				}	
			}else if(reqfields[i]=="email" && f.value!=""){
				if(!checkMail(f.value)){
					merr+="\nE-mail non valida";
					err++;
				}
			}else{
				if(f.value=="" || (f.type=="checkbox" && f.checked==false)){
					arc=f.parentNode.childNodes[0].innerHTML;
					merr+="\n"+arc.replace("*","");
					err++;
				}
			}
		}
	}
	if(err>0){
		alert("Compilare i seguenti campi:\n-------------------------------"+merr);
		return false;	
	}
}

function associa(){
	var els = document.all?document.all:document.getElementsByTagName("form");
    for(var i=0;i<els.length; i++){
		asc=els[i];
		if(asc.className=="cform"){
			asc.onsubmit= function () {return controllo(this);}	
		}
	}
}
function credits(){
	if(document.getElementById("credits")){
		obj=document.getElementById("credits");
		obj.onclick=function(){window.open(this.href);return false;	}
	}
}
window.onload=function(){
	associa();
	showmaps();	
	credits();
}