// JavaScript Document
	function Ajuda(sTexto, sDescricao){

		titulo_ajuda.innerHTML = sTexto;
		texto_ajuda.innerHTML = sDescricao;
		
	}
	function createXMLHTTP() 
	{
		var ajax;
		try 
		{
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				ajax = new ActiveXObject("Msxml2.XMLHTTP");
				alert(ajax);
			}
			catch(ex) 
			{
				try 
				{
					ajax = new XMLHttpRequest();
				}
				catch(exc) 
				{
					 alert("Esse browser não tem recursos para uso do Ajax");
					 ajax = null;
				}
			}
			return ajax;
		}
	
	
		   var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
							    "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
							    "Microsoft.XMLHTTP"];
		   for (var i=0; i < arrSignatures.length; i++) 
		   {
				try 
				{
					var oRequest = new ActiveXObject(arrSignatures[i]);
					return oRequest;
				} 
				catch (oError) 
				{
			    }
		   }
		
			   throw new Error("MSXML não está instalado em seu computador.");
	}
	
	// RECONHECE O BROWSER
var ns4 = (document.layers) ? true : false
var ie4 = (document.all) ? true : false
// FUNCOES PARA CONTROLE DE AVANCO EM CAMPOS DE TEXTO
function semTab() {
	checatab = false;
}

function comTab() {
	checatab = true;
} 

checatab = true;

// PULA PARA A CAIXA DE TEXTO DEFINIDA AO ALCANCAR O TAMANHO MAXIMO DE CARACTERES
function pula(origem, tamanho, destino) {
	if ((origem.value.length == tamanho) && checatab)
		destino.focus();
}
function filtra(obj, padrao) {
	tk    = (ie4) ? event.keyCode : event.which;
	letra = String.fromCharCode(tk);
	
	for (x = 0; x < padrao.length; x++)
		if (padrao.slice(x, x + 1) == letra) return true;
	
	return false;
}

// create the prototype on the String object
String.prototype.trim = function() {
 // skip leading and trailing whitespace
 // and return everything in between
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}



// create the prototype on the String object
String.prototype.trimLeadingZeros = function(todos) { //true, false
    if (""+todos=="undefined") todos=false;

    //tirando os zeros do começo
    var i=0;
    while ((i < this.length- (todos?0:1) ) && (this.substring(i,i+1)=='0')) i++;
    valor = this.substring(i);
	return valor;
}

function stripCharsNotInBag(bag, campo) { //campo só deve ser passado se for para alterar seu valor
	//bag = "0123456789";

	var temp="";
	if (campo==null) temp=this;
	if (campo!=null) temp=campo.value;

	var result = "";
	for (i=0; i<temp.length; i++){
		character = temp.charAt(i);
		if (bag.indexOf(character) != -1)
			result += character;
	}
	if (campo!=null && campo.value!=result) {
		campo.value=result;
	}
	return result;
}

// create the prototype on the String object
String.prototype.stripCharsNotInBag = stripCharsNotInBag;

function stripNotNumber(num) {
	return num.stripCharsNotInBag("0123456789");
}


var BASE_DATE = new Date("1997","09","07")  // 1999-out-07
var MAX_DATE = new Date("2025","01","21")   // 2025-fev-21

function ValidaData (data) {
	dt = data.value;

	if (dt.length<10) {
		alert("Tamnho inválido, digitar no formato dd/mm/aaaa.");
		data.select();
		return false;
	}

	dia = dt.substring(0,2);
	mes = dt.substring(3,5);
	ano = dt.substring(6,10);

	// month argument must be in the range 1 - 12
	// javascript month range : 0- 11
	var tempDate = new Date(ano,mes-1,dia);
		
	if ( (ano == tempDate.getFullYear()) &&
	     (mes == (tempDate.getMonth()+1)) &&
	     (dia == tempDate.getDate()) ) {
		var tmp = new Date();
		var todayDate = new Date(tmp.getFullYear(), tmp.getMonth(), tmp.getDate());

	     	//return (tempDate >= BASE_DATE && tempDate<=MAX_DATE && tempDate>=todayDate)
	     	return (tempDate >= BASE_DATE && tempDate<=MAX_DATE)
	} else {
		alert("Data inválida, digitar no formato dd/mm/aaaa.");
		data.select();
		return false;
	}
}


function formataDataDigitada(campo) {
    // retira tudo que nao eh numerico
    var temp=campo.value;
    var valor="";

    valor=stripNotNumber(temp);

    if (valor.length>8) { valor=valor.substring(0,8); }

    var j=0;
    temp="";
    for (var tam=0;tam<valor.length;tam++) {
        if (j==0) {
            temp+=valor.substring(tam,tam+1);
            if ( (tam==1) && (valor.length>2) ) { j++; temp+="/"; }
        } else if (j==1) {
            temp+=valor.substring(tam,tam+1);
            if ( (tam==3) && (valor.length>4) ) { j++; temp+="/"; }
        } else if (j==2) {
            temp+=valor.substring(tam,tam+1);
        }
    }

    if (campo.value!=temp) {
        campo.value=temp;
    }
}

function formataHoraDigitada(campo) {
    // retira tudo que nao eh numerico
    var temp=campo.value;
    var valor="";

    valor=stripNotNumber(temp);

    if (valor.length>8) { valor=valor.substring(0,8); }

    var j=0;
    temp="";
    for (var tam=0;tam<valor.length;tam++) {
        if (j==0) {
            temp+=valor.substring(tam,tam+1);
            if ( (tam==1) && (valor.length>2) ) { j++; temp+=":"; }
        } else if (j==1) {
            temp+=valor.substring(tam,tam+1);
            if ( (tam==3) && (valor.length>4) ) { j++; temp+=":"; }
        } else if (j==2) {
            temp+=valor.substring(tam,tam+1);
        }
    }

    if (campo.value!=temp) {
        campo.value=temp;
    }
}


	
function FormataNumero(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.

	RETVAL:
		The formatted number!
 **********************************************************************/
{
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number

	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign

	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);

	tmpNumStr = tmpNumStr.replace(/\./g,",");


	// Complete all decimal places
	if (decimalNum>0) {
		var iStart = tmpNumStr.indexOf(",");
		if (iStart < 0) {
			tmpNumStr+=",";
			iStart = tmpNumStr.indexOf(",");
		}

		for (i=(decimalNum-(tmpNumStr.length-iStart)); i>=0 ; i--) {
			tmpNumStr+="0";
		}
	}


	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(",");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "." + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function formataValorDigitado(campo, decimal) {
	var decimalNum=2;
	if (decimal!=null)
		decimalNum=decimal;

	var temp = FormataNumero(campo.value.stripCharsNotInBag("0123456789").trimLeadingZeros() / Math.pow(10,decimalNum), decimalNum, true, false, true);

    if (campo.value!=temp) {
        campo.value=temp;
    }
}	

function Valido(texto,valores)
{
  var valido = true;

  for (var i = 0;  i < texto.length;  i++)
  {
    var ch = texto.charAt(i);

    for (var j = 0;  j < valores.length;  j++)
      if (ch == valores.charAt(j))
        break;

    if (j == valores.length)
    {
      valido = false;
      break;
    }
  }
  return(valido);	
}
function moeda(campo, decimal) {
	var decimalNum=2;
	if (decimal!=null)
		decimalNum=decimal;

	var temp = FormataNumero(campo.value.stripCharsNotInBag("0123456789").trimLeadingZeros() / Math.pow(10,decimalNum), decimalNum, true, false, true);

    if (campo.value!=temp) {
        campo.value=temp;
    }
}	

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


