
function sostTesto(strSorgente, strTestoA, strTestoB)
{
    var strDestinazione = "";
    if (strTestoA.length > 0) {
        while ((i = strSorgente.indexOf(strTestoA)) != -1) {
            strDestinazione += strSorgente.substring(0,i) + strTestoB;
            strSorgente = strSorgente.substring(i + strTestoA.length);
        }
        strDestinazione += strSorgente;
    }
    else {
        strDestinazione = strSorgente;
    }
    return(strDestinazione);
}

function txtToHTML(strTesto)
{
   strTesto = sostTesto(strTesto, "&", "&amp;");
   strTesto = sostTesto(strTesto, "\"", "&quot;");
   strTesto = sostTesto(strTesto, "<", "&lt;");
   strTesto = sostTesto(strTesto, ">", "&gt;");
   return(strTesto);
}

function leggiSegnalibro(key, skips) {
	if (skips == null)
		skips = 0;
	var cookie_string = "" + document . cookie;
	var cookie_array = cookie_string . split ("; ");
	for (var i = 0; i < cookie_array . length; ++ i)
	{
		var single_cookie = cookie_array [i] . split ("=");
		if (single_cookie . length != 2)
			continue;
		var name  = unescape (single_cookie [0]);
		var value = unescape (single_cookie [1]);
		if (key == name && skips -- == 0)
			return value;
	}
	return null;
}

function scriviSegnalibro(name, value, path) {
	var expiration_date = new Date ();
	expiration_date . setYear (expiration_date . getYear () + 1);
	expiration_date = expiration_date . toGMTString ();
	var cookie_string = escape (name) + "=" + escape (value) +
		"; expires=" + expiration_date;
	if (path != null)
		cookie_string += "; path=" + path;
	document . cookie = cookie_string;
}

function aggSegnalibro() {
   var strSLUrl = location.href
   var strSLTitolo = prompt("Specifica il nome della pagina da aggiungere al tuo menu:\n", document.title);
   if (strSLTitolo != null) {
      if (strSLTitolo.length > 0) {
         var intSLCorrente = -1;
         var booSLCtrl = true;
         for (var x = 0; x < 10; ++x) {
            if (leggiSegnalibro("dpf_sl_url_" + x, null) == location.href) {
               booSLCtrl = false;
            } else {
               if (leggiSegnalibro("dpf_sl_titolo_" + x, null) == null && intSLCorrente == -1) {
                  intSLCorrente = x;
               }
            }
         }
         if (intSLCorrente != -1 && booSLCtrl) {
            scriviSegnalibro("dpf_sl_titolo_" + intSLCorrente, txtToHTML(strSLTitolo), "/");
            scriviSegnalibro("dpf_sl_url_" + intSLCorrente, strSLUrl, "/");
            alert("Segnalibro registrato. Fai click sul pulsante \"Il tuo menu\" per eventuali modifiche.");
            location.reload();
         } else if (!booSLCtrl) {
            alert("Segnalibro non registrato perché già esistente.");
         } else {
            alert("Segnalibro non registrato. Hai raggiunto il numero massimo.");
         }
      }
   } 
}

function vaiSegnalibro() {
   var optionMenu = document.forms[0].segnalibro.selectedIndex;
   var urlSegnalibro = document.forms[0].segnalibro.options[optionMenu].value;
   location.href = urlSegnalibro;
}

function apriNew(URL) {
  window.open(URL,"news","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=420,height=400,top=10,left=30");
}

function apriPostOffice() 
{
  var parametri = "width=800,height=600,top=100,left=100,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
  
  Win = window.open("","PostOffice",parametri);
  Win.document.open();

  Win.document.writeln('<','frameset rows="27%,*" frameborder=0 framespacing=0 border=0>');
  Win.document.writeln('<','frame name="Header" src="http://postoffice.finanze.it/appTopFrame.asp?appType=NuovaRubrica"  framespacing=0 frameborder=0 border=0 scrolling=no>');
  Win.document.writeln('<','frameset cols="100%,0" frameborder=1 framespacing=0 border=1>');
  Win.document.writeln('<','frame name="Data" src="http://postoffice.finanze.it/Rubrica/Dipartimenti/20009.htm" framespacing=0 frameborder=0 border=0>');
  Win.document.writeln('<','frame name="Elab" src="Blank.htm" framespacing=0 frameborder=0 border=0>');
  Win.document.writeln('<','/frameset>');
  Win.document.writeln('<','noframes>');
  Win.document.writeln('<','body>');
  Win.document.writeln('<','p>Questa pagina utilizza i frames, ma il vostro browser non li supporta.</p>');
  Win.document.writeln('<','/body>');
  Win.document.writeln('<','/noframes>');
  Win.document.writeln('<','/frameset>');
  
  Win.document.close();
  Win.focus();
}

function ricercamf(){
  location.href = "http://search.finanze.it/cgi-bin/query?mss=search&kl=it&i=AgenziaEntrate&q="+document.homeform.q.value;
}

function ValidaAzione(){
       //alert("?");
       return false;
}

function vaiMenu() {
   var optionMenu = document.forms[0].menu_servizi.selectedIndex;
   var urlMenu = document.forms[0].menu_servizi.options[optionMenu].value;
   location.href = urlMenu;
}

function invioModulo(){
   var tasto = window.event.keyCode;
   if (tasto == 13)
   {
      location.href = "http://search.finanze.it/cgi-bin/query?mss=search&text=true&kl=it&i=AgenziaEntrate&q="+document.homeform.q.value;
   }
}

function IsVuoto(stringa)
{
   var nLen;
   var bReturn = true;

   if (stringa != null)
   {
	   nLen = stringa.length;
  	   if (nLen != 0)
      for(var i=0;i<nLen;i++)
      {
         if(stringa.charAt(i) != " ")
         {
         bReturn = false;
         break;
         }
      }
   }
   return (bReturn);
} 

function validatePrompt (Ctrl, PromptStr)
{
  alert (PromptStr)
  Ctrl.focus();
  return;
}	

function IsDate(gg, mm, aa)
{
 if (gg < "01" || gg > "31" )
   return (false);
 else if (mm < "01" || mm > "12" )
   return (false);
 else if (aa < "2000" || aa > "2030" )
   return (false);
 else if (mm == "02" && gg > "29")
   return (false);
 else if ((mm == "02" && gg == "29") && !IsBisestile(parseInt(aa, 10)))
   return (false);
 else if ((mm == "04" || mm == "06" || mm == "09" || mm == "11") && gg > "30")
   return (false);
 else {
   //Ctrl.value = gg + "/" + mm + "/" + aaaa; 
   return (true);
 }
}

function IsBisestile(anno) {
  if ( (anno%100) == 0 ) { 
    // E' un secolo
    if ( (anno%400) == 0 )
      return (true);
  } else
    if ( (anno%4) == 0 )
      return (true);

  return (false);
}

function IsNumero(Ctrl) { 
  if (isNaN(parseInt(Ctrl.value, 10))) {
    return (false);
  } else {
    if ((parseInt(Ctrl.value, 10) == 0) && (Ctrl.value != "0")) {
      return (false);
    } else {
      //Valore numerico (ma possono esserci caratteri non numerici in coda)
      //Elimina eventuali caratteri non numerici in coda
      Ctrl.value = parseInt(Ctrl.value, 10);
      return (true);
    }
  }
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


var CT=0;
AJAX_N_CALLS=0;
function ajaxGetUrl(qryStr,url,returnFunction,httpMethod,AJAX_SHOW_TEST)
{
	CT++;
	
	if(!document.getElementById("ajaxStatus"))
	{
		root = document.createElement("div");
		root.id = "ajaxStatus";
		document.body.appendChild(root);
		
		//alert(_wsize);
		var _wsize = null;
		if( parseInt( navigator.appVersion ) > 3 ) 
		{
			if( navigator.appName === "Netscape" ) 
			{
				_wsize = "100%";
			}

			if( navigator.appName.indexOf( "Microsoft" ) !== -1 ) 
			{
				_wsize = document.body.offsetHeight;
			}
		}
		
		document.getElementById("ajaxStatus").style.zindex="100000";
		document.getElementById("ajaxStatus").style.position="absolute";
		document.getElementById("ajaxStatus").style.width="99%";
		document.getElementById("ajaxStatus").style.height=_wsize;
		document.getElementById("ajaxStatus").style.top="0";
		document.getElementById("ajaxStatus").style.left="0";
		document.getElementById("ajaxStatus").style.backgroundColor="#B8B9B8";
	}
	
	/* trasparenza */	
	var ie = (document.all) ? 1 : 0;
	var p = (ie) ? "filter" : "MozOpacity";
	v = 30;
	v = (ie) ? "alpha(opacity="+v+")" : v/100;
	document.getElementById("ajaxStatus").style[p] = v;
	/* trasparenza */
	
	document.getElementById("ajaxStatus").style.visibility ='visible';
	//window.scrollTo(0,0);

	
	var req = false;
	
    // XMLHttpRequest object
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
		} 
		catch(e)
		{
			req = false;
		}
	}// IE/Windows ActiveX version 
	else if(window.ActiveXObject) 
	{
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) 
			{
				req = false;
			}
		}
	}
	if(req) 
	{
		req.onreadystatechange =function() 
								{
									if (req.readyState == 4) 
									{
										if (req.status == 200) 
										{
											CT--;
											AJAX_N_CALLS--;
											document.getElementById("ajaxStatus").style.visibility ='hidden'; 
											eval(""+returnFunction+"(req.responseText);");
											document.getElementById("ajaxStatus").innerHTML = "";
										} 
										else 
										{
											alert("There was a problem retrieving the XML data:\n" + req.statusText);
										}
									}
									else if (req.readyState == 1)
									{
										//window.scrollTo(0,0);
										document.getElementById("ajaxStatus").innerHTML = "<div style='text-align:center;position:absolute;top:25%;width:99%;border:1px dashed gray;background-color:#EDEDED;font-size:60px;'>"+AJAX_SHOW_TEST+" loading...</div>";
									}
									else if (req.readyState == 2)
									{
										//window.scrollTo(0,0);
										document.getElementById("ajaxStatus").innerHTML = "<div style='text-align:center;position:absolute;top:25%;width:99%;border:1px dashed gray;background-color:#EDEDED;font-size:60px;'>"+AJAX_SHOW_TEST+" loaded...</div>";
									}
									else if (req.readyState == 3)
									{
										//window.scrollTo(0,0);
										document.getElementById("ajaxStatus").innerHTML = "<div style='text-align:center;position:absolute;top:25%;width:99%;border:1px dashed gray;background-color:#EDEDED;font-size:60px;'>"+AJAX_SHOW_TEST+" interactive...</div>";
									}
								};
		req.open(httpMethod, url, true);

		req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length",qryStr.length);
		req.setRequestHeader("Connection","close");								

		req.send(qryStr);
	}
}

function ajaxAddUrl(qryStr,url,returnFunction,httpMethod,AJAX_SHOW_TEST)
{
	if(CT>0)
	{
		setTimeout("ajaxAddUrl('"+qryStr+"','"+url+"','"+returnFunction+"','"+httpMethod+"','"+AJAX_SHOW_TEST+"')",100);
	}
	else
	{
		ajaxGetUrl(qryStr,url,returnFunction,httpMethod,AJAX_SHOW_TEST);
	}
}

function getTemplate(url)
{
	ajaxAddUrl("",url,"resTemplate","GET")
}

function resTemplate(data)
{
	document.getElementById("mainIframe").innerHTML=data;
}


function convertCharAscii(source)
{
	var ss = "";
	var i = 0;
	for(i=0;i<source.length;i++)
	{
		//alert(source);
		if(source.charCodeAt(i) >= 128)
		{
			var chr = "/"+source.substring(0,1)+"/g";

			source = source.replace(chr,"&#"+source.charCodeAt(i));
		}
	}
	return source;
}

function resContentPage(data)
{
	data = trim(data);
//prompt(data);

	if(trim(data)!="")
	{
		var xtData = data.split("^");
		var idPadre = xtData[0];
		var allId = xtData[1];
		
		var xtData = allId.split("|");
		
		var liData = "";
		
		for(var e=0;e<xtData.length;e++)
		{
			if(trim(xtData[e])!="")
			{
				var nowValue = xtData[e];
				
				var tData = nowValue.split("$");
				
				var idData = tData[0];
				var idDataT = tData[1];
				
				// .oO - KzM - Oo.
				var rep = /'/g;
				idDataT = idDataT.replace(rep,"&#39;");
				
				var rep = /\n/g;
				idDataT = idDataT.replace(rep,"");
				
				var rep = /\r/g;
				idDataT = idDataT.replace(rep,"");
//alert(idDataT);
//				var rep2 = /"/g;
//				idDataT = idDataT.replace(rep2,"&#34;");

//				idDataT = convertCharAscii(idDataT);
				// .oO - KzM fine - Oo.
				liData += "<li class=\"map_li\" id=\"map_"+idData+"\"><a href=\"?id="+idData+"\">"+idDataT+"</a></li>\n";

				AJAX_N_CALLS++;
				ajaxAddUrl("","site.php?id=admin&mode=mkmap&AJ=1&aj_idpage="+idData+"&aj_idpadre="+idPadre,"resContentPage","GET",idDataT);
				
			}
			
		}
		if(trim(liData)!="" && document.getElementById("map_"+idPadre))
		{
			document.getElementById("map_"+idPadre).innerHTML +="<ul class=\"map_ul\">"+liData+"</ul>\n";
			
			//window.scrollTo(0,document.getElementById(idPadre).offsetTop);
			
			//document.getElementById("ajaxStatus").style.top=document.getElementById(idPadre).offsetTop;
			//alert(document.getElementById(idPadre).offsetTop);
		}
		
	}
	
	if(AJAX_N_CALLS<=0)
	{
		alert("Creazione della mappa conclusa. Se la mappa è stata creata correttamente \npotete proseguire con il salvataggio della mappa.");
		document.getElementById('save_map').disabled=false;
	}

}

function saveMap()
{
	var data_map = document.getElementById('resMap').innerHTML;
	ajaxAddUrl("&data_map="+data_map,"site.php?id=admin&mode=mkmap&AJ=2","resSaveMap","POST","");
}

function resSaveMap(data)
{
	if(trim(data)=='TRUE')
	{
		alert("Mappa salvata correttamente.");
	}
	else
	{
		alert("Si sono provocati errori in fase di salvataggio.");
	}
}

