 // JavaScript Document

 //CHAMA AS FUNCOES DE FORMATAÇÃO
 function formatar(elemento,funcao) {
	 v_obj = elemento;
	 v_fun = funcao;
     setTimeout("exec_format()",1);
 }

 function exec_format(){
     v_obj.value = v_fun(v_obj.value);
 }
 //*************************************************************************************************


 //FORMATA CPF
 function formatar_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;
 }
 //*************************************************************************************************


 //FORMATA CNPJ
 function formatar_cnpj(v) {

     v = v.replace(/\D/g,"");
     v = v.replace(/^(\d{3})(\d)/,"$1.$2");
     v = v.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3");
     v = v.replace(/\.(\d{3})(\d)/,".$1/$2");
     v = v.replace(/(\d{4})(\d)/,"$1-$2");
     return v;
 }
 //*************************************************************************************************


 //FORMATA IE
 function formatar_ie(v) {

     v = v.replace(/\D/g,"");
     v = v.replace(/^(\d{3})(\d)/,"$1.$2");
     v = v.replace(/^(\d{3})\.(\d{3})(\d)/,"$1.$2.$3");
     v = v.replace(/^(\d{3})\.(\d{3})\.(\d{3})(\d)/,"$1.$2.$3.$4");
     return v;
 }
 //*************************************************************************************************


 //FORMATA IM
 function formatar_im(v) {
     return v;
 }
 //*************************************************************************************************


 //FORMATA CEP
 function formatar_cep(v) {

	 v = v.replace(/\D/g,"");
     v = v.replace(/^(\d{5})(\d)/,"$1-$2");
     return v;
 }
 //*************************************************************************************************


 //FORMATA TELEFONE
 function formatar_telefone(v) {

	 v = v.replace(/\D/g,"");
     v = v.replace(/(\d{4})(\d)/,"$1-$2");
     return v;
 }
 //*************************************************************************************************


 //FORMATA DATA
 function formatar_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;
 }
 //*************************************************************************************************


 //FORMATA HORA
 function formatar_hora(v) {

	 v = v.replace(/\D/g,"");
     v = v.replace(/(\d{2})(\d)/,"$1:$2");
     return v;
 }
 //*************************************************************************************************


 //FORMATA MOEDA
 function formatar_moeda_2(v) {

     v = v.replace(/\D/g,"");
	 v = v.replace(/^(0)/,"");
	 v = v.replace(/^(0)/,"");
     v = v.replace(/[0-9]{12}/,"inválido");

     if(v.length==1)
	     v = '00' + v;
     if(v.length==2)
	     v = '0' + v;

     v = v.replace(/(\d{1})(\d{8})$/,"$1.$2");
     v = v.replace(/(\d{1})(\d{5})$/,"$1.$2");
     v = v.replace(/(\d{1})(\d{1,2})$/,"$1,$2");
     return v;
 }
 function formatar_moeda_4(v) {

     v = v.replace(/\D/g,"");
	 v = v.replace(/^(0)+/,"");
     v = v.replace(/[0-9]{12}/,"inválido");

     if(v.length==1)
	     v = '0000' + v;
     if(v.length==2)
	     v = '000' + v;
     if(v.length==3)
	     v = '00' + v;
     if(v.length==4)
	     v = '0' + v;

     v = v.replace(/(\d)(\d{1,4})$/,"$1,$2");
     return v;
 }
 //*************************************************************************************************


 //FORMATA MOEDA
 function formatar_decimal(v) {

     v = v.replace(/\./g,",");
     v = v.replace(/[^0-9,]/g,"");
     return v;
 }
 //*************************************************************************************************


 //FORMATA SITE
 function formatar_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;
 }
 //*************************************************************************************************


 //FORMATA NUMERO
 function formatar_numero(v) {

     v = v.replace(/\D/g,"");
     return v;
 }
 //*************************************************************************************************


 //FORMATA NCM
 function formatar_ncm(v) {

     v = v.replace(/\D/g,"");
     v = v.replace(/(\d{4})(\d)/,"$1.$2"); 
     v = v.replace(/(\d{4})\.(\d{2})(\d)/,"$1.$2.$3"); 
     //v = v.replace(/(\d{2})\.(\d{2})\.(\d{2})\.(\d)/,"$1.$2.$3.$4"); 
     return v;
 }
 //*************************************************************************************************


 //INCLUIR ZERO A ESQUERDA
 function inclui_zero(el,qtd) {

     v = el.value;
	 v = retira_zero_esquerda(v);
	 v = caracter_a_esquerda(v, qtd, 0);
     el.value = v;
 }
 //*************************************************************************************************


 //MASCARA LABEL
 function mask_label_focus(el,label) {
     if(el.value==label) {
	     el.value = '';
	 }
 }
 function mask_label_blur(el,label) {
     if(el.value=='') {
	     el.value = label;
     }
 }
 //*************************************************************************************************


 //POPULATE SELECT
 function select_populate(id1, id2, url) {
	 var value = $('#' + id1).val();
	 $('#' + id2).get(0).options[0]	= new Option('Carregando...', '');
	 $('#' + id2).get(0).options[0].selected = true;
	 select_autopopulate(id2, value, url);
 }

 function select_autopopulate(id2, option, url) {
	 $('#' + id2).jselect({
		 loadUrl: url,
		 loadData: "o=" + parseInt(option),
		 loadDataType: "json"
	 });
 }
 //*************************************************************************************************