$ = function (a) { return document.getElementById(a); };

$_ = getFlashObject = getFlash = function(n) {
	if (navigator.appName.indexOf("Microsoft")!=-1) return window[n]; else return document[n];
};

/*
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
		var df;   // : DocumentFragment
		var r = this.ownerDocument.createRange();

		switch (String(sWhere).toLowerCase()) {  // convert to string and unify case
		case "beforebegin":
			r.setStartBefore(this);
			df = r.createContextualFragment(sHTML);
			this.parentNode.insertBefore(df, this);
			break;

		case "afterbegin":
			r.selectNodeContents(this);
			r.collapse(true);
			df = r.createContextualFragment(sHTML);
			this.insertBefore(df, this.firstChild);
			break;

		case "beforeend":
			r.selectNodeContents(this);
			r.collapse(false);
			df = r.createContextualFragment(sHTML);
			this.appendChild(df);
			break;

		case "afterend":
			r.setStartAfter(this);
			df = r.createContextualFragment(sHTML);
			this.parentNode.insertBefore(df, this.nextSibling);
			break;
		}
	};
}
*/

validaCpf = function(cpf) {
	cpf = cpf.replace(/\./g, "").replace(/\-/g, "");
	var rrValida = new Array(00000000000,11111111111,22222222222,33333333333,44444444444,55555555555,66666666666,77777777777,88888888888,99999999999);
	for(i=0;i<11;i++)if(cpf == rrValida[i])return false;	
	var i;
	var c = cpf.substr(0,9);
	var dv = cpf.substr(9,2);
	var d1 = 0;   
	for (i = 0; i < 9; i++) d1 += c.charAt(i)*(10-i);
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1) return false;
	d1 *= 2;
	for (i = 0; i < 9; i++) d1 += c.charAt(i)*(11-i);
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)  return false;
	return true;
}
validaCnpj = function(cnpj) {
    var i;
    var c = cnpj.substr(0,12);
    var dv = cnpj.substr(12,2);
    var d1 = 0;
    
    for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+(i % 8));
    if (d1 == 0) return false;
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(0) != d1)  return false;
    d1 *= 2;
    for (i = 0; i < 12; i++) d1 += c.charAt(11-i)*(2+((i+1) % 8));
    d1 = 11 - (d1 % 11);
    if (d1 > 9) d1 = 0;
    if (dv.charAt(1) != d1)  return false;
    return true;
}

String.prototype.format = function(casas) {
	var ret = new String('');
	while (ret.length < (casas-this.toString().length)) ret += '0';
	return ret+this;
};

String.prototype.isWhite = function() {
	if (!isVoid(this)) {
		var value = this.replace(/^\s+/m,'').replace(/\s+$/m,'');
		return (value == '');
	}
	return true;
}


String.prototype.isDate = function() {
	try {
		var arrData = new Array();
		arrData = this.split('/');
		var dia = arrData[0];
		var mes = arrData[1]-1;
		var ano = arrData[2];
		var dataEntrada = (arrData[0]*1)+'/'+(arrData[1]*1)+'/'+arrData[2];
		if (parseInt(ano) < 1920) {
			return null;
		}
		var minhaData = new Date(ano,mes,dia,12,0,0);
		var dataRetorno = minhaData.getDate()+'/'+(minhaData.getMonth()+1)+'/'+minhaData.getFullYear();
		if (dataEntrada == dataRetorno) {
			return true;
		} else {
			return null;
		}
	} catch(e) {
		return null;
	}
}


/*utilizado no calendario*/
Date.prototype.getString = function() {
	return this.getDate().toString().format(2)+'/'+(this.getMonth()+1).toString().format(2)+'/'+this.getFullYear();
};
/******************************************/


randRange = function(min, max) {
	return Math.floor(Math.random()*(max-min+1))+min;
}



/*ELEMENTOS HTML*/
function hideSelect() {
	var elems = document.getElementsByTagName("select");
	for (var x=0; x<elems.length; x++) elems[x].style.display = "none";
}
function showSelect() {
	var elems = document.body.getElementsByTagName("select");
	for (var x=0; x<elems.length; x++) elems[x].style.display = "inline";
}
/******************************************/



/*EVENTOS*/
function addEvent(obj, evType, fn){ 
	if (obj!=window)obj = $(obj);
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}
function cancelCascataEvento(e) {
	if (e != null) {
		if (is_ie) {
			e.cancelBubble = true;
		} else {
			e.stopPropagation();
		}
	}
};
/******************************************/



/*DIMENSÕES*/
unitsWindow = function() {
	// Variáveis utilizadas no objeto
	var yScroll = new Number(0);		//posição Y do scroll
	var xScroll = new Number(0);		//posição X do scroll
	var wndWidthVis = new Number(0);	//width janela, visível
	var wndHeightVis = new Number(0);	//height janela, visível
	var wndWidthHid = new Number(0);	//width janela, tamanho total visível + invisível
	var wndHeightHid = new Number(0);	//height janela, tamanho total visível + invisível
	
	this.widthVisible = function() {
		return wndWidthVis;
	}

	this.heightVisible = function() {
		return wndHeightVis;
	}

	this.widthTotal = function() {
		return wndWidthHid;
	}

	this.heightTotal = function() {
		return wndHeightHid;
	}

	this.posScrollY = function() {
		if (self.pageYOffset) yScroll = self.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop; 
		else if (document.body) yScroll = document.body.scrollTop;

		return yScroll;
	}

	this.posScrollX = function() {
		if (self.pageXOffset) xScroll = self.pageXOffset;
		else if (document.documentElement && document.documentElement.scrollLeft) xScroll = document.documentElement.scrollLeft; 
		else if (document.body) xScroll = document.body.scrollLeft; 

		return xScroll;
	}

	this.scrollToTop = function() {
		if (self.pageYOffset) self.pageYOffset = 0;
		else if (document.documentElement && document.documentElement.scrollTop) document.documentElement.scrollTop = 0; 
		else if (document.body) document.body.scrollTop = 0;

		return yScroll;
	}

	this.load = function() {
		if (window.innerWidth) wndWidthVis = window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth) wndWidthVis = document.documentElement.clientWidth;
		else if (document.body) wndWidthVis = document.body.clientWidth;

		if (window.innerHeight) wndHeightVis=window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight) wndHeightVis=document.documentElement.clientHeight;
		else if (document.body) wndHeightVis=document.body.clientHeight;
	
		wndWidthHid = document.body.clientWidth;

		wndHeightHid = document.body.clientHeight;	
	}
}
/******************************************/



/*FORMULÁRIOS*/
function verifica_email(str_email){
	if (str_email.search(/^[a-zA-Z0-9]+[_a-zA-Z0-9-]*(\.[_a-z0-9-]+)*@[a-z?G0-9]+(-[a-z?G0-9]+)*(\.[a-z?G0-9-]+)*(\.[a-z]{2,4})$/) != -1) return true;
	else return false;
}

//verifica se o campo contém o valor informado e apaga o valor no onfocus e volta o valor informado no onblur caso o campo esteja vazio
//v = valor
//a = acao (1=focus / 0=blur)
//o = campo (this)
function verValor(v,a,o){
	if(a==1)if(o.value==v)o.value='';//focus
	if(a==0)if(o.value=='')o.value=v;//blur
}
/******************************************/


function radio(object) {
	if(typeof object == 'object'){
		strChek = object.name;
	}else{
		strChek = object;
	}
	obj = document.getElementsByTagName('input');
	for (var i = 0; i< obj.length; i++){
		if (obj[i].type == 'radio' && obj[i].name == strChek  && obj[i].checked){
			return obj[i].value;
		}
	}
	return null
}


function isVoid(obj) {
	if (obj != null && typeof obj != 'undefined' && obj != "") return false;
	return true;
}

var key = 0;
function soNumeroPress(evento){
	if (key == 0)	{	
		key = evento.keyCode;	
	}	
	if ((key == 8 || key == 13 || key == 9 || key == 71 || key == 46 || key  == 37  || key  == 39) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)) {
		VerifiqueTAB=true; 
		return true;
	} else{
		return false;
	}
}

function soNumeroDown(evento){
	key = evento.keyCode;
	if ((key == 8 || key == 13 || key == 9 || key == 46) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)){
		VerifiqueTAB=true; 
		return true;
	} else {
		return false;
	}
}

VerifiqueTAB=true;
function Mostra(quem, tammax) {
	if ( (quem.value.length == tammax) && (VerifiqueTAB) ) {
		var i=0,j=0, indice=-1;
		for (i=0; i<document.forms.length; i++) {
			for (j=0; j<document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].name == quem.name) {
					indice=i;
					break;
				}
			}
			if (indice != -1)
		         break;
		}
		for (i=0; i<=document.forms[indice].elements.length; i++) {
			if (document.forms[indice].elements[i].name == quem.name) {
				while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
						(i < document.forms[indice].elements.length) ) {
							i++;
				}
				document.forms[indice].elements[(i+1)].focus();
				VerifiqueTAB=false;
				break;
			}
		}
	}
}


function setSomenteNum(obj,_event){
	eval('$(\''+obj+'\').'+_event+' = function(e) { return somenteNum(e); }');
}
function setSomenteNumVirg(obj,_event){
	eval('$(\''+obj+'\').'+_event+' = function(e) { return somenteNumVirg(e); }');
}
function somenteNumVirg(e) {
	var whichCode = getKey(e);
	if(/_13_|_8_|_9_|_36_|_35_|_46_|_110_|_188_/.test('_'+whichCode+'_')) return true;
	if((whichCode >= 48 && whichCode <= 57) || (whichCode >= 96 && whichCode <= 105) || whichCode == 110 || whichCode == 188){
		return true;
	}else{
		return false;	
	}
}
function somenteNum(e) {
	var whichCode = getKey(e);
	if(/_13_|_8_|_9_|_36_|_35_|_46_/.test('_'+whichCode+'_')) return true;
	if((whichCode >= 48 && whichCode <= 57) || (whichCode >= 96 && whichCode <= 105)){
		return true;
	}else{
		return false;	
	}
}
function getKey(e) {
	try{
		return event?(event.keyCode?event.keyCode:(event.which?event.which:event.charCode)):null;
	}catch(f){
		return e.keyCode;	
	}
}
filter2 = function(txtEntrada, strValidos) {
	var txtSaida = '';
	for (var a = 0; a<txtEntrada.length; a++) {
		if (strValidos.indexOf(txtEntrada.substr(a, 1)) != -1) {
			txtSaida = txtSaida+txtEntrada.substr(a, 1);
		}
	}
	return txtSaida;
};
mask = function(strMask, ev, objData) {
	ev = ev || event;
	var key = getKey(ev);
	if(key == 9) return;
	if (isNum(key, '%\'#$.') == true) {
		return true;
	}
	if (!(key>=37 && key<=40)) {
		var valor = filter2(objData.value, '0123456789');
		var tam = valor.length;
		var tamMask = strMask.length;
		var strOut = '';
		var intCont = 0;
		for (var a = 0; a<tamMask && intCont<=tam; a++) {
			if (strMask.substr(a, 1) == '#') {
				strOut += valor.substr(intCont++, 1);
			} else {
				strOut += strMask.substr(a, 1);
			}
		}
		objData.value = strOut;
		return true;
	}
};
isNum = function(code, strValidos) {
	var strCode = String.fromCharCode(code);
	strValidos = strValidos || '%\'#$.0123456789`abcdefghi	';
	if (strValidos.indexOf(strCode)>=0) {
		return true;
	} else {
		return false;
	}
};




function openFileIFrame(form, url)
{
	/*
	simula ajax
	abre uma página em iframe e esta página controla o js
	a pagina que utilizar esta funcao deve ter um form multipart com metodo post
	*/
	var anticache = "?anticache=" + new Date().getMilliseconds() + Math.random();
	var ts = new Date().getTime();
	var frame_name = 'frmupload'+ts;

	if (document.all && !window.opera) {
		var html = '<iframe name="'+frame_name+'" src="/media/img/blank.gif" style="width:0px;height:0px;visibility:hidden;"></iframe>';
		//var html = '<iframe name="'+frame_name+'" src="/media/img/blank.gif" style="width:1000px;height:500px;"></iframe>';
		document.body.insertAdjacentHTML('BeforeEnd', html);
	} else {
		var frame = document.createElement('IFRAME');
		frame.name = frame_name;
		frame.width = 10;
		frame.height = 10;
		frame.style.visibility = 'hidden';
		document.body.appendChild(frame);
	}

	var frm = $(form);
	//form.setAttribute('enctype', 'multipart/form-data');
	frm.target = frame_name;
	frm.action = url + anticache;
	frm.submit();
}



var galeriapub = {
	'opened': false,
	'img': '',

	'show': function(foto){
		this.opened = true;
		mensure.load();

		var DS = {x:mensure.widthVisible(), y:mensure.heightTotal()};
		$('blockbg').style.height = DS.y + 'px';
		if (!is_ie) {
			$('blockbg').style.width = (DS.x - 17) + 'px';
		} else {
			$('blockbg').style.width = DS.x + 'px';
		}
		$('blockbg').style.display = 'block';
		
		$('fotoGrande').style.display = "block";
		$('contentfotogrande').style.display = "none";

		this.alinha();
		this.loadImg(foto)
	},

	'alinha' : function(){
		if(!this.opened) return;
		var SC = {x:mensure.posScrollX(), y:mensure.posScrollY()};
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:$('fotoGrande').offsetWidth, y:$('fotoGrande').offsetHeight};
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-SZ.y)/2;
		$('fotoGrande').style.top = (newY+SC.y)+"px";
		$('fotoGrande').style.left = (newX+SC.x)+"px";
		$('fotoGrande').onclick=$('blockbg').onclick=function(){
			galeriapub.hide();
		}
	},

	'hide': function(){
		this.opened = false;
		$('fotoGrande').style.top = "-1000px";
		$('fotoGrande').style.lef = "-1000px";
		$('fotoGrande').style.display = "none";
		$('blockbg').style.display = 'none';
		$('blockbg').onclick=null;
	},

	/******************/

	'tImg':null,
	'loadedImg':false,
	'imgLoad':null,
	'checkLoadImg': function(){
		clearTimeout(this.tImg);
		if(this.loadedImg){
			$('contentfotogrande').src = this.imgLoad.src;
			$('contentfotogrande').style.display = 'block';
			$('contentfotograndeLoading').style.display = 'none';
			this.loadedImg = false;
		}else{
			this.tImg = setTimeout("galeriapub.checkLoadImg()",400);
		}
	},
	'loadImg': function(foto){
		$('contentfotogrande').style.display = 'none';
		$('contentfotograndeLoading').style.display = 'block';

		this.imgLoad = new Image(600,435);
		this.imgLoad.src = foto;
		if(this.imgLoad.complete){
			galeriapub.loadedImg = true;
			this.checkLoadImg();
			return;
		}
		this.imgLoad.onload = function(){
			galeriapub.loadedImg = true;
		}
		this.checkLoadImg();
	}

}