// Funcao para colocar a "/" automaticamente na data
function formataData(campo, teclapres) {
	var tecla = teclapres.keyCode;
	var vr = new String(campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	tam = vr.length + 1;

	// verifica se nao eh backspace nem TAB
	if (tecla != 9 && tecla != 8) {
		if (tam > 2 && tam < 5)
			campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
		if (tam >= 5 && tam <=10)
			campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
	}
}

function popup(url,largura,altura) {
	window.open(url,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,menubar=no,width='+largura+',height='+altura);
}

function escreve(id,texto) {
	document.getElementById(id).innerHTML = texto;
}

function apaga(id) {
	document.getElementById(id).innerHTML = "";
}

function hide(id) {
	if (ns4) document.layers[id].visibility = "hide"
	else document.getElementById(id).style.display="none";
}
function show(id) {
	if (ns4) document.layers[id].visibility = "show"
	else document.getElementById(id).style.display="";
}
function habilita(obj) {
	if (ns4) document.layers[obj].disabled = false;
	else document.getElementById(obj).disabled= false;
}
function desabilita(obj) {
	if (ns4) document.layers[obj].disabled = true;
	else document.getElementById(obj).disabled= true;
}

function pula_campo(campo1,campo2,campo3){			
	if ((eval('document.form1.'+campo1).value.length) == eval(campo3)) {
		eval('document.form1.'+campo2).focus();
	}	
}

//função para aceitar somente numeros e pontos
function soNumeros() { 
	tecla = event.keyCode; 
	if (tecla >= 48 && tecla <= 57) return true; 
	else return false; 
}

// funcao de mascara para campos de moeda
function mascaraMoeda(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;
    if (whichCode == 13) 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;
}

function mascaraCEP(e,src,mask){
	if(window.event)
		_TXT = e.keyCode;
	else if(e.which)
		 _TXT = e.which;
	if(_TXT > 47 && _TXT < 58) {
		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);
		return true;
	} else {
		if(_TXT != 8)
			return false;
		else
			return true;
	}
}

function popup(base){
	window.open(base + "/arquivo","popupRedir","resizable=0,toolbar=no,location=no,status=no,menubar=no,directories=no,scrollbars=1,width=400,height=140").focus();
}

/* Funcao para mudar o tamanho da fonte de um identificador dinamicamente */
function tamanhoTexto(tamanho){
	switch(tamanho){
		case 'p':
			document.getElementById('texto').className = 'txt_pequeno';
			break;
		case 'm':
			document.getElementById('texto').className = 'txt_medio';
			break;
		case 'g':
			document.getElementById('texto').className = 'txt_grande';
			break;
	}
}

/* Funcao para exibir a caixa 'enviar por e-mail'
   v1.0 06/2006
   Cronic e-Studios (gabrielz - desenvolvimento@cronic.com.br)
*/
function enviarEmail(){
	document.getElementById('enviar_email').style.display = 'block';
}
function escondeEnviar(){
	document.getElementById('enviar_email').style.display = 'none';
}

// Funcoes para alterar cor de fundo de objetos
function mudaFundo(objeto,cor) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	eval(elemento).style.background = cor;
}

// Funcoes para mostrar ou ocultar objetos
function mostraObjeto(objeto) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	eval(elemento).style.display = 'inline';
}
function escondeObjeto(objeto) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	eval(elemento).style.display = 'none';
}
function mostraEsconde(objMostra,objEsconde) {
	mostraObjeto(objMostra);
	escondeObjeto(objEsconde);
}

// Funcao para marcar/desmarcar todos os objetos 'checkbox'
function selTodos(obj,objTodos) {
	var total=obj.length;
	for(i=0;i<total;i++) {
		if (!(objTodos.checked)) {
		  	obj[i].checked=false;
		  	escondeObjeto('linkExcluirItens');
			mostraObjeto('btn_excluirsel_off');
		}
	  	else {
		  	obj[i].checked=true;
			mostraObjeto('linkExcluirItens');
	  	}
	}
}

// Funcao para os checkboxes
function verifChecks(obj,objTodos) {
	escondeObjeto('linkExcluirItens');
	escondeObjeto('linkExcluirItens_conf');
	mostraObjeto('btn_excluirsel_off');
	objTodos.checked = false;
	if (obj.length == undefined) {
		if (obj.checked) {
			mostraObjeto('linkExcluirItens');
			escondeObjeto('btn_excluirsel_off');
			escondeObjeto('linkExcluirItens_conf');
		}
	} else {
		var total=obj.length;
		var marcados = 0;
		for (i=0;i<total;i++) {
		  	if (obj[i].checked) {
				mostraObjeto('linkExcluirItens');
				escondeObjeto('btn_excluirsel_off');
				escondeObjeto('linkExcluirItens_conf');
				marcados = marcados + 1;
			}
		}
		if (marcados < 1) objTodos.checked = false;
	}
}
