function salvar() {
	verificar();
}

var pj = false;
var pf = false;
var func = false;
var site=1;
var razaoSocial=0;
var cpf = 0;
var sexo = 0;
var nascimento = 0;
var profissao = 0;
var celular = 0;
var cnpj = 0;
var inscricao = 0;
var ramoAtividade = 0;

function atualizarVisualizacao() {
	
	if  (cpf > 0){
		document.getElementById("trCpf").style.display = (document.all) ? "block" : "table-row";
	} else {
		document.getElementById("trCpf").style.display = "none";
	}
	
	if  (sexo > 0){
		document.getElementById("trSexo").style.display = (document.all) ? "block" : "table-row";
	} else {
		document.getElementById("trSexo").style.display = "none";
	}
	
	if  (nascimento > 0){
		document.getElementById("trNascimento").style.display = (document.all) ? "block" : "table-row";
	} else {
		document.getElementById("trNascimento").style.display = "none";
	}
	
	if  (celular > 0){
		document.getElementById("trCelular").style.display = (document.all) ? "block" : "table-row";
	} else {
		document.getElementById("trCelular").style.display = "none";
	}
	
	if  (cnpj > 0){
		document.getElementById("trCnpj").style.display = (document.all) ? "block" : "table-row";
	} else {
		document.getElementById("trCnpj").style.display = "none";
	}
	
	if  (inscricao > 0){
		document.getElementById("trInscricao").style.display = (document.all) ? "block" : "table-row";
	} else {
		document.getElementById("trInscricao").style.display = "none";
	}
	
	if  (razaoSocial > 0){
		document.getElementById("trRazaoSocial").style.display = (document.all) ? "block" : "table-row";		
	} else {
		document.getElementById("trRazaoSocial").style.display = "none";
	}
	
	if  (site > 0){
		document.getElementById("trSite").style.display = (document.all) ? "block" : "table-row";		
	} else {
		document.getElementById("trSite").style.display = "none";
	}
}

function mostrarPJ(campo){
	if (pf || func){
		alert("Uma pessoa física não podem ser uma pessoa jurídica.");
		campo.checked = false;
		return;
	} else {
		document.usuarioForm['vo.tipoPessoa'].value = campo.value;
	}
	
	var soma = 0;
	if (campo.checked){
		soma = 1;
		pj = true;
	} else {
		soma = -1;
		pj = false;
	}
	
	cnpj += soma;
	inscricao += soma;
	ramoAtividade += soma;
	razaoSocial += soma;
	site += soma;

	atualizarVisualizacao();
}

function mostrarPF(campo) {
	if (pj) {
		alert("Uma pessoa física não podem ser uma pessoa jurídica.");
		campo.checked = false;
		return;
	} else {
		document.usuarioForm['vo.tipoPessoa'].value = campo.value;
	}
	
	var soma = 0;
	if (campo.checked){
		soma = 1;
		pf = true;
	} else {
		soma = -1;
		pf = false;
	}

	cpf += soma;
	sexo += soma;
	nascimento += soma;
	profissao += soma;
	celular += soma;	
	
	atualizarVisualizacao();
}

function verificar() {
	if (!(pf || pj || func)){
		alert("Escolha o tipo da pessoa.");
		return;
	}

	if (document.usuarioForm['vo.nome'].value == ""){
		alert("O campo nome é obrigatório.");
		document.usuarioForm['vo.nome'].focus();
		return false;
	}
	
	// TIPOS
	var listaTipo = document.getElementsByName('listaTipo[]');
	var len = listaTipo.length;
	var check = 0;
	for (i = 0; i < len; i++) {
		if (listaTipo[i].checked == true) {
			check++;
		}
	}

	if (pf || func) {
		
		
			if (document.usuarioForm['cpf'].value != "" && !validaCPF(document.usuarioForm['cpf'].value)) {
				alert('CPF inválido.');
				document.usuarioForm['cpf'].focus();
				return false;
			}
		

	} else {
		if (document.usuarioForm['cnpj'].value == ""){
			
		} else if (!validaCNPJ(document.usuarioForm['cnpj'].value)){
			alert('CNPJ inválido.');
			document.usuarioForm['cnpj'].focus();
			return false;
		} else if (document.usuarioForm['vo.inscricaoEstadual'].value == ""){
			
		}
	}

	if (document.usuarioForm['vo.email'].value != ""){
		if (!validaEmail(document.usuarioForm['vo.email'])){
			alert("Email inválido.");
			document.usuarioForm['vo.email'].focus();
			return;
		}

		if (email == "E-mail já cadastrado!"){
			alert("Email já cadastrado.");
			document.usuarioForm['vo.email'].focus();
			return;
		}
	}
	
	if (document.usuarioForm['estado'].value == "0" || document.usuarioForm['estado'].value == "") {
		alert('O campo Estado é obrigatório');
		document.usuarioForm['estado'].focus();
		return false;
	}


	if (document.usuarioForm['cidade'].value == "0" || document.usuarioForm['cidade'].value == "") {
		alert('O campo Cidade é obrigatório');
		document.usuarioForm['cidade'].focus();
		return false;
	}
	
	if (check == 0) {
		alert('Pelo menos um tipo deve ser selecionado');
		return false;
	}
	
	//alert(document.usuarioForm['vo.naturezaOperacao.id'].value);
	
	
	document.usuarioForm.submit();
}

function editarFornecedor(id) {
	eval(document["frm_"+id].submit());
}

function alteraStatus(id, status) {
	window.open("exec.php?action=status&id="+id+"&status="+status, "iframe");
}

function isNumber(i) {
	if (i >= 48 && i <= 57)
		return true;
	else
		return false;
}

function isCharacter(i) {
	if ((i >= 97 && i <= 122) || (i >= 65 && i <= 90))
		return true;
	else
		return false;
}

function validaSenha(senha) {
	var numero=false;
	var texto=false;
	
	for (i=0; i<=senha.length-1; i++) {
		caracter = senha.substr(i, 1);
		
		if (isNumber(senha.charCodeAt(i))) numero=true;
		if (isCharacter(senha.charCodeAt(i))) texto=true;
	}
	
	if (numero && texto)
		return true;
	else
		return false;
}


function obs(id, nome) {
	var barra	= 1;
	var url 	= "obs.php?id="+id+"&nome="+nome;
	var largura	= 480;
	var altura	= 200;
	var janela	= "obs";
	var barra	= "0";
	
	window.open(url, janela, 'width=' + largura + ',height=' + altura + ', scrollbars=' + barra + ' ,top=' + ((screen.availHeight/2) - (altura/2)) + ',left=' + ((screen.availWidth/2) - (largura/2)));
}

