// JavaScript Document

function mudarPagina(url){
 location.href = url;	
}

function cadPopup(arquivo, campoId, campoDescricao, colunaId, colunaDescricao, funcaoRetorno, janela){
 window.open(arquivo + '?loc=true&btns=true&campoId='+ campoId +'&formRetorno=formCadastro&campoDescricao='+ campoDescricao +'&colunaId='+ colunaId +'&colunaDescricao='+ colunaDescricao +'&funcaoRetorno='+funcaoRetorno, janela,"width=690,height=600,top=150,left=150,scrollbars=NO,resizable=NO") ;
}

function obterGeral(){
//	
}

function buscarInfo(evento, funcao, id, focar){

	if (evento.keyCode == 9)  {
		eval(funcao + '(' + id + ')',focar);
	 }
}

buscaOnFocus = false;
function buscaInfoPopup(funcao, id){
 if (buscaOnFocus) {		 
	//alert('entrouPOPUP');
	eval(funcao + '(' + id + ')');	
    buscaOnFocus = false;
	}
}

function popupImprimir(arquivo){
   window.open(arquivo,'Imprimir','resizable=yes,scrollbars=no,width=700,height=850');
}

function contTexto(campo, campoContador, limite) {
 if (campo.value.length > limite) 
    document.onkeydown = campo.value = campo.value.substring(0, limite);
 else campoContador.value = limite - campo.value.length;
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');
    // *     returns 3: '1234.57'
 
    var n = number, prec = decimals, dec = dec_point, sep = thousands_sep;
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    sep = sep == undefined ? ',' : sep;
 
    var s = n.toFixed(prec),
        abs = Math.abs(n).toFixed(prec),
        _, i;
 
    if (abs > 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
 
        s = _.join(dec || ',');
    }
 	if (eval(s.length) <=6)
		s  =	s.replace(/\./, ',');
    return s;
}

function round ( val, precision, mode ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +    revised by: Onno Marsman
    // *     example 1: round(1241757, -3);
    // *     returns 1: 1242000
    // *     example 2: round(3.6);
    // *     returns 2: 4
 
    // Need to support mode flags: PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD
 
    return parseFloat(parseFloat(val).toFixed(precision));
}

/*----------------------------------------------------------------------------
Formatação para qualquer mascara
-----------------------------------------------------------------------------*/
function formatar(src, mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida)
	{
		src.value += texto.substring(0,1);
	}
}

	/*Função que padroniza valor monétario*/
	function formatarValor(v){
		v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
		v=v.replace(/^([0-9]{3}\.?){3}-[0-9]{2}$/,"$1,$2");
		//v=v.replace(/(\d{3})(\d)/g,"$1,$2")
		v=v.replace(/(\d)(\d{2})$/,"$1,$2") //Coloca ponto antes dos 2 últimos digitos
		return v
	}


/*
	LIMITAR DIGITAÇÃO DE CARACTERES CAMPO MEMO
*/
function LimitarDigitacao(campo, limite)
{
	var texto = campo.value;
	if (campo.value.length > limite)
		campo.value = texto.substring(0,limite);

	document.getElementById("tamanhoString").innerHTML = campo.value.length;

}

/*
* DESABILITA CHECKBOX DE UM DETERMINADO VETOR
*/
function DesativaCheck(campo)
{
	var i = 0;
	while (i < campo.length)
	{
		campo[i].checked = false ;		
		i++;
	}

}
function goUrl(url)
{
	location.href=url;
}

/*
* COLOCA O CURSOR NA POSIÇÃO DESEJADA
*/
function setaCursor(inputEl, selStart, selEnd) { 
 if (inputEl.setSelectionRange) { 
  inputEl.focus(); 
  inputEl.setSelectionRange(selStart, selEnd); 
 } else if (inputEl.createTextRange) { 
  var range = inputEl.createTextRange(); 
  range.collapse(true); 
  range.moveEnd('character', selEnd); 
  range.moveStart('character', selStart); 
  range.select(); 
 } 
}

function showHint(e, hint)
{
	var div = document.getElementById('divHintProd');
	var html = hint;

	if (!e) var e = window.event;
	
	if (e.pageX || e.pageY) 
	{
	  posx = e.pageX;
	  posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 
	{
		posx = e.clientX;
		posy = e.clientY ;

		posx = document.body.scrollLeft + posx;
		posy = document.body.scrollTop + posy;
	}


	div.style.left= posx -205;
	div.style.top = posy-20;
	div.style.display = 'inline';
	div.style.visibility = 'visible';
	div.innerHTML = html;
}

function hiddenHint()
{
	var div = document.getElementById('divHintProd');
	div.style.display = 'none';
	div.style.visibility = 'hidden';
}
