/**
 * Documentar!
 * 
 * @author Walker de Alencar | http://www.walkeralencar.com
 */
var Helper = {
  Mask: {
    cpf: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataCPF(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataCPF(objElement);
      });
    },
    cnpj: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataCNPJ(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataCNPJ(objElement);
      });
    },
    cep: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataCep(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataCEP(objElement);
      });
    },
    data: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataData(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataData(objElement);
      });
    },
    fone: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataTelefone(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataTelefone(objElement);
      });
    },
    onlyText: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataApenasTexto(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataApenasTexto(objElement);
      });
    },
    nome: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataNome(objElement);
      });
      Event.observe(objElement, "keypress", function() {
        formataNome(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataNome(objElement);
      });
    },
    onlyNumber: function(objElement){
      Event.observe(objElement, "keyup", function() {
        formataApenasNumero(objElement);
      });
      Event.observe(objElement, "change", function() {
        formataApenasNumero(objElement);
      });
    },
    displayLength: function(objElement,objElementDisplay){
      objElementDisplay.update(objElement.value.replace(/([\n\r])+/g, "  ").length);
      Event.observe(objElement, "keyup", function() {
        objElementDisplay.update(objElement.value.replace(/([\n\r])+/g, "  ").length);
      });
    }
  },
  icon: {
    build: function(objElement,options){
      var opt = {
        alt      : (options.alt  == undefined)?'Ícone':options.alt,
        msg      : (options.msg  == undefined)?'':options.msg,
        icon     : (options.icon == undefined)?'success.gif':options.icon
      }
      objElement.alt = opt.alt;
      objElement.title = opt.msg;
      objElement.src = pkykConf.imagesPath + '/icones/form/' + opt.icon
      objElement.show();
    },
    
    valid: function(objElement, msg){
      Helper.icon.build(objElement,{
        alt: 'Ícone de OK',
        msg: msg,
        icon: 'success.gif'
      });
    },  
    
    error: function(objElement, msg){
      Helper.icon.build(objElement,{
        alt: 'Ícone de Erro',
        msg: msg,
        icon: 'error.gif'
      });
    } 
  }, 
  indicator: {
    build: function(target,options){
      var opt = {
        alt      : (options.alt  == undefined)?'Ícone':options.alt,
        msg      : (options.msg  == undefined)?'':options.msg,
        icon     : (options.icon == undefined)?'success.gif':options.icon,
        className: (options.className == undefined)?'ok':options.className
      }
      var elementError = $(pkykConf.inlineErrorId + Helper.element.getName(target));
      if ( elementError == null ){
        new Insertion.After( target, 
          ' <img id="'+ pkykConf.inlineErrorId + Helper.element.getName(target) 
          + '" src="' + pkykConf.imagesPath + '/icones/form/' + opt.icon 
          + '" alt="' + opt.alt 
          + '" title="' + opt.msg + '"/>'
          );
      } else {
        Helper.icon.build(elementError,{
          alt: opt.alt,
          msg: opt.msg,
          icon: opt.icon
        });
      }
      target.className = opt.className;
    },
  	
    error: function(target, msg){
      Helper.indicator.build(target,{
        alt: 'Ícone de Erro',
        msg: msg,
        className: 'error',
        icon: 'error.gif'
      });
    },
    valid: function(target, msg){
      Helper.indicator.build(target,{
        alt: 'Ícone de OK',
        msg: msg,
        className: 'ok',
        icon: 'success.gif'
      });
    }
  },
  element: {
    getName: function(target){
      return target.name.replace('[','_').replace(']','');
    }
  }  
}

