﻿// JScript File

// Clicar no hyperlink dos últimos imóveis
function ClicarHypperlinkUltimosImoveis(v_numero_controle)
{
    v_numero_controle = v_numero_controle - 1;
    document.getElementById('ctl00_ContentPlaceHolder1_rep_UltimosEmpreendimentos_ctl0' + v_numero_controle + '_hplMaisDetalhes').click();
} 


// Clicar no hyperlink das últimas notícias
function ClicarHypperlinkUltimasNoticias(v_numero_controle)
{
    v_numero_controle = v_numero_controle - 6;
    document.getElementById('ctl00_ContentPlaceHolder1_rep_Midia_ctl0' + v_numero_controle + '_hplSaibaMais').click();
} 

// PERMITE QUE SOMENTE ALGARISMOS INTEIROS SEJAM DIGITADOS
function PermitirInteiros(e)
{
	if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
	
	if (tecla > 47 && tecla <= 57) // numeros de 0 a 9
	{
		return true;
    }
	else
	{
		if (tecla != 8 && tecla != 46) // backspace
		{
			event.keyCode = 0;
			//return false;
		}
		else
		{
			return false;
		}
	}
}

// VALIDA EMAIL
function ValidarEmail(mail)
{
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    
    if(typeof(mail) == "string")
    {
        if(er.test(mail))
        { 
            return true; 
        }
        else
        {
            alert("Email incorreto");
            return false;
        }
    }
    else if(typeof(mail) == "object")
    {
        if(er.test(mail.value))
        { 
            return true; 
        }
        else
        {
            alert("Email incorreto");
            return false;
        }
    }
    else
    {
        alert("Email incorreto");
        return false;
    }
}

// FORMATA DATA
function FormatarData(v_TextBox, e)
{
    if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
    
    //alert(event.keyCode);
    if (tecla != 8 && tecla != 46) // bacspace ou delete
    {
        if (v_TextBox.value.length == 2 || v_TextBox.value.length == 5)
        {
            v_TextBox.value = v_TextBox.value + "/";
        }
    }
}

// FORMATA DATA
function FormatarDataMesAno(v_TextBox, e)
{
    if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
    
    //alert(event.keyCode);
    if (tecla != 8 && tecla != 46) // bacspace ou delete
    {
        if (v_TextBox.value.length == 2)
        {
            v_TextBox.value = v_TextBox.value + "/";
        }
    }
}

// FORMATA O NÚMERO DE TELEFONE COM UM TRAÇO NO QUARTO DÍGITO
function FormatarTelefone(v_TextBox, e)
{
    if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
    
    //alert(event.keyCode);
    if (tecla != 8 && tecla != 46) // bacspace ou delete
    {
        if (v_TextBox.value.length == 4)
        {
            v_TextBox.value = v_TextBox.value + "-";
        }
    }
}

// FORMATA O NÚMERO DE TELEFONE PARA (99)9999-9999
function FormatarTelefoneCompleto(v_TextBox, e)
{
    if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
    
    //alert(tecla);
    if (tecla != 8 && tecla != 46) // bacspace ou delete
    {
        if (v_TextBox.value.length == 1)
        {
            v_TextBox.value = "(" + v_TextBox.value;
        }
        else if (v_TextBox.value.length == 3)
        {
            v_TextBox.value = v_TextBox.value + ")";
        }
        else if (v_TextBox.value.length == 8)
        {
            v_TextBox.value = v_TextBox.value + "-";
        }
    }
}

// FORMATA O CEP COM UM TRAÇO NO QUARTO DÍGITO
function FormatarCEP(v_TextBox, e)
{
    if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
    
    //alert(event.keyCode);
    if (tecla != 8 && tecla != 46) // bacspace ou delete
    {
        if (v_TextBox.value.length == 5)
        {
            v_TextBox.value = v_TextBox.value + "-";
        }
    }
}

// FORMATA O NÚMERO DE CPF COM PONTOS E TRAÇO
function FormatarCPF(v_TextBox, e)
{
    if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if(document.layers) // Nestcape
    {
		var tecla = e.which;
	}
    
    //alert(event.keyCode);
    if (tecla != 8 && tecla != 46) // bacspace ou delete
    {
        if (v_TextBox.value.length == 3 || v_TextBox.value.length == 7)
        {
            v_TextBox.value = v_TextBox.value + ".";
        }
        else if (v_TextBox.value.length == 11)
        {
            v_TextBox.value = v_TextBox.value + "-";
        }
    }
}


function AlterarCheckBoxBeneficios(v_CheckBox)
{
    v = v_CheckBox.id;
    alert(v);
    v = replace('chkBeneficiosOutros', 'txtBeneficiosOutros');
    alert(v);
    
    if (v_CheckBox.checked)
    {
        document.getElementById(v).disabled = true;
        document.getElementById(v).value = '';
    }
    else
    {
        document.getElementById(v).disabled = false;
    }
}


// ADICIONA AO TRAFFIC
function hTrack(v_titulo) 
{
	jQuery.get("traffic/track.asp", 
		  { mtpt: unescape(v_titulo), 
		  	mtr: unescape(document.referrer), 
			mtt: '2', 
			mts: window.screen.width + 'x' + window.screen.height, 
			mti: '1', 
			mtz: Math.random() } );
}

// Mostra o loading
function MostrarLoad()
{
    document.getElementById('div_Fundo').style.display='block';
    document.getElementById('div_BoxLoad').style.display='block';
}

// Verifica número máximo de caracteres de textbox
function checkMaxLen(txt,maxLen) 
{
    try
    {
        if(txt.value.length > (maxLen-1)) 
        {
            var cont = txt.value;
            txt.value = cont.substring(0,(maxLen -1));
            return false;
        }
    }
    catch(e)
    {
    }
}

// Fecha div mensagem geral
function FecharMensagemGeral()
{
    document.getElementById('ctl00_div_MensagemGeral').style.display='none';
    //ctl00_div_MensagemGeral
}

// Formata moeda
function FormatarMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e)
{
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;    

    // 13=enter, 8=backspace as demais retornam 0(zero)
    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    

    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
        return true;

    key = String.fromCharCode(whichCode); // Valor para o código da Chave

    if (strCheck.indexOf(key) == -1) 
        return false; // Chave inválida

    len = objTextBox.value.length;

    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) 
               break;

    aux = '';

    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
               aux += objTextBox.value.charAt(i);

    aux += key;

    len = aux.length;

    if (len == 0) 
        objTextBox.value = '';

    if (len == 1) 
        objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;

    if (len == 2) 
        objTextBox.value = '0'+ SeparadorDecimal + aux;

    if (len > 2) 
    {
        aux2 = '';

        for (j = 0, i = len - 3; i >= 0; i--) 
        {
            if (j == 3) 
            {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }

        objTextBox.value = '';
        len2 = aux2.length;

        for (i = len2 - 1; i >= 0; i--)
               objTextBox.value += aux2.charAt(i);

        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }

    return false;
}
