<!--

//-------------------------------------
// @VERSAO 3.0 2011-03-14
// @AUTOR  DAC
// @DESC   Ativa as funções da '_forms_LIB.asp'.
//-------------------------------------

function xAccao(form, prg, acc)
//--------------------------------------- 
{
  document.getElementById('h_iAcc').value = acc;
  form.action = prg;
  form.submit();
}

function xCriar(form, prg)
//--------------------------------------- 
{
  document.getElementById('h_iKey').value = '';
  document.getElementById('h_iAcc').value = 'N';
  form.action = prg;
  form.submit();
}

function xCriarCopia(form, chave)
//--------------------------------------- 
{
  document.getElementById('h_iKey').value = chave;
  document.getElementById('h_iAcc').value = 'NC';
  form.submit();
}

function xVer(form, chave)
//---------------------------------------  
{
  document.getElementById('h_iKey').value = chave;
  document.getElementById('h_iAcc').value = 'V';
  form.submit();
}


function xAlterar(form, chave)
//---------------------------------------  
{
  document.getElementById('h_iKey').value = chave;
  document.getElementById('h_iAcc').value = 'E';
  form.submit();
}


function xFiltro(form, prg)
//--------------------------------------- 
{
  document.getElementById('h_iKey').value = '';
  document.getElementById('h_iAcc').value = 'MP';
  form.action = prg;
  form.submit();
}


function xApagar(form, chave, confDel)
//---------------------------------------  
{
  if (confDel=='') { //não confirma
      document.getElementById('h_iKey').value = chave;
      document.getElementById('h_iAcc').value = 'D';
      form.submit();
  } else {
    if (confirm(confDel)) {
      document.getElementById('h_iKey').value = chave;
      document.getElementById('h_iAcc').value = 'D';
      form.submit();
    }
  }
}


function xCancelar(form)
//---------------------------------------  
{
  if (confirm('Quer mesmo cancelar este registo ?')) {
    document.getElementById('h_iKey').value = document.getElementById('h_k').value;
    document.getElementById('h_iAcc').value = 'D2';
    form.submit();
  }
}


function xAutorizar(form, prg)
//--------------------------------------- 
{
  document.getElementById('h_tPag').value = 0;
  form.action = prg; 
  form.submit();
}


function xMarcar(form, chave, lstNome, lstNum, tipo)
//--------------------------------------- 
{
  var i, cb, cl = '';

  if (tipo=="M") { //tipo: M - multipla marcação
    //atua na checkBox se existir - rever
    cb = eval(form.name+'.cb_'+lstNome+lstNum);
    cb.checked = !cb.checked;

    if (document.getElementById(lstNome+lstNum).className=='lista-marcado-mult') {
      document.getElementById(lstNome+lstNum).className = 'lista-escolhe';
      //form.h_k.value = replaceAll(form.h_k.value, "<" + chave + ">" , "");
    } else {
      document.getElementById(lstNome+lstNum).className = 'lista-marcado-mult';
      //form.h_k.value = form.h_k.value + "<" + chave + ">" ;
    }
  } else {
    if (document.getElementById(lstNome+lstNum).className!='lista-marcado') {
      if (form.h_trid.value!="") {
	if ((form.h_trid.value % 2)==0) {cl='lista-par';} else {cl='lista-impar'};
	document.getElementById(lstNome+form.h_trid.value).className = cl;
      }
      document.getElementById(lstNome+lstNum).className = 'lista-marcado';
    }
    form.h_k.value = chave; 
    form.h_trid.value = lstNum;

    for (var i=1; i<6; i++) {
      if (document.getElementById("B80" + i) != null) {
        document.getElementById("B80" + i).disabled = false;
      }
    }

    i = 1;
    while(document.getElementById("BUmenu" + i) != null) {
      document.getElementById("BUmenu" + i).disabled = false;
      i ++;
    }
  }
}


function xOver(obj)
//--------------------------------------- 
{
  if ((eval(obj).className!='lista-marcado') && (eval(obj).className!='lista-marcado-mult')) {
    eval(obj).className = 'lista-escolhe-over';
  }
}


function xOut(obj, classAnterior)
//--------------------------------------- 
{
  if ((eval(obj).className!='lista-marcado') && (eval(obj).className!='lista-marcado-mult')) {
    eval(obj).className = classAnterior; //lista-escolhe
  }
}


function xOrder(form, tord, campo)
//--------------------------------------- 
{
  var ord = "ASC";

  if (document.getElementById(tord).value.indexOf(campo)>=0) {
    if (document.getElementById(tord).value.indexOf(ord)>=0) { 
      ord = "DESC";
    } 
  }
  document.getElementById(tord).value = campo + ";" + ord;
  document.getElementById('h_iAcc').value = '';
  form.submit();
}


function xPag(form, campo)
//--------------------------------------- 
{
  document.getElementById('h_iAcc').value = '';
  document.getElementById('h_tPag').value = campo;
  form.submit();
}


function xRPag(form, campo)
//--------------------------------------- 
{
  document.getElementById('h_iAcc').value = '';
  document.getElementById('h_tRPag').value = campo;
  document.getElementById('h_tPag').value = 1;
  form.submit();
} 


function checkAll()
//--------------------------------------- 
{
  for (var i=0;i<document.results.elements.length;i++) {
    var ele = document.results.elements[i];
    if ((ele.name != 'chkall') && (ele.type=='checkbox')) {
      ele.checked = document.results.chkall.checked;
    }
  }
}

function replaceAll(entry, out, add) 
//--------------------------------------- 
{
  var temp = "" + entry; 

  while (temp.indexOf(out)>-1) {
    pos= temp.indexOf(out);
    temp = "" + (temp.substring(0, pos) + add + 
    temp.substring((pos + out.length), temp.length));
  }
  return temp
}


//--------------------------------------- 
// <SCRIPT type=""text/javascript"" src=""tiny_mce/tiny_mce.js""></SCRIPT>
//--------------------------------------- 
function setupMCE(id) { 
tinyMCE.init({
	mode : 'exact', //none, textareas, exact
	elements : id,
	theme : 'advanced', //simple, advanced
	content_css : "_cssMCE.css",
	element_format : "html",
	body_class : "",
	language : "pt",
	skin : 'o2k7',
	skin_variant : 'silver',
	invalid_elements : "b,i,center",
	forced_root_block : "div", 
	
	plugins : "advhr,advimage,advlink,advlist,contextmenu,directionality,emotions,fullscreen,iespell,insertdatetime,layer,media,nonbreaking,noneditable,pagebreak,paste,preview,print,safari,save,style,searchreplace,table,template,visualchars,xhtmlxtras",
	//plugins : "advhr,advimage,advlink,advlist,autoresize,autosave,bbcode,contextmenu,directionality,emotions,fullpage,fullscreen,iespell,inlinepopups,insertdatetime,layer,legacyoutput,media,nonbreaking,noneditable,pagebreak,paste,preview,print,save,searchreplace,spellchecker,style,tabfocus,table,template,visualchars,wordcount,xhtmlxtras",
	
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,|,removeformat,cleanup,preview,,code", 
	theme_advanced_buttons2 : "search,replace,|,image,charmap,nonbreaking,visualchars,|,forecolor,backcolor,|,link,unlink,cite,abbr,acronym,|,undo,redo,|,fullscreen", 
	theme_advanced_buttons3 : "",

	theme_advanced_toolbar_location : "top", //external, top
	theme_advanced_toolbar_align : "left",
	//theme_advanced_statusbar_location : "bottom", 
	theme_advanced_resizing : false,
	use_native_selects : true,
 
	formats : {
		underline : {inline : 'span', 'classes' : 'destaque1', exact : true}
	}

});
}

function toggleMCE(id) {
	if (!tinyMCE.get(id)) {
	  //tinyMCE.execCommand('mceAddControl', true, id);
	  setupMCE(id);
	} else {
	  tinyMCE.execCommand('mceRemoveControl', false, id);
	}
}
//--------------------------------------- 

//---------------------------------------
// mostrar XML
//---------------------------------------
var xmlv = new TxmlViewer();
function TxmlViewer()
{
    var id;
    var mais  = "<tt style='color:#FF0000;'>+</tt>";
    var menos = "<tt style='color:#FF0000;'>-</tt>";
    
    this.elem = function() { 
	return document.getElementById(id); 
    }
    this.show = function(_id, txt) {
        id = _id;
        xmlv.init(txt);
    }
    
    this.init = function(xmltxt) {
        var xmlDoc;
        if (window.DOMParser) {
          parser = new DOMParser();
          xmlDoc = parser.parseFromString(xmltxt,"text/xml");
        } else { // IE
          xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async="false";
          xmlDoc.loadXML(xmltxt); 
        }
        if (xmlDoc.xml=="") {
	  alert("Erro no XML" + "\n\n" + xmltxt);
	  return;
	}

        this.elem().innerHTML = this.createHtml(xmlDoc.documentElement, 0);
    }
    
    this.createHtml = function(xml, level) {        
        if (xml == null) return "";

        var menor  = "<span class=cinza>&lt;</span>";
        var menorb = "<span class=cinza>&lt;/</span>";
        var maior  = "<span class=cinza>&gt;</span>";
        var maiorb = "<span class=cinza> /&gt;</span>";
	var s = "<table align=left class=termos cellspacing='0' cellpadding='3' summary='XML formatado'";
        if (level>0) s+= " style='background-color:transparent; border-left:solid 1px #aaaaaa'";
        s += ">";
        var ident = ""; //"<td class=b0 width=1><tt>&nbsp;</tt></td>"
        var p = xml;

        while (p != null) {
            if (p.nodeName == '#text') {
              s += "<tr style='background-color:transparent; vertical-align:top;'>" + ident;
              s += "<td class=b0><strong>" + p.nodeValue + "</strong>";
              s += "</td></tr>";
            }
            else if (p.nodeName == '#comment') {
              s += "<tr style='background-color:transparent; vertical-align:top;'>" + ident;
              s += "<td class=b0></td><td class=b0>";
              s += menor + "<span class=cinza>" + p.nodeName + "</span>" + maiorb;
	      //s += menor + p.nodeName + "<code class=cinza><BR>" + htmlEncode(p.nodeValue, true, 0) + "<BR></code>" + maiorb;
	      s += "</td></tr>";
            }
            else if (p.childNodes.length == 0) {
              s += "<tr style='background-color:transparent; vertical-align:top;'>" + ident;
              s += "<td class=b0></td><td class=b0>";
	      s += menor + this.showNodeName(p) + this.showXmlAttributes(p) + maiorb;
              s += "</td></tr>";
            }
            else if (p.childNodes.length == 1 && p.firstChild.nodeName == '#text') {
              s += "<tr style='background-color:transparent;'>" + ident;
	      s += "<td class=b0></td><td class=b0>";
              s += menor+this.showNodeName(p) + this.showXmlAttributes(p) + maior;
              s += "<strong>" + p.firstChild.nodeValue + "</strong>";
              s += menorb + this.showNodeName(p) + maior;
              s += "</td></tr>";
            } else {
              s += "<tr style='background-color:transparent; vertical-align:top;'>" + ident;
              s += "<td class=b0 onclick='xmlv.sh(this);' style='cursor:pointer'>" + menos + "</td><td class=b0 onclick='xmlv.sh(this.previousSibling);' style='cursor:pointer'>";
              s += menor + this.showNodeName(p) + this.showXmlAttributes(p) + maior;
              s += "</td></tr>";

              s += "<tr style='background-color:transparent; vertical-align:top;'>" + ident;
	      s += "<td class=b0></td><td class=b0>";
              s += "" + this.createHtml(p.firstChild, level+1);
              s += "</td></tr>";

              s += "<tr style='background-color:transparent; vertical-align:top;'>" + ident;
	      s += "<td class=b0></td><td class=b0>";
              s += menorb + this.showNodeName(p) + maior;
              s += "</td></tr>";
            }
            p = p.nextSibling;
        }
        s += "</table>";
        return s;
    }
    
    this.showNodeName = function(xml) {
        return "<span class=vermelho2>" + xml.nodeName + "</span>";
    }
    
    this.showXmlAttributes = function(xml) {
        var i;
        var s = "";
        if (xml.attributes == null) return s;
        for (i=0; i<xml.attributes.length; i++) {
          s += " <span class=verde2>" + xml.attributes[i].name + "</span>";
          s += "<span class=cinza>=\"</span><strong class=azul>" + xml.attributes[i].value + "</strong><span class=cinza>\"</span>";
        }
        return s;
    }
    
    this.sh = function(elem) {
        if (elem.parentNode.nextSibling == null) return;
        if (elem.parentNode.nextSibling.style.display == '') {
          elem.innerHTML = mais;
          elem.parentNode.nextSibling.style.display = 'none';
        } else {
          elem.innerHTML = menos;
          elem.parentNode.nextSibling.style.display = '';
        }
    }
}


//---------------
function htmlEncode(source, display, tabs)
//---------------
{
/*
 * HTML-Encode the supplied input
 * 
 * Parameters:
 * (String)  source    The text to be encoded.
 *
 * (boolean) display   The output is intended for display.
 *
 *                     If true:
 *                     * Tabs will be expanded to the number of spaces 
 *                       indicated by the 'tabs' argument.
 *                     * Line breaks will be converted to <br />.
 *
 *                     If false:
 *                     * Tabs and linebreaks get turned into &#____;
 *                       entities just like all other control characters.
 *
 * (integer) tabs      The number of spaces to expand tabs to.  (Ignored 
 *                     when the 'display' parameter evaluates to false.)
 */

	function special(source)
	{
		var result = '';
		for (var i = 0; i < source.length; i++)
		{
			var c = source.charAt(i);
			if (c < ' ' || c > '~')
			{
				c = '&#' + c.charCodeAt() + ';';
			}
			result += c;
		}
		return result;
	}
	
	function format(source)
	{
		// Use only integer part of tabs, and default to 4
		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
		
		// split along line breaks
		var lines = source.split(/\r\n|\r|\n/);
		
		// expand tabs
		for (var i = 0; i < lines.length; i++)
		{
			var line = lines[i];
			var newLine = '';
			for (var p = 0; p < line.length; p++)
			{
				var c = line.charAt(p);
				if (c === '\t')
				{
					var spaces = tabs - (newLine.length % tabs);
					for (var s = 0; s < spaces; s++)
					{
						newLine += ' ';
					}
				}
				else
				{
					newLine += c;
				}
			}
			// If a line starts or ends with a space, it evaporates in html
			// unless it's an nbsp.
			newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
			lines[i] = newLine;
		}
		
		// re-join lines
		var result = lines.join('<br />');
		
		// break up contiguous blocks of spaces with non-breaking spaces
		result = result.replace(/  /g, ' &nbsp;');
		
		// tada!
		return result;
	}

	var result = source;
	
	// ampersands (&)
	result = result.replace(/\&/g,'&amp;');

	// less-thans (<)
	result = result.replace(/\</g,'&lt;');

	// greater-thans (>)
	result = result.replace(/\>/g,'&gt;');
	
	if (display)
	{
		// format for display
		result = format(result);
	}
	else
	{
		// Replace quotes if it isn't for display,
		// since it's probably going in an html attribute.
		result = result.replace(new RegExp('"','g'), '&quot;');
	}

	// special characters
	result = special(result);
	
	// tada!
	return result;
}


// -->

