/*==============================================================================================
--| Funções Padrão JavaScript
==============================================================================================*/
function $(id_obj) {
	return document.getElementById(id_obj);
}

function $N(name) {
	return document.getElementsByName(name);
}

function debug(texto) {
	$("debug").style.display = "block";
	$("debug").value = texto;
}

function paginacao(pagina, id_div, numero) {
	pagina += "?offset=" + numero;	
	ajaxIncludeDiv(pagina, id_div);
}

function Idioma(url, tag) {
	var retorno = '';
	var params = "AJAX=true&URL="+url+"&Tag="+tag;	
	var ajax = new AJAX();
	ajax.url = "/mercatto/geral/inc/BibliotecaXML.asp";	
	ajax.metodo = "POST";
	ajax.sincrono = false;
	ajax.params = params;
	ajax.retorno = function (texto) {
		 retorno = texto;
	}	
	ajax.conectar();	
	return retorno;
}

/*============================================================================================
--| Verifica se a tecla ENTER foi pressionada
=============================================================================================*/
function enter(evt) {
	var r = false;	
	var key_code = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode : evt.which ? evt.which : void 0;	
	if (key_code == 13) {
		r = true;
	}
	return r;
}

/*============================================================================================
--| Retorna a posição do elemento na tela
=============================================================================================*/
function getPosicaoElemento(elemID){
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}

/*============================================================================================
--| Retorna a posição X ou Y do mouse
=============================================================================================*/
function CoordenadaMouse(evt, coordenada) {
	var ie = ((navigator.userAgent.indexOf("MSIE")>-1)) ? true : false;
	var ns = ((navigator.userAgent.indexOf("Gecko")>-1)) ? true: false;
	var x;
	var y;
		
	if (ie) {
		x = (evt.clientX + document.documentElement.scrollLeft);
		y = (evt.clientY + document.documentElement.scrollTop);
	} else {
		x = evt.pageX;
		y = evt.pageY;
	}
	if (coordenada.toLowerCase() == "x") {
		return x;
	} else {
		return y;
	}
}

function AdicionaOption(combo, valor, texto) {
	
	var selecionou = false; 
	var tam = combo.length;
	
	for (var i = 0; i <= tam - 1; i++) {
		if (combo[i].value == valor) {	
			combo[i].selected = true;
			selecionou = true;
		} 
	}
	if (!selecionou) {
		combo[tam] = new Option(texto);
		combo[tam].value = valor; 
		combo.selectedIndex = tam;
	}
}

/*============================================================================================
--| Funções para exibir/ocultar uma mensagem de aguarde, informação, erro, etc.
=============================================================================================*/
var obj_aguarde = false;
function OcultaAguarde() {
	obj_aguarde = false;
	$('div_aguarde').style.display = "none";
}

function ExibeAguarde(mensagem,div) {

	mensagem = "<label class=verdana-10-escuro>" + mensagem +"  </label><img src=Imagem/aguarde.gif>";
	obj_aguarde = true;
	$(div).innerHTML = mensagem;
	$(div).style.display = "block";
}

// Exibe apenas quando solicitado
function BoxAguarde(evt, exibe) {
	var x = 0;
	var y = 0;
	if (obj_aguarde) {	
		if (!exibe) {
			$('div_aguarde').style.display = "none";
		} else {
			x = CoordenadaMouse(evt,"x");
			y = CoordenadaMouse(evt,"y");
			$('div_aguarde').style.left = (x)+'px';
			$('div_aguarde').style.top = (y+20)+'px';
			$('div_aguarde').style.display = "block";
		}
	}
}

function TrocaAspa(campo)
{
    var data = new String( campo.value );
    var wData = '';
	var cont = 0;
	
	for (i=0; i< data.length ; i++) {
	  if ( data.charAt(i) == '\'')	{
			  	wData += '`';
				cont ++;
  	  }

	  else {
		if ( data.charAt(i) == '\"' )	{		
			  	wData += '``';
				cont ++;
			}
			else {		
	  			wData += data.charAt(i);
	  		}	  		
	  }
	  
    }
	 
	if ( cont > 0 ) { 
    	// Atualiza o campo 
	    campo.value = wData;
	}
}

/*============================================================================================
--| Exibe a div do menu sobre os elementos <select> com o auxilio de um iframe
=============================================================================================*/
function ExibeMenu(numero, sim) {

	var qtd_menus = 4;
	var ul = $('ul_submenu'+numero);
	//var iframe = $("iframe_menu");
	if (sim) {
	    for (i = 1; i <= numero; i++) { // Esconde todos os menus
			if (i != numero) {
				$('ul_submenu'+i).style.display = "none";
			}
		}		
		ul.style.display = "block";	
	} else {
		ul.style.display = "none";
	}
}

function AbreMenu(pagina) {
	ExibeAguarde("Aguarde","submenu");
	ajaxIncludeDiv(pagina,"submenu");
	OcultaAguarde();
	document.getElementById("div_principal").innerHTML = "";
}
