/**
 * contextmenu
 * 
 * Função usada para mostrar um hint para links e etc...
 */
function contextmenu(str,show,e,x,y,maxwidth)
{
	var id_div = 'contextmenudiv';

	var factor = 23;
	if(!e) e = event;
	
	if(!document.getElementById(id_div))
	{
		div = document.createElement("div");
		div.setAttribute('id',id_div);
		document.body.appendChild(div);
	}
	
	document.getElementById(id_div).innerHTML = str;
	if(maxwidth && document.getElementById('contextmenudiv').offsetWidth > maxwidth)
	{
		document.getElementById('contextmenudiv').style.width = maxwidth+'px';
	}
	
	var scroll_top = 0, scroll_left = 0;

	scroll_top = (!isNaN(window.scrollY))?window.scrollY:document.documentElement.scrollTop;
	scroll_left = (!isNaN(window.scrollX))?window.scrollX:document.documentElement.scrollLeft;
	iw = (window.innerWidth)?window.innerWidth:document.documentElement.clientWidth;
	ih = (window.innerHeight)?window.innerHeight:document.documentElement.clientHeight;
	
	if(document.documentElement.scrollLeft)
	scroll_left = document.documentElement.scrollLeft;
	else if(window.scrollX)
	scroll_left = window.scrollX;
	
	xp = e.clientX + scroll_left + 10;
	yp = e.clientY + scroll_top;
	
	$(id_div).style.display = '';
	var div_height = $(id_div).offsetHeight;
	var div_width = $(id_div).offsetWidth;
	
	if(xp + div_width > iw)
		xp = iw - (div_width+10);
	if(yp + div_height > ih + scroll_top)
		yp = (ih+scroll_top) - (div_height+10);
	
	if(!isNaN(x)) xp = x;
	if(!isNaN(y)) yp = y;
	
	document.getElementById(id_div).style.top = yp+'px';
	document.getElementById(id_div).style.left = xp+'px';
	
	if(show == 'off')
	{
		document.getElementById(id_div).style.display = 'none';
	}
	if(show == 'on')
	{
		document.getElementById(id_div).style.display = '';
	}
}

function popup_window(url, sizex, sizey)
{
	if(!sizey) sizey = '600';
	if(!sizex) sizex = '800';
	dateobject = new Date();
	jan = window.open(url,"win"+dateobject.getTime(),'width='+sizex+',height='+sizey+',scrollbars=yes,resizable=no,location=no,directories=no,status=no,menubar=yes,top=150,left=150');
}


function fontup(option)
{
	if(option == 1)
	{
		document.getElementById('container').style.fontSize = '10px';
	}
	else if(option == 2)
	{
		document.getElementById('container').style.fontSize = '12px';
	}
	else if(option == 3)
	{
		document.getElementById('container').style.fontSize = '14px';
	}
}

function nl2br(str)
{
	str=str.replace(/\n/gi,"<br />");
	return str;
}

function getObject(obj)
{
	if(document.all)
		return document.all(obj);
	if(document.getElementById)
		return document.getElementById(obj);
	return null;
}

function in_array(needle, array)
{
	for (var id in array)
	{
	if(array[id] == needle)
	return true;
	}
	return false;
}


function Diversos()
{}

Diversos.prototype.getPageSize = function(){
    
    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;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){    
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }
    return {'pageWidth':pageWidth,'pageHeight':pageHeight,'windowWidth':windowWidth,'windowHeight':windowHeight};
}

Diversos.prototype.sonumeros = function(e,tipo,cut)
{
	var numeros = "0123456789";
	
	var obj;
	if(document.all)
	{
		obj = window.event.srcElement;
	}
	else
	{
		obj = e.target;
	}
	
	var keynum = null;
	if(window.event) //IE
		keynum = window.event.keyCode;
	else if(e.which) //Netscape/Firefox/Opera
		keynum = e.which;
	
	//permitindo teclas como tab, backspace, delete, shift que ficam bloqueadas pois o campo fica sendo atualizado a cada tecla pressionada
	var arrayTeclas = new Array(8,9,16,17,37,38,39,40,46);
	
	if(!in_array(keynum,arrayTeclas))
	{
		if(tipo=='data')
	    {
			numeros = "0123456789/";
		}
		else if(tipo=='hora')
	    {
			numeros = "0123456789:";
		}
		else if(tipo=='dinheiro')
	    {
			numeros = "0123456789,.";
		}
		else
		{
			numeros = "0123456789";
		}
	    str = obj.value;
	    tamanho = str.length;
	    retorno = "";
	    for ( i=0; i<tamanho; i++ ){
	    	letra = str.substr(i,1);
	    	indexof = numeros.lastIndexOf(letra);
	    	if ( indexof > -1 ){ 
	    		retorno = retorno + letra; 
	    	}
	    }
	    if(tipo=='cpf')
	    {
	    	if(retorno.length > 9)		obj.value = retorno.substr(0,3)+'.'+retorno.substr(3,3)+'.'+retorno.substr(6,3)+'-'+retorno.substr(9,2);
			else if(retorno.length > 6)	obj.value = retorno.substr(0,3)+'.'+retorno.substr(3,3)+'.'+retorno.substr(6,3);
			else if(retorno.length > 3)	obj.value = retorno.substr(0,3)+'.'+retorno.substr(3,3);
			else obj.value = retorno;
	    }
	    else if(tipo=='cnpj')
	    {
	    	if(retorno.length > 12)		obj.value = retorno.substr(0,2)+'.'+retorno.substr(2,3)+'.'+retorno.substr(5,3)+'/'+retorno.substr(8,4)+'-'+retorno.substr(12,2);
	    	else if(retorno.length > 8)	obj.value = retorno.substr(0,2)+'.'+retorno.substr(2,3)+'.'+retorno.substr(5,3)+'/'+retorno.substr(8,4);
	    	else if(retorno.length > 5)	obj.value = retorno.substr(0,2)+'.'+retorno.substr(2,3)+'.'+retorno.substr(5,3);
	    	else if(retorno.length > 2)	obj.value = retorno.substr(0,2)+'.'+retorno.substr(2,3);
	    	else obj.value = retorno;
	    }
	    else if(tipo=='cep')
	    {
	    	if(retorno.length > 5)	obj.value = retorno.substr(0,5)+'-'+retorno.substr(5,3);
	    	else obj.value = retorno;
	    }
	    else if(tipo=='rg')
	    {
	    	if(retorno.length > 7)	obj.value = retorno.substr(0,7)+'-'+retorno.substr(7,1);
	    	else obj.value = retorno;
	    }
	    else
	    {
	    	if(!cut) cut = retorno.length;
	    	obj.value = retorno.substr(0,cut);
	    }
    }
}

var Diversos = new Diversos();
