var flgEnviou = false;

function liberaForm(){
	document.frmCadastro.cepLocal.value = 0
}

function editaEndereco(){
	if (document.frmCadastro.cepLocal.value == 1){
		if(!checaNumero(document.frmCadastro.NRCEP.value))
			alert("Por favor preencha corretamente o CEP antes do endereço")
		window.focus()
	}
}

function preencheForm(tpLOG, LOGRADOURO, BAIRRO, CIDADE, UF){
	document.frmCadastro.TPLOG.value = tpLOG
	document.frmCadastro.LOGRADOURO.value = LOGRADOURO
	document.frmCadastro.BAIRRO.value= BAIRRO
	document.frmCadastro.CIDADE.value= CIDADE
	document.frmCadastro.UF.value= UF
	document.frmCadastro.NUMERO.focus()
}

function limpaEndereco(){
	document.frmCadastro.TPLOG.value = ""
	document.frmCadastro.LOGRADOURO.value = ""
	document.frmCadastro.BAIRRO.value= ""
	document.frmCadastro.CIDADE.value= ""
	document.frmCadastro.UF.value= ""
}

function buscaCEP (){
	if ((checaNumero(document.frmCadastro.NRCEP.value)) && (document.frmCadastro.NRCEP.value.length == 8)){
		limpaEndereco()	
		parent.frames[1].document.location.href = "/Cadastro/busca_cep2.asp?cep=" + document.frmCadastro.NRCEP.value
	}
	else{
		limpaEndereco()
		alert("Por favor preencha o CEP corretamente - apenas números")
	}
}

function preencheForm(tpLOG, LOGRADOURO, BAIRRO, CIDADE, UF){
	document.frmCadastro.TPLOG.value = tpLOG
	document.frmCadastro.LOGRADOURO.value = LOGRADOURO
	document.frmCadastro.BAIRRO.value= BAIRRO
	document.frmCadastro.CIDADE.value= CIDADE
	document.frmCadastro.UF.value= UF
}

function valida(){
	
	if(!checaNulo('document.frmCadastro.NOME','nome', true)){
		return;
	}
	if(!isEmail(document.frmCadastro.EMAIL.value)){
		alert('O E-mail digitado não é valido, porfavor verifique se o e-mail está correto.');
		document.frmCadastro.EMAIL.focus();
		return;
	}
	if(!validaRadio('document.frmCadastro.SEXO')){
		alert('Escolha o seu sexo');
		return;
	}
	if(checaNulo('document.frmCadastro.LOGRADOURO','logradouro',false)){
		if(!checaNulo('document.frmCadastro.NUMERO','número',true)){
			return;
		}
	}
	if(checaNulo('document.frmCadastro.APTO','apartamento',false)){	
		if(!checaNumero(document.frmCadastro.APTO.value)){
			alert('O apartamento só pode conter números');
			document.frmCadastro.APTO.focus();
			return;
		}
	}
	if(!checaNulo('document.frmCadastro.NRCEP','CEP',true)){
		return;
	}
	if(!checaNumero(document.frmCadastro.NRCEP.value)){
		alert('O CEP só pode conter números');
		document.frmCadastro.NRCEP.focus();
		return;
	}
	if(checaNulo('document.frmCadastro.DDD1','DDD', false)){
		if(!checaNumero(document.frmCadastro.DDD1.value)){
			alert('O DDD só pode conter números');
			document.frmCadastro.DDD1.focus();
			return;
		}
	}
	if(checaNulo('document.frmCadastro.TELEFONE1','telefone', false)){
		if(!checaNumero(document.frmCadastro.TELEFONE1.value)){
			alert('O telefone só pode conter números');
			document.frmCadastro.TELEFONE1.focus();
			return;
		}
	}
	if(checaNulo('document.frmCadastro.DTNASC','data de nascimento', false)){
		if(!validaData(document.frmCadastro.DTNASC.value)){
			document.frmCadastro.DTNASC.focus();
			return;
		}
	}
	if(checaNulo('document.frmCadastro.CPF','cpf',false)){
		if(!checaNumero(document.frmCadastro.CPF.value)){
			alert('O CPF só pode conter números');
			document.frmCadastro.CPF.focus();
			return;
		}
	}
	if(document.frmCadastro.cartaoOUTROS.checked && !checaNulo('document.frmCadastro.cartao_OUTROS','cartão',true)){
		document.frmCadastro.cartao_OUTROS.focus();
		return;
	}
	
	if (!flgEnviou){
		flgEnviou = true;	
		document.frmCadastro.submit();
	}
}

function buscaCliente(CAMPO, OBJ)
{
	if (OBJ.value!='')
	{
		if(!isEmail(OBJ.value))
		{
			alert('Digite o seu E-MAIL corretamente');
			event.returnValue = false;
			OBJ.select();
		}
		else
		{
			if (CAMPO!='' && OBJ.value!='')
				parent.frames[1].document.location.href = "/include/busca_cliente.asp?campo=" + CAMPO + "&valor="+ OBJ.value;
			else
			{
				parent.frames[0].window.location.href = "/scripts/erro.asp";
				alert('Acesso incorreto à página!\nFavor seguir instruções corretamente!');
			}
		}
	}

}

function preencheFormCli(CODCLI, EMAIL, NOME, TPLOG, LOGRADOURO, NUMERO, APTO, COMP, BAIRRO, CIDADE, UF, NRCEP, DDD1, TELEFONE1, RAMAL1, DTNASC, FLGCORRESP, FORMATO_EMAIL, CPF, SEXO, ESTCIVIL, CODGRAUINST, FLGENDERECO)
{
	document.frmCadastro.CODCLI.value = CODCLI;
	document.frmCadastro.EMAIL.value = EMAIL.toLowerCase();
	document.frmCadastro.NOME.value = NOME.toUpperCase();
	document.frmCadastro.TPLOG.value = TPLOG.toUpperCase();;
	document.frmCadastro.LOGRADOURO.value = LOGRADOURO.toUpperCase();;
	document.frmCadastro.NUMERO.value = NUMERO;
	document.frmCadastro.APTO.value = APTO;
	document.frmCadastro.COMP.value = COMP.toUpperCase();;
	document.frmCadastro.BAIRRO.value = BAIRRO.toUpperCase();;
	document.frmCadastro.CIDADE.value = CIDADE.toUpperCase();;
	document.frmCadastro.UF.value = UF.toUpperCase();;
	document.frmCadastro.NRCEP.value = NRCEP;
	document.frmCadastro.DDD1.value = DDD1;
	document.frmCadastro.TELEFONE1.value = TELEFONE1;
	document.frmCadastro.RAMAL1.value = RAMAL1;
	document.frmCadastro.DTNASC.value = DTNASC;
	document.frmCadastro.CPF.value = CPF;
	document.frmCadastro.FLGENDERECO.value = FLGENDERECO;


	if (CODGRAUINST != '')
	{
		var obj = eval(document.frmCadastro.CODGRAUINST);
		for (i=0;i<obj.length;i++)
		{
			if (obj[i].value == CODGRAUINST)
			{
				obj[i].selected = true;
			}
		}
	}


	if (SEXO == 'M')
		document.frmCadastro.SEXO[0].checked = true;
	else
		document.frmCadastro.SEXO[1].checked = true;

	if (ESTCIVIL == 'S')
		document.frmCadastro.ESTCIVIL[0].checked = true;
	else if (ESTCIVIL == 'C')
		document.frmCadastro.ESTCIVIL[1].checked = true;
	else if (ESTCIVIL == 'D')
		document.frmCadastro.ESTCIVIL[2].checked = true;
	else if (ESTCIVIL == 'O')
		document.frmCadastro.ESTCIVIL[3].checked = true;

		
	if (FLGCORRESP == 'S')
		document.frmCadastro.FLGCORRESP[0].checked = true;
	else
		document.frmCadastro.FLGCORRESP[1].checked = true;

/*	if (FORMATO_EMAIL == 'H')
		document.frmCadastro.FORMATO_EMAIL[0].checked = true;
	else
		document.frmCadastro.FORMATO_EMAIL[1].checked = true;*/
		
	liberaForm();
}

function limpaFormCli()
{
	document.frmCadastro.CODCLI.value = '';
	document.frmCadastro.NOME.value = '';
	document.frmCadastro.TPLOG.value = '';
	document.frmCadastro.LOGRADOURO.value = '';
	document.frmCadastro.NUMERO.value = '';
	document.frmCadastro.APTO.value = '';
	document.frmCadastro.COMP.value = '';
	document.frmCadastro.BAIRRO.value = '';
	document.frmCadastro.CIDADE.value = '';
	document.frmCadastro.UF.value = '';
	document.frmCadastro.NRCEP.value = '';
	document.frmCadastro.DDD1.value = '';
	document.frmCadastro.TELEFONE1.value = '';
	document.frmCadastro.RAMAL1.value = '';
	document.frmCadastro.DTNASC.value = '';
	document.frmCadastro.FLGCORRESP.value = '';
//	document.frmCadastro.FORMATO_EMAIL.value = ''; 
	document.frmCadastro.CPF.value = '';
	document.frmCadastro.SEXO.value = '';
	document.frmCadastro.FLGENDERECO.value = '';

	liberaFormCli();

	document.frmCadastro.EMAIL.focus();
}

function editaCliente(){
	if (document.frmCadastro.flgCli.value == 1)
	{
		if(document.frmCadastro.EMAIL.value=='')
			alert("Comece seu cadastro pelo E-MAIL.");

		document.frmCadastro.EMAIL.focus();
	}
}