function get(id)
{
	return document.getElementById(id);
}

function muda()
{
	if(get('adicionar').style.display == 'none')
	{
		get('fotos').style.display = 'none';
		get('adicionar').style.display = 'block';
		get('mudar').value = 'Fotos Cadastradas';
	}
	else
	{
		get('fotos').style.display = 'block';
		get('adicionar').style.display = 'none';
		get('mudar').value = 'Adicionar Fotos';
	}
}

function addFav(){
    var url      = "http://www.sinttromar.org.br";
    var title    = "Sinttromar";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

function removeMask(value)
{
	var valueLimpo='';
	var er = /[0-9]{1,1}/;	
	for(i=0; i < value.length; i++)
	{
		tmp = value.substring(i, i+1);
		//if(!isNaN(tmp))
		if(tmp.match(er))
		{
			valueLimpo+=tmp;
		}
	}
	return valueLimpo;
}

function empty(obj)
{ 
	var type = typeof obj;
	var er = /^\s{0,}$/;
	var retorno = false;

	if(type=="string")
	{
		if(obj.match(er))
			return true;
	}
	if(type!="undefined")
	{
		if(type == "object")
		{
			retorno  = obj.value.match(er);
		}
		else if(get(obj))
		{
			retorno = get(obj).value.match(er);
		}
	}	
	
	return retorno;
}

function validaEMAIL(email)
{
//	var er = /^[a-z0-9-_.]+@+[a-z0-9-_.]+\.[a-z]{2,4}((\.)[a-z]{2})?$/;
	var er = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	
	return email.match(er);
}

function validaCPF(cpf) 
{
	var cpf = new String(cpf);
    var aux_cpf = "";
    var erro = true;

	// retirar caracteres não numéricos
	aux_cpf = removeMask(cpf);

	if(aux_cpf.length!=11)
	{
		return false;
	}
    else 
    {
    	var sub = aux_cpf.substr(0,1);
    
    	if(aux_cpf.match(sub+'{11}'))
    		return false;
    		
    	var cpf1 = String(aux_cpf);
    	var cpf2 = cpf.substr(cpf.length-2,2);
      	var controle = "";
      	var start = 2;
      	var end = 10;

      	for(var i=1;i<=2;i++) 
      	{
      		var soma = 0;
      		
      		for(j=start;j<=end;j++)
      		{
      			soma += cpf1.substr((j-i-1),1)*(end+1+i-j);
      		}
        	if(i==2)
        	{
          		soma += digito * 2;
        	}
        	
        	digito = (soma * 10) % 11;
        	
        	if(digito==10)
        	{
          		digito = 0;
        	}
        	
        	controle += digito;
        	start = 3;
        	end = 11;
      	}
      	if(controle!=cpf2)
      	{
        	erro = false;
      	}
    }
  
	return erro;
}

function validaCNPJ(cnpj) 
{
	
	var erro = true; 
	var aux_cnpj = "";	
	var cnpj1=0 , cnpj2=0;
	var fator, controle;

	// retirar caracteres não numéricos

	aux_cnpj = removeMask(cnpj);
	
	if(aux_cnpj.length!=14)
	{
		return false;
	}
	else 
	{
		var sub = aux_cnpj.substr(0,1);
		
		if(aux_cnpj.match(sub+'{14}'))
			return false;
		
		cnpj1 = aux_cnpj.substr(0,12);
		cnpj2 = aux_cnpj.substr(aux_cnpj.length-2,2);
		fator = "543298765432";
		controle = "";
		
		for(j=0; j<2; j++) 
		{
			soma = 0;
			for(i=0; i<12; i++)
			{
				soma += cnpj1.substr(i,1) * fator.substr(i,1);
			}
			if(j==1)
			{
				soma += digito * 2;
			}
			
			digito = (soma * 10) % 11;
			
			if(digito==10)
			{
				digito = 0;
			}
			
			controle += digito;
			
			fator = "654329876543";
		} 
		if(controle != cnpj2)
		{
			erro = false;
		}
	} 
	return erro;
}

function validaCadastro()
{
	var msg = '';
	if (empty(get('Empresa')))
		msg += 'O campo "Empresa" é obrigatório.\n';
	if (!validaCNPJ(get('CNPJ').value))
		msg += 'O CNPJ fornecido não é válido.\n';
	if (empty(get('Endereco')))
		msg += 'O campo "Endereço" é obrigatório.\n';
	if (empty(get('Numero')))
		msg += 'O campo "Número" é obrigatório.\n';
	if (empty(get('Bairro')))
		msg += 'O campo "Bairro" é obrigatório.\n';
	if (empty(get('Cidade/UF')))
		msg += 'O campo "Cidade/UF" é obrigatório.\n';
	if (empty(get('CEP')))
		msg += 'O campo "Endereço" é obrigatório.\n';
	if (empty(get('RamoAtiv')))
		msg += 'O campo "Ramo Atividade" é obrigatório.\n';
	if (empty(get('DDD1')))
		msg += 'O campo "DDD" é obrigatório.\n';
	if (empty(get('Telefone1')))
		msg += 'O campo "Telefone" é obrigatório.\n';
	if (empty(get('Contato')))
		msg += 'O campo "Contato" é obrigatório.\n';
	if (empty(get('Escritorio')))
		msg += 'O campo "Escritório" é obrigatório.\n';
	if (!validaCNPJ(get('CNPJ2').value))
		msg += 'O CNPJ fornecido não é válido.\n';
	if (empty(get('Responsavel')))
		msg += 'O campo "Responsável" é obrigatório.\n';
	if (empty(get('DDD2')))
		msg += 'O campo "DDD" é obrigatório.\n';
	if (empty(get('Telefone2')))
		msg += 'O campo "Telefone" é obrigatório.\n';	

	if (!msg)
		return true;
	else{
		alert('Os seguintes erros foram encontrados: \n\n'+msg);
		return false;
	}
}

function validaAssocie()
{
	var msg = '';
	if (empty(get('Nome')))
		msg += 'O campo "Nome" é obrigatório.\n';
	if (empty(get('Nascimento')))
		msg += 'O campo "Nascimento" é obrigatório.\n';
	if (empty(get('EstadoCivil')))
		msg += 'O campo "Estado Civil" é obrigatório.\n';
	if (empty(get('Naturalidade')))
		msg += 'O campo "Naturalidade" é obrigatório.\n';
	if (empty(get('Sexo')))
		msg += 'O campo "Sexo" é obrigatório.\n';
	if (empty(get('DDD1')))
		msg += 'O campo "DDD" é obrigatório.\n';
	if (empty(get('Telefone1')))
		msg += 'O campo "Telefone" é obrigatório.\n';
	if (empty(get('NomeDoPai')))
		msg += 'O campo "Nome do Pai" é obrigatório.\n';
	if (empty(get('NomeDaMae')))
		msg += 'O campo "Nome da Mãe" é obrigatório.\n';
	if (!validaCPF(get('CPF').value))
		msg += 'O CPF fornecido não é válido.\n';
	if (empty(get('RG')))
		msg += 'O campo "Identidade/RG" é obrigatório.\n';
	if (empty(get('TituloEleitor')))
		msg += 'O campo "Título de Eleitor" é obrigatório.\n';
	if (empty(get('Zona')))
		msg += 'O campo "Zona" é obrigatório.\n';
	if (empty(get('Secao')))
		msg += 'O campo "Seção" é obrigatório.\n';
	if (empty(get('CarteiraHab')))
		msg += 'O campo "Carteira Hab" é obrigatório.\n';
	if (empty(get('CarteiraProf')))
		msg += 'O campo "Carteira Prof." é obrigatório.\n';
	if (empty(get('Serie')))
		msg += 'O campo "Série" é obrigatório.\n';
	if (empty(get('PIS')))
		msg += 'O campo "PIS" é obrigatório.\n';
	if (empty(get('Endereco')))
		msg += 'O campo "Endereço" é obrigatório.\n';
	if (empty(get('Numero')))
		msg += 'O campo "Número" é obrigatório.\n';
	if (empty(get('Bairro')))
		msg += 'O campo "Bairro" é obrigatório.\n';
	if (empty(get('Cidade/UF')))
		msg += 'O campo "Cidade/UF" é obrigatório.\n';
	if (empty(get('CEP')))
		msg += 'O campo "CEP" é obrigatório.\n';
	if (empty(get('Empresa')))
		msg += 'O campo "Empresa" é obrigatório.\n';
	if (empty(get('Admissao')))
		msg += 'O campo "Admissão" é obrigatório.\n';
	if (empty(get('Categoria')))
		msg += 'O campo "Categoria" é obrigatório.\n';
	if (empty(get('DDD3')))
		msg += 'O campo "DDD" é obrigatório.\n';	
	if (empty(get('Telefone3')))
		msg += 'O campo "Telefone" é obrigatório.\n';
	if (empty(get('LocalTrabalho')))
		msg += 'O campo "Local de Trabalho" é obrigatório.\n';	
	if (empty(get('Funcao')))
		msg += 'O campo "Função" é obrigatório.\n';			

	if (!msg)
		return true;
	else{
		alert('Os seguintes erros foram encontrados: \n\n'+msg);
		return false;
	}
}

function validaFaleConosco()
{
	var msg = '';
	if (empty(get('Nome')))
		msg += 'O campo "Nome" é obrigatório.\n';
	if (empty(get('Cidade')))
		msg += 'O campo "Cidade" é obrigatório.\n';
	if (empty(get('Assunto')))
		msg += 'O campo "Assunto" é obrigatório.\n';
	if (empty(get('Mensagem')))
		msg += 'O campo "Mensagem" é obrigatório.\n';
	if (empty(get('Telefone1')))
		msg += 'O campo "Telefone" é obrigatório.\n';
	if (!validaEMAIL(get('Email1').value))
		msg += 'O Email fornecido não é válido.\n';


	if (!msg)
		return true;
	else{
		alert('Os seguintes erros foram encontrados: \n\n'+msg);
		return false;
	}
}
