
// Variáveis globais para detectar browsers.

var isIE;
var isGecko;
var isOpera;
var isSafari;
var isKonqueror;

var ua = navigator.userAgent.toLowerCase();

isIE = (ua.indexOf("msie") != -1);
isGecko = (ua.indexOf("gecko") != -1);
isOpera = (ua.indexOf("opera") != -1);
isSafari = (ua.indexOf("safari") != -1);
isKonqueror = (ua.indexOf("konqueror") != -1);



//Funções comuns globais

function strrev(string){

	//alert("string: " + string);

	stringOriginal = string.split("");

	tamanhoString = stringOriginal.length;

	novaString = Array();

	ind = 0;
	for (i = tamanhoString - 1; i > -1; i--) {
		novaString[ind] = stringOriginal[i];
		ind++;
	}

	novaString = novaString.join("");

	//alert("novaString: " + novaString);

	return novaString;
}

function divida(qtd, string){

	//alert("string: " + string);

	stringOriginal = string;

	tamanhoString = stringOriginal.length;

	novaString = Array();

	ind = 0;

	continuar = true;

	while (continuar) {
		if (tamanhoString > qtd) {
			novaString[ind] = stringOriginal.substring(0, qtd);
			ind++;
			stringOriginal = stringOriginal.substring(qtd);
			tamanhoString = stringOriginal.length;
		} else {
			novaString[ind] = stringOriginal;
			continuar = false;
		}

	}

	//alert("novaString: " + novaString);

	return novaString;
}

function remove(str, sub) {

	rep = "";

	if (arguments[2] != null)
		rep = arguments[2];

	i = str.indexOf(sub);
	r = "";

	if (i == -1)
		return str;

	if (rep != "") {
		r += str.substring(0,i) + rep + remove(str.substring(i + sub.length), sub, rep);
	} else {
		r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
	}

	return r;
}



//Funções comuns globais de ações em formulários

function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.value.length)
		if (r.text == '') return o.value.length
		return o.value.lastIndexOf(r.text)
	} else return o.selectionStart
}

function getSelectionEnd(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveStart('character', -o.value.length)
		return r.text.length
	} else return o.selectionEnd
}

function focarCampo(campo) {
	setTimeout("", 250);
	campo.focus();
}

function limparFormulario(idsObjetos){
	for (i = 0; i < idsObjetos.length; i++) {
		if (window.document.getElementById(idsObjetos[i][0]) != null) {
			window.document.getElementById(idsObjetos[i][0]).innerHTML = idsObjetos[i][1];
		}
	}
}

function enableDisableGroup() {

	objeto = "";
	if (arguments[0] != null)
		objeto = arguments[0];

	nomeGrupo = "";
	if (arguments[1] != null)
		nomeGrupo = arguments[1];

	//campo_checado = document.getElementById(objeto);

	campo_checado = objeto;

	var arrayElements = document.getElementsByTagName('input');

	if (campo_checado.checked) {

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id != objeto) {
					if (arrayElements[i].id.indexOf(nomeGrupo) > -1) {
						arrayElements[i].disabled = "disabled";
					}
				}
			}
		}

	} else {

		for (var i=0; i<arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id != objeto) {
					if (arrayElements[i].id.indexOf(nomeGrupo) > -1) {
						arrayElements[i].disabled = "";
					}
				}
			}
		}

	}
}

function checkUncheckGroup() {

	objeto = "";
	if (arguments[0] != null)
		objeto = arguments[0];

	nomeGrupo = "";
	if (arguments[1] != null)
		nomeGrupo = arguments[1];

	//campo_checado = document.getElementById(objeto);

	campo_checado = objeto;

	var arrayElements = document.getElementsByTagName('input');

	if (campo_checado.checked) {

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id.indexOf(nomeGrupo) > -1) {
					arrayElements[i].checked = true;
				}
			}
		}

	} else {

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id.indexOf(nomeGrupo) > -1) {
					arrayElements[i].checked = false;
				}
			}
		}

	}

}

function enableDisableImportantCheck() {

	objeto = "";
	if (arguments[0] != null)
		objeto = arguments[0];

	nomeCheck = "";
	if (arguments[1] != null)
		nomeCheck = arguments[1];

	nomeGrupo = "";
	if (arguments[2] != null)
		nomeGrupo = arguments[2];

	//checkAtual = document.getElementById(objeto);

	checkAtual = objeto;

	var arrayElements = document.getElementsByTagName('input');

	var allGroupChecked = true;

	for (var i = 0; i < arrayElements.length; i++) {
		if (arrayElements[i].type == 'checkbox') {
			if (arrayElements[i].id.indexOf(nomeCheck) == -1) {
				if (arrayElements[i].id.indexOf(nomeGrupo) != -1) {
					if (!arrayElements[i].checked) {
						//alert(arrayElements[i].id);
						allGroupChecked = false;
					}
				}
			}
		}
	}

	setTimeout('',250);

	if (checkAtual.checked) {

		if (allGroupChecked) {
			for (var i = 0; i < arrayElements.length; i++) {
				if (arrayElements[i].type == 'checkbox') {
					//alert(arrayElements[i].id + " == " + nomeCheck);
					if (arrayElements[i].id == nomeCheck) {
						arrayElements[i].checked = true;
					}
				}
			}
		}

	} else {

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				//alert(arrayElements[i].id + " == " + nomeCheck);
				if (arrayElements[i].id == nomeCheck) {
					arrayElements[i].checked = false;
				}
			}
		}

	}
}



//Funções comuns de validação em formulários do Sistema

function caixaAlta(objeto) {

	objeto.value = objeto.value.toUpperCase();

}

function apenasNumeros(objeto, texto) {

	if (isNaN(objeto.value)) {
		alert(texto);
		objeto.value = '';
		focarCampo(objeto);
	}

}

function validarTextoGrande(campo) {

	//alert("Funcionou!");

	//alert(campo.value.length);

	if (campo.value.length > 240) {

		campo.value = campo.value.substring(0, 240);

		alert("São permitidos apenas 240 caracteres!");

	}

}

function checkAllSolics(nomeCheck, prefixo, nomeBotao) {

	campo_checado = document.getElementById(nomeCheck);

	if (campo_checado.checked) {
		var arrayElements = document.getElementsByTagName('input');

		existe_checks = false;

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id.indexOf(prefixo) != -1) {
					arrayElements[i].checked = true;
					existe_checks = true;
				}
			}
		}

		if (existe_checks)
			for (var i = 0; i < arrayElements.length; i++) {
				if (arrayElements[i].type == 'submit') {
					if (arrayElements[i].name == "indeferir-" + nomeBotao) {
						arrayElements[i].disabled = "";
						arrayElements[i].className = "form nivel_dois";
					}
				}
			}

	} else {
		var arrayElements = document.getElementsByTagName('input');

		existe_checks = false;

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id.indexOf(prefixo) != -1) {
					arrayElements[i].checked = false;
					existe_checks = true;
				}
			}
		}

		if (existe_checks)
			for (var i = 0; i < arrayElements.length; i++) {
				if (arrayElements[i].type == 'submit') {
					if (arrayElements[i].name == "indeferir-" + nomeBotao) {
						arrayElements[i].disabled = "disabled";
						arrayElements[i].className = "form nivel_dois desabilitado";
					}
				}
			}
	}
}

function checkCheckAllChecks(checkAtual, nomeGrupo, nomeIdentificador, nomeBotao) {

	setTimeout('',250);

	if (checkAtual.checked) {

		//alert("Entrou aqui");

		setTimeout('',250);

		var allCheckeds = true;

		var arrayElements = document.getElementsByTagName('input');

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id.indexOf(nomeGrupo) != -1) {
					//alert("Entrou aqui 2");
					if (arrayElements[i].id.indexOf(nomeIdentificador) != -1) {
						if (!arrayElements[i].checked) {
							//alert("Entrou aqui 3");
							allCheckeds = false;
						}
					}
				}
			}
			if (arrayElements[i].type == 'submit') {
				if (arrayElements[i].name == "indeferir-" + nomeBotao) {
					arrayElements[i].disabled = "";
					arrayElements[i].className = "form nivel_dois";
				}
			}
		}

		if (allCheckeds) {
			for (var i = 0; i < arrayElements.length; i++) {
				if (arrayElements[i].type == 'checkbox') {
					if (arrayElements[i].name == nomeGrupo + "-checkAllChecks") {
						arrayElements[i].checked = true;
					}
				}
			}
		}

	} else {

		setTimeout('',250);

		noneChecked = true;

		var arrayElements = document.getElementsByTagName('input');

		for (var i = 0; i < arrayElements.length; i++) {
			if (arrayElements[i].type == 'checkbox') {
				if (arrayElements[i].id.indexOf(nomeGrupo) != -1) {
					if (arrayElements[i].id.indexOf(nomeIdentificador) != -1) {
						if (arrayElements[i].checked) {
							noneChecked = false;
						}
					}
				}
				if (arrayElements[i].name == nomeGrupo + "-checkAllChecks") {
					arrayElements[i].checked = false;
				}
			}
		}

		if (noneChecked) {
			for (var i = 0; i < arrayElements.length; i++) {
				if (arrayElements[i].type == 'submit') {
					if (arrayElements[i].name == "indeferir-" + nomeBotao) {
						arrayElements[i].disabled = "disabled";
						arrayElements[i].className = "form nivel_dois desabilitado";
					}
				}
			}
		}

	}
}

function manterPreenchido(campo, origem, zeros) {

	if (origem == "f") {

		valorOriginal = campo.value;

		valorOriginal = valorOriginal.replace(".", "");

		valorOriginal = valorOriginal.replace(",", ".");

		valorOriginal = parseFloat(valorOriginal);

		if (valorOriginal == 0) {

			campo.value = "";

		}

	}

	if (origem == "b") {

		valorOriginal = campo.value;

		novoTamanhoValor = valorOriginal.length;

		posicaoVirgula = valorOriginal.indexOf(",");

		if ((valorOriginal == "") || (posicaoVirgula == novoTamanhoValor - 1)) {

			campo.value = zeros;

		}
	}

}

function validarValor(objeto, valor, decimais) {

	qtd_zeros = "";
	for (i = 0; i < decimais; i++) {
		qtd_zeros += "0";
	}

	if (valor != "") {

		valor = strrev(valor);
		if (valor.indexOf(".") == 0) {
			valor = valor.replace(".", ",");
		}
		if (valor.indexOf(".") == 1) {
			valor = valor.replace(".", ",");
		}
		if (valor.indexOf(".") == 2) {
			valor = valor.replace(".", ",");
		}
		valor = strrev(valor);

		//alert("valor antes: " + valor);

		valor = remove(valor, ".", "");
		valor = remove(valor, ",", ".");

		//alert("valor depois: " + valor);

		if (!isNaN(valor)) {

			valor = parseFloat(valor);

			//alert("valor antes: " + valor);

			valor = valor.toString();

			valor = remove(valor, ".", "@");
			valor = remove(valor, ",", ".");
			valor = remove(valor, "@", ",");

			//alert("valor depois: " + valor);

			tamanhoValor = valor.length;

			//alert("tamanhoValor " + tamanhoValor);

			if (tamanhoValor > 0) {

				caracteresValor = valor.split("");

				novosCaracteresValor = Array();

				ind = 0;

				naoExisteVirgula = true;

				for (i = 0; i < tamanhoValor; i++) {
					if (isNaN(caracteresValor[i])) {
						if (naoExisteVirgula) {
							if (caracteresValor[i] == ",") {
								naoExisteVirgula = false;
								novosCaracteresValor[ind] = caracteresValor[i];
								ind++;
							}
						}
					} else {
						novosCaracteresValor[ind] = caracteresValor[i];
						ind++;
					}
				}

				novoValor = novosCaracteresValor.join("");

				//alert("novoValor antes: " + novoValor);

				novoTamanhoValor = novoValor.length;

				posicaoVirgula = novoValor.indexOf(",");

				if (posicaoVirgula == -1) {
					novoValor = novoValor + "," + qtd_zeros;
				}

				if (tamanhoValor > 1) {
					if (posicaoVirgula == 0) {
						novoValor = "0," + novoValor;
					}
				} else {
					if (posicaoVirgula == 0) {
						novoValor = "0," + qtd_zeros;
					}
				}

				//alert("novoValor depois: " + novoValor);

				novoTamanhoValor = novoValor.length;

				posicaoVirgula = novoValor.indexOf(",");

				if (posicaoVirgula > 0) {

					primeiraParte = novoValor.substring(0, posicaoVirgula);

					//alert("primeiraParte: " + primeiraParte);

					segundaParte = novoValor.substring(posicaoVirgula + 1, novoTamanhoValor);

					//alert("segundaParte: " + segundaParte);

					qtdSegundaParte = segundaParte.length;

					//alert("qtdSegundaParte: " + qtdSegundaParte);

					if (qtdSegundaParte > decimais) {
						segundaParte = segundaParte.substring(0, decimais);
					}
					if (qtdSegundaParte < decimais) {
						segundaParte = segundaParte + qtd_zeros.substring(0, decimais - qtdSegundaParte);
					}


					qtdPrimeiraParte = primeiraParte.length;

					//alert("qtdPrimeiraParte: " + qtdPrimeiraParte);

					//alert("primeiraParte antes: " + primeiraParte);

					valor = primeiraParte;

					valor = strrev(valor);

					//alert("valor antes: " + valor);

					valor = divida(3, valor);

					//alert("valor depois: " + valor);

					valor = valor.join(".");

					valor = strrev(valor);

					primeiraParte = valor;

					//alert("primeiraParte depois: " + primeiraParte);

					novoValor = primeiraParte + "," + segundaParte;

					//alert("novoValor: " + novoValor);

				}

				//alert("novoValor final: " + novoValor);

				return novoValor;

			} else {
				return "0," + qtd_zeros;
			}

		} else {
			if (objeto != "") {
				alert("Digite apenas valores válidos neste campo!");
				focarCampo(window.document.getElementById(objeto));
			}
			return "0," + qtd_zeros;
		}

	} else {
		return "0," + qtd_zeros;
	}

}

function somarTodos(prefixo, visor, destino, decimais) {

	var arrayElements = document.getElementsByTagName('input');

	total = 0;

	for (var i = 0; i < arrayElements.length; i++) {
		if (arrayElements[i].id.indexOf(prefixo) > -1) {
			valor = arrayElements[i].value;
			valor = remove(valor, ".", "");
			valor = remove(valor, ",", ".");
			//alert(valor);
			valor = parseFloat(valor);
			//alert(valor);
			total = total + valor;
		}
	}

	total = total.toString();
	total = remove(total, ".", ",");

	total = validarValor("", total, decimais);

	document.getElementById(visor).innerHTML = total;
	document.getElementById(destino).value = total;
	//alert(document.getElementById(destino).value);
}

function limitarValor(idCampo_1, idCampo_2, texto) {

	//alert(idCampo_1);
	//alert(idCampo_2);

	valor_1 = window.document.getElementById(idCampo_1).value;
	//alert(valor_1);

	valor_1_original = valor_1;

	valor_1 = remove(valor_1,".","");
	valor_1 = remove(valor_1,",",".");
	//alert(valor_1);

	valor_1 = parseFloat(valor_1);
	//alert(valor_1);

	valor_2 = window.document.getElementById(idCampo_2).value;
	//alert(valor_2);

	valor_2 = remove(valor_2,".","");
	valor_2 = remove(valor_2,",",".");
	//alert(valor_2);

	valor_2 = parseFloat(valor_2);
	//alert(valor_2);

	tipo_comparacao = "";
	if (arguments[3] != null)
		tipo_comparacao = arguments[3];

	if (valor_2 > 0) {
		if (tipo_comparacao == "") {
			if (valor_1 < valor_2) {

				window.document.getElementById(idCampo_2).value = valor_1_original;

				alert(texto);

			}
		} else {
			if (valor_1 > valor_2) {

				window.document.getElementById(idCampo_2).value = valor_1_original;

				alert(texto);

			}
		}
	}
}

function validarDiaMes(nome_campo, texto) {

	campo = window.document.getElementById(nome_campo);

	valor = campo.value;
	valor = valor.replace("/", "");

	//alert(valor);

	if (isNaN(valor)) {
		campo.value = "";
		alert(texto);
		focarCampo(campo);
		return false;
	} else {

		//alert(valor.substring(0,2));

		var dia = parseInt(valor.substring(0,2));

		//alert("dia" + dia);

		if (dia > 28) {
			campo.value = "";
			alert(texto);
			focarCampo(campo);
			return false;
		} else {

			if (dia < 1) {
				campo.value = "";
				alert(texto);
				focarCampo(campo);
				return false;
			}
		}
	}
}

function mascararCPF(campo) {

	if (campo.value.length == 3){
		campo.value = campo.value + '.';
	}
	if (campo.value.length == 7){
		campo.value = campo.value + '.';
	}
	if (campo.value.length == 11){
		campo.value = campo.value + '-';
	}
}

function validarMascaraCPF(nome_campo) {

	campo = window.document.getElementById(nome_campo);

	valor = campo.value;
	valor = valor.replace(".", "");
	valor = valor.replace("-", "");

	if (isNaN(valor)) {
		campo.value = "";
		alert("Digite apenas números no campo CPF!");
		focarCampo(campo);
		return false;
	}
}

function mascararCNPJ(campo) {

	if (campo.value.length == 2){
		campo.value = campo.value + '.';
	}
	if (campo.value.length == 6){
		campo.value = campo.value + '.';
	}
	if (campo.value.length == 10){
		campo.value = campo.value + '/';
	}
	if (campo.value.length == 15){
		campo.value = campo.value + '-';
	}
}

function validarMascaraCNPJ(nome_campo) {

	campo = window.document.getElementById(nome_campo);

	valor = campo.value;
	valor = valor.replace(".", "");
	valor = valor.replace(".", "");
	valor = valor.replace("/", "");
	valor = valor.replace("-", "");

	if (isNaN(valor)) {
		campo.value = "";
		alert("Digite apenas números no campo CNPJ!");
		focarCampo(campo);
		return false;
	}
}



//Funções comuns de ações de ingerência nos dados do Sistema

var parametro_validar_rg = 'n';

function validarRG(campoRG, painel_resposta, e) {

	var RG_validar = window.document.getElementById(campoRG).value;

	if (RG_validar.length > 0) {

		if (parametro_validar_rg != "n") {

			RG_validar = remove(RG_validar," ", "");

			complemento = "";
			if (e != "") {
				complemento = "&e=" + e;
			}
			//alert(complemento);

			fonte_dados = "";
			if (arguments[3] != null)
				fonte_dados = arguments[3];

			origem = "../../sistema/funcoes/";

			necessitaResposta = true;
			tipoDadosResposta = "texto";
			modoResposta = ["alert",true,true];
			idDestinoResposta = painel_resposta;
			exibirCarregando = false;
			idDestinoCarregando = "";
			conteudoCarregando = "";
			tratarErro = false;
			idDestinoMensagemErro = "";
			metodoRequisicao = "GET";
			urlRequisicao = origem + "funcoes_validar_cpf.php?rg=" + RG_validar + complemento + fonte_dados;
			estadoAssincrono = true;
			dadosEnviar = null;

			retorneResposta(necessitaResposta, tipoDadosResposta, modoResposta, idDestinoResposta, exibirCarregando, idDestinoCarregando, conteudoCarregando, tratarErro, idDestinoMensagemErro, metodoRequisicao, urlRequisicao, estadoAssincrono, dadosEnviar);

		}

	}

}

var parametro_validar_cpf = 'n';

function validarCPF(campoCPF, painel_resposta, e) {

	var cpf_validar = window.document.getElementById(campoCPF).value;

	if (cpf_validar.length > 0) {

		var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;

		if (!filtro.test(cpf_validar)) {

			window.document.getElementById(campoCPF).value = "";
			focarCampo(window.document.getElementById(campoCPF));
			window.alert("1. CPF inválido. Tente novamente.");
			return false;

		} else {

			cpf_validar = remove(cpf_validar, ".");
			cpf_validar = remove(cpf_validar, "-");

			//alert(cpf_validar);

			if (cpf_validar.length != 11 ||
				cpf_validar == "00000000000" ||
				cpf_validar == "11111111111" ||
				cpf_validar == "22222222222" ||
				cpf_validar == "33333333333" ||
				cpf_validar == "44444444444" ||
				cpf_validar == "55555555555" ||
				cpf_validar == "66666666666" ||
				cpf_validar == "77777777777" ||
				cpf_validar == "88888888888" ||
				cpf_validar == "99999999999"){

				window.document.getElementById(campoCPF).value = "";
				focarCampo(window.document.getElementById(campoCPF));
				window.alert("2. CPF inválido. Tente novamente.");
				return false;

			} else {

				soma = 0;

				for (i = 0; i < 9; i++) {
					soma += parseInt(cpf_validar.charAt(i)) * (10 - i);
				}

				resto = 11 - (soma % 11);

				if (resto == 10 || resto == 11) {
					resto = 0;
				}

				if (resto != parseInt(cpf_validar.charAt(9))) {

					window.document.getElementById(campoCPF).value = "";
					focarCampo(window.document.getElementById(campoCPF));
					window.alert("3. CPF inválido. Tente novamente.");
					return false;

				} else {

					soma = 0;

					for (i = 0; i < 10; i ++) {
						soma += parseInt(cpf_validar.charAt(i)) * (11 - i);
					}

					resto = 11 - (soma % 11);

					if (resto == 10 || resto == 11) {
						resto = 0;
					}

					if (resto != parseInt(cpf_validar.charAt(10))) {

						window.document.getElementById(campoCPF).value = "";
						focarCampo(window.document.getElementById(campoCPF));
						window.alert("4. CPF inválido. Tente novamente.");
						return false;

					} else {

						if (parametro_validar_cpf != "n") {

							complemento = "";
							if (e != "") {
								complemento = "&e=" + e;
							}
							//alert(complemento);

							fonte_dados = "";
							if (arguments[3] != null)
								fonte_dados = arguments[3];

							origem = "../../sistema/funcoes/";

							necessitaResposta = true;
							tipoDadosResposta = "texto";
							modoResposta = ["alert",true,true];
							idDestinoResposta = painel_resposta;
							exibirCarregando = false;
							idDestinoCarregando = "";
							conteudoCarregando = "";
							tratarErro = false;
							idDestinoMensagemErro = "";
							metodoRequisicao = "GET";
							urlRequisicao = origem + "funcoes_validar_cpf.php?cpf=" + cpf_validar + complemento + fonte_dados;
							estadoAssincrono = true;
							dadosEnviar = null;

							retorneResposta(necessitaResposta, tipoDadosResposta, modoResposta, idDestinoResposta, exibirCarregando, idDestinoCarregando, conteudoCarregando, tratarErro, idDestinoMensagemErro, metodoRequisicao, urlRequisicao, estadoAssincrono, dadosEnviar);

						}

					}
				}
			}
		}
	}
}

var parametro_validar_cnpj = 'n';

function validarCNPJ(campoCNPJ, painel_resposta, e) {

	var CNPJ_validar = window.document.getElementById(campoCNPJ).value;

	//alert(CNPJ_validar);

	if (CNPJ_validar.length > 0) {

		str = CNPJ_validar;

		var filtro = /^\d{2}.\d{3}.\d{3}\/\d{4}-\d{2}$/i;

		//alert(str);

		//var filtro = /^\d?(\d{2})\.?(\d{3})\.?(\d{3})\/?(\d{4})\-?(\d{2})/;

		if (!filtro.test(str)) {

			window.document.getElementById(campoCNPJ).value = "";
			focarCampo(window.document.getElementById(campoCNPJ));
			window.alert("1. CNPJ inválido. Tente novamente.");
			return false;

		} else {

			str = remove(str, ".");
			str = remove(str, "-");
			str = remove(str, "/");

			//alert(str);

			var sum1 = 0, sum2 = 0, calc1 = 5, calc2 = 6;

			//alert(str);

			//alert(str);

			str = str.split("");

			for (var i = 0; i <= 12; i++) {
				calc1 = (calc1 < 2) ? 9 : calc1;

				//alert(str[i]);

				if (i <= 11)
					sum1 += parseInt(str[i]) * calc1;

				//alert(sum1);

				calc1--;
			}

			//alert("sum1: " + sum1);

			sum1 %= 11;

			//alert("sum1: " + sum1);

			sum1 = sum1 < 2 ? 0 : 11 - sum1;

			//alert("sum1: " + sum1);

			if (sum1 == str[12]) {

				for (var i=0; i <= 12; i++) {
					calc2 = (calc2 < 2) ? 9 : calc2;

					sum2 += parseInt(str[i]) * calc2;

					calc2--;
				}

				//alert("sum2: " + sum2);

				sum2 %= 11;

				//alert("sum2: " + sum2);

				sum2 = sum2 < 2 ? 0 : 11 - sum2;

				//alert("sum2: " + sum2);

				if (sum2 == str[13]) {

					str = str.join("");

					CNPJ_validar = str;

					if (parametro_validar_cnpj != "n") {

						complemento = "";
						if (e != "") {
							complemento = "&e=" + e;
						}
						//alert(complemento);

						fonte_dados = "";
						if (arguments[3] != null)
							fonte_dados = arguments[3];

						//alert(CNPJ_validar);

						origem = "../../sistema/funcoes/";

						necessitaResposta = true;
						tipoDadosResposta = "texto";
						modoResposta = ["alert",true,true];
						idDestinoResposta = painel_resposta;
						exibirCarregando = false;
						idDestinoCarregando = "";
						conteudoCarregando = "";
						tratarErro = false;
						idDestinoMensagemErro = "";
						metodoRequisicao = "GET";
						urlRequisicao = origem + "funcoes_validar_cpf.php?cnpj=" + CNPJ_validar + complemento + fonte_dados;
						estadoAssincrono = true;
						dadosEnviar = null;

						retorneResposta(necessitaResposta, tipoDadosResposta, modoResposta, idDestinoResposta, exibirCarregando, idDestinoCarregando, conteudoCarregando, tratarErro, idDestinoMensagemErro, metodoRequisicao, urlRequisicao, estadoAssincrono, dadosEnviar);

					}

				} else {

					window.document.getElementById(campoCNPJ).value = "";
					focarCampo(window.document.getElementById(campoCNPJ));
					window.alert("3. CNPJ inválido. Tente novamente.");
					return false;

				}

			} else {

			window.document.getElementById(campoCNPJ).value = "";
			focarCampo(window.document.getElementById(campoCNPJ));
			window.alert("2. CNPJ inválido. Tente novamente.");
			return false;

			}

		}

	}

}

function carregarSolicitantes(selectOrig, selectDest, idd, tt){

	enviar_tam_seletor = "";
	if (window.document.getElementById("tamanho_seletor")) {
		enviar_tam_seletor = "&ts=" + window.document.getElementById("tamanho_seletor").value;
	}

	valor_selecionado = selectOrig.value;

	origem = "../../sistema/funcoes/";

	necessitaResposta = true;
	tipoDadosResposta = "texto";
	modoResposta = ["innerHTML"];
	idDestinoResposta = selectDest;
	exibirCarregando = false;
	idDestinoCarregando = "";
	conteudoCarregando = "";
	tratarErro = false;
	idDestinoMensagemErro = "";
	metodoRequisicao = "GET";
	urlRequisicao = origem + "funcoes_listar_solicitantes.php?idund=" + valor_selecionado + "&idd=" + idd + "&tt=" + tt + enviar_tam_seletor;
	estadoAssincrono = true;
	dadosEnviar = null;

	retorneResposta(necessitaResposta, tipoDadosResposta, modoResposta, idDestinoResposta, exibirCarregando, idDestinoCarregando, conteudoCarregando, tratarErro, idDestinoMensagemErro, metodoRequisicao, urlRequisicao, estadoAssincrono, dadosEnviar);

}

function validarReprovacaoSolicitacoes(tipo) {

	permitir_indeferir = false;

	var arrayElements = document.getElementsByTagName('input');

	for (var i = 0; i < arrayElements.length; i++) {
		if (arrayElements[i].type == 'checkbox') {
			//alert(arrayElements[i].id);
			if (arrayElements[i].id.indexOf("solic-" + tipo) != -1) {
				if (arrayElements[i].checked) {
					permitir_indeferir = true;
				}
			}
		}
	}

	if (permitir_indeferir) {
		respostaUsr = confirm('Tem certeza que quer indeferir as Solicitações selecionadas?');
		if (!respostaUsr){
		//	formulario_indeferir_solicitacoes.submit();
		//} else {
			return false;
		}
	}

}

/* FUNÇÕES DIEGO RAPHAEL */

function mascaraMoeda(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete (Bug fixed)
	if (whichCode == 9) return true;  // Tab
	
	if (whichCode == 0) return true; // em fase de teste
	
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

