// JavaScript Document
//for form
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 isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function createnode(arc,text){
	newimg=document.createElement('span');
	newimg.className="controllo-form";
	newimg.innerHTML='<em>'+text+'</em>';
	arc.appendChild(newimg);	
}
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);
	}
  	reqfields=document.getElementById('required').value.split(',');
	for(i=0;i<reqfields.length;i++){
		f=document.getElementById(reqfields[i]);
		if(reqfields[i]=="email" && !isValidEmail(f.value)){
			arc=f.parentNode.childNodes[0];
			createnode(arc,'Email non Valida');
			err++;
		}else{
			if(f.value==""){
				arc=f.parentNode.childNodes[0];
				createnode(arc,'Campo Necessario');
				err++;
			}
		}
	}
	if(err>0){
		return false;	
	}
}
function associa(){
	var els = document.all?document.all:document.getElementsByTagName("form");
    for(var i=0,j=els.length; i<j; i++){
		asc=els[i];
		//alert(asc.TagName);
		if(asc.getAttribute('id')=="cform"){
			asc.onsubmit= function () {return controllo(this);}	
		}
	}
}
function associa_2(){
	if(document.getElementById('search')){
		document.getElementById('search').value="ricerca..";
		document.getElementById('search').onfocus= function () {
			if(document.getElementById('search').value=="ricerca.."){
				document.getElementById('search').value="";
			}
		}	
	}
}
window.onload=function(){
	associa();
	associa_2();
}