window.defaultStatus = "SGS - Sistema de Gerenciamento do Site";

function preenche(num, id)
{
	obj = document.getElementById(id);
	val = obj.value;
	fim = val + num;
	obj.value = "";
	obj.value = fim;
}

function testaSenha(senha,confirmacao,formulario)
{
	if (erro == true) 
		return true;
	obj1 = eval("document."+formulario+"."+senha);
	obj2 = eval("document."+formulario+"."+confirmacao);
	if (obj1.value != obj2.value)
	{
		alert ('Sua senha não foi confirmada!');
		document.formulario.senha.focus();
		return true;
	}
	else 
		return false;
}

function testaVazio(campo,comparacao,formulario,mensagem,erro) 
{
	if (erro == true)
		return true;
	obj1 = eval("document."+formulario+"."+campo);
	if (obj1.value == comparacao)
	{
		alert (mensagem);
		obj1.focus();
		return true;
	}
	else 
		return false;
}

function testaNumero(campo,formulario,mensagem,erro)
{
	if (erro == true)
		return true;
	obj1 = eval("document."+formulario+"."+campo);
	if (isNaN(obj1.value)) 
	{
		obj1.value = "";
		alert (mensagem);
		obj1.focus();
		return true;
	}
	else 
		return false;
}

function mostraConteudo(id_cont,  id_local)
{
	obj_cont = document.getElementById(id_cont);
	obj_local = document.getElementById(id_local);
	
	obj_local.innerHTML = obj_cont.innerHTML;
}

function verDados(valor, option)
{
	if(valor == "")
	{
		alert("Você deve escolher um valor válido!");
	}
	else if(valor == 0)
	{
		alert("Você deve escolher um valor, não uma categoria!");
	}
	else
	{
		document.location = "principal.php?m=" + option + "&action=ver&valor=" + valor	;
	}
}

function closeObj(id, method)
{
	obj = document.getElementById(id);
	
	if(method == 'display')
		obj.style.display = 'none';
	else
		obj.style.visibility = 'hidden';
	
}

function openObj(id, method)
{
	obj = document.getElementById(id);
	
	if(method == 'display')
		obj.style.display = 'block';
	else
		obj.style.visibility = 'visible';
	
}

function gerenciaAbas(abre,total)
{
    document.getElementById('aba').value = abre;
    
	for(x = 1; x <= total; x++)
	{
		document.getElementById('aba_bt_'+x).className = '';
		document.getElementById('abas_cad_'+x).style.display = 'none';
	}
	document.getElementById('aba_bt_'+abre).className = 'selected';
	document.getElementById('abas_cad_'+abre).style.display = 'block';

}

//USA JQUERY
function changeAba(pre,aba,total)
{
    for(x = 1; x <= total; x++)
	{
        $('#'+pre+'aba_'+x).removeClass('choose');
        $('#'+pre+'aba_'+x).addClass('aba');
        $('#'+pre+'div_'+x).hide();
    }
    $('#'+pre+'aba_'+aba).removeClass('aba');
    $('#'+pre+'aba_'+aba).addClass('choose');
    $('#'+pre+'div_'+aba).show();
}

var lastzoekstr = 'J29dWTNVQV8TugOoCaLplUHwWPTgbWE8';
function selectSearch(zoekstr, ListOfValues)
{
	zoekstr = zoekstr.toLowerCase();
	if (zoekstr != lastzoekstr && zoekstr != '')
	{
		lastzoekstr = zoekstr;
		var LOV = ListOfValues;
		var aantal = LOV.options.length;
		var gevonden = 0;

		for (var x = 0; x < aantal; x++)
		{
			var CurrentRecordIndex = LOV.options[x].value;
			var CurrentRecordText  = LOV.options[x].text;

			if ((zoekstr == '') || (CurrentRecordText.toLowerCase().indexOf(zoekstr) >= 0))
			{
					LOV.options[x].selected = true;
					LOV.options[x].style.display = 'block';
			} else {
					LOV.options[x].style.display = 'none';
			}
		}
	}
}

function resetaSelectSearch(ListOfValues)
{
	var LOV = ListOfValues;
	var aantal = LOV.options.length;
	var gevonden = 0;

	for (var x = 0; x < aantal; x++)
	{
		LOV.options[x].style.display = 'block';
	}
	LOV.options[0].selected = true;
}


function FormataValor(id,tammax,teclapres) {

    if(window.event) { // Internet Explorer
        var tecla = teclapres.keyCode;
    }
    else if(teclapres.which) { // Nestcape / firefox
        var tecla = teclapres.which;
    }


    vr = document.getElementById(id).value;
    vr = vr.toString().replace( "/", "" );
    vr = vr.toString().replace( "/", "" );
    vr = vr.toString().replace( ",", "" );
    vr = vr.toString().replace( ".", "" );
    vr = vr.toString().replace( ".", "" );
    vr = vr.toString().replace( ".", "" );
    vr = vr.toString().replace( ".", "" );
    tam = vr.length;

    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 <= 2 ){
        document.getElementById(id).value = vr; }
        if ( (tam > 2) && (tam <= 5) ){
        document.getElementById(id).value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }
        if ( (tam >= 6) && (tam <= 8) ){
        document.getElementById(id).value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
        if ( (tam >= 9) && (tam <= 11) ){
        document.getElementById(id).value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
        if ( (tam >= 12) && (tam <= 14) ){
        document.getElementById(id).value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }
        if ( (tam >= 15) && (tam <= 17) ){
        document.getElementById(id).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 checkAll(pre,tot,che)
{
    if(che == true)
    {
        for(x = 0; x < tot; x++)
            document.getElementById(pre+x).checked = true;
    }
    else
    {
         for(x = 0; x < tot; x++)
            document.getElementById(pre+x).checked = false;
    }
}
function relogio() //usa jquery
{
    momentoAtual = new Date()
    hora = momentoAtual.getHours();
    minuto = momentoAtual.getMinutes();
    segundo = momentoAtual.getSeconds();
    hora = hora.toString();
    minuto = minuto.toString();
    segundo = segundo.toString();
    if(hora.length == 1) hora = '0' + hora;
    if(minuto.length == 1) minuto = '0' + minuto;
    if(segundo.length == 1) segundo = '0' + segundo;

    horaImprimivel = hora + ":" + minuto + ":" + segundo;

    $('#hr').html(horaImprimivel);
    setTimeout("relogio()",1000);
}

function alerta(msg,classe) // usa jquery
{
    $('#alerta').html(msg);
    $('#alerta').removeClass('error');
    $('#alerta').removeClass('ok');
    $('#alerta').addClass(classe);
    $('#alerta').slideDown();

    setTimeout("$('#alerta').slideUp();",2000);
}

function fechaSessao(obj)
{
    //alert(obj.location);
    //document.location = 'deslogar.php';
}

function FormataCNPJ(Campo, teclapres){

   var tecla = teclapres.keyCode;

   var vr = new String(Campo.value);
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("/", "");
   vr = vr.replace("-", "");

   tam = vr.length + 1 ;


   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 6)
         Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
      if (tam >= 6 && tam < 9)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
      if (tam >= 9 && tam < 13)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
      if (tam >= 13 && tam < 15)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
      }
}


function mascara_cpf (campo,documento,f)
{
     var mydata = '';
     mydata = mydata + documento;

     if (mydata.length == 3){
        mydata   = mydata + '.';

        ct_campo = eval("document."+f+"."+campo+".value = mydata");
        ct_campo;
     }

     if (mydata.length == 7){
              mydata   = mydata + '.';

              ct_campo = eval("document."+f+"."+campo+".value = mydata");
              ct_campo;
     }

     if (mydata.length == 11){
        mydata      = mydata + '-';

        ct_campo1 = eval("document."+f+"."+campo+".value = mydata");
        ct_campo1;
     }

     if (mydata.length == 14){

        valida_cpf(f,campo);
     }

}


function valida_cnpj(f,campo){

     pri = eval("document."+f+"."+campo+".value.substring(0,2)");
     seg = eval("document."+f+"."+campo+".value.substring(3,6)");
     ter = eval("document."+f+"."+campo+".value.substring(7,10)");
     qua = eval("document."+f+"."+campo+".value.substring(11,15)");
     qui = eval("document."+f+"."+campo+".value.substring(16,18)");

     var i;
     var numero;
     var situacao = '';

     numero = (pri+seg+ter+qua+qui);

     s = numero;


     c = s.substr(0,12);
     var dv = s.substr(12,2);
     var d1 = 0;

     for (i = 0; i < 12; i++){
        d1 += c.charAt(11-i)*(2+(i % 8));
     }

     if (d1 == 0){
        var result = "falso";
     }
        d1 = 11 - (d1 % 11);

     if (d1 > 9) d1 = 0;

        if (dv.charAt(0) != d1){
           var result = "falso";
        }

     d1 *= 2;
     for (i = 0; i < 12; i++){
        d1 += c.charAt(11-i)*(2+((i+1) % 8));
     }

     d1 = 11 - (d1 % 11);
     if (d1 > 9) d1 = 0;

        if (dv.charAt(1) != d1){
           var result = "falso";
        }


     if (result == "falso") {
        alert("CNPJ inválido!");
        aux1 = eval("document."+f+"."+campo+".focus");
        aux2 = eval("document."+f+"."+campo+".value = ''");

     }
}


function valida_cpf(f,campo){

     pri = eval("document."+f+"."+campo+".value.substring(0,3)");
     seg = eval("document."+f+"."+campo+".value.substring(4,7)");
     ter = eval("document."+f+"."+campo+".value.substring(8,11)");
     qua = eval("document."+f+"."+campo+".value.substring(12,14)");

     var i;
     var numero;

     numero = (pri+seg+ter+qua);

     s = numero;
     c = s.substr(0,9);
     var dv = s.substr(9,2);
     var d1 = 0;

     for (i = 0; i < 9; i++){
        d1 += c.charAt(i)*(10-i);
     }

     if (d1 == 0){
        var result = "falso";
     }

     d1 = 11 - (d1 % 11);
     if (d1 > 9) d1 = 0;

     if (dv.charAt(0) != d1){
        var result = "falso";
     }

     d1 *= 2;
     for (i = 0; i < 9; i++){
        d1 += c.charAt(i)*(11-i);
     }

     d1 = 11 - (d1 % 11);
     if (d1 > 9) d1 = 0;

     if (dv.charAt(1) != d1){
        var result = "falso";
     }


     if (result == "falso") {
        alert("CPF inválido!");
        aux1 = eval("document."+f+"."+campo+".focus");
        aux2 = eval("document."+f+"."+campo+".value = ''");

     }
}

function formatar(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);
    }
}



