// *****************************************************************************
// MyWeb Suite - MySky Application Server
// Funcoes utilitarias em Javascript
// Autor: Daniel Ribeiro Gomes - danielrg@mysky.com.br
// Versao: $Id: utils.js,v 1.4 2004/06/28 18:41:23 danielrg Exp $
// *****************************************************************************

var RESERVA_EM_ANDAMENTO = 1;
var RESERVA_CONCLUIDA = 0;

// Retorna o valor de um cookie armazenado *************************************

function getCookie(nome)
{


  var cookies = document.cookie;
   
  var inicio = cookies.indexOf(nome);

  if (inicio == -1)
    return null;

  inicio = cookies.indexOf("=", inicio);

  if (inicio == -1)
    return null;
  
  inicio++;

  var fim = cookies.indexOf(";", inicio);

  if (fim == -1)
    fim = cookies.length;

  return unescape(cookies.substring(inicio, fim));
}

// Armazena um cookie no cliente ***********************************************

function setCookie(nome, valor, dataExpiracao)
{


  var cookie = nome + "=" + valor;

  if (dataExpiracao != null)
    cookie += ";expires=" + escape(dataExpiracao);
  
  document.cookie = cookie;
}

// Zera as opcoes de um campo select *******************************************

function clearSelect(select, tamanho)
{
  select.options.length = tamanho;
  
  for (var i=0; i < tamanho; i++)
    select.options[i] = new Option();
}

// Adiciona uma opcao em um campo select ***************************************

function addSelect(select, texto, valor, selecionado)
{
  //alert("select=" + select + " texto=" + texto + " valor=" + valor + " selecionado=" + selecionado);
  
  select.options.length++;
  select.options[select.options.length - 1] = new Option(texto, valor, false, selecionado);
}

// Retorna o valor selecionado em um campo select ******************************

function getSelect(select)
{
  return select.options[select.selectedIndex].value;
}

// Seleciona uma opcao em um campo select de acordo com um dado valor **********

function setSelect(select, valor)
{
  for (var i=0; i < select.options.length; i++)
    if (select.options[i].value == valor)
    {
      select.options[i].selected = true;
      return;
    }
}

// Retorna o valor selecionado em um botao de radio ****************************

function getRadio(radio)
{
  if (radio)
  {
    if (radio.length)
    {
      for (var i=0; i < radio.length; i++)
        if (radio[i].checked)
          return radio[i].value;

      return null;
    }
    else
    {
      return radio.checked ? radio.value : null;
    }
  }
  else
  {
    return null;
  }
}

// Seleciona uma opcao em um botao de radio de acordo com um dado valor ********

function setRadio(radio, valor)
{
  if (radio.length)
  {
    for (var i=0; i < radio.length; i++)
      radio[i].checked = radio[i].value == valor;
  }
  else
  {
    radio.checked = radio.value == valor;
  }
}

// Exibe uma URL em uma janela pop-up ******************************************

function popup(url, nome, largura, altura)
{
  window.open(url, nome, "toolbar=no,location=no,directories=no,status=no,menubar=no," + 
    "scrollbars=auto,resizable=no,copyhistory=no,width=" + largura + ",height=" + altura);
}

// *****************************************************************************

function reservaEmAndamento()
{
  var statusReserva = getCookie("statusReserva");
  
  if (statusReserva == null)
    return null;

  var campos = statusReserva.split(".");
  var status = campos[0];
  var loc = campos[1];

  if (status == RESERVA_EM_ANDAMENTO)
    return loc;
  else
    return null;
}

// *****************************************************************************
