// JavaScript Document

IE = ((ind1 = navigator.appVersion.indexOf("MSIE")) > -1) ? 1 : 0;
IE4 = (IE && document.all) ? 1:0;

NN = (navigator.appName.indexOf("Netscape")>-1) ? 1 : 0;
NN4 = (NN && document.layers) ? 1 : 0;
NN6 = (NN && document.getElementById) ? 1:0;


//visualizza il div news della home page
function visualizzaNews(){
	document.getElementById('news').style.display = 'block';
	document.getElementById('comunicatiStampa').style.display = 'none';
	}

//visualizza il div comunicatistampa della home page
function visualizzaComunicati(){
	document.getElementById('comunicatiStampa').style.display = 'block';
	document.getElementById('news').style.display = 'none';
	}

//funzione che visualizza il DIV cn il TAb scelto
function scegliTAB(idTab, tipoTab){
	
	var nomiTab_prodotti = new Array("tutti", "mobile", "carta", "internet", "telefono", "marketing");
	
	classVisibile = "divVisibile";
	classNascosto = "divNascosto";
	
	if(idTab != ""){
		// associo il div relativo al tipoTab, nel caso di una nuova gestione oltre ai prodotti
		// qui va associato l'array corretto
		var arrayTab = new Array();
		if(tipoTab.toLowerCase() == 'prodotti') {arrayTab = nomiTab_prodotti;}
		
		// nascondo tutti i TAB
		for (i=0; i<arrayTab.length; i++){
			var id = arrayTab[i].toLowerCase();
			if(document.getElementById(id)){
				document.getElementById(id).className = classNascosto;
				}
		}
		
		// visualizzo il div scelto
		document.getElementById(idTab).className = classVisibile;
		

	}
	else{alert('Errore nella selezione del TAB!');}
	
}

//gestisce la dimensione dei font nel load delle pagine
function gestioneFontScelto(){
	ok = (document.styleSheets) ? 1 : 0;
	
	if(ok) {
		if(IE4) stile = document.styleSheets[0].rules;
		if(NN6) stile = document.styleSheets[0].cssRules;
		
		//recupero il valore del campo hidden, se non c'e' nel cookie
		if(getCookie('dimFont')=='undefined'){
		dimFontScelto = document.getElementById('dimensioniFont').value;
		}
		else{dimFontScelto = getCookie('dimFont');}
		
			if (!parseFloat(dimFontScelto,2)){
				document.getElementById('dimensioniFont').value = 1;
				setCookie('dimFont','1');
				dimFontScelto = 1;
				}
			if (dimFontScelto != 1){
			document.getElementById('dimensioniFont').value = dimFontScelto;
			}
			stile[1].style.fontSize = dimFontScelto + "em";
		}
	else{
		alert("Non e' possibile diminuire il font,\nil vostro browser non lo permette.");
	}
}

//funzione per diminuire la dimesnione del testo
function diminuisciFont(){
	ok = (document.styleSheets) ? 1 : 0;
	
	if(ok) {
		if(IE4) stile = document.styleSheets[0].rules;
		if(NN6) stile = document.styleSheets[0].cssRules;
		
		//recupero il valore nel cookie o del campo hidden
		if(getCookie('dimFont')=='undefined'){
			dimFontScelto = document.getElementById('dimensioniFont').value;
			}
		else{dimFontScelto = getCookie('dimFont');}
		//alert(dimFontScelto);
		//diminuisco solo se >0,8em sotto cui non si legge + niente
		if(dimFontScelto > 0.8){
		dimFontScelto = (parseFloat(dimFontScelto,2) - 0.1);
		document.getElementById('dimensioniFont').value = dimFontScelto;
		setCookie('dimFont',dimFontScelto);
		stile[1].style.fontSize = dimFontScelto + "em";
         this.location.reload();
		}
		else{alert("Non e' possibile diminuire ulteriormente il font,\nrisulterebbe illegibile!");}
	}
	else{
		alert("Non e' possibile diminuire il font,\nil vostro browser non lo permette.");
	}
}

//funzione per aumentare la dimesnione del testo
function aumentaFont(){
	ok = (document.styleSheets) ? 1 : 0;
	
	if(ok) {
		if(IE4) stile = document.styleSheets[0].rules;
		if(NN6) stile = document.styleSheets[0].cssRules;

		//recupero il valore nel cookie o del campo hidden
		if(getCookie('dimFont')=='undefined'){
			dimFontScelto = document.getElementById('dimensioniFont').value;
			}
		else{dimFontScelto = getCookie('dimFont');}

		//aumento il font
		dimFontScelto = (parseFloat(dimFontScelto,2) + 0.1);
		setCookie('dimFont',dimFontScelto);
		stile[1].style.fontSize = dimFontScelto + "em";
		
        this.location.reload();

	}
	else{
		alert("Non e' possibile diminuire il font,\nil vostro browser non lo permette.");
	}
}


//funzione per riportare il testo standard
function normalizzaFont(){
		ok = (document.styleSheets) ? 1 : 0;
	if(ok) {
		if(IE4) stile = document.styleSheets[0].rules;
		if(NN6) stile = document.styleSheets[0].cssRules;
		
		//rimetto il font=1em
		document.getElementById('dimensioniFont').value = "1";
		setCookie('dimFont','1');
		stile[1].style.fontSize = "1em";
        this.location.reload();

	}
	else{
		alert("Non e' possibile diminuire il font,\nil vostro browser non lo permette.");
	}
}

// imposta il cookie sNome = sValore
// per la durata di iGiorni
function setCookie(sNome, sValore, iGiorni) {
  var dtOggi = new Date()
  var dtExpires = new Date()
  dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);
  var path="/";
  document.cookie = sNome + "=" + escape(sValore) + ((iGiorni == null) ? "" : ("; expires=" + dtExpires.toGMTString())) +"; path=" + path;
}

// prende il valore di un cookie
function getCookie (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                        return getCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                        if (i == 0)
                                break;
                }
   return null;
}

// valore del cokie
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

//popups
function pop(info,altezza,larghezza)
	{ 
		window.open(info,'','width='+larghezza+',height='+altezza+',toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no');
	}

function openPopup(url,w,h) {
    win_name = 'p_' + (new Date()).getTime().toString();
    win_left = (screen.width - w) / 2;
    win_top = (screen.height - h) / 2;
    var attr = "left=" + win_left + ",top=" + 
       win_top + ",resizable=yes,scrollbars=yes,width=" + w + 
       ",height=" + h;
    var popup=window.open(url,win_name,attr);
}

	
function stampaPreview(){
	var indirizzo=String(document.location);
	var params=indirizzo.split("?");
	var nuovoInd="";
	 if(params.length > 1){ nuovoInd = indirizzo + "&stampa=true";}
	 else{nuovoInd = indirizzo + "?stampa=true";}
	 
	 window.open(nuovoInd,"stampa","");
	 
}
 
 function controlloStampa(){
	 var dastampare=false;
	 
	 indirizzo=unescape(String(document.location));
	 params=indirizzo.split("?");
	 if(params.length > 1){
	 param=params[1].split("&");
	 
	 
		 for(i=0;i<param.length;i++){
			 var param_temp=param[i].split("=");
			 var param_name=param_temp[0];
			if(param_name.toLowerCase() == "stampa"){
				var obj = document.styleSheets
				dastampare = true;
				//diattivo tutti i fogli stile
				for(i=0; i<obj.length; i++){
					//obj[i].disabled=true;
					if(obj[i].title=="stampa"){obj[i].disabled=false;}
				}
				
				
			}
		}
	 }
	 
	 if(dastampare){window.print();}
 }



var errors = false

function vfRTextfield(form,id){
	idform= "form_"+id;
	if (document.forms[form][idform].value == ""){
		if (errors==false)
		alert("Campo obbligatorio: "+ id)
		errors = true
		return false
	}
}

function vfConsenso(form,id){
	idform= "form_"+id;
	if (document.forms[form][idform][0].checked == false){
		if (errors==false)
		alert("E' necessario dare il proprio assenso")
		errors = true
		return false
	}
}


function vfRSelect(form,id){
	idform= "form_"+id;
	if (document.forms[form][idform].value == ""){
		if (errors==false)
		alert("Campo obbligatorio: "+ id)
		errors = true
		return false
	}
}

function vfREmail(form,id){
	idform= "form_"+id;
	 EmailAddr = document.forms[form][idform].value;
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(EmailAddr))
      return true;
   else
      {
      if (errors==false)
		alert("Campo obbligatorio: "+ id)
		errors = true
		return false
      }

}

function vfRRadio(form,id){
	idform= "form_"+id;
	result = false
	for (var i = 0; i<document.forms[form][idform].length; i++){
		//alert(document.forms[form][id][i].checked)
		if (document.forms[form][idform][i].checked){
			result = true
		}
	}
	if (result == false){
		if (errors==false)
			alert("Campo obbligatorio: "+ id)
			errors = true
	}
	return result
}
function vfFormContattoInvestors(lang){
if (lang=='en') return vfFormContattoInvestorsEn();
	vfRTextfield('contatto','nome')
	vfRTextfield('contatto','cognome')

	vfRTextfield('contatto','azienda')
	vfRTextfield('contatto','settore_attivita')
	vfRTextfield('contatto','professione')


	vfRTextfield('contatto','indirizzo')
	vfRTextfield('contatto','numcivico')
	vfRTextfield('contatto','comune')
	vfRTextfield('contatto','cap')
	vfRTextfield('contatto','telefono')
        vfREmail('contatto','email')
	vfConsenso('contatto','radconsenso')
	if (errors == true) {
		errors = false
		return false
	}
}



//////////////////////////////////////////////////////

function vfRTextfieldEn(form,id){
	idform= "form_"+id;
	if (document.forms[form][idform].value == ""){
	 if (errors==false)
		alert("Please fill all mandatory fields: "+ id)
		errors = true
		return false
	}
}

function vfConsensoEn(form,id){
	idform= "form_"+id;
	if (document.forms[form][idform][0].checked == false){
		if (errors==false)
		alert("You must give your approval")
		errors = true
		return false
	}
}


function vfRSelectEn(form,id){
	idform= "form_"+id;
	if (document.forms[form][idform].value == ""){
		if (errors==false)
		alert("Please fill all mandatory fields.")
		errors = true
		return false
	}
}
function vfREmailEn(form,id){
	idform= "form_"+id;
	 EmailAddr = document.forms[form][idform].value;
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(EmailAddr))
      return true;
   else
      {
      if (errors==false)
		alert("Please fill all mandatory fields: email")
		errors = true
		return false
      }

}

function vfRRadioEn(form,id){
	idform= "form_"+id;
	result = false
	for (var i = 0; i<document.forms[form][idform].length; i++){
		//alert(document.forms[form][id][i].checked)
		if (document.forms[form][idform][i].checked){
			result = true
		}
	}
	if (result == false){
		if (errors==false)
			alert("Please fill all mandatory fields.")
			errors = true
	}
	return result
}


function vfFormContattoInvestorsEn(){
	vfRTextfieldEn('contatto','nome')
	vfRTextfieldEn('contatto','cognome')
	vfRTextfieldEn('contatto','azienda')
	vfRTextfieldEn('contatto','settore_attivita')
	vfRTextfieldEn('contatto','professione')
	vfRTextfieldEn('contatto','indirizzo')
	vfRTextfieldEn('contatto','numcivico')
	vfRTextfieldEn('contatto','comune')
	vfRTextfieldEn('contatto','cap')
	vfRTextfieldEn('contatto','telefono')
        vfREmailEn('contatto','email')
	vfConsenso('contatto','radconsenso')
	if (errors == true) {
		errors = false
		return false
	}
}

function FormValidator() {
this.form_errors=false;
this.bad_fields="";
this.focus_field=null;
}

FormValidator.prototype.reset_fields = function() {
this.form_errors=false;
this.bad_fields="";
this.focus_field=null;
}

FormValidator.prototype.addBadField = function(field,label) {
    if (this.bad_fields=="") {
        this.bad_fields="'"+label+"'";
    }
    else {
        this.bad_fields=this.bad_fields + ", '"+label+"'" 
    }
    if (field!=null) {
        if (this.focus_field==null) {this.focus_field=field;}
        field.style.background='yellow';
    }
    this.form_errors = true;
}

FormValidator.prototype.checkNumberField = function(form,id, label){
	idform= id;
    field=document.forms[form][idform];
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if ( (field.value == "") ||  (!anum.test(field.value)) ){
        this.addBadField(field,label);
		return false
	}
    else {
	 field.style.background='white';
     return true;
    }
}
FormValidator.prototype.checkTextField = function(form,id, label){
	idform= id;
    field=document.forms[form][idform];
	if (field.value == ""){
        this.addBadField(field,label);
		return false
	}
    else {
	 field.style.background='white';
     return true;
    }
}
FormValidator.prototype.checkEmailField = function(form,id,label){
	idform= id;
	 field = document.forms[form][idform];
	 EmailAddr = field.value;
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(EmailAddr)) {
	 field.style.background='white';
      return true;
   }
   else
      {
        this.addBadField(field,label);
		return false
      }
}

FormValidator.prototype.checkRadioField = function(form,id,label){
	idform= id;
	result = false
	 field = document.forms[form][idform];
	for (var i = 0; i<field.length; i++){
		if (field.checked){
			result = true
		}
	}
	if (result == false){
        this.addBadField(field,label);
		return false
	}
	return result
}

// array to hold all onLoad functions
onLoadFunctions = new Array();


function openFullscreenPopup(url) {
                win_name = 'yessv_' + (new Date()).getTime().toString();
                var popup=window.open(url,win_name,'width='+document.body.clientWidth+',height='+document.body.clientHeight+',left=0,top=0,resizable=yes,scrollbars=yes,toolbar=yes,status=1,menubar=yes,personalbar=yes,location=1');
}
// this runs on each page
function _initOnLoadFunctions() {
   // loop through all the items in the onLoad array
   // and execute any functions
   for(var i = 0; i < window.onLoadFunctions.length; i++) {
      if( typeof window.onLoadFunctions[i] == "function") window.onLoadFunctions[i]();
   }
}

function addOnLoad(func) {
   window.onLoadFunctions[window.onLoadFunctions.length] = func;
}

function cerca() {
   document.forms['form_cerca'].submit();
}

function cercaOrdina(tipo) {
   var input = document.forms['form_cercaordinato'].ordinamento;
   if (tipo == 0) {
   	input.value = "date";
   } else if (tipo == 1) {
   	input.value = "score";
   } else if (tipo == 2) {
   	input.value = "sitearea";
   } else {
   	input.value = "score";
   }
   document.forms['form_cercaordinato'].submit();
}

function cercaFormato(tipoFormato) {
   var input_format = document.forms['form_cercaformato'].formato;
   if (tipoFormato == 0) {
   	input_format.value = "tutti";
   } else if (tipoFormato == 1) {
   	input_format.value = "html";
   } else if (tipoFormato == 2) {
   	input_format.value = ".pdf";
   } else {
   	input_format.value = "tutti";
   }
   document.forms['form_cercaformato'].submit();
}

//converte il formato di input dd/MM/yyyy nel formato di output yyyyMMdd necessario per la ricerca
function convertiformato(origine) {
	document.forms['search1'].to.value=document.forms['search1'].to_formato.value;
}

