/**
 * 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,speed){
  if(!speed){ speed=200;}
  acth=actHeight(id);
  if(acth==0){
    maxh=maxHeight(id);
    g(id).style.display="block";
    g(id).style.height="0px";
    var speedmath=Math.ceil(speed/maxh);
    for(i=1;i<=maxh;i++){ setTimer("g('"+id+"').style.height='"+i+"px'",speedmath*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,250);
  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();
}


