/**
 * contextmenu
 *  
 * Função usada para mostrar um hint para links e etc...
 */

function contextmenu(str,show,e,x,y,maxwidth)
{
	divcont.innerHTML = str;
	
	if(maxwidth && divcont.offsetWidth > maxwidth)
	{
		divcont.style.width = maxwidth+'px';
	}
	
	var top_ = divcont.style.top;
	
	if(show == 'off')
	{
		divcont.style.display = 'none';
	}
	if(show == 'on')
	{
		divcont.style.display = '';
	}
}

function moveMouse(e)
{
	scrollX = window.getScrollLeft();
	scrollY = window.getScrollTop();
	
	
	if(navigator.appName!='Microsoft Internet Explorer')
	{
		posx = e.clientX + scrollX;
		posy = e.clientY + scrollY;
	}
	else
	{
		posx = event.clientX + scrollX;
		posy = event.clientY + scrollY;
	}
	
	var id_div = 'contextmenudiv';

	if(!document.getElementById(id_div))
	{
		divcont = document.createElement("div");
		divcont.setAttribute('id',id_div);
		document.body.appendChild(divcont);
		divcont.style.display = 'none';
	}

	divcont.style.left = posx+10+'px';
	divcont.style.top = posy+10+'px';
}

document.onmousemove = moveMouse;

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()
{
	this.palavra_anterior = '';
	
	this.max_font = 1;
	this.min_font = -1;
	this.atual_font = 0;
}

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);
		}
	}
}

Diversos.prototype.campoExemplo = function(obj, texto, e)
{
	if(e.type == 'focus')
	{
		if(obj.value == texto)
		{
			this.palavra_anterior = obj.value;
			obj.value = '';
		}
	}
	else if(e.type == 'blur')
	{
		if(!obj.value)
		{
			obj.value = this.palavra_anterior;
		}
	}
}

Diversos.prototype.changecss = function(theClass, element, value, fator)
{
	if(!fator) fator = false;
	
	var cssRules;
	if (document.all) 
	{
		cssRules = 'rules';
	}
	else if (document.getElementById) 
	{
		cssRules = 'cssRules';
	}
	
	var added = false;
	for (var S = 0; S < document.styleSheets.length; S++)
	{
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++)
		{
			if (document.styleSheets[S][cssRules][R].selectorText == theClass)
			{
				if(document.styleSheets[S][cssRules][R].style[element])
				{
					if(fator == true)
					{
						coletado = parseInt(document.styleSheets[S][cssRules][R].style[element]);
						document.styleSheets[S][cssRules][R].style[element] = (coletado + value) + 'px';
					}
					else
					{
						document.styleSheets[S][cssRules][R].style[element] = value;
					}
					added=true;
					break;
				}
			}
		}
		
		if(!added)
		{
			if(document.styleSheets[S].insertRule)
			{
				document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			}
			else if (document.styleSheets[S].addRule)
			{
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
		}
	}
}

Diversos.prototype.fontUp = function(id, fator)
{
	if(fator > 0)
	{
		this.atual_font++;
	}
	else
	{
		this.atual_font--;
	}
	
	if(this.atual_font > this.max_font)
	{
		this.atual_font = this.max_font;
	}
	else if(this.atual_font < this.min_font)
	{
		this.atual_font = this.min_font;
	}
	else
	{
		Diversos.changecss(id,'fontSize',fator,true);
	}
}

Diversos.prototype.applyBorderEffect = function()
{
	if(estilo_borda_site)
	{
		var inputs = document.getElementsByTagName('input');
	
		for(var x = 0;x < inputs.length;x++)
		{
			if(inputs[x].type == 'text' && !inputs[x].getAttribute('semEfeitoBorda'))
			{
				tmp = inputs[x].name;
				inputs[x].addEvent('focus', function(){this.style.border = estilo_borda_site});
				inputs[x].addEvent('blur', function(){this.style.border = ''});
			}
		}
		
		inputs = document.getElementsByTagName('textarea');
		
		for(var x = 0;x < inputs.length;x++)
		{
			if(!inputs[x].getAttribute('semEfeitoBorda'))
			{
				inputs[x].addEvent('focus', function(){this.style.border = estilo_borda_site});
				inputs[x].addEvent('blur', function(){this.style.border = ''});
			}
		}
		
		inputs = document.getElementsByTagName('select');
		
		for(var x = 0;x < inputs.length;x++)
		{
			if(!inputs[x].getAttribute('semEfeitoBorda'))
			{
				inputs[x].addEvent('focus', function(){this.style.border = estilo_borda_site});
				inputs[x].addEvent('blur', function(){this.style.border = ''});
			}
		}
	}
}

Diversos.prototype.adicionarFavoritos = function(url, titulo)
{
	if(url.indexOf('http://') != 0)
	{
		url = 'http://' + url;
	}
		
	if (document.all && !window.opera)
	{
		window.external.AddFavorite(url,escape(titulo));
	}
	else if (navigator.userAgent.indexOf('Opera')>-1||navigator.userAgent.indexOf('Mozilla')>-1)
	{
		window.sidebar.addPanel(titulo, url,"");
	}
}

var Diversos = new Diversos();

//Agenda Menu
var t_o, idpast;
function simplemenu(son,appear)
{
	document.getElementById(son).style.display = 'block';
	
	if(appear == false)
		t_o = setTimeout('simplemenu_disapear(\''+son+'\')',400);
	else if(idpast == son)
		clearTimeout(t_o);
	idpast = son;
}

function simplemenu_disapear(son)
{
	var obj = document.getElementById(son);
	obj.style.display = 'none';
}
