﻿//JavaScript

function mostrasombra(objeto)
{
    var obj = document.getElementById(objeto);
    obj.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=#888888, direction=135, strength=3)";
    obj.style.position = "absolute";         
}

function ApenasNumero(obj, ev){
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( x<31 && x!=13 ) return true;
	if (x>95 && x<106) return true;
	if(x<48 || x>57){
		return false;
	}
}
 
function ApenasValor(obj, ev){
    var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( (x==44 || x==45 || x==46 || x<31) && x!=13 ) return true;
	if(x<48 || x>57){
		return false;	
	}
}

function ApenasFone(obj, ev){
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( x<31 && x!=13 ) return true;
	if(x!=32 && (x<40 || x>57 && x<96)){
		return false;
	}
	if (obj.value.length==2) obj.value +='-';
}

function ApenasEmail(obj, ev){
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( x<31 && x!=13 ) return true;
	if((x<37 || x>39) && x!=45 && x!=46 && (x<48 || x>57) && (x<64 || x>90) && x!=95 && (x<97 || x>122)){
		return false;
	}
}

function ApenasLetra(obj, ev) {
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( x<31 && x!=13 ) return true;
	if (x!=39 && x!=32 && (x<65 || x>90) && (x<97 || x>122) && x<128){
		return false;
	}
}

function BloqueiaEnter(obj, ev){
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if(x == 13){
		return false;
	}
}

function AcionaBotaoComEnter(obj, ev){
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if(x == 13){
		document.getElementById(obj).click();
        return false;		
	}
}

function ApenasCPF(obj, ev)
{
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( x<31 && x!=13) return true;
	if(x<48 || x>57 && x<96){
		return false;
	}
    if (obj.value.length == 3) obj.value += '.';
    if (obj.value.length == 7) obj.value += '.';
    if (obj.value.length == 11) obj.value += '-';
}
function VerificaCPF(cpf) 
{
    if (vercpf(cpf)) 
    {
        return true;
    }
    else 
    {
        return false;
    }
}
function vercpf(cpf) 
    {
        if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
            return false;
        add = 0;
        for (i=0; i < 9; i ++)
            add += parseInt(cpf.charAt(i)) * (10 - i);
        rev = 11 - (add % 11);
        if (rev == 10 || rev == 11)
            rev = 0;
        if (rev != parseInt(cpf.charAt(9)))
            return false;
        add = 0;
        for (i = 0; i < 10; i ++)
            add += parseInt(cpf.charAt(i)) * (11 - i);
        rev = 11 - (add % 11);
        if (rev == 10 || rev == 11)
            rev = 0;
        if (rev != parseInt(cpf.charAt(10)))
            return false;
        //alert('O CPF INFORMADO É VÁLIDO.');
        return true;
}

function ApenasCEP(obj, ev)
{
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if ( x<31 && x!=13 ) return true;
	if(x<48 || x>57 && x<96){
		return false;
	}
    if (obj.value.length == 5) obj.value += '-';
}

// *********************************************
// FUNÇÕES PARA FORMATAÇÃO E VALIDAÇÃO DE DATAS
// *********************************************

function ApenasData(obj, ev) 
{
	var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
	if (x < 31) return true;
	if((x < 47) || (x > 57 && x<96)) 
	    return false;
	else 
	{
		if((x != 8) && (x != 47)) 
		{
			if (obj.value.length == 2) obj.value += '/';
			if (obj.value.length == 5) obj.value += '/'; 
		}
		else if (x == 47) 
		{
			if (obj.value.length == 1) obj.value = '0' + obj.value;
			if (obj.value.length == 4) obj.value = obj.value.substr(0,3) + '0' + obj.value.substr(3,1); 
		} 
	} 
}
function ValidaData(obj, podeVazio)
{
    //var obj = document.getElementById(box);
	CompletaAno(obj);
	if ((podeVazio == false) && (obj.value == ''))
	{
        alert('Preenchimento de data obrigatório!');
        try
        {
            obj.focus(); 
            obj.select();
        }
        catch(er){}
        return false;
    }
    if (isDate(obj.value) == false)
    {
        alert('Data incorreta!');
        try
        {
            obj.value = "";
            obj.focus(); 
            obj.select();
        }
        catch(er){}
        return false;
    }
    return true;
}
function isDate(x)
{
	var dia, mes, ano;
	if (x.length == 0) return true;
	if (x.length < 10) return false;
	mes = x.substr(3,2);
	if (mes > '12' || mes == '00') return false;
	dia = x.substr(0,2);
	if (dia == '00') return false;
	ano = x.substr(6,4);
	if (ano < '0200') return false;
	if (mes == '01' || mes == '03' || mes == '05' || mes == '07' || mes == '08' || mes == '10' || mes == '12')
	{
		if (dia > '31') return false; 
	}
	if (mes == '04' || mes == '06' || mes == '09' || mes =='11')
	{
		if (dia > '30') return false;
	}
	if (mes == '02')
	{
		if (Bissexto(parseInt(ano)) == true)
		{
			if (dia > '29') return false;
		}
		else
			if (dia > '28') return false; 
	}
	return true;
}
function CompletaAno(obj)
{
	var sAux;
	if (obj.value.length == 8) 
	{
		sAux = obj.value.substr(6,2);
		if (sAux >= '25')
		{
			sAux = '19' + sAux; 
		}
		else {
			sAux = '20' + sAux;
		}
		obj.value = obj.value.substr(0,6) + sAux;
	}
}
function Bissexto(ano)
{
    var bissexto = false;
    if( (((ano % 4) == 0) && ((ano % 100) != 0)) || ((ano % 400) == 0) )
        bissexto = true;
	return bissexto;
}


// *********************************************
// FUNÇÕES PARA FORMATAÇÃO E VALIDAÇÃO DE HORAS
// *********************************************


function ApenasHora(obj, ev)
{
    var x = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;  
    if ( x>=48 && x<=58 ) 
    {
        return true;   
    }
    else
    {
        if (obj.value.length == 2) obj.value +=':';    
    }
    if ( x==44 || x==46 ) 
    {
        return true;
    }
    else
    {
        if (obj.value.length == 2) obj.value +=':';
    }
    return false;
}
function ValidaHora(obj, podeVazio) {
	if ((podeVazio == false) && (obj.value == ''))
	{
        alert('Favor preencher o campo!');
        try
        {
            obj.focus(); 
            obj.select();
        }
        catch(er){}
        return false;
    }
    if (sHora != '')
    {
        FormataHora(obj);
        var bFormatoHora = true;
        var bOK = true;
        var sHora = Trim_Hora(obj.value)
        var iPos2p = sHora.indexOf(':');
        var sH = Trim_Hora(sHora.substring(0, iPos2p));
        var sM = Trim_Hora(sHora.substring(iPos2p + 1, sHora.length));
        if (!IsNumeric_Hora(sH) || !IsNumeric_Hora(sM))
        {
            bOK = false;
        }
        else if (bFormatoHora)
        {
            if (parseInt(sH) > 23 || parseInt(sH) < 0)
            {
                bOK = false;
            }
            else if (parseInt(sM) > 59 || parseInt(sM) < 0)
            {
                bOK = false;
            }
        }
        if (!bOK)
        {
            alert('Hora incorreta!');
            try
            {
                obj.value = '';
                obj.focus(); 
                obj.select();
            }
            catch(er){}
            return false;
        }
    }
}
function Trim_Hora(STRING)
{
    STRING = LTrim_Hora(STRING);
    return RTrim_Hora(STRING);
}
function RTrim_Hora(STRING)
{
    while(STRING.charAt((STRING.length -1))==' ')
    {
        STRING = STRING.substring(0,STRING.length-1);
    }
    return STRING;
}
function LTrim_Hora(STRING)
{
    while(STRING.charAt(0)==' ')
    {
        STRING = STRING.replace(STRING.charAt(0),'');
    }
    return STRING;
}
function IsNumeric_Hora(VALUE)
{
    var bOK = true;
    for(var ivA = 0; ivA < VALUE.length;ivA ++)
    {
        if(VALUE.charCodeAt(ivA) < 48 || VALUE.charCodeAt(ivA) > 57)
        {
            if(VALUE.charCodeAt(ivA) != 46 && VALUE.charCodeAt(ivA) != 32 && VALUE.charAt(ivA) != ',')
            {
                bOK = false;
            }
        }
    }
    return bOK;
}
function FormataHora(obj)
{
    var iPos2p;
    var sMesa;
    var sH;
    var sM;
    var sValor = obj.value;
    sMesa = Trim_Hora(sValor);
    if (sMesa.length > 0)
    {
        iPos2p = sMesa.indexOf(':');
        if (iPos2p == -1) iPos2p = sMesa.indexOf('.');
        if (iPos2p == -1) iPos2p = sMesa.indexOf(',');
        sH = sMesa;
        sM = sMesa;
        if (iPos2p > -1)
        {
            sH = Trim_Hora(sH.substring(0, iPos2p));
            sM = Trim_Hora(sM.substring(iPos2p + 1, sM.length));
            if (sM.length == 1) {sM = sM + '0';}
            sMesa = sH + ':' + sM;
            if (iPos2p == 1) {sMesa = '0' + sMesa;}
            if (sM == '') {sMesa = sMesa + '00';}
        }
        else
        {
            if (sMesa.length >= 3)
            {
                sH = sH.substring(0, sH.length - 2);
                sM = sM.substring(sM.length - 2, sM.length);
                sMesa = sH + ':' + sM;
            }
            else
            {
                sMesa = sMesa + ':00';
            }
            iPos2p = sMesa.indexOf(':');
            if (iPos2p == 1) {sMesa = '0' + sMesa};
        }
    }
    obj.value = sMesa;
}
function VerificaReservado(campo)
{
	var txt = document.getElementById(campo).value.toUpperCase();
	var problema = '0';
	if (txt.indexOf('DELETE') >= 0) problema = '1';
	if (txt.indexOf('DROP') >= 0) problema = '1';
	if (txt.indexOf('UPDATE') >= 0) problema = '1';
	if (txt.indexOf('INSERT') >= 0) problema = '1';
	if (txt.indexOf('ALTER') >= 0) problema = '1';
	if (txt.indexOf('CREATE') >= 0) problema = '1';
	if (problema == '1') {
		if(confirm('Comando perigoso detectado.\nDeseja executar?') == false) return false;
	}
}

function ClicaBotao(obj)
{
    document.getElementById(obj).click();
}

function TrocaImg1(obj, caminho)
{
    obj.src = caminho;    
}

function TrocaImg2(obj, caminho)
{
    obj.src = caminho;    
}

function AbrePopup(url, alt, larg)
{   
	window.open(url, "fotos", "height=" + alt + ", width=" + larg + ", resizable=no, scrollbars=yes, status=no, location=no, toolbar=no, menubar=no, top=50, left=50");
}
function AbreLinks()
{
    AbrePopup("../popup/links_site.aspx", "400", "520");
}

function AbreComentarios(sItemId, sTipoReg, sExibeForm, sExibeComents)
{
    AbrePopup("../popup/comentarios.aspx?itemid=" + sItemId + "&tipo=" + sTipoReg + "&form=" + sExibeForm + "&coments=" + sExibeComents, "600", "500");
}

function AbreColorPopup(url)
{
	window.open(url, "corpopup", "height=160, width=200, resizable=no, scrollbars=no, status=no, location=no, toolbar=no, menubar=no, top=200, left=200");
}

function AbrePhotoPopup(url)
{
	window.open(url, "corpopup", "height=588, width=650, resizable=no, scrollbars=no, status=yes, location=no, toolbar=no, menubar=no, top=10, left=10");
}

function RetornaValor(sCampo, valor) 
{
	var obj;
	obj = opener.document.getElementById(sCampo);
	obj.value = "" + valor;
	try { obj.focus() } catch(er){};
	window.close();
}
function RetornaCor(sCampo, valor) 
{
	var obj;
	obj = opener.document.getElementById(sCampo);
	obj.style.backgroundColor = valor;
	obj.value = "" + valor;
	try { obj.focus() } catch(er){};
	window.close();
}
// FORMATA NÚMERO TIPO MOEDA USANDO O EVENTO ONKEYDOWN

function FormataValor2(obj, iMaxDec)
{ 
    //var obj = document.getElementById(box); 
    if (obj.value == '') return true; 
    var posNeg = obj.value.indexOf('-'); 
    var decPart = ''; 
    var i; 
    var newVal = ''; 
    var x = ''; 
    for (i=0; i<obj.value.length; i++) 
    { 
        x = obj.value.substr(i,1);
        if ((x != '.') && (x != '-')) newVal += x; 
    } 
    var iPos = newVal.indexOf(','); 
    if (iPos >= 0 ) 
    { 
        decPart = newVal.substr(iPos+1, iMaxDec); 
    }
    else 
    { 
        iPos = newVal.length+1; 
    }
    for (i=decPart.length; i<iMaxDec; i++) 
    { 
        decPart = decPart + '0'; 
    } 
    newVal = newVal.substr(0, iPos);
    var retorno = ''; 
    var cento = ''; 
    for (i=newVal.length; i>=0; i--) 
    { 
        if (cento.length == 3) 
        { 
            retorno = '.' + retorno; 
            cento = ''; 
        } 
        cento += newVal.substr(i,1);
        retorno = newVal.substr(i,1) + retorno; 
    }
    retorno += ',' + decPart; 
    if (posNeg >= 0) retorno = '-' + retorno; 
    obj.value = retorno;
}

function FormataMoeda(campo,tammax,ev,decimal)
{

    var tecla = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
    vr = Limpar(campo.value,"0123456789");
    tam = vr.length;
    dec=decimal;
    
    alert("teste");
    
    if (tam < tammax && 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 <= dec )
        {
            campo.value = vr;
        }

        if ( (tam > dec) && (tam <= 5) )
        {
            campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam );
        }

        if ( (tam >= 6) && (tam <= 8) )
        {
            campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
        }

        if ( (tam >= 9) && (tam <= 11) )
        {
            campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
        }

        if ( (tam >= 12) && (tam <= 14) )
        {
            campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
        }

        if ( (tam >= 15) && (tam <= 17) )
        {
            campo.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 );
        }

    }

}
function AjustaResolucao(obj)
{
    document.getElementById(obj).style.height = (screen.height - 173) + "px" ;
    document.getElementById("tableprincipal").style.height = (screen.height - 175) + "px" ;
}

function MostraDiv(x, y)
{
    var obj = document.getElementById('divMostra');
    if (obj.style.display == '')
    {
        obj.style.display = 'none';
    }
    else
    {
        obj.style.display = '';
        obj.style.left = (event.clientX  - x) + 'px';
        obj.style.top = (event.clientY - y) + 'px';
        obj.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=#000000, direction=135, strength=3)";
    }
}
function AtualizaPagPai(txt, valor1, lbl, valor2)
{
    opener.document.getElementById(txt).value = valor1;
    opener.document.getElementById(lbl).innerHTML = valor2;
}

//opacity
var idset=0
var valor=0
var obj=0
function BotaOpacity(box, val)
{
    var div = document.getElementById(box);
    div.style.opacity = "0.0";
    div.style.filter = "Alpha(Opacity=0)";
    div.style.display = "";
    obj = box;
    valor = val;
    try
    {
        idset = setTimeout('BotaOpacityAux();',5);
    }
    catch(er)
    {
        idset = null;
    }
}
function BotaOpacityAux()
{
    var div = document.getElementById(obj);
    var opold = 1
    
    if (div.style.opacity > "0.0")
    {  
        opold = div.style.opacity.substr(2,2);  
    }
    opold++;
    opold++;
    if (opold < 10)
    {
        div.style.opacity = "0.0" + opold;   
    }
    else
    {
        div.style.opacity = "0." + opold; 
    } 
    div.style.filter = "Alpha(Opacity=" + opold + ")";
    if (opold < valor)
    {
        try
        {
            idset = setTimeout('BotaOpacityAux();',5);
        }
        catch(er)
        {
            idset = null;
        }
    }
    else
    {
        idset = null;
        obj = null;
        valor = null;
    }
}
function TiraOpacity(box, val)
{
    var div = document.getElementById(obj);
    //div.style.display = "none";
    obj = box;
    valor = val;
    try
    {
        idset = setTimeout('TiraOpacityAux();',5);
    }
    catch(er)
    {
        idset = null;
    }
}
function TiraOpacityAux()
{
    var div = document.getElementById(obj);
    var opold = 100
    if (div.style.opacity > 0)
    {    
        opold = div.style.opacity.substr(2,2);
    }
    opold--;
    opold--;  
    div.style.opacity = "0." + opold;
    div.style.filter = "Alpha(Opacity=" + opold + ")";
    if (opold > 0)
    {
        try
        {
            idset = setTimeout('TiraOpacityAux();',5);
        }
        catch(er)
        {
            idset = null;
        }
    }
    else
    {
        EscondeDiv()
        idset = null;
        obj = null;
        valor = null;
    }
}
function EscondeDiv()
{
    document.getElementById(obj).style.display="none";    
}
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
//fim opacity

function addFav(url){
    var url      = url;
    var title    = "Código Fonte - O Melhor Conteúdo para Programadores Web";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

function Trim(str)
{
    return str.replace(/^\s+|\s+$/g,"");
}

function Pesquisa(txt, textotext, id)
{
    var busca = document.getElementById(txt).value;
    var cat = "0";
    
    if (busca != textotext && Trim(busca) != "")
    {
        busca = busca.replace(" ","+");
        window.location.href = "../home/pesquisa.aspx?id=" + id + "&busca=" + busca + "&cat=" + cat;
    }
    else
    {
        document.getElementById(txt).value = textotext; 
        return false;      
    }
}
//scripts para campos de newsletter inicio
function EscondeLabel()
{
    document.getElementById('divMsgNS').style.display = "none";
    clearTimeout(MsgTimeOut);
}
//scripts para campos de newsletter fim
//função para exibir popup em div com fundo escuro transparente
function ExibirPopup(exibe)
{
    if (exibe)
    {
        document.getElementById('divFundo').style.display="";
        document.getElementById('divPopup').style.display=""; 
        //document.getElementById('divCombo').style.visibility = "hidden";
        document.getElementById('divFundo').style.height = (window.screen.height + 150) + "px";
        document.getElementById('divFundo').style.width = (window.screen.width.toString() - 15) + "px"; 
        //document.getElementById('divFundo').style.height = getHeight().toString() + "px";
        //document.getElementById('divFundo').style.width = getWidth().toString() + "px";
    }
    else
    {
        document.getElementById('divFundo').style.display="none";
        document.getElementById('divPopup').style.display="none"; 
        //document.getElementById('divCombo').style.visibility = "visible"; 
    }
    return false;
}






