function abrir_pop(vc_arquivo,vc_parametros,W,H,C) {

	var X	= Math.ceil( (window.screen.height - H) / 2 ) - 20;
	var Y	= Math.ceil( (window.screen.width - W) 	/ 2 ) - 16;
	
	janela = window.open(vc_arquivo+'.asp?'+vc_parametros,'','width='+W+',height='+H+',top='+X+',left='+Y+',location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,toolbar=no');
	
	if (C == 0) {
		//centralizar (janela, X, Y)
	} else if (C == 1) {
		upar (janela)
	}
	
}

function centralizar (janela, X, Y) {
	janela.moveTo(X, Y)
}
function upar(janela) {
	
	janela.moveTo(-1,-1);
	
}


function carregando(td){
	td.innerHTML = '<center><img src="../images/layout/carregando.gif"> <b>Carregando...</b></center>';
}

function enviar(origem, destino, indice){
	
	destino[destino.options.length] = new Option(origem.options[indice].text,origem.options[indice].value, false, false);
	origem.remove(indice);
	
}

function excluir(url){
	
	if (confirm("Deseja mesmo excluir este registro?"))
		parent.codigo.location = url;
	
}

function mascaraCentavo(mn_valor) {
	if (mn_valor.indexOf("0") == 0) {
		mn_valor	= mn_valor.replace("0.","");
		mn_valor	= mn_valor.replace("0.0","");
	}
	
	mn_valor		= mn_valor.replace(".","");
	return mn_valor;
}

/*# MASCARA MOEDA #*/
function mascaraMoeda(valor,cx) {
	
	if (valor) {
		var doc					= eval('document.all.'+cx);
		
		if (window.event.keyCode == 110 || window.event.keyCode == 188 || window.event.keyCode == 190 || window.event.keyCode == 194)	{
			doc.value			= valor.substr(0,(valor.length-1));
		} else {
			valor				= mascaraCentavo(valor);
			
			if (mascaraCentavo(valor).length > 2) {
				str				= valor.replace(".","");
				doc.value		= str.substr(0,(str.length-2))+"."+str.substr((str.length-2),str.length);
			} else {
				valor			= mascaraCentavo(valor);
				
				if (valor.length == 1) {
					doc.value	= "0.0"+valor;
				} else {
					doc.value	= "0."+valor;
				}
			}
		}
	}
}

/*# MASCARA GENÉRICA */
function mascarar(vc_campo,vc_mascara) {
	var i		= vc_campo.value.length;
	var x		= vc_mascara.substring(0,1);
	var y		= vc_mascara.substring(i);
	
	if (y.substring(0,1) != x) {
		vc_campo.value += y.substring(0,1);
	}
}

function menu (act, tr, td){
	
	if (act == 1){
		tr.className = 'tr_02';
		td.className = 'tr_03';
	} else {
		tr.className = 'tr_01';
		td.className = '';
	}
	
}

function ordenar(vc_ordem){

	var int_flag 			= 0
	var url 				= new String(location.href);
	var pos_barra			= url.lastIndexOf("/")	
	var pos_interrogacao	= url.indexOf("?")

	if (pos_interrogacao > -1){

		vc_parametros 	= url.substring(pos_interrogacao + 1,url.length);
		vc_pagina		= url.substring(pos_barra + 1, pos_interrogacao);
		vc_array 		= vc_parametros.split("&")	
		n 				= Number(vc_array.length - 1)

		for(i=0;i<n;i++){
			vc_string = String(vc_array[i]);
			if (vc_string.indexOf("vc_ordem=") > -1){
				if(vc_string != "vc_ordem="+vc_ordem){
					vc_array[i] = "vc_ordem="+vc_ordem
					vc_array[i+1] = "it_desc=0"
				} else {
					if(vc_array[i+1] == "it_desc=1"){
						vc_array[i+1] = "it_desc=0"
					} else {
						vc_array[i+1] = "it_desc=1"
					}
				}
				int_flag = 1
			}
		}

		if(int_flag == 0){
			vc_array.push("vc_ordem=" + vc_ordem)
			vc_array.push("it_desc=0")
		}

		vc_parametros = "?" + vc_array.join("&")

	} else {
		vc_pagina		= url.substring(pos_barra + 1, url.length);
		vc_parametros	= '?vc_ordem=' + vc_ordem + '&it_desc=0';
	}

	location.href = vc_pagina + vc_parametros;

}

function preencheEstado(combo,selecionado){
	
	if (combo.length == 1){
		combo.remove(0);
		
		//ADICIONAR OS ESTADOS
		combo[0] 	= new Option('Acre',				'AC', false, false)
		combo[1] 	= new Option('Alagoas',				'AL', false, false)
		combo[2] 	= new Option('Amapa',				'AP', false, false)
		combo[3] 	= new Option('Amazonas',			'AM', false, false)
		combo[4] 	= new Option('Bahia',				'BA', false, false)
		combo[5] 	= new Option('Ceará',				'CE', false, false)
		combo[6] 	= new Option('Distrito Federal',	'DF', false, false)
		combo[7] 	= new Option('Espírito Santo',		'ES', false, false)
		combo[8] 	= new Option('Goiás',				'GO', false, false)
		combo[9] 	= new Option('Maranhão',			'MA', false, false)
		combo[10] 	= new Option('Minas Gerais',		'MG', false, false)
		combo[11] 	= new Option('Mato Grosso',			'MT', false, false)
		combo[12] 	= new Option('Mato Grosso do Sul',	'MS', false, false)
		combo[13] 	= new Option('Pará',				'PA', false, false)
		combo[14] 	= new Option('Paraíba',				'PB', false, false)
		combo[15] 	= new Option('Paraná',				'PR', false, false)
		combo[16] 	= new Option('Pernambuco',			'PE', false, false)
		combo[17] 	= new Option('Piauí',				'PI', false, false)
		combo[18] 	= new Option('Rio de Janeiro',		'RJ', false, false)
		combo[19] 	= new Option('Rio Grande do Norte',	'RN', false, false)
		combo[20]	= new Option('Rio Grande do Sul',	'RS', false, false)
		combo[21] 	= new Option('Rondônia',			'RO', false, false)
		combo[22] 	= new Option('Roraima',				'RR', false, false)
		combo[23] 	= new Option('Santa Catarina',		'SC', false, false)
		combo[24] 	= new Option('São Paulo',			'SP', false, false)
		combo[25] 	= new Option('Sergipe',				'SE', false, false)
		combo[26] 	= new Option('Tocantins',			'TO', false, false)
		combo[27] 	= new Option('Outros',				'OU', false, false)
	} 

	if (selecionado){
		combo.value = selecionado;
	}
}

String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, "");
}

function redirecionar (pagina, parametros){
	
	codigo.location = 'codigos/' + pagina + '.php?' + parametros

}

function redirecionar2 (pagina, parametros){
	
	location.href = pagina + '.php?' + parametros

}

function replaceChars(entry, sai, entra) {
// OTIMA FUNCAO, coloca a string completa depois o valor q sai e o valor q substituirá!
	out = sai; // replace this
	add = entra; // with this
	temp = "" + entry; // temporary holder
	
	while (temp.indexOf(out)>-1) {
		pos		= temp.indexOf(out);
		temp 	= "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function selecionar_itens (combo) {
	
	for (i = 0; i < combo.options.length; i++){
		combo.options[i].selected = true ;
	}

}

/*#SO NUMERO#*/
function soNumero(event)
{

	var kCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	var caract = new RegExp(/^[0-9 \b]+$/i);
    var caract = caract.test(String.fromCharCode(kCode));

    if(!caract){
        return false;
    }

}

function valida_generico(bot, form){

    /* LEGENDA
        se tiver ID COM #01 verifica se há alguma coisa digitada!                                    ex: Id = "NOME#01"
        se tiver ID COM #02 verifica se há alguma alternativa selecionada em um select normal!       ex: Id = "CATEGORIA#02"
        se tiver ID COM #03 comparação de valor com outro campo !                                    ex: Id = "SENHA#03|vc_conf_senha"
        se tiver ID COM #04 verifica se há alguma alternativa selecionada em um select multiple!     ex: Id = "CATEGORIA#04"
        se tiver ID COM #05 verifica se o E-Mail digitado é válido                                   ex: Id = "E-MAIL#05"
        se tiver ID COM #06 verifica se o campo tem x caracteres digitados                           ex: Id = "CEP#06|9"
        se tiver ID COM #06 verifica se o campo tem x ou mais caracteres digitados                   ex: Id = "CEP#07|9"
	*/
	
	/* VERIFICA SE O FORMULARIO EXISTE */
	if (!form){
		alert("O formulário não existe!");
		return false;		
	}
	/* VERIFICA SE O FORMULARIO EXISTE */
	
    for    (i = 0; i < form.elements.length; i++){

        if (form.elements[i].id.indexOf("#01") != -1){
            pos = form.elements[i].id.indexOf("#")
            if(form.elements[i].value == ''){
                alert('Por favor preencha o campo '+form.elements[i].id.substring(0,pos)+'!')
                form.elements[i].focus();
                return false;
            }
        }

        if (form.elements[i].id.indexOf("#02") != -1){
            if(form.elements[i].value == '0'){
                alert('Por favor selecione um(a) '+form.elements[i].id.substring(0,eval(form.elements[i].id.length-3))+'!')
                form.elements[i].focus();
                return false;
            }
        }

        if (form.elements[i].id.indexOf("#03") != -1){

            pos = form.elements[i].id.indexOf("#03")

            if(form.elements[i].value == ''){
                alert('Por favor preencha o campo '+form.elements[i].id.substring(0,pos)+'!')
                form.elements[i].focus();
                return false;
            }

            valor = form.elements[i].id.substring(pos+4,form.elements[i].id.length)
            comparacao = eval('form.'+valor+'.value')
            id = eval('form.'+valor+'.id')
            if (form.elements[i].value != comparacao){
                alert('Os campos '+form.elements[i].id.substring(0,pos)+' e '+id.substring(0,(id.length - 3))+' não batem!')
                form.elements[i].focus();
                return false;
            }
        }

        if (form.elements[i].id.indexOf("#04") != -1){
            if(form.elements[i].value.length == 0){
                alert('Por favor selecione um(a) '+form.elements[i].id.substring(0,eval(form.elements[i].id.length-3))+'!')
                form.elements[i].focus();
                return false;
            }
        }

        if (form.elements[i].id.indexOf("#05") != -1){

            pos = form.elements[i].id.indexOf("#05")

            if (form.elements[i].value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
                alert('Por favor preencha corretamente o campo '+form.elements[i].id.substring(0,pos)+'!')
				form.elements[i].focus();
				return false;
			}            

        }

        if (form.elements[i].id.indexOf("#06") != -1){
            
            pos = form.elements[i].id.indexOf("#06")

            if(form.elements[i].value == ''){
                alert('Por favor preencha o campo '+form.elements[i].id.substring(0,pos)+'!')
                form.elements[i].focus();
                return false;
            }
            
            tamanho = form.elements[i].id.substring(pos+4,form.elements[i].id.length)
            if (form.elements[i].value.length != tamanho){
                alert('O campo ' + form.elements[i].id.substring(0,pos) + ' deve ter ' + tamanho + ' caracteres!')
                form.elements[i].focus();
                return false;
            }
        }

        if (form.elements[i].id.indexOf("#07") != -1){
            
            pos = form.elements[i].id.indexOf("#07")

            if(form.elements[i].value == ''){
                alert('Por favor preencha o campo '+form.elements[i].id.substring(0,pos)+'!')
                form.elements[i].focus();
                return false;
            }
            
            tamanho = form.elements[i].id.substring(pos+4,form.elements[i].id.length)
            if (form.elements[i].value.length < tamanho){
                alert('O campo ' + form.elements[i].id.substring(0,pos) + ' deve ter ' + tamanho + ' ou mais caracteres!')
                form.elements[i].focus();
                return false;
            }
        }

    }

    bot.value = "Carregando...";
    bot.disabled = true;
    form.submit();

}

/*# VERIFICA DATA #*/
function verificaData(vc_campo) { 
	if (vc_campo.value.length > 0) {  
		it_dia		= (vc_campo.value.substring(0,2)); 
		it_mes		= (vc_campo.value.substring(3,5)); 
		it_ano		= (vc_campo.value.substring(6,10)); 
		it_erro		= 0; 
		
		if ((it_dia < 01) || (it_dia < 01 || it_dia > 30) && (it_mes == 04 || it_mes == 06 || it_mes == 09 || it_mes == 11 ) || it_dia > 31) { 
			it_erro	= 1; 
		} 

		if (it_mes < 01 || it_mes > 12 ) { 
			it_erro	= 1; 
		} 

		if (it_mes == 2 && (it_dia < 01 || it_dia > 29 || (it_dia > 28 && (parseInt(it_ano / 4) != it_ano / 4)))) { 
			it_erro	= 1; 
		} 

		if ((it_ano < 1900) || (it_ano > 2078)){
			it_erro	= 1;
		}

		if (it_erro == 1) { 
			alert("Por favor, preencha uma data válida!"); 
			vc_campo.value	= '';
			vc_campo.focus(); 
		}
	} 
}