/*
Objetivo:
O script tem por objetivo formatar a entrada do dado no input.
Ele vai servir muito bem para datas, horas, datas e horas, cpf,
cgf e outras cositas mais que tenham tamanho fixo e sejam numé-
ricos. 
Exemplo de chamada da função no formulário. 

1º passo: chamar o arquivo, no caso dir_js eh o local onde vc vai colocar o bichaum!!!
<script src="dir_js/formata.js" type="text/javascript"></script>

2º passo: mandar a função no input!!!!
<form name="form1" action="teste.php">
	<input type="Text" name="teste1" size="17" maxlength="17" onkeydown="FormataCampo(this,event,'##/##:##-## ##:##')">	
</form>
*/

	function round(number,X) {
		// rounds number to X decimal places, defaults to 2
		X = (!X ? 2 : X);
		//alert(Math.round(number*Math.pow(10,X))/Math.pow(10,X));
		return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
	}


function zeros(campo){
	var s
	s = campo.value
	t = 10 - s.length
	for(x = 0 ;  x < t ; x++){
		s = 0 + s
	}
	campo.value = s
}

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 TextCont(Objeto, Limite) {
	if (Objeto.value.length > Limite) Objeto.value = Objeto.value.substring(0, Limite);
}

function AvaliarNumero(Objeto) {
	var x, i, ponto=0, pos_ponto, casas=0, retorno='';

	for(i=0; i<Objeto.value.length; i++) {
		x = Objeto.value.charAt(i);
		if(x=='.') 	x = ',';
		if(x==',') 	{ ponto += 1; pos_ponto = i; }
		if(ponto==1) 	casas = Objeto.value.substring(pos_ponto).length-2; 
		if(((!isNaN(x)) || (x == ',')) && (ponto <= 1) && (pos_ponto != 0) && (x != ' ') && (retorno.length < 10)) retorno = retorno + x; 
	}
	if((casas>1)) retorno = Objeto.value.substring(0, Objeto.value.length - 1);
	Objeto.value = retorno;
}

function AvaliarInteiro(Objeto) {
	var x, i, retorno='';
	for(i=0; i<Objeto.value.length; i++) { x = Objeto.value.charAt(i); if((!isNaN(x)) && (x != ' ') && (retorno.length < 10)) retorno = retorno + x; }
	Objeto.value = retorno;
}

function currencyFormat(fld, milSep, decSep, e) {
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;  // Enter
key = String.fromCharCode(whichCode);  // Get key value from key code
if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
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;
}
//  End -->

function AvaliarMonetario(Objeto) {
	if (Objeto.readOnly) return('');

	return(currencyFormat(Objeto,'',',',event))

	//<input type=text name=test length=15 onKeyPress="return(ValidarMonetario(this))">
}


function JanelaFull(url, nome) { 
  var features;
  w = screen.availWidth-10;
  h = screen.availHeight-20;
  features = 'width='+w+',height='+h;
  features += ',left=0,top=0,screenX=0,screenY=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,scrolling=yes';

  window.open(url, nome, features);
}

function Janela(pagina, nomedajanela, w, h) {
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 40;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',status=yes,scrollbars=yes,resizable=no,menubar=no'
	win = window.open(pagina, nomedajanela, winprops)
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}




function JanelaBoleto(pagina) {
	var w = 700;
	var h = 500;
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 40;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',status=yes,scrollbars=yes,resizable=no,menubar=yes,toolbar=yes'
	win = window.open(pagina, 'Boleto', winprops)
	if (parseInt(navigator.appVersion) >= 4) win.window.focus();
}


