var offsetfromcursorX=12;
var offsetfromcursorY=10;
var offsetdivfrompointerX=10; 
var offsetdivfrompointerY=14; 
document.write('<div id="dhtmltooltip"></div>'); //write out tooltip DIV
document.write('<img style="visibility:hidden;display:none" id="dhtmlpointer" src="../../../images/arrow2.gif">'); //write out pointer image
var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;
if (ie || ns6) {
	var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";
}
var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : "";


function ietruebody()
{
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function ddrivetip(thetext, thewidth, thecolor)
{
  if (ns6||ie)
  {
    if (typeof thewidth!="undefined") {tipobj.style.width=thewidth+"px";}
    tipobj.style.height="100px";
    if (typeof thecolor!="undefined" && thecolor!=="") {tipobj.style.backgroundColor=thecolor;}
    tipobj.innerHTML=thetext;
    enabletip=true;
    return false;
  }
}

function ddrivetip2(thetext, thewidth, thecolor)
{
  if (ns6||ie)
  {
    if (typeof thewidth!="undefined") {tipobj.style.width=thewidth+"px";}
    tipobj.style.height="25px";
    if (typeof thecolor!="undefined" && thecolor!=="") {tipobj.style.backgroundColor=thecolor;}
    tipobj.innerHTML=thetext;
    enabletip=true;
    return false;
  }
}

function positiontip(e)
{
  if (enabletip)
  {
    var nondefaultpos=false;
    var curX=(ns6)?e.pageX : event.x+ietruebody().scrollLeft;
    var curY=(ns6)?e.pageY : event.y+ietruebody().scrollTop;
    //Find out how close the mouse is to the corner of the window
    var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20;
    var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20;

    var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
    var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;

    var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-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=curX-tipobj.offsetWidth+"px";
      nondefaultpos=true;
    }
    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+offsetfromcursorX-offsetdivfrompointerX+"px";
      pointerobj.style.left=curX+offsetfromcursorX+"px";
    }

    //same concept with the vertical position
    if (bottomedge<tipobj.offsetHeight)
    {
      tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px";
      nondefaultpos=true;
    }
    else
    {
      tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px";
      pointerobj.style.top=curY+offsetfromcursorY+"px";
    }
    tipobj.style.visibility="visible";
    if (!nondefaultpos)
    {
      pointerobj.style.visibility="visible";
      pointerobj.style.display="block";
    }
    else
    {
      pointerobj.style.visibility="hidden";
      pointerobj.style.display="none";
    }
  }
}

function hideddrivetip()
{
  if (ns6||ie)
  {
    enabletip=false;
    tipobj.style.visibility="hidden";
    pointerobj.style.visibility="hidden";
    pointerobj.style.display ="none";
    tipobj.style.left="-1000px";
    tipobj.style.backgroundColor='';
    tipobj.style.width='';
  }
}

document.onmousemove=positiontip;

function ehNumero(s)
{
  var i;
  var dif = 0;
  for (i = 0; i < s.length; i=i+1)
  {
    var c = s.charAt(i);
    if (!((c >= "0") && (c <= "9")))
    {
     dif = 1;
    }
   }
  if (dif == 1)
   {
    return false;
   }
    return true;
}

//Seleciona todos os itens de uma lista
function SelecionarTodosItens(Lista) {
  for(i=0; i<Lista.options.length; i=i+1) {
	  Lista.options[i].selected=true;}
}

//Verifica se na textaarea não ultrapassa o valor desejado
function checkLength(field, len) { 
   if(field.value.length > len) {
      field.value = field.value.substr(0, len); }
}

function openURL(sURL, sTarget, strOpener){
  if (strOpener === null)
  {
    strOpener = "alwaysRaised=yes,resizable=yes,dependent=yes,menubar=no,location=no,status=yes,scrollbars=yes,toolbar=no,width=850,height=500";
  }
  var tempWindow = window.open("", sTarget, strOpener);
  if (sURL !== '') {tempWindow.location.href = sURL;}
  if (tempWindow != null && tempWindow.opener==null) {
  	tempWindow.opener=self;
  }
  tempWindow.focus();
} 

function openURLModal(pUrl, pWidth, pHeight) {
	if (window.showModalDialog) {
		return window.showModalDialog(pUrl, window,
		  "dialogWidth:" + pWidth + "px;dialogHeight:" + pHeight + "px");
	} else {
		try {
			netscape.security.PrivilegeManager.enablePrivilege(
			  "UniversalBrowserWrite");
			window.open(pUrl, "wndModal", "width=" + pWidth
			  + ",height=" + pHeight + ",resizable=no,modal=yes");
			return true;
		}
		catch (e) {
			alert("Script nao confiavel, nao sera possivel abrir janela modal.");
			return false;
		}
	}
}

function voltarHistorico()
{
    history.go(-1);
}

function fecharJanela()
{
  close();
}

function limparString(S){
// Deixa so' os digitos no numero
var Digitos = "0123456789";
var temp = "";
var digito = "";
  for (var i=0; i<S.length; i=i+1)  {
  digito = S.charAt(i);
    if (Digitos.indexOf(digito)>=0) {
      temp=temp+digito  }
  } //for
  return temp
}

function validarCPFCGC(s) 
{
  s = limparString(s);
  // checa se é cpf
  if (s.length == 11)
  {
    if (ehCPF(s) == false)
      {
        return false;
      }
  }
  // checa se é cgc
  else if (s.length == 14)
  {
    if (ehCGC(s) == false)
    {
      return false;
    }
  }
  else 
  {
      return false;
  }
}
	
function ehCPF(s) 
{
 var i;
 s = limparString(s);
 var c = s.substr(0,9);
 var dv = s.substr(9,2);
 var d1 = 0;
 for (i = 0; i < 9; i=i+1)
 {
   d1 += c.charAt(i)*(10-i);
 }
        if (d1 == 0) return false;
 d1 = 11 - (d1 % 11);
 if (d1 > 9) d1 = 0;
 if (dv.charAt(0) != d1)
 {
   return false;
 }

 d1 *= 2;
 for (i = 0; i < 9; i=i+1)
 {
   d1 += c.charAt(i)*(11-i);
 }
 d1 = 11 - (d1 % 11);
 if (d1 > 9) d1 = 0;
 if (dv.charAt(1) != d1)
 {
   return false;
 }
        return true;
}

function ehCGC(s)
{
 var i;
 s = limparString(s);
 var c = s.substr(0,12);
 var dv = s.substr(12,2);
 var d1 = 0;
 for (i = 0; i < 12; i=i+1)
 {
   d1 += c.charAt(11-i)*(2+(i % 8));
 }
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
 if (d1 > 9) d1 = 0;
 if (dv.charAt(0) != d1)
 {
   return false;
 }

 d1 *= 2;
 for (i = 0; i < 12; i=i+1)
 {
   d1 += c.charAt(11-i)*(2+((i+1) % 8));
 }
 d1 = 11 - (d1 % 11);
 if (d1 > 9) d1 = 0;
 if (dv.charAt(1) != d1)
 {
   return false;
 }
 return true;
}

function ehData (Data) 
{
  var dma = -1;
  var data = Array(3);
  var ch = Data.charAt(0);
  if (Data.length < 10) {
  	return false
  }
  
  for(i=0; i < Data.length && (( ch >= '0' && ch <= '9' ) || ( ch == '/' && i != 0 ) ); ){
   dma=dma+1;
   data[dma] = '';
   if(ch!='/' && i != 0) return false;
   if(i != 0 ) {
  	i=i+1;
   	ch = Data.charAt(i);
   }
   if(ch=='0') {
  	i=i+1;
   	ch = Data.charAt(i);
   }
   while( ch >= '0' && ch <= '9' ){
    data[dma] += ch;
  	i=i+1;
    ch = Data.charAt(i);
   }
  }
  if(ch!='') return false;
  if(data[0] == '' || isNaN(data[0]) || parseInt(data[0]) < 1) return false;
  if(data[1] == '' || isNaN(data[1]) || parseInt(data[1]) < 1 || parseInt(data[1]) > 12) return false;
  if(data[2] == '' || isNaN(data[2]) || ((parseInt(data[2]) < 0 || parseInt(data[2]) > 99 ) && (parseInt(data[2]) < 1900 || parseInt(data[2]) > 9999))) return false;
  if(data[2] < 50) data[2] = parseInt(data[2]) + 2000;
  else if(data[2] < 100) data[2] = parseInt(data[2]) + 1900;
  switch(parseInt(data[1])){
   case 2: { if(((parseInt(data[2])%4!=0 || (parseInt(data[2])%100==0 && parseInt(data[2])%400!=0)) && parseInt(data[0]) > 28) || parseInt(data[0]) > 29 ) return false; break; }
   case 4: case 6: case 9: case 11: { if(parseInt(data[0]) > 30) return false; break;}
   default: { if(parseInt(data[0]) > 31) return false;}
  }
  return true;
}

function ehEmail(emailStr) 
{
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")"; 
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

emailStr=emailStr.toLowerCase();

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {
  alert("Email em branco ou incorreto.");
  return false;
}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i=i+1) {
  if (user.charCodeAt(i)>127) {
    alert("Nome do usuario conteinvalidoteres invalidos.");
    return false;
   }
}
for (i=0; i<domain.length; i=i+1) {
  if (domain.charCodeAt(i)>127) {
    alert("Nome de dom&iacute;nio contem caracteres invalidos.");
    return false;
   }
}

if (user.match(userPat)==null) {
   alert("Nome do usuario invalido.");
  return false;
}
 
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
  for (var i=1;i<=4;i=i+1) {
  if (IPArray[i]>255) {
    alert("Numero de IP invalido!");
    return false;
   }
}
return true;
}
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i=i+1) {
  if (domArr[i].search(atomPat)==-1) {
    alert("Nome de dominio parece estar invalido."); 
    return false;
   }
}

if (checkTLD && domArr[domArr.length-1].length!=2 &&
    domArr[domArr.length-1].search(knownDomsPat)==-1) {
    alert("O email deve terminar com um dominio valido ou duas letras do pais.");
    return false;
}

if (len<2) {
  alert("Hostname invalido!");
  return false;
}

return true;
}


function ehValor(s)
{
 var sValor;
  sValor = s;
  sValor = sValor.replace(".", "");
  sValor = sValor.replace(",", "");
  if (ehNumero(sValor) == false)
  {
    return (false);
  }
  return true;
}

function dataExtenso() {
	DataAtual = new Date();
	Data = DataAtual.getDate();
	Dia = DataAtual.getDay();
	Mes = DataAtual.getMonth();
    Ano = DataAtual.getFullYear();
	if (Data<10)
		Data = "0" + Data;
  
	NomeDia = new Array(7);
	NomeDia[0] = "Domingo"; 
	NomeDia[1] = "Segunda-feira";
	NomeDia[2] = "Ter&ccedil;a-feira";
	NomeDia[3] = "Quarta-feira";
	NomeDia[4] = "Quinta-feira";
	NomeDia[5] = "Sexta-feira";
	NomeDia[6] = "S&aacute;bado";
  
	NomeMes = new Array(12);
	NomeMes[0] = "Janeiro";
	NomeMes[1] = "Fevereiro";
	NomeMes[2] = "Mar&ccedil;o";
	NomeMes[3] = "Abril";
	NomeMes[4] = "Maio";
	NomeMes[5] = "Junho";
	NomeMes[6] = "Julho";
	NomeMes[7] = "Agosto";
	NomeMes[8] = "Setembro";
	NomeMes[9] = "Outubro";
	NomeMes[10] = "Novembro";
	NomeMes[11] = "Dezembro";
  
	return NomeDia[Dia] + ", " + Data + " de " + NomeMes[Mes] + " de " + Ano; 
  
}

function mesReduzido(Mes) 
{
	NomeMes = new Array(12);
	NomeMes[0] = "JAN";
	NomeMes[1] = "FEV";
	NomeMes[2] = "MAR";
	NomeMes[3] = "ABR";
	NomeMes[4] = "MAI";
	NomeMes[5] = "JUN";
	NomeMes[6] = "JUL";
	NomeMes[7] = "AGO";
	NomeMes[8] = "SET";
	NomeMes[9] = "OUT";
	NomeMes[10] = "NOV";
	NomeMes[11] = "DEZ";
  
	return NomeMes[Mes]; 
  
}

function validarCpf(campoCpf) 
{   
	if (campoCpf.value.length == 0){
		return false;
	}

   if (ehCPF(campoCpf.value) == false) {
   	alert('CPF invalido!!!');
   	campoCpf.focus();
    return false;
   }
}		

function validarNumero(campoNumero) {
	if (ehNumero(campoNumero.value) == false) {
   		alert('Numero invalido!!!');
   		campoNumero.focus();
   		return false;
	}		
	return true;
}
function validarData(campoData) 
{   
	if (campoData.value.length == 0){
		return false;
	}

   if (ehData(campoData.value) == false) {
   	alert('Data invalida!!!');
   	campoData.focus();
    return false;
   }
}		

function validarValor(campoValor) 
{   
	if (campoValor.value.length == 0){
		return true;
	}

   if (ehValor(campoValor.value) == false) {
   	alert('Valor invalido!!!');
   	campoValor.focus();
    return false;
   }
	return true;
}		

function validarCep(campoCep) 
{   
	if (campoCep.value.length == 0){
		return false;
	}

	if (ehCep(campoCep.value) == false) {
		alert('CEP invalido!!!');
		campoCep.focus();
		return false;
   	}
}		

function ehCep(s){
  s = limparString(s);
  if (s.length < 8) 
{
    return false; 
}
  if (ehNumero(s) == false)
  {
    return false;
  }
}

/**
* Função para validar o browser.
* Retorna uma variavel boolean.
*
* @return IE - boolean com true = IE / false = Mozilla
*/
function validaBrowser(){
	var IE = true;
	if(window.ActiveXObject){
		IE = true;
	}else{
		IE = false;
	}
	return IE;
}

function marcaDesmarcaCheck(form,valor){ 
	for (i=0;i<form.elements.length;i++) 
 		if(form.elements[i].type == "checkbox") 
    		form.elements[i].checked=valor
} 

function float2moeda(num) {

   x = 0;

   if(num<0) {
      num = Math.abs(num);
      x = 1;
   }   if(isNaN(num)) num = "0";
      cents = Math.floor((num*100+0.5)%100);

   num = Math.floor((num*100+0.5)/100).toString();

   if(cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
         num = num.substring(0,num.length-(4*i+3))+'.'+num.substring(num.length-(4*i+3));ret = num + ',' + cents;   if (x == 1) if (num != 0 || cents != '00') ret = ' -' + ret;return ret;

}

function moeda2float(moeda){

   moeda = moeda.replace(".","");

   moeda = moeda.replace(",",".");

   return parseFloat(moeda);

}

function trunca (valor) {

	valorTruncado = parseInt(valor * 100) / 100
	return valorTruncado;
}

function getSituacaoCheque(situacao) {

	if (situacao == 'A')
		return 'Ativo'
	if (situacao == 'B')
		return 'Depositado'
	if (situacao == 'D')
		return 'Devolvido'
	if (situacao == 'C')
		return 'Compensado'
	if (situacao == 'U')
		return 'Utilizado'
	if (situacao == 'E')
		return 'Cancelado'
	if (situacao == 'P')
		return 'Pendente'
	if (situacao == '')
		return "N&atilde;o Aplicado"
}

function getSituacao(situacao) {

	if (situacao == 'A')
		return 'Ativo'
	if (situacao == 'C')
		return 'Cancelado'
	if (situacao == 'T')
		return 'Transferido'
	if (situacao == 'P')
		return 'Pendente'
	if (situacao == 'B')
		return 'Bloqueado'
  if (situacao == 'F')
    return 'Finalizado'
}

  function somaMesData(data, mesSomar, fds) {
    
    dia = data.getDate()
    mes = data.getMonth()
    ano = data.getFullYear()
    if (ano < 1900) { ano+=1900; } 
    mes = mes + mesSomar;

    var d1=new Date(ano,mes,dia);
    if (fds == false) {
      if (d1.getDay() == 0) {
        dia = dia + 1;
      } else if (d1.getDay() == 6) {
        dia = dia + 2;
      }
    } 

    var d1=new Date(ano,mes,dia);
    dia = d1.getDate()
    mes = d1.getMonth()
    ano = d1.getFullYear()
    if (ano < 1900) { ano+=1900; } 
    
    mes = mes + 1;
    dia = dia +'';
    mes = mes +'';
    ano = ano +'';
    if (dia.length == 1)
      dia = "0" + dia;
    if (mes.length == 1)
      mes = "0" + mes;
    if (ano.length == 2)
      ano = "20" + ano;
    
    return dia + "/" + mes + "/" + ano; 
  }
  
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}

function getElementsByName_iefix(tag, name) {  
       
     var elem = document.getElementsByTagName(tag);  
     var arr = new Array();  
     for(i = 0,iarr = 0; i < elem.length; i++) {  
          att = elem[i].getAttribute("name");  
          if(att == name) {  
               arr[iarr] = elem[i];  
               iarr++;  
          }  
     }  
     return arr;  
}
