// Funcao para gerar numero aleatorio
function rand(numero) {
	return Math.floor(Math.random()*numero);
}

function direcionar(arquivo) {
	document.location.href = arquivo;
}



function popup(arquivo, titulo, largura, altura, tipo) {
	
	w = screen.width;
	h = screen.height;
	
	meio_w = w/2;
	meio_h = h/2;
	
	altura2 = altura/2;
	largura2 = largura/2;
	meio1 = meio_h-altura2;
	meio2 = meio_w-largura2;
	
		
	if( typeof(tipo) == "undefined" ){
		novaJanela = window.open(arquivo, titulo, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=0, width='+largura+', height='+altura+', top='+meio1+', left='+meio2);
	} else if(tipo == "novo" || tipo == "nova") {
		novaJanela = window.open(arquivo, titulo, '');
	} else {
		novaJanela = window.open(arquivo, titulo, tipo);
	}
	
	novaJanela.focus();
	
}

//Trocar a cor de fundo
function corFundo(el,cor) {
	el.style.backgroundColor=cor;
}

// Funcoes para mostrar ou ocultar objetos
function mostra(objeto) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	if(elemento) eval(elemento).style.display = 'block';
}

function esconde(objeto) {
	elemento = 'document.getElementById(\'' + objeto + '\')';
	if(elemento) eval(elemento).style.display = 'none';
}

function escondeMostra(objeto) {
	var elemento = 'document.getElementById(\'' + objeto + '\')';
	if (eval(elemento).style.display == 'block' || eval(elemento).style.display == 'inline') {
		eval(elemento).style.display = 'none';
	} else {
		eval(elemento).style.display = 'block';
	}
}

// Função mostra e esconde na tabela.
function mostraEscondeAutomaticoBloco(objeto) {
	if(document.all) { // IE4+
		elemento = 'document.all[\'' + objeto + '\']';
	} else { // NS6+
		elemento = 'document.getElementById(\'' + objeto + '\')';
	}
	if (eval(elemento).style.display == 'block') {
		eval(elemento).style.display = 'none';
	} else {
		eval(elemento).style.display = 'block';
	}
}

//Funcao para marcar campo de erro
function campoErro(formulario, campo) {
	if (campo != undefined && formulario != undefined) {
	
		var campo = 'document.' + formulario + '.' + campo;
		eval(campo).focus();
		eval(campo).style.border = '1px solid #CC3300';
		eval(campo).style.background = '#FFFFE5';
	}
}

//Funcao para desmarcar campo de erro
function campoErroDesmarca(formulario, campo) {
	if (campo != undefined && formulario != undefined) {
		var campo = 'document.' + formulario + '.' + campo;
		eval(campo).style.border = '1px solid #d2d2d2';
		eval(campo).style.background = '#FFFFFF';
	}
}


// Funcao que altera o valor caso de erro
function camposFile(nomeForm) {

	var campos = '';
	var separador = "/";
	var primeiro = 0;

	//Limpa marca de erro de todos os campos
	for(i=0; i<document.form1.length; i++) {
		if(document.form1[i].type == 'file') {
			if(primeiro == 0) {
				separador = "";
				primeiro = 1;
			} else {
				separador = "/";
			}
			campos += separador + document.form1[i].name;
		}
	}
	
	return campos;
}

function verificaTam(campo, tam, prox ,nomeForm){	
	str = "document." + nomeForm + "." + campo + ".value";
	obj = eval (str);
	if (obj.length == tam) {
	   str = "document."+ nomeForm + "." + prox + ".focus()";
	   eval (str);
	}
	return 1;    	    
}

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;
	
	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);
	}
}

	MaskInput = function(f, m){
    function mask(e){
        var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[\xC0-\xFF]/i, "8": /./ },
            rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
        function accept(c, rule){
            for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
                if(r & i && patterns[i].test(c))
                    break;
                return i <= r || c == rule;
        }
        var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
        (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
            r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
            : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
            r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
    }
    for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
        addEvent(f, i, mask);
};

/*
**************************************
* Event Listener Function v1.4       *
* Autor: Carlos R. L. Rodrigues      *
**************************************
*/
addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};


function MascaraCPF(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(".", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;
	if (tecla != 9 && tecla != 8) {
	 	if(tam > 3 && tam < 5){
		Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
	 	}
	 if(tam > 6 && tam < 8) {
	  		Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, tam);
	 	}
	 if(tam > 9 && tam < 11) {
	  		Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '-' + vr.substr(9, 11);
	 	} 
	}
}

function limiteCampo(campo, limite) {
	if (campo.value.length > limite)
		campo.value = campo.value.substring(0, limite);
}

// Funcao que altera o valor caso de erro
function marcaErro(nomeForm, campos) {

	// Transforma lista em array
	campoAtual = campos.split(",");

	// Configura quais os campos que falharam
	for(i=0; i < campoAtual.length; i++) {
		var campoNovo = 'document.' + nomeForm + '.' + campoAtual[i];

		// Coloca a borda de erro e o background no campo
		eval(campoNovo).style.border = '1px solid #CC3300';
		eval(campoNovo).style.background = '#FFFFE5';
	}
	
	// Da o foco no primeiro campo de erro
	eval('document.' + nomeForm + '.' + campoAtual[0]).focus();
}