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);
}

/*
 * Formata uma string com um valor em reais para um float
 * ex. "1.000,05" para 1000.05
 */
function formataReaisParaFloat(valor){
    valor = valor.replace(".","");
    valor = valor.replace(",",".");
    return parseFloat(valor);
}

/**
* Transforma uma String formata como float em uma String formata como Reais.
* Ex.: "1234.56" => "1.234,56"
*      "1234"    => "1.234,00"
*      "1234.5"  => "1.234,50"
*/
function mascaraValor(entrada){

    if(entrada == '') return '';
	// Já está formatado?
	if( entrada.match(/,/) ) return entrada;
	
	var t = entrada;
	var ponto;

	for(i=0;i<t.length;){
		caracter=t.substr(i,1);
		if(caracter=='.'){
			ponto=i;
		}
		i++;
	}
	if(!ponto) t = t + '.00';

	t = t.split(".");
    var milhares = t[0];
    var decimais = t[1];
    // Assegurar que a parte decimal tem duas casas
    if(decimais.length == 1) decimais += '0';
    if(decimais.length == 0) decimais += '00';
	// Formatar os milhares com ponto
	for(p = milhares.length; (p-=3) >= 1;) {
		milhares = milhares.substr(0,p) + "." + milhares.substr(p);
	}
	return milhares + ',' + decimais;
}

/**
* wrapper para a funcao mascaraValor
* ela é chamada na visao
* quando obj for um numero ela apenas repassa para mascaraValor,
* caso for um objeto de um form por exemplo passa o valor para mascaValor
* e seta no form o retorno
* @param obj mixed tanto um numero como um objeto
* @return mixed void para objeto e o valor formatado quando numero
*/
function mascaraReais(obj) {
    if(typeof obj == 'string'){
        return mascaraValor(obj);
    }
    else{
        if(ret=mascaraValor(obj.value)){
            obj.value=ret;
        }
    }
}

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){
	if(window.Event){
		var whichCode=event.which;
	}
	else{
		var whichCode=event.keyCode;
	}
	
	/*
	Executa a formatacao apos 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{ // inte	rnet 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 apos 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 = (event.which) ? event.which : event.keyCode;
	if (whichCode == 8 || whichCode == 46)
	{
		obj.onkeypress();
	}
}

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;

}

