/******************************************************************
	TRABALHE CONOSCO
******************************************************************/

function validaTrabalhe()
{
	with (document.formulario)
	{
		if (isEmpty(nome))
		{
			abreAlerta("Preencha seu nome.");
			nome.focus();
			return;
		}
		
		if (isEmpty(email))
		{
			abreAlerta("Preencha seu e-mail.");
			email.focus();
			return;
		}
		
		if (!isEmail(email))
		{
			abreAlerta("E-mail digitado é inválido.");
			email.focus();
			return;
		}
		
		if (isEmpty(telefone))
		{
			abreAlerta("Preencha um telefone de contato.");
			telefone.focus();
			return;
		}
		
		if (isEmpty(estado))
		{
			abreAlerta("Selecione o estado onde reside.");
			estado.focus();
			return;
		}
		
		if (isEmpty(cidade))
		{
			abreAlerta("Selecione a cidade onde reside.");
			cidade.focus();
			return;
		}
		
		if (isEmpty(setor))
		{
			abreAlerta("Selecione o setor desejado.");
			setor.focus();
			return;
		}
		
		if (isEmpty(arquivo))
		{
			abreAlerta("Selecione seu currículo.");
			arquivo.focus();
			return;
		}
		
		if (!testFileType(arquivo.value, ['doc', 'docx', 'rtf', 'pdf', 'odt']))
		{
			abreAlerta("A extensão do arquivo selecionado é inválida.");
			arquivo.focus();
			return;								  
		}
				
		submit();
	}	
}



/******************************************************************
	VALIDA USUARIO
******************************************************************/

function validaCadastro()
{
	with (document.formulario)
	{
		if (isEmpty(nome) || nome.value == "Nome:")
		{
			abreAlerta("Preencha seu nome.");
			nome.focus();
			return;
		}
		
		if (isEmpty(email) || email.value == "E-mail:")
		{
			abreAlerta("Preencha seu e-mail.");
			email.focus();
			return;
		}
		
		if (!isEmail(email))
		{
			abreAlerta("E-mail digitado é inválido.");
			email.focus();
			return;
		}
		
		var dados = $("#formulario").serialize();
		
		$.post("acoes.php?acao=cadastro", dados, function(msg){
	
			if (msg == "ok")
			{
				abreAlerta('Seus dados foram enviados com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
				clearForm("#formulario");
			}
			
			if (msg == "codigo_invalido")
			{
				abreAlerta('Código digitado não confere com a imagem.', null);
			}

			if (msg == "erro")
			{
				abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
				clearForm("#formulario");
			}
			
		});
	}	
}


/******************************************************************
	CONTATO
******************************************************************/

function validaContato()
{
	with (document.formularioc)
	{
		if (isEmpty(nome))
		{
			abreAlerta("Preencha seu nome.", 1000);
			nome.focus();
			return;
		}
		
		if (isEmpty(cidade))
		{
			abreAlerta("Selecione a cidade onde reside.", 1000);
			cidade.focus();
			return;
		}
		
		if (isEmpty(telefone))
		{
			abreAlerta("Preencha um telefone de contato.", 1000);
			telefone.focus();
			return;
		}
		
		if (isEmpty(email))
		{
			abreAlerta("Preencha seu e-mail.", 1000);
			email.focus();
			return;
		}
		
		if (!isEmail(email))
		{
			abreAlerta("E-mail digitado é inválido.", 1000);
			email.focus();
			return;
		}
		
		if (isEmpty(mensagem))
		{
			abreAlerta("Preencha a mensagem.", 1000);
			mensagem.focus();
			return;
		}
				
		//Capta os dados do formulário
		var dados = $("#formularioc").serialize();
		
		$.post("acoes.php?acao=contato", dados, function(msg){
	
			if (msg == "ok")
			{
				abreAlerta('Contato enviado com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
				clearForm("#formularioc");
			}
			
			if (msg == "codigo_invalido")
			{
				abreAlerta('Código digitado não confere com a imagem.', null);
			}

			if (msg == "erro")
			{
				abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
				clearForm("#formularioc");
			}
			
		});
	}	
}


/******************************************************************
	ASSOCIASSE PART1
******************************************************************/

function validaA1()
{
	with (document.formularioa)
	{
		if (isEmpty(nome) || !isNotEqual(nome, 'Nome:'))
		{
			abreAlerta('Preencha o Nome Fantasia.', 1000);
			nome.focus();
			return;
		}

		if (isEmpty(razao) || !isNotEqual(razao, 'Nome:'))
		{
			abreAlerta('Preencha a Razão Social.', 1000);
			razao.focus();
			return;
		}
		
		if (isEmpty(cnpj))
		{
			abreAlerta("Preencha o CNPJ.", 1000);
			cnpj.focus();
			return;
		}
		
		if (isEmpty(ins_estadual))
		{
			abreAlerta("Preencha a Inscrição Estadual.", 1000);
			ins_estadual.focus();
			return;
		}
		
		if (isEmpty(endereco))
		{
			abreAlerta("Preencha o campo Endereço.", 1000);
			endereco.focus();
			return;
		}
		
		if (isEmpty(numero))
		{
			abreAlerta("Preencha o campo Nº.", 1000);
			numero.focus();
			return;
		}
		
		if (isEmpty(bairro))
		{
			abreAlerta("Preencha o campo Bairro.", 1000);
			bairro.focus();
			return;
		}
		
		if (isEmpty(cidade))
		{
			abreAlerta("Preencha o campo Cidade.", 1000);
			cidade.focus();
			return;
		}
		
		if (isEmpty(estado))
		{
			abreAlerta("Preencha o campo Estado.", 1000);
			estado.focus();
			return;
		}
		
		if (isEmpty(tel_comercial))
		{
			abreAlerta("Preencha o campo Telefone Comercial.", 1000);
			tel_comercial.focus();
			return;
		}
		
		if (isEmpty(tel_residencial))
		{
			abreAlerta("Preencha o campo Telefone Residencial.", 1000);
			tel_residencial.focus();
			return;
		}
		
		if (isEmpty(area))
		{
			abreAlerta("Preencha a Área de Venda (m²).", 1000);
			area.focus();
			return;
		}
		
		
		if (isEmpty(lojas))
		{
			abreAlerta("Preencha o Número de Lojas.", 1000);
			lojas.focus();
			return;
		}
		
		if (isEmpty(checkout))
		{
			abreAlerta("Preencha o Nº Checkout.", 1000);
			checkout.focus();
			return;
		}

		if (isEmpty(fatura))
		{
			abreAlerta("Preencha a Fatura Mensal(R$).", 1000);
			fatura.focus();
			return;
		}

		if (isEmpty(empregados))
		{
			abreAlerta("Preencha o Nº de Empregados.", 1000);
			empregados.focus();
			return;
		}
		
		
		document.getElementById('fr1').style.display = 'none';
		document.getElementById('fr2').style.display = 'block';
		
		
/*//Capta os dados do formulário
var dados = $("#formularioa").serialize();

$.post("acoes.php?acao=associesse", dados, function(msg){

if (msg == "ok")
{
abreAlerta('Solicitação enviada com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
clearForm("#formularioa");
}

if (msg == "erro")
{
abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
clearForm("#formularioa");
}

});		*/
	
	
	}	
}




/******************************************************************
	ASSOCIASSE PART2
******************************************************************/

function validaA2()
{
	with (document.formularioa)
	{
		if (isEmpty(socio_titular) || !isNotEqual(socio_titular, 'Nome:'))
		{
			abreAlerta('Você é Socio Titular da empresa.', 1000);
			socio_titular.focus();
			return;
		}

		if (isEmpty(rg))
		{
			abreAlerta('Preencha o campo RG.', 1000);
			rg.focus();
			return;
		}
		
		if (isEmpty(cpf))
		{
			abreAlerta("Preencha o campo CPF.", 1000);
			cpf.focus();
			return;
		}
		
		if (isEmpty(profissao))
		{
			abreAlerta("Preencha o Campo Profissão.", 1000);
			profissao.focus();
			return;
		}
		
		if (isEmpty(estado_civil))
		{
			abreAlerta("Preencha o campo Estado Civil.", 1000);
			estado_civil.focus();
			return;
		}
		
		if (isEmpty(nacionalidade))
		{
			abreAlerta("Preencha o campo Nacionalidade.", 1000);
			nacionalidade.focus();
			return;
		}
		
		if (isEmpty(data_nascimento))
		{
			abreAlerta("Digite sua data de nascimento.", 1000);
			data_nascimento.focus();
			return;
		}
		
		if (isEmpty(local_nascimento))
		{
			abreAlerta("Preencha o campo Local de Nascimento.", 1000);
			local_nascimento.focus();
			return;
		}
		
		if (isEmpty(endereco_socio))
		{
			abreAlerta("Preencha seu Endereço.", 1000);
			endereco_socio.focus();
			return;
		}
		
		if (isEmpty(num_socio))
		{
			abreAlerta("Preencha o campo Nº.", 1000);
			num_socio.focus();
			return;
		}
		
		if (isEmpty(cidade_socio))
		{
			abreAlerta("Preencha o campo Cidade.", 1000);
			cidade_socio.focus();
			return;
		}
		
		if (isEmpty(estado_socio))
		{
			abreAlerta("Preencha o campo Estado.", 1000);
			estado_socio.focus();
			return;
		}
		
		
		document.getElementById('fr2').style.display = 'none';
		document.getElementById('fr3').style.display = 'block';
		
		
/*//Capta os dados do formulário
var dados = $("#formularioa").serialize();

$.post("acoes.php?acao=associesse", dados, function(msg){

if (msg == "ok")
{
abreAlerta('Solicitação enviada com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
clearForm("#formularioa");
}

if (msg == "erro")
{
abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
clearForm("#formularioa");
}

});		*/
	
	
	}	
}




/******************************************************************
	ASSOCIASSE PART3
******************************************************************/

function validaA3()
{
	
		document.getElementById('fr3').style.display = 'none';
		document.getElementById('fr4').style.display = 'block';
		
		
/*//Capta os dados do formulário
var dados = $("#formularioa").serialize();

$.post("acoes.php?acao=associesse", dados, function(msg){

if (msg == "ok")
{
abreAlerta('Solicitação enviada com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
clearForm("#formularioa");
}

if (msg == "erro")
{
abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
clearForm("#formularioa");
}

});		*/
}




/******************************************************************
	ASSOCIASSE PART4
******************************************************************/

function validaA4()
{
	with (document.formularioa)
	{
		if (participa[0].checked == false && participa[1].checked == false)
		{
			abreAlerta('Sua empresa participa de alguma Associação?.', 1000);
			participa.focus();
			return;
		}

		if (participa[0].checked)
		{
			if(isEmpty(qual)){
				abreAlerta('Qual associação você participa.', 1000);
				qual.focus();
				return;
			}
		}
		
		if (computador[0].checked == false && computador[1].checked == false)
		{
			abreAlerta('Sua empresa possui computador.', 1000);
			computador.focus();
			return;
		}

		if (computador[0].checked)
		{
			if(isEmpty(quantos)){
			abreAlerta('Quantos computadores possui sua empresa?.', 1000);
			quantos.focus();
			return;
			}
		}
		
		if (isEmpty(email))
		{
			abreAlerta("Preencha o campo E-mail.", 1000);
			email.focus();
			return;
		}
		
		if (!isEmail(email))
		{
			abreAlerta('E-mail digitado é inválido.', 1000);
			email.focus();
			return;
		}
		
		//Capta os dados do formulário
		var dados = $("#formularioa").serialize();
		
		$.post("acoes.php?acao=associesse", dados, function(msg){
		
		if (msg == "ok")
		{
		abreAlerta('Solicitação enviada com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
		clearForm("#formularioa");
		document.getElementById('fr4').style.display = 'none';
		document.getElementById('fr1').style.display = 'block';
		}
		
		if (msg == "erro")
		{
		abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
		clearForm("#formularioa");
		}
		
		});	
		
		
	}	
}


/******************************************************************
	VOLTA ASSOCIASSE 2
******************************************************************/

function volta2()
{
	
		document.getElementById('fr2').style.display = 'none';
		document.getElementById('fr1').style.display = 'block';
}


/******************************************************************
	VOLTA ASSOCIASSE 3
******************************************************************/

function volta3()
{
	
		document.getElementById('fr3').style.display = 'none';
		document.getElementById('fr2').style.display = 'block';
}

/******************************************************************
	VOLTA ASSOCIASSE 4
******************************************************************/

function volta4()
{
	
		document.getElementById('fr4').style.display = 'none';
		document.getElementById('fr3').style.display = 'block';
}

/******************************************************************
	FORNECEDOR
******************************************************************/

function validaFornecedor()
{
	with (document.formulariof)
	{
		if (isEmpty(nome) || !isNotEqual(nome, 'Nome:'))
		{
			abreAlerta('Preencha seu nome.', 1000);
			nome.focus();
			return;
		}
		
		if (isEmpty(empresa))
		{
			abreAlerta("Preencha o nome da empresa.", 1000);
			empresa.focus();
			return;
		}
		
		if (isEmpty(cnpj))
		{
			abreAlerta("Digite o CNPJ de sua empresa.", 1000);
			cnpj.focus();
			return;
		}
		
		if (isEmpty(cidade))
		{
			abreAlerta("Preencha o campo cidade.", 1000);
			cidade.focus();
			return;
		}
		
		if (isEmpty(endereco))
		{
			abreAlerta("Preencha o endereço de sua empresa.", 1000);
			endereco.focus();
			return;
		}
		
		if (isEmpty(telefone))
		{
			abreAlerta("Preencha o campo telefone.", 1000);
			telefone.focus();
			return;
		}
		
		if (isEmpty(email) || !isNotEqual(email, 'E-mail:'))
		{
			abreAlerta('Preencha seu e-mail.', 1000);
			email.focus();
			return;
		}
		
		if (!isEmail(email))
		{
			abreAlerta('E-mail digitado é inválido.', 1000);
			email.focus();
			return;
		}
		
		if (isEmpty(texto))
		{
			abreAlerta("Descreva o que você fornece.", 1000);
			mensagem.focus();
			return;
		}
		
		//Capta os dados do formulário
		var dados = $("#formulariof").serialize();
		
		$.post("acoes.php?acao=fornecedor", dados, function(msg){
	
			if (msg == "ok")
			{
				abreAlerta('Solicitação enviada com sucesso.<br><br>Obrigado<br>Equipe Redefort.', null);
				clearForm("#formulariof");
			}
			
			if (msg == "erro")
			{
				abreAlerta('Ocorreu um erro ao enviar seu contato, tente novamete.', null);
				clearForm("#formulariof");
			}
			
		});		
	}	
}



/******************************************************************
	LOGIN
******************************************************************/

function validaLogin()
{
	with (document.formulario)
	{
		if (isEmpty(usuario))
		{
			abreAlerta('Preencha seu login.', 1000);
			usuario.focus();
			return;
		}
		
		if (isEmpty(senha))
		{
			abreAlerta('Digite sua senha.', 1000);
			senha.focus();
			return;
		}
		
		//Capta os dados do formulário
		var dados = $("#formulario").serialize();
		
		$.post("acesso.php", dados, function(msg){
	
			if (msg == "erro_senha")
			{
				abreAlerta('Sua senha é inválida.', null);
			}
			
			if (msg == "erro")
			{
				abreAlerta('O usuário digitado é inválido.', null);
			}
			
			if (msg == "ok")
			{
				location.href = 'index.php?pg=arearestrita';
			}
						
		});
	}	
}


/******************************************************************
	ALERTA
******************************************************************/

function abreAlerta(msg, tempo)
{
	tm = (tempo==null || tempo=="") ? 2000 : tempo;
	
	$.blockUI({ 
		message: '<h2>'+msg+'</h2>',
		fadeIn: 400, 
        fadeOut: 200,
		timeout: 2000
	}); 	
}


/******************************************************************
	FOCUS E BLUR
******************************************************************/

function onFocus(campo, valor)
{
	if (campo.value == valor)
	{
		campo.value = "";
	}
}

function onBlur(campo, valor)
{
	if (campo.value == "")
	{
		campo.value = valor;
	}
}


/******************************************************************
	VALIDAÇÕES PADRÃO
******************************************************************/


function clearForm(form) 
{
	// iterate over all of the inputs for the form
	// element that was passed in
	$(':input', form).each(function() {
		
		var type = this.type;
		var tag = this.tagName.toLowerCase(); // normalize case
		
		// it's ok to reset the value attr of text inputs,
		// password inputs, and textareas
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = "";
		
		// checkboxes and radios need to have their checked state cleared
		// but should *not* have their 'value' changed
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		
		// select elements need to have their 'selectedIndex' property set to -1
		// (this works for both single and multiple select elements)
		else if (tag == 'select')
			this.selectedIndex = -1;
		
	});
}

function isEmpty(str)
{
	if (str.value == "" || str.value.length == 0)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isDefValue(str, def)
{
	if (str.value == def)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isInvalidFormat(str)
{
	var er = new RegExp(/[@!#$%&*+=?|-]/);
	
	if (typeof(str.value) == "string")
	{
        if (er.test(str.value))
		{ 
			return true; 
		}
    }
	else if (typeof(str) == "object")
	{
        if (er.test(str))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

function isEqual(str1, str2)
{
	if (str2.value == str1.value)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isNotEqual(str, val)
{
	if (str.value != val)
	{
		return true;	
	}
	else
	{
		return false;
	}
}

function isDate(str) 
{
	var dia = str.value.substr(0,2);
	var mes = str.value.substr(3,2);
	var ano = str.value.substr(6,4);
	
	var data = new Date();
	var intDia;
	var intMes;
	var intAno;

	data.setFullYear(ano, mes-1, dia);

	intDia = (data.getDate() == dia);
	intMes = (data.getMonth() == mes-1);
	intAno = (data.getFullYear() == ano);

	if (intDia && intMes && intAno)
	{
		return true;
	}
	else
	{
		return false;	
	}
}

function isEmail(str)
{
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
   
    if (typeof(str.value) == "string")
	{
        if (er.test(str.value))
		{ 
			return true; 
		}
    }
	else if (typeof(str) == "object")
	{
        if (er.test(str))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

function isImage(str)
{
    var er = new RegExp(/[\w]+\.(gif|jpg|bmp|png|jpeg)$/gi);
   
    if (typeof(str.value) == "string")
	{
        if (er.test(str.value))
		{ 
			return true; 
		}
    }
	else if (typeof(str) == "object")
	{
        if (er.test(str))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

function isDDD(str)
{
	var ddd = str.value.substr(1,2);
	
	if (ddd == "00" || str.length == 0 || str == "")
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function isPhoneNumber(str)
{
	var fone   = str.value.substr(5,9);
	var digito = str.value.substr(5,1);
	var numero = fone.replace(".", "");	
	
	if (numero == "00000000" || numero == "11111111" || numero == "22222222" || numero == "33333333" || numero == "44444444" || numero == "55555555" || numero == "66666666" || numero == "77777777" || numero == "88888888" || numero == "99999999" || str.value.length < 14 || str.value == "" || digito == 0 || digito == 8 || digito == 9)
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function isCelularNumber(str)
{
	var fone   = str.value.substr(5,9);
	var digito = str.value.substr(5,1);
	var numero = fone.replace(".", "");
	
	if (numero == "00000000" || numero == "11111111" || numero == "22222222" || numero == "33333333" || numero == "44444444" || numero == "55555555" || numero == "66666666" || numero == "77777777" || numero == "88888888" || numero == "99999999" || str.value.length < 14 || str.value == "" || digito < 8)
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function isInteger(str)
{
	var er = new RegExp(/^[0-9]+$/);
	
	if (er.test(str.value)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function testFileType(fileName, fileTypes) //['gif', 'jpg', 'png', 'jpeg']
{
	if (!fileName) return;
	
	dots = fileName.split(".");
	
	//get the part AFTER the LAST period.
	fileType = "." + dots[dots.length-1];
	
	return (fileTypes.join(".").indexOf(fileType) != -1) ? true : false;
}

function calculaIdade(d, m, y)
{
	var dia = parseInt(d);
	var mes = parseInt(m)-1;
	var ano = parseInt(y);
	
	var age;
	var now = new Date();
	
	diaAtual = now.getDate();
	mesAtual = now.getMonth();
	anoAtual = now.getFullYear();
	
	if ((mesAtual > mes)||(mesAtual == mes & diaAtual >= dia))
	{
		age = ano;
	}
	else
	{
		age = ano+1;
	}
	
	return anoAtual-age;
}
