// JavaScript Document
window.onload = function() {
  if (document.location.href.search(/edit|admin/) != -1) {
    if (document.location.href.search(/\/$/) != -1 || document.location.href.search(/page=%2f/i) != -1) {
      //document.location.href = '/albero/index.html';
    }
    if (document.getElementById('albero_01')) {
      document.getElementById('albero_01').href= '/albero/index.html?id=0';
      document.getElementById('albero_02').href= '/albero/index.html?id=1';
      document.getElementById('albero_03').href= '/albero/index.html?id=2';
      document.getElementById('form_ricerca_ambiti').action= '/albero/index.html';
    }
  }
}

function PopupPic(sPicURL) { 
// dimensioni iniziali finestra prima che si riadatti
 popwidth = 250;
 popheight = 400;
 // apre il popup e passa la url immagine
 window.open( "/popups/pop_fit.html?"+sPicURL, "", 
 "left=" + Math.floor((screen.width - popwidth) / 2) + ",top=" + Math.floor((screen.height - popheight) / 2) +
 "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width="+popwidth+",height="+popheight); 
}

function PopAmico(page) {
popwidth = 500;
popheight = 300;
OpenWin = this.open(page, "PopWin2", "toolbar=no, menubar=no, location=no, scrollbars=no, resizable=no, width="+popwidth+", height="+popheight+", left=" + Math.floor((screen.width - popwidth) / 2) + ", top=" + Math.floor((screen.height - popheight) / 2));
}

function Credits(page) {
popwidth = 300;
popheight = 270;
OpenWin = this.open(page, "PopWin3", "toolbar=no, menubar=no, location=no, scrollbars=no, resizable=no, width="+popwidth+", height="+popheight+", left=" + Math.floor((screen.width - popwidth) / 2) + ", top=" + Math.floor((screen.height - popheight) / 2));
};

function controllo_ricerca(theForm){
	if (theForm.testo_ricerca.value == "") {
    	alert("Per favore inserire l'oggetto della ricerca.\nGrazie.");
    	theForm.testo_ricerca.focus();
    	return (false);
	}
}

function controllo_ricerca_ambiti(theForm){
	if (theForm.srctext.value == "" && theForm.searchClass.selectedIndex == 0) {
    	alert("Per favore inserire l'oggetto della ricerca oppure selezionare un'ambito.\nGrazie.");
    	theForm.srctext.focus();
    	return (false);
	}
}

function submit_ricerca(theForm){
	if (theForm.testo_ricerca.value == "") {
    	alert("Per favore inserire l'oggetto della ricerca.\nGrazie.");
    	theForm.testo_ricerca.focus();
	} else {
		  theForm.submit()
	}		
}

function URLEncode(s)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = s;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	
	return encoded;
};

function URLDecode(s)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = s;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};


/*

var aumentoDimensioneTesto = location.search.substring(location.search.indexOf("STB=")+4, (location.search.substr(location.search.indexOf("STB=")+4).indexOf(";"))+(location.search.indexOf("STB=")+4));

dimensionamentoTesto();

function writeIcoText(){
  if(aumentoDimensioneTesto!="BIG"){
		document.write('<a href="javascript:manualTextResize()"><img src="/immagini/azioni/txt_big.gif" border="0" alt="Ingrandisci il testo" name="icoTextResize" hspace="3" align="absmiddle"></a>');
  }
  else{
		document.write('<a href="javascript:manualTextResize()"><img src="/immagini/azioni/txt_small.gif" border="0" alt="Riduci il testo" name="icoTextResize" hspace="3" align="absmiddle"></a>');
  }
}

function manualTextResize(){
	  if(document.all){	  
				if(aumentoDimensioneTesto!="BIG"){
					  aumentoDimensioneTesto = 'BIG';	  
					  styleFile.href="/css/stili_fabbrica_grandi.css";
					  document.images['icoTextResize'].src = "/immagini/azioni/txt_small.gif"
					  document.images['icoTextResize'].alt = "Riduci il testo";
				}
				else{
					  aumentoDimensioneTesto = '';
					  styleFile.href="/css/stili_fabbrica.css";
					  document.images['icoTextResize'].src = "/immagini/azioni/txt_big.gif";
					  document.images['icoTextResize'].alt = "Ingrandisci il testo";	  
				}	
	  } else {
					 if(aumentoDimensioneTesto!="BIG"){
						  if (location.href.indexOf("?") <= 0) {
								location.href = location.href + "?STB=BIG;";
						  } else {
								location.href = location.href + "&STB=BIG;";
						  }
					}else{
							location.href = location.href.substr(0, location.href.indexOf("STB=") - 1);
					}
	  }	
}

function dimensionamentoTesto(){
  if(aumentoDimensioneTesto=="BIG"){
		if(document.all){
		  document.write('<link id="styleFile" rel="stylesheet" href="/css/stili_fabbrica_grandi.css">');
		}
		else{
		  document.write('<link rel="stylesheet" href="/css/stili_fabbrica_grandi.css">');
		}
  }
  else{
		if(document.all){
		  document.write('<link id="styleFile" rel="stylesheet" href="/css/stili_fabbrica.css">');
		}
		else{
		  document.write('<link rel="stylesheet" href="/css/stili_fabbrica.css">');
		}
  }
}

*/

function  Verifica_checkbox_selezionati(theForm) {
var i=0;
var vero=false;

while (i <=7){
	if (theForm.elements[i].checked == true) {
	vero=true;
	}
++i;
}

if (vero == false) {
	alert("Attenzione!\nE' necessario selezionare almeno un criterio di ricerca");
	return false;

} else if (theForm.SearchedTxt.value == "") {
	alert("Attenzione!\nE' necessario digitare almeno una parola da cercare");
	return false;

} else {

var pat = /^[0-9|\s]*$/;

var da_giorno = theForm.da_giorno.value ;
var da_mese = theForm.da_mese.value ;
var da_anno = theForm.da_anno.value ;
var data = true;

if (da_giorno > 28) {
    if (da_giorno > 30 && da_mese != 2) {
 	  if (da_mese == 4 || da_mese == 6 || da_mese == 9 || da_mese == 11) {
	   	alert("Attenzione: il mese selezionato ha 30 giorni.");
   		theForm.da_giorno.focus();
		return(false);
	  }
	} else {
	    if (da_mese == 2) {
		  if (((da_anno % 4 != 0 || da_anno == 1900) && da_giorno == 29) || (da_giorno >= 30)) {
		   	alert("Attenzione: Il mese di febbraio dell'anno selezionato ha 28 giorni.");
		   	theForm.da_giorno.focus();
			return(false);
		  }
		}
	}
}

if ((da_giorno > 31) || (da_giorno == 0)) {
   	alert("Attenzione: il giorno da cui far partire la ricerca non e' corretto.");
   	theForm.da_giorno.focus();
	return(false);
}

if ((da_mese > 12) || (da_mese == 0)) {
   	alert("Attenzione: il mese da cui far partire la ricerca non e' corretto.");
   	theForm.da_mese.focus();
	return(false);
}

 if(!pat.test(da_giorno) || da_giorno =="") {
	alert("Il giorno da cui far partire la ricerca non e' valido o non e' stato inserito.\rIl formato deve essere gg.\nAd esempio 09");
	theForm.da_giorno.focus();
	return false;
}	  

 if(!pat.test(da_mese) || da_mese =="") {
	alert("Il mese da cui far partire la ricerca non e' valido o non e' stato inserito.\rIl formato deve essere mm.\nAd esempio 01");
	theForm.da_mese.focus();
	return false;
}	

 if(!pat.test(da_anno) || da_anno =="") {
	alert("L'anno da cui far partire la ricerca non e' valido o non e' stato inserito.\rIl formato deve essere aaaa.\nAd esempio 1970");
	theForm.da_anno.focus();
	return false;
}	

var a_giorno = theForm.a_giorno.value ;
var a_mese = theForm.a_mese.value ;
var a_anno = theForm.a_anno.value ;
var data = true;

if (a_giorno > 28) {
    if (a_giorno > 30 && a_mese != 2) {
 	  if (a_mese == 4 || a_mese == 6 || a_mese == 9 || a_mese == 11) {
	   	alert("Attenzione: il mese selezionato ha 30 giorni.");
   		theForm.a_giorno.focus();
		return(false);
	  }
	} else {
	    if (a_mese == 2) {
		  if (((a_anno % 4 != 0 || a_anno == 1900) && a_giorno == 29) || (a_giorno >= 30)) {
		   	alert("Attenzione: Il mese di febbraio dell'anno selezionato ha 28 giorni.");
		   	theForm.a_giorno.focus();
			return(false);
		  }
		}
	}
}

if ((a_giorno > 31) || (a_giorno == 0)) {
   	alert("Attenzione: il giorno da cui si vuole far partire la ricerca non è corretto non e' corretto.");
   	theForm.a_giorno.focus();
	return(false);
}

if ((a_mese > 12) || (a_mese == 0)) {
   	alert("Attenzione: il mese da cui si vuole far partire la ricerca non è corretto non e' corretto.");
   	theForm.a_mese.focus();
	return(false);
}

 if(!pat.test(a_giorno) || a_giorno =="") {
	alert("Attenzione: il giorno a cui si vuole far finire la ricerca non è corretto o non inserito.\rIl formato deve essere gg.\nAd esempio 09");
	theForm.a_giorno.focus();
	return false;
}	  

 if(!pat.test(a_mese) || a_mese =="") {
	alert("Attenzione: Il mese a cui si vuole far finire la ricerca non è corretto o non inserito.\rIl formato deve essere mm.\nAd esempio 01");
	theForm.a_mese.focus();
	return false;
}	

 if(!pat.test(a_anno) || a_anno =="") {
	alert("Attenzione: l'anno a cui si vuole far finire la ricerca non è corretto non valido o non inserito.\rIl formato deve essere aaaa.\nAd esempio 1970");
	theForm.a_anno.focus();
	return false;
}	

}
};

