function MM_showHideLayers() {
  var i,p,v,obj,args=MM_showHideLayers.arguments;if(document.getElementById){
   for (i=0; i<(args.length-2); i+=3){ obj=tmt_findObj(args[i]);v=args[i+2];
   v=(v=='show')?'visible':(v='hide')?'hidden':v;
   if(obj)obj.style.visibility=v;}} else{
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }}
}

function showTip(tip)
{
	eval("dyntip"+tip+"Layer.moveTo("+(xMouse+15)+","+(yMouse+10)+")")
	eval("dyntip"+tip+"Layer.show()")	
}
function esconde(camada)
{
	MM_showHideLayers(camada,'','hide');
}

function vaiPara(url)
{
	document.location.href = url;	
}

function FormataCGC(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	// vr = document.form[campo].value;
	vr = event.srcElement.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		event.srcElement.value = vr ; }
	 	if ( (tam > 2) && (tam <= 6) ){
	 		event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 7) && (tam <= 9) ){
	 		event.srcElement.value = vr.substr( 0, tam - 6 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 10) && (tam <= 12) ){
	 		event.srcElement.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 13) && (tam <= 14) ){
	 		event.srcElement.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ;}
	}		
}
function FormataCEP(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	// vr = document.form[campo].value;
	vr = event.srcElement.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 3 ){ 
	 		event.srcElement.value = vr ; }
	 	if ( (tam > 3) && (tam <= 8) ){
	 		event.srcElement.value = vr.substr( 0, tam - 3 ) + '-' + vr.substr( tam - 3, tam ) ; }
	}		
}
function FormataTelefone(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	// vr = document.form[campo].value;
	vr = event.srcElement.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( " ", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( ")", "" );
	vr = vr.replace( "(", "" );
	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 4 ){ 
	 		event.srcElement.value = vr ; }
	 	if ( (tam > 4) && (tam <= 8) ){
	 		event.srcElement.value = vr.substr( 0, tam - 4 ) + '-' + vr.substr( tam - 4, tam ) ; }
	 	if ( (tam >= 9) && (tam < 10) ){
	 		event.srcElement.value = vr.substr( 0, tam - 8 ) + ') ' + vr.substr( tam - 8, 4 ) + '-' + vr.substr( tam - 4, tam ) ; }
	 	if ( (tam == 10)){
	 		event.srcElement.value = '(' + vr.substr( tam - 10, 3 ) + ') ' + vr.substr( tam - 7, 3 ) + '-' + vr.substr( tam - 4, tam ) ; }
	 	if ( (tam == 11)){
	 		event.srcElement.value = '(' + vr.substr( tam - 11, 3 ) + ') ' + vr.substr( tam - 8, 4 ) + '-' + vr.substr( tam - 4, tam ) ; }
		

	}		
}



function FormataData(Campo,teclapres)
{
	var tecla = teclapres.keyCode;
	//vr = document.form[Campo].value;
	vr = event.srcElement.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	if (((tecla > 47) && (tecla < 58)) || tecla == 46)
	{
		if ( tecla != 9 && tecla != 8 )
		{
			if ( tam > 2 && tam < 5 )
			{
				//	document.form[Campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
				event.srcElement.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
			}
			if ( tam >= 5 && tam <= 10 )
			{
				//	document.form[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); }
				event.srcElement.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
			}
			return true;
		}
	}
	else
	{
		alert("Digite somente números");
		event.srcElement.value = vr.substr( 0, tam - 1  )
		return false;
	}
}


function High(e)
{
	e.filters.alpha.opacity = 100;
}

function Low(e)
{
	e.filters.alpha.opacity = 60;
}
function abreJanela(endereco, nome)
{
   window.open(endereco, nome,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1');
}

// Impede Seleção
//document.onselectstart = function() { return false; }

function tmt_findObj(n){
	var x,t; if((n.indexOf("?"))>0&&parent.frames.length){t=n.split("?");
	x=eval("parent.frames['"+t[1]+"'].document.getElementById('"+t[0]+"')");
	}else{x=document.getElementById(n)}return x;
}
function abreFecha(nome,imagem)
{
  //WK Sistemas - Juarez Fernando de Souza - 15/08/2005
  objeto = tmt_findObj(nome)
  imagem = tmt_findObj(imagem)
  
  // primeiro fecha todos os menus
  var ar = document.getElementById("masterdiv").getElementsByTagName("span");
  var im = document.getElementById("masterdiv").getElementsByTagName("img");
  for (var i = 0; i < ar.length; i++)
  {
    if (ar[i].className=="Options")
    ar[i].style.display = "none";
  }

  for (var i=0; i<im.length; i++)
  {
    im[i].src="../imagens/arrow.gif";
  }

  // agora abre apenas o menu selecionado
  if (objeto.style.display=="")
  {
    objeto.style.display="none";
    imagem.src='../imagens/arrow.gif';
  }
  else
  {
    objeto.style.display="";
    imagem.src='../imagens/arrow_expanded.gif';
  }

}
function abreF4(endereco,janela)
{
  window.open(endereco, janela,'width=550, height=500, toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1');
}
function ativaGuia(nomeGuia)
{
	var objGuiaVigente = document.all.item(nomeGuia);		
	trocaEstilo(objGuiaVigente);
}

function trocaEstilo(nomeTitulo)
{	    
    var guiaVigente = 'guia_' + nomeTitulo.id;    
	var objGuiaVigente = document.all.item(guiaVigente);		
	
	var tabelas = document.getElementsByTagName("table");
	for (var x = 0; x < tabelas.length; x++)
	{			
		if (tabelas[x].id.substring(0,4) == "guia")
		{
			if (tabelas[x].id == objGuiaVigente.id)
			{
				tabelas[x].style.display = "";				
			}
			else
			{
				tabelas[x].style.display = "none";				
			}
		}
	}
	var objLinhaTitulo = document.all.item('tabelaTitulo');		
	var colunas = objLinhaTitulo.cells;
	for (var i = 0; i < colunas.length; i++)
	{
	    if (objLinhaTitulo.cells[i].id != 'nulo')
	    {
		   if (objLinhaTitulo.cells[i].id == nomeTitulo.id)
		   {					
		  	 objLinhaTitulo.cells[i].className = 'telaTituloON';
		   }
		   else
		   {
		     objLinhaTitulo.cells[i].className = 'telaTituloOFF';
		   }
	    }
	}
}

function submitForm()
{
	var btOK = document.all.item("btOK");	
	var btCancelar = document.all.item("btCancelar");
	btOK.style.display = "none";
	btCancelar.style.display = "none";
	document.forms[0].submit();	
}

function somenteNumeros(teclapres)
{
	var tecla = teclapres.keyCode;
	if (((tecla > 47) && (tecla < 58)) || tecla == 46)
		return true;
	else
		return false;
}

function RequisicaoAjax(url, callback, async) {
    if(window.XMLHttpRequest) {
        RequisicaoXML = new XMLHttpRequest();
    } else {
        RequisicaoXML = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    RequisicaoXML.onreadystatechange = callback;
    
    if (async)
        RequisicaoXML.open("GET", url, async);
    else
        RequisicaoXML.open("GET", url, false);

    RequisicaoXML.send();
}

function getElementByTag(xml, tag) {
    try {
        ret = xml.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
    } catch(e) {
        ret = "";
    }
    return ret;
}

if (typeof String.prototype.format !== 'function') {
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
              ? args[number]
              : match
            ;
        });
    };
}

if (typeof String.prototype.startsWith !== 'function') {
    String.prototype.startsWith = function (str) {
        return this.indexOf(str) == 0;
    };
}

if (typeof String.prototype.endsWith !== 'function') {
    String.prototype.endsWith = function (str) {
        return this.slice(-str.length) == str;
    };
}

var selectDisable = (function (id) {
    $(id).css({
        "-webkit-user-select": "none",
        "-khtml-user-select": "none",
        "-moz-user-select": "none",
        "-ms-user-select": "none",
        "-o-user-select": "none",
        "user-select": "none",
        "-webkit-touch-callout": "none"
    });
    $(id).attr("onselectstart", "return false;");
    $(id).attr("unselectable", "on;");
});

var msgWait = ({
    show: (function (msg, showImage) {
        if (!msg) msg = "Aguarde, carregando...";
        if (showImage === undefined || showImage === null) showImage = true;
        if ($("body > #msgWait_loading_overlay").length > 0)
            msgWait.hide();
        var msgWait_loading_overlay = $("<div id='msgWait_loading_overlay'>");
        msgWait_loading_overlay.css({
            "position": "absolute",
            "top": "0",
            "left": "0",
            "right": "0",
            "bottom": "0",
            "background-color": "#000",
            "opacity": "0.7",
            "filter": "alpha(opacity=70)"
        });
        msgWait_loading_overlay.appendTo("body");

        var msgWait_loading_message = $("<div id='msgWait_loading_message'>");
        msgWait_loading_message.css({
            "position": "absolute",
            "width": "400px",
            "height": "100px",
            "left": "50%",
            "top": "50%",
            "margin-left": "-200px",
            "margin-top": "-50px",
            "line-height": "100px",
            "text-align": "center",
            "font-size": "1.2em",
            "background-color": "#fff",
            "border": "1px solid #cccccc",
            "-webkit-border-radius": "9px",
            "-moz-border-radius": "9px",
            "-o-border-radius": "9px",
            "-khtml-border-radius": "9px",
            "border-radius": "9px"
        });
        msgWait_loading_message.html("{0} {1}".format(msg, showImage ? "<img src='../imagens/ajax-loader.gif' />" : ""));
        msgWait_loading_message.appendTo("body");

        selectDisable("body");
        selectDisable("#msgWait_loading_overlay");
        selectDisable("#msgWait_loading_message");

        msgWait_loading_overlay.css("cursor", "wait");
        msgWait_loading_message.css("cursor", "wait");
    }),

    hide: (function () {
        $("#msgWait_loading_message").remove();
        $("#msgWait_loading_overlay").remove();

        $("#msgWait_loading_message").css("cursor", "");
        $("#msgWait_loading_overlay").css("cursor", "");
    })
});

var log = (function (msg) {
    if (window.console && window.console.log)
        console.log(msg);
});
