/**
 *
 * function mask(_mask, val)
 *
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * val   = Valor a ser formatado.
 *
 * Formata um valor  para a mascara definida.
 *
 * pedro.leao@ig.com.br 2003/08/16
 *
 * Acrescentado o tamanho maximo da data. 
 * andre sipac
 */
function mask(_mask, val) {
	var i, mki;
	var aux="";
	
	/*limite para o tamanho maximo da data. Aceita o tamanho maximo 10 -> xx/xx/xxxx */
	if (val.length == 11){
		return val.substr(0,10);
	}else{ 	
		for(i=mki=0; i<val.length; i++, mki++) {
			if(_mask.charAt(mki)=='' || _mask.charAt(mki)=='#' || _mask.charAt(i)==val.charAt(i)) {
				aux+=val.charAt(i);
			} else {
				aux+=_mask.charAt(mki)+val.charAt(i);
				mki++;
			}
		}
	}
	return aux;
}

/**
 * function maskEvent(field, _mask, event)
 *
 * field = Objeto que esta enviando o evendo onKeyPress()
 * _mask = Mascara Exemplo: ##/##/#### ou ###.###.###-##
 * event = Evento a ser observado.
 *
 * Formata um valor para a mascara definida conforma o valor vai sendo digitado.
 *
 * pedro.leao@ig.com.br 2003;08/16
 *
 * SOLINO ceap adicionado o tratamento do backspace no firefox 
 */
function maskEvent(field, _mask, event) {
	var key ='';
	var aux='';
	var len=0;
	var i=0;
	var strCheck = '0123456789';
	var rcode = (window.Event) ? event.which : event.keyCode;
	
	if(rcode == 13) {
		//Enter
		return true;
	}

	//SOLINO adiciona a tecla backspace para funcionar no firefox
	if(rcode == 8) {
		//backspace
		return true;
	}

	
	//Get key value from key code
	key=String.fromCharCode(rcode);
	
	if(strCheck.indexOf(key)==-1) {
		//Not a valid key
		return false;
	}
	
	aux=field.value+key;
	//window.alert(aux);
	aux=mask(_mask,aux);
	//window.alert(aux);
	field.value=aux;	
	return false;
}



function selecionarCheckbox(id){
    var targetElement = document.getElementById(id);		
    if(targetElement==null)
    	return;
	targetElement.checked = true;		
}

function mostrarElemento(id) 
{
    var targetElement = document.getElementById(id);		
    if(targetElement==null)
    	return;
	targetElement.style.display = "";	
}

function ocultarElemento(id) 
{
    var targetElement = document.getElementById(id);
    if(targetElement==null)
    	return;
	targetElement.style.display = "none";		    
}

//<gehring>
function trogleElementoeImg(id,objeto,img1,img2){
    var targetElement = document.getElementById(id);		 
    if(targetElement==null)
   		return;
    if (targetElement.style.display == 'none') {
		targetElement.style.display = '';		
		objeto.src=img1;       	
    }else{
    	targetElement.style.display = 'none';		    
    	objeto.src=img2;
    }
}
//Alterna ocultar mostrar o elemento
function trogleElemento(id)
{
    var targetElement = document.getElementById(id);		 
    if(targetElement==null)
   		return;
    if (targetElement.style.display == 'none') {
		targetElement.style.display = '';		       	
    }else{
    	targetElement.style.display = 'none';		    
    }
}
//Oculta todos os elementos prefixo+num (Obs: num sempre deve começar por 1)
//exemplo: teste1 teste2 teste3 
//OcultarTodos(teste), oculta todos os elementos de teste1 a teste3
function ocultarTodos(prefixo)
{
	if(!prefixo)
		return;		
	var targetElement;
	for(i=1;targetElement = document.getElementById(prefixo+i);i++){
		ocultarElemento(prefixo+i);
	}
}

//</gehring>
function habilitarElemento(id)
{
    var targetElement = document.getElementById(id);		
	targetElement.disabled = false;
}

function desabilitarElemento(id)
{
    var targetElement = document.getElementById(id);		
	targetElement.disabled = true;
}

function setValor(id,valor) 
{
    var targetElement = document.getElementById(id);		 
	targetElement.value = valor;		    
}


/**
*  So aceitar numeros em um campo
* retirado de http://www.forumweb.com.br/foruns/index.php?showtopic=31551
*/

function numero(e)
{
	navegador = /msie/i.test(navigator.userAgent);
	if (navegador){
		var tecla = event.keyCode;
	}else{
		var tecla = e.which;
	}
	if(tecla > 47 && tecla < 58){ // numeros de 0 a 9
		return true;
	}
	if (tecla == 8){ // backspace
		return true;
	}
	if(tecla == 0){ //tab -isso aqui nao ta certo o codigo do tab eh 9 mas com ele a funcao nao ta funcionando
		return true;
	}
	
	return false;
	
}

/*
*	fim so aceita numero em um campo
/


/**
*	Mascara para valores em reais
*   retirado de http://www.waltercruz.com/devel/javascript/mascarareais.html
*/

documentall = document.all;
/*
* função para formatação de valores monetários retirada de
* http://jonasgalvez.com/br/blog/2003-08/egocentrismo
*/

function formatamoney(c) {
    var t = this; if(c == undefined) c = 2;		
    var p, d = (t=t.split("."))[1].substr(0, c);
    for(p = (t=t[0]).length; (p-=3) >= 1;) {
	        t = t.substr(0,p) + "." + t.substr(p);
    }
    return t+","+d+Array(c+1-d.length).join(0);
}

String.prototype.formatCurrency=formatamoney

function demaskvalue(valor, currency){
/*
* Se currency é false, retorna o valor sem apenas com os números. Se é true, os dois últimos caracteres são considerados as 
* casas decimais
*/
var val2 = '';
var strCheck = '0123456789';
var len = valor.length;
	if (len== 0){
		return 0.00;
	}

	if (currency ==true){	
		/* Elimina os zeros à esquerda 
		* a variável  <i> passa a ser a localização do primeiro caractere após os zeros e 
		* val2 contém os caracteres (descontando os zeros à esquerda)
		*/
		
		for(var i = 0; i < len; i++)
			if ((valor.charAt(i) != '0') && (valor.charAt(i) != ',')) break;
		
		for(; i < len; i++){
			if (strCheck.indexOf(valor.charAt(i))!=-1) val2+= valor.charAt(i);
		}

		if(val2.length==0) return "0.00";
		if (val2.length==1)return "0.0" + val2;
		if (val2.length==2)return "0." + val2;
		
		var parte1 = val2.substring(0,val2.length-2);
		var parte2 = val2.substring(val2.length-2);
		var returnvalue = parte1 + "." + parte2;
		return returnvalue;
		
	}
	else{
			/* currency é false: retornamos os valores COM os zeros à esquerda, 
			* sem considerar os últimos 2 algarismos como casas decimais 
			*/
			val3 ="";
			for(var k=0; k < len; k++){
				if (strCheck.indexOf(valor.charAt(k))!=-1) val3+= valor.charAt(k);
			}			
	return val3;
	}
}

function reais(obj,event){

var whichCode = (window.Event) ? event.which : event.keyCode;
/*
Executa a formatação após o backspace nos navegadores !document.all
*/
if (whichCode == 8 && !documentall) {	
/*
Previne a ação padrão nos navegadores
*/
	if (event.preventDefault){ //standart browsers
			event.preventDefault();
		}else{ // internet explorer
			event.returnValue = false;
	}
	var valor = obj.value;
	var x = valor.substring(0,valor.length-1);
	obj.value= demaskvalue(x,true).formatCurrency();
	return false;
}
/*
Executa o Formata Reais e faz o format currency novamente após o backspace
*/
FormataReais(obj,'.',',',event);
} // end reais


function backspace(obj,event){
/*
Essa função basicamente altera o  backspace nos input com máscara reais para os navegadores IE e opera.
O IE não detecta o keycode 8 no evento keypress, por isso, tratamos no keydown.
Como o opera suporta o infame document.all, tratamos dele na mesma parte do código.
*/

var whichCode = (window.Event) ? event.which : event.keyCode;
if (whichCode == 8 && documentall) {	
	var valor = obj.value;
	var x = valor.substring(0,valor.length-1);
	var y = demaskvalue(x,true).formatCurrency();

	obj.value =""; //necessário para o opera
	obj.value += y;
	
	if (event.preventDefault){ //standart browsers
			event.preventDefault();
		}else{ // internet explorer
			event.returnValue = false;
	}
	return false;

	}// end if		
}// end backspace

function FormataReais(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 == 8 ) return true; //backspace - estamos tratando disso em outra função no keydown
if (whichCode == 0 ) return true;
if (whichCode == 9 ) return true; //tecla tab
if (whichCode == 13) return true; //tecla enter
if (whichCode == 16) return true; //shift internet explorer
if (whichCode == 17) return true; //control no internet explorer
if (whichCode == 27 ) return true; //tecla esc
if (whichCode == 34 ) return true; //tecla end
if (whichCode == 35 ) return true;//tecla end
if (whichCode == 36 ) return true; //tecla home

/*
O trecho abaixo previne a ação padrão nos navegadores. Não estamos inserindo o caractere normalmente, mas via script
*/

if (e.preventDefault){ //standart browsers
		e.preventDefault()
	}else{ // internet explorer
		e.returnValue = false
}

var key = String.fromCharCode(whichCode);  // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inválida

/*
Concatenamos ao value o keycode de key, se esse for um número
*/
fld.value += key;

var len = fld.value.length;
var bodeaux = demaskvalue(fld.value,true).formatCurrency();
fld.value=bodeaux;

/*
Essa parte da função tão somente move o cursor para o final no opera. Atualmente não existe como movê-lo no konqueror.
*/
  if (fld.createTextRange) {
    var range = fld.createTextRange();
    range.collapse(false);
    range.select();
  }
  else if (fld.setSelectionRange) {
    fld.focus();
    var length = fld.value.length;
    fld.setSelectionRange(length, length);
  }
  return false;

}

/*
*fim das funcoes da mascara em reais
*/