

function NOME_FUNCAO()
{
    try
    {
        alert('tada');
    }
    catch(err)
    {
        document.write(err.description);
    }
}

function TrocaImagem(name)
{
    var imagem = document.getElementById(name);
    
	if(imagem.style.visibility == "hidden")
	{
		imagem.style.visibility = "visible";
	}
	else
	{
		imagem.style.visibility = "hidden";
	}
}

function Mostra_ddlEstado()
{
    try
    {
        var ddlPais     = document.getElementById('ddlCtPais');
        var ddlEstado   = document.getElementById('ddlCtEstado');
        var lblEstado   = document.getElementById('lblCtEstado');
        
        if(ddlPais.value == '31')
        {
            ddlEstado.style.visibility = 'visible';
            lblEstado.style.visibility = 'visible';
        }
        else
        {
            ddlEstado.style.visibility = 'hidden';    
            lblEstado.style.visibility = 'hidden';
        }
    }
    catch(err)
    {
        document.write(err.description);
    }
}

function CamposPreenchidos()
{
   retorno = true;
    
    try
    {
        var CorErro                 = '#FF0000';
        var CorOk                   = '#666666';
        
        var rdnPf                   = document.getElementById('rdnPf');
        var rdnPj                   = document.getElementById('rdnPj');
        
        var txtNome                 = document.getElementById('txtNome');
        var txtSobrenome            = document.getElementById('txtSobrenome');
        var txtCpf                  = document.getElementById('txtCpf'); 
        
        var txtRazaoSocial          = document.getElementById('txtRazaoSocial');
        var txtCnpj                 = document.getElementById('txtCnpj');
        var txtInscEstadual         = document.getElementById('txtInscEstadual');
        var txtRefContato           = document.getElementById('txtRefContato');
        
        var txtCtEndereco           = document.getElementById('txtCtEndereco');
        var txtCtNumero             = document.getElementById('txtCtNumero');
        var txtCtBairro             = document.getElementById('txtCtBairro');
        var txtCtCidade             = document.getElementById('txtCtCidade');
        var txtCtCep                = document.getElementById('txtCtCep');
        var txtCtTelefoneDdd        = document.getElementById('txtCtTelefoneDdd');
        var txtCtTelefone           = document.getElementById('txtCtTelefone');
        
        var txtCbEndereco           = document.getElementById('txtCbEndereco');
        var txtCbNumero             = document.getElementById('txtCbNumero');
        var txtCbBairro             = document.getElementById('txtCbBairro');
        var txtCbCidade             = document.getElementById('txtCbCidade');
        var txtCbCep                = document.getElementById('txtCbCep');
        var txtCbTelefoneDdd        = document.getElementById('txtCbTelefoneDdd');
        var txtCbTelefone           = document.getElementById('txtCbTelefone');        
        
        var txtUsuEmail1            = document.getElementById('txtUsuEmail1');
        var txtUsuSenha1            = document.getElementById('txtUsuSenha1');
        var txtUsuSenhaConfirme1    = document.getElementById('txtUsuSenhaConfirme1');
        
        var txtUsuEmail1            = document.getElementById('txtUsuEmail1');
        var txtUsuSenha1            = document.getElementById('txtUsuSenha1');
        var txtUsuSenhaConfirme1    = document.getElementById('txtUsuSenhaConfirme1'); 
        
        if(rdnPf.checked)
        {   
            if(Trim(txtNome.value) == '')           {MudaCorBorda(txtNome, CorErro);                retorno = false;} else MudaCorBorda(txtNome, CorOk);           
            if(Trim(txtSobrenome.value) == '')      {MudaCorBorda(txtSobrenome, CorErro);           retorno = false;} else MudaCorBorda(txtSobrenome, CorOk);           
            
            if(Trim(txtCpf.value) == '' || !validaCPF(Trim(txtCpf.value)))            
            {
				MudaCorBorda(txtCpf, CorErro);                 
				
				retorno = false;
			} 
			else
				MudaCorBorda(txtCpf, CorOk);  
            
            MudaCorBorda(txtRazaoSocial, CorOk);   
            MudaCorBorda(txtCnpj, CorOk);          
            MudaCorBorda(txtInscEstadual, CorOk);  
            MudaCorBorda(txtRefContato, CorOk);
            
            MudaCorBorda(txtCbEndereco, CorOk);   
            MudaCorBorda(txtCbNumero, CorOk);          
            MudaCorBorda(txtCbBairro, CorOk);  
            MudaCorBorda(txtCbCidade, CorOk);
            MudaCorBorda(txtCbCep, CorOk);   
            MudaCorBorda(txtCbTelefoneDdd, CorOk);          
            MudaCorBorda(txtCbTelefone, CorOk);                       
        }
        else if(rdnPj.checked)
        {
            if(Trim(txtRazaoSocial.value) == '')    {MudaCorBorda(txtRazaoSocial, CorErro);         retorno = false;} else MudaCorBorda(txtRazaoSocial, CorOk);           
            if(Trim(txtInscEstadual.value) == '')   {MudaCorBorda(txtInscEstadual, CorErro);        retorno = false;} else MudaCorBorda(txtInscEstadual, CorOk);           
            if(Trim(txtRefContato.value) == '')     {MudaCorBorda(txtRefContato, CorErro);          retorno = false;} else MudaCorBorda(txtRefContato, CorOk);           

            if(Trim(txtCnpj.value) == '' || !validaCNPJ(Trim(txtCnpj.value)))           
            {
				MudaCorBorda(txtCnpj, CorErro);                
				
				retorno = false;
			} 
			else 
				MudaCorBorda(txtCnpj, CorOk); 
				            
			if(Trim(txtCbEndereco.value) == '')     {MudaCorBorda(txtCbEndereco, CorErro);          retorno = false;} else MudaCorBorda(txtCbEndereco, CorOk);           
			if(Trim(txtCbNumero.value) == '')       {MudaCorBorda(txtCbNumero, CorErro);            retorno = false;} else MudaCorBorda(txtCbNumero, CorOk);           
			if(Trim(txtCbBairro.value) == '')       {MudaCorBorda(txtCbBairro, CorErro);            retorno = false;} else MudaCorBorda(txtCbBairro, CorOk);           
			if(Trim(txtCbCidade.value) == '')       {MudaCorBorda(txtCbCidade, CorErro);            retorno = false;} else MudaCorBorda(txtCbCidade, CorOk); 
			if(Trim(txtCbCep.value) == '')          {MudaCorBorda(txtCbCep, CorErro);               retorno = false;} else MudaCorBorda(txtCbCep, CorOk);           
			if(Trim(txtCbTelefoneDdd.value) == '')  {MudaCorBorda(txtCbTelefoneDdd, CorErro);       retorno = false;} else MudaCorBorda(txtCbTelefoneDdd, CorOk);           
			if(Trim(txtCbTelefone.value) == '')		{MudaCorBorda(txtCbTelefone, CorErro);			retorno = false;} else MudaCorBorda(txtCbTelefone, CorOk);           

            MudaCorBorda(txtNome, CorOk);      
            MudaCorBorda(txtSobrenome, CorOk); 
            MudaCorBorda(txtCpf, CorOk);
        } 
        
        if(Trim(txtCtEndereco.value) == '')         {MudaCorBorda(txtCtEndereco, CorErro);          retorno = false;} else MudaCorBorda(txtCtEndereco, CorOk);           
        if(Trim(txtCtNumero.value) == '')           {MudaCorBorda(txtCtNumero, CorErro);            retorno = false;} else MudaCorBorda(txtCtNumero, CorOk);           
        if(Trim(txtCtBairro.value) == '')           {MudaCorBorda(txtCtBairro, CorErro);            retorno = false;} else MudaCorBorda(txtCtBairro, CorOk);           
        if(Trim(txtCtCidade.value) == '')           {MudaCorBorda(txtCtCidade, CorErro);            retorno = false;} else MudaCorBorda(txtCtCidade, CorOk); 
        if(Trim(txtCtCep.value) == '')              {MudaCorBorda(txtCtCep, CorErro);               retorno = false;} else MudaCorBorda(txtCtCep, CorOk);           
        if(Trim(txtCtTelefoneDdd.value) == '')      {MudaCorBorda(txtCtTelefoneDdd, CorErro);       retorno = false;} else MudaCorBorda(txtCtTelefoneDdd, CorOk);           
        if(Trim(txtCtTelefone.value) == '')			{MudaCorBorda(txtCtTelefone, CorErro);			retorno = false;} else MudaCorBorda(txtCtTelefone, CorOk);           
        if(Trim(txtUsuEmail1.value) == '')          {MudaCorBorda(txtUsuEmail1, CorErro);           retorno = false;} else MudaCorBorda(txtUsuEmail1, CorOk);           
        
        if(Trim(txtUsuSenha1.value) == '')          {MudaCorBorda(txtUsuSenha1, CorErro);           retorno = false;} else MudaCorBorda(txtUsuSenha1, CorOk);                    
        if(Trim(txtUsuSenhaConfirme1.value) == '')  {MudaCorBorda(txtUsuSenhaConfirme1, CorErro);   retorno = false;} else MudaCorBorda(txtUsuSenhaConfirme1, CorOk);


        if(!retorno)
        {
            alert('Preencha os campos corretamente!');
        }                                             
    }
    catch(err)
    {
        document.write(err.description);
    }
    
    return retorno;          
}

function CamposPreenchidosEdicao()
{
    retorno = true;
    
    try
    {
        var CorErro                 = '#FF0000';
        var CorOk                   = '#666666';
        
        var rdnPf                   = document.getElementById('rdnPf');
        var rdnPj                   = document.getElementById('rdnPj');
        
        var txtNome                 = document.getElementById('txtNome');
        var txtSobrenome            = document.getElementById('txtSobrenome');
        var txtCpf                  = document.getElementById('txtCpf'); 
        
        var txtRazaoSocial          = document.getElementById('txtRazaoSocial');
        var txtCnpj                 = document.getElementById('txtCnpj');
        var txtInscEstadual         = document.getElementById('txtInscEstadual');
        var txtRefContato           = document.getElementById('txtRefContato');
        
        var txtUsuEmail1            = document.getElementById('txtUsuEmail1');
        var txtUsuSenha1            = document.getElementById('txtUsuSenha1');
        var txtUsuSenhaConfirme1    = document.getElementById('txtUsuSenhaConfirme1');
        
        var txtUsuEmail1            = document.getElementById('txtUsuEmail1');
        var txtUsuSenha1            = document.getElementById('txtUsuSenha1');
        var txtUsuSenhaConfirme1    = document.getElementById('txtUsuSenhaConfirme1'); 
        
        if(rdnPf.checked)
        {   
            if(Trim(txtNome.value) == '')           {MudaCorBorda(txtNome, CorErro);                retorno = false;} else MudaCorBorda(txtNome, CorOk);           
            if(Trim(txtSobrenome.value) == '')      {MudaCorBorda(txtSobrenome, CorErro);           retorno = false;} else MudaCorBorda(txtSobrenome, CorOk);           
            
            if(Trim(txtCpf.value) == '' || !validaCPF(Trim(txtCpf.value)))            
            {
				MudaCorBorda(txtCpf, CorErro);                 
				
				retorno = false;
			} 
			else
				MudaCorBorda(txtCpf, CorOk);           
            
            MudaCorBorda(txtRazaoSocial, CorOk);   
            MudaCorBorda(txtCnpj, CorOk);          
            MudaCorBorda(txtInscEstadual, CorOk);  
            MudaCorBorda(txtRefContato, CorOk);
        }
        else if(rdnPj.checked)
        {
            if(Trim(txtRazaoSocial.value) == '')    {MudaCorBorda(txtRazaoSocial, CorErro);         retorno = false;} else MudaCorBorda(txtRazaoSocial, CorOk);           
            if(Trim(txtInscEstadual.value) == '')   {MudaCorBorda(txtInscEstadual, CorErro);        retorno = false;} else MudaCorBorda(txtInscEstadual, CorOk);           
            if(Trim(txtRefContato.value) == '')     {MudaCorBorda(txtRefContato, CorErro);          retorno = false;} else MudaCorBorda(txtRefContato, CorOk);           
            
                    
            if(Trim(txtCnpj.value) == '' || !validaCNPJ(Trim(txtCnpj.value)))           
            {
				MudaCorBorda(txtCnpj, CorErro);                
				
				retorno = false;
			} 
			else 
				MudaCorBorda(txtCnpj, CorOk);           

            MudaCorBorda(txtNome, CorOk);      
            MudaCorBorda(txtSobrenome, CorOk); 
            MudaCorBorda(txtCpf, CorOk);
        } 
        
        if(Trim(txtUsuEmail1.value) == '')          {MudaCorBorda(txtUsuEmail1, CorErro);           retorno = false;} else MudaCorBorda(txtUsuEmail1, CorOk);           
        

        if(!retorno)
        {
            alert('Preencha os campos corretamente!');
        }                                             
    }
    catch(err)
    {
        document.write(err.description);
    }
    
    return retorno;       
}


function MudaCorBorda(obj, cor)
{
    try
    {        
        var Borda               = '1px';
        var BordaEstilo         = 'solid';
        
        obj.style.border        = Borda;
        obj.style.borderStyle   = BordaEstilo;
        obj.style.borderColor   = cor;        
    }
    catch(err)
    {
        document.write(err.description);
    }    
}

function AlternaPfPj()
{
    try
    {
        var divPf				= document.getElementById('DadosFisica');
        var divPj				= document.getElementById('DadosJuridica');
        var rdnPf				= document.getElementById('rdnPF');
        var rdnPj				= document.getElementById('rdnPJ');

        if(rdnPf.checked)
        {
            divPf.style.display				= 'block';            
            divPj.style.display				= 'none';
        }
        else if(rdnPj.checked)
        {
            divPf.style.display 			= 'none';            
            divPj.style.display 			= 'block';
        }
    }
    catch(err)
    {
        document.write(err.description);
    }
}



function AlternaPfPj2()
{
    try
    {
        var divPf			= document.getElementById('DadosFisica');
        var divPj			= document.getElementById('DadosJuridica');
        
        var rdnPf			= document.getElementById('rdnPf');
        var rdnPj			= document.getElementById('rdnPj');
        
        var txtRazaoSocial  = document.getElementById('txtRazaoSocial');
        var txtCnpj         = document.getElementById('txtCnpj');
        var txtInscEstadual = document.getElementById('txtInscEstadual');
        var txtRefContato   = document.getElementById('txtRefContato');        
        var ckbIsento       = document.getElementById('ckbIsento');
        
        var txtNome         = document.getElementById('txtNome');
        var txtSobrenome    = document.getElementById('txtSobrenome');
        var txtCpf          = document.getElementById('txtCpf');
        var txtRg           = document.getElementById('txtRg');
        var txtNascDia      = document.getElementById('txtNascDia');
        var txtNascMes      = document.getElementById('txtNascMes');                
        var txtNascAno      = document.getElementById('txtNascAno');                
        
        

        
        if(rdnPf.checked)
        {
            divPf.style.display				= 'block';            
            divPj.style.display				= 'none';
            
            txtRazaoSocial.value    		= 
            txtCnpj.value           		= 
            txtInscEstadual         		=
            txtRefContato           		= 
            ckbIsento               		= '';
        }
        else if(rdnPj.checked)
        {
            divPf.style.display 			= 'none';            
            divPj.style.display 			= 'block';
            
            txtNome         				=
            txtSobrenome    				=
            txtCpf          				=
            txtRg           				=
            txtNascDia      				=
            txtNascMes      				=
            txtNascAno      				= '';
            
        }
        else
        {
            divPf.style.display 			= 'none';            
            divPj.style.display 			= 'none';
        }
    }
    catch(err)
    {
        document.write(err.description);
    }
}




/* CADASTRO */
function validaCPF(cpf) 
{
	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 digitos para verificacao do CPF! \n\n"; 
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n"; 
		
		if(cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
		{
				erro += "Numero de CPF invalido!"
		}
	
		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 +="Digito 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 ("/","");
	}
	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 ValidaEmail(email)
{
	var retorno = true;

    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Trim(email)))) 
	{ 
		retorno = false;
	} 
	
	return retorno;
}

function CopiaEndereco()
{
    if(document.getElementById('rdnPJ').checked)
    {
		if(document.getElementById('ckbMesmo').checked)
		{
			document.getElementById('txtCbEndereco').value          = document.getElementById('txtCtEndereco').value;
			document.getElementById('txtCbNumero').value            = document.getElementById('txtCtNumero').value;
			document.getElementById('txtCbComplemento').value       = document.getElementById('txtCtComplemento').value;
			document.getElementById('txtCbBairro').value            = document.getElementById('txtCtBairro').value;
			document.getElementById('txtCbCidade').value            = document.getElementById('txtCtCidade').value;
			document.getElementById('ddlCbEstado').selectedIndex    = document.getElementById('ddlCtEstado').selectedIndex;
			document.getElementById('ddlCbPais').selectedIndex      = document.getElementById('ddlCtPais').selectedIndex;
			document.getElementById('txtCbCep').value               = document.getElementById('txtCtCep').value;
			document.getElementById('txtCbTelefoneDdd').value       = document.getElementById('txtCtTelefoneDdd').value; 
			document.getElementById('txtCbTelefone').value          = document.getElementById('txtCtTelefone').value;         
			document.getElementById('txtCbFaxDdd').value            = document.getElementById('txtCtFaxDdd').value; 
			document.getElementById('txtCbFax').value               = document.getElementById('txtCtFax').value; 
			document.getElementById('txtCbCelDdd').value            = document.getElementById('txtCtCelDdd').value; 
			document.getElementById('txtCbCel').value               = document.getElementById('txtCtCel').value; 			
		}
		else
		{
			document.getElementById('txtCbEndereco').value          = '';
			document.getElementById('txtCbNumero').value            = '';
			document.getElementById('txtCbComplemento').value       = '';            
			document.getElementById('txtCbBairro').value            = '';
			document.getElementById('txtCbCidade').value            = '';        
			document.getElementById('ddlCbEstado').selectedIndex    = 25;
			document.getElementById('ddlCbPais').selectedIndex      = 30;
			document.getElementById('txtCbCep').value               = '';
			document.getElementById('txtCbTelefoneDdd').value       = ''; 
			document.getElementById('txtCbTelefone').value          = '';           
			document.getElementById('txtCbFaxDdd').value            = '';    
			document.getElementById('txtCbFax').value               = '';  
			document.getElementById('txtCbCelDdd').value            = '';    
			document.getElementById('txtCbCel').value               = '';         			       
		}
	}
}

function Trim(STRING)
{
    STRING = LTrim(STRING);
    
    return RTrim(STRING);
}   

function RTrim(STRING)
{
    while(STRING.charAt((STRING.length -1))==" ")
    {
        STRING = STRING.substring(0,STRING.length-1);
    }
    
    return STRING;
}


function LTrim(STRING)
{
    while(STRING.charAt(0)==" ")
    {
        STRING = STRING.replace(STRING.charAt(0),"");
    }
    
    return STRING;
}


function MostraFormaPagamento(FormaPgto)
{
    try
    {
	    var divCartao				= document.getElementById('div_CartaoDeCredito');
        var divDeposito				= document.getElementById('div_DepositoBancario');
        var divBoleto				= document.getElementById('div_BoletoBancario');
       
        if(FormaPgto == "1")
        {
	        divCartao.style.display				= 'block';            
            divDeposito.style.display			= 'none';
            divBoleto.style.display				= 'none';
        }
        else if(FormaPgto == "2")
        {
	        divCartao.style.display				= 'none';            
            divDeposito.style.display			= 'block';
            divBoleto.style.display				= 'none';
        }
        else if(FormaPgto == "3")
        {
	        divCartao.style.display				= 'none';            
            divDeposito.style.display			= 'none';
            divBoleto.style.display				= 'block';
        }
    }
    catch(err)
    {
        document.write(err.description);
    }
}

