var trCheck 	= '';

function getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY){
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else {
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		if(yScroll < windowHeight) pageHeight = windowHeight;
		else pageHeight = yScroll;
		if(xScroll < windowWidth) pageWidth = windowWidth;
		else pageWidth = xScroll;
		arrayPageSize = {pageWidth:pageWidth,pageHeight:pageHeight,windowWidth:windowWidth,windowHeight:windowHeight}
		return arrayPageSize;
}

function centralizeDivOld(element, altura) {

	var pageSize = getPageSize();

	elementMidX = document.getElementById(element).offsetWidth / 2;
	elementMidY = document.getElementById(element).offsetHeight / 2;
	baseMidX = pageSize.pageWidth/2;
	baseMidY = pageSize.pageHeigh/2;

	midx = (baseMidX - elementMidX);
	midy = (baseMidY - elementMidY);
	document.getElementById(element).style.left = midx+'px';
	document.getElementById(element).style.top = midy+'px';
}


function centralizeDiv(base,element) {
	
	var pageSize = getPageSize();

	elementMidX = document.getElementById(element).offsetWidth / 2;
	elementMidY = document.getElementById(element).offsetHeight / 2;
	baseMidX = pageSize.pageWidth/2;
	baseMidY = pageSize.pageHeigh/2;

	midx = (baseMidX - elementMidX);
	midy = (baseMidY - elementMidY);
	document.getElementById(element).style.left = midx+'px';
	document.getElementById(element).style.top = midy+'px';
}


function showDiv(div, e, pagina, divRetorno, space) {
	space = (space) ? space : 6;

	var x = 0, y = 0, posX = 0, posY = 0, largura, altura;
		
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		x = e.pageX;
		y = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
		
	largura = document.getElementById(div).style.width.split('px');
	altura  = document.getElementById(div).style.height.split('px');

	posX = ( (x - Number(largura[0]) ) < 0 ) ? x : (x - Number(largura[0]) );
	posY = (y + Number(altura[0]) > document.body.clientHeight) ? (y - Number(altura[0]) ) : y ;

	document.getElementById(div).style.top  = (posY + space)+'px';
	document.getElementById(div).style.left = (posX + 4)+'px';
	document.getElementById(div).style.display = '';
	
	//if (pagina) exibirPagina(pagina, divRetorno);
	if (pagina) $('#'+divRetorno).load(pagina);
}


function showLoading(obj){
	$(obj).html('<img src=\'img/loading.gif\' align=\'absmiddle\' /> aguarde...');
}


function pularCampo(obj, objNext, total) {
	try {
		if (document.getElementById(obj).value.length >= total) {
			document.getElementById(objNext).focus();
			document.getElementById(objNext).select();
		}
	}
	catch(e) {
	}
}


function soNumeros(e) {

	var tecla  = (window.event) ? e.keyCode : e.which;
//	alert(tecla);

	//teclas dos numemros(0 - 9) de 48 a 57
	if ( (tecla >= 48 && tecla <= 57) || (tecla == 8) || (tecla == 0) ) return true;
	else return false;
	
}


function toggleLine(div, classe) {
	if (document.getElementById(trCheck) != div) div.className = classe;
}


function popup(pagina,janela,w,h,rolagem,maxi) {
	lar = screen.width;
	alt = screen.height;
	if (maxi == '') maxi = 'no';
	if (rolagem == '') rolagem = 'no';
	var winDt = window.open(pagina,janela,'width='+w+',height='+h+',scrollbars='+rolagem+',toolbar=no,location=no,status=no,menubar=no,resizable='+maxi+',left='+((lar-w)/2)+',top='+((alt-h)/2)+'');
	winDt.focus();
}

function bloquearTela() {
	sizeWindow = getPageSize();
	var configModal = {
		id      : 'divWaiting',
		width   : $('body').width(),
		height  : sizeWindow.pageHeight,
		classe  : 'modal-waiting'
	}
	$('body').loading(configModal);
}

function closeModal(modal) {
	/* se modal = lastModal, fecha modal */
	if ((modal == lastModal) || (lastModal == null) || ($('#'+lastModal).css('display') == 'none'))	{
		$('#' + modal).hide();
		lastModal = null;
	}
	
	closeWaiting = true;
	numModal     = 0;

	/* se existir mais de uma modal aberta, nao fecha waiting */
	$('body').find('.modal').each(function() {
		if ($(this).css('display') != 'none') {
			numModal = numModal + 1;
			if (numModal > 1) {
				closeWaiting = false;
			}
		}
	});
	if (closeWaiting == true) {
		$('#divWaiting').hide();
	} else {
		$('#'+lastModal).effect('bounce', {}, 50);
	}
}

