// BEGIN CAPTION //////////////////////////////////////
var left_offset = '24%';
var top_offset = 20;
var makecapvisible = false;
var title = "";
var msg = "";
var nao = 0;
function ExibeCampo(lValue){
	var painel = document.getElementById('divSenha');
	if (lValue == false){ painel.style.visibility = 'hidden';}
	if (lValue == true){ painel.style.visibility = '';}
}


function ShowCap(thetitle, themsg) 
{
	if(document.layers)
		document.captureEvents(Event.MOUSEMOVE);
			
	document.onmousemove = MouseMove;

	title = thetitle;
	msg = themsg;
switch(thetitle)
{
	case 'Categoria':
	case 'CodCedente':
	case 'CodigoDeAtividade':
	case 'GrcsOnLine':
	case 'Email':
	case 'Orçamento':
	case 'Agência':
		left_offset='5%';
		nao=1;
		break;
	default:
		nao=0;
		left_offset='24%';
}
	makecapvisible = true;			
}

function CapVisible()
{
//	var content = '<table border="0" cellpadding="0" cellspacing="0" width="236"><tr><td width="176" colspan="5" bgcolor="#CCCCCC"><img src="/img/spacer_clear.gif" width="236" height="1"></td></tr><tr><td width="1" bgcolor="#CCCCCC"><img src="/img/spacer_clear.gif" width="1" height="1"></td><td width="1" bgcolor="#999999"><img src="/img/spacer_clear.gif" width="10" height="1"></td><td width="214" class="copyglossary" bgcolor="#999999"><img src="/img/spacer_clear.gif" width="214" height="10"><br><b>' + title + ':</b> ' + msg + '<br><img src="/img/spacer_clear.gif" width="214" height="10"></td><td width="1" bgcolor="#999999"><img src="/img/spacer_clear.gif" width="10" height="1"></td><td width="1" bgcolor="#CCCCCC"><img src="/img/spacer_clear.gif" width="1" height="1"></td></tr><tr><td width="216" colspan="5" bgcolor="#CCCCCC"><img src="/img/spacer_clear.gif" width="216" height="1"></td></tr></table>';
//	var content = '<table border="0" cellpadding="0" cellspacing="0" width="236"><tr><td width="176" colspan="5" bgcolor="#CC3300"><img src="/img/spacer_clear.gif" width="236" height="1"></td></tr><tr><td width="1" bgcolor="#CC3300"><img src="/img/spacer_clear.gif" width="1" height="1"></td><td width="1" bgcolor="#FF9966"><img src="/img/spacer_clear.gif" width="10" height="1"></td><td width="214" class="copyglossary" bgcolor="#FF9966"><img src="/img/spacer_clear.gif" width="214" height="10"><br><b>' + title + ':</b> ' + msg + '<br><img src="/img/spacer_clear.gif" width="214" height="10"></td><td width="1" bgcolor="#FF9966"><img src="/img/spacer_clear.gif" width="10" height="1"></td><td width="1" bgcolor="#CC3300"><img src="/img/spacer_clear.gif" width="1" height="1"></td></tr><tr><td width="216" colspan="5" bgcolor="#CC3300"><img src="/img/spacer_clear.gif" width="216" height="1"></td></tr></table>';
//	var content = '<table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td width="75%" colspan="5" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="100%" height="1"></td></tr><tr><td width="1" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="1" height="1"></td><td width="1" bgcolor="#FF9966"><img src="/Images/spacer_clear.gif" width="10" height="1"></td><td width="85%" bgcolor="#FF9966"><img src="/Images/spacer_clear.gif" width="85%" height="1"><font face="verdana, arial" size="1"><div align="right"><br><b>' + title + ':</b><br> ' + msg + '</div></font><img src="/Images/spacer_clear.gif" width="85%" height="1"></td><td width="1" bgcolor="#FF9966"><img src="/Images/spacer_clear.gif" width="10" height="1"></td><td width="1" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="1" height="1"></td></tr><tr><td width="85%" colspan="5" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="85%" height="1"></td></tr></table>';
//	var content = '<table border="0" cellpadding="0" cellspacing="0" width="50%"><tr><td width="75%" colspan="5" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="100%" height="1"></td></tr><tr><td width="1" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="1" height="1"></td><td width="1" bgcolor="#FF9966"><img src="/Images/spacer_clear.gif" width="10" height="1"></td><td width="85%" bgcolor="#FF9966"><img src="/Images/spacer_clear.gif" width="85%" height="1"><font face="verdana, arial" size="1"><div align="right"><br><b>' + title + ':</b><br> ' + msg + '</div></font><img src="/Images/spacer_clear.gif" width="85%" height="1"></td><td width="1" bgcolor="#FF9966"><img src="/Images/spacer_clear.gif" width="10" height="1"></td><td width="1" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="1" height="1"></td></tr><tr><td width="85%" colspan="5" bgcolor="#CC3300"><img src="/Images/spacer_clear.gif" width="85%" height="1"></td></tr></table>';
	var content = '<table border="0" cellpadding="0" cellspacing="0" width="45%"><tr><td width="75%" colspan="5" bgcolor="#008000"><img src="/Images/spacer_clear.gif" width="100%" height="1"></td></tr><tr><td width="1" bgcolor="#008000"><img src="/Images/spacer_clear.gif" width="1" height="1"></td><td width="1" bgcolor="#f0fafa"><img src="/Images/spacer_clear.gif" width="10" height="1"></td><td width="85%" bgcolor="#f0fafa"><img src="/Images/spacer_clear.gif" width="85%" height="1"><font face="verdana, arial" size="1"><div align="right"><br><b>' + title + ':</b><br> ' + msg + '</div></font><img src="/Images/spacer_clear.gif" width="85%" height="1"></td><td width="1" bgcolor="#f0fafa"><img src="/Images/spacer_clear.gif" width="10" height="1"></td><td width="1" bgcolor="#008000"><img src="/Images/spacer_clear.gif" width="1" height="1"></td></tr><tr><td width="85%" colspan="5" bgcolor="#008000"><img src="/Images/spacer_clear.gif" width="85%" height="1"></td></tr></table>';
	if(document.getElementById)
	{
		var theDiv = document.getElementById("capdiv");
		if(theDiv)
		{
			theDiv.innerHTML = content;
			theDiv.style.visibility = "visible";
		}
	}
	else if(document.layers && document.capdiv) 
	{
		document.capdiv.document.write(content); 
		document.capdiv.document.close();
		document.capdiv.visibility = "visible";
	}
	else if(document.all && document.all.capdiv) 
	{
		document.all.capdiv.innerHTML = content;
		document.all.capdiv.style.visibility = "visible";  
	}
}

function HideCap() 
{
	if(document.layers)
		document.releaseEvents(Event.MOUSEMOVE);

	document.onmousemove = null;
			
	if(document.getElementById)
	{
		var theDiv = document.getElementById("capdiv");
		if(theDiv)
			theDiv.style.visibility = "hidden";
	}
	else if(document.layers && document.capdiv) 
		document.capdiv.visibility = "hidden";
	else if(document.all && document.all.capdiv) 
		document.all.capdiv.style.visibility = "hidden";  
}

function MouseMove(e) 
{
	if(document.all && document.all.capdiv) 
	{
		// var x = event.clientX;
//		var x = left_offset;
if(nao)
{
		var x = left_offset;
}
else
{
		x = event.clientX - 13;
}				
//		x += document.body.scrollTop;
		document.all.capdiv.style.left = x;
		var y = top_offset;
		y += event.clientY - 13;
		y += document.body.scrollTop;
		document.all.capdiv.style.top = y;
	}
	else if(document.getElementById)
	{
		var theDiv = document.getElementById("capdiv");
		if(theDiv)
		{
			var x = left_offset;
			theDiv.style.left = x;

			var y = top_offset;
			y += e.clientY - 8;
			y += pageYOffset;
			theDiv.style.top = y;
		}
	}
	else if(document.layers && document.capdiv) 
	{
		// var x = e.pageX;
		var x = left_offset;
		document.capdiv.left = x;
				
		var y = top_offset;
		y += e.pageY - 11;
		// y += pageYOffset;
		// y -= pageYOffset;
		document.capdiv.top = y;
	}
	
	if(makecapvisible == true)
	{
		makecapvisible = false;
		CapVisible();
	}
}
// END CAPTION //////////////////////////////////////

/**
 * formataValor   - Formata uma string para um valor monetário
 *                 De 123456 para 123.456,00
 *
 */
function formataValor( num ) {
  num = num.toString().replace(/\$|\./g,'');
  num = num.toString().replace(/\$|\,/g,'.');
  num = parseFloat( num );
  var negativo = false;
  var isDesc = false;
  
  if( isNaN( num ) )
    return "";

  if ( eval(num) < 0 ) {
    negativo = true;
    num      = Math.abs( num );
  }
  cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
  num = Math.floor( num ).toString();

  if( cents < 10 ) {
    cents = "0" + cents;
  }
    
  if ( cents > 0 )
    isDesc = true;
  
  if (num.length > 11) {
    if (isDesc) {
        num = num.substring(0,9);
        cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
          if( cents < 10 ) cents = "0" + cents;
    } else
        num = num.substring(0,9);
  }
   

  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 ) );

  valor = num + "," + cents;

  if ( negativo ) {
     valor = "-" + valor;
  }

  return valor;
}

/**
 * formataValorIndice   - Formata uma string para um valor de índice
 *                        com até oito casas decimais, enviando as
 *                        respectivas mensagens de erro caso necessário.
 */
function formataValorIndice(valor, decimais) {

  // Elimina os caracteres "." e troca o caracter "," por "." para a validação.
  valorFormatado = valor.toString().replace(/\$|\./g,'');
  valorFormatado = valorFormatado.toString().replace(/\$|\,/g,'.');
  
  // Verifica se o número é válido.
  if (trim(valorFormatado) == "") {
    return "";
  }

  // Transforma em número o valor.
  num = parseFloat(valorFormatado);
  
  // Verifica se o número é válido.
  if (isNaN(num)) {
    alert("Valor de índice inválido.")
    return "";
  }

  // Verifica a validade do expoente.
  expoente = parseInt(decimais);
  if (isNaN(expoente)) {
    alert("O número de casas decimais informado para o índice é inválido.")
    return "";
  } else {
    if ((eval(expoente) < 0) || (eval(expoente) > 8)) {
      // Não permite número de decimais negativo e nem superior a 8 (valor acumulado de índice).
      alert("O número de casas decimais do índice deve ser maior que zero e menor ou igual a 8.")
      return "";
    }
  }
  
  // Verifica se o número é negativo.
  negativo = false;
  if (eval(num) < 0) {
    negativo = true;
  }

  // Se o número começa por vírgula, assume zero à esquerda da vírgula.
  posicaoPonto = valorFormatado.indexOf('.');
  if (posicaoPonto == 0) {
    valorFormatado = "0" + valorFormatado;
  }
  posicaoPonto = valorFormatado.indexOf('.');

  // Separa a parte inteira da decimal.
  if (posicaoPonto > 0) {
    if (negativo) {
        parteInteira = valorFormatado.substring(1, posicaoPonto);
    } else {
        parteInteira = valorFormatado.substring(0, posicaoPonto);
    }
    parteDecimal = valorFormatado.substring(posicaoPonto + 1, valorFormatado.length);
  } else {
    if (negativo) {
        parteInteira = valorFormatado.substring(1, valorFormatado.length);
    } else {
        parteInteira = valorFormatado;
    }
    parteDecimal = "0";
  }
  
  if (parteInteira == "") {
    parteInteira = "0";
  }
  
  // Só permite até 16 dígitos na parte inteira (valor acumulado de índice).
  if (parteInteira.length > 16) {
    alert("O índice só pode ser informado com até " + parteInteira.length + " dígitos na parte inteira.")
    return "";
  }

  // Só permite até "expoente" dígitos na parte decimal.
  if (parteDecimal.length > expoente) {
    alert("O índice só pode ser informado com até " + expoente + " casas decimais.")
    return "";
  }
  
  // Formata a parte inteira sem zeros à esquerda. 
  inteira = parseInt(parteInteira);
  parteInteira = inteira.toString();
  
  // Formata a parte decimal com zeros à direita.
  fracao = parteDecimal;
  if (fracao.length < expoente) {
    for (var i=1; fracao.length < expoente; i++) {
      fracao = fracao + "0";
    }
  } 
   
  // Coloca os caracteres '.' para separar os milhares.
  valor = parteInteira;
  for (var i=0; i < Math.floor((valor.length - (1 + i)) / 3); i++)
    valor = valor.substring(0, valor.length - (4 * i + 3)) + "." + valor.substring(valor.length - (4 * i + 3));

  // Introduz a parte decimal.
  valor = valor + "," + fracao;

  // Introduz o sinal caso necessário.
  if (negativo) {
     valor = "-" + valor;
  }

  // Retorna o valor do índice.
  return valor;
}

