// Common

var d=document;
var BASE_URL='http://www.ecostyle24.it/';
var ADMIN_BASE_URL='http://www.ecostyle24.it/admin_new/';
var ADMIN_IMAGES_URL=ADMIN_BASE_URL+'images/';

var bgMBGreen='#006600';
var bgMBYellow='#FFEE99';
var bgMBRed='#FF6C6C';
var bgMBGray='#666666';

var id=0;
var ids='';

function removeAllChild(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  while(obj.firstChild)
    {obj.removeChild(obj.firstChild);}
}

function setEvent(obj,ev,fnc)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(BROWSER=='Internet Explorer')
    {obj[ev]=new Function(fnc);}
  else
    {obj.setAttribute(ev,fnc);}
}

var timeouts=new Array();

function setVisibility(e,f,v,fd,af,at,oc)
{
  if(typeof(e)=='string')
    {e=d.getElementById(e);}

  if(e)
  {
    if(!fd || noFade)
    {
      if(f=='visibility')
        {e.style.visibility=v;}
      else if(f=='display')
        {e.style.display=v;}

      if(typeof(oc)=='function')
        {oc();}
    }
    else
    {
      if(!(af>=0) || !(at>=0))
      {
        if((f=='visibility' && v=='hidden') || (f=='display' && v=='none'))
        {
          af=100;
          at=0;
        }
        else
        {
          af=0;
          at=100;
        }
      }


      setVisibilityFade(e,f,v,af,at,oc);
    }
  }
}

function  setVisibilityFade(e,f,v,af,at,oc)
{
  if(typeof(e)=='string')
    {e=d.getElementById(e);}

  if(e)
  {
    if((f=='visibility' && v=='hidden') || (f=='display' && v=='none'))
    {
      if(!timeouts[e.id])
      {
        timeouts[e.id]=new Array();
        timeouts[e.id]['opacity']=af;
        if(BROWSER=='Internet Explorer')
          {e.style.filter='alpha(opacity='+(timeouts[e.id]['opacity'])+')';}
        else
          {e.style.opacity=(timeouts[e.id]['opacity']/100);}
        timeouts[e.id]['eventId']=window.setTimeout("setVisibilityFade('"+e.id+"','"+f+"','"+v+"','"+af+"','"+at+"')",5);
        if(typeof(oc)=='function')
          {timeouts[e.id]['oc']=oc;}
      }
      else
      {
        window.clearTimeout(timeouts[e.id]['eventId']);
        if(timeouts[e.id]['opacity']>at)
        {
          if(timeouts[e.id]['opacity']>=85)
            {timeouts[e.id]['opacity']-=5;}
          else if(timeouts[e.id]['opacity']>=60)
            {timeouts[e.id]['opacity']-=10;}
          else if(timeouts[e.id]['opacity']>=40)
            {timeouts[e.id]['opacity']-=15;}
          else
            {timeouts[e.id]['opacity']-=25;}

          if(timeouts[e.id]['opacity']<0)
            {timeouts[e.id]['opacity']=0;}

          if(BROWSER=='Internet Explorer')
            {e.style.filter='alpha(opacity='+(timeouts[e.id]['opacity'])+')';}
          else
            {e.style.opacity=(timeouts[e.id]['opacity']/100);}
          timeouts[e.id]['eventId']=window.setTimeout("setVisibilityFade('"+e.id+"','"+f+"','"+v+"','"+af+"','"+at+"')",5);
        }
        else
        {
          setVisibility(e.id,f,v);
          if(typeof(timeouts[e.id]['oc'])=='function')
            {timeouts[e.id]['oc']();}
          delete timeouts[e.id];
        }
      }
    }
    else
    {
      if(!timeouts[e.id])
      {
        timeouts[e.id]=new Array();
        timeouts[e.id]['opacity']=af;
        if(BROWSER=='Internet Explorer')
          {e.style.filter='alpha(opacity='+(timeouts[e.id]['opacity'])+')';}
        else
          {e.style.opacity=(timeouts[e.id]['opacity']/100);}
        setVisibility(e.id,f,v);
        timeouts[e.id]['eventId']=window.setTimeout("setVisibilityFade('"+e.id+"','"+f+"','"+v+"','"+af+"','"+at+"')",5);
      }
      else
      {
        window.clearTimeout(timeouts[e.id]['eventId']);
        if(timeouts[e.id]['opacity']<at)
        {
          if(timeouts[e.id]['opacity']>=70)
            {timeouts[e.id]['opacity']+=5;}
          else if(timeouts[e.id]['opacity']>=40)
            {timeouts[e.id]['opacity']+=10;}
          else
            {timeouts[e.id]['opacity']+=15;}

          if(timeouts[e.id]['opacity']>100)
            {timeouts[e.id]['opacity']=100;}

          if(BROWSER=='Internet Explorer')
            {e.style.filter='alpha(opacity='+(timeouts[e.id]['opacity'])+')';}
          else
            {e.style.opacity=(timeouts[e.id]['opacity']/100);}
          timeouts[e.id]['eventId']=window.setTimeout("setVisibilityFade('"+e.id+"','"+f+"','"+v+"','"+af+"','"+at+"')",5);
        }
        else
          {delete timeouts[e.id];}
      }
    }
  }
}

function clearTimeouts()
{
  for(var i=0;i<timeouts.length;i++)
    {window.clearTimeout(timeouts[i]['eventId']);}
  timeouts=new Array();
}

function getVisibility(e,f)
{
  if(typeof(e)=='string')
    {e=d.getElementById(e);}

  if(e)
  {
    if(f=='visibility')
      {return e.style.visibility;}
    else if(f=='display')
      {return e.style.display;}
  }
}

function getXPos(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(obj)
  {
    var curleft=0;
    if(obj.offsetParent)
    {
      while(obj.offsetParent)
      {
        curleft+=obj.offsetLeft
        obj=obj.offsetParent;
      }
    }
    else if (obj.x)
      curleft+=obj.x;
    return curleft;
  }
  else
    {return -1;}
}

function getYPos(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(obj)
  {
    var curtop=0;
    if(obj.offsetParent)
    {
      while(obj.offsetParent)
      {
        curtop+=obj.offsetTop
        obj=obj.offsetParent;
      }
    }
    else if (obj.y)
      curtop+=obj.y;
    return curtop;
  }
  else
    {return -1;}
}

function getInnerWindowWidth()
{
  if (!window.innerWidth)
    {return d.documentElement.clientWidth;}
  else
    {return window.innerWidth;}
}

function getInnerWindowHeight()
{
  if(!window.innerHeight)
    {return d.documentElement.clientHeight;}
  else
    {return window.innerHeight;}
}

function windowCenterX()
{
  if(d.documentElement.scrollLeft>=0)
    {return getInnerWindowWidth()/2+d.documentElement.scrollLeft;}
  else
    {return getInnerWindowWidth()/2+window.pageXOffset;}
}

function windowCenterY()
{
  return getInnerWindowHeight()/2;
}

function pageScrollTop()
{
  if(d.documentElement.scrollTop>=0)
    {return d.documentElement.scrollTop;}
  else
    {return window.pageYOffset;}
}

function getMaxChildWidth(obj,tagname)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(obj)
  {
    var max=0;

    for(var i=0;i<obj.childNodes.length;i++)
    {
      if(!tagname)
        {max=(obj.childNodes[i].offsetWidth>max)?obj.childNodes[i].offsetWidth:max;}
      else if(typeof(tagname)=='string')
      {
        if(obj.childNodes[i].tagName==tagname)
          {max=(obj.childNodes[i].offsetWidth>max)?obj.childNodes[i].offsetWidth:max;}
      }
      else if(tagname.indexOf(obj.childNodes[i].tagName)!=-1)
        {max=(obj.childNodes[i].offsetWidth>max)?obj.childNodes[i].offsetWidth:max;}
    }

    return max;
  }
  else
    {return -1;}
}

function getMaxWidthByTagName(tn)
{
  coll=d.getElementsByTagName(tn);
  var max=0;
  for(var i=0;i<coll.length;i++)
    {max=(coll[i].offsetWidth>max)?coll[i].offsetWidth:max;}

  return max;
}

var minWindowWidth=350;
var minWindowHeight=250;
var maxWindowWidth=window.screen.availWidth-120;
var maxWindowHeight=window.screen.availHeight-200;

function resizeWindowToContent()
{
  var coll=d.getElementsByTagName('iframe');
  if(coll.length>0)
  {
    minWindowWidth=800;
    minWindowHeight=maxWindowHeight;
  }

  if(action=='detail' || action=='insert' || action=='edit' || action=='multi_edit' || action=='search' || action=='search_ext')
    {var w=getMaxWidthByTagName('TABLE');}
  else if(action=='image' || action=='file')
    {var w=minWindowWidth;}
  else if(action=='structure')
    {var w=minWindowWidth;}
  else
    {var w=getMaxWidthByTagName('TABLE');}

  var h=d.getElementById('dBody').offsetHeight;

  w=(w<=maxWindowWidth)?w:maxWindowWidth;
  h=(h<=maxWindowHeight)?h:maxWindowHeight;
  w=(w>minWindowWidth)?w:minWindowWidth;
  h=(h>minWindowHeight)?h:minWindowHeight;

  if(!window.innerWidth)
    {resizeTo(w+100,h+100);}
  else
    {resizeTo(w+75,h+100);}
}

function resizeIframe()
{
  var coll=d.getElementsByTagName('iframe');
  for(var i=0;i<coll.length;i++)
  {
    if(coll[i].className=='childIframeClose' || coll[i].className=='childIframeOpen')
    {
      coll[i].style.width=(getInnerWindowWidth()-40)+'px';
    }
  }
}

function showIframe(auto)
{
  ifr=window.parent.d.getElementById(window.name);
  img=d.getElementById('showChildIframeImg');
  if(ifr)
  {
    XHDataSubmit=createXMLHttpRequest();
    XHDataSubmit.onreadystatechange=XHDataSubmitCallback;
    showLoadingOverlay();

    if(ifr.className=='childIframeClose' || (auto && IFRAME && !IFRAME_CLOSE))
    {
      ifr.className='childIframeOpen';
      img.src=ADMIN_IMAGES_URL+'href_top_32.png';

      XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=write_module_settings&sm='+modulo+'&sn=iframe_close&sv=0',true);
    }
    else
    {
      ifr.className='childIframeClose';
      img.src=ADMIN_IMAGES_URL+'href_bottom_32.png';

      XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=write_module_settings&sm='+modulo+'&sn=iframe_close&sv=1',true);
    }
    XHDataSubmit.send(null);
  }
}

function loadArray(s,d)
{
  return s.split(d);
}

function showLoadingOverlay()
{
  showOverlayFullScreen('loadingOverlay');
  setVisibility('loadingOverlay','visibility','visible',true,0,80);
}

function hideLoadingOverlay()
{
  setVisibility('loadingOverlay','visibility','hidden',true,80,0,hideOverlayFullScreen('loadingOverlay'));
}

function showOverlayFullScreen(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(obj)
  {
    obj.style.width='100%';
    obj.style.height='100%';
  }
}

function hideOverlayFullScreen(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(obj)
  {
    obj.style.width='0px';
    obj.style.height='0px';
  }
}

var rightClick=false;

function catchClick(e)
{
  if(!e)
    {var e=window.event;}

	if(e.which)
    {rightClick=(e.which==3);}
	else if(e.button)
    {rightClick=(e.button==2);}
  else
    {rightClick=false;}

  if(dragDropEnabled)
    {return false;}
  else
    {return true;}
}

function disableClick(e)
{
  if(!e)
    {var e=window.event;}

  if(dragDropEnabled)
  {
    if(e.which)
      {return !(e.which==3);}
    else if(e.button)
      {return !(e.button==2);}
  }

  return true;
}

function setMouseClickEvents()
{
  window.onmousedown=catchClick;
  window.onmouseup=disableClick;
  window.onclick=disableClick;
  window.ondblclick=disableClick;

  if(BROWSER=='Internet Explorer')
  {
    d.onmousedown=catchClick;
    d.onmouseup=disableClick;
    d.oncontextmenu=new Function('return !dragDropEnabled;');
  }
}

function onLoad()
{
  getBrowser();
  hideLoadingOverlay();
  if(d.getElementById('hideMenuImg'))
  {
    var frameset=window.top.document.getElementById('frameset');
    if(frameset)
      if(frameset.cols=='0,*')
        {d.getElementById('hideMenuImg').src=ADMIN_IMAGES_URL+'menu_frame_show.png';}
  }
  if(action=='')
    {setMouseClickEvents();}

  if(IFRAME)
    {showIframe(true);}

  enableScreenSaverTimer();
  if(typeof(onLoadModule)=='function')
    {onLoadModule();}
}

function onUnload()
{
  showLoadingOverlay();
  if(typeof(onUnloadModule)=='function')
    {onUnloadModule();}
}

var screenSaverTimeoutId;

function enableScreenSaverTimer()
{
  if(screenSaverTimeout>0)
    {screenSaverTimeoutId=window.setTimeout("screenSaverLogout()",screenSaverTimeout*1000);}
}

function disableScreenSaverTimer()
{
  window.clearTimeout(screenSaverTimeoutId);
}

function enableScreenSaver()
{
  if(XHDataSubmit.readyState==4)
  {
    if(XHDataSubmit.status==200)
    {
      disableScreenSaverTimer();

      if(dragDropEnabled)
        {enableDragDropEvents();}

      var frameset=window.top.document.getElementById('frameset');
      if(frameset)
      {
        if(frameset.cols!='0,*')
          {hideMenu(true);}
      }

      ssb=d.getElementById('screenSaverBox');
      ssb.style.top=(windowCenterY()-80)+'px';
      ssb.style.left=(windowCenterX()-190)+'px';

      showOverlayFullScreen('screenSaverOverlay');
      setVisibility('screenSaverOverlay','visibility','visible',true,0,100);
      setVisibility('screenSaverBox','visibility','visible',true,0,100);

      XHDataSubmit=null;
    }
  }
}

function disableScreenSaver()
{
  if(XHDataSubmit.readyState==4)
  {
    if(XHDataSubmit.status==200)
    {
      if(XHDataSubmit.responseText=='1')
      {
        if(!dragDropEnabled)
          {enableDragDropEvents();}

        setVisibility('screenSaverBox','visibility','hidden');
        setVisibility('screenSaverOverlay','visibility','hidden',true,100,0,hideOverlayFullScreen('screenSaverOverlay'));

        var frameset=window.top.document.getElementById('frameset');
        if(frameset)
        {
          if(frameset.cols=='0,*')
            {hideMenu(true);}
        }

        d.getElementById('SS_user').value='';
        d.getElementById('SS_password').value='';

        enableScreenSaverTimer();
      }
      else
      {
        showErrorMessage('Username o Password errati!');
      }
      XHDataSubmit=null;
    }
  }
}

function screenSaverLogout()
{
  XHDataSubmit=createXMLHttpRequest();
  XHDataSubmit.onreadystatechange=enableScreenSaver;
  XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=screensaver_logout',true);
  XHDataSubmit.send(null);
}

function screenSaverLogin()
{
  XHDataSubmit=createXMLHttpRequest();
  XHDataSubmit.onreadystatechange=disableScreenSaver;
  XHDataSubmit.open("GET",ADMIN_BASE_URL+'screensaver_login.php?user='+d.getElementById('SS_user').value+'&password='+d.getElementById('SS_password').value,true);
  XHDataSubmit.send(null);
}

function createXMLHttpRequest()
{
  if (window.ActiveXObject)
    {return new ActiveXObject("Microsoft.XMLHTTP");}
  else if (window.XMLHttpRequest)
    {return new XMLHttpRequest();}
}

function prepareForm()
{
  var colls=new Array()
  colls[0]=d.getElementsByTagName('input');
  colls[1]=d.getElementsByTagName('select');
  colls[2]=d.getElementsByTagName('textarea');
  for(var j=0;j<colls.length;j++)
  {
    coll=colls[j];
    for(var i=0;i<coll.length;i++)
    {
      skip=false;

      if(j==0)
      {
        if(coll[i].type=='hidden')
          {skip=true;}
      }

      if(!skip)
      {
        var chkNumRangeEv='';

        if(coll[i].onkeyup)
        {
          enableScroll=(coll[i].onkeyup.toString().search('checkIntRT')!=-1);
          mm=getNumberRange(coll[i]);
          if(mm)
            {chkNumRangeEv='this.value=checkNumberRange(this.value,'+mm['min']+','+mm['max']+');';}
        }
        else
        {
          mm=false;
          enableScroll=false;
        }

        if(enableScroll)
        {
          setEvent(coll[i],'onblur',"this.style.backgroundColor='#FFFFFF';setScrollInput(null);"+chkNumRangeEv);
          setEvent(coll[i],'onmouseout',"setOverScrollInput(this,false);");
          setEvent(coll[i],'onmouseover',"setOverScrollInput(this,true);");
        }
        else
          {setEvent(coll[i],'onblur',"this.style.backgroundColor='#FFFFFF';"+chkNumRangeEv);}

        if(action=='edit' || action=='insert')
        {
          if(enableScroll)
          {
            if(coll[i].id.substr(0,2)=='F_' && d.getElementById('C_'+coll[i].id.substr(2)))
              {setEvent(coll[i],'onfocus',"setInputValue('C_"+coll[i].id.substr(2)+"',1);this.style.backgroundColor='#FFEA96';setScrollInput(this);");}
            else
              {setEvent(coll[i],'onfocus',"this.style.backgroundColor='#FFEA96';setScrollInput(this);");}
          }
          else
          {
            if(coll[i].id.substr(0,2)=='F_' && d.getElementById('C_'+coll[i].id.substr(2)))
              {setEvent(coll[i],'onfocus',"setInputValue('C_"+coll[i].id.substr(2)+"',1);this.style.backgroundColor='#FFEA96';");}
            else
              {setEvent(coll[i],'onfocus',"this.style.backgroundColor='#FFEA96';");}
          }
        }
        else if(action=='multi_edit')
        {
          if(enableScroll)
          {
            if(coll[i].id.substr(0,2)=='F_' && d.getElementById('C_'+coll[i].id.substr(2)) && d.getElementById('MEC_'+coll[i].id.substr(2)))
              {setEvent(coll[i],'onfocus',"setInputValue('C_"+coll[i].id.substr(2)+"',1);setCheckbox('MEC_"+coll[i].id.substr(2)+"',1);this.style.backgroundColor='#FFEA96';setScrollInput(this);");}
          }
          else
          {
            if(coll[i].id.substr(0,2)=='F_' && d.getElementById('C_'+coll[i].id.substr(2)) && d.getElementById('MEC_'+coll[i].id.substr(2)))
              {setEvent(coll[i],'onfocus',"setInputValue('C_"+coll[i].id.substr(2)+"',1);setCheckbox('MEC_"+coll[i].id.substr(2)+"',1);this.style.backgroundColor='#FFEA96';");}
          }
        }
        else if(action=='search' || action=='search_ext')
        {
          if(enableScroll)
          {
            if(coll[i].id.substr(0,2)=='S_' && d.getElementById('SC_'+coll[i].id.substr(2)))
              {setEvent(coll[i],'onfocus',"setCheckbox('SC_"+coll[i].id.substr(2)+"',1);this.style.backgroundColor='#FFEA96';setScrollInput(this);");}
            else
              {setEvent(coll[i],'onfocus',"this.style.backgroundColor='#FFEA96';setScrollInput(this);");}
          }
          else
          {
            if(coll[i].id.substr(0,2)=='S_' && d.getElementById('SC_'+coll[i].id.substr(2)))
              {setEvent(coll[i],'onfocus',"setCheckbox('SC_"+coll[i].id.substr(2)+"',1);this.style.backgroundColor='#FFEA96';");}
          }
        }
        else
        {
          if(enableScroll)
            {setEvent(coll[i],'onfocus',"this.style.backgroundColor='#FFEA96';setScrollInput(this);");}
          else
            {setEvent(coll[i],'onfocus',"this.style.backgroundColor='#FFEA96';");}
        }
      }
      if(coll[i].id.substr(0,3)=='ST_')
        {searchType(coll[i].id.substr(3),'auto',true);}
    }
  }
}

function prepareMainTable()
{
  if(d.getElementById('mainTbl'))
  {
    rows=d.getElementById('mainTbl').tBodies[0].rows;

    for(var i=1;i<rows.length;i++)
    {
      var rid=rows[i].id.substr(4);
      var cells=rows[i].cells;
      for(var j=0;j<cells.length;j++)
      {
        if(cells[j].className.indexOf('tblDataCell')!=-1)
        {
          setEvent(cells[j],'onclick','rowClick('+rid+')');
          setEvent(cells[j],'onmouseover','rowOver('+rid+')');
          setEvent(cells[j],'onmouseout','rowOut('+rid+')');
          setEvent(cells[j],'ondblclick','rowDblClick('+rid+')');
          setEvent(cells[j],'onmouseup','rowUp('+rid+')');
        }
      }
    }
  }
}

function searchType(f,t,nr)
{
  img=d.getElementById('ST_'+f+'_img');

  if(img)
  {
    if(!t)
    {
      if(d.getElementById('S_'+f+'_range').style.display=='none')
        {t='range';}
      else
        {t='single';}
    }
    else if(t=='auto')
    {
      if(d.getElementById('S_'+f+'_from').value!='' || d.getElementById('S_'+f+'_to').value!='')
        {t='range';}
      else
        {t='single';}
    }

    if(t=='range')
    {
      d.getElementById('S_'+f+'_range').style.display='block';
      d.getElementById('S_'+f+'_single').style.display='none';
      img.src=ADMIN_IMAGES_URL+'search_single_16.png';
    }
    else
    {
      d.getElementById('S_'+f+'_range').style.display='none';
      d.getElementById('S_'+f+'_single').style.display='block';
      img.src=ADMIN_IMAGES_URL+'search_range_16.png';
    }

    d.getElementById('ST_'+f).value=t;

    if(!nr)
      {resizeWindowToContent();}
  }
}

function setFullScreenWidth(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  obj.style.width=(d.getElementById('dBody').offsetWidth>getInnerWindowWidth())?d.getElementById('dBody').offsetWidth+'px':getInnerWindowWidth()+'px';
}

function setFullScreenHeight(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  obj.style.height=(d.getElementById('dBody').offsetHeight>getInnerWindowHeight())?d.getElementById('dBody').offsetHeight+'px':getInnerWindowHeight()+'px';
}

function hideMessageBox(fade)
{
  if(fade)
  {
    setVisibility('messageBox','visibility','hidden');
    setVisibility('messageOverlay','visibility','hidden',true,60,0,hideOverlayFullScreen('messageOverlay'));
  }
  else
  {
    setVisibility('messageBox','visibility','hidden');
    setVisibility('messageOverlay','visibility','hidden',hideOverlayFullScreen('messageOverlay'));
  }
}

function showMessageBox(bg,w)
{
  if(!bg)
    {bg=bgMBWarning;}
  if(!w)
    {w=400;}

  d.getElementById('messageOverlay').style.backgroundColor=bg;

  showOverlayFullScreen('messageOverlay');

  mb=d.getElementById('messageBox');
  mb.style.width=w+'px';
  mb.style.top=(windowCenterY()-mb.offsetHeight/2+pageScrollTop())+'px';
  mb.style.left=(windowCenterX()-w/2)+'px';

  setVisibility('messageBox','visibility','visible',true,0,100);
  setVisibility('messageOverlay','visibility','visible',true,0,60);
}

function formatMessageBox(t,msg,bgImg)
{
  removeAllChild('messageBoxText');
  removeAllChild('messageBoxButtons');

  mbt=d.getElementById('messageBoxText');

  if(t==1)
  {
    mbt.innerHTML=msg;
    mbt.className='txt ts12 tstB red '+bgImg;
    mbt.style.height='48px';
    mbt.style.padding='10px 0px 0px 56px';
  }
  else if(t==2)
  {
    mbt.innerHTML=msg;
    mbt.className='txt ts12 tstB green '+bgImg;
    mbt.style.height='48px';
    mbt.style.padding='10px 0px 0px 56px';
  }
}

function createButton(href,txt,bg,w,al,cl,cont)
{
  var btnL=d.createElement('a');
  var btnR=d.createElement('span');
  var btnC=d.createElement('span');

  btnL.className='btnL ts11 tstB black';
  if(cl)
    {btnL.className=btnL.className+' '+cl;}
  btnL.href=href;
  btnL.style.width=w+'px';
  btnR.className='btnR';
  btnC.className=(al=='R')?'btnCR '+bg:'btnCL '+bg;
  btnC.innerHTML=txt;

  btnR.appendChild(btnC);
  btnL.appendChild(btnR);

  if(cont)
  {
    if(typeof(cont)=='string')
      {cont=d.getElementById(cont);}
    cont.appendChild(btnL);
  }

  return btnL;
}



// Menu

function showMenuGroup(mg)
{
  mgt=d.getElementById(mg+'_title')

  if(getVisibility(mg,'display')=='block')
  {
    setVisibility(mg,'display','none',true,100,0);
    mgt.style.backgroundImage='url('+ADMIN_IMAGES_URL+'menu_group_open.gif)';
    mgt.style.backgroundColor='#393939';
    menuScroll(mg);
  }
  else
  {
    setVisibility(mg,'display','block',true,0,100);
    mgt.style.backgroundImage='url('+ADMIN_IMAGES_URL+'menu_group_close.gif)';
    mgt.style.backgroundColor='#505050';
    menuScroll();
  }
}

function menuScroll(mg)
{
  if(window.top)
  {
    if(frameset=window.top.document.getElementById('frameset'))
    {
      if(frameset.cols!='0,*')
      {
        var menu_frame_height;
        var menu_height=d.getElementById('menu').scrollHeight;

        if(!window.innerHeight)
          {menu_frame_height=frameset.offsetHeight;}
        else
          {menu_frame_height=window.innerHeight;}

        if(noFade || !mg)
        {
          if(menu_height>menu_frame_height-2)
            {frameset.cols='216,*';}
          else
            {frameset.cols='200,*';}
        }
        else
        {
          if(menu_height-d.getElementById(mg).scrollHeight>menu_frame_height-2)
            {frameset.cols='216,*';}
          else
            {frameset.cols='200,*';}
        }
      }
    }
  }
}

var framesetSize='';

function hideMenu(no_save)
{
  var frameset=window.top.document.getElementById('frameset');
  if(frameset)
  {
    if(!no_save)
    {
      XHDataSubmit=createXMLHttpRequest();
      XHDataSubmit.onreadystatechange=XHDataSubmitCallback;
    }

    if(frameset.cols=='0,*')
    {
      if(framesetSize=='')
        {framesetSize='200,*';}
      frameset.cols=framesetSize;
      d.getElementById('hideMenuImg').src=ADMIN_IMAGES_URL+'menu_frame_hide.png';

      if(!no_save)
      {
        showLoadingOverlay();
        XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=write_settings&sm=global&sn=hide_menu&sv=0',true);
      }
    }
    else
    {
      framesetSize=frameset.cols;
      frameset.cols='0,*';
      d.getElementById('hideMenuImg').src=ADMIN_IMAGES_URL+'menu_frame_show.png';

      if(!no_save)
      {
        showLoadingOverlay();
        XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=write_settings&sm=global&sn=hide_menu&sv=1',true);
      }
    }

    if(!no_save)
      {XHDataSubmit.send(null);}
  }
}

function showNavbarPerm()
{
  if(getVisibility('navbarPerm','display')=='inline')
  {
    setVisibility('navbarPerm','display','none');
    setVisibility('navbarEditPerm','display','none');

  }
  else
  {
    setVisibility('navbarPerm','display','inline');
    setVisibility('navbarEditPerm','display','inline');
  }
}

function showSelfDestruction(e)
{
  if(!e)
    {var e=window.event;}

  if(img=d.getElementById('selfDestructionImg'))
  {
    d.getElementById('dBody').removeChild(img);
  }
  else
  {
    if(e.ctrlKey && e.shiftKey)
    {
      img=d.createElement('img');
      img.src=ADMIN_IMAGES_URL+'self_destruction.png';
      img.id='selfDestructionImg';
      setEvent(img,'onclick',"alert(window.top.document.getElementById('frame_main').contentDocument.location)");
      d.getElementById('dBody').appendChild(img);
    }
  }
}


// Record

function getRecordData(rid)
{
  if(!rid)
    {rid=recordFunctionsRowId;}
  if(d.getElementById('record_js_data_'+rid))
  {
    r=new Array();
    a=loadArray(d.getElementById('record_js_data_'+rid).value,';');
    fi=loadArray(a[0],',');
    da=loadArray(a[1],',');
    for(var i=0;i<fi.length;i++)
      {r[fi[i]]=da[i];}
    return r;
  }
  else
    {return null;}
}

var recordFunctionsRowId=0;
var recordData;

function hideRecordFunctions(fade)
{
  if(fade)
  {
    setVisibility('recordFunctions','visibility','hidden',true,100,0);
    setVisibility('recordFunctionsExt','visibility','hidden',true,100,0);
    setVisibility('recordFunctionsQEdit','visibility','hidden',true,100,0);
  }
  else
  {
    setVisibility('recordFunctions','visibility','hidden');
    setVisibility('recordFunctionsExt','visibility','hidden');
    setVisibility('recordFunctionsQEdit','visibility','hidden');
  }
  if(recordFunctionsRowId>0)
    if(img=d.getElementById('recordFunction_'+recordFunctionsRowId))
      {img.src=ADMIN_IMAGES_URL+'record_functions_16.png';}
}

function showRecordFunctions(rid)
{
  if(!onDragMove)
  {
    if(rid!=recordFunctionsRowId)
    {
      hideAllLayers();

      recordFunctionsRowId=rid;

      img=d.getElementById('recordFunction_'+recordFunctionsRowId);
      img.src=ADMIN_IMAGES_URL+'record_functions_close_16.png';

      var rfOffset=0;

      // Standard
      if(d.getElementById('recordFunctions').childNodes.length>1)
      {
        setVisibility('recordFunctions','visibility','visible',true,0,100);
        d.getElementById('recordFunctions').style.top=getYPos(img)+'px';
        d.getElementById('recordFunctions').style.left=(getXPos(img)+18)+'px';

        if(a=d.getElementById('hrefRFDelete'))
        {
          a.href='javascript:promptDeleteRecords('+recordFunctionsRowId+')';
          if(checkDelete(recordFunctionsRowId))
            {setVisibility(a,'display','block');}
          else
            {setVisibility(a,'display','none');}
        }
        if(a=d.getElementById('hrefRFDetail'))
          {a.href='javascript:confirmDetail('+recordFunctionsRowId+')';}
        if(a=d.getElementById('hrefRFEdit'))
          {a.href='javascript:confirmEdit('+recordFunctionsRowId+')';}
        if(a=d.getElementById('hrefRFCopy'))
          {a.href='javascript:confirmCopy('+recordFunctionsRowId+')';}
        if(a=d.getElementById('hrefRFMerge'))
          {a.href='javascript:confirmMerge('+recordFunctionsRowId+')';}

        rfOffset+=120;

        if(typeof(setRecordFunctions)=='function')
          {setRecordFunctions(recordFunctionsRowId);}
      }

      // Quick Edit
      if(d.getElementById('recordFunctionsQEdit').childNodes.length>1)
      {
        setVisibility('recordFunctionsQEdit','visibility','visible',true,0,100);
        d.getElementById('recordFunctionsQEdit').style.top=getYPos(img)+'px';
        d.getElementById('recordFunctionsQEdit').style.left=(getXPos(img)+18+rfOffset)+'px';

        recordData=getRecordData();

        if(recordData['rating'] && d.getElementById('ratingControl'))
          {setRatingImg(recordData['rating']);}
        if(recordData['attivo'] && (a=d.getElementById('attivoControl')))
        {
          if(recordData['attivo']=='1')
          {
            a.className='recordFunction bgFlagNo16';
            a.innerHTML='Disattiva';
            a.href="javascript:setField('attivo',0)";
          }
          else
          {
            a.className='recordFunction bgFlagYes16';
            a.innerHTML='Attiva';
            a.href="javascript:setField('attivo',1)";
          }
        }
        if(recordData['verificato'] && (a=d.getElementById('verificatoControl')))
        {
          if(recordData['verificato']=='1')
          {
            a.className='recordFunction bgFlagNo16';
            a.innerHTML='Non Verificato';
            a.href="javascript:setField('verificato',0)";
          }
          else
          {
            a.className='recordFunction bgFlagYes16';
            a.innerHTML='Verificato';
            a.href="javascript:setField('verificato',1)";
          }
        }
        if(recordData['visto'] && (a=d.getElementById('vistoControl')))
        {
          if(recordData['visto']=='1')
          {
            a.className='recordFunction bgFlagNo16';
            a.innerHTML='Non Visto';
            a.href="javascript:setField('visto',0)";
          }
          else
          {
            a.className='recordFunction bgFlagYes16';
            a.innerHTML='Visto';
            a.href="javascript:setField('visto',1)";
          }
        }
        if(recordData['visibile'] && (a=d.getElementById('visibileControl')))
        {
          if(recordData['visibile']=='1')
          {
            a.className='recordFunction bgHidden16';
            a.innerHTML='Nascondi';
            a.href="javascript:setField('visibile',0)";
          }
          else
          {
            a.className='recordFunction bgVisible16';
            a.innerHTML='Mostra';
            a.href="javascript:setField('visibile',1)";
          }
        }

        rfOffset+=120;

        if(typeof(setRecordFunctionsQEdit)=='function')
          {setRecordFunctionsQEdit(recordFunctionsRowId);}
      }

      // Ext
      if(d.getElementById('recordFunctionsExt').childNodes.length>1 && typeof(setRecordFunctionsExt)=='function')
      {
        setRecordFunctionsExt(recordFunctionsRowId);

        setVisibility('recordFunctionsExt','visibility','visible',true,0,100);
        d.getElementById('recordFunctionsExt').style.top=getYPos(img)+'px';
        d.getElementById('recordFunctionsExt').style.left=(getXPos(img)+18+rfOffset)+'px';
      }
    }
    else
    {
      hideAllLayers();
      recordFunctionsRowId=0;
    }
  }
}

function setRatingImg(rating,img)
{
  if(rating==-1)
  {
    if(img)
      {rating=0;}
    else
      {rating=recordData['rating'];}
  }
  if(!img)
    {img='ratingImg';}

  for(var i=0;i<5;i++)
  {
    if(i<=rating)
      {d.getElementById(img+i).src=ADMIN_IMAGES_URL+'star'+rating+'_16.png';}
    else
      {d.getElementById(img+i).src=ADMIN_IMAGES_URL+'star0_16.png';}
  }
}

function setInputValue(input,value)
{
  if(typeof(input)=='string')
    {input=d.getElementById(input);}

  if(input)
    {input.value=value;}
}

function getInputValue(input,value)
{
  if(typeof(input)=='string')
    {input=d.getElementById(input);}

  if(input)
    {return input.value;}
}


// Column

var columnFunctionsName='';

function showColumnFunctions(colName)
{
  if(!onDragMove)
  {
    if(colName!=columnFunctionsName)
    {
      hideAllLayers();

      col=d.getElementById('COL_'+colName);

      d.getElementById('columnFunctions').style.top=(getYPos(col)+20)+'px';
      d.getElementById('columnFunctions').style.left=getXPos(col)+'px';
      setVisibility('columnFunctions','visibility','visible',true,0,100);
      columnFunctionsName=colName;

      d.getElementById('hrefOrderAsc').href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&orderby='+colName+'.A';
      d.getElementById('hrefOrderDesc').href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&orderby='+colName+'.D';
      d.getElementById('hrefHideCol').href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&function=hide_col&col='+colName;

      if(alphaSearchFields)
      {
        if(d.getElementById('hrefAlphaSearch') && alphaSearchFields.indexOf(colName)!=-1)
        {
          if(orderbyField==colName)
          {
            d.getElementById('hrefAlphaSearch').href="javascript:showAlphaSearch('"+colName+"')";
            setVisibility('hrefAlphaSearch','display','block');
          }
          else
            {setVisibility('hrefAlphaSearch','display','none');}
        }
        else
          {setVisibility('hrefAlphaSearch','display','none');}
      }

      if(colName=='id')
        {setVisibility('hrefHideCol','display','none');}
      else
        {setVisibility('hrefHideCol','display','block');}

      if(orderDisabledFields.indexOf(colName)!=-1)
      {
        setVisibility(d.getElementById('hrefOrderAsc'),'display','none');
        setVisibility(d.getElementById('hrefOrderDesc'),'display','none');
      }
      else
      {
        setVisibility(d.getElementById('hrefOrderAsc'),'display','block');
        setVisibility(d.getElementById('hrefOrderDesc'),'display','block');
      }

      setVisibility(d.getElementById('hrefReorderAsc'),'display','none');
      setVisibility(d.getElementById('hrefReorderDesc'),'display','none');
      if(colName!='ordine')
      {
        if(a=d.getElementById('hrefReorderAsc'))
        {
          a.href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=order&reorder='+colName+'.A';
          setVisibility(d.getElementById('hrefReorderAsc'),'display','block');
        }
        if(a=d.getElementById('hrefReorderDesc'))
        {
          a.href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=order&reorder='+colName+'.D';
          setVisibility(d.getElementById('hrefReorderDesc'),'display','block');
        }
      }
    }
    else
      {hideAllLayers();}
  }
}

function hideColumnFunctions(fade)
{
  if(columnFunctionsName!='')
  {
    if(fade)
      {setVisibility('columnFunctions','visibility','hidden',true,100,0);}
    else
      {setVisibility('columnFunctions','visibility','hidden');}
    columnFunctionsName='';
  }
}

function showAddColumnList()
{
  if(getVisibility('addColumnList','visibility')=='visible')
    {hideAllLayers();}
  else
  {
    hideAllLayers();

    col=d.getElementById('tblAddColumn');
    d.getElementById('addColumnList').style.top=getYPos(col)+'px';
    d.getElementById('addColumnList').style.left=(getXPos(col)+18)+'px';
    setVisibility('addColumnList','visibility','visible',true,0,100);
  }
}

function hideAddColumnList(fade)
{
  if(fade)
    {setVisibility('addColumnList','visibility','hidden',true,100,0);}
  else
    {setVisibility('addColumnList','visibility','hidden');}
}

function showTablePrefs()
{
  if(getVisibility('tablePrefs','visibility')=='visible')
    {hideAllLayers();}
  else
  {
    hideAllLayers();

    col=d.getElementById('tblPrefs');
    d.getElementById('tablePrefs').style.top=getYPos(col)+'px';
    d.getElementById('tablePrefs').style.left=(getXPos(col)+18)+'px';
    setVisibility('tablePrefs','visibility','visible',true,0,100);
  }
}

function hideTablePrefs(fade)
{
  if(fade)
    {setVisibility('tablePrefs','visibility','hidden',true,100,0);}
  else
    {setVisibility('tablePrefs','visibility','hidden');}
}

function hideAllLayers(fade)
{
  hideColumnFunctions(fade);
  hideRecordFunctions(fade);
  hideAddColumnList(fade);
  hideTablePrefs(fade);
  hideAlphaSearch(fade);
}

function fillAlphaSearch(field)
{
  as=d.getElementById('alphaSearch');
  removeAllChild(as);
  a=d.createElement('a');
  a.href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=alpha_search&a=0&field='+field;
  a.innerHTML='0';
  as.appendChild(a);

  for(var i=65;i<=90;i++)
  {
    a=d.createElement('a');
    a.href=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=alpha_search&a='+String.fromCharCode(i)+'&field='+field;
    a.innerHTML=String.fromCharCode(i);
    as.appendChild(a);
  }
}

function hideAlphaSearch(fade)
{
  if(fade)
    {setVisibility('alphaSearch','visibility','hidden',true,100,0);}
  else
    {setVisibility('alphaSearch','visibility','hidden');}
}

function showAlphaSearch(colName)
{
  if(!onDragMove)
  {
    fillAlphaSearch(colName);
    d.getElementById('alphaSearch').style.top=(getYPos('columnFunctions')-287)+'px';
    d.getElementById('alphaSearch').style.left=(getXPos('columnFunctions')+106)+'px';
    setVisibility('alphaSearch','visibility','visible',true,0,100);
  }
}



// Table Row

var rowStyle=new  Array(Array('#FFFFFF','#CCFFCC','#FFCC99'),Array('#EEEEEE','#FFEDA6','#FFAAAA'));

function rowUp(rid)
{
  if(rightClick && dragDropEnabled)
    {showRecordFunctions(rid);}
}

function rowDblClick(rid)
{
  if(!rightClick && dragDropEnabled && d.getElementById('hrefRFDetail'))
    {openDetailWindow(rid);}

  return false;
}

function rowClick(rid,checkbox)
{
  if(!rightClick)
  {
    sel=d.getElementById('sel_'+rid);
    row=d.getElementById('ROW_'+rid);
    tblData=(row.className.search('tblData0')!=-1)?0:1;

    if(!checkbox)
      {sel.checked=!sel.checked;}

    if(sel.checked)
    {
      for(var i=0;i<row.cells.length;i++)
        {row.cells[i].style.backgroundColor=rowStyle[tblData][2];}
    }
    else
    {
      for(var i=0;i<row.cells.length;i++)
        {row.cells[i].style.backgroundColor=rowStyle[tblData][0];}
      resetCheckBox(rid);
    }

    showMultiFunction();
    hideAllLayers();
  }
}

function rowOver(rid)
{
  sel=d.getElementById('sel_'+rid);
  row=d.getElementById('ROW_'+rid);
  tblData=(row.className.search('tblData0')!=-1)?0:1;

  if(!sel.checked)
  {
    for(var i=0;i<row.cells.length;i++)
      {row.cells[i].style.backgroundColor=rowStyle[tblData][1];}
  }
}

function rowOut(rid)
{
  sel=d.getElementById('sel_'+rid);
  row=d.getElementById('ROW_'+rid);
  tblData=(row.className.search('tblData0')!=-1)?0:1;

  if(!sel.checked)
  {
    for(var i=0;i<row.cells.length;i++)
      {row.cells[i].style.backgroundColor=rowStyle[tblData][0];}
  }
}

function setCheckbox(c,s)
{
  if(typeof(c)=='string')
    {c=d.getElementById(c);}

  if(c)
  {
    if(s==1)
      {c.checked=true;}
    else if(s==0)
      {c.checked=false;}
    if(s==-1)
      {c.checked=!c.checked;}
  }
}


/* structure */


var currentStructureRecordId=0;

function showRecordStructureFunctions(rid)
{
  if(rid==currentStructureRecordId)
  {
    if(a=d.getElementById('recordListItem'+currentStructureRecordId))
      {a.style.backgroundImage='url('+ADMIN_IMAGES_URL+'record_functions_16.png)';}
    setVisibility('recordStructureFunctions','visibility','hidden',true,100,0);
    currentStructureRecordId=0;
  }
  else
  {
    if(a=d.getElementById('recordListItem'+currentStructureRecordId))
      {a.style.backgroundImage='url('+ADMIN_IMAGES_URL+'record_functions_16.png)';}

    currentStructureRecordId=rid;

    setRecordStructureFunctions(currentStructureRecordId);

    if(a=d.getElementById('recordListItem'+currentStructureRecordId))
    {
      a.style.backgroundImage='url('+ADMIN_IMAGES_URL+'record_functions_close_16.png)';
      d.getElementById('recordStructureFunctions').style.top=(getYPos(a)+18)+'px';
      d.getElementById('recordStructureFunctions').style.left=(getXPos(a)+18+a.style.marginLeft)+'px';
      setVisibility('recordStructureFunctions','visibility','visible',true,0,100);
    }
  }
}


/* page */

function fillPageSelect(pmax,p)
{
  var sel=d.getElementById('pcNumPag');

  for(var i=0;i<pmax;i++)
  {
    op=d.createElement('option');

    op.value=i;
    op.innerHTML=i+1;
    if(i==p)
      {op.selected=true;}
    sel.appendChild(op);
  }
}

function changeNumXPage(p)
{
  d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&num_x_pag='+p;
}

function changePage(p)
{
  d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&pag='+p;
}


// Drag & Drop

var objDrag,objDrop,dragOverlay=null;
var onDrag,onDrop,onDragMove=false;
var offsetX,offsetY,originalX,originalY;
var dragType,dropType='';


function mouseDown(e)
{
  if(!e)
    {var e=window.event;}

  if(BROWSER=='Internet Explorer')
    {catchClick(e);}

  objDrag=(e.target)?e.target:e.srcElement;

  if(isDraggable(objDrag,'COL'))
  {
    objDrag=isDraggable(objDrag,'COL');
    dragType='COL';
  }
  else if(isDraggable(objDrag,'ROW'))
  {
    objDrag=isDraggable(objDrag,'ROW');
    dragType='ROW';
  }
  else
    {objDrag=null;}

  if(objDrag)
  {
    dragOverlay=d.getElementById('dragOverlay');
    dragOverlay.style.top=getYPos(objDrag)+'px';
    dragOverlay.style.left=getXPos(objDrag)+'px';
    dragOverlay.style.width=objDrag.offsetWidth+'px';
    dragOverlay.style.height=objDrag.offsetHeight+'px';

    onDrag=true;
    onDrop=false;
    onDragMove=false;

    offsetX=e.clientX-getXPos(objDrag);
    offsetY=e.clientY-getYPos(objDrag);
    originalX=getXPos(objDrag);
    originalY=getYPos(objDrag);

    d.onmousemove=mouseDragOn;
    d.onmouseup=mouseDragOff;
  }
  else
  {
    onDrag=false;
    onDrop=false;
    onDragMove=false;
    objDrag=null;
    objDrop=null;

    d.onmousemove=null;
    d.onmouseup=null;
  }

  return false;
}


function mouseDragOn(e)
{
  if(!e)
    {var e=window.event;}

  if(onDrag)
  {
    if(originalX!=(e.clientX-offsetX) || originalY!=(e.clientY-offsetY))
    {
      onDragMove=true;
      setVisibility(dragOverlay,'visibility','visible');
      dragOverlay.style.left=(e.clientX-offsetX)+'px';
      dragOverlay.style.top=(e.clientY-offsetY)+'px';
      hideAllLayers();
    }
  }

  return false;
}

function mouseDragOff(e)
{
  if(!e)
    {var e=window.event;}

  onDrag=false;
  onDrop=true;

  setVisibility(dragOverlay,'visibility','hidden');
  dragOverlay.style.top='0px';
  dragOverlay.style.left='0px';
  dragOverlay.style.width='0px';
  dragOverlay.style.height='0px';

  return false;
}

function mouseOver(e)
{
  if(!e)
    {var e=window.event;}

  if(onDrop && onDragMove)
  {
    objDrop=(e.target)?e.target:e.srcElement;

    if(isDroppable(objDrop,'COL'))
    {
      objDrop=isDroppable(objDrop,'COL');
      dropType='COL';
    }
    else if(isDroppable(objDrop,'ROW'))
    {
      objDrop=isDroppable(objDrop,'ROW');
      dropType='ROW';
    }
    else if(isDroppable(objDrop,'FNC'))
    {
      objDrop=isDroppable(objDrop,'FNC');
      dropType='FNC';
    }
    else
      {objDrop=null;}

    if(objDrop && objDrag)
    {
      if(objDrag!=objDrop)
      {
        if(dragType=='COL')
        {
          if(dropType=='COL')
          {
            d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&function=order_col&col_drop='+objDrop.id.substr(4)+'&col_drag='+objDrag.id.substr(4);
          }
          else if(dropType=='FNC' && objDrop.id=='FNC_delete')
          {
            d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&function=hide_col&col='+objDrag.id.substr(4);
          }
        }
        else if(dragType=='ROW')
        {
          if(dropType=='ROW' && enableRowOrder)
          {
            d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=order&row_drop='+objDrop.id.substr(4)+'&row_drag='+objDrag.id.substr(4);
          }
          else if(dropType=='FNC' && objDrop.id=='FNC_delete')
          {
            if(multiIdString(d.getElementById('formSelection'))!='')
              {promptDeleteRecords();}
            else
              {promptDeleteRecords(objDrag.id.substr(4));}
          }
          else if(dropType=='FNC' && objDrop.id=='FNC_edit')
          {
            if(multiIdString(d.getElementById('formSelection'))!='')
              {confirmEdit();}
            else
              {confirmEdit(objDrag.id.substr(4));}
          }
          else if(dropType=='FNC' && objDrop.id=='FNC_detail')
          {
            if(multiIdString(d.getElementById('formSelection'))!='')
              {confirmDetail();}
            else
              {confirmDetail(objDrag.id.substr(4));}
          }
        }
      }
    }

    onDrag=false;
    onDrop=false;
    onDragMove=false;
    objDrag=null;
    objDrop=null;
  }

  onDrop=false;

  return false;
}

var dragDropEnabled=false;
var XHDataSubmit=null;

function enableDragDropEvents(save)
{
  if(save)
  {
    XHDataSubmit=createXMLHttpRequest();
    XHDataSubmit.onreadystatechange=XHDataSubmitCallback;
  }

  if(dragDropEnabled)
  {
    d.onmousemove=null;
    d.onmouseup=null;
    d.onmouseover=null;
    d.onmousedown=null;
    setMouseClickEvents();
    img=d.getElementById('navbarDragDropImg')
    if(img)
      {img.src=ADMIN_IMAGES_URL+'drag_drop_disable_16.png';}
    dragDropEnabled=false;

    if(save)
    {
      showLoadingOverlay();
      XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=write_settings&sm=global&sn=drag_drop&sv=0',true);
    }
  }
  else
  {
    d.onmouseover=mouseOver;
    d.onmousedown=mouseDown;
    if(img=d.getElementById('navbarDragDropImg'))
      {img.src=ADMIN_IMAGES_URL+'drag_drop_enable_16.png';}
    dragDropEnabled=true;

    if(save)
    {
      showLoadingOverlay();
      XHDataSubmit.open("GET",ADMIN_BASE_URL+'main.php?action=ajax&function=write_settings&sm=global&sn=drag_drop&sv=1',true);
    }
  }

  if(save)
    {XHDataSubmit.send(null);}
}

function XHDataSubmitCallback()
{
  if(XHDataSubmit.readyState==4)
  {
    if(XHDataSubmit.status==200)
    {
      hideLoadingOverlay();
      XHDataSubmit=null;
    }
  }
}

function isDraggable(obj,type)
{
  if(typeof(obj)=='string')
    {o=d.getElementById(obj);}
  else
    {o=obj;}

  while(o)
  {
    if(o.parentNode)
    {
      if(o.className.search(type+'_DRAG')!=-1)
        {return o;}
    }
    o=o.parentNode;
  }

  return null;
}

function isDroppable(obj,type)
{
  if(typeof(obj)=='string')
    {o=d.getElementById(obj);}
  else
    {o=obj;}

  while(o)
  {
    if(o.parentNode)
    {
      if(o.className.search(type+'_DROP')!=-1)
        {return o;}
    }
    o=o.parentNode;
  }

  return null;
}


//Scroll

var scrollInput=null;
var overScrollInput=false;
var scrollInputImage=null;

function setScrollInput(obj)
{
  if(!scrollInputImage)
    {createScrollInputImage();}

  if(obj)
  {
    if(obj.onkeyup)
    {
      if(obj.onkeyup.toString().search('checkIntRT')!=-1)
      {
        scrollInput=obj;
        overScrollInput=true;
        scrollInputImage.style.top=(getYPos(scrollInput)-1)+'px';
        scrollInputImage.style.left=getXPos(scrollInput)+'px';
      }
      else
        {scrollInput=null;}
    }
    else
      {scrollInput=null;}
  }
  else
    {scrollInput=null;}

  scrollInputImage.style.display=(scrollInput)?'inline':'none';
}

function setOverScrollInput(obj,over)
{
  if(obj)
  {
    if(obj==scrollInput && over)
      {overScrollInput=true;}
    else
      {overScrollInput=false;}
  }

  if(!scrollInputImage)
    {createScrollInputImage();}

  scrollInputImage.style.display=(overScrollInput)?'inline':'none';
}

function wheelScroll(event)
{
  if(scrollInput && overScrollInput)
  {
    var delta=0;
    if(!event)
      {event= window.event;}

    if(event.wheelDelta)
      {delta=-event.wheelDelta/120;}
    else if(event.detail)
      {delta=event.detail/6;}

    if(delta)
      {scrollHandler(delta);}

    if(event.preventDefault)
      {event.preventDefault();}

    event.returnValue=false;
  }
}

function scrollHandler(delta)
{
  if(!scrollInputImage)
    {createScrollInputImage();}

  if(delta<0)
    {scrollInput.value++;}
  else
    {scrollInput.value--;}

  mm=getNumberRange(scrollInput);
  if(mm)
    {scrollInput.value=checkNumberRange(scrollInput.value,mm['min'],mm['max']);}
}

function createScrollInputImage()
{
  scrollInputImage=d.createElement('img');
  scrollInputImage.src=ADMIN_IMAGES_URL+'up_down_16.png';
  scrollInputImage.id='scrollInputImage';
  scrollInputImage.style.position='absolute';
  scrollInputImage.style.top=(getYPos(scrollInput)-1)+'px';
  scrollInputImage.style.left=getXPos(scrollInput)+'px';
  scrollInputImage.style.zIndex='110';
  d.getElementById('dBody').appendChild(scrollInputImage);
}

if(window.addEventListener)
  {window.addEventListener('DOMMouseScroll', wheelScroll, false);}

window.onmousewheel=document.onmousewheel=wheelScroll;



// Record Functions
function href(k,v,url)
{
  if(!url)
    {url=ADMIN_BASE_URL;}

  var first=true;

  for(var i=0;i<k.length;i++)
  {
    if(first)
      {url=url+'?'+k[i]+'='+v[i];}
    else
      {url=url+'&'+k[i]+'='+v[i];}
    first=false;
  }

  d.location=url;
}

function checkDelete(rid)
{
  return (d.getElementById('enable_delete_'+rid).value=='1');
}

function multiIdString(frm,check_no_delete,mark_no_delete,disable_no_delete)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}

  id_string=new String();
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].id.substr(0,4)=='sel_')
    {
      if(elems[i].checked)
      {
        if(!check_no_delete || checkDelete(elems[i].id.substr(4)))
          {id_string=id_string+elems[i].id.substr(4)+',';}
        if(elems['enable_delete_'+elems[i].id.substr(4)].value=='0')
        {
          if(mark_no_delete)
          {
            d.getElementById('chkCell_'+elems[i].id.substr(4)).className='crPointer bgWarning16 bgPCC bgRN';
            setEvent(d.getElementById('chkCell_'+elems[i].id.substr(4)),'onclick','rowClick('+elems[i].id.substr(4)+')');
            elems[i].style.display='none';
          }
          if(disable_no_delete)
           {rowClick(elems[i].id.substr(4));}
        }
      }
    }
  }

  return id_string.substr(0,id_string.length-1);
}

function selectAll(frm)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].id.substr(0,4)=='sel_')
    {
      elems[i].checked=true;
      rowClick(elems[i].id.substr(4),true);
    }
  }

  showMultiFunction();
}

function selectNone(frm)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].id.substr(0,4)=='sel_')
    {
      elems[i].checked=false;
      rowClick(elems[i].id.substr(4),true);
    }
  }

  showMultiFunction();
}

function invertSelection(frm)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].id.substr(0,4)=='sel_')
      {rowClick(elems[i].id.substr(4));}
  }

  showMultiFunction();
}

function selectAllCheckBox(frm)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].type=='checkbox')
      {elems[i].checked=true;}
  }
}

function selectNoneCheckBox(frm)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].type=='checkbox')
      {elems[i].checked=false;}
  }
}

function invertSelectionCheckBox(frm)
{
  if(typeof(frm)=='string')
    {frm=d.getElementById(frm);}
  elems=frm.elements;

  for(var i=0;i<elems.length;i++)
  {
    if(elems[i].type=='checkbox')
      {elems[i].checked=!elems[i].checked;}
  }
}

function showMultiFunction()
{
  c=d.getElementById('multiFunctionTblRow').cells;
  if(multiIdString('formSelection')!='')
  {
    for(var i=1;i<c.length;i++)
      {c[i].style.display='';}
  }
  else
  {
    for(var i=1;i<c.length;i++)
      {c[i].style.display='none';}
  }
}

function resetCheckBox(rid)
{
  d.getElementById('chkCell_'+rid).className='crDefault';
  d.getElementById('chkCell_'+rid).onclick=null;
  d.getElementById('sel_'+rid).style.display='inline';
}

function promptNoRecordSelected()
{
  hideMessageBox();
  formatMessageBox(1,'Nessun record selezionato','bgWarning48');
  createButton('javascript:hideMessageBox(true)','OK','bgConfirm16',60,'L','flR','messageBoxButtons');
  showMessageBox(bgMBGray,300);
}

function promptNoRecordDelete()
{
  hideMessageBox();
  formatMessageBox(1,'Non è possibile eliminare i record selezionati','bgWarning48');
  createButton('javascript:hideMessageBox(true)','OK','bgConfirm16',60,'L','flR','messageBoxButtons');
  showMessageBox(bgMBGray,300);
}

function promptNoRecordDeletePart()
{
  hideMessageBox();
  formatMessageBox(1,'Non è possibile eliminare alcuni dei record selezionati','bgWarning48');
  createButton('javascript:hideMessageBox(true)','OK','bgConfirm16',60,'L','flR','messageBoxButtons');
  showMessageBox(bgMBGray,300);
}

function showErrorMessage(msg)
{
  hideMessageBox();
  formatMessageBox(1,msg,'bgError48');
  createButton('javascript:hideMessageBox(true)','OK','bgConfirm16',60,'L','flR','messageBoxButtons');
  showMessageBox(bgMBGray,300);
}

function promptDeleteRecords(rid)
{
  if(rid)
  {
    id=rid;
    ids='';
    ids_chk='';
  }
  else
  {
    id=0;
    ids_chk=multiIdString(d.getElementById('formSelection'));
    ids=multiIdString(d.getElementById('formSelection'),true,true);
  }

  if(id>0 || ids!='')
  {
    if(ids!=ids_chk)
      {promptNoRecordDeletePart();}
    else
    {
      hideMessageBox();
      formatMessageBox(1,'Confermi l\'eliminazione?','bgDelete48');
      createButton('javascript:confirmDelete()','Conferma','bgConfirm16',100,'L','flL','messageBoxButtons');
      createButton('javascript:hideMessageBox(true)','Annulla','bgCancel16',100,'L','flR','messageBoxButtons');
      showMessageBox(bgMBGray,300);
    }
  }
  else
  {
    if(ids_chk!='')
      {promptNoRecordDelete();}
    else
      {promptNoRecordSelected();}
  }
}

function confirmDelete()
{
  if(id>0)
  {
    if(action=='detail')
    {
      window.opener.d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=delete&id='+id;
      window.close();
    }
    else
      {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=delete&id='+id;}
  }
  else if(ids!='')
    {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=delete&ids='+ids;}
}

function openEditWindow(rid,m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=edit&id='+rid+ep,'admin_'+m+'_'+rid,'location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function confirmEdit(rid)
{
  if(rid)
  {
    id=rid;
    ids='';
  }
  else
  {
    id=0;
    ids=multiIdString(d.getElementById('formSelection'));
  }

  if(id>0)
    {openEditWindow(id);}
  else if(ids!='')
  {
    var id_s=ids.split(',');

    for(var i=0;i<id_s.length;i++)
      {openEditWindow(id_s[i]);}
  }
  else
    {promptNoRecordSelected();}

  hideAllLayers();
}

function openMultiEditWindow(m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  ids=multiIdString(d.getElementById('formSelection'));
  if(ids!='')
    {window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=multi_edit&ids='+ids+ep,'admin_'+m+'_multi_edit','location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);}
}

function confirmCopy(rid)
{
  if(rid)
  {
    id=rid;
    ids='';
  }
  else
  {
    id=0;
    ids=multiIdString(d.getElementById('formSelection'));
  }

  if(id>0)
    {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=copy&id='+id;}
  else if(ids!='')
    {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=copy&ids='+ids;}
  else
    {promptNoRecordSelected();}

  hideAllLayers();
}

function confirmMerge(rid)
{
  id=rid;
  ids=multiIdString(d.getElementById('formSelection'));

  if(id>0 && ids!='')
    {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=merge&id='+id+'&ids'+ids;}
  else
    {promptNoRecordSelected();}

  hideAllLayers();
}

function openDetailWindow(rid,m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=detail&id='+rid+ep,'admin_'+m+'_'+rid,'location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function confirmDetail(rid)
{
  if(rid)
  {
    id=rid;
    ids='';
  }
  else
  {
    id=0;
    ids=multiIdString(d.getElementById('formSelection'));
  }

  if(id>0)
    {openDetailWindow(id);}
  else if(ids!='')
  {
    var id_s=ids.split(',');

    for(var i=0;i<id_s.length;i++)
      {openDetailWindow(id_s[i]);}
  }
  else
    {promptNoRecordSelected();}

  hideAllLayers();
}

function openInsertWindow(m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=insert'+ep,'admin_'+m+'_insert','location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function openSearchWindow(m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=search'+ep,'admin_'+m+'_search','location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function openSearchExtWindow(m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=search_ext'+ep,'admin_'+m+'_search_ext','location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function openImageWindow(rid,m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=image&id='+rid+ep,'admin_'+m+'_image_'+rid,'location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function openFileWindow(rid,m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=file&id='+rid+ep,'admin_'+m+'_file_'+rid,'location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function openStructureWindow(m,ep)
{
  if(!m)
    {m=modulo;}
  if(!ep)
    {ep='';}
  window.open(ADMIN_BASE_URL+'main.php?modulo='+m+'&action=structure'+ep,'admin_'+m+'_structure','location=no,menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width='+window.screen.availWidth/2+',height='+window.screen.availHeight/2);
}

function openMainPageFromStructure(m,rn,rid,ep)
{
  if(!ep)
    {ep='';}
  window.opener.d.location=ADMIN_BASE_URL+'main.php?modulo='+m+'&ext_id=1&'+rn+'='+rid+ep;
  window.close();
}

function promptDeleteImage(rid)
{
  hideMessageBox();
  formatMessageBox(1,'Confermi l\'eliminazione?','bgDelete48');
  createButton('javascript:confirmDeleteImage('+rid+')','Conferma','bgConfirm16',100,'L','flL','messageBoxButtons');
  createButton('javascript:hideMessageBox(true)','Annulla','bgCancel16',100,'L','flR','messageBoxButtons');
  showMessageBox(bgMBGray,300);
}

function confirmDeleteImage(rid)
{
  d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=image_delete&id='+rid+'&return_action='+action;
}

function promptDeleteFile(rid)
{
  hideMessageBox();
  formatMessageBox(1,'Confermi l\'eliminazione?','bgDelete48');
  createButton('javascript:confirmDeleteFile('+rid+')','Conferma','bgConfirm16',100,'L','flL','messageBoxButtons');
  createButton('javascript:hideMessageBox(true)','Annulla','bgCancel16',100,'L','flR','messageBoxButtons');
  showMessageBox(bgMBGray,300);
}

function confirmDeleteFile(rid)
{
  d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=file_delete&id='+rid+'&return_action='+action;
}

function setField(field,value)
{
  if(recordFunctionsRowId>0)
    {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=set_field&id='+recordFunctionsRowId+'&function=set_field&field='+field+'&value='+value;}
  else
  {
    ids=multiIdString(d.getElementById('formSelection'));
    if(ids!='')
      {d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&action=set_field&ids='+ids+'&function=set_field&field='+field+'&value='+value;}
    else
      {promptNoRecordSelected();}
  }
}


// Field Check

function checkString(input,msg,ereg)
{
  if(typeof(input)=='string')
    {input=d.getElementById(input);}

  if(ereg)
  {
    er=new RegExp(ereg,"gi");
    if(er.test(input.value))
      {return true;}
  }
  else
  {
    if(input.value!='')
      {return true;}
  }

  showErrorMessage(msg);
  input.style.backgroundColor='#FFBBBB';
  return false;
}


function checkInt(input,msg,chk)
{
  if(typeof(input)=='string')
    {input=d.getElementById(input);}

  if(chk)
  {
    if(chk=='>')
    {
      if(input.value>0)
        {return true;}
    }
    else if(chk=='>=')
    {
      if(input.value>=0)
        {return true;}
    }
    else if(chk=='<')
    {
      if(input.value<0)
        {return true;}
    }
    else if(chk=='<=')
    {
      if(input.value<=0)
        {return true;}
    }
  }
  else
  {
    if(input.value!='' && (input.value>=0 || input.value<0))
      {return true;}
  }

  showErrorMessage(msg);
  input.style.backgroundColor='#FFBBBB';
  return false;
}

function checkIntRT(str,min,max)
{
  return str.replace(/([^0-9\-])+/g,'');
}

function checkFloatRT(str,min,max)
{
  return str.replace(/([^0-9.\-])+/g,'');
}

function checkNumberRange(val,min,max)
{
  res=val;

  if(min!==undefined)
    {res=(res>=min)?res:min;}
  if(max!==undefined)
    {res=(res<=max)?res:max;}

  return res;
}

function getNumberRange(obj)
{
  if(typeof(obj)=='string')
    {obj=d.getElementById(obj);}

  if(obj.onkeyup)
  {
    if(obj.onkeyup.toString().search('checkIntRT')!=-1)
    {
      var mm=obj.onkeyup.toString();
      var sf=mm.indexOf('checkIntRT')+10;
      var sf=mm.indexOf('(',sf)+1;
      var st=mm.indexOf(')',sf);
      mm=mm.substr(sf,st-sf);
      mm=mm.split(',');
    }
    else if(obj.onkeyup.toString().search('checkFloatRT')!=-1)
    {
      var mm=obj.onkeyup.toString();
      var sf=mm.indexOf('checkFloatRT')+10;
      var sf=mm.indexOf('(',sf)+1;
      var st=mm.indexOf(')',sf);
      mm=mm.substr(sf,st-sf);
      mm=mm.split(',');
    }

    if(mm)
    {
      var res=new Array();
      if(mm[1]!==undefined)
        {res['min']=Number(mm[1]);}
      if(mm[2]!==undefined)
        {res['max']=Number(mm[2]);}

      return res;
    }
  else
    {return false;}
  }
  else
    {return false;}
}

function changeLang(i)
{
  d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&O_id_lingua='+i;
}

function submitFormEdit()
{
  frm=d.getElementById('formEdit');
  if(checkFormEdit(frm))
  {
    if(frm.O_close_window.value=='1')
    {
      if(window.opener)
        {frm.target=window.opener.name;}
      frm.return_action.value='';
      frm.submit();
      window.close();
    }
    else
    {
      frm.target='';
      frm.return_action.value='detail';
      frm.submit();
    }
  }
}

function submitFormInsert()
{
  frm=d.getElementById('formInsert');
  if(checkFormInsert(frm))
  {
    if(window.opener)
      {frm.target=window.opener.name;}
    frm.submit();
    if(frm.O_close_window.value=='1')
      {window.close();}
    else
      {d.location=d.location;}
  }
}

function submitFormMultiEdit()
{
  frm=d.getElementById('formMultiEdit');
  if(checkFormMultiEdit(frm))
  {
    frm.submit();
    if(frm.O_close_window.value=='1')
      {window.close();}
    else
      {frm.reset();}
  }
}

function submitFormSearch()
{
  frm=d.getElementById('formSearch');
  if(checkFormSearch(frm))
  {
    if(action=='search')
    {
      if(window.opener)
        {frm.target=window.opener.name;}
    }
    frm.submit();
    if(action=='search')
    {
      if(frm.O_close_window.value=='1')
        {window.close();}
    }
  }
}

function changeTimeout(i)
{
  d.location=ADMIN_BASE_URL+'main.php?modulo='+modulo+'&timeout='+i;
}

function selectOption(select,value)
{
  if(typeof(select)=='string')
    {select=d.getElementById(select);}

  var found=false;
  var i;

  for(i=0;i<select.options.length;i++)
  {
    if(select.options[i].value==value)
    {
      select.options[i].selected=true;
      found=true;
    }
    else
      {select.options[i].selected=false;}
  }

  if(!found)
  {
    for(i=0;i<select.options.length;i++)
    {
      if(select.options[i].value=='' || select.options[i].value=='0')
      {
        select.options[i].selected=true;
        break;
      }
    }
  }
}


// Compatibility

if(!Array.indexOf)
{
  Array.prototype.indexOf=function(obj)
  {
    for(var i=0;i<this.length;i++)
    {
       if(this[i]==obj)
         {return i;}
    }
    return -1;
  }
}


// Browser Detection

var BROWSER='';
var BROWSER_VERSION='';
var BROWSER_VERSION_NUMBER='';

function getBrowser()
{
  var uA=navigator.userAgent;
  if(uA.indexOf('Opera')!=-1)
  {
    BROWSER='Opera';
    var s=uA.indexOf('Opera')+6;
    var e=uA.indexOf(' ',s);
    if(e>0)
      {BROWSER_VERSION=uA.substr(s,e-s);}
    else
      {BROWSER_VERSION=uA.substr(s);}
  }
  else if (uA.indexOf('MSIE')!=-1)
  {
    BROWSER='Internet Explorer';
    var s=uA.indexOf('MSIE ')+4;
    var e=uA.indexOf(';',s);
    BROWSER_VERSION=uA.substr(s,e-s);
  }
  else if (uA.indexOf('Firefox')!=-1)
  {
    BROWSER='Firefox';
    BROWSER_VERSION=uA.substr(uA.indexOf('Firefox/')+8);
  }
  else if (uA.indexOf('Safari')!=-1)
  {
    BROWSER='Safari';
    if(uA.indexOf('Version')!=-1)
      {BROWSER_VERSION=uA.substr(uA.indexOf('Version/')+8,(uA.indexOf(' ',uA.indexOf('Version/')+8))-(uA.indexOf('Version/')+8));}
    else
      {BROWSER_VERSION=uA.substr(uA.indexOf('safari/')+7);}
  }
  else if ((uA.indexOf('mozilla/5.0')!=-1) && (uA.indexOf('rv:')!=-1) && (uA.indexOf('gecko/')!=-1))
  {
    BROWSER='Mozilla';
    var s=uA.indexOf('rv:')+3;
    var e=uA.indexOf(')',s);
    if(e>0)
      {BROWSER_VERSION=uA.substr(s,e-s);}
    else
      {BROWSER_VERSION=uA.substr(s);}
  }

  if(BROWSER_VERSION!='')
  {
    var fp=true;
    var index=0;
    for(var i=0;i<BROWSER_VERSION.length;i++)
    {
      if(BROWSER_VERSION[i]>=0)
        {index=i;}
      else if(BROWSER_VERSION[i]=='.' && fp)
      {
        index=i;
        fp=false;
      }
      else
        {break;}
    }
    BROWSER_VERSION_NUMBER=BROWSER_VERSION.substr(0,index+1);

    if(!(BROWSER_VERSION_NUMBER>0))
      {BROWSER_VERSION_NUMBER='';}
  }
}


// Calendar

var weekDaysStr=new Array();
weekDaysStr[0]='Lunedì';
weekDaysStr[1]='Martedì';
weekDaysStr[2]='Mercoledì';
weekDaysStr[3]='Giovedi';
weekDaysStr[4]='Venerdi';
weekDaysStr[5]='Sabato';
weekDaysStr[6]='Domenica';

var monthsStr=new Array();
monthsStr[0]='Gennaio';
monthsStr[1]='Febbraio';
monthsStr[2]='Marzo';
monthsStr[3]='Aprile';
monthsStr[4]='Maggio';
monthsStr[5]='Giugno';
monthsStr[6]='Luglio';
monthsStr[7]='Agosto';
monthsStr[8]='Settembre';
monthsStr[9]='Ottobre';
monthsStr[10]='Novembre';
monthsStr[11]='Dicembre';

var calendarCloseOnSelect=false;
var calendar_input;

var date=new Date();
var selected_date=new Date();
var month,day,year;

function buildCalendar(input,date_obj,selected_date_obj)
{
  var table,tbody,tr,td;

  if(typeof(input)=='string')
    {input=d.getElementById(input);}
  if(input)
    {calendar_input=input;}
  if(date_obj)
    {date.setTime(date_obj.getTime());}
  else
    {date=new Date();}
  if(selected_date_obj)
    {selected_date.setTime(selected_date_obj.getTime());}

  day=date.getDate();
  month=date.getMonth()+1;
  year=date.getFullYear();

  var days=getDaysInMonth(date);
  var first_day=getDateFromDMY(1,month,year).getDay();
  var dt=1;

  removeAllChild(d.getElementById('calendarContainer'));
  table=d.createElement('table');
  table.id='calendar';
  table.setAttribute('cellSpacing','1');
  table.className='txt ts11 taC'
  tbody=d.createElement('tbody');
  table.appendChild(tbody);
  d.getElementById('calendarContainer').appendChild(table);

  tr=d.createElement('tr');
  tbody.appendChild(tr);

  td=d.createElement('td');
  td.className='crPointer';
  td.innerHTML='&nbsp;';
  td.style.backgroundImage='url('+ADMIN_IMAGES_URL+'calendar_16.png)';
  tr.appendChild(td);

  td=d.createElement('td');
  td.setAttribute('colSpan','5');
  td.style.width='100px';
  input=d.createElement('input');
  input.type='text';
  input.value=year;
  input.className='txt ts11 tstB taR dB flL';
  input.size='6';
  input.id='calendarYearInput';
  input.style.margin='0px 4px 0px 20px';
  setEvent(input,'onkeyup','this.value=checkIntRT(this.value,1902,2037);');
  setEvent(input,'onblur','setScrollInput(null);this.value=checkNumberRange(this.value,1902,2037);');
  setEvent(input,'onmouseout','setOverScrollInput(this,false);');
  setEvent(input,'onmouseover','setOverScrollInput(this,true);');
  setEvent(input,'onfocus','setScrollInput(this);');
  td.appendChild(input);
  img=d.createElement('img');
  img.src=ADMIN_IMAGES_URL+'next_16.png';
  img.className='dB flL crPointer';
  setEvent(img,'onclick', "changeYear(d.getElementById('calendarYearInput').value)");
  td.appendChild(img);
  tr.appendChild(td);

  td=d.createElement('td');
  td.className='crPointer';
  td.innerHTML='&nbsp;';
  td.style.backgroundImage='url('+ADMIN_IMAGES_URL+'close_16.png)';
  setEvent(td,'onclick', "setVisibility('calendarContainer','visibility','hidden',true,100,0)");
  tr.appendChild(td);

  tr=d.createElement('tr');
  tr.className='tstB';
  tbody.appendChild(tr);

  td=d.createElement('td');
  td.className='crPointer';
  td.innerHTML='&nbsp;';
  td.style.backgroundImage='url('+ADMIN_IMAGES_URL+'page_prev_fast_16.png)';
  setEvent(td,'onclick', 'prevYear()');
  tr.appendChild(td);

  td=d.createElement('td');
  td.className='crPointer';
  td.innerHTML='&nbsp;';
  td.style.backgroundImage='url('+ADMIN_IMAGES_URL+'page_prev_16.png)';
  setEvent(td,'onclick', 'prevMonth()');
  tr.appendChild(td);

  td=d.createElement('td');
  td.innerHTML='&nbsp;';
  td.setAttribute('colSpan','3');
  td.className='ts10';
  td.innerHTML=monthsStr[month-1];
  tr.appendChild(td);

  td=d.createElement('td');
  td.className='crPointer';
  td.innerHTML='&nbsp;';
  td.style.backgroundImage='url('+ADMIN_IMAGES_URL+'page_next_16.png)';
  setEvent(td,'onclick', 'nextMonth()');
  tr.appendChild(td);

  td=d.createElement('td');
  td.className='crPointer';
  td.innerHTML='&nbsp;';
  td.style.backgroundImage='url('+ADMIN_IMAGES_URL+'page_next_fast_16.png)';
  setEvent(td,'onclick', 'nextYear()');
  tr.appendChild(td);


  tr=d.createElement('tr');
  tr.className='tstB'
  tbody.appendChild(tr);
  for(var dy=0;dy<7;dy++)
  {
    td=d.createElement('td');
    td.innerHTML=weekDaysStr[dy].substr(0,1);
    tr.appendChild(td);
  }

  for(var w=0;dt<=days;w++)
  {
    tr=d.createElement('tr');
    tbody.appendChild(tr);
    for(var dy=1;dy<=7;dy++)
    {
      td=d.createElement('td');

      if((w>0 || dy>=first_day) && dt<=days)
      {
        td.className='crPointer';

        if(dt==(new Date()).getDate() && month==((new Date()).getMonth()+1) && year==(new Date()).getFullYear())
          {td.style.borderColor='#CC0000';}

        if(dt==selected_date.getDate() && month==(selected_date.getMonth()+1) && year==selected_date.getFullYear())
          {td.style.backgroundColor='#FFCC00';}

        td.innerHTML=dt;
        setEvent(td,'onclick', 'setCalendarInput(this,'+dt+')');

        dt++;
      }
      else
        {td.innerHTML='&nbsp;';}

      tr.appendChild(td);
    }
  }
}

function getDaysInMonth(date)
{
  var m=date.getMonth();

  if(m==0 || m==2 || m==4 || m==6 || m==7 || m==9 || m==11)
    {return 31;}
  else if(m==3 || m==5 || m==8 || m==10)
    {return 30;}
  else if(m==1)
  {
    if(((date.getFullYear()%4)==0 && (date.getFullYear()%100)!=0) || (date.getFullYear()%400)==0)
      {return 29;}
    else
      {return 28;}
  }
  else
    {return 0;}
}

function nextMonth()
{
  date.setDate(1);
  date.setMonth(date.getMonth()+1);
  buildCalendar(calendar_input,date);
}

function prevMonth()
{
  date.setMonth(date.getMonth()-1);
  buildCalendar(calendar_input,date);
}

function nextYear()
{
  date.setDate(1);
  date.setFullYear(date.getFullYear()+1);
  buildCalendar(calendar_input,date);
}

function prevYear()
{
  date.setFullYear(date.getFullYear()-1);
  buildCalendar(calendar_input,date);
}

function changeYear(y)
{
  date.setFullYear(y);
  buildCalendar(calendar_input,date);
}

function setCalendarInput(cell,day)
{
  selected_date.setDate(day);
  selected_date.setMonth(month-1);
  selected_date.setFullYear(year);
  resetCalendarCellBG();
  cell.style.backgroundColor='#FFCC00';
  if(calendar_input)
    {calendar_input.value=((selected_date.getDate()<10)?'0'+(selected_date.getDate()):(selected_date.getDate()))+'/'+((selected_date.getMonth()+1<10)?'0'+(selected_date.getMonth()+1):(selected_date.getMonth()+1))+'/'+selected_date.getFullYear();}
  if(calendarCloseOnSelect)
    {setVisibility('calendarContainer','visibility','hidden',true,100,0);}

  if(action=='search')
    {setInputValue('S_'+calendar_input.id.substr(7),getDateTimeStrFromFields(calendar_input.id.substr(7)));}
  else
  {
    setInputValue('F_'+calendar_input.id.substr(7),getDateTimeStrFromFields(calendar_input.id.substr(7)));
    setInputValue('C_'+calendar_input.id.substr(7),1);
    if(action=='multi_edit')
      {setCheckbox('MEC_'+calendar_input.id.substr(7),1);}
    if(action=='search' || action=='search_ext')
      {setCheckbox('SC_'+calendar_input.id.substr(7),1);}
  }
}

function resetCalendarCellBG()
{
  tb=d.getElementById('calendar').tBodies[0];
  for(r=0;r<tb.rows.length;r++)
  {
    for(c=0;c<tb.rows[r].cells.length;c++)
      {tb.rows[r].cells[c].style.backgroundColor='#FFFFFF';}
  }
}

function getDateFromDMY(day,month,year)
{
  var date=new Date();
  date.setDate(day);
  date.setMonth(month-1);
  date.setFullYear(year);
  return date;
}

function getDateFromSTR(dmy_str)
{
  var dmy=dmy_str.split('/');
  return getDateFromDMY(dmy[0],dmy[1],dmy[2]);
}

function getDateFromUnixTime(unix_time)
{
  var date=new Date();
  if(unix_time>0)
    {date.setTime(unix_time*1000);}
  return date;
}

function showCalendar(input,date,selected_date,close)
{
  calendarCloseOnSelect=close;
  buildCalendar(input,getDateFromUnixTime(date),getDateFromUnixTime(selected_date));

  var cc=d.getElementById('calendarContainer');
  cc.style.top=getYPos(input)+'px';
  cc.style.left=getXPos(input)+'px';

  setVisibility(cc,'visibility','visible',true,0,100);
}

function getDateTimeStrFromFields(field)
{
  if(d.getElementById('F_date_'+field))
    {var date=d.getElementById('F_date_'+field).value;}
  else
    {var date='01/01/1970';}

  if(d.getElementById('F_h_'+field))
    {date=date+'-'+d.getElementById('F_h_'+field).value+':'+d.getElementById('F_m_'+field).value+':'+d.getElementById('F_s_'+field).value;}
  else
    {date=date+'-00:00:00';}

  return date;
}