 function pageSize()
{
 var size = {
  height : 0, width : 0
 }
 ;
 if (document.documentElement && document.documentElement.scrollHeight)
 {
  size.width = document.documentElement.scrollWidth;
  size.height = document.documentElement.scrollHeight;
 }
 else if (document.body) // other Explorers
 {
  size.width = document.body.scrollWidth;
  size.height = document.body.scrollHeight;
 }
 return size;
}
 
function jumpScrollEndOfPage() {
	size = pageSize();
   	window.scroll(0,size.height); // horizontal and vertical scroll targets
}

function caricaTerritorio() {
    var oTextbox = new AutoSuggestControl(document.getElementById("territorio"), new StateSuggestions());        
}

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 controlloTipologiaCap(){
	
	var attivazioneCaptcha = document.form1.attivazioneCaptcha.value;
	if('SI' == attivazioneCaptcha){
	var base = document.form1.baseForm.value;
	if(document.getElementById("controllo")!= null)
 		document.getElementById("controllo").style.display="none";
	if(document.getElementById("controlloMappe")!= null)
 		document.getElementById("controlloMappe").style.display="none";
	// ricerca per cap
	if( (trim(document.getElementById("territorio").value)).length == 5) {
	  	if(!isNaN(document.getElementById("territorio").value)){
			//rigeneraCaptcha(base);
			document.getElementById("cerca3").style.display="none";
			document.getElementById("cerca3captcha").style.display="block";
		}else{
	 		document.getElementById("cerca3captcha").style.display="none";
	 		document.getElementById("cerca3").style.display="block";
	 	}
	}else{
	 	document.getElementById("cerca3captcha").style.display="none";
	 	document.getElementById("cerca3").style.display="block";
		}
	}
}

function paginazione(attivazioneCaptcha,tipo,occorrenzeLibere,inizio,massimo) {
 	
 	if( "SI" == attivazioneCaptcha){
 	// se chiamo la pagina numero (occorrenzeLibere/10) o se pagino la ricerca per cap, mostro il captcha
 	document.getElementById("cerca3captcha").style.display="none";
 	document.getElementById("ricerca").value="";
 	document.getElementById("territorio").value="";
 	var base = document.ricaricaPagina.base.value;
 	rigeneraCaptcha(base);
 		 	
 	if(tipo.toLowerCase() == "cap"){
 		document.getElementById("controllo").style.display="block";
 		document.ricaricaPagina.start.value= inizio;
		document.ricaricaPagina.max.value= massimo;
		jumpScrollEndOfPage();
	}else{     
	 	if(inizio >=occorrenzeLibere){
	 		document.getElementById("controllo").style.display="block";
	 		document.ricaricaPagina.start.value= inizio;
			document.ricaricaPagina.max.value= massimo;
		}else{     
	 		document.getElementById("controllo").style.display="none";
	 	   	document.ricaricaPagina.start.value= inizio;
			document.ricaricaPagina.max.value= massimo;
		   	document.ricaricaPagina.submit();
	   	}
   	}
   	}else{
   		document.getElementById("cerca3captcha").style.display="none";
	 	document.getElementById("ricerca").value="";
	 	document.getElementById("territorio").value="";
	 	document.getElementById("controllo").style.display="none";
		document.ricaricaPagina.start.value= inizio;
		document.ricaricaPagina.max.value= massimo;
		document.ricaricaPagina.submit();
   	
   	}
}
 

function VerificaCaptchaPaginazione(){
		
	var fieldcaptcha = document.form_controllo.fieldcaptcha.value;
 	var cod = trim2(fieldcaptcha);
	ElaboraDato.checkCaptcha(cod,controllaCaptcha );
}

function controllaCaptcha(data){
    if(data == "errore"){
 		jumpScrollEndOfPage();
 		alert("Incorrect Security Code.");
 		var base = document.ricaricaPagina.base.value;
 		rigeneraCaptchaDaPaginazione(base);
 		document.form_controllo.fieldcaptcha.value="";
 		document.form_controllo.fieldcaptcha.style.backgroundColor="#FFFFCC";
	 		
 	}else{
 		document.ricaricaPagina.textfieldcaptcha.value=document.form_controllo.fieldcaptcha.value;
 		document.ricaricaPagina.submit();
 	}
 }
 
 
function VerificaCaptchaPaginazioneMappe(){
	var fieldcaptcha = document.form_controllo.fieldcaptcha.value;
 	var cod = trim2(fieldcaptcha);
	ElaboraDato.checkCaptcha(cod, controllaCaptchaMappe );
}

function controllaCaptchaMappe(data){
    if(data == "errore"){
 		jumpScrollEndOfPage();
 		alert("Incorrect Security Code.");
 		var base = document.refreshPageMultiMap.base.value;
		rigeneraCaptchaDaPaginazione(base);
		document.form_controllo.fieldcaptcha.value ="";
 		document.form_controllo.fieldcaptcha.style.backgroundColor="#FFFFCC";
	}else{
 		document.refreshPageMultiMap.textfieldcaptcha.value=document.form_controllo.fieldcaptcha.value;
		document.refreshPageMultiMap.submit();
 	}
 } 

function VerificaCaptchaRicerca(codiceCaptcha){
	
	cod=trim2(codiceCaptcha);
	ElaboraDato.checkCaptcha(cod,controllaCampiConCaptcha );
}

function controllaCampi(){ 
	pageTrack();
		
	document.form1.ricerca.style.backgroundColor="#FFFFFF";
	document.form1.territorio.style.backgroundColor="#FFFFFF";
	error =""; 
	if(document.form1.ricerca.value == '' || (trim(document.form1.ricerca.value)).length <= 2){
		error = error + " - Please fill in the research field (3 characters at least)\n";
		document.form1.ricerca.style.backgroundColor="#FFFFCC";
	}
	
	if( (trim(document.form1.territorio.value)).length == 1){
		error = error + " - Please insert a valid Region, Province, Municipality, ZIP  \n";
		document.form1.territorio.style.backgroundColor="#FFFFCC";
	}
	if((document.form1.select1SEL.value == ": ") &&  (document.getElementById("tipoForm").value=="nazione") ){
		error = error + " - Please select the country \n";
		document.form1.select1SEL.style.backgroundColor="#FFFFCC";
	}
	if(error != ""){
        alert(error);
    }else{
    	document.form1.submit();
    }
}  


function controllaCampiConCaptcha(data){

		pageTrack();
		var from = document.getElementById("from").value;
	   
	   	document.form1.ricerca.style.backgroundColor="#FFFFFF";
		document.form1.territorio.style.backgroundColor="#FFFFFF";
		document.form1.textfieldcaptcha.style.backgroundColor="#FFFFFF";
		var base = document.form1.baseForm.value;
		error =""; 
		if(document.form1.ricerca.value == '' || (trim(document.form1.ricerca.value)).length <= 2){
			error = error + " - Please fill in the research field (3 characters at least)\n";
			document.form1.ricerca.style.backgroundColor="#FFFFCC";
		}
		
		if( (trim(document.form1.territorio.value)).length == 1){
			error = error + " - Please insert a valid Region, Province, Municipality, ZIP \n";
			document.form1.territorio.style.backgroundColor="#FFFFCC";
		}
		if(data == 'errore'){
			error = error + " - Incorrect Security Code\n";
	 		document.form1.textfieldcaptcha.style.backgroundColor="#FFFFCC";
	 		document.form1.textfieldcaptcha.value="";
	 	}
	 	if(error != ""){
	        alert(error);
	        if(from == '')
	        	rigeneraCaptcha(base);
	        else
	        	rigeneraCaptchaDaLista(base);
	    }else{
	    	document.form1.submit();
	    }
    
}  
function controllaFormContatti(){

	document.getElementById('problema').style.backgroundColor="#FFFFFF";
	document.getElementById('messaggio').style.backgroundColor="#FFFFFF";
	document.getElementById('email').style.backgroundColor="#FFFFFF";
	document.getElementById('nome').style.backgroundColor="#FFFFFF";
	document.getElementById('cognome').style.backgroundColor="#FFFFFF";
	//document.getElementById('telefono').style.backgroundColor="#FFFFFF";
					
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	errore =""; 
	if(document.getElementById("problema").value == ""){
		errore = errore + " - Please specify the problem kind \n";
		document.getElementById('problema').style.backgroundColor="#FFFFCC";
	}
	if(document.getElementById("messaggio").value == ""){
		errore = errore + " - Please write a short message \n";
		document.getElementById('messaggio').style.backgroundColor="#FFFFCC";
	}
	if(document.getElementById("email").value == ""){
		errore = errore + " - Please insert address email\n";
		document.getElementById('email').style.backgroundColor="#FFFFCC";
	}
	if(document.getElementById("nome").value == ""){
		errore = errore + " - Please insert your name \n";
		document.getElementById('nome').style.backgroundColor="#FFFFCC";
	}
	if(document.getElementById("cognome").value == ""){
		errore = errore + " - Please insert your second name \n";
		document.getElementById('cognome').style.backgroundColor="#FFFFCC";
	}
	if(document.getElementById("email").value != "" && filter.test(document.getElementById("email").value) == false){
		errore = errore + " - Email address is not valid \n";
		document.getElementById('email').style.backgroundColor="#FFFFCC";
	}
	/*if(document.getElementById("telefono").value != "" && isNaN(document.getElementById("telefono").value)){
		errore = errore + " - Phone number is not valid \n";
		document.getElementById('telefono').style.backgroundColor="#FFFFCC";
	}*/
	
	if(errore != ""){
		alert(errore);
		return false;	
	}
	
	return true;

}

function go2() {
      if(document.forms[0].oldUser != null && document.forms[0].oldUser.value != "") {
          if(document.forms[0].user.value != document.forms[0].oldUser.value) {
              if(document.forms[0].force != null) document.forms[0].force.value = "no";
          }
      }
  }
  function go(){
      go2();
      document.forms[0].action="<%=urlTelemaco%>";
      document.forms[0].submit;
      return true;
  }
  function carica(){
      if(document.forms[0].oldUser != null && document.forms[0].oldUser.value != "")
          document.forms[0].user.value = document.forms[0].oldUser.value;
  }
  
  function goSmart(){
      document.forms[0].user.value="";
      document.forms[0].pass.value="";
      document.forms[0].action="<%=urlTelemacoSM%>";
      document.forms[0].method="GET";
      document.forms[0].submit;
  }
  function goPageSmart(url) {
	  if(url.indexOf("dama") != -1) {
	  		 var newUrl = url.substring(0,url.indexOf("dama")) + "asia/pageSmart.jsp?appCallFrom=comc";
	  		 location.href=newUrl;
	  	 } else if(url.indexOf("comd") != -1) {
	  		 var newUrlComd = url.substring(0,url.indexOf("comd")) + "asia/pageSmart.jsp?appCallFrom=comc";
	  		 location.href=newUrlComd;
	  	 } else {
		  location.href=url +"/asia/pageSmart.jsp?appCallFrom=comc";
	  	 }
  }
  
  function trim(value) {
  return value.replace(/^\s+/,"").replace(/\s+$/,"");
  }
  
  function tolgoSpazi(stringa){
  	var splitted = stringa.split(" ");
	var stringatmp ="";
	for (i in splitted) {
		stringatmp = stringatmp + splitted[i];
	}
  	return stringatmp;
  
  
  }
  
 function rigeneraCaptcha(base){
 	if(document.getElementById("frameCaptcha") != null){
 		if(document.getElementById("numero").value ==""){
 			document.getElementById("numero").value="2";
 			document.getElementById("frameCaptcha").src=base + "/ricerca/codiceCaptcha.jsp";
 		}
 		else{	
 			document.getElementById("numero").value="";
 			document.getElementById("frameCaptcha").src=base + "/ricerca/codiceCaptcha2.jsp";
 		}
 	}
 	if(document.getElementById("frameCaptcha2") != null){
 		if(document.getElementById("numero2").value ==""){
 			document.getElementById("numero2").value="2";
 			document.getElementById("frameCaptcha2").src=base + "/ricerca/codiceCaptcha2.jsp";
 		}
 		else{	
 			document.getElementById("numero2").value="";
 			document.getElementById("frameCaptcha2").src=base + "/ricerca/codiceCaptcha.jsp";
 		}
 	}
 }
 
 function rigeneraCaptchaDaLista(base){
 	if(document.getElementById("controllo")!= null)
 		document.getElementById("controllo").style.display="none";
	if(document.getElementById("controlloMappe")!= null)
 		document.getElementById("controlloMappe").style.display="none";
	rigeneraCaptcha(base);
 }
 
 function rigeneraCaptchaDaPaginazione(base){
 	document.getElementById("cerca3captcha").style.display = "none";
 	rigeneraCaptcha(base);
 }
 
 
 function mostraCaptcha(){
 	document.getElementById("cerca3").style.display="none";
 	document.getElementById("cerca3captcha").style.display="block";
 }
 
 function nascondiCaptcha(){
 	document.getElementById("cerca3").style.display="block";
 	document.getElementById("cerca3captcha").style.display="none";
 }

function submitenter(myfield,e) {//fix enter key on submit 
  if (document.getElementById("cerca3captcha") == null || document.getElementById("cerca3captcha").style.display == "none") {
     var keycode;
     if (window.event) keycode = window.event.keyCode;
     else if (e) keycode = e.which;
     else return true; 
     if (keycode == 13)
       {controllaCampi();
        //myfield.form.submit();
        return false;
       }
     else return true;
  }
  else  { var keycode2;
          if (window.event) keycode2 = window.event.keyCode;
          else if (e) keycode2 = e.which;
          else return true; 
          if (keycode2 == 13)
             {VerificaCaptchaRicerca(document.form1.textfieldcaptcha.value);
              return false;
             }
          else  return true;
  
  }
}
 
 


