var trCheck 	= '';
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";
		
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	
	num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + num + ',' + cents);
}

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;
	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 zoom_livro(id) {
	$('#div_capa_principal').load('site.php', {'mod':'zoom', 'idLivro':id});
}

function disableCtrlKeyCombination(e)
{
	//list all CTRL + key combinations you want to disable
	var forbiddenKeys = new Array('a', 'n', 'c', 'x', 'v', 'j');
	var key;
	var isCtrl;
	
	if(window.event)
	{
		key = window.event.keyCode;//IE
		isCtrl = (window.event.ctrlKey)?true:false;
	}
	else
	{
		key = e.which;     //firefox
		isCtrl = (e.ctrlKey)?true:false;
	}
	
	//if ctrl is pressed check if other key is in forbidenKeys array
	if(isCtrl)
	{
		for(i=0; i<forbiddenKeys.length; i++)
		{
			//case-insensitive comparation
			if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
			{
				return false;
			}
		}
	}
	return true;
}

function array_search (array, val) {
	for (var i = 0; i < array.length; i++) {
		if (array[i] == val) {
			return i;
		}
	}
	return false;
}

function acaoTecla(event, tecla) {
	var retorno, keynum;
	if (window.event) { //IE  
		keynum = event.keyCode  
	} else if (event.which) { // Netscape/Firefox/Opera  
		keynum = event.which  
	}  
	return (keynum == tecla ) ? true : false;
}

function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

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 closeModal(modal) {
	/* se modal = lastModal, fecha modal */
	if ((modal == lastModal) || (lastModal == null))	{
		$('#' + modal).fadeOut();
		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);
	}
}

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

function focoCadastro() {
	$('.borda-cadastro').mouseover(function() {
		$(this).addClass('borda-cadastro-active');
	}).mouseout(function() {
		$(this).removeClass('borda-cadastro-active');
	});	
}


function checa_seguranca(pass, campo){
	var senha = document.getElementById(pass).value;
	var entrada = 0;
	var resultadoado;
	
	if(senha.length < 7){
	entrada = entrada - 1;
	}
	
	if(!senha.match(/[a-z_]/i) || !senha.match(/[0-9]/)){
	entrada = entrada - 1;
	}
	
	if(!senha.match(/\W/)){
	entrada = entrada - 1;
	}
	
	if(entrada == 0){
		resultado = 'A  Segurança de sua senha é: <font color=\'#99C55D\'>EXCELENTE</font>';
	}else if(entrada == -1){
		resultado = 'A Segurança de sua senha é: <font color=\'#7F7FFF\'>BOM</font>';
	}else if(entrada == -2){
		resultado = 'A Segurança de sua senha é: <font color=\'#FF5F55\'>BAIXA</font>';
	}else if(entrada == -3){
		resultado = 'A Segurança de sua senha é: <font color=\'#A04040\'>MUITO BAIXA</font>';
	}
	document.getElementById(campo).innerHTML = resultado;
	return;
}

function check_password_safety(pwd){

	var msg = "";
	var points = pwd.length;
	//var password_info = document.getElementById('password_info');
	
	var has_letter		= new RegExp("[a-z]");
	var has_caps		= new RegExp("[A-Z]");
	var has_numbers		= new RegExp("[0-9]");
	var has_symbols		= new RegExp("\\W");
	
	if(has_letter.test(pwd)) 	{ points += 4; }
	if(has_caps.test(pwd)) 		{ points += 4; }
	if(has_numbers.test(pwd)) 	{ points += 4; }
	if(has_symbols.test(pwd)) 	{ points += 4; }
	
	
	if (pwd.length == 0) {
		msg = '<span style="color: #060;"></span>';
		$('#tblForcaSenha').css('width', '0%');		
	} else if( points >= 24 ) {
		msg = '<span style="color: #900;">Excelente</span>';
		$('#tblForcaSenha').css('width', '100%');
	} else if( points >= 16 ) {
		msg = '<span style="color: #FC3;">Médio</span>';
		$('#tblForcaSenha').css('width', '75%');
	} else if( points >= 12 ) {
		msg = '<span style="color: #690;">Fraco</span>';
		$('#tblForcaSenha').css('width', '50%');
	} else{
		msg = '<span style="color: #060;">Muito fraco</span>';
		$('#tblForcaSenha').css('width', '25%');
	}
	
	//password_info.innerHTML = msg ;
	$('#password_info').html(msg);
 
}

jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};

jQuery.fn.extend({ 
  opacidade: function(valor) //declarando assinatura do método ( nome_do_metodo: function(variaveis) )
  { 
    //inicio do corpo do método  
    this.css({
              'opacity': valor, //declaração web standards
              '-khtml-opacity': valor, // browsers baseados na engine Webkit/ KDE project
              '-moz-opacity': valor, // declaração para versões antigas do mozilla/firefox
              'filter': 'alpha(opacity='+valor*100+')' // Internet Explorer < 8( super standards )
            });
    //fim do corpo do método
  }
});