function show(portlet_title){
  var opened = portlet_title.opened.Value()=="true";
  if(opened)
    return;
  var content_area=portlet_title.content_area.Value();
  var content_url=portlet_title.content_url.Value();
  var loaded= opened || portlet_title.loaded;
  if(!loaded){
    var iframes=document.getElementById(content_area).getElementsByTagName("iframe");
    if(!iframes.length){
      ZtVWeb.Include("../jsp/"+content_url, content_area);
      portlet_title.loaded=true;
    } else {
      iframes[0].src=document.getElementById(content_area+'_src').value;
      LibJavascript.Events.addEvent(iframes[0],'load',function(){
        effectOpenClose2(content_area,true);
      });
    }
  }
  portlet_title.opened.Value("true");

  div=document.getElementById(content_area);
  div.style.height='1px';
  div.style.display='';
  div.style.overflow='hidden';

  window.setTimeout(function(){
    effectOpenClose2(content_area,true);
  }, 150);
}

function hide(portlet_title){
  var opened = portlet_title.opened.Value()=="true";
  if(!opened)
    return;
  portlet_title.loaded=true;
  var content_area=portlet_title.content_area.Value();
  portlet_title.opened.Value("false");
  var div=document.getElementById(content_area);
  div.style.overflow='hidden';
  effectOpenClose2(content_area,false);
}

function adjustContainer(containerName){   // Funzione chiamata dai control che si allungano in esecuzione (Es Grid o treeView)
  var containerCtrl=document.getElementById(containerName)
  if (containerCtrl!=null){
    if(containerCtrl.tagName.toLowerCase()=='iframe' && containerCtrl.getAttribute("toResize")!='no'){
      var content=containerCtrl.contentWindow.document.body;
      if(Math.abs(parseInt(containerCtrl.style.height) - parseInt(content.scrollHeight)) > 1) {
        containerCtrl.style.height =  content.scrollHeight;
        containerCtrl.parentNode.style.height=content.scrollHeight;
      }
    }
  }
}

function effectOpenClose2(el,open){
  var DELTA_H=80, DEALY=50;
  if(IsA(el, 'C'))
    el=document.getElementById(el);
  if(open){
    var content;
    if(el.tagName.toLowerCase()=='iframe'){
      content=el.contentWindow.document.body;
    }else
      content=el
    if(el.offsetHeight<content.scrollHeight){
      el.style.height = (el.offsetHeight + Math.min(DELTA_H, content.scrollHeight-content.offsetHeight))+'px';
      window.setTimeout(function(){
        effectOpenClose2(el,open);
      }, DEALY);
    }else{
      el.style.height=el.scrollHeight+'px';
      el.style.overflow='';
    }
  }else{
    if(el.offsetHeight<=1){
      el.style.height='0px';
      el.style.display='none';
    }else{
      el.style.height=Math.max(el.offsetHeight-DELTA_H, 1);
      window.setTimeout(function(){
        effectOpenClose2(el,open);
      }, DEALY);
    }
  }
}

function getTitle(title_portlet){
  return title_portlet.title.Value();
}
function getPortletName(obj){
  var ele =document.getElementById(obj.formid)
  while(ele.tagName!='INPUT')
    ele=ele.previousSibling
  return  Strtran(ele.name,"_portlet.jsp","")
}

function effectOpenClose(id,open){
  var frame=document.getElementById(id)
  if(frame!=null){
    if(open){
      if(frame.offsetHeight>=frame.contentWindow.document.body.scrollHeight && frame.offsetHeight>0){
        frame.style.height=frame.contentWindow.document.body.scrollHeight;
        return;
      }
      frame.style.height=frame.offsetHeight+40
    }else{
      if(frame.offsetHeight<=1){
        frame.style.height='0px';
        frame.style.display='none';
        return;
      }
      frame.style.height=Math.max(frame.offsetHeight-40,0)
    }
    if(typeof(frame.src)!='undefined' && frame.src!='') setTimeout("effectOpenClose('"+id+"',"+open+")",10)
  }
}

function resizeIframe(frame,src){
  if(Empty(frame)) return;
  if(IsA(frame,'C')) frame=document.getElementsByName(frame)[0];
  if(src && frame.src!=src){
    frame.src=src;
    return;
  }
  //Per caricare URL esterni
  try{window.frames[frame.name].document}catch(e){frame.style.height='100%';return;}
  if(('m_cCaption' in window.frames[frame.name] && window.frames[frame.name].m_nRowsPerPage!=null && window.frames[frame.name].m_oTrsQuery!=null && window.frames[frame.name].m_oTrsQuery.length>0) || window.frames[frame.name].m_cProgName=='reportproperty' || window.frames[frame.name].m_cProgNameAlter=='reportproperty' || window.frames[frame.name].document.title=='Portal Studio'){ //nel caso sia una page
    frame.style.height='100%';//soluzione momentanea
    try{
      frame.parentNode.style.height='100%';
      frame.parentNode.parentNode.style.height='100%';
    }catch(e){}
  }else if('m_nPreferredHeight' in window.frames[frame.name]){
    frame.style.height=window.frames[frame.name].GetWindowPreferredSize().h;
  }else{
    frame.style.height=frame.contentWindow.document.body.scrollHeight;
  }
  if(window.frameElement!=null && typeof(parent.resizeIframe)!="undefined") parent.resizeIframe(window.frameElement);
}

/*
function resizeIframe(frame,src){

  if(IsA(frame,'C')) frame=document.getElementsByName(frame)[0];
  if(src && frame.src!=src){
    frame.src=src;
    return;
  }
  //Per caricare URL esterni
  try{window.frames[frame.name].document}catch(e){frame.style.height='100%';return;}
  if ('m_nPreferredHeight' in window.frames[frame.name])  {
    frame.style.height=window.frames[frame.name].GetWindowPreferredSize().h;
  }else if(('m_cCaption' in window.frames[frame.name] && window.frames[frame.name].m_nRowsPerPage!=null) || window.frames[frame.name].m_cProgName=='reportproperty' || window.frames[frame.name].m_cProgNameAlter=='reportproperty' || window.frames[frame.name].document.title=='Portal Studio'){ //nel caso sia una page{
    frame.style.height='100%';//soluzione momentanea
  }else{
    frame.style.height=frame.contentWindow.document.body.scrollHeight;
  }
  if(window.frameElement!=null && typeof(parent.resizeIframe)!="undefined") parent.resizeIframe(window.frameElement);

}
*/
function collapseCol(id,closeWidth){
  var col_=document.getElementById(id+"_container")
  col_.style.display='none'
  if(closeWidth==null )  closeWidth=18
  var col_openclose=document.getElementById(id+"_openclose")
  col_openclose.style.width=closeWidth+'px' //larghezza minima del portlet decoratore
  ZtVWeb.windowResized('da colonna chiusa')
}

function openCol(id){
  var col_=document.getElementById(id+"_container")
  col_.style.display='block'
  var col_openclose=document.getElementById(id+"_openclose")
  col_openclose.style.width=col_.style.width
  var iframes=document.getElementsByTagName('IFRAME');
  for(var i=0,iframe; iframe=iframes[i++]; resizeIframe(iframe));
    ZtVWeb.windowResized()
  //ZtVWeb.AdjustHeight();
  //ZtVWeb._onrsz()
}

function addCombo(id,value,caption){
  var obj=document.getElementById(id);
  var opt=new Option;
  opt.text=caption;
  opt.value=value;
  obj.options[obj.options.length] = opt;
}

function appendPortlet(group,url,title,opened){
  var container=document.getElementById(group+"_container");  // necessario aggiungere il div contenitore globale del gruppo

  var cnt_container=document.createElement('div');
  cnt_container.id=url+"_container"
  //cnt_container.style.display='none'
  container.appendChild(cnt_container);


  var cnt_title=document.createElement('div');
  cnt_title.id=url+"_title"
  cnt_title.style.overflow='hidden'
  cnt_container.appendChild(cnt_title);
  var cnt_content=document.createElement('div');
  cnt_content.id=url+"_content"
  cnt_content.style.display='block';
  cnt_container.appendChild(cnt_content);

  var title_par="?content_url="+url+"&content_area="+url+"_content&title="+title+"&opened="+opened;

  ZtVWeb.Include("../jsp-decorators/default_title_portlet.jsp"+title_par,cnt_title);
  ZtVWeb.Include(url,cnt_content);



}

function selectGroupItem(combo,single,title_close){
  if(combo["value"]!=''){
    var valueobj=new Function("return "+combo["value"]+";")();
    var url_selected=valueobj["url"];
    var container_selected=valueobj["container"];
    var content_selected=valueobj["content"];
    var valueobj_tmp;
    if(single){
      for(var i=1;i<combo.options.length;i++){
        valueobj_tmp=new Function("return "+combo.options[i].value+";")()
        if(valueobj_tmp.container!=container_selected){
          document.getElementById(valueobj_tmp.container).style.display='none';
        }else{
          document.getElementById(valueobj_tmp.container).style.display='block';
          if(Empty(document.getElementById(content_selected).innerHTML) && !title_close){
            ZtVWeb.Include(url_selected,document.getElementById(content_selected));
          }
        }
      }
    }else{
      document.getElementById(container_selected).style.display='block';
      if(Empty(document.getElementById(content_selected).innerHTML) && !title_close){
        ZtVWeb.Include(url_selected,document.getElementById(content_selected));
      }
    }
  }else{
    for(var i=1;i<combo.options.length;i++){
      valueobj_tmp=new Function("return "+combo.options[i].value+";")()
      document.getElementById(valueobj_tmp["container"]).style.display='none';
    }
  }
}
