function validaCNPJ(input) {
	if (!validarCNPJ(input.value)) {
		alert("CNPJ inválido!");
		return false;
	}
	return true;
}

function validarCNPJ(cnpj){
	var ret = false;
	// 12.345.678/9012-34 => 12.456.890/2345-78
	cnpj = cnpj.replace(".","").replace(".", "");
	cnpj = cnpj.replace("-","");
	cnpj = cnpj.replace("/","");

	if (cnpj.length != 14) {
		return false;
	}
	else if (cnpj == "00000000000000") {
		return false;
	}
	else {
		var soma = 0;
		var d1;
		var d2;
		var resto;
		for (var i = 0; i < 4; i++) {
			soma += parseInt(cnpj.charAt(i)) * (5 - i);
		}
		for (var i = 4; i < 12; i++) {
			soma += parseInt(cnpj.charAt(i)) * (13 - i);
		}
		resto = soma % 11;
		if (resto == 0 || resto == 1) {
			d1 = 0;
		}
		else {
			d1 = 11 - resto;
		}

		if (d1 != parseInt(cnpj.charAt(12))) {
			return false;
		}
		else {
			soma = 0;
			for (var i = 0; i < 5; i++) {
				soma += parseInt(cnpj.charAt(i)) * (6 - i);
			}
			for (var i = 5; i < 13; i++) {
				soma += parseInt(cnpj.charAt(i)) * (14 - i);
			}
			resto = soma % 11;
			if (resto == 0 || resto == 1) {
				d2 = 0;
			}
			else {
				d2 = 11 - resto;
			}

			return (d2 == parseInt(cnpj.charAt(13)));
		}
	}
	return false;
}

function validaCPF(input) {
	if (!validarCPF(input.value)) {
		alert("CPF inválido!");
	}
}

function validarCPF(CPF) {
	CPF = CPF.substr(0,3) + CPF.substr(4,3) + CPF.substr(8,3) + CPF.substr(12,2);
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
			soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++){
			soma += parseInt(CPF.charAt(i)) * (11 - i);
	}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10))) {
		return false;
	}
	return true;
}

function validaData(input) {
	if (!validarData(input.value)) {
		alert("Data inválida!");
	}
}

function validarData(data) {
	var erro = false;
	if (data.length != 10) {
		erro = true;
	}
	else {
		var x = data.split("/");
		if (x[0].length != 2 || x[1].length != 2 || x[2].length != 4) {
			erro = true;
		}
		else {
			switch (x[1]) {
			case "02":
				if (x[2] % 4 == 0) {
					if (x[0] > 29) {
						erro = true;
					}
				}
				else {
					if (x[0] > 28) {
						erro = true;
					}
				}
				break;
			case "01":
			case "03":
			case "05":
			case "07":
			case "08":
			case "10":
			case "12":
				if (x[0] > 31) {
					erro = true;
				}
				break;
			case "04":
			case "06":
			case "09":
			case "11":
				if (x[0] > 30) {
					erro = true;
				}
				break;
			default:
				erro = true;
				break;
			}
		}
	}
	return !erro;
}

function validaEmail(input) {
	if (!validarEmail(input.value)) {
		alert("E-mail inválido!");
	}
}

function validarEmail(email) {
	return !(email.indexOf("@") == -1 || email.indexOf(".") == -1 || email == "");
}
 
function ajustarCEP(input, e){
	if (!soNumeros(e)) {
		return false;
	}
	if(input.value.length==5){
			input.value += "-" ;
	}
}


function ajustarCNPJ(input, e) {
	if (!soNumeros(e)) {
		return false; 
	}
	// 12.456.890/2345-78
	if (input.value.length == 2 || input.value.length == 6) {
		input.value += "." ;
	}
	if (input.value.length == 10) {
		input.value += "/";
	}
	if (input.value.length == 15) {
		input.value += "-";
	}
}

function ajustarCPF(input, e) {
	if (!soNumeros(e)) {
		return false;
	}
	if ((input.value.length==3) || (input.value.length==7))
		input.value += "." ;
	if (input.value.length==11)
		input.value += "-";
}

function ajustarData (input, e) {
	if (!soNumeros(e)) {
		return false;
	}
	if (input.value.length == 2) {
		input.value += "/" ;
	}
	if (input.value.length == 5) {
		input.value += "/" ;
	}
}

function ajustarEstadual(input, e){
	if (!soNumeros(e)) {
		return false;
	}
	if ((input.value.length==2) || (input.value.length==6)) {
		input.value += "." ;
	}
	if (input.value.length==10) {
		input.value += "-";
	}
}

function ajustarTel(input, e) {
	if (!soNumeros(e)) {
		return false;
	}
	
	if(input.value.length==0) {
		input.value += "(" ;
	}
	
	if(input.value.length==3) {
		input.value += ") " ;
	}
	
	if(input.value.length==9) {
		input.value += "-" ;
	}
}

function soNumeros(e) {
	if (document.all) { // ie
		if((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8 && event.keyCode != 0){
			event.returnValue = false; 
			return false;
		}
	}
	else {
		if((e.which < 48 || e.which > 57) && e.which != 8 && e.which != 0){
			e.preventDefault();
			return false;
		}		
	}
	return true;
}

function getHttpRequest(handler) { 
	var objXmlHttp = null;
	
	if (navigator.userAgent.indexOf("MSIE") >= 0) { 
		var strName = "Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5") >= 0) {
			strName = "Microsoft.XMLHTTP";
		}
		try {
			objXmlHttp = new ActiveXObject(strName);
			objXmlHttp.onreadystatechange = handler;
			return objXmlHttp;
		}
		catch (e) {
			alert("Error. Scripting for ActiveX might be disabled") ;
			return null;
		}
	}
	else if (navigator.userAgent.indexOf("Mozilla") >= 0) {
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
		return objXmlHttp;
	}
	else if (navigator.userAgent.indexOf("Opera") >= 0) {
		alert("This example doesn't work in Opera");
		return null;
	}
}

function formataMoeda(input){
	/*
	var valor = new String(input).replace(".",",");
	while(valor.indexOf(".") != -1)
		valor = valor.replace(".","");
	while(valor.charAt(0) == "0")
		valor = valor.substr(1);
	while(valor.length < 2)
		valor = "0" + valor;
	if(valor.length == 2)
		valor = "0," + valor;
	else if(valor.length > 5){
		var z = valor.length - 2;
		var real = valor.substr(0,z);
		var dec = valor.substr(z);
		if(real.length > 3){
			for(i = Math.floor((real.length-1) / 3); i > 0; i--){
				var y = real.length - i * 3;
				real = real.substr(0,y) + "." + real.substr(y);
			}
		}
		valor = real + "," + dec; 
	}
	else{
		var x = valor.length - 2;
		valor = valor.substring(0,x) + "," + valor.substr(x);
	}
	return valor;
	*/
//	var valor = new String(input);
//	valor = valor.replace(".", ",");
	var valor = number_format_novo(input, 2, ',', '.');
/*	alert("O valor é " + valor);
	if(valor.indexOf(",") >= 0){
		var decimal = valor.split(",");
		var tam = decimal[1].length;
		if(tam == 1){
			valor += "0";
		}
		else if(tam > 2){
			decimal[1] = decimal[1].substr(0, 2);
			valor = decimal.join(",");
		}
	}
	else {
		valor += ",00";
	}
*/	
	return valor;
}


function number_format_novo(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}


function alteraDiv(div){

	var divFisica = document.getElementById("dvFisica1");

	var divJuridica = document.getElementById("dvJuridica1");

	if(div == '1'){


		divFisica.style.display = "block";
		divJuridica.style.display = "none";

	}

	else{

		divJuridica.style.display = "block";

		divFisica.style.display = "none";

	}

}




function validaJuridica(form){

	var retJ = false;

	emailRE = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");

	with(form){

		if(nomeEmp.value == ""){

			alert("Informe nome da Empresa!");

			nomeEmp.style.backgroundColor = "#FFFFCC";

			nomeEmp.focus();

		}
		
		
		else if (!validarCNPJ(cnpj.value)) {

			alert("CNPJ invalido!");

			cnpj.style.backgroundColor = "#FFFFCC";

			cnpj.focus();

		}
		
		
		else if(nomeFan.value == ""){

			alert("Informe nome Fantasia!");

			nomeFan.style.backgroundColor = "#FFFFCC";

			nomeFan.focus();

		}
		
		
		else if (DataConstituicao.value=="") {

			alert("Informe a Data de Constituicao");

			DataConstituicao.style.backgroundColor = "#FFFFCC";

			DataConstituicao.focus();

		}
		
	else if (endJur.value == "") {

		   alert("Informe o endereço!");

		   endJur.style.backgroundColor = "#FFFFCC";

		   endJur.focus();

		}
		
		else if (numeroJur.value == "") {

		   alert("Informe o numero");

		   numeroJur.style.backgroundColor = "#FFFFCC";

		   numeroJur.focus();

		}
		
		
		else if (bairroJur.value == "") {

		   alert("Informe o Bairro");

		   bairroJur.style.backgroundColor = "#FFFFCC";

		   bairroJur.focus();

		}
		
		else if (cidadeJur.value=="") {

		   alert("Informe a cidade!");

		   cidadeJur.style.backgroundColor = "#FFFFCC";

		   cidadeJur.focus();

		}
		
		else if (estadoJur.value=="") {

		   alert("Informe o Estado de localizacao da Empresa!");

		   estadoJur.style.backgroundColor = "#FFFFCC";

		   estadoJur.focus();

		}
		
		

		else if(cepJur.value == ""){

			alert("Informe o CEP!");

			cepJur.style.backgroundColor = "#FFFFCC";

			cepJur.focus();

		}
		
		else if (telJur.value=="") {

			alert("Informe o telefone!");

			telJur.style.backgroundColor = "#FFFFCC";

			telJur.focus();

		}
	
	
	
	else if (!emailRE.test(emailJur.value)) {

			alert("Email invalido!");

			emailJur.style.backgroundColor = "#FFFFCC";

			emailJur.focus();

  		}
	
	


		else {

			retJ = true;

		}

	}

	return retJ;

}



function validaFisica(form){

	var retF = false;

	emailREFis = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");

	with(form){
		 if(nomeCadastro.value == ""){

			alert("Informe o seu o nome!");

			nomeCadastro.style.backgroundColor = "#FFFFCC";

			nomeCadastro.focus();

		}
		
		
		else if(!validarCPF(cpf.value)) {

			alert("CPF invalido!");

			cpf.style.backgroundColor = "#FFFFCC";

			cpf.focus();

	

		}
		
		
		else if(rg.value == "") {

			alert("INFORME O RG!");

			rg.style.backgroundColor = "#FFFFCC";

			rg.focus();

	

		}


	else if(rg_emissao.value == "") {

			alert("Data de Emissao do RG!");

			rg_emissao.style.backgroundColor = "#FFFFCC";

			rg_emissao.focus();

	

		}

	else if(rg_orgao.value == "") {

			alert("Informe o Orgao do RG!");

			rg_orgao.style.backgroundColor = "#FFFFCC";

			rg_orgao.focus();

	

		}

	

		else if (nascimento.value=="") {

			alert("Informe sua Data de Nascimento!");

			nascimento.style.backgroundColor = "#FFFFCC";

			nascimento.focus();

		}


		else if (sexo.value=="") {

			alert("Selecione o Sexo");

			sexo.style.backgroundColor = "#FFFFCC";

			sexo.focus();

		}



		else if (EstadoCivil.value=="") {


			alert("Informe o Estado Civil");

			EstadoCivil.style.backgroundColor = "#FFFFCC";

			EstadoCivil.focus();

		}
		
		
		else if (natural.value=="") {

			alert("Informe a naturalidade");

			natural.style.backgroundColor = "#FFFFCC";

			natural.focus();

		}
		
		else if (pai.value=="") {

			alert("Informe o nome do Pai");

			pai.style.backgroundColor = "#FFFFCC";

			pai.focus();

		}
		
		
		else if (mae.value=="") {

			alert("Informe o nome da Mae");

			mae.style.backgroundColor = "#FFFFCC";

			mae.focus();

		}


		else if (endereco.value == "") {

		   alert("Informe o endereço!");

		   endereco.style.backgroundColor = "#FFFFCC";

		   endereco.focus();

		}
		
		else if (numero.value == "") {

		   alert("Informe o numero do endereco!");

		   numero.style.backgroundColor = "#FFFFCC";

		   numero.focus();

		}
		
		
		else if (bairroFis.value=="") {

		   alert("Informe o bairro!");

		   bairroFis.style.backgroundColor = "#FFFFCC";

		   bairroFis.focus();

		}
		
		
		else if (cidade.value=="") {

		   alert("Informe a cidade!");

		   cidade.style.backgroundColor = "#FFFFCC";

		   cidade.focus();

		}
		
		
		else if (estado.value=="") {

			alert("Selecione o seu Estado");

			estado.style.backgroundColor = "#FFFFCC";

			estado.focus();

		}

		else if(cep.value == ""){

			alert("Informe o CEP!");

			cep.style.backgroundColor = "#FFFFCC";

			cep.focus();

		}
		
		else if (tel.value=="") {

			alert("Informe o telefone!");

			tel.style.backgroundColor = "#FFFFCC";

			tel.focus();

		}
		
		
		
		else if (!emailREFis.test(emailFis.value)) {

			alert("Email invalido!");

			emailFis.style.backgroundColor = "#FFFFCC";

			emailFis.focus();

  		}
		
		
		
		else if (nome_conjuge.value=="" && EstadoCivil.value == "Casado") {

		   alert("Informe o nome do Conjuge.");

		   nome_conjuge.style.backgroundColor = "#FFFFCC";

		   nome_conjuge.focus();

		}
		
		
		else if(!validarCPF(cpf_conjuge.value) && EstadoCivil.value == "Casado") {

			alert("CPF do Conjuge invalido!");

			cpf_conjuge.style.backgroundColor = "#FFFFCC";

			cpf_conjuge.focus();

	

		}
		
		
		
		
		else if (rg_conjuge.value=="" && EstadoCivil.value == "Casado") {

		   alert("Informe o RG do Conjuge.");

		   rg_conjuge.style.backgroundColor = "#FFFFCC";

		   rg_conjuge.focus();

		}
		
		else if (rg_data_emissao_conjuge.value=="" && EstadoCivil.value == "Casado") {

		   alert("Informe a data de emissao do RG do Conjuge.");

		   rg_data_emissao_conjuge.style.backgroundColor = "#FFFFCC";

		   rg_data_emissao_conjuge.focus();

		}
		
		else if (rg_orgao_emissor_conjuge.value=="" && EstadoCivil.value == "Casado") {

		   alert("Informe o orgao emissor do RG do Conjuge.");

		   rg_orgao_emissor_conjuge.style.backgroundColor = "#FFFFCC";

		   rg_orgao_emissor_conjuge.focus();

		}
		
		
		else if (ref_banco.value=="") {

		   alert("Em referencias: Informe o Banco.");

		   ref_banco.style.backgroundColor = "#FFFFCC";

		   ref_banco.focus();

		}
		
		else if (ref_agencia.value=="") {

		   alert("Em referencias: Informe a Agencia.");

		   ref_agencia.style.backgroundColor = "#FFFFCC";

		   ref_agencia.focus();

		}
		
		else if (ref_conta.value=="") {

		   alert("Em referencias: Informe a Conta.");

		   ref_conta.style.backgroundColor = "#FFFFCC";

		   ref_conta.focus();

		}
		
		
		else if (ref_telefone_banco.value=="") {

		   alert("Em referencias: Informe o Telefone do Banco");

		   ref_telefone_banco.style.backgroundColor = "#FFFFCC";

		   ref_telefone_banco.focus();

		}
		
		
		else if (referencia1.value=="") {

		   alert("Em referencias: Informe a 1ª Referencia");

		   referencia1.style.backgroundColor = "#FFFFCC";

		   referencia1.focus();

		}
		
		else if (referencia1_tel.value=="") {

		   alert("Em referencias: Informe o telefone da 1ª Referencia");

		   referencia1_tel.style.backgroundColor = "#FFFFCC";

		   referencia1_tel.focus();

		}
		
		
		else if (referencia2.value=="") {

		   alert("Em referencias: Informe a 2ª Referencia");

		   referencia2.style.backgroundColor = "#FFFFCC";

		   referencia2.focus();

		}
		
		else if (referencia2_tel.value=="") {

		   alert("Em referencias: Informe o telefone da 2ª Referencia");

		   referencia2_tel.style.backgroundColor = "#FFFFCC";

		   referencia2_tel.focus();

		}
		

		
	else{

			retF = true;

		}

	}

	return retF;

}




function validarCadastro(form){



	if(document.getElementById("tipoFisica").checked){

		return validaFisica(form);

	}

	else{

		return validaJuridica(form);

	}

}
