// *****************************************************************************
// MyWeb Suite - MySky Application Server
// Gerenciamento da pagina inicial do MyWeb Suite
// Autor: Daniel Ribeiro Gomes - danielrg@mysky.com.br
// Versao: $Id: main.js,v 1.5 2004/06/28 18:42:29 danielrg Exp $
// *****************************************************************************

var DIR_CGI;
var VER_TMP;

// Define o diretorio dos CGI's ************************************************

function setDirCGI(dir)
{
  DIR_CGI = dir;
}

function setVerTmp(ver)
{
  VER_TMP = ver;
}

// Inicia o documento **********************************************************

function iniciar()
{

  iniciarForms(document.forms);
  
  var selecaoVenda = getCookie("selecaoVenda");


  if (selecaoVenda != null)
    setSelecaoFormVenda(selecaoVenda);

  var img = document.getElementById("imgDia");

  if (img)
    iniciarImagemDoDia(img);

}

// Inicia os formularios do documento ******************************************

function iniciarForms(forms)
{
  for (var i=0; i < forms.length; i++)
  {
    var form = forms[i];
    var nome = form.name;
    
    if (nome == "formVenda")
      iniciarFormVenda(form);
    else if (nome == "formLogin")
      iniciarFormLogin(form);
    else if (nome == "formTimeTable")
      iniciarFormTimeTable(form);
  }
}

// Inicia o formulario de venda ************************************************

function iniciarFormVenda(form, diaHoje, mesHoje, anoHoje)
{
  form.action = DIR_CGI + "/mysky_web_v1.cgi/" + VER_TMP ;
  setAeroportos(form);
  setDatas(form);
  setPaxes(form);
}


// Inicia o formulario de login de agencias ************************************

function iniciarFormLogin(form)
{
}

// Inicia o formulario de Time Table *******************************************

function iniciarFormTimeTable(form, diaHoje, mesHoje, anoHoje)
{
  form.action = DIR_CGI + "/timeTable.cgi";
  setAeroportos(form) ;
  setDatas(diaHoje, mesHoje, anoHoje, form);
}

// Inicia a imagem do dia ******************************************************

function iniciarImagemDoDia(imagem)
{
  var INICIO_NOME = "/dia/dia0";
  var FIM_NOME = ".jpg";
  var NUMERO_IMAGENS = 7;
  var data = new Date();
  //var numero = data.getDate(); // Dia do mes
  //var numero = data.getDay(); // dia da semana
  var numero = data.getSeconds(); // Segundos
  
  imagem.src = INICIO_NOME + (numero % NUMERO_IMAGENS + 1) + FIM_NOME;
}

// Verifica a validade dos formularios *****************************************

function isFormOk(form)
{

  var nome = form.name;
  
  if (nome == "formVenda")
    return isFormVendaOk(form);
  else if (nome == "formLogin")
    return isFormLoginOk(form);
  else if (nome == "formTimeTable")
    return isFormTimeTableOk(form);
  else
    return false;
}

// Valida o formulario de venda ************************************************

function isFormVendaOk(form)
{


  var mens='';
  
  if (form.origem.value == '---') {
    mens=mens + "Não foi selecionado a origem.\n";
  }
  if (form.destino.value == '---') {
    mens=mens + "Não foi selecionado o destino.\n";
  }
  
  var adt = Number(form.numADT.value);
  var chd = Number(form.numCHD.value);
  
  if (adt == 0 && chd == 0)
  {
    mens=mens + "Selecione passageiros para a sua reserva.\n";
  }

if (mens != '') { alert(mens);  return false; }

  
  var selecao = getSelecaoFormVenda();

 

 
  setCookie("selecaoVenda", selecao);
  
//  var loc = reservaEmAndamento();
//  
//  if (loc != null)
//  {
//  
//    document.formVenda.action = DIR_CGI + "/mysky_R.cgi" + VER_TMP ;  
//    document.formVenda.loc.value = loc;
//  }

  return true;
}

// Retorna String descritiva da selecao do usuario no form de venda ************

function getSelecaoFormVenda()
{
  var SEP = ".";
  var form = document.formVenda;
  
  var retorno =
    String(form.origem.selectedIndex)   + SEP +
    String(form.day0.selectedIndex)   + SEP +
    String(form.date0.selectedIndex)   + SEP +
    String(form.destino.selectedIndex)  + SEP +
    String(form.day1.selectedIndex) + SEP +
    String(form.date1.selectedIndex) + SEP +
    String(form.numADT.selectedIndex)   + SEP +
    String(form.numCHD.selectedIndex)   + SEP +
    String(form.numINF.selectedIndex)  + SEP +
    String(form.alt0.selectedIndex)   + SEP +
    String(form.alt1.selectedIndex) 
    ;
  
  return retorno;
}

// Define a selecao no form de venda de acordo com string descritiva ***********

function setSelecaoFormVenda(string)
{
  var SEP = ".";
  var campos = string.split(SEP);
  var form = document.formVenda;
  

  
  form.origem.selectedIndex = Number(campos[0]);
  setDestinos();
  form.destino.selectedIndex = Number(campos[3]);
  form.date0.selectedIndex = Number(campos[2]);
  mudouMesIda();
  form.day0.selectedIndex = Number(campos[1]);
  mudouDiaIda();
  form.date1.selectedIndex = Number(campos[5]);
  mudouMesVolta();
  form.day1.selectedIndex = Number(campos[4]);
  form.numADT.selectedIndex = Number(campos[6]);
  mudouPaxAdultos();
  form.numCHD.selectedIndex = Number(campos[7]);
  form.numINF.selectedIndex = Number(campos[8]);
  form.alt0.selectedIndex = Number(campos[9]);
  form.alt1.selectedIndex = Number(campos[10]);  

}

// Valida o formulario de login ************************************************

function isFormLoginOk(form)
{
  var agencia = form.agt.value;
  var email   = form.email.value;
  var senha   = form.senha.value;
  
  // Visualizacao de status das agencias
  if (agencia == "" && email == "" && senha != "")
  {
    form.action = DIR_CGI + "/agencstatus.cgi";
    return true;
  }
  
  // O preenchimento do campo de email eh facultativo para o login de agencia
  else if (agencia != "" && senha != "")
  {
    form.action = 'http://www.mysky.com.br';
    form.action = DIR_CGI + "/agencstatus.cgi";
    return true;
  }
  
  alert("Preencha os dados para efetuar login.");
  return false;
}

// Valida o formulario de time table *******************************************

function isFormTimeTableOk(form)
{
  return true;
}
