/*****************************Validação durante o preenchimento*******************************/
function VerificaTipoPessoaSenha()
{
//	try
//	{
		var rdnPF = document.getElementById('rdnPF');
		var rdnPJ = document.getElementById('rdnPJ');
		
		var DadosPessoaFisica		= document.getElementById('DadosPessoaFisica');
		var DadosPessoaJuridica		= document.getElementById('DadosPessoaJuridica');
		
	    if(rdnPF.checked || (!rdnPF.checked && !rdnPJ.checked))
	    {
			DadosPessoaFisica.style.display		= 'block';	
			DadosPessoaJuridica.style.display	= 'none';
			rdnPF.checked						= true;
	    }	
	    else
	    {
			DadosPessoaFisica.style.display		= 'none';	
			DadosPessoaJuridica.style.display	= 'block';	    

	    }
//	}
//	catch(err)
//	{
//		alert(err);
//	}
}

function VerificaCPF(campo)
{
	var spanMsgValidacao = document.getElementById('spanMsgValidacaoCPF');
		
	if(Trim(campo.value) == '' || validaCPF(Trim(campo.value)))
	{
		spanMsgValidacao.innerHTML = '';	
		
		formcadastro.ExisteCPF(Trim(campo.value), callback_ExisteCPF);
	}
	else
	{
		spanMsgValidacao.innerHTML = '« CPF inválido';
	}
}

function callback_ExisteCPF(res)
{
	if(!res.error)
	{
		document.getElementById('spanMsgValidacaoCPF').innerHTML = res.value;
	}
	else
	{
		alert(res.error);
	}
}

function VerificaCNPJ(campo)
{
	var spanMsgValidacao = document.getElementById('spanMsgValidacaoCNPJ');
		
	if(Trim(campo.value) == '' || validaCNPJ(Trim(campo.value)))
	{
		spanMsgValidacao.innerHTML = '';	
		
		formcadastro.ExisteCNPJ(Trim(campo.value), callback_ExisteCNPJ);
	}
	else
	{
		spanMsgValidacao.innerHTML = '« CNPJ inválido';
	}
}

function callback_ExisteCNPJ(res)
{
	if(!res.error)
	{
		document.getElementById('spanMsgValidacaoCNPJ').innerHTML = res.value;
	}
	else
	{
		alert(res.error);
	}
}

function VerificaIE()
{
	var spanMsgValidacao	= document.getElementById('spanMsgValidacaoIE');
	var campo				= document.getElementById('formcadastro_txtPJIE');
	var check				= document.getElementById('formcadastro_ckbIsento');
	var retorno				= '';
		
	if(Trim(campo.value) == '' || (Trim(campo.value) == 'Isento' && check.checked) || Number(Trim(campo.value)).toString() != 'NaN')
	{
		spanMsgValidacao.innerHTML = '';	
		
	}
	else
	{
		spanMsgValidacao.innerHTML	= '« IE inválida';
		retorno						= '« IE inválida';
	}
	
	return retorno;
}

function VerificaDataNascimento()
{
	var spanMsgValidacao			= document.getElementById('spanMsgValidacaoDataNascimento');
	var currentTime					= new Date();
	var retorno						= '';

	var dia = document.getElementById('formcadastro_txtPFNascDia');
	var mes = document.getElementById('formcadastro_txtPFNascMes');
	var ano = document.getElementById('formcadastro_txtPFNascAno');
	
	if((Trim(dia.value) == '' && Trim(mes.value) == '' && Trim(ano.value) == '') || ValidaData(Trim(dia.value), Trim(mes.value), Trim(ano.value)))
	{
		if(Number(Trim(ano.value)) > currentTime.getFullYear() || (Number(Trim(ano.value)) == currentTime.getFullYear() && Number(Trim(mes.value)) > (currentTime.getMonth() +1)) || (Number(Trim(ano.value)) == currentTime.getFullYear() && Number(Trim(mes.value)) == (currentTime.getMonth() +1) && Number(Trim(dia.value)) > currentTime.getDate()))
		{
			spanMsgValidacao.innerHTML	= '« Você realmente veio do futuro?';	
			retorno						= '« Você realmente veio do futuro?';	
		}
		else
		{
			spanMsgValidacao.innerHTML = '';	
		}
	}
	else
	{
		if(Trim(dia.value) == '' || Trim(mes.value) == '' || Trim(ano.value) == '')
		{
			spanMsgValidacao.innerHTML	= '« Data de Nascimento incompleta';	
			retorno						= '« Data de Nascimento incompleta';	
		}
		else if(Number(Trim(ano.value)) < 1900)
		{
			spanMsgValidacao.innerHTML	= '« Somente a partir de 1900';	
			retorno						= '« Somente a partir de 1900';	
		}
		else
		{
			spanMsgValidacao.innerHTML	= '« Data de Nascimento inválida';	
			retorno						= '« Data de Nascimento inválida';	
		}
	}
	return retorno;
}

function VerificaCEP(campo)
{
	var spanMsgValidacao	= document.getElementById('spanMsgValidacaoCEP');
	var retorno				= ''
		
	if(Trim(campo.value).replace('-','') == '' || (Number(Trim(campo.value).replace('-','')).toString() != 'NaN' && Trim(campo.value).replace('-','').length == 8))
	{
		spanMsgValidacao.innerHTML = '';	
	}
	else
	{
		if(Number(Trim(campo.value).replace('-','')).toString() == 'NaN')
		{
			spanMsgValidacao.innerHTML	= '« CEP inválido';
			retorno						= '« CEP inválido';	
		}
		else
		{
			spanMsgValidacao.innerHTML	= '« CEP incompleto';
			retorno						= '« CEP incompleto';	
		}
	}
	return retorno;
}

function VerificaTelefone(tipo)
{
	var spanMsgValidacao		= document.getElementById('spanMsgValidacaoTel'+tipo);
	var ddd						= document.getElementById('formcadastro_txtTel'+tipo+'Ddd');
	var telefone				= document.getElementById('formcadastro_txtTel'+tipo);
	var ramal					= document.getElementById('formcadastro_txtRamal'+tipo);
	
	if(ddd == null)
	{
		ddd						= document.getElementById('txtTel'+tipo+'Ddd');
		telefone				= document.getElementById('txtTel'+tipo);
		ramal					= document.getElementById('txtRamal'+tipo);
	}
	
	var retorno					= '';
	
	if (ramal == null)
	{
		ramal = '';
	}
	else
	{
		ramal = ramal.value;
	}
	
	if(Trim(ddd.value) == '' || (Number(Trim(ddd.value)).toString() != 'NaN' && Trim(ddd.value).length == 2))
	{
		spanMsgValidacao.innerHTML = '';	
		
		if(Trim(telefone.value).replace('-','') == '' || Number(Trim(telefone.value).replace('-','')).toString() != 'NaN')
		{
			if(!(Trim(ramal) == '' || Number(Trim(ramal)).toString() != 'NaN'))
			{
				spanMsgValidacao.innerHTML	= '« Ramal inválido';
				retorno						= '« Ramal inválido';
			}				
		
			if(spanMsgValidacao.innerHTML == '')
			{
				if(Trim(ddd.value) != '' || Trim(telefone.value) != '' || Trim(ramal) != '')
				{
					if(Trim(ddd.value) == '' || Trim(telefone.value) == '' || Trim(telefone.value).replace('-','').length < 7)
					{
						spanMsgValidacao.innerHTML	= '« Incompleto';
						retorno						= '« Incompleto';					
					}
				}
			}
		}
		else
		{
			if(tipo == 'Fax')
			{
				spanMsgValidacao.innerHTML	= '« Fax inválido';
				retorno						= '« Fax inválido';
			}
			else if(tipo == 'Cel')
			{
				spanMsgValidacao.innerHTML	= '« Celular inválido';
				retorno						= '« Celular inválido';
			}			
			else
			{
				spanMsgValidacao.innerHTML	= '« Telefone inválido';
				retorno						= '« Telefone inválido';
			}
		}		
	}
	else
	{
		if(Number(Trim(ddd.value)).toString() == 'NaN')
		{
			spanMsgValidacao.innerHTML	= '« DDD inválido';
			retorno						= '« DDD inválido';
		}
		else
		{
			spanMsgValidacao.innerHTML	= '« DDD incompleto';
			retorno						= '« DDD incompleto';
		}
	}
	return retorno;
}

function VerificaEmail()
{
	var spanMsgValidacao	= document.getElementById('spanMsgValidacaoEmail');
	var campo				= document.getElementById('formcadastro_txtEmail');
		
	if(Trim(campo.value) == '' || ValidaEmail(Trim(campo.value)))
	{
		spanMsgValidacao.innerHTML = '';	
		
		formcadastro.ExisteEmail(Trim(campo.value), callback_ExisteEmail);
	}
	else
	{
		spanMsgValidacao.innerHTML	= '« E-mail inválido';
	}
}

function callback_ExisteEmail(res)
{
	if(!res.error)
	{
		document.getElementById('spanMsgValidacaoEmail').innerHTML = res.value;
	}
	else
	{
		alert(res.error);
	}
}

function VerificaSenha()
{
	var spanMsgValidacao	= document.getElementById('spanMsgValidacaoSenha');
	var campo				= document.getElementById('formcadastro_txtSenha');
	var retorno				= '';
		
	if(Trim(campo.value) == '' || (Trim(campo.value).length >= 6 && ValidaSenha(Trim(campo.value))))
	{
		spanMsgValidacao.innerHTML = '';	
		
	}
	else
	{
		if(Trim(campo.value).length < 6)
		{
			spanMsgValidacao.innerHTML	= '« No mínimo 6 caracteres';
			retorno						= '« No mínimo 6 caracteres';		
		}
		else
		{
			spanMsgValidacao.innerHTML	= '« A Senha contém algum caractere inválido';
			retorno						= '« A Senha contém algum caractere inválido';
		}
	}
	
	return retorno;
}

function VerificaConfSenha()
{
	var spanMsgValidacao	= document.getElementById('spanMsgValidacaoConfirmacaoSenha');
	var campo				= document.getElementById('formcadastro_txtSenha');
	var campo1				= document.getElementById('formcadastro_txtSenhaConfirmacao');
	
	var retorno				= '';
		
	if(Trim(campo.value) == '' || Trim(campo1.value) == '' || Trim(campo.value) == Trim(campo1.value))
	{
		spanMsgValidacao.innerHTML = '';	
		
	}
	else
	{
		spanMsgValidacao.innerHTML	= '« Confirme corretamente a Senha';
		retorno						= '« Confirme corretamente a Senha';		
	}
	
	return retorno;
}

function VerificaCodigoSeguranca(campo)
{
	formcadastro.ValidaCodigoSeguranca(Trim(campo.value), callback_ValidaCodigoSeguranca);
}

function callback_ValidaCodigoSeguranca(res)
{
	if(!res.error)
	{
		if (document.getElementById('spanMsgValidacaoCodigoSeguranca'))
		{
			document.getElementById('spanMsgValidacaoCodigoSeguranca').innerHTML = res.value;
		}
	}
	else
	{
		alert(res.error);
	}
}

/*********************************************************************************************/
function ReloadImagemSeguranca()
{
	formcadastro.ReloadImagemSeguranca(callback_ReloadImagemSeguranca);
}

function callback_ReloadImagemSeguranca(res)
{
	if(!res.error)
	{
		document.getElementById('imgSeguranca').src = '../cadastro/codigoImagem.aspx?sc=' + res.value;
	}
	else
	{
		alert(res.error);
	}
}

function AtivarIsento()
{ 
	var ckbIsento = document.getElementById("formcadastro_ckbIsento"); 
	var txtPJIE = document.getElementById("formcadastro_txtPJIE");

	if(ckbIsento.checked) 
	{
		txtPJIE.value		= "Isento" ;
		txtPJIE.readOnly	= true;
	}
	else
	{
		txtPJIE.readOnly	= false;
		
		if(txtPJIE.value == "Isento")
		{
			txtPJIE.value		= "" ;
		}
	}
}


function VerificaTipoPessoa()
{
	try
	{
		var formcadastro_rdnPF		= document.getElementById('formcadastro_rdnPF');
		var formcadastro_rdnPJ		= document.getElementById('formcadastro_rdnPJ');
		
		var DadosPessoaFisica		= document.getElementById('DadosPessoaFisica');
		var DadosPessoaJuridica		= document.getElementById('DadosPessoaJuridica');
		
	    if(formcadastro_rdnPF.checked || (!formcadastro_rdnPF.checked && !formcadastro_rdnPJ.checked))
	    {
			DadosPessoaFisica.style.display		= 'block';	
			DadosPessoaJuridica.style.display	= 'none';
			formcadastro_rdnPF.checked			= true;
	    }	
	    else
	    {
			DadosPessoaFisica.style.display		= 'none';	
			DadosPessoaJuridica.style.display	= 'block';	    

	    }
	}
	catch(err)
	{
		alert(err.description);
	}
}

function VerificaTipoPessoaEdicao()
{
	try
	{
		VerificaTipoPessoa();

		if (document.getElementById('form_container'))
		{
			document.getElementById('form_container').style.display	= "none";
		}
	}
	catch(err)
	{
		alert(err.description);
	}
}

function VerificaTipoPessoaInclusao()
{
	try
	{
		VerificaTipoPessoa();

		if (document.getElementById('form_container'))
		{
			document.getElementById('form_container').style.display	= "block";
		}
	}
	catch(err)
	{
		alert(err.description);
	}
}



function CamposPreenchidos(ParentForm)
{
	var retorno = true;
    
    try
    {
		var formcadastro_rdnPF					= document.getElementById('formcadastro_rdnPF');
		var formcadastro_rdnPJ					= document.getElementById('formcadastro_rdnPJ');
        
        var formcadastro_txtPFNome              = document.getElementById('formcadastro_txtPFNome');
        var formcadastro_txtPFSobrenome         = document.getElementById('formcadastro_txtPFSobrenome');
        var formcadastro_txtPFCPF               = document.getElementById('formcadastro_txtPFCPF'); 
        var formcadastro_txtPFRG				= document.getElementById('formcadastro_txtPFRG');
        var formcadastro_txtPFNascDia           = document.getElementById('formcadastro_txtPFNascDia');
        var formcadastro_txtPFNascMes           = document.getElementById('formcadastro_txtPFNascMes');
        var formcadastro_txtPFNascAno           = document.getElementById('formcadastro_txtPFNascAno');
        
        var formcadastro_txtPJRazaoSocial       = document.getElementById('formcadastro_txtPJRazaoSocial');
        var formcadastro_txtPJCNPJ              = document.getElementById('formcadastro_txtPJCNPJ');
        var formcadastro_txtPJIE				= document.getElementById('formcadastro_txtPJIE');
        var formcadastro_txtPJNomeFantasia		= document.getElementById('formcadastro_txtPJNomeFantasia');
        
        var formcadastro_txtCep					= document.getElementById('formcadastro_txtCep');
        var formcadastro_txtEndereco			= document.getElementById('formcadastro_txtEndereco');
        var formcadastro_txtNumero				= document.getElementById('formcadastro_txtNumero');        
        var formcadastro_txtComplemento			= document.getElementById('formcadastro_txtComplemento');
        var formcadastro_txtBairro				= document.getElementById('formcadastro_txtBairro');
        var formcadastro_txtCidade				= document.getElementById('formcadastro_txtCidade');
        
        var formcadastro_ddlEstado				= document.getElementById('formcadastro_ddlEstado');
        
        var formcadastro_txtTel1Ddd				= document.getElementById('formcadastro_txtTel1Ddd');
        var formcadastro_txtTel1				= document.getElementById('formcadastro_txtTel1');
        var formcadastro_txtRamal1				= document.getElementById('formcadastro_txtRamal1');
		
        var formcadastro_txtTel2Ddd				= document.getElementById('formcadastro_txtTel2Ddd');
        var formcadastro_txtTel2				= document.getElementById('formcadastro_txtTel2');
        var formcadastro_txtRamal2				= document.getElementById('formcadastro_txtRamal2');
        
        var formcadastro_txtTel3Ddd				= document.getElementById('formcadastro_txtTel3Ddd');
        var formcadastro_txtTel3				= document.getElementById('formcadastro_txtTel3');
        var formcadastro_txtRamal3				= document.getElementById('formcadastro_txtRamal3');
                
        var formcadastro_txtTelCelDdd			= document.getElementById('formcadastro_txtTelCelDdd');
        var formcadastro_txtTelCel				= document.getElementById('formcadastro_txtTelCel');
        
        var formcadastro_txtTelFaxDdd			= document.getElementById('formcadastro_txtTelFaxDdd');
        var formcadastro_txtTelFax				= document.getElementById('formcadastro_txtTelFax');
        
        var formcadastro_txtEmail				= document.getElementById('formcadastro_txtEmail');
        
        var formcadastro_txtSenha				= document.getElementById('formcadastro_txtSenha');
        var formcadastro_txtSenhaConfirmacao	= document.getElementById('formcadastro_txtSenhaConfirmacao'); 
        
        var formcadastro_txtCodigoSeguranca		= document.getElementById('formcadastro_txtCodigoSeguranca'); 
        
        //MudaCorBorda(formcadastro_txtPFNome,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPFSobrenome,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPFCPF,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPFRG,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPFNascDia,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPFNascMes,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPFNascAno,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPJRazaoSocial,'#B92025');        				
        //MudaCorBorda(formcadastro_txtPJCNPJ,'#B92025'); 
        //MudaCorBorda(formcadastro_txtPJIE,'#B92025');   
        //MudaCorBorda(formcadastro_txtPJNomeFantasia,'#B92025');        				
        //MudaCorBorda(formcadastro_txtCep,'#B92025');        				
        //MudaCorBorda(formcadastro_txtEndereco,'#B92025');        				
        //MudaCorBorda(formcadastro_txtNumero,'#B92025');        				
        //MudaCorBorda(formcadastro_txtComplemento,'#B92025');        				
        //MudaCorBorda(formcadastro_txtBairro,'#B92025');        				
        //MudaCorBorda(formcadastro_txtCidade,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTel1Ddd,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTel1,'#B92025');        				
        //MudaCorBorda(formcadastro_txtRamal1,'#B92025'); 
        //MudaCorBorda(formcadastro_txtTel2Ddd,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTel2,'#B92025');        				
        //MudaCorBorda(formcadastro_txtRamal2,'#B92025');         
        //MudaCorBorda(formcadastro_txtTel3Ddd,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTel3,'#B92025');        				
        //MudaCorBorda(formcadastro_txtRamal3,'#B92025');     
        //MudaCorBorda(formcadastro_txtTelCelDdd,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTelCel,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTelFaxDdd,'#B92025');        				
        //MudaCorBorda(formcadastro_txtTelFax,'#B92025');        				        
        //MudaCorBorda(formcadastro_txtEmail,'#B92025');        				
        //MudaCorBorda(formcadastro_txtSenha,'#B92025');        				
        //MudaCorBorda(formcadastro_txtSenhaConfirmacao,'#B92025');        	
        //MudaCorBorda(formcadastro_txtCodigoSeguranca,'#B92025');   
        
        for (j=0; j<document.getElementsByTagName("span").length; j++)
		{
			var spanMsgValidacao = document.getElementsByTagName("span")[j];

			if (spanMsgValidacao.id.indexOf('spanMsgValidacao') > -1 && spanMsgValidacao.id.indexOf('spanMsgValidacaoEmail') == -1 && spanMsgValidacao.id.indexOf('spanMsgValidacaoCPF') == -1 && spanMsgValidacao.id.indexOf('spanMsgValidacaoCNPJ') == -1 && spanMsgValidacao.id.indexOf('spanMsgValidacaoCodigoSeguranca') == -1)
			{
				spanMsgValidacao.innerHTML = '';
			}
		}
        
		if(formcadastro_rdnPF.checked)
		{   
			if(Trim(formcadastro_txtPFNome.value) == '')           
			{
				document.getElementById('spanMsgValidacaoNome').innerHTML = '« Preencha o Nome';				
				retorno = false;
				//MudaCorBorda(formcadastro_txtPFNome,'red');
			} 
			
			if(Trim(formcadastro_txtPFSobrenome.value) == '')      
			{
				document.getElementById('spanMsgValidacaoSobrenome').innerHTML = '« Preencha o Sobrenome';					
				retorno = false;
				//MudaCorBorda(formcadastro_txtPFSobrenome,'red');
			} 

			if(Trim(formcadastro_txtPFCPF.value) == '')            
			{
				document.getElementById('spanMsgValidacaoCPF').innerHTML = '« Preencha o CPF';					
				retorno = false;
				//MudaCorBorda(formcadastro_txtPFCPF,'red');
			} 
				        
			if(Trim(formcadastro_txtPFCPF.value) != '' && document.getElementById('spanMsgValidacaoCPF').innerHTML != '')
			{
				retorno = false;
				//MudaCorBorda(formcadastro_txtPFCPF,'red');
			} 			
			
			if(Trim(formcadastro_txtPFRG.value) == '')			   
			{
				document.getElementById('spanMsgValidacaoRG').innerHTML = '« Preencha o RG';									
				retorno = false;
				//MudaCorBorda(formcadastro_txtPFRG,'red');
			}
	        
			if(Trim(formcadastro_txtPFNascDia.value) == '' && Trim(formcadastro_txtPFNascMes.value) == '' && Trim(formcadastro_txtPFNascAno.value) == '')        
			{
				document.getElementById('spanMsgValidacaoDataNascimento').innerHTML = '« Preencha a Data de Nascimento';											
				//MudaCorBorda(formcadastro_txtPFNascDia,'red');
				//MudaCorBorda(formcadastro_txtPFNascMes,'red');
				//MudaCorBorda(formcadastro_txtPFNascAno,'red');
				retorno = false;
			}
     
			if(document.getElementById('spanMsgValidacaoDataNascimento').innerHTML == '' && VerificaDataNascimento() != '')
			{
				//MudaCorBorda(formcadastro_txtPFNascDia,'red');
				//MudaCorBorda(formcadastro_txtPFNascMes,'red');
				//MudaCorBorda(formcadastro_txtPFNascAno,'red');
				retorno = false;
			}
		}
		else if(formcadastro_rdnPJ.checked)
		{
			if(Trim(formcadastro_txtPJRazaoSocial.value) == '')		
			{
				document.getElementById('spanMsgValidacaoRazaoSocial').innerHTML = '« Preencha a Razão Social';
				retorno = false;
				//MudaCorBorda(formcadastro_txtPJRazaoSocial,'red');
			}
			
			if(Trim(formcadastro_txtPJNomeFantasia.value) == '')    
			{
				document.getElementById('spanMsgValidacaoNomeFantasia').innerHTML = '« Preencha o Nome Fantasia';
				retorno = false;
				//MudaCorBorda(formcadastro_txtPJNomeFantasia,'red');
			}

			if(Trim(formcadastro_txtPJCNPJ.value) == '')           
			{
				document.getElementById('spanMsgValidacaoCNPJ').innerHTML = '« Preencha o CNPJ';
				retorno = false;
				//MudaCorBorda(formcadastro_txtPJCNPJ,'red');
			} 
		
			if(Trim(formcadastro_txtPJCNPJ.value) != '' && document.getElementById('spanMsgValidacaoCNPJ').innerHTML != '')
			{
				retorno = false;
				//MudaCorBorda(formcadastro_txtPJCNPJ,'red');
			} 
			
			if(Trim(formcadastro_txtPJIE.value) == '')				
			{
				document.getElementById('spanMsgValidacaoIE').innerHTML = '« Preencha a IE';				
				retorno = false;
				//MudaCorBorda(formcadastro_txtPJIE,'red');
			} 
			
			if(Trim(formcadastro_txtPJIE.value) != '' && VerificaIE() != '')				
			{
				retorno = false;
				//MudaCorBorda(formcadastro_txtPJIE,'red');
			} 
		} 
	    
		if(Trim(formcadastro_txtCep.value).replace('-','') == '')				
		{
			document.getElementById('spanMsgValidacaoCEP').innerHTML = '« Preencha o CEP';	
			retorno = false;
			//MudaCorBorda(formcadastro_txtCep,'red');
		} 
		
		if(Trim(formcadastro_txtCep.value).replace('-','') != '' && VerificaCEP(formcadastro_txtCep) != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtCep,'red');		
		}
		
		if(Trim(formcadastro_txtEndereco.value) == '')          
		{
			document.getElementById('spanMsgValidacaoEndereco').innerHTML = '« Preencha o Endereço';
			retorno = false;
			//MudaCorBorda(formcadastro_txtEndereco,'red');
		} 
		
		if(Trim(formcadastro_txtNumero.value) == '')			
		{
			document.getElementById('spanMsgValidacaoNumero').innerHTML = '« Preencha o Número';
			retorno = false;
			//MudaCorBorda(formcadastro_txtNumero,'red');
		} 
		
		if(Trim(formcadastro_txtBairro.value) == '')            
		{
			document.getElementById('spanMsgValidacaoBairro').innerHTML = '« Preencha o Bairro';
			retorno = false;
			//MudaCorBorda(formcadastro_txtBairro,'red');
		} 
		
		if(Trim(formcadastro_txtCidade.value) == '')			
		{
			document.getElementById('spanMsgValidacaoCidade').innerHTML = '« Preencha a Cidade';
			retorno = false;
			//MudaCorBorda(formcadastro_txtCidade,'red');
		} 
	    
	    if(Trim(formcadastro_txtTel1Ddd.value) == '' && Trim(formcadastro_txtTel1.value) == '')			
		{
			document.getElementById('spanMsgValidacaoTel1').innerHTML = '« Preencha o Telefone 1';
			retorno = false;
			//MudaCorBorda(formcadastro_txtTel1Ddd,'red');
			//MudaCorBorda(formcadastro_txtTel1,'red');
		} 
		
		if(document.getElementById('spanMsgValidacaoTel1').innerHTML == '' && VerificaTelefone('1') != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtTel1Ddd,'red');		
			//MudaCorBorda(formcadastro_txtTel1,'red');
			
			if(document.getElementById('spanMsgValidacaoTel1').innerHTML.indexOf('Ramal') > -1)
			{
				//MudaCorBorda(formcadastro_txtRamal1,'red');
			}
		}
		
		if(VerificaTelefone('2') != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtTel2Ddd,'red');		
			//MudaCorBorda(formcadastro_txtTel2,'red');
			
			if(document.getElementById('spanMsgValidacaoTel2').innerHTML.indexOf('Ramal') > -1)
			{
				//MudaCorBorda(formcadastro_txtRamal2,'red');
			}
		}
		
		if(VerificaTelefone('3') != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtTel3Ddd,'red');		
			//MudaCorBorda(formcadastro_txtTel3,'red');
			
			if(document.getElementById('spanMsgValidacaoTel3').innerHTML.indexOf('Ramal') > -1)
			{
				//MudaCorBorda(formcadastro_txtRamal3,'red');
			}
		}		
		
		if(VerificaTelefone('Cel') != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtTelCelDdd,'red');		
			//MudaCorBorda(formcadastro_txtTelCel,'red');
		}
		
		if(VerificaTelefone('Fax') != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtTelFax,'red');		
			//MudaCorBorda(formcadastro_txtTelFax,'red');
		}

		if(Trim(formcadastro_txtEmail.value) == '')
		{
			document.getElementById('spanMsgValidacaoEmail').innerHTML = '« Preencha o E-mail';
			retorno = false;
			//MudaCorBorda(formcadastro_txtEmail,'red');
		} 

		if(Trim(formcadastro_txtEmail.value) != '' && document.getElementById('spanMsgValidacaoEmail').innerHTML != '')
		{
			retorno = false;
			//MudaCorBorda(formcadastro_txtEmail,'red');
		} 

		if(ParentForm == 'FormInclusaoCadastro')
		{
			if(Trim(formcadastro_txtSenha.value) == '')				
			{
				document.getElementById('spanMsgValidacaoSenha').innerHTML = '« Preencha a Senha';	
				retorno = false;
				//MudaCorBorda(formcadastro_txtSenha,'red');
			} 

			if(Trim(formcadastro_txtSenha.value) != '' && VerificaSenha() != '')				
			{
				retorno = false;
				//MudaCorBorda(formcadastro_txtSenha,'red');
			} 
						
			if(Trim(formcadastro_txtSenhaConfirmacao.value) == '')  
			{
				document.getElementById('spanMsgValidacaoConfirmacaoSenha').innerHTML = '« Preencha a Confirmação da Senha';	
				retorno = false;
				//MudaCorBorda(formcadastro_txtSenhaConfirmacao,'red');
			} 
			
			if(formcadastro_txtSenhaConfirmacao.value != '' && VerificaConfSenha() != '')
			{
				retorno = false;
				//MudaCorBorda(formcadastro_txtSenhaConfirmacao,'red');
			} 
		}
		
		if(document.getElementById('BlocoSeguranca').style.display != 'none')
		{
			if(Trim(formcadastro_txtCodigoSeguranca.value) == '')
			{
				document.getElementById('spanMsgValidacaoCodigoSeguranca').innerHTML = '« Preencha a seqüência';	
				retorno = false;
				//MudaCorBorda(formcadastro_txtCodigoSeguranca,'red');
			}
			
			//if(Trim(formcadastro_txtCodigoSeguranca.value) != '' && document.getElementById('spanMsgValidacaoCodigoSeguranca').innerHTML != '')
			//{
			//	retorno = false;
				//MudaCorBorda(formcadastro_txtCodigoSeguranca,'red');
			//} 	
		}
    }
    catch(err)
    {
        alert(err.description);
    }
    
    if(!retorno)
    {
		alert('Atenção!\n\rHá informações não preenchidas corretamente!');
    }
    
    if(retorno)
    {
		formcadastro_txtCidade.disabled = false;
		formcadastro_ddlEstado.disabled = false;
    }
    
    return retorno;          
}

function MudaCorBorda(obj, cor)
{
    try
    {        
		if(obj != null)
		{
			var Borda               = '1,9px';
			var BordaEstilo         = 'solid';
	        
			obj.style.border        = Borda;
			obj.style.borderStyle   = BordaEstilo;
			obj.style.borderColor   = cor;        
		}
    }
    catch(err)
    {
        document.write(err.description);
    }    
}

/* CADASTRO */
function validaCPF(cpf) 
{
	var retorno = true;
	

	if(cpf == "")
	{
		retorno = false;
	}
	else
	{
		cpf = cpf.replace(".", "");
		cpf = cpf.replace(".", "");
		cpf = cpf.replace(".", "");
		cpf = cpf.replace(",", "");
		cpf = cpf.replace("-", "");
		cpf = cpf.replace(" ", "");

		erro = new String;
		if (cpf.length < 11) erro += "Sao necessarios 11 dígitos para verificação do CPF! \n\n"; 
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) erro += "A verificação de CPF suporta apenas números! \n\n"; 
		
		if(cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
		{
				erro += "Número de CPF inválido!"
		}
	
		var a = [];
		var b = new Number;
		var c = 11;
		
		for (i=0; i<11; i++)
		{
				a[i] = cpf.charAt(i);
				if (i < 9) b += (a[i] * --c);
		}
		
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		
		b = 0;
		c = 11;
		
		for (y=0; y<10; y++) b += (a[y] * c--); 
		
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]))
		{
				erro +="Dígito verificador com problema!";
		}
		
		if (erro.length > 0)
		{
			//alert("Numero de CPF invalido!");
			retorno = false;
		}
	}    

	return retorno;
}


function validaCNPJ(cnpj)
{
	var retorno = true;
	
	CNPJ = Trim(cnpj);
	
	erro = new String;
	
	/*
	if (CNPJ.length < 18) erro += "É necessario preencher corretamente o número do CNPJ! "; 
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
	if (erro.length == 0) erro += "É necessário preencher corretamente o número do CNPJ! ";
	}
	//substituir os caracteres que não são números
	if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CNPJ.substring(0,2);
			x += CNPJ. substring (3,6);
			x += CNPJ. substring (7,10);
			x += CNPJ. substring (11,15);
			x += CNPJ. substring (16,18);
			CNPJ = x; 
	} else {
			CNPJ = CNPJ. replace (".","");
			CNPJ = CNPJ. replace (".","");
			CNPJ = CNPJ. replace ("-","");
			CNPJ = CNPJ. replace ("/","");
	}
	*/
	
	CNPJ = CNPJ. replace (".","");
	CNPJ = CNPJ. replace (".","");
	CNPJ = CNPJ. replace ("-","");
	CNPJ = CNPJ. replace ("/","");
			
			
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) erro += "A verificação de CNPJ suporta apenas números! "; 
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++)
	{
			a[i] = CNPJ.charAt(i);
			b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
			b += (a[y] * c[y]); 
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
			erro +="Dígito verificador com problema!";
	}
	
	if (erro.length > 0)
	{
		//alert("Numero de CNPJ invalido!");
		retorno = false;
	}
				
    return retorno;				
}

function ValidaSenha(senha)
{
	var retorno = true;

	var filter=/((\w)|([@#$%^&+=])){6,}$/i; 
		
	if (!filter.test(senha)) 
	{
		retorno = false;
	}

	return retorno;
}


function Mascara(src, mask) 
{
     var i = src.value.length;
     var saida = mask.substring(0,1);
     var texto = mask.substring(i)
     if (texto.substring(0,1) != saida) 
     {
          src.value += texto.substring (0,1);
     }
}

function ValidaData(dia, mes, ano)
{
	retorno = true;
		
	/* foi encontrado um bug no javascript (IE 6 e Firefox 2) na execucção
	   de parseInt com a string '08' retornando 0.   */
    
	dia = Number(dia);
	mes = Number(mes);
	ano = Number(ano);    

	if(Trim(dia.toString()) == 'NaN' || Trim(mes.toString()) == 'NaN' || Trim(ano.toString()) == 'NaN')
	{
		return false;
	}
	
	if(ano < 1900)
	{
		return false;
	}
	
	switch (mes) 
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
        {
            if  (dia > 31) 
            {
                retorno = false;
            }
            break
        }
        
        case 4:        
        case 6:
        case 9:
        case 11:
        {
            if  (dia > 30) 
            {
            
                retorno = false;
            }
            break
        }
        case 2:
        {
			var bissexto = 0;
            /* Validando ano Bissexto / fevereiro / dia */
            if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
            { 
                bissexto = 1; 
            } 
            if ((bissexto == 1) && (dia > 29)) 
            { 
                retorno = false;;                 
            } 
            if ((bissexto != 1) && (dia > 28)) 
            { 
                retorno = false; 
            }            
            break                      
		}
		default:
		{
			retorno = false;
			break;
		}
    }

   	return retorno;
}


function ColocaFocoEm(NomeObj, e) 
{ 
	try
	{
		var key; 
		if(window.event) 
		{ 
			key = event.keyCode; 
		}
		else
		{ 
			key = e.which; 
		}
		if(key == 13)
		{
			document.getElementById(NomeObj).focus();
			event.keyCode = 0;
		}
	} 
	catch(err)
	{
		alert(err.description);
	}
	
	return false;
} 

/* Ajax */
function CarregaEndereco(campo)
{
	var CEP = campo.replace("-","");
	TrocaImagem("loading", "visible");
	
	if(Number(CEP).toString() != 'NaN')
	{
		formcadastro.CarregaEndereco(CEP, CarregaEndereco_CallBack);
	}
}

function CarregaEndereco_CallBack(response)
{
	if(!response.error)
	{
		var retorno = response.value;
		
		if(retorno[0])
		{
			var Cep = document.getElementById(retorno[5] + "txtCep").value;
			
			if(Cep.indexOf("-") == -1)
			{
				Cep = Cep.substr(0,5) + "-" + Cep.substr(5,3);
			}
			
			document.getElementById(retorno[5] + "txtCep").value		= Cep;
			document.getElementById(retorno[5] + "txtEndereco").value	= retorno[1];
			document.getElementById(retorno[5] + "txtBairro").value		= retorno[2];
			document.getElementById(retorno[5] + "txtCidade").value		= retorno[3];			
			
			document.getElementById(retorno[5] + "ddlEstado").value		= retorno[4];
			
			document.getElementById(retorno[5] + "txtCidade").disabled = true;			
			document.getElementById(retorno[5] + "ddlEstado").disabled = true;
		}
		else
		{
			document.getElementById(retorno[2] + "txtEndereco").value	= "";
			document.getElementById(retorno[2] + "txtBairro").value		= "";
			document.getElementById(retorno[2] + "txtCidade").value		= "";
			document.getElementById(retorno[2] + "ddlEstado").value		= "SP";

			document.getElementById(retorno[2] + "txtCidade").disabled = false;				
			document.getElementById(retorno[2] + "ddlEstado").disabled = false;
		}
	}
	else
	{
		alert(response.error);
	}
		
	TrocaImagem("loading", "hidden");
}

function RedirectAlterarSenha(URL)
{
	document.location.href = URL + "/senhaalteracao.aspx";
}

function Redireciona(URL)
{
	document.location.href = URL;
}

