var cod_cidade;
var cod_natureza_tipo;

function validaForm()
{
	var nomeFantasia = document.getElementById("nomeFantasia").value;
	var txt_razaosocial = document.getElementById("razaoSocial").value;
	var txt_CNPJ = document.getElementById("CNPJ").value;
	var txt_email = document.getElementById("email").value;
	var txt_senha = document.getElementById('senhaCadastro').value;
	var txt_senha2 = document.getElementById('senhaCadastro2').value;

	
	if(nomeFantasia=="" || txt_razaosocial=="" || txt_CNPJ=="" || txt_email=="" || txt_senha=="" || txt_senha2=="")
	{
		alert("É obrigatório o preenchimento de todos os campos");
		return false;
	}else{
		document.getElementById("form1").submit();
		return true;
	}
	
}

function PegaRegional(cod_rg)
{
	if(cod_rg==0){
		var option = "";
		option = '<select name="select_cidades" id="select_cidades" >';
		option += "<option>-- Escolha uma Regional --</option>";
		option += '</select>';
		document.getElementById('cidades').innerHTML = option;
	}else{
		var url = "/sonih/sih/includes/ajax/busca_municipio.php?";
			url+= "cod_rg="+cod_rg;
			url+= "&busca=municipios";
			url+= "&rand="+Math.random();

			ajax_get(url);
	}
}
function PegaRegionalAdmin(cod_rg)
{
	if(cod_rg==0){
		var option = "";
		option = '<select name="select_cidades" id="select_cidades" >';
		option += "<option>-- Escolha uma Regional --</option>";
		option += '</select>';
		document.getElementById('cidades').innerHTML = option;
	}else{
		var url = "/sonih/sih/includes/ajax/admin/busca_municipio.php?";
			url+= "cod_rg="+cod_rg;
			url+= "&busca=municipios";
			url+= "&rand="+Math.random();

			ajax_get(url);
	}
}

function verificaSenha()
{
	var senha = document.getElementById('senha').value;
	var senha2 = document.getElementById('senha2').value;
	document.getElementById('senha').style.backgroundColor = "#FFFFFF"; 
	document.getElementById('senha2').style.backgroundColor = "#FFFFFF"; 
	
	if(senha.length<=4){
		alert("A senha deve conter mais de 4 caracteres");
		document.getElementById('senha').style.backgroundColor = "red"; 
		return false;
	}
	
	if(senha != senha2)
	{
		alert("As senhas digitadas não são iguais");
		document.getElementById('senha2').style.backgroundColor = "red"; 
		return false;
	}
	
	return true;
}

function PegaNaturezaTipo(cod_natureza)
{
	if(cod_natureza==0){
		var option = "";
		option = '<select name="natureza" id="natureza">';
		option += "<option>-- Escolha uma Natureza --</option>";
		option += "</select>"; 
		document.getElementById('natureza').innerHTML = option;
	}else{
		var url = "/sonih/sih/includes/ajax/busca.php?";
		url+= "cod_natureza="+cod_natureza;
		url+= "&busca=natureza";
		url+= "&rand="+Math.random();

		ajax_get(url);
	}
}

function recebe(msg)
{
	var opcao = msg.split("|");
	var qtde_registros = opcao[2];
	var y = 3;
	var option = "";
	var select = "";
	switch(opcao[0])
	{
		case "municipios":
			switch (opcao[1]){
			case "pega_municipios":
				option = '<select name="select_cidades" id="select_cidades" >';
				for(x=0;x<qtde_registros;x++)
				{
					if(cod_cidade){
						if(cod_cidade==opcao[y]){
							select = "selected='selected'";
						}else{
							select = "";
						}
					}
					option += "<option value='" + opcao[y] + "' "+select+">"+opcao[y+1]+"</option>";
					y=y+2;
				}
				option += "</select>";
				document.getElementById('cidades').innerHTML = option;
				break;
			}
			break;
		case "natureza":
			switch (opcao[1]){
			case "pega_natureza":
				option = '<select name="natureza" id="natureza">';
				for(x=0;x<qtde_registros;x++)
				{
					if(cod_natureza_tipo==opcao[y]){
						select = "selected='selected'";
					}else{
						select = "";
					}
					option += "<option value='" + opcao[y] + "' "+select+">"+opcao[y+1]+"</option>";
					y=y+2;
				}
				option += "</select>"; 
				document.getElementById('td_natureza').innerHTML = option;
				break;
			}
			break;
	}
}

function pesquisa()
{
	$("#message").html("Pesquisando...");
	$("#message").fadeIn("fast");
	
	var values = $("#formFiltro").serialize();
	
	$.ajax({
		type    : "POST",
		url     : "/sonih/sih/includes/ajax/relatorioDinamico.php",
		data    : values,
		success : function(result){
			$(".relatorio").html(result);
			
			$("#message").fadeOut("slow");
		}
	});
}