function createXHR(){

    var request = false;

       try {
       request = new ActiveXObject('Msxml2.XMLHTTP');
       }

       catch (err2) {

          try {
          request = new ActiveXObject('Microsoft.XMLHTTP');
          }

          catch (err3) {

             try {
             request = new XMLHttpRequest();
             }

             catch (err1){
             request = false;
             }

          }

       }

   return request;

}

function gerar_swf (arquivo, altura, largura, id){

   document.writeln('    <object id="globalnav-object" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + largura + '" height="' + altura + '" id="' + id + '" name="' + id + '">');
   document.writeln('        <param name="movie" value="' + arquivo + '" />');
   document.writeln('        <param name="bgcolor" value="#ffffff" />');
   document.writeln('        <param name="menu" value="false" />');
   document.writeln('        <param name="quality" value="high" />');
   document.writeln('        <param name="salign" value="tl" />');
   document.writeln('        <param name="scale" value="noscale" />');
   document.writeln('        <embed id="globalnav-embed" src="' + arquivo + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="loc=en_US&htmlApp=false&gatewayURL=gwurl" bgcolor="#ffffff" menu="false" quality="high" salign="tl" scale="noscale" id="' + id + '" width="' + largura + '" height="' + altura + '"></embed>');
   document.writeln('    </object>');

}

function popup (windowURL, windowWidth, windowHeight, scroll) {

   var windowLeft = (screen.width - windowWidth) / 2;
   var windowTop = (screen.height - windowHeight) / 2;
   var windowProperties = 'height=' +windowHeight+ ',width=' +windowWidth+ ',top=' +windowTop+ ',left=' +windowLeft+ ',scrollbars=' +scroll+ ',resizable=0,menubar=0,toolbar=0,location=0,statusbar=0,fullscreen=0';

   var obj_window = window.open (windowURL, 'aguas_pop', windowProperties);

   if (parseInt(navigator.appVersion) >= 4) {
      obj_window.window.focus();
   }

}

function voto (ID){

   opcoes = document.formEnquete.escolha;
   escolhido = 0;

   for (x = 0; x < opcoes.length; x++){

      if (opcoes[x].checked == true){
         escolhido = opcoes[x].value;
      }

   }

   if (escolhido){

      html_volta = document.getElementById('resp').innerHTML;

      document.getElementById('resp').innerHTML = '<ul><li class="aguarde"><img src="/img/load.gif">Aguarde</li></ul>';

      var request = createXHR();

      request.onreadystatechange = function(){

         if (request.readyState == 4){

            if (request.status == 200){
            document.getElementById('resp').innerHTML = request.responseText;
            }else{
            document.getElementById('resp').innerHTML = '<ul><li class="aguarde"><img src="/img/erro.gif">Erro</li><li>Um erro ocorreu ao computar o seu voto. Por favor, tente novamente mais tarde</li><li><a href="javascript:void(0)" onclick="javascript:document.getElementById(\'resp\').innerHTML = html_volta;"><< voltar</a></ul>';
            }

         }

      }

      request.open('GET', '/voto.php?enquete=' +ID+ '&escolha=' +escolhido,  true);
      request.send('NULL');

   }else{

      alert ('Escolha uma das opções');

   }

}

function muda_galeria (ID){

   if (fotoTotal > 5){

      var p1 = 3;
      var p2 = fotoTotal - 2;
      var pos = new Array();
      var y = -80;

      for (x = 1; x <= fotoTotal; x++){

          if (x <= p1){
          pos[x] = 0;
          }

          if (x > p1 && x < p2){
          pos[x] = y;
          y = y - 80;
          }

          if (x >= p2){
          pos[x] = y;
          }

      }

      var doscroll = document.getElementById('doscroll');

      if (!doscroll.style.top){
      var top = 0;
      }else{
      var top = parseFloat(doscroll.style.top);
      }

      if (top != pos[ID]){
      movie = new Tween (doscroll.style, 'top', Tween.strongEaseOut, top, pos[ID], 1, 'px');
      movie.start();
      }

   }

   for (x = 1; x <= fotoTotal; x++){

      if (x == ID){
      document.getElementById('thumb_' +x).style.opacity = 100;
      document.getElementById('thumb_' +x).style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
      }else{
      document.getElementById('thumb_' +x).style.opacity = 0.3;
      document.getElementById('thumb_' +x).style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=30)';
      }

   }

   document.getElementById('galimg').style.marginTop = fotoY[ID]+ 'px';
   document.getElementById('galimg').style.marginLeft = fotoX[ID]+ 'px';
   document.getElementById('galimg').src = fotoURL[ID];
   document.getElementById('fotodesc').innerHTML = '<b>' +fotoD[ID]+ '</b><br /><i>' +fotoC[ID]+ '</i>';

}

function galeria (secao, ID){

   if (secao && ID){
   javascript:popup ('/galerias.php?ID=' +ID+ '&tipo=' +secao, 623, 540, 0);
   }

}

function cadastra_newsletter (){

   var erro = '';
   var nome = document.getElementById('newsletter_nome').value;
   var email = document.getElementById('newsletter_email').value;

   if (!email || email == 'E-Mail'){
      erro = 'Informe o seu e-mail no campo correspondente.';
   }else if (email && !checkMail(email)){
      erro = 'O e-mail informado não é correto.';
   }

   if (!nome || nome == 'Nome'){
      erro = 'Informe o seu nome no campo correspondente.';
   }else if (nome && nome.length < 5){
      erro = 'Informe o seu nome com no mínimo 5 caracteres.';
   }

   if (erro){
      alert(erro);
   }else{

      // Loading
      document.getElementById('newsletter_retorno').innerHTML = '<ul class="aguarde"><li class="titulo"><img src="/img/load.gif">Aguarde</li><li>Conferindo o seu e-mail ...</li></ul>';

      var requestCadastro = createXHR();

      requestCadastro.onreadystatechange  = function(){

         if (requestCadastro.readyState  == 4){

            if (requestCadastro.status  == 200){

               if (checkMail(requestCadastro.responseText)){
                  document.getElementById('newsletter_retorno').innerHTML = '<ul class="aguarde"><li class="titulo"><img src="/img/erro.gif">Erro</li><li>O e-mail informado (' +requestCadastro.responseText+ ') já se encontra cadastrado.</li><li><a href="javascript:void(0);" onclick="javascript:html_newsletter();">Clique aqui</a> para retornar.</li></ul>';
               }else{
                  document.getElementById('newsletter_retorno').innerHTML = requestCadastro.responseText;
               }

            }else{
            document.getElementById('newsletter_retorno').innerHTML = "Erro: " +requestCadastro.status;
            }

         }

      }

      requestCadastro.open('POST', '/newsletter.php',  true);
      requestCadastro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      requestCadastro.send('acao=insere&nome=' +nome+ '&email=' +email);

   }

} // Fim function

function html_newsletter (){

   document.getElementById('newsletter_retorno').innerHTML = '<ul> \
                                                              <li>Informe abaixo o seu nome e e-mail para receber a nossa newsletter</li> \
                                                              <li><p>Nome</p></li> \
                                                              <li><input id="newsletter_nome" name="nome"></li> \
                                                              <li><p>E-Mail</p></li> \
                                                              <li><input id="newsletter_email" name="email"></li> \
                                                              <li class="b4"><input type="button" value="ok" onclick="javascript:cadastra_newsletter();"></li> \
                                                              </ul>';

} // Fim function

function checkMail (x) {

   var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

   if (filter.test(x)){
   return true;
   } else {
   return false;
   }

} // Fim function

function contato_set_submit(){
   document.getElementById('retorno').innerHTML = '<input type="button" value="Enviar Contato >>" onclick="javascript:contato_envia()" class="botao_submit">';
}

function contato_envia (){

   var erro = '';
   var nome = document.getElementById('nome').value;
   var email = document.getElementById('email').value;
   var mensagem = document.getElementById('mensagem').value;

   if (!mensagem){
      erro = 'Digite a sua mensagem no campo correspondente.';
   }else if (mensagem && mensagem.length < 10){
      erro = 'Digite a sua <b>mensagem</b> com no mínimo 10 caracteres.';
   }

   if (!email){
      erro = 'Informe o seu <b>e-mail</b> no campo correspondente.';
   }else if (email && !checkMail(email)){
      erro = 'O <b>e-mail</b> informado não é válido.';
   }

   if (!nome){
      erro = 'Informe o seu <b>nome</b> no campo correspondente.';
   }else if (nome && nome.length < 5){
      erro = 'Informe o seu <b>nome</b> com no mínimo 5 caracteres.';
   }

   if (erro){
      document.getElementById('retorno').innerHTML = '<img src="/img/alert_32.gif"><b>ERRO</b><br />' +erro;
   }else{


      // Loading
      document.getElementById('retorno').innerHTML = '<img src="/img/ajax_loading.gif"><b>Aguarde ...</b><br />Enviando o seu formulário';

      var requestCadastro = createXHR();

      requestCadastro.onreadystatechange  = function(){

         if (requestCadastro.readyState  == 4){

            if (requestCadastro.status  == 200){

               if (requestCadastro.responseText == 'erro_tempo'){
                  document.getElementById('retorno').innerHTML = '<img src="/img/alert_32.gif"><b>ERRO</b><br />Aguarde ao menos 10 minutos para enviar um novo contato.';
               }else{
                  document.getElementById('retorno').innerHTML = requestCadastro.responseText;
               }

            }else{
               document.getElementById('retorno').innerHTML = "Erro: " +requestCadastro.status;
            }

         }

      }

      requestCadastro.open('POST', '/contatos.php',  true);
      requestCadastro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      requestCadastro.send('acao=insere&nome=' +nome+ '&email=' +email+ '&mensagem=' +mensagem);

   }

} // Fim function



