// 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) {
		
	if( typeof(tipo) == "undefined" ){
		novaJanela = window.open(arquivo, titulo, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+largura+',height='+altura+',top=0,left=0');
	} else if(tipo == "novo" || tipo == "nova") {
		novaJanela = window.open(arquivo, titulo, '');
	} else {
		novaJanela = window.open(arquivo, titulo, tipo);
	}

	// Verifica se abriu a janela
	if (!novaJanela) {

		elemento1 = 'document.getElementById("popupBloqueado")';
		elemento2 = 'document.getElementById("estrutura")';
		elemento3 = 'document.getElementById("bannerLateral")';

		// Verifica se existe o DIV
		if (eval(elemento1)) {
			eval(elemento1).style.display = 'block';
		}

		// Verifica se existe o DIV
		if (eval(elemento2)) {
			eval(elemento2).style.top = '42px';
		}

		// Verifica se existe o DIV
		if (eval(elemento3)) {
			eval(elemento3).style.top = '42px';
		}

	} else {
		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';
	}
}

function campoErroBusca(formulario, campo) {
	if (campo != undefined && formulario != undefined) {
		var campo = 'document.' + formulario + '.' + campo;
		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);
};

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);
}

function trim(valor){
	valor = valor.replace(/^\s+/, '');
	valor = valor.replace(/\s+$/, '');
	return valor;
}


function FormataData(Campo, teclapres) {
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
}

function numeros(obj) {
	// Verificacao apenas dos numeros
	var checkstr = "0123456789";

	// Variavel temporaria para guardar
	var temp = '';

	// Loop por toda a string para verificar se e' valida e retorna apenas os itens validos
	for (i = 0; i < obj.value.length; i++) {
		if (checkstr.indexOf(obj.value.substr(i, 1)) >= 0) {
			temp = temp + obj.value.substr(i,1);
		}
	}

	// Salva no input
	obj.value = temp;
}

function mascaraValor(o, n, dig, dec){
	o.c = !isNaN(n) ? Math.abs(n) : 2;
	o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;
	addEvent(o, "keypress", function(e){
		if(e.key > 47 && e.key < 58){
			var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
			if(o.maxLength + 1 && l >= o.maxLength) return false;
			l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
			for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
			n && n < l && (s[l - ++n] += o.dec);
			o.value = s.join("");
		}
		e.key > 30 && e.preventDefault();
	});
}

