	function localizar_campos(){
                var Tags = ['input','textarea', 'select', 'checkbox', 'hidden', 'password', 'file', 'radio', 'button', 'img' ];
                var i = 0;
                parte = document;
                for(z=0;z<Tags.length;z++){
                        Inputs=parte.getElementsByTagName(Tags[z]);						
                        for(i=0;i<Inputs.length;i++)						
						   {
					        if($("#"+Inputs[i].id).attr("enviar"))
							  {
							   validacao_enviar(Inputs[i]);
							  }
					        if($("#"+Inputs[i].id).attr("mascara"))
							  {
							   mascaras_aplicar(Inputs[i]);
							  }
				          }
                }		
	}

	 function validacao_aplicar(campo){
                var Tags = ['input','textarea', 'select', 'checkbox', 'hidden', 'password', 'file', 'radio' ];	
				form = $("#"+campo.id).attr('enviar');
				parte = document.getElementById(form);
                if (typeof parte == "undefined") parte = document;				
		        saida = "sim";
                for(z=0;z<Tags.length;z++){
                        Inputs=parte.getElementsByTagName(Tags[z]);
                        for(i=0;i<Inputs.length;i++)
						   {
                            if(('button,image,hidden,submit,reset').indexOf(Inputs[i].type.toLowerCase())==-1)
							  {	
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'text')
								 {if(validar_input(Inputs[i]) == 'nao'){ saida = "nao";}}
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'hidden')
								 {if(validar_input(Inputs[i]) == 'nao'){ saida = "nao";}}							 
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'password')
								 {if(validar_input(Inputs[i]) == 'nao'){ saida = "nao";}}
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'file')
								 {if(validar_input(Inputs[i]) == 'nao'){ saida = "nao";}}
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'textarea')
								 {if(validar_input(Inputs[i]) == 'nao'){ saida = "nao";}}
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'select-one')
								 {if(validar_select(Inputs[i]) == 'nao'){ saida = "nao";}}    
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'checkbox')
								 {if(validar_checkbox(Inputs[i]) != 'sim'){ saida = "nao";}}    
  							   if($('#'+Inputs[i].id).attr('validar') == 'sim' && Inputs[i].type.toLowerCase() == 'radio')
								 {if(validar_radio(Inputs[i]) != 'sim'){ saida = "nao";}}    
							   if(saida == 'nao'){break;}
						      }							  
				           }
			      if(saida == 'nao'){break;}
				}
  	       return saida;	
		 };
	 function validar_input(campo){	
	      campo = $("#"+campo.id); txt = campo.attr("txt_"); if(!txt){txt = 'O campo '+campo.attr("txt")+' está em branco';} if(!txt){txt = '';}
		  conf_value = campo.attr('conf_value'); conf_value_ = campo.attr('conf_value_'); obs = campo.attr('obs'); obs_txt = campo.attr('obs_txt');
		  n_min = campo.attr('n_min'); n_max = campo.attr('n_max'); n_txt = campo.attr('n_txt');
		  if(!conf_value){conf_value = '';}  if(!conf_value_){conf_value_ = '';} if(!obs){obs = '';}
		  if(!n_txt){n_txt = '';} if(!n_min){n_min = '';} if(!n_max){n_max = '';}
		  if(conf_value_ != ""){conf_value = $("#"+conf_value_).val(); }		  
		  if(n_txt == ''){n_txt_min = 'Você deve digitar no mínimo '+n_min+' caracteres no campo '+campo.attr("txt");
		  n_txt_max = 'Você deve digitar no máximo '+n_max+' caracteres no campo '+campo.attr("txt");} else {n_txt_min = n_txt; n_txt_max = n_txt;}
		//  if(campo.attr('editor') == 'sim'){nome_campo = campo.attr('id'); valor_campo = FCKeditorAPI.__Instances[nome_campo].GetHTML(); campo.val(valor_campo);}		  
		  if(campo.val() != '' && n_min > 0)
		    {
 		     valor = campo.val();
  	         if(n_min > valor.length){alert(n_txt_min); campo.focus(); return 'nao';}
			}
		  if(campo.val() != '' && n_max > 0)
		    {
		     valor = campo.val();
  	         if(n_max < valor.length){alert(n_txt_max); campo.focus(); return 'nao';}
			}
 		  if(obs == 'email'){
             email = campo.val();
			 if(email == '')
			   {
                alert(txt); campo.focus(); return 'nao';			   
			   }
             if(email.indexOf('@')==-1 || email.indexOf('.')==-1)
               {
                campo.attr('value', '');
                alert('Este e-mail está incorreto');
		        campo.focus(); return 'nao';
			   }
		    }
 		  if(obs == 'comparacao'){			  
 		      if(campo.val() == ''){ alert(txt); campo.focus(); return 'nao';}
 		      if(campo.val() != conf_value){ alert(obs_txt); campo.focus(); return 'nao';}			  
		    }
		  if(campo.val() == conf_value && obs == ''){ alert(txt); campo.focus(); return 'nao';}
  	      return 'sim';
		 };
	 function validar_select(campo){
	      campo_select = $("#"+campo.id); txt = campo_select.attr("txt_"); if(!txt){txt = 'Você deve selecionar um item no campo ' + campo_select.attr("txt")+'';} if(!txt){txt = '';}
          texto = campo.options[campo.selectedIndex].value;
          if(texto == '')
            {
             alert(txt);
             campo.focus();
             return 'nao';
            }		
		 return 'sim';	
        };
	 function validar_checkbox(campo){
	      campo_checkbox = $("#"+campo.id); txt = campo_checkbox.attr("txt_"); if(!txt){txt = 'Você deve selecionar o campo '+campo_checkbox.attr("txt")+'';} if(!txt){txt = '';}
          if(!campo.checked)
            {
             alert(txt);
             campo.focus();
             return 'nao';
            }		 
		 return 'sim';
        };
	 function validar_radio(campo){
	      campo_radio = $("#"+campo.id); txt = campo_radio.attr("txt_"); if(!txt){txt = 'Você deve selecionar o campo '+campo_radio.attr("txt")+'';} if(!txt){txt = '';}  		  
          my_question = $("input[@type=radio][@name=troca][@checked]").val();
          if(!my_question)
            {
             alert(txt);
             campo.focus();
             return'nao';
            }		 
		 return'sim';
        };
	function validacao_enviar(campo){	
  		  $("#"+campo.id).attr('onClick', ''); 
		  if($("#"+campo.id).attr('type') == 'password' || $("#"+campo.id).attr('type') == 'text')
		    {
              $("#"+campo.id).keyup(function(event){ 										 
				 if(event.keyCode==13)
			       {
			        retorno = validacao_aplicar(campo);											
			        if(retorno != "nao")
			          {form = $("#"+campo.id).attr('enviar');
			           document.getElementById(form).submit();}			  
				   }
			  });		 
			}
		  else
		   {
		    $("#"+campo.id).click(function(){
			 retorno = validacao_aplicar(campo);											
			 if(retorno != "nao")
			   {form = $("#"+campo.id).attr('enviar');
			    document.getElementById(form).submit();}			  
             });
		   }
		 };	 
		 
// mascara
        var IsIE = navigator.appName.toLowerCase().indexOf('microsoft')!=-1;
        var AZ = /[A-Z]/i;
        var Acentos = /[À-ÿ]/i;
        var Num = /[0-9]/;
        function mascaras_aplicar(campo){
                tipo = campo.getAttribute('tipo');
                readonly = campo.getAttribute('readonly');
                orientacao = campo.getAttribute('orientacao');				
                mascara = campo.getAttribute('mascara');
                if(!tipo)
                  {
                   tipo = 'personalizado';
				   mascara = mascara.replace('#', '*');
                  }					
                if (!tipo) return;
                if (readonly) return;
                if (tipo.toLowerCase() == "personalizado"){

                      mascara = mascara.replace('#', '*');
					  $("#"+campo.id).mask(mascara);
					  return;
					}												
                if (tipo.toLowerCase() == "decimal"){
                        orientacao = "esquerda";
                        casasdecimais = campo.getAttribute('casasdecimais');
                        tamanho = campo.getAttribute('maxLength');
                        if (!tamanho || tamanho > 50)
                                tamanho = 10;
                        if (!casasdecimais)
                                casasdecimais = 2;
                        campo.setAttribute("mascara", mascaras_geraMascaraDecimal(tamanho, casasdecimais));
                        campo.setAttribute("tipo", "num");
                        campo.setAttribute("orientacao", orientacao);
                }
                if (orientacao && orientacao.toLowerCase() == "esquerda") campo.style.textAlign = "right";
                if (mascara) campo.setAttribute("maxLength", mascara.length);
                if (tipo){
                        campo.onkeypress = function(e){ return mascaras_onkeypress(e?e:event); };
                        campo.onkeyup = function(e){ mascaras_onkeyup(e?e:event, campo) };
                }
                campo.setAttribute("snegativo", ((campo.value).substr(0,1) == "-" ? "s" : "n"));
        };
        function mascaras_onkeypress(e){
                KeyCode = IsIE ? event.keyCode : e.which;
                campo =  IsIE ? event.srcElement : e.target;
                maxlength = campo.getAttribute('maxlength');
                selecao = mascaras_selecao(campo);
                if (selecao.length > 0 && KeyCode != 0){
                        campo.value = ""; return true;
                }
                if (KeyCode == 0) return true;
                Char = String.fromCharCode(KeyCode);
                valor = campo.value;
                mascara = campo.getAttribute('mascara');
                if (KeyCode != 8){
                        tipo = campo.getAttribute('tipo').toLowerCase();
                        negativo = campo.getAttribute('negativo');
                        if(negativo && KeyCode == 45){
                                snegativo = campo.getAttribute('snegativo');
                                snegativo = (snegativo == "s" ? "n" : "s");
                                campo.setAttribute("snegativo", snegativo);
                        }else{
                                valor += Char;
                                if (tipo == "num" && Char.search(this.Num) == -1) return false;
                                if (KeyCode != 32 && tipo == "caracter" && Char.search(this.AZ) == -1 && Char.search(this.Acentos) == -1) return false;
                        }
                }
                if (mascara){
                        mascaras_aplicarMascara(campo, valor);
                        return false;
                }
                return true;
        };
        function mascaras_onkeyup(e, campo){
                KeyCode = IsIE ? event.keyCode : e.which;
                if (KeyCode != 9 && KeyCode != 16 && KeyCode != 109){
                        valor = campo.value;
                        if (KeyCode == 8 && !IsIE) valor = valor.substr(0,valor.length-1);
                        mascaras_aplicarMascara(campo, valor);
                }
        };
        function mascaras_aplicarMascara(campo, valor){
                mascara = campo.getAttribute('mascara');
                if (!mascara) return;
                negativo = campo.getAttribute('negativo');
                snegativo = campo.getAttribute('snegativo');
                if (negativo && valor.substr(0,1) == "-")
                        valor = valor.substr(1,valor.length-1);
                orientacao = campo.getAttribute('orientacao');
                var i = 0;
                for(i=0;i<mascara.length;i++){
                        caracter = mascara.substr(i,1);
                        if (caracter != "#") valor = valor.replace(caracter, "");
                }
                retorno = "";
                if (orientacao != "esquerda"){
                        contador = 0;
                        for(i=0;i<mascara.length;i++){
                                caracter = mascara.substr(i,1);
                                if (caracter == "#"){
                                        retorno += valor.substr(contador,1);
                                        contador++;
                                }else
                                        retorno += caracter;
                                if(contador >= valor.length) break;
                        }
                }else{
                        contador = valor.length-1;
                        for(i=mascara.length-1;i>=0;i--){
                                if(contador < 0) break;
                                caracter = mascara.substr(i,1);
                                if (caracter == "#"){
                                        retorno = valor.substr(contador,1) + retorno;
                                        contador--;
                                }else
                                        retorno = caracter + retorno;
                        }
                }
                if (negativo && snegativo == "s")
                        retorno = "-" + retorno;
                campo.value = retorno;
        };
        function mascaras_geraMascaraDecimal(tam, decimais){
                var retorno = ""; var contador = 0; var i = 0;
                decimais = parseInt(decimais);
                for (i=0;i<(tam-(decimais+1));i++){
                        retorno = "#" + retorno;
                        contador++;
                        if (contador == 3){
                                retorno = "." + retorno;
                                contador=0;
                        }
                }
                retorno = retorno + ",";
                for (i=0;i<decimais;i++) retorno += "#";
                return retorno;
        };
        function mascaras_selecao(campo){
                if (this.IsIE)
                        return document.selection.createRange().text;
                else
                        return (campo.value).substr(campo.selectionStart, (campo.selectionEnd - campo.selectionStart));
        };
// fim mascara
carregar(['localizar_campos();']);

function selecionar_carregar(){
   var Tags = ['select'];
   var i = 0;
   parte = document;
   for(z=0;z<Tags.length;z++){
   Inputs=parte.getElementsByTagName(Tags[z]);
   if(Tags[z] == "select")
     {
      for(i=0;i<Inputs.length;i++)
         {
          campo = Inputs[i];
          valor = campo.getAttribute('seleciona_valor');
          if(valor)
            {
             obj2 = campo;
             for(j = 0; j<obj2.length; j++)
                {
                 if(obj2.options[j].value == valor)
                   {
                    obj2.options[j].selected = true;
                   }
                }
            }
         }
     }
   }
 }
carregar(['selecionar_carregar();']);

function msg(txt, type){
	  if(type == 'alert')
	    {
		 alert(txt);
		 return true;
		}
	  if(type == 'pergunta')
	    {
         if(confirm(txt))
           {
		    return true;
		   }
		 else
		   {
		    return false;
		   }
		}
		
	}
function redireciona_url(url){
	parent.location.href=url;
	}
function janela(url, largura, altura){	
         var d = new Date(); 
         nome_janela = "hadialog"+d.getSeconds()+''+d.getMilliseconds();
         var janela_popup = window.open(url, nome_janela,
             "toolbar=no,menubar=yes,personalbar=no,width="+largura+",height="+altura+"," +
             "scrollbars=yes,resizable=yes");	
	}
//funções para selects
function select_remove_all(id){
$("#"+id+" option").each(function(){
 $(this).remove()
});
	}
function select_add(id, nome, valor, selected){
$('#'+id).append("<option value='"+valor+"' "+selected+" >"+nome+"</option>");	
	}	
function select_carrega_ajax(url_ajax, campo, valor) {
             select_add(campo, "Carregando...", "");
             $.ajax({
                 type: "GET",
                 url: url_ajax,
                 dataType: "html",
                 error: function(xhr, desc, exceptionobj) {
                      select_remove_all(campo);
                      select_add(campo, "Erro no carregamento", "");
					 },
                 success: function(xml) {
                     select_remove_all(campo);
                     $(xml).find('option').each(function(){
                         var value = $(this).attr('value')
                         var nome = $(this).attr('nome')
						 if(valor != '' && valor == value)
						   {   
                            select_add(campo, nome, value, 'selected');
						   }
						 else
						   {
                            select_add(campo, nome, value, '');
						   }
                     });					 
                 }
             });
          }		  
function select_carrega(url, campo, valor){
select_remove_all(campo);
select_carrega_ajax(url, campo, valor);
}
// FCK Editor
 function load_FCKeditor(campo, largura, altura) {
 var oFCKeditor = new FCKeditor(campo) ;
 oFCKeditor.Config["CustomConfigurationsPath"] = "wd.config.js"
 oFCKeditor.BasePath = 'editor_wd2/' ;
 oFCKeditor.ToolbarSet = 'WD' ;
 oFCKeditor.Width = '490' ;
 oFCKeditor.Height = '400' ;
 oFCKeditor.ReplaceTextarea() ;
 }

function FCKeditor_OnComplete( editorInstance )
 { 
  editorInstance.Events.AttachEvent( 'OnSelectionChange', DoSomething ) ;
 } 
function DoSomething( editorInstance )
{	
 document.getElementById(editorInstance.Name).value = editorInstance.GetHTML();
}
// FIM FCK Editor
//////////////// BASE64 ENCODE - DECODE //////////////////////////
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
 var output = "";
 var chr1, chr2, chr3;
 var enc1, enc2, enc3, enc4;
 var i = 0;
 do {
 chr1 = input.charCodeAt(i++);
 chr2 = input.charCodeAt(i++);
 chr3 = input.charCodeAt(i++);
 enc1 = chr1 >> 2;
 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
 enc4 = chr3 & 63;

 if (isNaN(chr2)) {
 enc3 = enc4 = 64;
 } else if (isNaN(chr3)) {
 enc4 = 64;
 }

 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
 keyStr.charAt(enc3) + keyStr.charAt(enc4);
 } while (i < input.length);

 return output;
}
function decode64(input) {
 var output = "";
 var chr1, chr2, chr3;
 var enc1, enc2, enc3, enc4;
 var i = 0;

 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

 do {
 enc1 = keyStr.indexOf(input.charAt(i++));
 enc2 = keyStr.indexOf(input.charAt(i++));
 enc3 = keyStr.indexOf(input.charAt(i++));
 enc4 = keyStr.indexOf(input.charAt(i++));

 chr1 = (enc1 << 2) | (enc2 >> 4);
 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
 chr3 = ((enc3 & 3) << 6) | enc4;

 output = output + String.fromCharCode(chr1);

 if (enc3 != 64) {
 output = output + String.fromCharCode(chr2);
 }
 if (enc4 != 64) {
 output = output + String.fromCharCode(chr3);
 }
 } while (i < input.length);

 return output;
}

function favoritos(){
	var url = document.URL;
	var title = document.title;
	if (window.sidebar) 
		window.sidebar.addPanel(title, url, "");
	else 
		if (window.opera && window.print) {
			var mbm = document.createElement('a');
			mbm.setAttribute('rel', 'sidebar');
			mbm.setAttribute('href', url);
			mbm.setAttribute('title', title);
			mbm.click();
		}
		else {
			window.external.AddFavorite(url, title);
		}
}
