var ns4;
var op5;
var op6;
var agt;
var mac;
var ie;
var mac_ie;
var moz;
var htmphttp;

sniffBrowsers();

SetCookie('DocbWeb2ScreenWidth',screen.width,10000,false);
SetCookie('DocbWeb2ScreenHeight',screen.height,10000,false);

function GetCookie(cookieName)
{
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName);
if (ind==-1 || cookieName=="") return null;
var ind1=theCookie.indexOf(';',ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue,nDays,escape)
{
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0)
  {
    if (escape)
      document.cookie = cookieName + "=" +escape(cookieValue) + ";path=/";
    else
      document.cookie = cookieName + "=" +cookieValue + ";path=/";
  }
  else
  {
    expire.setTime(today.getTime() + 3600000*24*nDays);

    if (escape)
      document.cookie = cookieName + "=" +escape(cookieValue) + ";expires="+expire.toGMTString() + ";path=/";
    else
      document.cookie = cookieName + "=" +cookieValue + ";expires="+expire.toGMTString() + ";path=/";
  }
}


function SetDesmarcar(o)
{
var child,i;

    for (i=0;i<o.form.childNodes.length;i++)
    {
      child = o.form.childNodes[i];
      if (child.nodeType == 3)
      {
        child.nodeValue = 'Desmarcar';
        break;
      }
    }
}

function SetMarcar(o)
{
var child,i;

    for (i=0;i<o.form.childNodes.length;i++)
    {
      child = o.form.childNodes[i];
      if (child.nodeType == 3)
      {
        child.nodeValue = 'Marcar';
        break;
      }
    }
}


function RecordSelect(o, cookie_name, mfn)
{
 
    if ((GetCookie(cookie_name) == null) || (GetCookie(cookie_name) == ""))
    {
      if (o.checked)
      {
        SetDesmarcar(o);
        SetCookie(cookie_name,"&" + mfn + "=checked",10000,false)
      }
      else
      {
        SetMarcar(o);
        SetCookie(cookie_name,"&" + mfn + "=unchecked",10000,false)
      }
    }
    else
    {
      cur_cookievalue = GetCookie(cookie_name)
      if (o.checked)
      {
        SetDesmarcar(o);
        if (cur_cookievalue.indexOf("&"+mfn+"=unchecked") != -1)
        {
          cur_cookievalue = cur_cookievalue.replace("&"+mfn+"=unchecked","&" + mfn + "=checked");
        }
        else
        {
          cur_cookievalue = cur_cookievalue + "&" + mfn + "=checked";
        }
          
        /*cur_cookievalue.replace("&"+mfn+"=checked","&" + mfn + "=checked")*/
        /*SetCookie(cookie_name,cur_cookievalue + "&" + mfn + "=checked",10000,false)*/
      }
      else
      {
        SetMarcar(o);       
        if (cur_cookievalue.indexOf("&"+mfn+"=checked") != -1)
        {
          cur_cookievalue = cur_cookievalue.replace("&"+mfn+"=checked","&" + mfn + "=unchecked");
        }
        else
        {
          cur_cookievalue = cur_cookievalue + "&" + mfn + "=unchecked";
        }
          
        /*cur_cookievalue.replace("&"+mfn+"=unchecked","&" + mfn + "=unchecked")*/
        /*SetCookie(cookie_name,cur_cookievalue + "&" + mfn + "=unchecked",10000,false)*/
      }
      SetCookie(cookie_name,cur_cookievalue,10000,false)
    }
}


function ExportIso2709(BaseName,SessionID,CookieName)
{
  window.open("Iso2709.asp?Base=" + BaseName + "&SID=" + SessionID + "&Cookie=" + CookieName,"_blank","height=150,width=400,top=50,left=50");
}

function showDesc(obj)
{
  if (obj.id == 'simples')
    window.status = 'Pesquisa simples';
  if (obj.id == 'avançada')
    window.status = 'Pesquisa avançada';
  if (obj.id == 'índice')
    window.status = 'Pesquisa por índice';
  if (obj.id == 'thesaurus')
    window.status = 'Pesquisa por thesaurus';
  if (obj.id == 'cdu simples')
    window.status = 'Pesquisa temática simples';
  if (obj.id == 'cdu avançada')
    window.status = 'Pesquisa temática avançada';
  if (obj.id == 'links') 
    window.status = 'Pequisa de links';
  if (obj.id == 'favoritos')
    window.status = 'Favoritos';
  if (obj.id == 'notícias')
    window.status = 'Notícias';
    
  return true;    
}   

function hideDesc()
{
  window.status = '';
}


function ChangeImage(obj, image)
{
  obj.src = image;
}



function CommentWindow(WebMail)
{
  window.open("comment.asp?Email=" + WebMail,"_blank","height=300,width=450,top=50,left=50"); 
}

function doTooltip(e, msg)
{
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, msg);
}

function hideTip()
{
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}


function ToggleViewItems(o, mfn)
{
var divelement,child,i, aelement;

  aelement = document.getElementById("ItemLink" + mfn);

  divelement = document.getElementById("SitExemplar" + mfn);
  
  if (divelement != null)
  {
    if (divelement.style.display == "")
    {
      divelement.style.display = "none";
      
      if (aelement.firstChild != null)
        if (aelement.firstChild.firstChild != null)
          aelement.firstChild.firstChild.nodeValue = 'Ver Informação de Exemplares';
    
    }
    else
    {
      divelement.style.display = "";
     
      if (aelement.firstChild != null)
        if (aelement.firstChild.firstChild != null)
          aelement.firstChild.firstChild.nodeValue = 'Esconder Informação de Exemplares';
      
    }
  }
  
}

function findPosY(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; 
}

function ScrollToElement(theElement)
{ 
var selectedPosX = 0; 
var selectedPosY = 0;

  while(theElement != null)
  { 
    selectedPosX += theElement.offsetLeft; 
    selectedPosY += theElement.offsetTop; 
    theElement = theElement.offsetParent; 
  } 
  
  window.scrollTo(selectedPosX,selectedPosY-document.body.clientHeight); 
}


function getElementHeight(Elem) 
{
var yPos;

  if (ns4) 
  {
    var elem = getObjNN4(document, Elem);
    return elem.clip.height;
  } 
  else 
  {
    if(document.getElementById) 
    {
      var elem = document.getElementById(Elem);
    } 
    else if (document.all)
    {
      var elem = document.all[Elem];
    }
    if (elem!=null) 
    {
      if (op5) 
      { 
        yPos = elem.style.pixelHeight;
      } 
      else 
      {
        yPos = elem.offsetHeight;
      }
    }
    return yPos;
  } 
}


function setElementHeight(Elem,NewHeight) 
{
  if (ns4) 
  {
    var elem = getObjNN4(document, Elem);
    elem.clip.height = NewHeight;
  } 
  else 
  {
    if(document.getElementById) 
    {
      var elem = document.getElementById(Elem);
    } 
    else if (document.all)
    {
      var elem = document.all[Elem];
    }
    if (elem!=null) 
    {
      if (op5) 
      { 
        elem.style.pixelHeight = NewHeight;
      } 
      else 
      {
        if (NewHeight<300) NewHeight=300;
        elem.style.height = NewHeight;
      }
    }
  } 
}

function getElementWidth(Elem) 
{
var xPos = -1;

  if (ns4) {
    var elem = getObjNN4(document, Elem);
    return elem.clip.width;
  } 
  else 
  {
    if (document.getElementById) 
    {
      var elem = document.getElementById(Elem);
    } 
    else if (document.all)
    {
      var elem = document.all[Elem];
    }
    if (elem!=null) 
    {
      if (op5) 
      {
        xPos = elem.style.pixelWidth;    
      } 
      else 
      {
        xPos = elem.offsetWidth;
      }
    }
    return xPos;
  }
}



function sniffBrowsers() 
{ 
  ns4 = document.layers; 
  op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) ||(navigator.userAgent.indexOf("Opera/5")!=-1); 
  op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) ||(navigator.userAgent.indexOf("Opera/6")!=-1); 
  agt=navigator.userAgent.toLowerCase();
  mac = (agt.indexOf("mac")!=-1);
  ie = (agt.indexOf("msie") != -1);
  moz = (agt.indexOf("firefox") != -1);
  mac_ie = mac && ie;
}  

function loadHTMLDoc(url,onready)
{
  try
  {
    htmlhttp = new XMLHttpRequest();
  }
  catch(ee)
  {
    try
    {
      htmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        htmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(E)
      {
        htmlhttp = false;
      }
    }
  }
  htmlhttp.onreadystatechange = onready;
  htmlhttp.open("GET", url, true);
  htmlhttp.send(null);
}

function ToggleStyle(divel,mode)
{
  if (mode == 0)
    divel.className = "ButtonNormal";
  if (mode == 1)
    divel.className = "ButtonMouseOver";
  if (mode == 2)
    divel.className = "ButtonMouseDown";
}

function SubmitForm(formname)
{
var f = document.forms;

  for (i=0;i<f.length;i++)
  {
    if (f[i].name == formname)
    {
      f[i].submit();
      break;
    }
  }
}


function ParentForm(obj)
{
  if (obj.parentElement)
  {
    while (obj.parentElement)
    {
      if (obj.tagName == 'FORM')
        return obj;
      obj = obj.parentElement;
    }
  }
  return obj;
}

function LoadHTMLDocEx(url,target,func)
{

  function whenDone()
  {
    if (req.readyState == 4)
    {
      if (req.status == 200)
      {
        document.getElementById(target).innerHTML = req.responseText;
        if (func != '') eval(func+"()");
      }
      else
      {
        document.getElementById(target).innerHTML = req.statusText;
      }
    }
  }
  
  try
  {
    var req = new XMLHttpRequest();
  }
  catch(ee)
  {
    try
    {
      var req = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        var req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(E)
      {
        var req = false;
      }
    }
  }
  if (req)
  {
    req.onreadystatechange = function() { whenDone(); };
    req.open("GET", url, true);
    req.send(null);
  }
}      

/* só funciona chamado depois da tag <body> */
function clientWidth()
{
  if (document.compatMode && document.compatMode != "BackCompat")
    return(document.documentElement.clientWidth);
  else  
    return(document.body.clientWidth);
}

/* só funciona chamado depois da tag <body> */
function clientHeight()
{
  if (document.compatMode && document.compatMode != "BackCompat")
    return(document.documentElement.clientHeight);
  else  
    return(document.body.clientHeight);
}

function screensize()
{
  if (ie)
  {
    cwidth = document.documentElement.offsetWidth;
    cheight = document.documentElement.offsetHeight;
  }
  if (moz)
  {
    cwidth = document.body.clientWidth;
    cheight = document.body.clientHeight;
  }
}
