// JavaScript Document



function openAjax() {
	
	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;

}




function arquivo(file, div, msg) {

	var ajax = openAjax();
	
	ajax.open("GET", file, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	ajax.onreadystatechange = function() {
	
	if(ajax.readyState == 1) {
	  
		document.getElementById(div).innerHTML =  '<span style=\"text-decoration: blink;\">'+msg+'<span>';
		
	}
	
		if(ajax.readyState == 4 ) {
		
			 if(ajax.status == 200 ) {
	
				document.getElementById(div).style.visibility="visible";
				document.getElementById(div).innerHTML = ajax.responseText;
			
			 } else {
			 
				document.getElementById(div).innerHTML = "Ops, ocorreu um erro ao carregar o arquivo !!!";

			 }
			 
			 
		} 
	
	}
	
	ajax.send(null);
	
}





function banner(id) {
	
	$("#registraBanner").load('includes/banner.php?id='+id);	
	
}




function bannerRandomico() { 

	$("#saudacaoBanner").load('includes/saudacaoBanner.php');

}




function consultaDownload(sessao) { 

	$("#portfolioResultado").text("Aguarde, buscando dados...").show();
	$("#portfolioResultado").load('includes/portfolioResultado.php?'+sessao+'&busca=false');
	return false;

}






function irPagina(sessao) {
	
	var	IdPagina	=	$('select[name=IdPagina]').val();
	window.location =	"?sessao="+sessao+"&pg="+IdPagina;

}




function janela(url, x, y) {

	window.open(url,"","toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width="+x+",height="+y+",left=40%, top=20%");
	

}










function openAjax() {
	
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;

}





function municipio(valor, arquivo) {

	try {

		ajax = new ActiveXObject("Microsoft.XMLHTTP");

	} catch(e) {

		try {
			
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch(ex) {
			
			try {
				
				ajax = new XMLHttpRequest();
				
			} catch(exc) {
				
				alert("Esse browser nao tem recursos para uso do Ajax");
				ajax = null;
			
			}
		
		}
	
	}

	
	if(ajax) {

		$('#IdMunicipio').children().remove();
		$('#IdMunicipio').append("<option id=\"opc_municipio\"></option>");

		 
		ajax.open("GET", arquivo + "?codEstado=" + valor, true);
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		ajax.onreadystatechange = function() {

			if(ajax.readyState == 1) {
				
				document.getElementById("opc_municipio").innerHTML = "Carregando Municípios...";   
			
			}

	
			if(ajax.readyState == 4 ) {
				
				if(ajax.responseXML) {
					
					processXML(ajax.responseXML);
					
				} else {

					document.getElementById("opc_municipio").innerHTML = "Selecione o Estado";
				
				}
			
			}
		
		}

		var params = "codEstado=" + valor;
		
		ajax.send(params);
		
	}
	
}


function processXML(obj){

	var dataArray   = obj.getElementsByTagName("cidade");
	
	if(dataArray.length > 0) {

		for(var i = 0 ; i < dataArray.length ; i++) {
				
			var item = dataArray[i];
			var codigo    =  item.getElementsByTagName("id")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("nome")[0].firstChild.nodeValue;

			document.getElementById("opc_municipio").innerHTML = "Selecione o Município";
		
			var novo = document.createElement("option");
	
				novo.setAttribute("id", "opcoes");
				novo.value = codigo;
				novo.text  = descricao;
				
				$("#IdMunicipio").append("<option value='"+codigo+"'>"+descricao+"</option>");

				//document.forms[0].IdMunicipio.options.add(novo);


		}


	} else {

		document.getElementById("opc_municipio").innerHTML = "Selecione o Estado";

	}	  



}








function buscaCEP() {
	
	var	cep	=	$('input[name=cadastroCEP]').val();
	document.getElementById('buscaCEP').innerHTML =  "<img src=\"templates/imagens/loader.gif\" />&nbsp;Aguarde, estamos consultando seu CEP...";
	$("#buscaCEP").load('bibliotecas/Php/buscaCEP.php?cep='+cep);
	
}







function preencheCEP(logradouro, bairro, cidade, estado, idEstado) {
	
	$("input#cadastroEndereco").val(logradouro);
	$("input#cadastroBairro").val(bairro);
	$("input#IdMunicipio").val(cidade);
	$("input#IdEstado").val(estado);

	$("input#ocultoMunicipio").val(cidade);
	$("input#ocultoEstado").val(estado);
	$("input#ocultoIdEstado").val(idEstado);

	$("input#cadastroComplemento").focus();
	
}







function mp3(arquivo) { 

	$("#mp3player").text("Aguarde, carregando...").show();
	$("#mp3player").load('includes/mp3.php?arquivo='+arquivo);

}

