function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}
function Esconde (id) {
	document.getElementById(id).style.display = (document.getElementById(id).style.display == "none") ? "" : "none";
}

function SubmitForm() {
	document.form1.submit();
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function mudacor(prmThis,prmName)	{
	if ((window.document.all) || (window.document.getElementById))
		prmThis.className = prmName;
	else if (window.document.layers)
		prmThis.className = prmName;
}

var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',noresizable,status=yes'
	win = window.open(mypage,myname,settings)
}

function Redimensiona(w, h) {
	window.resizeTo(w, h);
	window.focus();
}

function StatusBar(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

// Recupera informações de um cookie
function getCookieVal(inicio) {
	var fim = document.cookie.indexOf(";", inicio);
	if (fim == -1) fim = document.cookie.length;
	return unescape(document.cookie.substring(inicio, fim));
}

function getCookie(label) {
	var arg = label + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg) return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function setCookie(label, valor, expires) {
	document.cookie = label + "=" + escape(valor) + 
		((expires) ? "; expires=" + expires.toGMTString() : "");
}

function delCookie(label) {
	if (getCookie(label)) {
		document.cookie = label + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// Deixa a string apenas com os numeros, eliminando o restante.
function trimToDigits(string) {
	s = "";
	var Numeros = /\d/;
	for (i = 0; i <= string.length; i++) 
		if (Numeros.test(string.substr(i,1))) s += string.substr(i,1);
	return s;	
}

function VEmail( strEmail ) {
     var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
     return (filter.test(strEmail));
}

function VAno(ano) {
	ano = trimToDigits(ano);
	return ((parseInt(ano) < 1900) || (ano.length != 4) || (parseInt(ano) > 2050)) ? false : true; 
}

function VMes(mes) {
	mes = trimToDigits(mes);
	return ((mes.length != 2) || (parseInt(mes) < 1) || (parseInt(mes) > 12)) ? false : true; 
}

function VMesAno(data) {
	data = trimToDigits(data);
	if (data.length < 6) return false;
	mes = data.substr(0,2);
	ano = data.substr(2,4);
	return (!VAno(ano) || !VMes(mes)) ? false : true;
}
//Verifica se a data é valida
function VData(data) {
	data = trimToDigits(data);
	if (data.length < 8) return false;
	dia = data.substr(0,2);
	mes = data.substr(2,2);
	ano = data.substr(4,4);
	
	if(ano >= 0 && ano < 1900) return false; 
	if(ano > 2100 || ano < 0 || mes > 12 || mes < 1) return false; 
	numero = ((parseInt(ano) - 1884) / 4) 
	dias = (numero == Math.floor(numero)) ? "312931303130313130313031" : "312831303130313130313031"; 
	diamax = parseInt(dias.substring((mes - 1) * 2, ((mes - 1) * 2) + 2)); 
	if(dia < 1 || dia > diamax) return false; 	
	
	return true;
}

// Verifica a autenticidade do CPF
function VCPF(cpf) {
	cpf = trimToDigits(cpf);
	if (cpf.length < 11) return false;
	valor = true;
	if (cpf == "00000000000" || cpf == "11111111111" || 
			cpf == "22222222222" || cpf == "33333333333" || 
			cpf == "44444444444" || cpf == "55555555555" || 
			cpf == "66666666666" || cpf == "77777777777" || 
			cpf == "88888888888" || cpf == "99999999999") return false;

	var a = [];
	var b = new Number;
	var c = 11;
	for (i = 0; i < 11; i++) {
		a[i] = cpf.charAt(i);
		b = (i < 9) ? b + (a[i] *  --c) : b;
	}
	
	a[9] = ((x = b % 11) < 2) ? 0 : 11 - x;

	b = 0;
	c = 11;
	for (y = 0; y < 10; y++) b += (a[y] *  c--); 
	a[10] = ((x = b % 11) < 2) ? 0 : 11 - x;

	return (cpf.charAt(9) == a[9]) && (cpf.charAt(10) == a[10]);
}	



function ExibeFlash(w,h,wmode,movie) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'">');
	document.write('<param name="wmode" value="'+wmode+'"/>');
	document.write('<param name="quality" value="high"/>');
	document.write('<param name="movie" value="'+movie+'"/>');
	document.write('<embed src="'+movie+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="'+wmode+'" width="'+w+'" height="'+h+'"></embed>');
	document.write('</object>');
}

function addEvent(obj, e, func, s) {
		var r = obj[r = "_" + (e = "on" + e)] = obj[r] || (obj[e] ? [[obj[e], obj]] : []), a, c, d;
		r[r.length] = [func, s || obj], obj[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(func) {}
				for(d = 1, func = r.length; func; r[--func] && (a = r[func][0], obj = r[func][1], a.call ? c = a.call(obj, e) : (obj._ = a, c = obj._(e), obj._ = null), d &= c !== false));
				return e = null, !!d;
		}
};

function removeEvent(obj, e, func, s) {
		for(var i = (e = obj["_on" + e] || []).length; i;)
				if(e[--i] && e[i][0] == func && (s || obj) == e[i][1])
						return delete e[i];
		return false;
};

maskInput = {
	tagsInput: ['input', 'textarea'], 
	
	setInputRequired: function(input) {
		var inputObrigatorio = 'inputObrigatorio';
		var classes = input.className;
		var pos = (classes.length > 0) ? inputObrigatorio.indexOf(classes.toLowerCase()) : -1;
		if (pos == -1)
			classes += ' ' + inputObrigatorio;
		else {
			classes = classes.split(" ");
			var i = 0;
			while (pos < 0) {
				pos = (classes[i] == inputObrigatorio) ? i : pos; 
				i++;
			}
			classes.join(" ");
		}	
		input.className = classes;
	},
	
	initMask: function() {
		var restrict = new Restrict("form1");
		for (var i = 0; i < this.tagsInput.length; i++) {
			listInputs = document.getElementsByTagName(this.tagsInput[i]);
			for (var y = 0; y < listInputs.length; y++)
				if (('button,image,hidden,submit,reset').indexOf(listInputs[y].type.toLowerCase()) == -1) {
					if (listInputs[y].getAttribute('obrigatorio') != null) this.setInputRequired(listInputs[y]);
					if ((mascara = listInputs[y].getAttribute('mascara')) != null) {
						if (('MOEDA').indexOf(mascara) != -1) { this.formatValor(listInputs[y], 2)};
						if (('FLOAT').indexOf(mascara) != -1) { this.formatValor(listInputs[y], 2, "", ",")};
						if (('DATA,HORA,UF,CPF,CNPJ,CEP,FONE,INT').indexOf(mascara) != -1) {
							switch (mascara) {
								/*  * ".": Qualquer caracter
										* "w": Somente A-z, a-z, 0-9 e _
										* "W": Qualquer caracter, exceto: A-z, a-z, 0-9 e _
										* "d": 0-9
										* "D": Qualquer caracter, exceto 0-9
										* "s": Permite espaço em branco, tabulação, quebra de linha, etc (\f\n\r\t\v)
										* "a": Permite somente letras acentuadas
										* "A": Permite qualquer caracter, exceto letras acentuadas
								*/									
								case 'INT': listInputs[y].mask = '##########'; listInputs[y].expreg = '\\\\d'; break;
								case 'DATA': listInputs[y].mask = '##/##/####'; listInputs[y].expreg = '\\\\d'; break;
								case 'HORA': listInputs[y].mask = '##:##'; listInputs[y].expreg = '\\\\d'; break;
								case 'UF': listInputs[y].mask = '##'; listInputs[y].expreg = 'ABCDEFGHIJKLLMNOPQRSTUVWXYZ'; break;
								case 'CPF': listInputs[y].mask = '###.###.###-##'; listInputs[y].expreg = '\\\\d'; break;
								case 'CNPJ': listInputs[y].mask = '##.###.###/####-##'; listInputs[y].expreg = '\\\\d'; break;
								case 'CEP': listInputs[y].mask = '##.###-###'; listInputs[y].expreg = '\\\\d'; break;
								case 'FONE': listInputs[y].mask = '(##) ####-####'; listInputs[y].expreg = '\\\\d'; break;
							};
							eval('restrict.field.'+listInputs[y].name+' = "'+listInputs[y].expreg+'"');
							eval('restrict.mask.'+listInputs[y].name+' = "'+listInputs[y].mask+'"');
						};
					}; 
				}; 
		}; 
		restrict.start();
		//restrict.onKeyRefuse = function(o, k){ o.style.backgroundColor = "#fdc"; };
		//restrict.onKeyAccept = function(o, k){ if(k > 30) o.style.backgroundColor = "transparent"; };
	},
	
	keyAccept: function(obj, accept){
		obj.style.backgroundColor = (accept) ? "transparent" : "#fdc";
	},
	
	formatValor: function(obj, num, strdig, strdec) {
		obj.c = !isNaN(num) ? Math.abs(num) : 2;
		obj.dec = typeof strdec != "string" ? "," : strdec, obj.dig = typeof strdig != "string" ? "." : strdig;
		obj.keyAccept = this.keyAccept;
		addEvent(obj, "keypress", function(e) {
			if(e.key > 47 && e.key < 58) {
				var s, l = (s = (obj.value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, num;
				if(obj.maxLength + 1 && l >= obj.maxLength) return false;
				l <= (num = obj.c) && (s = new Array(num - l + 2).join("0") + s);
				for(var i = (l = (s = s.split("")).length) - num; (i -= 3) > 0; s[i - 1] += obj.dig);
				num && num < l && (s[l - ++num] += obj.dec);
				obj.value = s.join("");
			}
			e.key > 30 && e.preventDefault();
			this.keyAccept(obj, ((e.key < 30) || (e.key > 47 && e.key < 58)));
		});
		addEvent(obj, "keyup", function() {
			var obj = this, s, l = (s = (obj.value.replace(/^0+/g, "")).replace(/\D/g, "")).length, num;
			if(obj.maxLength + 1 && l >= obj.maxLength) return false;
			l <= (num = obj.c) && (s = new Array(num - l + 2).join("0") + s);
			for(var i = (l = (s = s.split("")).length) - num; (i -= 3) > 0; s[i - 1] += obj.dig);
			num && num < l && (s[l - ++num] += obj.dec);
			obj.value = s.join("");
		});
	}
}

Restrict = function(form){
	this.form = form, this.field = {}, this.mask = {}, this.name = {};
}
Restrict.field = Restrict.inst = Restrict.c = null;
Restrict.prototype.start = function(){
		var $, __ = document.forms[this.form], s, x, j, c, sp, o = this, l;
		var p = {".":/./, w:/\w/, W:/\W/, d:/\d/, D:/\D/, s:/\s/, a:/[\xc0-\xff]/, A:/[^\xc0-\xff]/};
		for(var _ in $ = this.field)
				if(/text|textarea|password/i.test(__[_].type)){
						x = $[_].split(""), c = j = 0, sp, s = [[],[]];
						for(var i = 0, l = x.length; i < l; i++)
								if(x[i] == "\\" || sp){
										if(sp = !sp) continue;
										s[j][c++] = p[x[i]] || x[i];
								}
								else if(x[i] == "^") c = (j = 1) - 1;
								else s[j][c++] = x[i];
						o.mask[__[_].name] && (__[_].maxLength = o.mask[__[_].name].length);
						__[_].pt = s, addEvent(__[_], "keydown", function(e){
								var r = Restrict.field = e.target;
								if(!o.mask[r.name]) return;
								r.l = r.value.length, Restrict.inst = o; Restrict.c = e.key;
								setTimeout(o.onchanged, r.e = 1);
						});
						addEvent(__[_], "keyup", function(e){
								(Restrict.field = e.target).e = 0;
						});
						addEvent(__[_], "keypress", function(e){
								o.restrict(e) || e.preventDefault();
								var r = Restrict.field = e.target;
								if(!o.mask[r.name]) return;
								if(!r.e){
										r.l = r.value.length, Restrict.inst = o, Restrict.c = e.key || 0;
										setTimeout(o.onchanged, 1);
								}
						});
				}
}
Restrict.prototype.restrict = function(e){
		var o, c = e.key, n = (o = e.target).name, r;
		var has = function(c, r){
				for(var i = r.length; i--;)
						if((r[i] instanceof RegExp && r[i].test(c)) || r[i] == c) return true;
				return false;
		}
		var inRange = function(c){
				return has(c, o.pt[0]) && !has(c, o.pt[1]);
		}
		return (c < 30 || inRange(String.fromCharCode(c))) ?
				(this.onKeyAccept && this.onKeyAccept(o, c), true) :
				(this.onKeyRefuse && this.onKeyRefuse(o, c),  false);
}
Restrict.prototype.onchanged = function(){
		var ob = Restrict, si, moz = false, o = ob.field, t, lt = (t = o.value).length, m = ob.inst.mask[o.name];
		if(o.l == o.value.length) return;
		if(si = o.selectionStart) moz = true;
		else if(o.createTextRange){
				var obj = document.selection.createRange(), r = o.createTextRange();
				if(!r.setEndPoint) return false;
				r.setEndPoint("EndToStart", obj); si = r.text.length;
		}
		else return false;
		for(var i in m = m.split(""))
				if(m[i] != "#")
						t = t.replace(m[i] == "\\" ? m[++i] : m[i], "");
		var j = 0, h = "", l = m.length, ini = si == 1, t = t.split("");
		for(i = 0; i < l; i++)
				if(m[i] != "#"){
						if(m[i] == "\\" && (h += m[++i])) continue;
						h += m[i], i + 1 == l && (t[j - 1] += h, h = "");
				}
				else{
						if(!t[j] && !(h = "")) break;
						(t[j] = h + t[j++]) && (h = "");
				}
		o.value = o.maxLength > -1 && o.maxLength < (t = t.join("")).length ? t.slice(0, o.maxLength) : t;
		if(ob.c && ob.c != 46 && ob.c != 8){
				if(si != lt){
						while(m[si] != "#" && m[si]) si++;
						ini && m[0] != "#" && si++;
				}
				else si = o.value.length;
		}
		!moz ? (obj.move("character", si), obj.select()) : o.setSelectionRange(si, si);
}

function menuSH(ID) {
	if (document.getElementById('menu'+ID).style.visibility == 'hidden') {
		document.getElementById('menu'+ID).style.visibility = 'visible';
	}
	else {
		document.getElementById('menu'+ID).style.visibility = 'hidden'; 
	}
}
