// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}

function LTrim(str)
/*
		 Propósito: Remover espaços em branco do lado esquerdo da string.
   Entrada: str - a string que queremos tirar os espaços
   função retirada do site:
   http://www.vermontsoftware.com/Javascript/trim.html
   acessado em 03 de Maio de 2004
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) 
   {

      var j=0, i = s.length;


      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
/*
		 Propósito: Remover espaços em branco do lado direito da string.
   Entrada: str - a string que queremos tirar os espaços
   função retirada do site:
   http://www.vermontsoftware.com/Javascript/trim.html
   acessado em 03 de Maio de 2004

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

function Trim(str)
/*
   Propósito: Remover espaços em branco do lado direito da string.
   Entrada: str - a string que queremos tirar os espaços
   Retorna: uma string sem espaços em branco do lado direito ou esquerdo
   função retirada do site:
   http://www.vermontsoftware.com/Javascript/trim.html
   acessado em 03 de Maio de 2004
*/
{
   return RTrim(LTrim(str));
}

function mostraTabela(spanId) {
document.getElementById(spanId).style.display="block";
}

function escondeTabela(spanId) {
document.getElementById(spanId).style.display="none";
}
 //ileila - peguei do visor e coloquei aqui
function showHideTD(td)
{
 elm=document.getElementById(td);
 if (elm.style.display=="block") elm.style.display = "none";
 else if (elm.style.display=="none") elm.style.display = "block";
}      
//fleila 
function fnAbreArqHelp(strHost, strNomeArqCompleto)
{
	strDirHelp="/help";
	if(strNomeArqCompleto.substring(0,1) != "/")
	{
		strDirHelp+="/";
	}
	var strCaminhoCompleto="http://"+strHost+strDirHelp+strNomeArqCompleto;
	window.open(strCaminhoCompleto,'Ajuda');
}

//ileila 13.09.04 
//funcão p selecionar todos os itens de uma lista html (select)
function selecionaTodosListaHtml(lista)
{
	//alert("lista.length:"+lista.length);
    for (var i = 0; i < lista.length; i++)
    {
        if(lista.options[i].value != "-1")//ignora o item -1 , caso exista
        {
            lista.options[i].selected =true;
            //alert("lista.options[i]:"+lista.options[i]);
        }
    }
}
//fleila

// giorgio 03/11/2004
// alterna de classe, utilizado nos menus para manter um intem seleciona
var styleClassSelected = null;
function setStyleClassSelected( objTag, strClass ) {
	if( styleClassSelected != null ) {
		styleClassSelected.className = styleClassSelected.noneClassName;
	}
	styleClassSelected = objTag;
	if( styleClassSelected != null ) {
		if( !styleClassSelected.noneClassName ) {
			styleClassSelected.noneClassName = styleClassSelected.className;
		}
		styleClassSelected.className = strClass;
	}
}

//ileila 09.10.04 
/*função que verifica se o número que vem como parametro é par*/
function ehNumeroPar(numero)
{
	nResultDiv=numero%2;
	bRetorno=false;
	if(nResultDiv ==0)
	{
		bRetorno=true;
	}
	return bRetorno;
}
//fleila 09.10.04 

function setBarraTitulo( strTitulo ) {
	objParentTitulo = document.getElementById( "barraTitulo");
	objParentTitulo.replaceChild( document.createTextNode( strTitulo ), objParentTitulo.firstChild );
}

function janelaPopup(mypage,id,w,h, scroll) {
	return janelaPopup(mypage,id,w,h, scroll, false);
}

//Cristiano: 11/03/2009 - Correção BUG 7489.
//Abre um popup contendo uma página html.
//Se 'modal=true' o popup eh modal, suportado no 'IE4+' e 'FF3+'.
//No 'FF2-' eh necessario uma script confiavel ou um jar assinado,
//conforme 'http://www.mhavila.com.br/topicos/web/window_modal.html'.
function janelaPopup(mypage,id,w,h, scroll, modal) {

   	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	if (scroll == null) { //isto permite que o parametro scroll seja opcional
	
		if (modal && window.showModalDialog) {
			scroll = ';scroll:no';
		} else {
			scroll = ',scrollbars=no';
		}
		
	} else {
	
		if (modal && window.showModalDialog) {
			scroll = ';scroll:' + scroll;
		} else {
			scroll = ',scrollbars=' + scroll;
		}
	}

	if (modal && window.showModalDialog) {
	
		winprops = 'dialogheight:'+h+'px;dialogWidth:'+w+'px;dialogtop:'+wint+'px;dialogleft:'+winl+'px'+scroll+';status:no;resizable:no;help:no';
		window.showModalDialog(mypage, window, winprops);
		win = null;
  
	} else {
	
		winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+scroll+',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no';
		win = window.open(mypage, id, winprops)
	}

	if ((win != null) && (parseInt(navigator.appVersion) >= 4)) {
		win.window.focus();
	}
	
	return win;
}

 //////////////////////////
/// Retorna a strings com os %s substituídos
/// Utilização
/// sprintf("Maçã é %s", "vermelha")
/// sprintf("Repolho é %s ou %s", ["branco", "roxo"])
/// sprintf("Férias na %s ou %s?", {v1: "praia", v2: "sítio"})
/// sprintf("Quero escrever %%s normalmente na %s.", "tela")
var sprintf = function(string, variaveis) {
	var string = string+"";
	if (/^string|number$/.test(typeof variaveis)) {
		string=string.replace(/^%s|([^%])%s/, "$1"+variaveis);
	} else if (/^object$/.test(typeof variaveis)) {
		for (var v in variaveis){
			string=string.replace(/^%s|([^%])%s/, "$1"+variaveis[v]);
		}
	}
	return string.replace(/%%/g, "%");
}

