function soNumero(myfield, e, dec)
{
	var key;
	var keychar;
	if (dec == null) dec = "";
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	    (key==9) || (key==13) || (key==27) )
	   return true;
	// numbers
	else if ((("0123456789" + dec).indexOf(keychar) > -1))
	   return true;
	// decimal point jump
	else
	   return false;
}

//Mascara para Data
function FormataData(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);
  }
}

function FreezeScreen(msg) {
      scroll(0,0);
      var outerPane = document.getElementById('FreezePane');
      var innerPane = document.getElementById('InnerFreezePane');
      if (outerPane) outerPane.className = 'FreezePaneOn';
      if (innerPane) innerPane.innerHTML = msg;
   }

//Checa data
function checkDate(txtField) {
	 NumDig = 4;
	 valor = txtField.value;
     
	 if (valor.length > 0){
	     if (valor.charAt(2) != '/' || valor.charAt(5) != '/' || valor.length != 10){
	        alert('Formato de data invalido!\nFormato correto: DD/MM/AAAA!');
			txtField.select();
	        txtField.focus();
	        return false;        
	     }
	     else{
		     var Dia,Mes,Ano;
		     
			 Dia = (valor.charCodeAt(0) == 48)? parseInt(valor.substring(1,2)) : parseInt(valor.substring(0,2));
			 Mes = (valor.charCodeAt(3) == 48)? parseInt(valor.substring(4,5)) : parseInt(valor.substring(3,5));
		     Ano = parseInt(valor.substring(6,valor.length));
		     
			 var AnoBiss = ((Ano%4 == 0 && Ano%100 != 0) || Ano%400 == 0);
		
		     if ((Dia < 1 || Dia > 31 || isNaN(Dia)) || (Mes < 1 || Mes > 12 || isNaN(Mes)) || ((NumDig == 4 && Ano < 1900) || isNaN(Ano)) || (((Mes == 2 && AnoBiss && Dia > 29) || (Mes == 2 && !AnoBiss && Dia > 28)) || ((Mes == 4 || Mes == 6 || Mes == 9 || Mes == 11) && Dia > 30))){ 
		             alert('A data fornecida esta incorreta!');
					 txtField.select();
                     txtField.focus();
		             return false;
		     }
		}
	}
}

function formataCEP(formato, keypress, objeto)
{
//onKeyPress="formataCEP('CEP', window.event.keyCode, this);"
campo = eval(objeto);
if (formato=='CEP')
	{
	caracteres = '01234567890';
	separacoes = 1;
	separacao1 = '-';
	conjuntos = 2;
	conjunto1 = 5;
	conjunto2 = 3;
	if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < 
	(conjunto1 + conjunto2 + 1))
		{
		if (campo.value.length == conjunto1) 
		   campo.value = campo.value + separacao1;
		}
	else 
		event.returnValue = false;
	}
}

function FormataCampo(Campo,teclapres,mascara){ 
//pegando o tamanho do texto da caixa de texto com delay de -1 no event 
//ou seja o caractere que foi digitado não será contado. 
strtext = Campo.value 
tamtext = strtext.length 
//pegando o tamanho da mascara 
tammask = mascara.length 
//criando um array para guardar cada caractere da máscara 
arrmask = new Array(tammask) 
//jogando os caracteres para o vetor 
for (var i = 0 ; i < tammask; i++){ 
arrmask[i] = mascara.slice(i,i+1) 
} 
//alert (teclapres.keyCode) 
//começando o trabalho sujo 
if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))){ 
if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){ 
Organiza_Casa(Campo,arrmask[tamtext],teclapres.keyCode,strtext) 
} 
else{ 
Detona_Event(Campo,strtext) 
} 
} 
else{//Aqui funcionaria a mascara para números mas eu ainda não implementei 
if ((arrmask[tamtext] == "A")) { 
charupper = event.valueOf() 
//charupper = charupper.toUpperCase() 
Detona_Event(Campo,strtext) 
masktext = strtext + charupper 
Campo.value = masktext 
} 
} 
} 
function Organiza_Casa(Campo,arrpos,teclapres_key,strtext){ 
if (((arrpos == "/") || (arrpos == ".") || (arrpos == ",") || (arrpos == ":") || (arrpos == " ") || (arrpos == "-")) && !(teclapres_key == 8)){ 
separador = arrpos 
masktext = strtext + separador 
Campo.value = masktext 
} 
} 
function Detona_Event(Campo,strtext){ 
event.returnValue = false 
if (strtext != "") { 
Campo.value = strtext 
} 
}

function Mascara(objeto, evt, mask) {
//Mascara(objeto, evt, '(##) ####-####');
// Telefone --> onkeypress="Mascara(this, window.event.keycode, '(##) ####-####');"
// CEP --> onkeypress="Mascara(this, window.event.keycode, '#####-###');"
// CNPJ --> onkeypress="Mascara(this, window.event.keycode, '##.###.###/####-##');"
var LetrasU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var LetrasL = 'abcdefghijklmnopqrstuvwxyz';
var Letras  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var Numeros = '0123456789';
var Fixos  = '().-:/ '; 
var Charset = " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_/`abcdefghijklmnopqrstuvwxyz{|}~";

evt = (evt) ? evt : (window.event) ? window.event : "";
var value = objeto.value;
if (evt) {
 var ntecla = (evt.which) ? evt.which : evt.keyCode;
 tecla = Charset.substr(ntecla - 32, 1);
 if (ntecla < 32) return true;

 var tamanho = value.length;
 if (tamanho >= mask.length) return false;

 var pos = mask.substr(tamanho,1); 
 while (Fixos.indexOf(pos) != -1) {
  value += pos;
  tamanho = value.length;
  if (tamanho >= mask.length) return false;
  pos = mask.substr(tamanho,1);
 }

 switch (pos) {
   case '#' : if (Numeros.indexOf(tecla) == -1) return false; break;
   case 'A' : if (LetrasU.indexOf(tecla) == -1) return false; break;
   case 'a' : if (LetrasL.indexOf(tecla) == -1) return false; break;
   case 'Z' : if (Letras.indexOf(tecla) == -1) return false; break;
   case '*' : objeto.value = value; return true; break;
   default : return false; break;
 }
}
objeto.value = value; 
return true;
}

function popup(caminho,nome,largura,altura,rolagem) 
{
	var esquerda = (screen.width - largura) / 2;
	var cima = (screen.height - altura) / 2 -50;
	window.open(caminho,'cal','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + rolagem + ',resizable=no,copyhistory=no,top=' + cima + ',left=' + esquerda + ',width=' + largura + ',height=' + altura);
//popup('lrm_default.cfm','logisticaMaster','750','550','yes');
}

function modal(caminho,nome,largura,altura,rolagem)
{
	var l=(screen.width-largura)/2; 
	var h=(screen.height-altura)/2;
	window.showModalDialog(caminho,nome,'status:no;dialogWidth:'+largura+'px;dialogHeight:'+altura+'px;dialogHide:true;help:no;scroll:'+rolagem+';left:'+l+'px;top:'+h+';',window);
}

function FormataMoeda(fld, e)
{
var milSep = ".";
var decSep = ",";
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;

if (whichCode == 13)
return true;

key = String.fromCharCode(whichCode);

if (strCheck.indexOf(key) == -1)
return false;

len = fld.value.length;

for (i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep))
break;

aux = '';

for (; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1)
aux += fld.value.charAt(i);

aux += key;
len = aux.length;

if (len == 0)
fld.value = '';

if (len == 1)
fld.value = '0'+ decSep + '0' + aux;

if (len == 2)
fld.value = '0'+ decSep + aux;

if (len > 2){
aux2 = '';

for (j = 0, i = len - 3; i >= 0; i--){
if (j == 3){
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}

fld.value = '';
len2 = aux2.length;

for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}

return false;
} 

function textCounter(field,MaxLength) { 
obj = document.all(field); 
if (MaxLength !=0) { 
if (obj.value.length > MaxLength) 
obj.value = obj.value.substring(0, MaxLength); 
} 
}
function FormataTempo(fld, e) 
			{
				if (!fld.readOnly)
				{
					if (document.selection.createRange().text.length > 0) {
						fld.select();
						fld.value = fld.value.replace(RegExp('\\b' + document.selection.createRange().text + '\\b', 'g'), '');
						document.selection.empty();
						document.selection.clear();
					}
					if (fld.length > 0)
					{
						fld.value = '';				
						fld.blur();
						fld.focus();
					}
					var decSep = ':';
					var milSep = '';
					var sep = 0;		
					var key = ''; 
					var i = j = 0; 
					var len = len2 = 0; 
					var strCheck = '0123456789'; 
					var aux = aux2 = ''; 
					var whichCode = (window.Event) ? e.which : e.keyCode; 
					if (whichCode == 13) return true; 
					key = String.fromCharCode(whichCode); 
					if (strCheck.indexOf(key) == -1) return false; 
					len = fld.value.length; 				
					if (len <= 5)
					{
						for(i = 0; i < len; i++) 
						if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break; 
						aux = ''; 
						for(; i < len; i++) 
						if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i); 
						aux += key; 
						len = aux.length; 
						if (len == 0) fld.value = ''; 
						if (len == 1) fld.value = '0'+ decSep + '0' + aux; 
						if (len == 2) fld.value = '0'+ decSep + aux; 
						if (len > 2) { 
						aux2 = ''; 
						for (j = 0, i = len - 3; i >= 0; i--) { 
						if (j == 3) { 
						aux2 += milSep; 
						j = 0; 
						} 
						aux2 += aux.charAt(i); 
						j++; 
						} 
						fld.value = ''; 
						len2 = aux2.length; 
						for (i = len2 - 1; i >= 0; i--) 
						fld.value += aux2.charAt(i); 
						fld.value += decSep + aux.substr(len - 2, len); 
						} 
					}

				}
				return false; 
			}
