var SITE = "http://festare.com.br";



/****************************\
FUNCAO QUE CRIA O OBJETO AJAX
\****************************/
function criarAjax() {
var ajax;
	try{
	    ajax = new XMLHttpRequest(); 
	}catch(ee){
	    try{
	        ajax = new ActiveXObject("Msxml2.XMLHTTP"); 
	    }catch(e){
	        try{
	            ajax = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            ajax = false;
	        }
	    }
	}
	return ajax;
}


try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
       try{
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       }catch(E){
          xmlhttp = false;
       }
    }
}

function resultado(){	
	


}


function enviar_enquete(){

	var pergunta = document.votacao.cod_pergunta.value;

	//O voto. Cria uma variÃ¡vel como a opÃ§Ã£o escolhida.
	if(document.votacao.escolha1.checked)  		var voto=0
	else if(document.votacao.escolha2.checked) var voto=1
	else if(document.votacao.escolha3.checked) var voto=2
	else if(document.votacao.escolha4.checked) var voto=3
	else var voto=""
	
	if((voto != 0) && (voto != 1) && (voto != 2) && (voto != 3)){
		alert("Escolha uma opÃ§ao para poder votar!")
	}else{
		//Abre a conexÃ£o
		xmlhttp.open("GET",SITE + "/scripts/votar.php?voto=" + voto + "&per=" + pergunta, true);

		//FunÃ§Ã£o para tratamento do retorno
		xmlhttp.onreadystatechange=function() {

			if (xmlhttp.readyState==4){

				//Mostra mensagem
				var mensagem	= xmlhttp.responseText;
				mensagem		= mensagem.replace(/\+/g," ");
				mensagem		= unescape(mensagem);
				document.getElementById('votacao').innerHTML=mensagem;
			}
		}

		//Executa
		xmlhttp.send(null)
	}	
	
	return false;
}



/********************************\
FUNCAO PARA PEGAR A CIDADE DE 
ACORDO COM O ESTADO SELECIONADO
\********************************/
function cidades(valor) {
	     //deixa apenas o elemento 1 no option, os outros são excluídos
		 document.imovel.opt_cidade.options.length = 1;
	     
		 idOpcao  = document.getElementById("opcoes");
		 
	 	 var ajax = criarAjax();
		 
		 ajax.open("POST",SITE + "/adm/cidades.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = 
		 function(){
				//enquanto estiver processando...emite a msg de carregando
				if(ajax.readyState == 1) {
					 idOpcao.innerHTML = "Carregando...";
				}
				//após ser processado - chama função processXML que vai varrer os dados
				if(ajax.readyState == 4 ) {
					if(ajax.responseXML) {
						 processXML(ajax.responseXML, 'Cidades');
					}else{
					//caso não seja um arquivo XML emite a mensagem abaixo
					idOpcao.innerHTML = "--Primeiro selecione o estado--";
					}
				}
     }
		 //passa o código do estado escolhido
	   var params = "estado="+valor;
     ajax.send(params);
}

/********************************\
FUNCAO PARA PEGAR DADOS DO XML
\********************************/
function processXML(obj){
	
	var dataArray   = obj.getElementsByTagName("cidade");
	
	//TOTAL DE ELEMENTOS CONTIDOS NA TAG DO XML
	if(dataArray.length > 0){
	    //PERCORRE O ARQUIVO XML PAARA EXTRAIR OS DADOS
	    for(var i = 0 ; i < dataArray.length ; i++) {
	      var item = dataArray[i];
				
			//CONTÉUDO DOS CAMPOS NO ARQUIVO XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			
			idOpcao.innerHTML = "Selecione";
			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			//atribui um ID a esse elemento
			//novo.setAttribute("id", "opcoes");
			//atribui um valor
			novo.value = codigo;
			//atribui um texto
			novo.text  = descricao;
			
			if(codigo == 11237){
				
			}
			
			//FINALMENTE ADICIONA O NOVO ELEMENTO
			document.imovel.opt_cidade.options.add(novo);

		}
	}else{
		 idOpcao.innerHTML = "--Primeiro selecione o estado--";
	}
	 //caso o XML volte vazio, printa a mensagem abaixo
}

$(function(){
	$('tbody tr')
		.mouseover(function(){
			$(this).addClass('over');
		})
		.mouseout(function(){
			$(this).removeClass('over');
		});


	$('#mostradiv')
		.click(function(){
			$('#oculta').toggle();
		});
	
	$('#bairro')
		.change(function(){
			
			if ($('#bairro').val() == 0){
				$('#cad_bairro').fadeIn("normal");
				$('#cad_bairro').attr("disabled", false);
				$('#cad_bairro').focus();
			}else{
				$('#cad_bairro').fadeOut(1000);
				$('#cad_bairro').attr("disabled", true);
				$('#cad_bairro').val("");
			}
		});

});







$(document).ready(function() {
	
	// Mascaras
//	$("#data").mask("99/99/9999");
	$("#cep").mask("99999-999");
	//$("#cpf").mask("999.999.999-99");
	$("#cnpj").mask("99.999.999/9999-99");
	$("#telefone").mask("(99) 9999-9999");
						   
	 $('a[rel*=popup]').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.8,
		imageLoading: SITE + '/template/js/lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose: SITE + '/template/js/lightbox/images/lightbox-btn-close.gif',
		imageBtnPrev: SITE + '/template/js/lightbox/images/lightbox-btn-prev.gif',
		imageBtnNext: SITE + '/template/js/lightbox//images/lightbox-btn-next.gif',
		imageBlank: SITE + '/template/js/lightbox//images/lightbox-blank.gif',
		txtImage: 'Imagem',
		txtOf: 'de'
 	  });	
	  
	  
	$("#txt-procurar").click(function() {
		$('#txt-procurar').val("");
	});

	$("#txt-procurar").blur(function() {
		if($.trim($('#txt-procurar').val()) == '') {
			$('#txt-procurar').val('Faça a busca no site');
		};
	});
	
	// email
	$("#txt-email").click(function() {
		$('#txt-email').val("");
	});

	$("#txt-email").blur(function() {
		if($.trim($('#txt-email').val()) == '') {
			$('#txt-email').val('e-mail');
		};
	});	
	
	// Nome
	$("#txt-nome").click(function() {
		$('#txt-nome').val("");
	});

	$("#txt-nome").blur(function() {
		if($.trim($('#txt-nome').val()) == '') {
			$('#txt-nome').val('Nome');
		};
	});	
	
		// Login
	$("#txt-login").click(function() {
		$('#txt-login').val("");
	});

	$("#txt-login").blur(function() {
		if($.trim($('#txt-login').val()) == '') {
			$('#txt-login').val('Login de suprimentos ');
		};
	});	
	
		// Login
	$("#txt-senha").click(function() {
		$('#txt-senha').val("");
	});

	$("#txt-senha").blur(function() {
		if($.trim($('#txt-senha').val()) == '') {
			$('#txt-senha').val('Senha');
		};
	});		
		
	$('#player-atual').cycle();	
	
	// Mostra a div oculta da consultoria
	$(".com-nome").toggle(
		function() {
			$(this).parents("div.comentario").children('.com-comentario').slideDown('slow'); // ou slideDown()
		},
		function() {
			$(this).parents("div.comentario").children('.com-comentario').slideUp('slow'); // ou slideDown()
		}	
	);


	// Mostra a div oculta do menu
	$(".bt").toggle(
		function() {
			$(this).parents("div.menu-categoria").children('.sub-categoria').slideDown('slow'); // ou slideDown()
		},
		function() {
			$(this).parents("div.menu-categoria").children('.sub-categoria').slideUp('slow'); // ou slideDown()
		}	
	);
	
$("#resultado").click(
		function(event) {
			
			

			var pergunta = document.votacao.cod_pergunta.value;	
		//Abre a conexÃ£o
		xmlhttp.open("GET",SITE + "/scripts/votar.php?resultado=res&per=" + pergunta, true);

		//FunÃ§Ã£o para tratamento do retorno
		xmlhttp.onreadystatechange=function() {

			if (xmlhttp.readyState==4){

				//Mostra mensagem
				var mensagem	= xmlhttp.responseText;
				mensagem		= mensagem.replace(/\+/g," ");
				mensagem		= unescape(mensagem);
				document.getElementById('votacao').innerHTML=mensagem;
			}
		}

		//Executa
		xmlhttp.send(null)


			event.preventDefault(event);
			return false;
		});
	

});	






