/**
 * Copyright (c) 2008, Andrea La Rosa.
 *
 */
 
// GENERAL VARIBLES JS CORE
var _imgLoading='<img src="'+_urlSito+'js/img/ajax-loader.gif" title="loading" height="16" width="16"> Caricamento in corso...';

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 ====
*/
var enableCache = true;
var jsCache = new Array();
var DynObj = new Array();
function ajax(){
  this.xmlhttp=null;
  this.resetData=function(){
    this.metodo="POST";
    //this.queryStringSeparator="?";
    //this.argumentSeparator="&";
    this.URLString="";
    this.encodeURIString=true;
    this.execute=false;
    this.element=null;
    this.elementObj=null;
    this.requestFile= false;
    this.vars=new Object();
    this.responseStatus=new Array(2);
  };
  this.resetFunctions = function(){
    this.onLoading = function(){};
    this.onLoaded = function(){};
    this.onInteractive = function(){};
    this.onCompletion = function(){};
    this.onError = function(){};
    this.onFail = function(){};
  };
  this.reset = function(){
    this.resetFunctions();
    this.resetData();
  };
  this.crAjx = function(){
    try{this.xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");}
    catch(e1){
      try{this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
      catch(e2){this.xmlhttp=null;}
    }
    if(!this.xmlhttp){
      if(typeof XMLHttpRequest!="undefined"){this.xmlhttp=new XMLHttpRequest();}
      else{this.failed=true;}
    }
  };
  this.runResponse = function(){eval(this.response);};
  this.runAJAX = function(_a){
    if(this.failed){ this.onFail();}
    else{
      if(_a) this.URLString = _a;
      //this.createURLString(_a);
      //if(this.element){ this.elementObj=g(this.element); }
      if(this.xmlhttp){
        var _b=this;
        if(this.metodo=="GET"){
          //totalurlstring=this.requestFile+this.queryStringSeparator+this.URLString;
          //this.xmlhttp.open(this.metodo,totalurlstring,true);
          this.xmlhttp.open(this.metodo,this.requestFile,true);
        }else{
          this.xmlhttp.open(this.metodo,this.requestFile,true);
          try{
            // -- this.xmlhttp.setRequestHeader("Method", "POST "+this.requestFile+" HTTP/1.1");
            this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          }
          catch(e){ }
        }
        this.xmlhttp.onreadystatechange=function(){
          switch(_b.xmlhttp.readyState){
            case 1: _b.onLoading(); break;
            case 2: _b.onLoaded(); break;
            case 3: _b.onInteractive(); break;
            case 4:
              _b.response=replaceTxt(_b.xmlhttp.responseText);
              //_b.response=_b.xmlhttp.responseText;
              _b.responseXML=_b.xmlhttp.responseXML;
              _b.responseStatus[0]=_b.xmlhttp.status;
              _b.responseStatus[1]=_b.xmlhttp.statusText;
              if(_b.execute){ _b.runResponse(); }
              if(_b.elementObj){
                elemNodeName=_b.elementObj.nodeName;
                elemNodeName.toLowerCase();
                if(elemNodeName=="input"||elemNodeName=="select"||elemNodeName=="option"||elemNodeName=="textarea"){ _b.elementObj.value=_b.response;}
                else{ _b.elementObj.innerHTML=_b.response; }
              }
              if(_b.responseStatus[0]=="200"){ _b.onCompletion(); }
              else{ _b.onError(); }
              _b.URLString="";
            break;
          }
        };
        this.xmlhttp.send(this.URLString);
      }
    }
  };
  this.reset();
  this.crAjx();
}

function XHConn(){
  var xmlhttp, bComplete = false;
  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){
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET"){
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }else{
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete){
          bComplete = true; fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

/*
  AJAX ==== funzioni ====
*/
function ajaxShow(_d,_e,_f,_10,_11){
  if(_11=="appear"){ opacity(_d,0,101,600);}
  if(_11=="highlight"){ highLight(_d); }
  var _12=g(_d);
  _12.innerHTML=DynObj[_e].response;
  if(_11=="blind"){
    g(_d).style.position="";
    blindDown(_d);
  }
  if(enableCache){ jsCache[_f]=DynObj[_e].response; }
  DynObj[_e]=false;
  ajax_parseJs(_12);
}
function ajaxReplace(id,url){
  opacity(id,100,0,400);
  g(id).style.height="";
  fn="ajaxload('"+id+"','"+url+"',false,'appear',false,false)";
  setTimer(fn,400);
}
function ajaxLoad(id,url,vLoading,stile,vCache,sVarPostOf){
  if(stile=="appear"){changeOpac(0,id);}
  if(stile=="blind"){
    var ids=g(id).style;
    ids.overflow="hidden";
    ids.display="block";
    ids.height="0px";
  }
  if(vCache){
    if(enableCache&&jsCache[url]){
      if(stile=="appear"){ opacity(id,0,101,600); }
      if(stile=="highlight"){ highLight(id); }
      g(id).innerHTML=jsCache[url];
      if(stile=="blind"){
        g(id).style.position="";
        blindDown(id);
      }
      return;
    }
  }
  var _1b=DynObj.length;
  if(vLoading!=false){ g(id).innerHTML=_imgLoading; } // Loading ...
  DynObj[_1b]=new ajax();
  DynObj[_1b].requestFile=url;
  DynObj[_1b].onCompletion=function(){ ajaxShow(id,_1b,url,vLoading,stile); };
  if (sVarPostOf) var sVarPost = makeURLstring(sVarPostOf);
  else var sVarPost = '';
  DynObj[_1b].URLString = sVarPost ;
  DynObj[_1b].runAJAX();
}
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;
}
function ajax_parseJs(obj){
  var _1d=obj.getElementsByTagName("SCRIPT");
  var _1e="";
  var _1f="";
  for(var no=0;no<_1d.length;no++){
    if(_1d[no].src){
      var _21=document.getElementsByTagName("head")[0];
      var _22=document.createElement("script");
      _22.setAttribute("type","text/javascript");
      _22.setAttribute("src",_1d[no].src);
    }else{
      if(DHTMLSuite.clientInfoObj.isOpera){
        _1f=_1f+_1d[no].text+"\n";
      }else{
        _1f=_1f+_1d[no].innerHTML;
      }
    }
  }
  if(_1f){ ajax_installScript(_1f); }
}
function ajax_installScript(_c){
  if(!_c){return;}
  if(window.execScript){window.execScript(_c);}
  else{
    if(window.jQuery&&jQuery.browser.safari){STO(_c,0);}
    else{setTimer(_c,0);}
  }
}

/* ============ 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 displayMail(Server, Login, Display){
if ((Display.length == 0) || (Display.indexOf('@')+1)) {
document.write("<a href=" + "'mai" + "lto:" + Login + "@" + Server + "'>" + Login + "@" + Server + "</a>"); }
else {
document.write("<a href=" + "'mai" + "lto:" + Login + "@" + Server + "'>" + Display + "</a>"); }
}

/* ============ 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;
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();
}

/*
  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='asc'; 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,$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);
  }
}

/* ============ 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');
    }
	}
}
