var http_request = false;
var contato = false;
function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      // set type accordingly to anticipated content type
      //http_request.overrideMimeType('text/xml');
      http_request.overrideMimeType('text/html');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Lamento, mas o site não é compativel com essa versão do seu browser.');
    return false;
  }

  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      result = http_request.responseText;
      if ((result.indexOf( 'http:' ) > -1) &&(result.indexOf( 'http:' ) < 5)){
        window.location = result;
      } else if (result == 'senhaerrada'){
        alert('Senha errada!');
      } else if (result.indexOf('<alert>') == 0){
        result = result.slice(7);
        if (result == 'Enviado!'){
          contato = true;
        } else {
          contato = false;
        }
        alert(result);
      } else {
        document.getElementById('conteudo').innerHTML = result;
      }

      if (document.getElementById('retorno')){
        document.getElementById('retorno').style.display = '';
      }

    } else {
      alert('Lamento, ouve algum problema com a sua solicitção ! Tente novamente.');
    }
  }
}


function sendContato(origem){
	if (contato == false){

	contato = '';
  nome=document.getElementById('input_nome').value;
  email=document.getElementById('input_email').value;
  rs=document.getElementById('input_rs').value;
  cnpj=document.getElementById('input_cnpj').value;

  if (origem == 'colecao'){
    if (validaCNPJ(cnpj) == false){
      return;
    }
  }
  telefone=document.getElementById('input_telefone').value;
  cidade=document.getElementById('input_cidade').value;
  objEstado=document.getElementById('selectEstado');
  estado=objEstado.options[objEstado.selectedIndex].value;
  mensagem=document.getElementById('input_mensagem').value;
  var poststr = "nome=" + encodeURI( nome ) +
  "&email=" + encodeURI( email ) +
  "&rs=" + encodeURI( rs ) +
  "&cnpj=" + encodeURI( cnpj ) +
  "&telefone=" + encodeURI( telefone ) +
  "&cidade=" + encodeURI( cidade ) +
  "&estado=" + encodeURI( estado ) +
  "&origem=" + origem +
  "&mensagem=" + encodeURI( mensagem );
  makePOSTRequest('/action.php', poststr);
  }else   if (contato == true) {
    alert ('Recebemos sua mensagem, em breve entraremos em contato.');
  } else  if (contato == '') {
    alert ('Aguarde, enviando sua mensagem.');
  }
}

function login(origem){
  senha=document.getElementById('input_senha').value;
  var poststr = "s=" + encodeURI( senha ) +
  "&origem=" + origem;
  makePOSTRequest('/action.php', poststr);
}

function showInformation(desc){
  if(desc == "makingoff" || desc == "makingoff_show"){
    document.getElementById('conteudo').style.background = 'url("/imagem/movie.jpg") no-repeat scroll 50% 50% transparent';
  }else{
    document.getElementById('conteudo').style.background = 'url("/imagem/santee_b.jpg") no-repeat scroll 50% 50% transparent';
  }

  var poststr = "desc=" + encodeURI( desc );
  makePOSTRequest('/show.php', poststr);

}

function validaCNPJ(CNPJ) {
//  CNPJ = document.validacao.CNPJID.value;
  erro = new String;
  if (CNPJ.length < 14) {
    erro += "É necessario preencher corretamente o número do CNPJ1! \n\n";
  } else if (CNPJ.length > 14) {
    if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
      if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ2! \n\n";
    }
    //substituir os caracteres que não são números
    if(document.layers && parseInt(navigator.appVersion) == 4){
      x = CNPJ.substring(0,2);
      x += CNPJ. substring (3,6);
      x += CNPJ. substring (7,10);
      x += CNPJ. substring (11,15);
      x += CNPJ. substring (16,18);
      CNPJ = x;
    } else {
      CNPJ = CNPJ. replace (".","");
      CNPJ = CNPJ. replace (".","");
      CNPJ = CNPJ. replace ("-","");
      CNPJ = CNPJ. replace ("/","");
    }
  }
  var nonNumbers = /\D/;
  if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! \n\n";
  var a = [];
  var b = new Number;
  var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
  for (i=0; i<12; i++){
    a[i] = CNPJ.charAt(i);
    b += a[i] * c[i+1];
  }
  if ((x = b % 11) < 2) {
    a[12] = 0
  } else {
    a[12] = 11-x
  }
  b = 0;
  for (y=0; y<13; y++) {
    b += (a[y] * c[y]);
  }
  if ((x = b % 11) < 2) {
    a[13] = 0;
  } else {
    a[13] = 11-x;
  }
  if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
    erro +="Dígito verificador com problema!";
  }
  if (erro.length > 0){
    alert(erro);
    return false;
  } 
  return true;
}


