function getHTTPObject() {
var req;
 
try {
 if (window.XMLHttpRequest) {
  req = new XMLHttpRequest();
 
  if (req.readyState == null) {
   req.readyState = 1;
   req.addEventListener("load", function () {
   req.readyState = 4;
 
   if (typeof req.onReadyStateChange == "function")
    req.onReadyStateChange();
   }, false);
  }
 
  return req;
 }
 
 if (window.ActiveXObject) {
  var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 
  for (var i = 0; i < prefixes.length; i++) {
   try {
    req = new ActiveXObject(prefixes[i] + ".XmlHttp");
    return req;
   } catch (ex) {};
 
  }
 }
} catch (ex) {}
 
alert("XmlHttp Objects not supported by client browser");
}
var http = getHTTPObject();
// Logo apos fazer a verificacao, o chamada a funcao e passada 
// o valor e variavel global http.
 function atualizaSubCategoria()
{
  campo_select = document.pesquisa_produto.subcategoria;
  if (document.pesquisa_produto.categoria.value == ""){
	return false;  
  }
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(";");
    for( i = 0; i < results.length; i++ )
    { 
      string = results[i].split( "|" );
      if(string[0] != 0)
      {
        campo_select.options[i] = new Option( string[0], string[1] );
      }
    }
  }
}
function atualizaProduto()
{
  campo_select = document.pesquisa_produto.produtos;
   if (document.pesquisa_produto.subcategoria.value == ""){
	return false;  
  }
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(";");
    for( i = 0; i < results.length; i++ )
    { 
      string = results[i].split( "|" );
      if(string[0] != 0)
      {
        campo_select.options[i] = new Option( string[0], string[1] );
      }
    }
  }
}
function list_dados( valor )
{
 if (document.pesquisa_produto.categoria.value == ""){
	return false;  
  }
  http.open("GET", "subcategorias.ajax.php?id=" + valor, true);
  http.onreadystatechange = function(){
	if (http.readyState==4){
		atualizaSubCategoria();
	}
  }
  http.send(null);
}
function list_dados_produtos( valor )
{
	if (document.pesquisa_produto.subcategoria.value == ""){
	return false;  
  }
  http.open("GET", "produtos.ajax.php?id=" + valor, true);
  http.onreadystatechange = atualizaProduto;
  http.send(null);
}
function adicionarProduto(hash)
{
  var valor = document.pesquisa_produto.produtos.value;
  http.open("GET", "adicionarProduto.ajax.php?id=" + valor +"&hash="+hash, true);
   http.onreadystatechange = function()
   {
	  if (http.readyState == 4){
			listaProdutos();
	  }
   }
  http.send(null);
}
function deletarProduto(id)
{
  http.open("GET", "excluiItem.ajax.php?id=" + id, true);
   http.onreadystatechange = function()
   {
	  if (http.readyState == 4){
		listaProdutos();
	  }
   }
  http.send(null);
}
function exibeFotos()
{
	var id = document.pesquisa_produto.produtos.value;
  http.open("GET", "fotosprodutos.ajax.php?id=" + id, true);
   http.onreadystatechange = function()
   {
	  if (http.readyState == 4){
		document.getElementById('detalhes').innerHTML = http.responseText;
	  }
   }
  http.send(null);
}
function Aleatorio(){ aleat = Math.random() * 5000; aleat = Math.round(aleat); return aleat;}
function listaProdutos(){
   var url = "tabela-orcamento.php?aleatorio="+Aleatorio();
   http.open("GET", url, true);
   http.onreadystatechange = function()
   {
	   if(http.readyState == 4 && http.status == 200) 
	   {		
	   	if (navigator.appName == "Microsoft Internet Explorer"){
			var newdiv = document.createElement("div");
			newdiv.innerHTML = http.responseText;
			var container = document.getElementById('orcamento');
			container.innerHTML = "";
			container.appendChild(newdiv);
		}else{
			var resposta = http.responseText;
			document.getElementById('orcamento').innerHTML = resposta;
		}
	   }
   }
 http.send(null);
}
function mudaCor(opcao){
	if (opcao == 1){
		document.getElementById('menu').style.borderBottom  = "4px solid #666";
	}
	if (opcao == 2){
		document.getElementById('menu').style.borderBottom  = "4px solid #ffcb05";	
	}
	if (opcao == 3){
		document.getElementById('menu').style.borderBottom  = "4px solid #0b97bc";
	}
	if (opcao == 4){
		document.getElementById('menu').style.borderBottom  = "4px solid #000";
	}
	if (opcao == 5){
		document.getElementById('menu').style.borderBottom  = "4px solid #9c6e57";
	}
	if (opcao == 6){
		document.getElementById('menu').style.borderBottom  = "4px solid #f80011";
	}
}
function voltaCor(){
	document.getElementById('menu').style.borderBottom  = "4px solid #666";
}
function aumentaQuant(id){
	var quantidade_prod = document.getElementById('quantidade_'+id).value;
	if (quantidade_prod == ""){
		document.getElementById('quantidade_'+id).value= 1;
	}else{
		document.getElementById('quantidade_'+id).value= (parseInt(document.getElementById('quantidade_'+id).value)+ 1);
	}
}
function diminuiQuant(id){
	var quantidade_prod = document.getElementById('quantidade_'+id).value;
	if (quantidade_prod > 1){
		document.getElementById('quantidade_'+id).value = document.getElementById('quantidade_'+id).value - 1;
	}
}
function atualizaQuant(id, valor){
	http.open("GET", "atualizaQuantidade.ajax.php?id=" + id +"&valor="+valor, true);
   http.onreadystatechange = function()
   {
	  if (http.readyState == 4){
		  return false;
	  }
   }
  http.send(null);
}
function numero(e){
    var tecla=(window.event)?event.keyCode:e.which;   
    if((tecla>47 && tecla<58)) return true;
    else{
    	if (tecla==8 || tecla==0) return true;
	else  return false;
    }
}
function mudaImagem(thumb, destaque, altura, largura){
	var img_p = document.getElementById('destaque');
	var img_m = document.getElementById('light');
	var principal = document.getElementById('gallery');
	img_p.src = thumb;
    img_p.style.width=largura;
    img_p.style.height=altura;
	principal.href=destaque;
}
function Trim(str){return str.replace(/^\s+|\s+$/g,"");}
function enviarOrcamento(){
	var d = document.forms[0];
	if (Trim(d.cliente.value)==""){
		alert('Preencha seu nome antes de prosseguir!');
		d.cliente.focus();
		return false;
	}
	if (Trim(d.cep.value)=="" || d.cep.value.length <8){
		alert('Informe um CEP antes de prosseguir!');
		d.cep.focus();
		return false;
	}
	if (Trim(d.endereco.value)==""){
		alert('Preencha seu endereço antes de prosseguir!');
		d.endereco.focus();
		return false;
	}
	if (Trim(d.bairro.value)==""){
		alert('Preencha seu bairro antes de prosseguir');
		d.bairro.focus();
		return false;
	}
	if (Trim(d.cidade.value)==""){
		alert('Preencha sua cidade antes de prosseguir');
		d.cidade.focus();
		return false;
	}
	if (d.estado.selectedIndex == 0){
		alert('Preencha seu estado antes de prosseguir');
		d.estado.focus();
		return false;
	}
	if (Trim(d.cnpj.value)=="" || d.cnpj.value.length < 14){
		alert('Preencha seu CNPJ antes de prosseguir');
		d.cnpj.focus();
		return false;
	}
	if (Trim(d.inscricao_estadual.value)== ""){
		alert('Preencha sua inscrição estadual antes de prosseguir');
		d.inscricao_estadual.focus();
		return false;
	}
	if (Trim(d.pagamento.value)==""){
		alert('Preencha a condição de pagamento antes de prosseguir');
		d.pagamento.focus();
		return false;
	}
	if (Trim(d.telefone.value)=="" || d.telefone.value.length < 10){
		alert('Preencha seu telefone antes de prosseguir');
		d.telefone.focus();
		return false;
	}
	if (Trim(d.contato.value)==""){
		alert('Preencha o contato antes de prosseguir');
		d.contato.focus();
		return false;
	}
	verificaPedidos();
}
function verificaPedidos(){
  http.open("GET", "verificaProdutos.ajax.php", true);
   http.onreadystatechange = function()
   {
	  if (http.readyState == 4){
			var resposta = http.responseText;
			if (resposta == 0){
				alert('Você não adicionou produtos suficientes!');
			}
			else{
				document.forms[0].action="envia-orcamento";
				document.forms[0].submit();
			}
	  }
   }
  http.send(null);	
}
function infoProduto(div){
	for (i = 1; i<4;i++){
		var div_prod = "infoproduto_"+i;
		document.getElementById(div_prod).style.display = "none";
	}
	document.getElementById("infoproduto_"+div).style.display = "inline";
	for (i=1;i<4;i++){
		var div_aba = "aba"+i;
		document.getElementById(div_aba).className = div_aba;	

	}
	document.getElementById("aba"+div).className = "aba"+div+"-ativa";
}
function indicarProduto(produto){
	var mywindow = window.open("http://www.botafogo.ind.br/indicar-produto/"+produto,"Indicar","width=365px, height = 385px, scrollbars = no, toolbar = no, menubar = no");
	mywindow.focus();
}
function validaIndicacao(){
	var d = document.indicar;
	if (Trim(d.nome.value)==""){
		alert('Preencha seu nome antes de prosseguir!');
		d.nome.focus();
		return false;
	}
	if (Trim(d.email.value)==""){
		alert('Preencha seu email antes de prosseguir!');
		d.email.focus();
		return false;
	}
	if (Trim(d.nomeamigo.value)==""){
		alert('Preencha o nome do seu amigo antes de prosseguir!');
		d.nomeamigo.focus();
		return false;
	}
	if (Trim(d.emailamigo.value)==""){
		alert('Preencha o email do seu amigo antes de prosseguir!');
		d.emailamigo.focus();
		return false;
	}
	d.submit();
}
function enviaContato(){
	var d = document.contato;
	if (d.assunto.selectedIndex == 0){
		alert('Preencha o assunto antes de prosseguir!');
		d.assunto.focus();
		return false;
	}
	if (Trim(d.nome.value)==""){
		alert('Preencha seu nome antes de prosseguir!');
		d.nome.focus();
		return false;
	}	
	if (Trim(d.ddd.value)=="" || d.ddd.value.length != 2){
		alert('Preencha seu DDD antes de prosseguir!');
		d.ddd.focus();
		return false;
	}
	if (Trim(d.telefone.value)=="" || d.telefone.value.length != 8){
		alert('Preencha seu telefone antes de prosseguir!');
		d.telefone.focus();
		return false;
	}
	if (Trim(d.email.value)==""){
		alert('Preencha seu email antes de prosseguir!');
		d.email.focus();
		return false;
	}
	if (Trim(d.texto.value)==""){
		alert('Preencha a mensagem antes de prosseguir!');
		d.texto.focus();
		return false;
	}
	d.action = "enviar-email";
	d.submit();
}
