function FormatMilhar(valor, CasaDec) {
	var Inteiro = valor.toString();
	Inteiro = (Inteiro == "")? "0" : Inteiro;
	
	if(Inteiro.length > 1){
		Inteiro = Inteiro.substring(0,Inteiro.indexOf("."));
		var Decimal = valor.toString();
		Decimal = Decimal.substring(Decimal.indexOf(".")+1,Decimal.indexOf(".")+1+CasaDec);
	}else{
		Decimal = "0";
	};
	for(ContaDec=1; Decimal.length < CasaDec; ContaDec++){
		Decimal += "0";
	};

	return Inteiro + "," + Decimal;
};

/*
Funções:
	NumeroPadrao(valor)
	TrocaTexto(Texto, Original, Substituto)
	FormataMilhar(valor)
	MudaFocoPara(ObjAtu, ProxObj)
	stringOf(strChar, intCount)
	SoNumero(Objeto)
	SoNumero2(valor)
	SoData(Objeto)
	DestacaCelulaOn(celula)
	DestacaCelulaOff(celula)
	DestacaLinhaOn(celula)
	DestacaLinhaOff(celula)
	OcultaLinha(LINHA, imagem)
	Formatar(src, mask)
	DateHourMask(objInput, intValue)
	ValidaData(Data, Obrigatorio, EhInterface)
	ValidaHora(Hora, Obrigatorio, EhInterface)
	IniciarEspecial(Tag)
	LimitaTamanho(objeto, TamanhoLimite, EhInterface)
	isReal(strValue)
	isAlfabetic(strValue)
	isNumeric(strValue)
	testRegExp(strValue, strRegExp, strFlags)
	LimpaTexto(strValue)
	LimpaTexto2(objeto)
	ContaCaracter(objeto_texto, objeto_mostrador, valor_limite)
	OptionDePara(SelectDe, SelectPara)
	DataAnteriorHoje(Data1)
	DataPosteriorHoje(Data1)
	DataIgualHoje(Data1)
	ComparaDatas(Data1, comparador, Data2)
	NomeDoMes(nMes)
	CentralizaObjeto(oObjeto)
	wait(intervalo)
*/
var CorDoFundo, CorDaFonte;
var LinhaCorDoFundo, LinhaCorDaFonte;

function NumeroPadrao(valor){
	valor = TrocaTexto(valor, '.', '');
	valor = TrocaTexto(valor, ',', '.');
	return valor;
};

function TrocaTexto(Texto, Original, Substituto){
	if(Texto != '') {
		do{Texto = Texto.replace(Original, Substituto);}
		while(Texto.indexOf(Original) != -1);
	};
	return(Texto);
};

function FormataMilhar(valor) {
		valor = "" + Math.round(valor * Math.pow(10,2));
		var Int,Dec;
		var ContDec = 0;
		var aux = '';
		while(valor.length<=2){
			valor = '0'+ valor;
		}
		Int = valor.substr(0,valor.length-2);
		Dec = valor.substr(valor.length-2,2);

		for ( var i=Int.length + 1; i >= 0; i--){
			ContDec++;
			if (ContDec == 3){
				ContDec = 0;
				aux = aux + '.';
			}
			aux += Int.charAt(i);
		} 
		if (aux.charAt(0) == '.') { aux = aux.substr(1,aux.length-1) };
		if (aux.charAt(aux.length-1) == '.') { aux = aux.substr(0,aux.length-2)};
		Int='';
		for (var i = aux.length+1; i >= 0; i--){
			Int += aux.charAt(i);
		}
		
	return Int + ',' + Dec;
};

function MudaFocoPara(ObjAtu, ProxObj){
	if(ObjAtu.type == "text"){
		if (ObjAtu.value.length == ObjAtu.maxLength){
			ProxObj.focus();
		};
	};
};

function stringOf(strChar, intCount)
{
	var strReturn = "";
	for(var i = 0; i < intCount; i += strChar.length){strReturn += strChar}
	return(strReturn);
};

function SoNumero(Objeto){
	var CaracNumerico = '0123456789,';
	var Retorno = '';
	var Texto = Objeto.value;
	var ContaVirgula = 0;
	if(Texto != '') {
		for(Ctd = 0; Ctd<=Texto.length-1; Ctd++){
			if( CaracNumerico.indexOf(Texto.charAt(Ctd)) != -1 ){
				if (Texto.charAt(Ctd) == ',') {
					ContaVirgula++;
					if (ContaVirgula == 1){
						if (Ctd == 0){Retorno += '0'};
						Retorno += Texto.charAt(Ctd);
					};
				}else{
					Retorno += Texto.charAt(Ctd);
				};
			};
		};
		Objeto.value = '';
		Objeto.value = Retorno;
	};
};

function SoNumero2(valor){
	var CaracNumerico = '0123456789,';
	var Retorno = '';
	var Texto = valor;
	var ContaVirgula = 0;
	if(Texto != '') {
		for(Ctd = 0; Ctd<=Texto.length-1; Ctd++){
			if( CaracNumerico.indexOf(Texto.charAt(Ctd)) != -1 ){
				if (Texto.charAt(Ctd) == ',') {
					ContaVirgula++;
					if (ContaVirgula == 1){
						if (Ctd == 0){Retorno += '0'};
						Retorno += Texto.charAt(Ctd);
					};
				}else{
					Retorno += Texto.charAt(Ctd);
				};
			};
		};
		valor = Retorno;
	};
	return valor;
};

function SoData(Objeto){
	var CaracNumerico = '0123456789/';
	var Retorno = '';
	var Texto = Objeto.value;
	var ContaBarra = 0;
	if(Texto != '') {
		for(Ctd = 0; Ctd<=Texto.length-1; Ctd++){
			if( CaracNumerico.indexOf(Texto.charAt(Ctd)) != -1 ){
				if (Texto.charAt(Ctd) == '/') {
					ContaBarra++
					if (ContaBarra <= 2){
						Retorno += Texto.charAt(Ctd)
					};
				}else{
					Retorno += Texto.charAt(Ctd);
				};
			};
		};
		Objeto.value = '';
		Objeto.value = Retorno;
	};
};

function DestacaCelulaOn(celula){
	CorDoFundo = celula.style.backgroundColor;
	CorDaFonte = celula.style.color;
	celula.style.color = 'black';
	celula.style.backgroundColor = 'silver';
};

function DestacaCelulaOff(celula){
	celula.style.backgroundColor = CorDoFundo;
	celula.style.color = CorDaFonte;
};

function DestacaLinhaOn(celula){
	LinhaCorDoFundo = celula.parentElement.style.backgroundColor;
	celula.parentElement.style.backgroundColor = 'silver';
};

function DestacaLinhaOff(celula){
	celula.parentElement.style.backgroundColor = LinhaCorDoFundo;
};

function OcultaLinha(LINHA, imagem){
	if (LINHA.style.display == 'none') {
		LINHA.style.display = '';
		imagem.src = "http://sistemasnet/psv/Imagens/Outros/out_menos.gif";
		imagem.alt = 'Recolhe';
	}else{
		LINHA.style.display = 'none';
		imagem.src = "http://sistemasnet/psv/Imagens/Outros/out_mais.gif";
		imagem.alt = 'Exibe';
	};
};

function Formatar(src, mask){
	var i = src.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i);
	if (texto.substring(0,1) != saida){
		src += texto.substring(0,1);
	};
	return src;
};

// Insere a máscara de data no campo
function DateHourMask(objInput, intValue)
{
   var validos = "0123456789";  
   var len   = objInput.value.length;
   var keyEntered = objInput.value.substring(len - 1, len);

   if (validos.indexOf(keyEntered) == -1) objInput.value = objInput.value.substring(0, len - 1);
   len   = objInput.value.length;
   if(intValue == 1){ 	
		switch (len)
		{
		   case  2: objInput.value += "/"; break;
		   case  5: objInput.value += "/"; break;
		}     
   }
   if(intValue == 2){ 	
		switch (len)
		{
			case  2: objInput.value += ":"; break;      
		}     
	}
};

function ValidaData(Data, Obrigatorio, EhInterface){
	var situacao = true;
	if (Data.value != "") {
		var dia = (Data.value.substring(0,2));
		var mes = (Data.value.substring(3,5));
		var ano = (Data.value.substring(6,10));
		// verifica o dia valido para cada mes 
		if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
			situacao = false;
		};
		// verifica se o mes e valido 
		if (mes < 01 || mes > 12 ) { 
			situacao = false;
		};
		// verifica se e ano bissexto 
		if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4, 10) != ano / 4)))) { 
			situacao = false;
		};
	};
	if (Data.value == "" && Obrigatorio == true) {
		situacao = false;
	};

	if (situacao == false && EhInterface == true){
		if (Data.value=='' && Obrigatorio){
			alert('Data obrigatória.');
		}else{
			alert('Data inválida.');
		};
		Data.focus();
	};
	return (situacao);
};

function ValidaHora(Hora, Obrigatorio, EhInterface){
	var situacao = true;
	if (Hora.value != "") {
		var vHora = (Hora.value.substring(0,2));
		var vMinuto = (Hora.value.substring(3,2));
		// verifica a hora valida
		if (vHora > 23 || vHora < 0 || vMinuto > 59 || vMinuto < 0) { 
			situacao = false;
		};
	};
	if (Hora.value == "" && Obrigatorio == true) {
		situacao = false;
	};

	if (situacao == false && EhInterface == true){
		if (Hora.value=='' && Obrigatorio){
			alert('Hora obrigatória.');
		}else{
			alert('Hora inválida.');
		};
		Hora.focus();
	};
	return (situacao);
};

function IniciarEspecial(Tag){
	//var Util =  new XMLUtil();
	for(var i = 1 ; i < arguments.length; i++){
		if (document.all(arguments[i])){
			var InicializaDepois = document.all(arguments[i]).InicializaDepois;
			if (InicializaDepois){
				try{
					//InicializaDepois = Util.replaceAll(InicializaDepois, 'this', 'document.all[\''+ arguments[i] +'\']');
					InicializaDepois = TrocaTexto(InicializaDepois, Original, 'this', 'document.all[\''+ arguments[i] +'\']'); //Util.replaceAll(InicializaDepois, 'this', 'document.all[\''+ arguments[i] +'\']');
					eval(InicializaDepois);
				}catch(e){
					alert(e.description);
				};
			};
		};
	};

	var tags = document.all.tags(Tag);
	for(var i = 0; i < tags.length; i++){
		if (tags[i]){
			var InicializaDepois = tags[i].InicializaDepois;
			if (InicializaDepois){
				try{
					//InicializaDepois = Util.replaceAll(InicializaDepois, 'this', '(document.all.tags(\''+ Tag +'\'))[' + i + ']');
					InicializaDepois = TrocaTexto(InicializaDepois, 'this', '(document.all.tags(\''+ Tag +'\'))[' + i + ']');
					eval(InicializaDepois);
				}catch(e){
					alert(e.description);
				};
			};
		};
	};
};

function LimitaTamanho(objeto, TamanhoLimite, EhInterface){
	var TamanhoAtual = objeto.value.length;
	if (TamanhoAtual > TamanhoLimite){
		if (EhInterface){
			alert("O texto excedeu o limite de " + TamanhoLimite + " caracteres e será truncado.")
		};
		objeto.value = objeto.value.substr(0, TamanhoLimite);
		objeto.focus();
	};
};

function isReal(strValue){
	return(!testRegExp(strValue, "[^0-9.,]", "i"));
};

function isAlfabetic(strValue){
	return(!testRegExp(strValue, "[^A-Z]", "i"));
};

function isNumeric(strValue){
	return(!testRegExp(strValue, "\\D", ""));
};

function testRegExp(strValue, strRegExp, strFlags){
	var objRegExp = new RegExp(strRegExp, strFlags);
	return(objRegExp.test(strValue));
};

function LimpaTexto(strValue){
	strValue = TrocaTexto(strValue,"'","`");
	strValue = TrocaTexto(strValue,'"',"`");
	strValue = TrocaTexto(strValue,"&","e");
	strValue = TrocaTexto(strValue,"INSERT","");
	strValue = TrocaTexto(strValue,"DELETE","");
	strValue = TrocaTexto(strValue,"UPDATE","");
	strValue = TrocaTexto(strValue,"sp_","");
	strValue = TrocaTexto(strValue,"=","");
	return strValue;
};

function LimpaTexto2(objeto){
	var VALOR = objeto.value;
	VALOR = TrocaTexto(VALOR,"'","`");
	VALOR = TrocaTexto(VALOR,'"',"`");
	VALOR = TrocaTexto(VALOR,"&","e");
	strValue = TrocaTexto(strValue,"INSERT","");
	strValue = TrocaTexto(strValue,"DELETE","");
	strValue = TrocaTexto(strValue,"UPDATE","");
	strValue = TrocaTexto(strValue,"sp_","");
	strValue = TrocaTexto(strValue,"=","");
	objeto.value = VALOR;
};

function ContaCaracter(objeto_texto, objeto_mostrador, valor_limite){
	var Texto_Objeto = String(objeto_texto.value);
	objeto_mostrador.value = Texto_Objeto.length + " de " + valor_limite + " caracteres.";
};

function SoHora(Objeto){
	var CaracNumerico = '0123456789:';
	var Retorno = '';
	var Texto = Objeto.value;
	var ContaVirgula = 0;
	if(Texto != '') {
		for(Ctd = 0; Ctd<=Texto.length-1; Ctd++){
			if( CaracNumerico.indexOf(Texto.charAt(Ctd)) != -1 ){
				if (Texto.charAt(Ctd) == ':') {
					ContaVirgula++;
					if (ContaVirgula == 1){
						if (Ctd == 0){Retorno += '00'};
						Retorno += Texto.charAt(Ctd);
					};
				}else{
					Retorno += Texto.charAt(Ctd);
				};
			};
		};
		Objeto.value = '';
		Retorno = (Retorno == ':')? '00:00' : Retorno;
		Objeto.value = Retorno;
	};
};

function OptionDePara(SelectDe, SelectPara){
	var fRETORNO = '';
	if (SelectDe.selectedIndex >= 0){
		if (SelectDe.item(SelectDe.selectedIndex).getAttribute("value")){
			var OPCAO = document.createElement("OPTION");
			OPCAO.text = SelectDe.item(SelectDe.selectedIndex).getAttribute("text");
			OPCAO.value = SelectDe.item(SelectDe.selectedIndex).getAttribute("value");
			fRETORNO = OPCAO.value;
			SelectPara.add(OPCAO);
		};
		SelectDe.item(SelectDe.selectedIndex).removeNode();

		if (SelectDe.children.length > 0){
			if (SelectDe.item(0).attributes.item("value").value == ''){
				SelectDe.item(0).removeNode();
			};
		};

		if (SelectDe.children.length == 0){
			var OPCAO = document.createElement("OPTION");
			OPCAO.text = "LISTA VAZIA";
			OPCAO.value = "";
			SelectDe.add(OPCAO);
		};

		if (SelectPara.children.length > 0){
			if (SelectPara.item(0).attributes.item("value").value == ''){
				SelectPara.item(0).removeNode();
			};
		};

		if (SelectPara.children.length == 0){
			var OPCAO = document.createElement("OPTION");
			OPCAO.text = "LISTA VAZIA";
			OPCAO.value = "";
			SelectPara.add(OPCAO);
		};
	};

	return fRETORNO;
};

function SoDigitos(Objeto){
	var CaracNumerico = '0123456789';
	var Retorno = '';
	var Texto = Objeto.value;
	var ContaVirgula = 0;
	if(Texto != '') {
		for(Ctd = 0; Ctd<=Texto.length-1; Ctd++){
			if( CaracNumerico.indexOf(Texto.charAt(Ctd)) != -1 ){
				if (Texto.charAt(Ctd) == ',') {
					ContaVirgula++;
					if (ContaVirgula == 1){
						if (Ctd == 0){Retorno += '0'};
						Retorno += Texto.charAt(Ctd);
					};
				}else{
					Retorno += Texto.charAt(Ctd);
				};
			};
		};
		Objeto.value = '';
		Objeto.value = Retorno;
	};
};

function DataAnteriorHoje(Data1){
	var dia1 = parseInt(Data1.substring(0,2), 10);
	var mes1 = parseInt(Data1.substring(3,5), 10);
	var ano1 = parseInt(Data1.substring(6,10), 10);
	var DataInformada = (ano1*10000) + (mes1*100) + dia1;

	var Hoje = new Date();
	var dia2 = Hoje.getDate();
	var mes2 = Hoje.getMonth()+1;
	var ano2 = Hoje.getFullYear();
	var DataHoje = (ano2*10000) + (mes2*100) + dia2;

	var Retorno = (DataInformada < DataHoje)? 'S' : 'N';
	return(Retorno);
};

function DataPosteriorHoje(Data1){
	var dia1 = parseInt(Data1.substring(0,2), 10);
	var mes1 = parseInt(Data1.substring(3,5), 10);
	var ano1 = parseInt(Data1.substring(6,10), 10);
	var DataInformada = (ano1*10000) + (mes1*100) + dia1;

	var Hoje = new Date();
	var dia2 = Hoje.getDate();
	var mes2 = Hoje.getMonth()+1;
	var ano2 = Hoje.getFullYear();
	var DataHoje = (ano2*10000) + (mes2*100) + dia2;

	var Retorno = (DataInformada > DataHoje)? 'S' : 'N';
	return(Retorno);
};

function DataIgualHoje(Data1){
	var dia1 = parseInt(Data1.substring(0,2), 10);
	var mes1 = parseInt(Data1.substring(3,5), 10);
	var ano1 = parseInt(Data1.substring(6,10), 10);
	var DataInformada = (ano1*10000) + (mes1*100) + dia1;

	var Hoje = new Date();
	var dia2 = Hoje.getDate();
	var mes2 = Hoje.getMonth()+1;
	var ano2 = Hoje.getFullYear();
	var DataHoje = (ano2*10000) + (mes2*100) + dia2;

	var Retorno = (DataInformada == DataHoje)? 'S' : 'N';
	return(Retorno);
};

function ComparaDatas(Data1, comparador, Data2){
	var dia1 = parseInt(Data1.substring(0,2), 10);
	var mes1 = parseInt(Data1.substring(3,5), 10);
	var ano1 = parseInt(Data1.substring(6,10), 10);
	var DataInformada1 = (ano1*10000) + (mes1*100) + dia1;

	var dia2 = parseInt(Data2.substring(0,2), 10);
	var mes2 = parseInt(Data2.substring(3,5), 10);
	var ano2 = parseInt(Data2.substring(6,10), 10);
	var DataInformada2 = (ano2*10000) + (mes2*100) + dia2;

	var Retorno = eval( "(DataInformada1" + comparador + "DataInformada2)? true : false" );
	return(Retorno);
};

function NomeDoMes(nMes){
	var vMes = new Array(null, 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
	return vMes[nMes];
};

function CentralizaObjeto(oObjeto){
	var ah = screen.availHeight - 30;
	var aw = screen.availWidth - 10;
	var xc = (aw - oObjeto.offsetWidth) / 2;
	var yc = (ah - oObjeto.offsetHeight) / 2;
	oObjeto.style.top = yc + window.document.body.scrollTop - 100;
	oObjeto.style.left = xc + window.document.body.scrollLeft;    
};

function wait(intervalo){
	var then,now; then=new Date().getTime();
	now=then;
	while((now-then)<intervalo) {
		now=new Date().getTime();
	};
};