// JavaScript Document finestre versione per f13framework

function f_scrollLeft() {
        return f_filterResults (
                window.pageXOffset ? window.pageXOffset : 0,
                document.documentElement ? document.documentElement.scrollLeft : 0,
                document.body ? document.body.scrollLeft : 0
        );
}
function f_scrollTop() {
        return f_filterResults (
                window.pageYOffset ? window.pageYOffset : 0,
                document.documentElement ? document.documentElement.scrollTop : 0,
                document.body ? document.body.scrollTop : 0
        );
}
function f_filterResults(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
                n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function open_caratt(id2) {
	if(document.getElementById(id2).style.display == "none")
	{
		Effect.BlindDown(id2, { duration: 1 });
		disp=1;
	}
	else
	{
		Effect.BlindUp(id2, { duration: 1 });
		disp=0;
	}
}

function scr(yy){
	window.scrollBy(0,yy);
}

function WindowOpen(id2, arr) {
	b=0;
	if(document.getElementById(id2).style.display == "none")
	{
		yy=(window.pageYOffset)?window.pageYOffset:document.body.scrollTop;
		for(i=0; i<arr.length; i++)
		{
			if(document.getElementById(arr[i]).style.display == "none")
			{
				b=b+0;
			}
			else 
			{ 
				b++;
			}
		}
		if(b==0){
			tp=30;
			zind=0;}
		else
			tp=tp+20
		document.getElementById(id2).style.top = tp+'px';
		//document.getElementById(id2).style.right = Math.round(Math.random()*40)+30+'px';
		zind++;
		document.getElementById(id2).style.zIndex = zind;
		Effect.Appear(id2, { duration: .3 });
		
		
	}
	

}

function openForm(id2) {
	var hwin=(document.compatMode=='CSS1Compat' && !window.opera)?document.documentElement.clientHeight:document.body.clientHeight;	
	document.getElementById(id2).style.top=(Math.round(hwin)-(document.getElementById(id2).style.height.replace('px', '')))/2+f_scrollTop()+"px";
	document.getElementById(id2).style.left=(((document.compatMode=='CSS1Compat' && !window.opera)?document.documentElement.clientWidth:document.body.clientWidth)-document.getElementById(id2).style.width.replace(/px/, ""))/2+"px";
	new Draggable(id2, {handle: 'chiudi', starteffect: 'none', endeffect:'none'});
	Effect.Appear(id2, { duration: .0 });
	
}

function openFormMouse(id2, e) {
	if(!e) e = window.event;
	document.getElementById(id2).style.top=e.clientY+20+"px";
	width = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	var hwin=(document.compatMode=='CSS1Compat' && !window.opera)?document.documentElement.clientHeight:document.body.clientHeight;
	document.getElementById(id2).style.top=(Math.round(hwin)-(document.getElementById(id2).style.height.replace('px', '')))/2+f_scrollTop()+"px";	//document.getElementById(id2).style.top=(Math.round(hwin-f_scrollTop())-(document.getElementById(id2).style.height.replace(/px/, "")))/2+"px";
	//alert(document.getElementById(id2).style.height.replace('px', ''));
	//document.getElementById(id2).style.left= ((e.clientX>width/2) ? (e.clientX-width/2) : e.clientX)+"px";
	document.getElementById(id2).style.left=(width-(document.getElementById(id2).style.width.replace('px', '')))/2+"px";
	//alert((width-(document.getElementById(id2).style.width.replace('px', '')))/2+"px");
	new Draggable(id2, {handle: id2+'_barra', starteffect: '', endeffect:''});
	Effect.Appear(id2, { duration: .0 });
	focusdiv(id2);
	// resizable
	setTimeout("DragCorner('"+id2+"', '"+id2+"_res')", 100);

}

function closeForm(id2) {
	Effect.Fade(id2, { duration: .0 });
}

// Caricamento moduli
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
    try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (err2) {
        try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (err3) {
            req = false;
        }
    }
}
return req;
} 

function img_attesa(iddiv, conn){
		if(conn.readyState!=4)
		{
			var divinner=document.getElementById(iddiv);
			divinner.innerHTML='Sto trasferendo';
			img_attesa(iddiv, conn);
		}
		else
		{
			return
		}
		
}

/*function gestisciRichiesta()
	{
		var divinner=document.getElementById(iddiv);
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200 || xmlHttp.status==0)
			{
				var risultatoTxt = xmlHttp.responseText;
				divinner.innerHTML=risultatoTxt;
			} 
			else 
			{
			alert("comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText+")");
			}
		}
		else
		{
		 	divinner.innerHTML='Sto Caricando...';
		}
	}*/

function apertura_pagina(id, iddiv, selettore, url){
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange= function gestisciRichiesta()
	{
		var divinner=document.getElementById(iddiv);
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200 || xmlHttp.status==0)
			{
				var risultatoTxt = xmlHttp.responseText;
				divinner.innerHTML=risultatoTxt;
			} 
			else 
			{
			alert("comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText+")");
			}
		}
		else
		{
		 	divinner.innerHTML='<img src="css/img/ing035.gif" />';
		}
	};
	xmlHttp.open('get', url+'?id='+id+'&div='+selettore+'&divref='+iddiv, true);
	xmlHttp.send(null);
}
	/*var xmlHttp = getXMLHTTPRequest();
	xmlHttp.open('get', url+'?'+id+'&div='+iddiv, false);
	xmlHttp.send(null);
	if(xmlHttp.status==200 || xmlHttp.status==0){
		var risultatoTxt = xmlHttp.responseText;
		var divinner=document.getElementById(iddiv);
		divinner.innerHTML=risultatoTxt;
	} else {
		alert("comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText+")");
	}
}*/


function form_modifica(id, iddiv, selettore){
	return apertura_pagina(id, iddiv, selettore, 'formmod.php');
	/*var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange= function gestisciRichiesta()
	{
		var divinner=document.getElementById(iddiv);
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200 || xmlHttp.status==0)
			{
				var risultatoTxt = xmlHttp.responseText;
				divinner.innerHTML=risultatoTxt;
			} 
			else 
			{
			alert("comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText+")");
			}
		}
		else
		{
		 	divinner.innerHTML='Sto Caricando...';
		}
	};
	xmlHttp.open('get', 'formmod.php?id='+id+'&div='+iddiv, true);
	xmlHttp.send(null);*/
}

function open_menu(id2) {
	if(document.getElementById(id2).style.display == "none")
	{
		Effect.SlideDown(id2, { duration: 1 });
		document.getElementById('barra_menu_'+id2).style.backgroundImage = "url(css/img/"+id2+"_menusu.png)";
	}
	else
	{
		Effect.SlideUp(id2, { duration: 1 });
		document.getElementById('barra_menu_'+id2).style.backgroundImage = "url(css/img/"+id2+"_menugiu.png)";
	}
}


function open_win(id) {
	if(document.getElementById(id).style.display == "none")
	{
		Effect.Appear(id, { duration: 0.0 });
	}
	else
	{
		Effect.Fade(id, { duration: 0.0 });

	}
}

function refresh(param) {
	window.location.href=unescape(window.location.pathname+param);
}

function refresh_div(iddiv, url)
{
	var xmlHttp = getXMLHTTPRequest();
	xmlHttp.onreadystatechange= function gestisciRichiesta()
	{
		var divinner=document.getElementById(iddiv);
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200 || xmlHttp.status==0)
			{
				var risultatoTxt = xmlHttp.responseText;
				divinner.innerHTML=risultatoTxt;
			} 
			else 
			{
			alert("comunicazione fallita (codice: "+xmlHttp.status+"! Motivo: "+xmlHttp.statusText+")");
			}
		}
		else
		{
		 	divinner.innerHTML='<img src="css/img/ing035.gif" />';
		}
	};
	xmlHttp.open('get', url, true);
	xmlHttp.send(null);
}


//Controllo campi

function controllo_campo_num(campo,tipo)
{
 var lunghezza = campo.value.length;
 var codascii=campo.value.charCodeAt(lunghezza -1);
 if ( codascii==46 || ( codascii>=48 && codascii<=57))
 {
  return true;
 }
 else
 {
  document.getElementById(tipo).value=campo.value.substr(0,lunghezza -1);
 }
}

function scorri(div, spost, a, dir, j, verso){
		j++;
		if(dir=="v")
		{
			if(verso>0)
				document.getElementById(div).scrollTop=document.getElementById(div).scrollTop+spost;
			else
				document.getElementById(div).scrollTop=document.getElementById(div).scrollTop-spost;
		}
		else
		{
			
			if(verso>0)
			{
				//alert(document.getElementById(div).scrollLeft);
				document.getElementById(div).scrollLeft=document.getElementById(div).scrollLeft+spost;
			}
			else
				document.getElementById(div).scrollLeft=document.getElementById(div).scrollLeft-spost;
		}
		if(j<61)
		{
			spostamento=Math.round(a*(Math.pow(Math.sin(j*Math.PI/60),2)));
			setTimeout("scorri(\'"+div+"\', "+spostamento+", "+a+", \'"+dir+"\', "+j+", "+verso+");", 20);
		}
}

function scrolling(div, dir, verso)
{
	// direzione: o-> orizzontale, v-> verticale
	j=0;
	
	var sum=0;
	for(a=0; a<61; a++)
	{
		sum=sum+Math.pow(Math.sin(a*Math.PI/60),2);
	}
	ampiezza= (dir=="o") ? $(div).getDimensions().width/sum : $(div).getDimensions().height/sum;
	scorri(div, 0, ampiezza, dir, j, verso);
}

function scrolling_qta(div, dir, qta)
{
	j=0;
	
	var sum=0;
	pippo="";
	for(a=0; a<61; a++)
	{
		sum=sum+Math.pow(Math.sin(a*Math.PI/60),2);
	}
	ampiezza= document.getElementById(div).style.height.replace(/px/, "")/sum
	spost=((qta-1)*ampiezza)-(document.getElementById(div).scrollTop)/sum;
	dir=(spost<0) ? -1 : 1;
	spost=(spost<0) ? -1 * spost : spost;
	scorri(div, 0, spost, dir, j);
}

function modfica_session(iddiv, value) // accoppiata ad una funzione PHP si puņ variare il valore di una variabile SESSION
{
var xmlHttp = getXMLHTTPRequest();
	xmlHttp.open('get', 'formmod.php?value='+value+'&div='+iddiv, false);
	xmlHttp.send(null);
}




          
function populate(selectName, id, idsel)
{
   
   var xmlHttp = getXMLHTTPRequest();
	xmlHttp.open('get', 'formmod.php?id='+id+'&div='+selectName, false);
	xmlHttp.send(null);
	if(xmlHttp.status==200 || xmlHttp.status==0){
		var risultatoTxt = xmlHttp.responseText;
		str_nome_val=risultatoTxt.split('*----*');
		testo=str_nome_val[0].split('.;.');
		valore=str_nome_val[1].split('.;.');
		//alert(risultatoTxt);
	} else {
	alert('errore');
	}
	var sel = document.getElementsByName(selectName)[0];
   var tutti = Element.descendants(sel);
   
   for(i = 0; i < tutti.length; i++)
   {      
      sel.remove(tutti[i]);
   }
   for(i = 0; i < testo.length; i++)
   {
      var opt = document.createElement('option');
	  opt.value = valore[i];
      opt.appendChild(document.createTextNode(testo[i]));
      sel.appendChild(opt);
	}
	if(idsel!=''){
		sel.value=idsel;
	}
	
}

function maxzindex(){
	var div = document.getElementsByTagName('div');
	var maxzind=0;
	for (var i=0; i<div.length; i++)
	{
		if(div[i].style.position=='absolute')
			maxzind=(parseInt(div[i].style.zIndex)>maxzind)?parseInt(div[i].style.zIndex):maxzind;
	}
	return(maxzind);
}


function focusdiv(iddiv)
{
	var zind=document.getElementById(iddiv).style.zIndex
	document.getElementById(iddiv).style.zIndex=(zind==maxzindex() && zind!=0)?zind:maxzindex()+1;
	return true;
	}
	
function messaggio_salva(div)
	{
		document.getElementById(div).innerHTML='Cambiamenti non salvati';
	}
	
function apri_finestra(get, id_finestra, selettore, e)
{
	form_modifica(get, id_finestra+'_contenuto', selettore);
	if($(id_finestra+'_menu')!=null) form_modifica(get, id_finestra+'_menu', selettore+'_menu');
	if($(id_finestra+'_butt')!=null) form_modifica(get, id_finestra+'_butt', selettore+'_butt');
	openFormMouse(id_finestra, e);
	return id_finestra;
}

function carica_main(get, id_finestra, selettore)
{
	form_modifica(get, id_finestra+'_contenuto', selettore);
	if($(id_finestra+'_menu')!=null) form_modifica(get, id_finestra+'_menu', selettore+'_menu');
	if($(id_finestra+'_butt')!=null) form_modifica(get, id_finestra+'_butt', selettore+'_butt');
	return id_finestra;
}

function apriMenu(titolomenu){
	resetMenu();
	var disp=($(titolomenu+'_div').style.display=='none') ? 'block' : 'none';
	$(titolomenu+'_div').setStyle({
								  left: $(titolomenu).cumulativeOffset()[0]+'px',
								  top: $(titolomenu).cumulativeOffset()[1]+5+$(titolomenu).getDimensions().height+'px',
								  display: disp
								  });
}

function resetMenu()
{
		var menu=document.getElementsByClassName('menu');
	for(i=0; i<menu.length; i++)
	{
			menu[i].style.display='none';
	}
}
