var cod;		

function visualizzaControlloCap(){
	visualizzaCampo("cap_captcha");
	visualizzaCampo("cap_testo");
	nascondiCampo("cap_ritesto");
	visualizzaCampo("img_invia_captcha");
	nascondiCampo("img_invia");	
	resetValueCampo("cap_textfieldcaptcha");	
	document.location.href="#capCaptcha";
	focusCampo("cap_textfieldcaptcha");
}

function nascondiControlloCap(){
	nascondiCampo("cap_captcha");
	resetValueCampo("cap_textfieldcaptcha");
	visualizzaCampo("img_invia");
	nascondiCampo("img_invia_captcha");		
}

function visualizzaControlloCapRic(){
	visualizzaCampo("cap_captcha");
	visualizzaCampo("cap_ritesto");
	nascondiCampo("cap_testo");
	visualizzaCampo("img_invia_captcha");
	nascondiCampo("img_invia");	
	resetValueCampo("cap_textfieldcaptcha");	
	document.location.href="#capCaptcha";
	focusCampo("cap_textfieldcaptcha");
}

function visualizzazioneCaptchaCap(){
   stringaDove = eliminaLettere(document.frmRicerca.dove.value)
	  	if(trim2(stringaDove).length <= 0){
			nascondiControlloCap()
		}
}

function controlloTipologiaCap(){
   stringaDove = eliminaLettere(document.frmRicerca.dove.value)
	  	if(trim2(stringaDove).length > 0){	 
			visualizzaControlloCap();
		}else{
	 		inviaRicerca();
	 	}
}

function ControllaRicercaNoCaptcha(){

  if (document.frmRicerca.ricerca.value == "") {
   window.open("/impr/popup_errore.jsp?codice=error_106", "popup_errore", "width=320,height=170,scrollbars=no,toolbar=no,resizable=no,menubar=no,top=200,left=240");
  }else{
   inviaRicerca();
  }

}

function ControllaRicerca() {

  if (document.frmRicerca.ricerca.value == "") {
   window.open("/impr/popup_errore.jsp?codice=error_106", "popup_errore", "width=320,height=170,scrollbars=no,toolbar=no,resizable=no,menubar=no,top=200,left=240");
  }else{
  controlloTipologiaCap();
  }
}

function inviaRicerca(){
document.frmRicerca.submit();
}


function verificaCaptcha(){
	cod=trim2(document.getElementById("cap_textfieldcaptcha").value);	
    dove=document.frmRicerca.dove.value;
    ricerca=document.frmRicerca.ricerca.value;
    if(document.getElementById("tipoRicerca")!=null){
    	tipoRicerca=document.getElementById("tipoRicerca").value;
    	url="/impr/ricerca/capCaptcha.jsp?cap_textfieldcaptcha="+cod+"&ricerca="+ricerca+"&dove="+dove+"&tipoRicerca="+tipoRicerca;
    }else{
		url="/impr/ricerca/capCaptcha.jsp?cap_textfieldcaptcha="+cod+"&ricerca="+ricerca+"&dove="+dove;    
    }
	urlErrore="/impr/index.jsp";	
	new Ajax.Request(url, { 
	method:'get', onSuccess: function(transport){       
	var response = transport.responseText || "no response text";  
        if(response.match("OK")!= null) {		       
			inviaRicerca();
       	}else{
			url = document.location.href;
			var varReloadPezzo1="";
			if(url.indexOf("lista_globale.jsp")!=-1){
				varReloadPezzo1="?ricaricaCap=si&flagDove=true";
			}else{
			    varReloadPezzo1="?ricaricaCap=si&cer=1";
			}
			if(url.indexOf("pagCaptcha.jsp")!=-1){
				url="/impr/ricerca/risultati_globale.jsp";
			}else if(url.indexOf("?")!=-1){
				url = url.substr(0,"?");       	       							
			}else if(url.indexOf("#capCaptcha")!=-1){
				url = url.substr(0,"#capCaptcha"); 				       	       			
			}else{
				url = url.substr(0,url.length-1);			
			}
			var varReloadPezzo2=new String(response);
	   	    document.location.href=url+varReloadPezzo1+varReloadPezzo2;		    
       	}        
    }, 
    onFailure: function(){ document.location.href=urlErrore; } 
  });
 }



function refresh()
{
    window.location.reload( true );
}

function trim2(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;
}

function visualizzaCampo(id){
	document.getElementById(id).style.display="block";
	
}

function resetValueCampo(id){
	document.getElementById(id).value="";
	
}

function focusCampo(id){
	document.getElementById(id).focus();	
	
}
 
function nascondiCampo(id){
	document.getElementById(id).style.display="none";
} 

function addEvent(obj,ev,fn){
	if(obj.addEventListener) {
		// metodo w3c
		obj.addEventListener(ev, fn, false);
	} else if(obj.attachEvent) {
	// metodo IE
		obj.attachEvent('on'+ev, fn);
	} else {
		// se i suddetti metodi non sono applicabili
		// se esiste gia' una funzione richiamata da quel gestore evento
		if(typeof(obj['on'+ev])=='function'){
			alert("5");
	
			// salvo in variabile la funzione gia' associata al gestore
			var f=obj['on'+ev];
			// setto per quel gestore una nuova funzione 
			// che comprende la vecchia e la nuova
			obj['on'+ev]=function(){if(f)f();fn()}
		}
		// altrimenti setto la funzione per il gestore
		else obj['on'+ev]=fn;
	}
}

 function eliminaLettere(_str){ 	
	var str = new String(_str);
	var new_str = new String('');
	for(i=0;i<str.length;i++){
			var a =new String(str.charAt(i));
			if(!isNaN(a)){
				new_str=new_str+a;
			}
	}
	return new_str;
 }


