/**
 * Copyright (c) 2008, Andrea La Rosa.
 *
 */

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

function g(id){
if(document.getElementById){return document.getElementById(id);}
else if(document.all){return document.all[id];}
else if(document.layers){return document.layers[id];}
}

/*
  text ====
*/
function DecToHexa($v){var $newV=parseInt($v).toString(16); if($v<16){$newV="0"+$newV;}return $newV;}
function replaceTxt($str){
//str=str.replace(/&#39;/g,"´"); return str;
$str=$str.replace(/&#39;/g,"&lsquo;"); return $str;
}
function addslashes($str){
  $str=$str.replace(/\'/g,'\\\'');
  $str=$str.replace(/\"/g,'\\"');
  $str=$str.replace(/\\/g,'\\\\');
  $str=$str.replace(/\0/g,'\\0');
  return $str;
}
function stripslashes($str){
  $str=$str.replace(/\\'/g,'\'');
  $str=$str.replace(/\\"/g,'"');
  $str=$str.replace(/\\\\/g,'\\');
  $str=$str.replace(/\\0/g,'\0');
  return $str;
}

/*
  morphing ====
*/
function setTimer($fn,$time){return window.setTimeout($fn,$time);}
function actHeight($id){
var $h=g($id).clientHeight; if($h==0){$h=g($id).offsetHeight;} return $h;
}
function actWidth(id){
  width=g(id).clientWidth;
  if(width==0){ width=g(id).offsetWidth; }
  return width;
}
function maxHeight($id){
  var $ids=g($id).style;
  $ids.overflow="hidden";
  if(actHeight($id)!=0){return actHeight($id);}
  else{
    origdisp=ids.display;
    origheight=ids.height;
    origpos=ids.position;
    origvis=ids.visibility;
    ids.visibility="hidden";
    ids.height="";
    ids.display="block";
    ids.position="absolute";
    height=actHeight(id);
    ids.display=origdisp;
    ids.height=origheight;
    ids.position=origpos;
    ids.visibility=origvis;
    return height;
  }
}
function blindUp(id,_2f){
  if(!_2f){ _2f=200; }
  acth=actHeight(id);
  maxh=maxHeight(id);
  if(acth==maxh){
    g(id).style.display="block";
    var _30;
    _30=Math.ceil(_2f/acth);
    for(i=0;i<=acth;i++){
      newh=acth-i;
      setTimer("g('"+id+"').style.height='"+newh+"px'",_30*i);
    }
  }
}
function blindDown($id,_32){
  if(!_32){ _32=200;}
  acth=actHeight($id);
  if(acth==0){
    maxh=maxHeight($id);
    g($id).style.display="block";
    g($id).style.height="0px";
    var _33; _33=Math.ceil(_32/maxh);
    for(i=1;i<=maxh;i++){ setTimer("g('"+$id+"').style.height='"+i+"px'",_33*i); }
  }
}
function toggle(id)
{
  if(actHeight(id)==0){blindDown(id);}
  else{blindUp(id);}
}
function opacity(id,_35,_36,_37){
  //if(g(id).style.width==0){ g(id).style.width=actWidth(id); }
  var _38=Math.round(_37/100);
  var _39=0;
  if(_35>_36){
    for(i=_35;i>=_36;i--){
      setTimer("changeOpac("+i+",'"+id+"')",(_39*_38));
      _39++;
    }
  }else{
    if(_35<_36){
      for(i=_35;i<=_36;i++){
        setTimer("changeOpac("+i+",'"+id+"')",(_39*_38));
        _39++;
      }
    }
  }
}
function pulsate(id,num,speed){
  if(!speed) speed=300;
  for(i=1;i<=num;i++){
    numx=i*((speed*2)+100)-(speed*2);
    setTimer("opacity('"+id+"', 100, 0, "+speed+")",numx);
    setTimer("opacity('"+id+"', 0, 100, "+speed+")",numx+speed+100);
  }
}
function changeOpac(valore,id){
  var ids=g(id).style;
  ids.opacity=(valore/100);
  ids.MozOpacity=(valore/100);
  ids.KhtmlOpacity=(valore/100);
  ids.filter="alpha(opacity="+valore+")";
}
function shiftOpacity(id,_3e){
  if(g(id).style.opacity<0.5){ opacity(id,0,100,_3e); }
  else{ opacity(id,100,0,_3e); }
}
function currentOpac(id,_40,_41){
  var _42=100;
  if(g(id).style.opacity<100){ _42=g(id).style.opacity*100; }
  opacity(id,_42,_40,_41);
}
function highLight(id,_44,_45,_46){
  if(_44){ milli=_44; }
  else{ milli=900; }
  if(_45){ endcol=_45; }
  else{ endcol="#FFFFFF"; }
  if(_46){ origcol=_46;}
  else{ origcol="#FFFFA6"; }
  colorize(origcol,endcol,id,milli,"high");
}
function textColor(id,_48,_49,_4a){
  if(_4a){ milli=_4a; }
  else{ milli=900; }
  colorize(_48,_49,id,milli,"text");
}
function morphColor(id,_4c,_4d,_4e,_4f,_50,_51,_52){
  if(_52){ milli=_52; }
  else{ milli=900; }
  colorize(_4c,_4d,id,milli,"text");
  colorize(_4e,_4f,id,milli,"back");
  if(_50!=false){ colorize(_50,_51,id,milli,"border"); }
}
function colorize(_53,end,id,_56,_57){
  dr=parseInt(_53.substring(1,3),16);
  dg=parseInt(_53.substring(3,5),16);
  db=parseInt(_53.substring(5,7),16);
  fr=parseInt(end.substring(1,3),16);
  fg=parseInt(end.substring(3,5),16);
  fb=parseInt(end.substring(5,7),16);
  steps=_56/10;
  cr=dr; cg=dg; cb=db;
  sr=(fr-dr)/steps;
  sg=(fg-dg)/steps;
  sb=(fb-db)/steps;
  var zzi=10;
  for(var x=0;x<steps;x++){
    color="#"+DecToHexa(cr)+DecToHexa(cg)+DecToHexa(cb);
    if(x==(steps-1)){
      if(_57=="high"){color="";}else{color=end;}
    }
    mytime=(x);
    if(_57=="back"||_57=="high"){ newfonc="g(\""+id+"\").style.backgroundColor=\""+color+"\";"; }
    else{
      if(_57=="text"){ newfonc="g(\""+id+"\").style.color=\""+color+"\";"; }
      else{
        if(_57=="border"){ newfonc="g(\""+id+"\").style.borderColor=\""+color+"\";"; }
      }
    }
    setTimer(newfonc,zzi);
    cr+=sr; cg+=sg; cb+=sb; zzi+=10;
  }
}
/*
  AJAX ===== core ====
*/

function XHConn(){
  var xmlhttp;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone,fnError){
    if (!xmlhttp) return false;
    sMethod = sMethod.toUpperCase();
    var sTime = new Date().getTime();
    try {
      if (sMethod == "GET"){
        sVars = sVars ? '/'+sVars : '' ;
        xmlhttp.open(sMethod, sURL+sVars+'/stime_'+sTime+_html, true);
        sVars='';
      }else{
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+sTime+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", sVars.length);
        xmlhttp.setRequestHeader("Connection", "close");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4){
          if(xmlhttp.status == 200){ fnDone(xmlhttp); }else{ fnError(xmlhttp); }
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

/*
  AJAX ==== funzioni ====
*/

function ajaxShow(id,obj,stile){
  if(stile=="appear"){ opacity(id,0,101,400);}
  if(stile=="highlight"){ highLight(id); }
  var gId=g(id);
  gId.innerHTML=obj.responseText;
  if(stile=="blind"){ gId.style.position=""; blindDown(id); }
}

function ajaxLoad(id,url,stile){
  if(stile=="appear"){changeOpac(0,id);}
  if(stile=="blind"){ var ids=g(id).style; ids.overflow="hidden"; ids.display="block"; ids.height="0px"; }
  var ajax = new XHConn();
  if(ajax){
    var fnCompleto = function(obj){ ajaxShow(id,obj,stile); }
    var fnErrore = function(){ return true; }
    //var fnErrore = function (obj) { alert("HTTP error: "+obj.status); }
    ajax.connect(url,"GET",false,fnCompleto,fnErrore);
    return false;
  }else{
    return true;
  }
}

function ajaxReplace(id,url){
  opacity(id,100,0,200);
  g(id).style.height="";
  fn="ajaxLoad('"+id+"','"+url+"','appear')";
  setTimer(fn,400);
  return false;
}

function makeURLstring(nomeForm){
  var s; var form=document.forms[nomeForm];
  var nEl=form.elements.length;
  for(var i=0;i<nEl;i++){
      s+=form.elements[i].name+"="+encodeURIComponent(form.elements[i].value); if(i<nEl-1){s +="&";}
  }
  return s;
}

/* ============ vari = */

function trim(str){str = str.replace(/&nbsp;$/g, ' '); return str.replace(/^\s*|\s*$/g,'');}
function cambiaSelect(p){this.location.href=p;} // html -> <select onchange="cambiaSelect(this.value)">
function redirectto(newurl){ if(newurl!="") location.href=newurl; }

function changecategory(newcat){g('action').value="change"; g('form').submit();}
function startUpload(){g('loading').style.display='block'; return true;}
/* aperture */
function enlarge(id){var el=g(id); if (el.className=="acceso"){el.className="spento";}else{ el.className="acceso"; } }
function apri(id){g(id).className="acceso";}
function chiudi(id){g(id).className="spento";}
function openMenu(id){for(i=1;i<=qMenu;i++){g(i).className="spento"; if(id==i){g(id).className = "acceso";}}}
function showBoxForm(id){
  var el = g(id); var elH=g('hidden_'+id); var elL=g('link_'+id);
  if(el.className=="acceso"){act="spento";}else{act="acceso";}
  el.className=act; elL.className='arrow_'+act; elH.value=act;
}
function tastoInvia($e){ var $key;
  if(window.event){$key = $e.keyCode;} /* Per IE */
  else if($e.which){$key = $e.which;} /* PEr Firefox/Netscape/Opera */
  if($key==13){g("loginForm").submit();}
}
function checkTutti(id,idc){with(g(id)){for(var i=0; i<elements.length; i++){if(elements[i].type=='checkbox' && elements[i].name==idc+"[]"){elements[i].checked=true;}}}}
function uncheckTutti(id,idc){with(g(id)){for(var i=0; i<elements.length; i++){if(elements[i].type=='checkbox' && elements[i].name==idc+"[]"){elements[i].checked=false;}}}}

/* ============ loadlist = */

// Ritorna il valore dell'elemento <option> selezionato in una lista
function getSelected(select){return select.options[select.selectedIndex].value;} 

function addOption(select,value,text){
	var option = document.createElement("option");
	option.value = value, option.text = text;
	try { select.add(option, null); }
  catch(e) { select.add(option); }// Per Internet Explorer
}

function loadList(url,obg){
  var _n=DynObj.length;
  DynObj[_n] = new ajax();
  DynObj[_n].requestFile = url+'QVobg_'+obg+'.html';
  DynObj[_n].metodo = 'GET';
  DynObj[_n].onCompletion = function(){
    var resp = DynObj[_n].response;
    if(resp){
    	var values = resp.split(';'); // Le coppie di valori nella striga di risposta sono separate da ;
    	var listId = values.shift(); // Il primo elemento è l'ID della lista.
    	var select = g(listId);
    	while (select.options.length) { select.remove(0); } // Elimina i valori precedenti
    	addOption(select, '', '----', 0);
    	var limit = values.length;
    	for(i=0; i < limit; i++){
    		var pair = values[i].split('|');
    		addOption(select, pair[0], pair[1]);
    	}
    }
  };
  DynObj[_n].runAJAX();
}

function loadListMulti(url,obg,tab){
  var _n=DynObj.length;
  DynObj[_n] = new ajax();
  DynObj[_n].requestFile = url+'/tab_'+tab+'/obg_'+obg;
  DynObj[_n].metodo = 'GET';
  DynObj[_n].onCompletion = function(){
    var resp = DynObj[_n].response;
    if(resp){ 
      var values=resp.split('|'); 
      var lista=values[0]; 
      var box=g('from-'+obg+'-'+lista);
      box.innerHTML=values[1];
    }
  };
  DynObj[_n].runAJAX();
}

/*
  sortable ============
*/

var table=function(){
	function sorter(n){
		this.n=n; this.t; this.b; this.r; this.d; this.p; this.w; this.a=[]; this.l=0
	}
	sorter.prototype.init=function(t,f){
		this.t=document.getElementById(t);
		this.b=this.t.getElementsByTagName('tbody')[0];
		this.r=this.b.rows; var l=this.r.length;
		for(var i=0;i<l;i++){
			if(i==0){
				var c=this.r[i].cells; this.w=c.length;
				for(var x=0;x<this.w;x++){
					if(c[x].className!='nosort'){
						c[x].className='head';
						c[x].onclick=new Function(this.n+'.work(this.cellIndex)')
					}
				}
			}else{
				this.a[i-1]={}; this.l++;
			}
		}
		if(f!=null){
			var a=new Function(this.n+'.work('+f+')'); a()
		}
	}
	sorter.prototype.work=function(y){
		this.b=this.t.getElementsByTagName('tbody')[0]; this.r=this.b.rows;
		var x=this.r[0].cells[y],i;
		for(i=0;i<this.l;i++){
			this.a[i].o=i+1; var v=this.r[i+1].cells[y].firstChild;
			this.a[i].value=(v!=null)?v.nodeValue:''
		}
		for(i=0;i<this.w;i++){
			var c=this.r[0].cells[i];
			if(c.className!='nosort'){c.className='head'}
		}
		if(this.p==y){
			this.a.reverse(); x.className=(this.d)?'asc':'desc';
			this.d=(this.d)?false:true
		}else{
			this.p=y; this.a.sort(compare); x.className='desc'; this.d=false
		}
		var n=document.createElement('tbody');
		n.appendChild(this.r[0]);
		for(i=0;i<this.l;i++){
			var r=this.r[this.a[i].o-1].cloneNode(true);
			n.appendChild(r); r.className=(i%2==0)?'even':'odd'
		}
		this.t.replaceChild(n,this.b)
	}
	function compare(f,c){
		f=f.value,c=c.value;
		var i=parseFloat(f.replace(/(\$|\,)/g,'').replace(/\-/g,'').replace(/ /g,'').replace(/\:/g,'')),n=parseFloat(c.replace(/(\$|\,)/g,'').replace(/\-/g,'').replace(/ /g,'').replace(/\:/g,''));
		if(!isNaN(i)&&!isNaN(n)){f=i,c=n}
		return (f<c?1:(f>c?-1:0))
	}
	return{sorter:sorter}
}();

/* ============ fiframe = */

function createIframeUpload(fName,tipo,caso,maxFile,nDrag){
  var iframeUpload=g('iframeUpload-'+fName);
  iframeUpload.innerHTML='&nbsp;';
  var newIframe=document.createElement('iframe');
  newIframe.setAttribute('id','uploadIframe-'+tipo);
  //newIframe.setAttribute('class','iframeFile');
  newIframe.src=_urlSito+'if-upload.php?f='+fName+'&t='+tipo+'&c='+caso+'&m='+maxFile+'&d='+nDrag;
  newIframe.frameBorder="0";
  iframeUpload.replaceChild(newIframe,iframeUpload.childNodes[0]);
}
function delDragList(divDrag,divDel){
  var $divDrag=g(divDrag),$divDel=g(divDel);
  $divDrag.removeChild($divDel);
}
function delUpload($divDrag,$fName,$idDel){
  var $divDel = 'upfile-'+$fName+'_'+$idDel;
  delDragList($divDrag,$divDel);
  var $delDiv = g('del-'+$fName);
  var $inpid = window.document.createElement("input");
  $inpid.type = "hidden"; $inpid.name = "fileDel-"+$fName+"[]"; $inpid.value = $idDel;
  $delDiv.appendChild($inpid);
}
/*
function delUpload($divDrag,$fName,$idDel,$ses){
  var $divDel = 'upfile-'+$fName+'_'+$idDel;
  delDragList($divDrag,$divDel);
  if($ses){
    var $delDiv = g('del-'+$fName);
    var $inpid = window.document.createElement("input");
    $inpid.type = "hidden"; $inpid.name = "fileDel-"+$fName+"[]"; $inpid.value = $idDel;
    var $inpidStep = window.document.createElement("input");
    $inpidStep.type = "hidden"; $inpidStep.name = "sesDel-"+$fName+"[]"; $inpidStep.value = $ses;
    $delDiv.appendChild($inpid);
    $delDiv.appendChild($inpidStep);
  }
}
*/
/* Script by: www.jtricks.com
 * Version: 20070301
 * Latest version:
 * www.jtricks.com/javascript/window/box_alone.html
 */
 
// Moves the box object to be directly beneath an object.
function move_box(an,box)
{
    var cleft = 0; var ctop = 0; var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    box.style.top = ctop + 'px';
}

// Hides other alone popup boxes that might be displayed
function hide_other_alone(obj)
{
    if (!document.getElementsByTagName)
        return;

    var all_divs = document.body.getElementsByTagName("DIV");

    for (i = 0; i < all_divs.length; i++)
    {
        if (all_divs.item(i).style.position != 'absolute' ||
            all_divs.item(i) == obj ||
            !all_divs.item(i).alonePopupBox)
        {
            continue;
        }

        all_divs.item(i).style.display = 'none';
    }
    return;
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function show_hide_box(an, width, height, borderStyle)
{
    var href = an.href;
    var boxdiv = document.getElementById(href);

    if (boxdiv != null)
    {
        if (boxdiv.style.display=='none')
        {
            hide_other_alone(boxdiv);
            // Show existing box, move it
            // if document changed layout
            move_box(an, boxdiv);
            boxdiv.style.display='block';

            // Workaround for Konqueror/Safari
            if (!boxdiv.contents.contentWindow)
                boxdiv.contents.src = href;
        }
        else
            // Hide currently shown box.
            boxdiv.style.display='none';
        return false;
    }

    hide_other_alone(null);

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    // Add object identification variable
    boxdiv.alonePopupBox = 1;

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = borderStyle;
    boxdiv.style.textAlign = 'right';
    boxdiv.style.padding = '5px';
    boxdiv.style.background = '#FFFFFF';
    document.body.appendChild(boxdiv);

    var offset = 0;

    // Remove the following code if 'Close' hyperlink
    // is not needed.
    var close_href = document.createElement('a');
    close_href.href = 'javascript:void(0);';
    close_href.onclick = function()
        { show_hide_box(an, width, height, borderStyle); }
    close_href.appendChild(document.createTextNode('<< Nascondi'));
    boxdiv.appendChild(close_href);
    offset = close_href.offsetHeight;
    // End of 'Close' hyperlink code.

    var contents = document.createElement('iframe');
    //contents.scrolling = 'no';
    contents.overflowX = 'hidden';
    contents.overflowY = 'scroll';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = (height - offset) + 'px';

    boxdiv.contents = contents;
    boxdiv.appendChild(contents);

    move_box(an, boxdiv);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
}

/* ============ showImg = */

function showImg($divIco,$divGallery) {
  var getImg=g($divGallery).getElementsByTagName('div');
  var getIco=g($divIco).getElementsByTagName('b');
  var getN=getIco.length;
	for (var i=0; i<getN; i++) {
		if (getIco[i].className=='switch') {
      getIco[i].onclick = function () {
 				for (var z=0; z<getN; z++) {
   				getImg[z].className=getImg[z].className.replace('show','hide');
   				getIco[z].className=getIco[z].className.replace('switch over', 'switch');
   				getIco[z].className=getIco[z].className.replace('switch off', 'switch over');
 				}
        this.className = this.className=='switch' ? 'switch off' : 'switch';
        var num='no'; for (var x=0; x<getN; x++) { if(getIco[x].className=='switch off'){ var num=x;} }
    		if(num!='no'){ getImg[num].className='show' ;}
      }
		}
		if(i==0){
      getIco[i].className = getIco[i].className.replace('switch','switch off');
    }
	}
}

