// JavaScript Document

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: 0.6 });
	}
	else
	{
		Effect.BlindUp(id2, { duration: 0.6 });
	}
}

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: 'chiudi', 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, 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='+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){
	return apertura_pagina(id, iddiv, '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){
		j++;
		if(dir>0)
			document.getElementById(div).scrollTop=document.getElementById(div).scrollTop+spost;
		else
			document.getElementById(div).scrollTop=document.getElementById(div).scrollTop-spost;
		if(j<61)
		{
			spostamento=Math.round(a*(Math.pow(Math.sin(j*Math.PI/60),2)));
			setTimeout("scorri(\'"+div+"\', "+spostamento+", "+a+", "+dir+", "+j+");", 20);
		}
}

function scrolling(div, dir)
{
	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
	scorri(div, 0, ampiezza, dir, j);
}

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 slideshowf13(tt, banner){
		//lo script, crea due div all'interno del div banner e li mostra alternati. Prima di mostrare nuovamente il div, ne cambia lo sfondo
// tt č il tempo di transizione
	 var contenitore=document.getElementById(banner);
	 var altezza = contenitore.getHeight();
	 var larghezza=contenitore.getWidth();
	 
	var img=contenitore.getElementsByTagName('img');
	//contenitore.innerHTML="";
	var newNode1 = document.createElement("div");
   	newNode1.setAttribute("id", "foto2");
    //newNode.setAttribute("class", "banner");
    newNode1.setAttribute("style", "height:"+altezza+"px; width:"+larghezza+"px; position: relative; left:0; top:0; display: none; background-position: center;");
    contenitore.appendChild(newNode1);
	contenitore.style.background='url('+img[0].src+') no-repeat';
	contenitore.style.backgroundPosition="center";
	muovi_sfondo(banner, tt,  50, 50, Math.random()*1*((Math.random()>0.5) ? 1 : -1), Math.random()*1*((Math.random()>0.5) ? 1 : -1), 0);
	cambiaimg('foto2', banner, 1, tt);
								  
			return 0;
}

function cambiaimg(obj1, contenitore, pass, tt) {
		
		var foto1=document.getElementById(obj1);
		var cont=document.getElementById(contenitore);
		var img=cont.getElementsByTagName('img');
		foto1.style.background='url('+img[pass].src+') no-repeat';
		foto1.style.backgroundPosition="center";
		setTimeout('Effect.Appear(\'foto2\', { duration: 1 })', tt);
		setTimeout('muovi_sfondo(\'foto2\', '+tt+', 50, 50, '+Math.random()*1*((Math.random()>0.5) ? 1 : -1)+', '+Math.random()*1*((Math.random()>0.5) ? 1 : -1)+', 0)', tt);
		setTimeout('muovi_sfondo(\''+contenitore+'\', '+tt+', 50, 50, '+Math.random()*1*((Math.random()>0.5) ? 1 : -1)+', '+Math.random()*0.5*((Math.random()>0.5) ? 1 : -1)+', 0)', 2*tt);
		if(pass==img.length-1) pass=0; else pass++;
//		ass=(pass==img.length-1) ? 0 : pass+1;
		setTimeout('document.getElementById(\''+contenitore+'\').style.background=\'url('+img[pass].src+') no-repeat\'; document.getElementById(\''+contenitore+'\').style.backgroundPosition=\'center\';', tt+1000);
		setTimeout('Effect.Fade(\'foto2\', { duration: 1 })', 2*tt);
		if(pass==img.length-1) pass=0; else pass++;
		//pass= ? 0 : pass+1;
		setTimeout('cambiaimg(\''+obj1+'\', \''+contenitore+'\', '+pass+', '+tt+')', 2*tt+1000);
		return 0;
}

function muovi_sfondo(obj1, tt, posx, posy, incx, incy, passo) {
		var foto1=document.getElementById(obj1);
		var npass=tt/1000*50;
		posx=posx+incx;
		posy=posy+incy;
		foto1.style.backgroundPosition=posx+"% "+posy+"%";
		passo++;
		if(passo<npass)
			setTimeout('muovi_sfondo(\''+obj1+'\', '+tt+', '+posx+', '+posy+', '+incx+', '+incy+', '+passo+')', 1000/25);
}
