/*
*	Script:	Mascaras em Javascript
*	Autor:	Matheus Biagini de Lima Dias
*	Data:	13/05/2008
*	Obs:	
*/
	/*Função Pai de Mascaras*/
	function Mascara(o,f){
		v_obj=o
		v_fun=f
		setTimeout("execmascara()",1)
	}
	
	/*Função que Executa os objetos*/
	function execmascara(){
		v_obj.value=v_fun(v_obj.value)
	}
	
	/*Função que Determina as expressões regulares dos objetos*/
	function leech(v){
		v=v.replace(/o/gi,"0")
		v=v.replace(/i/gi,"1")
		v=v.replace(/z/gi,"2")
		v=v.replace(/e/gi,"3")
		v=v.replace(/a/gi,"4")
		v=v.replace(/s/gi,"5")
		v=v.replace(/t/gi,"7")
		return v
	}
	
	/*Função que permite apenas numeros*/
	function Integer(v){
		return v.replace(/\D/g,"")
	}
	
	/*Função que padroniza telefone (11) 4184-1241*/
	function Telefone(v){
		v=v.replace(/\D/g,"")                 
		v=v.replace(/^(\d\d)(\d)/g,"($1) $2") 
		v=v.replace(/(\d{4})(\d)/,"$1-$2")    
		return v
	}
	
	/*Função que padroniza telefone (11) 41841241*/
	function TelefoneCall(v){
		v=v.replace(/\D/g,"")                 
		v=v.replace(/^(\d\d)(\d)/g,"($1) $2")    
		return v
	}
	
	/*Função que padroniza CPF*/
	function Cpf(v){
		v=v.replace(/\D/g,"")                    
		v=v.replace(/(\d{3})(\d)/,"$1.$2")       
		v=v.replace(/(\d{3})(\d)/,"$1.$2")       
												 
		v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") 
		return v
	}
	
	/*Função que padroniza CEP*/
	function Cep(v){
		v=v.replace(/D/g,"")                
		v=v.replace(/^(\d{5})(\d)/,"$1-$2") 
		return v
	}
	
	/*Função que padroniza CNPJ*/
	function Cnpj(v){
		v=v.replace(/\D/g,"")                   
		v=v.replace(/^(\d{2})(\d)/,"$1.$2")     
		v=v.replace(/^(\d{2})\.(\d{3})(\d)/,"$1.$2.$3") 
		v=v.replace(/\.(\d{3})(\d)/,".$1/$2")           
		v=v.replace(/(\d{4})(\d)/,"$1-$2")              
		return v
	}
	
	/*Função que permite apenas numeros Romanos*/
	function Romanos(v){
		v=v.toUpperCase()             
		v=v.replace(/[^IVXLCDM]/g,"") 
		
		while(v.replace(/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/,"")!="")
			v=v.replace(/.$/,"")
		return v
	}
	
	/*Função que padroniza o Site*/
	function Site(v){
		v=v.replace(/^http:\/\/?/,"")
		dominio=v
		caminho=""
		if(v.indexOf("/")>-1)
			dominio=v.split("/")[0]
			caminho=v.replace(/[^\/]*/,"")
			dominio=dominio.replace(/[^\w\.\+-:@]/g,"")
			caminho=caminho.replace(/[^\w\d\+-@:\?&=%\(\)\.]/g,"")
			caminho=caminho.replace(/([\?&])=/,"$1")
		if(caminho!="")dominio=dominio.replace(/\.+$/,"")
			v="http://"+dominio+caminho
		return v
	}

	/*Função que padroniza DATA*/
	function Data(v){
		v=v.replace(/\D/g,"") 
		v=v.replace(/(\d{2})(\d)/,"$1/$2") 
		v=v.replace(/(\d{2})(\d)/,"$1/$2") 
		return v
	}
	
	/*Função que padroniza DATA*/
	function Hora(v){
		v=v.replace(/\D/g,"") 
		v=v.replace(/(\d{2})(\d)/,"$1:$2")  
		return v
	}
	
	/*Função que padroniza valor monétario*/
	function Valor(v){
		v=v.replace(/\D/g,""); //Remove tudo o que não é dígito
		v=v.replace(/(\d{2})$/,",$1"); //Coloca a virgula
		v=v.replace(/(\d+)(\d{3},\d{2})$/g,"$1.$2"); //Coloca o primeiro ponto
		var qtdLoop = (v.length-3)/3;
		var count = 0;
		while (qtdLoop > count)
		{
		count++;
		v=v.replace(/(\d+)(\d{3}.*)/,"$1.$2"); //Coloca o resto dos pontos
		}
		v=v.replace(/^(0)(\d)/g,"$2"); //Coloca hífen entre o quarto e o quinto dígitos
		return v;
	}
	
	/*Função que padroniza Area*/
	function Area(v){
		v=v.replace(/\D/g,"") 
		v=v.replace(/(\d)(\d{2})$/,"$1.$2") 
		return v
		
	}
	
	/*Função generica que disponibiliza qualquer formatação*/
	function MascaraGenerica(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);
		}
	}
	
	
	/*Função de Mascara Monetaria pt_br*/
	
	documentall = document.all;
	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){
		var val2 = '';
		var strCheck = '0123456789';
		var len = valor.length;
		if (len== 0){
			return 0.00;
		}
		if (currency ==true){	
			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{
			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;
		if (whichCode == 8 && !documentall) {	
		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;
		}
		FormataReais(obj,'.',',',event);
	} 
	function backspace(obj,event){
		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
			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
			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;
	}
	
	
	
