//
// barratopo.new.js
//
var CatContainer = "CategoriaContainer";
var FabContainer = "FabricanteContainer";
var CatSelContainer = "selcategorias";
var FabSelContainer = "fabricantes";
var GuiaContainer = "GuiaContainer";

function getY( oElement ) {
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

function getX( oElement ) {
  var iReturnValue = 0;
  while( oElement != null ) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
  }
  return iReturnValue;
}

function getWindowHeight() {
  return window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
}

function getWindowWidth() {
  return window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
}

function Set_MaxSize( obj ) {
  var x = getX( obj );
  var y = getY( obj );
  var WinW = getWindowWidth();
  var WinH = getWindowHeight();
  obj.style.maxWidth = ( WinW - x - 30 ).toString() + "px";
  obj.style.maxHeight = ( WinH - y - 30 ).toString() + "px";
  obj.style.overflow = "auto";
  return 0;
}

function setCatContainer( Container ) {
  CatContainer = Container
}

function UpdateCat( Request ) {
  var Container = document.getElementById( CatContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = Request.responseText;
}

function UpdateCatLoading() {
  var Container = document.getElementById( CatContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = "<img src='images/wait.gif' />";
}

function UpdateCatError() {
  var Container = document.getElementById( CatContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = 'Ocorreu um erro no script ou no requester';
}

function GetCategoriasHTML() {
  var randomnumber = Math.floor( Math.random() * 1001 );
  var ListaURL = 'listas/categorias.colunas.html';
  if ( document.location.protocol == 'https:' ) {
    ListaURL = 'categorias.colunas.inc.asp';
  }
  var Request = new Ajax.Request( ListaURL,
                                  { method: 'get',
                                    parameters: { foo: randomnumber },
                                    onSuccess: UpdateCat,
                                    onFailure: UpdateCatError,
                                    onLoading: UpdateCatLoading } );
}

function Menus_Off() {
  var mnuEmpresa = document.getElementById( "mnuEmpresa" );
  var mnuProdutos = document.getElementById( "mnuProdutos" );
  var mnuMarcados = document.getElementById( "mnuMarcados" );
  var mnuBusca = document.getElementById( "mnuBusca" );
  var btnProdutos = document.getElementById( "btnProdutos" );
  var btnEmpresa = document.getElementById( "btnEmpresa" );
  var btnMarcados = document.getElementById( "btnMarcados" );
  mnuEmpresa.style.display = "none";
  mnuProdutos.style.display = "none";
  mnuMarcados.style.display = "none";
  mnuBusca.style.display = "none";
  btnProdutos.style.backgroundColor = "";
  btnProdutos.style.color = "";
  btnEmpresa.style.backgroundColor = "";
  btnEmpresa.style.color = "";
  btnMarcados.style.backgroundColor = "";
  btnMarcados.style.color = "";
  // evento
  document.body.onclick = function() {
    Menus_Off();
  };
}

function Toggle_Empresa( e ) {
  var mnuEmpresa = document.getElementById( "mnuEmpresa" );
  var btnEmpresa = document.getElementById( "btnEmpresa" );
  if ( mnuEmpresa.style.display != "block" ) {
    Menus_Off();
    mnuEmpresa.style.display = "block";
    btnEmpresa.style.backgroundColor = "#e77817";
    btnEmpresa.style.color = "white";
  }
  else {
    Menus_Off();
  }
  e.cancelBubble = true;
  return true;
}

function Toggle_Produtos( e ) {
  var mnuProdutos = document.getElementById( "mnuProdutos" );
  var btnProdutos = document.getElementById( "btnProdutos" );
  if ( mnuProdutos.style.display != "block" ) {
    Menus_Off();
    mnuProdutos.style.display = "block";
    btnProdutos.style.backgroundColor = "#e77817";
    btnProdutos.style.color = "white";
  }
  else {
    Menus_Off();
  }
  e.cancelBubble = true;
  return true;
}

function Toggle_Marcados( e ) {
  var mnuMarcados = document.getElementById( "mnuMarcados" );
  var btnMarcados = document.getElementById( "btnMarcados" );
  if ( mnuMarcados.style.display != "inline-block" ) {
    Menus_Off();
    mnuMarcados.style.display = "inline-block";
    btnMarcados.style.backgroundColor = "#e77817";
    btnMarcados.style.color = "white";
  }
  else {
    Menus_Off();
  }
  e.cancelBubble = true;
  return true;
}

function Toggle_Busca( e, onoff ) {
  var mnuBusca = document.getElementById( "mnuBusca" );
  var palavras = document.getElementById( "palavras" );
  var chkCategoria = document.getElementById( "chkCategoria" );
  var chkFabricante = document.getElementById( "chkFabricante" );
  var categorias = document.getElementById( CatSelContainer );
  var fabricantes = document.getElementById( "fabricantes" );
  if ( onoff ) {
    Menus_Off();
    mnuBusca.style.display = "inline-block";
    palavras.focus();
  }
  else {
    mnuBusca.style.display = "none";
  }
  palavras.value = "";
  chkCategoria.checked = false;
  categorias.style.display = "none";
  chkFabricante.checked = false;
  fabricantes.style.display = "none";
  e.cancelBubble = true;
  return true;
}

function MenuProdutos_Off() {
  var btnToggleCategoria1 = document.getElementById( "btnToggleCategoria1" );
  var btnToggleFabricante = document.getElementById( "btnToggleFabricante" );
  var btnToggleGuia = document.getElementById( "btnToggleGuia" );
  var mnuCategoria1 = document.getElementById( "mnuCategoria1" );
  var mnuFabricante = document.getElementById( "mnuFabricante" );
  var mnuGuia = document.getElementById( "mnuGuia" );
  btnToggleCategoria1.style.backgroundColor = "";
  btnToggleCategoria1.style.color = "";
  btnToggleFabricante.style.backgroundColor = "";
  btnToggleFabricante.style.color = "";
  btnToggleGuia.style.backgroundColor = "";
  btnToggleGuia.style.color = "";
  mnuFabricante.style.display = "none";
  mnuCategoria1.style.display = "none";
  mnuGuia.style.display = "none";
}

function Toggle_Categoria1( e ) {
  var btnToggleCategoria1 = document.getElementById( "btnToggleCategoria1" );
  var mnuCategoria1 = document.getElementById( "mnuCategoria1" );
  if ( mnuCategoria1.style.display != "inline-block" ) {
    MenuProdutos_Off();
    mnuCategoria1.style.display = "inline-block";
    btnToggleCategoria1.style.backgroundColor = "#e77817";
    btnToggleCategoria1.style.color = "white";
    GetCategoriasHTML();
  }
  else {
    MenuProdutos_Off();
  }
  e.cancelBubble = true;
  return true;
}

function setFabContainer( Container ) {
  FabContainer = Container
}

function UpdateFab( Request ) {
  var Container = document.getElementById( FabContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = Request.responseText;
}

function UpdateFabLoading() {
  var Container = document.getElementById( FabContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = "<img src='images/wait.gif' />";
}

function UpdateFabError() {
  var Container = document.getElementById( FabContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = 'Ocorreu um erro no script ou no requester';
}

function GetFabricantesHTML() {
  var randomnumber = Math.floor( Math.random() * 1001 );
  var ListaURL = 'listas/fabricantes.colunas.html';
  if ( document.location.protocol == 'https:' ) {
    ListaURL = 'fabricantes.colunas.inc.asp';
  }
  var Request = new Ajax.Request( ListaURL,
                                  { method: 'get',
                                    parameters: { foo: randomnumber },
                                    onSuccess: UpdateFab,
                                    onFailure: UpdateFabError,
                                    onLoading: UpdateFabLoading } );
}

function Toggle_Fabricante( e ) {
  var btnToggleFabricante = document.getElementById( "btnToggleFabricante" );
  var mnuFabricante = document.getElementById( "mnuFabricante" );
  if ( mnuFabricante.style.display != "inline-block" ) {
    MenuProdutos_Off();
    mnuFabricante.style.display = "inline-block";
    //Set_MaxSize( mnuFabricante );
    btnToggleFabricante.style.backgroundColor = "#e77817";
    btnToggleFabricante.style.color = "white";
    GetFabricantesHTML();
  }
  else {
    MenuProdutos_Off();
  }
  e.cancelBubble = true;
  return true;
}

function setGuiaContainer( Container ) {
  GuiaContainer = Container
}

function UpdateGuia( Request ) {
  var Container = document.getElementById( GuiaContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = Request.responseText;
}

function UpdateGuiaLoading() {
  var Container = document.getElementById( GuiaContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = "<img src='images/wait.gif' />";
}

function UpdateGuiaError() {
  var Container = document.getElementById( GuiaContainer );
  Container.style.display = "inline-block";
  Container.innerHTML = 'Ocorreu um erro no script ou no requester';
}

function GetGuiasHTML() {
  var randomnumber = Math.floor( Math.random() * 1001 );
  var ListaURL = 'listas/guia.colunas.html';
  if ( document.location.protocol == 'https:' ) {
    ListaURL = 'guia.colunas.inc.asp';
  }
  var Request = new Ajax.Request( ListaURL,
                                  { method: 'get',
                                    parameters: { foo: randomnumber },
                                    onSuccess: UpdateGuia,
                                    onFailure: UpdateGuiaError,
                                    onLoading: UpdateGuiaLoading } );
}

function Toggle_Guia( e ) {
  var btnToggleGuia = document.getElementById( "btnToggleGuia" );
  var mnuGuia = document.getElementById( "mnuGuia" );
  if ( mnuGuia.style.display != "inline-block" ) {
    MenuProdutos_Off();
    mnuGuia.style.display = "inline-block";
    //Set_MaxSize( mnuGuia );
    btnToggleGuia.style.backgroundColor = "#e77817";
    btnToggleGuia.style.color = "white";
    GetGuiasHTML();
  }
  else {
    MenuProdutos_Off();
  }
  e.cancelBubble = true;
  return true;
}

function UpdateCatSel( Request ) {
  try {
    var Container = document.getElementById( CatSelContainer );
    if ( navigator.appName == 'Microsoft Internet Explorer' ) {
      Container.innerHTML = "<option value='' style='color:green;'>Selecione...</option>";
    }
    else {
      Container.innerHTML = Request.responseText;
    }
  }
  catch ( err ) {
    alert( err.description );
  }
}

function UpdateCatSelLoading() {
  try {
    var Container = document.getElementById( CatSelContainer );
    Container.innerHTML = "aguarde...";
  }
  catch ( err ) {
    alert( err.description );
  }
}

function UpdateCatSelError() {
  try {
    var Container = document.getElementById( CatSelContainer );
    Container.innerHTML = 'Ocorreu um erro no script ou no requester';
  }
  catch ( err ) {
    alert( err.description );
  }
}

function GetCategoriaSelect() {
  try {
    var randomnumber = Math.floor( Math.random() * 1001 );
    var Request = new Ajax.Request( 'categorias.select.inc.asp',
                                    { method: 'get',
                                      parameters: { foo: randomnumber },
                                      onSuccess: UpdateCatSel,
                                      onFailure: UpdateCatSelError,
                                      onLoading: UpdateCatSelLoading } );
  }
  catch ( err ) {
    alert( err.description );
  }
}

function Toggle_CategoriaSelect( e, obj ) {
  var Container = document.getElementById( CatSelContainer );
  if ( obj.checked ) {
    GetCategoriaSelect();
    document.getElementById( CatSelContainer ).style.display = 'inline-block'; 
  }
  else {
    Container.innerHTML = "";
    document.getElementById( CatSelContainer ).style.display = 'none';
  }
  e.cancelBubble = true;
  return true;
}

function UpdateFabSel( Request ) {
  var Container = document.getElementById( FabSelContainer );
  Container.innerHTML = Request.responseText;
}

function UpdateFabSelLoading() {
  var Container = document.getElementById( FabSelContainer );
  Container.innerHTML = "aguarde...";
}

function UpdateFabSelError() {
  var Container = document.getElementById( FabSelContainer );
  Container.innerHTML = 'Ocorreu um erro no script ou no requester';
}

function GetFabricanteSelect() {
  var randomnumber = Math.floor( Math.random() * 1001 );
  var Request = new Ajax.Request( 'fabricantes.select.inc.asp',
                                  { method: 'get',
                                    parameters: { foo: randomnumber },
                                    onSuccess: UpdateFabSel,
                                    onFailure: UpdateFabSelError,
                                    onLoading: UpdateFabSelLoading } );
}

function Toggle_FabricanteSelect( e, obj ) {
  var Container = document.getElementById( FabSelContainer );
  if ( obj.checked ) {
    GetFabricanteSelect();
    document.getElementById( 'fabricantes' ).style.display = 'inline-block'; 
  }
  else {
    //Container.innerHTML = "";
    document.getElementById( 'fabricantes' ).style.display = 'none';
  }
  e.cancelBubble = true;
  return true;
}

function BuscaKP( keyEvent ) {
  var KV = keyEvent.keyCode;
  var BForm = document.getElementById( "frmBusca" );
  var mnuBusca = document.getElementById( "mnuBusca" );
  if ( KV == 13 ) {
    BuscaSubmit( BForm );
  }
  else if ( KV == 27 ) {
    mnuBusca.style.display = "none";
  }
}

function BuscaSubmit( BForm ) {
  var MyAction = "busca.new.asp?mode=auto";
  if ( BForm.chkCategoria.checked ) {
    MyAction += "&DoCat=1";
  }
  else {
    MyAction += "&DoCat=0";
  }
  if ( BForm.chkFabricante.checked ) {
    MyAction += "&DoFab=1";
  }
  else {
    MyAction += "&DoFab=0";
  }
  if ( BForm.chkDescontinuados.checked ) {
    MyAction += "&desc=1";
  }
  else {
    MyAction += "&desc=0";
  }
  BForm.action = MyAction;
  BForm.submit();
}

function createCookie( name, value, days ) {
  if ( days ) {
    var date = new Date();
    date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
    var expires = "; expires=" + date.toGMTString();
  }
  else {
    var expires = "";
  }
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie( name ) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for ( var i=0; i < ca.length; i++ ) {
    var c = ca[i];
    while ( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length );
    if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length );
  }
  return null;
}

function eraseCookie( name ) {
  createCookie( name, "", -1 );
}
