//Sistema
//Botões dinâmicos
function OverImg(id_img,img){
	if(id_img && img){
		document.getElementById(id_img).src = '_images/'+img;
	}
}
function SetDivTxt(txt,id_div){
	document.getElementById(id_div).innerHTML = txt;
	//window.status = txt;
}
//Imagens
function VisualizaImagem(imagem){
	var imgWin = window.open('imagem_view.php?imagem='+imagem+'&','ERGO_IMAGE_VIEW','width=620,height=500,top=0,left=0');
	imgWin.focus();
}
function GetObj(id){
	return document.getElementById(id);
}
//Paginação
function Go(page,form){
	if(!form) form = 'formulario';
	GetObj('go').value = page;	
	GetObj(form).submit();
	DisableForm(form);
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//////////////////////////////////////////////////////////////////////////////////
//Serviços & propostas
function AdicionaServico(servico){
	var faixa = 0;
	if(GetObj('faixa'+servico)) faixa = GetObj('faixa'+servico).value;
	location.href = '?page=servicos/add&id='+servico+'&faixa='+faixa;
}
function RemoveServico(servico){
	if(confirm("Tem certeza de que deseja remover este serviço da sua proposta?"))
		location.href = '?page=servicos/del&id='+servico;
}
function FinalizaProposta(){
	location.href = '?page=servicos/finaliza&';
}
function AbrePopTexto(cod){
	var nw = window.open('servicos/poptexto.php?id='+cod+'&','POPTEXTOERGO','width=300,height=250,top=80,left=80,scrollbars=yes')
}
function AbrePopDescricao(cod){
	var nw = window.open('servicos/popdescricao.php?id='+cod+'&','POPDESCRICAOERGO','width=300,height=250,top=80,left=80,scrollbars=yes')
}
//////////////////////////////////////////////////////////////////////////////////
//Contato
function DisableForm(formulario){
	var act = "var count = document."+formulario+".elements.length;";
	eval(act);
	var target,obj;
	for(i=0;i<count;i++){
		target = "obj = document."+formulario+".elements[i].disabled = true;"
		eval(target);
	}
}
function EnviaContato(){
	location.href = '?page=contato/contato&flag=1';	
}
//////////////////////////////////////////////////////////////////////////////////
//Outros / geral
function LimpaCampos(campo,condicao){
	var target = GetObj(campo);
	if(target.value==condicao)
		target.value = '';
}
function RestauraCampos(campo,valor){
	var target = GetObj(campo);
	if(target.value=='')
		target.value = valor;
}
function Cadastro(theForm){
	document.formCadastro.submit();
}
//////////////////////////////////////////////////////////////////////////////////
//Clientes
//Geral
function AbrePopHelp(cod){
	var nw = window.open('clientes/pophelp.php?id='+cod+'&','POPHELPERGO','width=300,height=250,top=80,left=80,scrollbars=yes')
}
function Login(formulario){
	GetObj(formulario).submit();
}
//Pop
function ExibePopForm(form){	 
	window.open("clientes/exibe_ppp.php?form="+form+"&",form,'width=664,height=502,top=50,left=50,resizable=no,scrollbars=yes');	
}
//Demissão
function Demite(id_funcionario){
	if (confirm('Você confirma a demissão desse empregado?')){
		location.href = '?page=clientes/clientes&action=info_funcionarios&act=demissao&id_funcionario='+id_funcionario+'';
		GetObj('form_demi').submit();
	}
	
}
//Formulário
function ValidaCampos(formulario){	

	var docdate = new Date();
	var year = docdate.getYear();

	if (GetObj('nome_resp_ppp').value.length < 3){
		alert ("Por favor, informe nome do responsável pelo PPP, com no máximo 40 caracteres.");	
		GetObj('nome_resp_ppp').focus();
		return false;
	}
	else if (GetObj('nome_resp_ppp').value.length > 40){
		alert ("Por favor, informe nome do responsável pelo PPP, com no máximo 40 caracteres.");
		GetObj('nome_resp_ppp').focus();
		return false;
	}
	else if (GetObj('pis').value.length < 11){
	alert ("Por favor, informe o número do PIS, com no mínimo 11 caracteres.");
	GetObj('pis').focus();
	return false;
	}
	else if (GetObj('nome').value.length < 3){
	alert ("Por favor, informe nome do empregado.");
	GetObj('nome').focus();
	return false;
	} 
	else if (GetObj('nome').value.length > 40){
	alert ("Por favor, informe nome do empregado, com no máximo 40 caracteres.");
	GetObj('nome').focus();
	return false;
	}
	else if (GetObj('empresa').value.length == ''){
	alert ("Por favor, selecione a empresa do empregado.");
	GetObj('empresa').focus();
	return false;
	} 
	else if (GetObj('sexo').value == 0){
	alert ("Por favor, selecione o sexo do empregado.");
	GetObj('sexo').focus();
	return false;
	}
	else if (GetObj('cargo').value.length == ''){
	alert ("Por favor, informe o cargo do empregado.");
	GetObj('cargo').focus();
	return false;
	}
	else if (GetObj('cbo').value.length != 6){
	alert ("Por favor, informe a Classificação Brasileira de Ocupação,\n código de 6 digitos.");
	GetObj('cbo').focus();
	return false;
	}
	else if (GetObj('setor').value.length == ''){
	alert ("Por favor, informe setor conforme descrito no LTCAT.");
	GetObj('setor').focus();
	return false;
	}
	else if (GetObj('br_pdh').value.length == ''){
	alert ("Por favor, selecione BR para Beneficiário Reabilitado, PDH para Portador de Deficiência Habilitado,\n ou NA para Não aplicável.");
	GetObj('br_pdh').focus();
	return false;
	}
	else if (GetObj('ano_nasc').value == year){
	alert ("Por favor, informe a data de nascimento do empregado.");
	GetObj('dia_nasc').focus();
	return false;
	}	
	else if (GetObj('pis_func').value.length < 11){
	alert ("Por favor, informe o Nº do PIS/PASEP com 11 dígitos.");
	GetObj('pis_func').focus();
	return false;
	}
	else if (GetObj('ctps').value.length == ''){
	alert ("Por favor, informe o número da Carteira de Trabalho com Nº (7 dig.), \nSérie (5 dig.)e UF(2 caracteres)");
	GetObj('ctps').focus();
	return false;
	}
	else if (GetObj('horario_trab').value.length == ''){
	alert ("Por favor, informe o horário de trabalho no seguinte formato:n\ Ex.: 24 x 72 horas ou 14 x 211 dias");
	GetObj('horario_trab').focus();
	return false;
	}
	else if (GetObj('ano_adm').value == year){
	alert ("Por favor, informe a data de admissão do empregado.");
	GetObj('dia_adm').focus();
	return false;
	}
	else if (GetObj('cat_ativo1').checked == true){
			if (GetObj('ano_cat').value == year){
				alert("Por favor, informe a data de abertura da CAT");
				GetObj('dia_cat').focus();
				return false;
			}
			else if (GetObj('numero_cat').value.length == ''){
				alert("Por favor, informe o número da CAT.");
				GetObj('numero_cat').focus();
				return false;
			}
			else 
				return true;
	}
	else 
		return true;
	
	return true;
}

function Desabilita(){	
		GetObj('dia_cat').disabled = 'true';
		GetObj('mes_cat').disabled = 'true';
		GetObj('ano_cat').disabled = 'true';
		GetObj('numero_cat').disabled = 'true';		
}
function Habilita(){	
		GetObj('dia_cat').disabled = '';
		GetObj('mes_cat').disabled = '';
		GetObj('ano_cat').disabled = '';
		GetObj('numero_cat').disabled = '';		
}
//////////////////////////////////////////////////////////////////////////////////
//Enquete
function VotarEnquete(){
	if(GetObj('id').value.length > 0){
		var nome_form = 'enquete';
		var action_form = "internas.php?page=enquetes/resultados&";
		var resposta = num_respostas = GetObj(nome_form).opcao.length;
		for(i=0;i<num_respostas;i++){
			if(GetObj(nome_form).opcao[i].checked){
				action_form = action_form + "&opcao=" + GetObj(nome_form).opcao[i].value;
				action_form = action_form + "&id=" + GetObj('id').value;
				resposta  = i;
			}
		}
		if(resposta==num_respostas){
			alert("Você deve escolher uma opção!");
		}
		else{
			GetObj(nome_form).action = action_form;
			GetObj(nome_form).submit();
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////
// Valida o campo de E-mail

function chechEmail(email) {
	
	if (email.value == ""){
		alert("Por favor, digite seu e-mail.");
		email.focus();
		return (false);
	}	
	
	var checkStr = email.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailAtN = 0;
	var EmailPeriod = false;
	
	for (i = 0;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		
		if ( ch == "@" )	
			if ( i > 0 ){	// O @ não pode ser o primeiro caracter digitado
				EmailAt  = true;
				EmailAtN = i;
			}

		if ( ch == "." )	
			if ( EmailAt )	// Deve existir um "." após o "@"
				if ( i > (EmailAtN + 1) )	// Deve haver algum caracter entre o "@" e o "."
					if ( i < (checkStr.length - 2) ) 	// Devem haver pelo menos 2 caracteres após o "."
						EmailPeriod = true;

		if (EmailAt && EmailPeriod){
			EmailValid = true
			break;
		}
	}
	
	if (!EmailValid){
		alert("O e-mail digitado não é válido.");
		email.focus();
		return false;
	}
	
	return true;
}

