
/*
#########################################################################
#																		#
#	Funções auxiliares - utilizadas pelo Framework par tarefas básicas	#
#																		#
#########################################################################
*/

function showMessageDialog(message, message_type, redirect, onclick_extra) {
		page = location.href
		page = page.replace("#","");
		location.href = page + '#';
        var types = new Array("error", "warning", "success");
        var onclick = "document.getElementById('messageBox').style.visibility='hidden';";

        if (redirect && redirect.length > 0) {
                onclick += "location.href='"+redirect+"';";
        }
        if (onclick_extra && onclick_extra.length > 0) {
                onclick += onclick_extra;
        }

		lang = 'pt_BR';

		title = new Array();
        var messageBox = document.getElementById('messageBox');
        switch (message_type) {
        	case "error":
        	case "erro":
        		title['pt_BR'] = "Erro";
        		title['en_US'] = "Error";
        	break;
        	case "warning":
        	case "aviso":
        		title['pt_BR'] = "Aviso";
        		title['en_US'] = "Warning";
        		break;
        	case "success":
        	case "sucesso":
	        	title['pt_BR'] = "Sucesso";
	        	title['en_US'] = "Success";
	        	break;
        }

        messageBox.innerHTML = '<div class="border">' +
			'<span class="'+message_type+'Header"><div class='+message_type+'Icon></div><h2>'+title[lang]+'</h2></span>'+
			'<p>'+message+'<div class="message_button">' +
					'<a href="#" id="messageBoxLink" onclick="'+onclick+'">OK</a></div>'+
			'</p></div>';
		messageBox.style.visibility = "visible";
		document.getElementById('messageBoxLink').focus();
}

function back() {
	if (confirm("Você deseja voltar sem salvar as alterações?")) {
		history.go(-1);
	} else {
		return false;
	}
}


/*
#################################################################
#																#
#	Funções para paginação e controle de variáveis na sessão	#
#																#
#################################################################
*/

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmlhttp = false;
	}
	}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
var http = getHTTPObject();

function Load_location(endereco,reload) {
	http.open("GET", endereco, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			if (reload) {
				location.reload();
			}
		}
	}
	http.send(null);
	return false;
}

function sendFilter(varname, field, value) {
	send('page', 1, false);
	send('restrict_field', field, false);
	send(varname, value, true);
}

function sendOrderBy(field) {
	send('page', 1, false);
	send('order_by', field, true);
}

function sendLimit(number) {
	send('page', 1, false);
	send('limit', number, true);
}

function sendPage(number) {
	send('page', number, true);
}

var grupo = 'default';

function setGroup(group) {
	grupo = group;
}

function getGroup() {
	alert(grupo);
}


function send(varname, value, reload) {
	http = null;
	http = getHTTPObject();
	url = document.URL;
	path = url.substr(url.indexOf("modules"));
	tokens = path.split("/");
	var relative = '';
	for (i=1; i < tokens.length; i++) {
		relative += "../";
	}
	moduleDir = tokens[1]; //diretório do módulo - retirado da URL
	var url = encodeURI(varname) + "=" + encodeURI(value);
	if (varname == "language") {
		grupo = "system";
	}
	destino = relative + "include/session.php" + "?group="+grupo+"&moduleDir="+moduleDir+"&" + url;

	Load_location(destino, reload);


}

/*
#########################################################################
#																		#
#					Funções de Upload de arquivo						#
#																		#
#########################################################################
*/

function Upload() {

	this._win = null;
	this._opened_internally = false; // Alterado para false se o Pop-up foi aberto com o 'método' insertImage

	this.openWindow = function(url) {
		window.open(url,"upload", "width=350,height=30,top=200,left=200,resizable=yes,status=no,location=no,scrollbars=no,menubar=no");
		this._opened_internally = true;
	}

	this.getOpenedInternally = function() {
		return this._opened_internally;
	}
	
	this.insertImage = function(field_name, file) {
		url = document.URL;
		path = url.substring(0, url.indexOf("/modules"));
		tokens1 = path.split("/");
		rootDir = tokens1[(tokens1.length-1)];
		host = url.substring(0, url.indexOf(rootDir));

		module = url.substr(url.indexOf("modules"));
		tokens2 = module.split("/");
		moduleDir = tokens2[1];

		final_url = host + "_upload/" + moduleDir + "/" + file;
		len  = window.location.host.length;
		last_url = host.substring(host.indexOf(window.location.host)+len);
		
		final_url = last_url + "_upload/" + moduleDir + "/" + file;
		
		win = this.getWindow();
		if (win != null) {
			win.document.forms[0].elements[field_name].value = final_url;
			this.setWindow(null);
		} else {
			document.forms[0].elements[field_name].value = final_url;
		}
	}

	this.setWindow = function(win) {
		this._win = win;
	}

	this.getWindow = function() {
	   return this._win;
	}
}

var Upload = new Upload();


/*
#########################################################################
#																		#
#	Funções para tabelas e abas - coloração e auto-seleção de checkbox	#
#																		#
#########################################################################
*/

function OpenTab(object, id) {
	var tabobj = document.getElementById("menu-tabs");
	var tabcontent = document.getElementById("miolo");
	for(x in tabobj.getElementsByTagName("A")) {
		if(tabobj.getElementsByTagName("A")[x]) {
			if(tabobj.getElementsByTagName("A")[x].className == "tab-on") {
				tabobj.getElementsByTagName("A")[x].className = "";
				x=0;
				break
			}
		}
	}
	for(x in tabcontent.getElementsByTagName("DIV")) {
		if(tabcontent.getElementsByTagName("DIV")[x]) {
			if(tabcontent.getElementsByTagName("DIV")[x].className == "tab-content") {
				if (tabcontent.getElementsByTagName("DIV")[x].id == id) {
					tabcontent.getElementsByTagName("DIV")[x].style.display = "block";
				} else {
					tabcontent.getElementsByTagName("DIV")[x].style.display = "none";
				}
			}
		}
	}
	object.className = "tab-on";
}

function menuOpen(text_hide, text_show) {
	var menu = document.getElementById("menu").style;
	var linke = document.getElementById("menu-link");
	if(menu.display == "" || menu.display == "block") {
		menu.display = "none";
		linke.innerHTML = text_show;
	} else {
		menu.display = "block";
		linke.innerHTML = text_hide;
	}
}


function trOver(object) {

	if (object.style.backgroundColor != "rgb(255, 254, 178)" && object.style.backgroundColor != "rgb(255,254,178)") {
		object.style.backgroundColor = "#FFFFDC";
	}
}
function trOut(object) {
//	alert(object.parentNode.style.backgroundColor);
	if (object.style.backgroundColor != "rgb(255, 254, 178)" && object.style.backgroundColor != "rgb(255,254,178)") {
		object.style.backgroundColor = "";
	}
}

function selectCheck(el) {

	// provoca problemas ao clicar sobre o checkbox
	if (el.checked == 0) {
		el.parentNode.parentNode.style.backgroundColor = "";
	} else {
		el.parentNode.parentNode.style.backgroundColor = "rgb(255, 254, 178)";
	}

	/*input = el.getElementsByTagName('input');
	// provoca problemas ao clicar sobre o checkbox
	if (input.length == 1) {
		if (input[0].checked == 1) {
			input[0].checked = 0;
			el.style.backgroundColor = "";
		} else {
			input[0].checked = 1;
			el.style.backgroundColor = "rgb(226, 226, 187)";
		}
	}*/

}

function checkAll(el, id) {
	if (el.checked == true) {
		n = 1;
		color = "rgb(255, 254, 178)";
	} else {
		n = 0;
		color = "";
	}
	table = document.getElementById(id).getElementsByTagName("TR");
	for(x in table) {
		if (table[x].style) {
			table[x].style.backgroundColor = color;
		}
	}
	temp = document.getElementById(id).elements.length;

	for (i=0; i < temp; i++) {
		if (document.getElementById(id).elements[i] !== el) {
			document.getElementById(id).elements[i].checked=n;


		}
	}
}


/*
#########################################################################
#																		#
#				Tooltip para apresentação de mensagens					#
#																		#
#########################################################################
*/


function startTooltip() {
	var body = document.getElementsByTagName("BODY").item(0);
	div = document.createElement("DIV");
	div.setAttribute("id","div_tooltip");
	body.appendChild(div);
	if (ie||ns6)
	tipobj = document.all? document.all["div_tooltip"] : document.getElementById? document.getElementById("div_tooltip") : ""
	document.onmousemove=positiontip
}

var tipobj;
var offsetxpoint=0 //Customize x offset of tooltip
var offsetypoint=15 //Customize y offset of tooltip
var ie = document.all
var ns6 = document.getElementById && !document.all
var enabletip = false
window.onload = startTooltip;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function tooltip(thetext, thewidth){
	if ((ns6||ie ) && typeof tipobj != "undefined"){
		if (typeof thewidth!="undefined") {
			tipobj.style.width=thewidth+"px";
		} else {
			size = thetext.length * 5.6;
			tipobj.style.width = size +"px"
		}
		tipobj.innerHTML = thetext
		enabletip = true
		return false
	}
}

function positiontip(e){
	if (enabletip){
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		//Find out how close the mouse is to the corner of the window
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
		else if (curX<leftedge)
		tipobj.style.left="5px"
		else
		//position the horizontal position of the menu where the mouse is positioned
		tipobj.style.left=curX+offsetxpoint+"px"

		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight)
		tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
		else
		tipobj.style.top=curY+offsetypoint+"px"
		tipobj.style.visibility="visible"
	}
}

function hide(){
	if (ns6||ie && typeof tipobj != "undefined"){
		enabletip=false
		tipobj.style.visibility="hidden"
		tipobj.style.left="-1000px"
		tipobj.style.backgroundColor=''
		tipobj.style.width=''
	}
}

//setTimeout('startTooltip()',1000);
