/*********************************************************/
/* Este arquivo contém funções para inserir máscaras     */
/* nos campos de CPF, datas, CEP e telefones             */
/*********************************************************/

/*********************************************************/
/* Funcao para inserir máscara no campo de CPF no        */
/* formato 999.999.999-99                                */
/* Parametros: objetoCpf                                 */
/* Retorna: false se o CPF contiver no. de dígitos       */
/* incorreto, senão insere a máscara                     */
/*********************************************************/
function openWindow(theURL,winName,features)
{ //v2.0
  window.open(theURL,winName,features);
}

function redireciona(pagina)
{
	window.location=pagina;
}

function mascara_cpf(objetoCpf)
{
	campo_cpf = eval (objetoCpf);

	if (campo_cpf.value.length == 0 || campo_cpf.value.length == 14)
	{
		return true;
	}

	if (campo_cpf.value.length == 11)
	{
		strParte1Cpf = campo_cpf.value.substring(0,3)
		strParte2Cpf = campo_cpf.value.substring(3,6)
		strParte3Cpf = campo_cpf.value.substring(6,9)
		strParte4Cpf = campo_cpf.value.substring(9,11)
	
		campo_cpf.value = strParte1Cpf + "." + strParte2Cpf + "." + strParte3Cpf + "-" + strParte4Cpf;
	}
	else
	{
		alert("CPF inválido! Digite novamente.");
		objetoCpf.select();
		return false;
	}
}


/*********************************************************/
/* Funcao para inserir máscara nos campos de data        */
/* no formato dd/mm/aaaa                                 */
/* Parametros: objetoData                                */
/* Retorna: false se a data contiver no. de dígitos      */
/* incorreto, senão insere a máscara                     */
/*********************************************************/
function mascara_data(objetoData)
{
	campo_data = eval (objetoData);
	
	if (campo_data.value.length == 0 || campo_data.value.length == 10)
	{
		return true;
	}	

	if (campo_data.value.length == 8)
	{
		strParte1Data = campo_data.value.substring(0,2)
		strParte2Data = campo_data.value.substring(2,4)
		strParte3Data = campo_data.value.substring(4,8)
	
		campo_data.value = strParte1Data + "/" + strParte2Data + "/" + strParte3Data;
	}
	else
	{
		alert("Digite corretamente a data no formato ddmmaaaa.");
		objetoData.select();	
		return false;	
	}
}

/*********************************************************/
/* Funcao para inserir máscara no campo de CEP no        */
/* formato 99999-999                                     */
/* Parametros: objetoCep                                 */
/* Retorna: false se o CEP contiver no. de dígitos       */
/* incorreto, senão insere a máscara                     */
/*********************************************************/
function mascara_cep(objetoCep)
{
	campo_cep = eval (objetoCep);

	if (campo_cep.value.length == 8)
	{
		strParte1Cep = campo_cep.value.substring(0,5)
		strParte2Cep = campo_cep.value.substring(5,8)
	
		campo_cep.value = strParte1Cep + "-" + strParte2Cep;
	}
	else
	{
		alert("C.E.P inválido! Digite novamente.");
		objetoCep.select();
		return false;
	}
}

/*********************************************************/
/* Funcao para inserir máscara nos campos de CPF telefone*/
/* no formato (99)9999-9999 ou (99)999-9999              */
/* Parametros: strTel                                    */
/* Retorna: false se o telefone contiver no. de dígitos  */
/* incorreto, senão insere a máscara                     */
/*********************************************************/
function mascara_tel(strTel)
{
	campo_tel = eval (strTel);
	//nomeCampo = eval (nomeCampo1);
	
	if (campo_tel.value.length == 9 || campo_tel.value.length == 10)
	{
		strParte1Tel = campo_tel.value.substring(0,2)
		strParte2Tel = campo_tel.value.substring(2,10)
	
		campo_tel.value = "(" + strParte1Tel + ")" + strParte2Tel;	

	}
	else if (campo_tel.value.length != 0)
	{
		if (campo_tel.name == "strTelRes")
		{
			alert('Por favor digite corretamente o campo Telefone Residencial!');
			strTel.select();
			return false;
		}
		if (campo_tel.name == "strTelCom")
		{
			alert('Por favor digite corretamente o campo Telefone Comercial!')
			strTel.select();	
			return false;	
		}			
		if (campo_tel.name == "strTelCel")
		{
			alert('Por favor digite corretamente o campo Telefone Celular!')
			strTel.select();	
			return false;			
		}	
		if (campo_tel.name == "strTel")
		{
			alert('Por favor digite corretamente o campo Telefone!')
			strTel.select();	
			return false;			
		}			
	}
}


//Arruma CNPJ ao digitar

  function FormataCNPJ(Formulario, Campo, TeclaPres)
  {
    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 14;
 
    eval("strCampo = document." + Formulario + "." + Campo);
 
    vr = strCampo.value;
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    vr = vr.replace(",", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
      tam = tam - 1;
    }

    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105)
    {
      if (tam <= 2)
      {
        strCampo.value = vr;
      }
       if ((tam > 2) && (tam <= 6))
       {
         strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 7) && (tam <= 9))
       {
         strCampo.value = vr.substr(0, tam - 6) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
      }
       if ((tam >= 10) && (tam <= 12))
       {
         strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
      }
       if ((tam >= 13) && (tam <= 14))
       {
         strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
      }
       if ((tam >= 15) && (tam <= 17))
       {
         strCampo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + '-' + vr.substr(tam - 2, tam);
      }
    }
  }