// -----------------------------------------------------------------------
// V - Todos os forms

function check_all_checkbox(chk, frm_name) {
	var i, flag = false;
	var frm = document.forms[frm_name];
	
	for(i = 0; i < frm.elements.length; i++) {
		if(frm.elements[i].name.indexOf("chk_") != -1) {
			if(chk.checked) {
				frm.elements[i].checked = true;
			}
			else {
				frm.elements[i].checked = false;
			}
			
			if(frm.elements[i].value) {
				tr_on_off(frm.elements[i], frm.elements[i].value);
			}
		}
	}
}

function v_retorno(frm_name, vlr) {
	var frm = document.forms[frm_name];
	frm.retorno.value = vlr;
}

function v_checkbox(frm) {
	var i, flag = false;
	
	for(i = 0; i < frm.elements.length; i++) {
		if((frm.elements[i].name.indexOf("chk_") != -1) && (frm.elements[i].checked)) {
			flag = true;
			break;
		}
	}
	
	return flag;
}

function v_checkbox2(frm_name) {
	var i, flag = false;
	var frm = document.forms[frm_name];
	
	for(i = 0; i < frm.elements.length; i++) {
		if((frm.elements[i].name.indexOf("chk_") != -1) && (frm.elements[i].checked)) {
			flag = true;
			break;
		}
	}
	
	if(!flag) {
		v_alerta("ERRO", "Selecione pelo menos 1 registro");
	}
	else {
		v_alerta("INFO", "Processando...");
	}
	
	return flag;
}

function v_frm_filtro_enter(frm_name) {
	if(event.keyCode == 13) {
		v_filtro(frm_name, 1);
	}
	
	return false;
}

function v_frm_enter(frm_name) {
	if(event.keyCode == 13) {
		document.forms[frm_name].submit();
	}
	
	return false;
}

function cancelar() {
	if(confirm('Cancelar ação?\nDados inseridos e/ou modificados serão perdidos.')) {
		voltar();
	}
	
	return false;
}

function v_filtro(f, pg, ord, sent) {
	var frm = document.forms[f];

	if(!pg) {
		pg = 1;
	}
	
	if(ord) {
		frm.filtro_ordenar.value = ord;
	}
	
	if(sent) {
		frm.filtro_ordenar_sentido.value = sent;
	}
	
	frm.filtro.value = "ativar";
	frm.filtro_pgn.value = pg;
	
	frm.submit();
}

function v_qtd_reg_pgn(f) {
	var frm = document.forms[f];
	var rdo = frm.rdo_qtd_reg_pgn;
	var qtd = frm.filtro_qtd_reg_pgn;
	var i;
	
	for(i = 0; i < rdo.length; i++) {
		if(rdo[i].checked) {
			qtd.value = rdo[i].value;
			break;
		}
	}
}

function v_excluir() {
	if(!confirm('Confirmar exclusão de dados?')) {
		return false;
	}
	
	v_alerta("INFO", "Processando...");
	
	return true; 
}

// -----------------------------------------------------------------------
// V - Tooltip


function v_alerta(tipo, msg) {
	var m = "<font size='3'>&nbsp;&nbsp;</font>";
	var obj = parent.document.getElementById("dv-msg");
	
	if((tipo != "") && (tipo == 0)) {
		tipo = "OK";
	}
	
	if((tipo != "") && (tipo == 1)) {
		tipo = "ERRO";
	}
	
	if(tipo == "ERRO") {
		m = "<font color='#FFCC00'><strong>ERRO<font size='3'>&nbsp;&raquo;&nbsp;</font></strong>"+msg+"</font>";
	}
	else if(tipo == "OK") {
		m = "<font color='#FFFFFF'><strong>OK<font size='3'>&nbsp;&raquo;&nbsp;</font></strong>"+msg+"</font>";
	}
	else if(tipo == "INFO"){
		m = "<font color='#333333'><strong>INFO<font size='3'>&nbsp;&raquo;&nbsp;</font></strong>"+msg+"</font>";
	}
	
	obj.innerHTML = m;
	
	/*if(obj.innerHTML.indexOf("ERRO") != -1) {
		if(tipo != "INFO") {
			obj.innerHTML = m;
		}
	}
	else {
		obj.innerHTML = m;
	}*/
}

function v_tooltip_img(pasta, img) {
	if(!img) {
		toolTip();
		return false;
	}
	
	var cnt = "<img src='../include/imagem.asp?PathImagem="+pasta+"\\"+img+"' border='0' align='absmiddle' width='160' style='margin:3px 0px 2px 0px'>";
	toolTip(cnt);
}

function v_tooltip(ttip, req, tipo, msg) {
	var cnt = "<span class=pequenapreto>";
	
	if(!ttip) {
		toolTip();
		return false;
	}
	
	if(msg) {
		cnt += msg+"<br>";
	}
	
	if(req == 1) {
		cnt += "<font color='#CC0000'>This field is mandatory</font><br>";
	}
	
	if(tipo == "email") {
		cnt += "E-mail: alphanumeric field";
		cnt += "<br>Ex.: usuario@provedor.com.br";
	}
	else if(tipo == "cnpj") {
		cnt += "Cnpj: numeric field";
		cnt += "<br>Ex.: xx.xxx.xxx/xxxx-xx";
	}
	else if(tipo == "data") {
		cnt += "Date: numeric field";
		cnt += "<br>Ex.: dd/mm/aaaa";
	}
	else if(tipo == "cpf") {
		cnt += "Cpf: numeric field";
		cnt += "<br>Ex.: xxx.xxx.xxx-xx";
	}
	else if(tipo == "num") {
		cnt += "Numeric field";
		cnt += "<br>Ex.: 123456";
	}
	else if(tipo == "cep") {
		cnt += "Cep: campo numérico";
		cnt += "<br>Ex.: xxxxx-xxx";
	}
	else if(tipo == "combo") {
		cnt += "Lista de seleção";
		cnt += "";
	}
	else if(tipo == "checkbox") {
		cnt += "Caixa de seleção";
		cnt += "";
	}
	else if(tipo == "radio") {
		cnt += "Caixa de seleção";
		cnt += "";
	}
	else if(tipo == "arquivo") {
		cnt += "Endereço do arquivo";
		cnt += "";
	}
	else {
		cnt += "Alphanumeric field";
		cnt += "<br>Ex.: 1234text";
	}
	
	cnt += "</span>";
	
	toolTip(cnt);
}

// -----------------------------------------------------------------------
// V - FORM AREA ATIVIDADE

function v_area_atividade_form(frm, acao) {
	var area_atividade = frm.txt_area_atividade;
	
	if(!v_texto(area_atividade.value, 0, 1, 100)) {
		v_alerta("ERRO", "ÁREA DE ATIVIDADE: Preenchimento obrigatório.");
		area_atividade.focus();
		return false;
	}
	
	if(acao == "A") {
		if(!confirm('Confirmar edição de dados?')) {
			return false;
		}
	}
	
	v_alerta("INFO", "Processando...");
	
	return true;
}


function v_pf_pj(vlr) {
	var td_pf1_row = document.getElementById("td-pf1-row");	
	var td_pf2_row = document.getElementById("td-pf2-row");
	var td_pj1_row = document.getElementById("td-pj1-row");
	var td_pj2_row = document.getElementById("td-pj2-row");	
		
	
	if(vlr == "J") {
		td_pf1_row.style.display = "none";
		td_pf2_row.style.display = "none";
		
		td_pj1_row.style.display = "block";
		td_pj2_row.style.display = "block";
		
		
	}
	else {
		td_pf1_row.style.display = "block";
		td_pf2_row.style.display = "block";
		
		td_pj1_row.style.display = "none";
		td_pj2_row.style.display = "none";
	}
	
	return false;
}

// -----------------------------------------------------------------------
// V - FORM CADASTRO

function v_cadastro_form(frm, acao) {
	var nome = frm.txt_nome;
	var email = frm.txt_email;
	var login = frm.txt_login;
	var tipopessoa = frm.slc_tipo_pessoa.value;
	var cpf = frm.txt_cpf;
	var cnpj = frm.txt_cpf;
	var senha = frm.txt_senha;
	var senhac = frm.txt_senha_c;
	
	var cpf = frm.txt_cpf;
	
	if(!v_texto(nome.value, 0, 1, 100)) {
		v_alerta("ERRO", "NOME: Preenchimento obrigatório.");
		document.location.hash = 'topo';
		//nome.focus();
		return false;
	}
	
	if(!v_email(email.value, 0)) {
		v_alerta("ERRO", "E-MAIL: Preenchimento obrigatório e/ou formato incorreto.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}else{
	    login.value = email.value;	
	}
	
	
	if(!v_texto(login.value, 0, 1, 100)) {
		v_alerta("ERRO", "LOGIN: Preenchimento obrigatório e/ou formato incorreto.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	if (tipopessoa == 'F'){
	
	    if(!v_cpf(''+ cpf.value +'', 0)) {
		    v_alerta("ERRO", "CPF: Cpf inválido.");
		    document.location.hash = 'topo';
		    //nome.focus();
		    return false;
	    }
	}else if (tipopessoa == 'J'){
	     if(!v_cnpj(''+ cnpj.value +'', 0)) {
		    v_alerta("ERRO", "CNPJ: Cnpj inválido.");
		    document.location.hash = 'topo';
		    //nome.focus();
		    return false;
	    }
	}
	

	
	
	if(!v_texto(senha.value, 0, 1, 100)) {
		v_alerta("ERRO", "SENHA: Preenchimento obrigatório e/ou formato incorreto.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	if(!v_texto(senhac.value, 0, 1, 100)) {
		v_alerta("ERRO", "CONF. SENHA: Preenchimento obrigatório e/ou formato incorreto.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	if(senha.value != senhac.value) {
		v_alerta("ERRO", "SENHA: As senhas não conferem.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	if(acao == "A") {
		if(!confirm('Confirmar alteração de dados?')) {
			return false;
		}
	}
	
	v_alerta("INFO", "Processando...");
	
	return true;
}




// -----------------------------------------------------------------------
// V - FORM RECUPERAR SENHA

function v_recupsenha_form(frm) {
	var login = frm.txt_user;
	var email = frm.txt_email;

	login.value = email.value;	
	return true;
}



// -----------------------------------------------------------------------
// V - FORM SENHA

function v_senha_form(frm, acao) {
	var senhaatual = frm.txt_senha_atual;
	var senhanova = frm.txt_senha_nova;
	var senhanovac = frm.txt_senha_nova_c;
	
	
	
	
	if(!v_texto(senhaatual.value, 0, 1, 100)) {
		v_alerta("ERRO", "SENHA: Preenchimento obrigatório e/ou formato incorreto.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	
	
	if(!v_texto(senhanova.value, 0, 1, 100)) {
		v_alerta("ERRO", "NOVA SENHA: Preenchimento obrigatório e/ou formato incorreto.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	if(senhanova.value != senhanovac.value) {
		v_alerta("ERRO", "SENHA: As novas senhas não conferem.");
		document.location.hash = 'topo';
		//email.focus();
		return false;
	}
	
	
	if(acao == "A") {
		if(!confirm('Confirmar alteração de dados?')) {
			return false;
		}
	}
	
	//v_alerta("INFO", "Processando...");
	
	return true;
}