
jQuery.fn.TextField = function(params) {
   if (!params.type)
      return;
   
   return this.each(function() {
      if (this.tagName != 'INPUT' && this.getAttribute('type') != 'text')
			return;
      
      this.textF = {};
      this.textF.type = params.type;
      this.textF.object = this;
      this.textF.defaultValue = params.defaultValue || "";
      this.textF.defaultValueClass = params.defaultValueClass || "defaultValue";
      this.textF.errorHtml = params.errorHtml || false;
      this.textF.errorTarget = jQuery(params.errorTarget)[0] || null;
      this.textF.errorClass = params.errorClass || "";
      this.textF.inputTarget = params.inputTarget || null;
      
      this.textF.addDefaultValue = function() {
         if (this.defaultValue) {
            if (this.errorClass) 
               jQuery(this.object).removeClass(this.errorClass);
            jQuery(this.object).val(this.defaultValue);
            if (this.defaultValueClass)
               jQuery(this.object).addClass(this.defaultValueClass);
         }
      };
      
      this.textF.removeDefaultValue = function() {
         jQuery(this.object).val("");
         if (this.defaultValueClass)
            jQuery(this.object).removeClass(this.defaultValueClass);
      };
      
      this.textF.error = params.onError && params.onError.constructor == Function ? params.onError : function(obj) {
         if (this.errorTarget && this.errorHtml)
            jQuery(this.errorTarget).html(this.errorHtml);
         if (this.errorClass)
            jQuery(this.object).addClass(this.errorClass);
      };
      
      this.textF.success = params.onSuccess && params.onSuccess.constructor == Function ? params.onSuccess : function(obj) {
      };
      
      this.textF.validate = function(throwError,throwSuccess) {
         if (!this.check()) {
            if (throwError)
               this.error(this.object);
            return false;
         }
         else {
            if (throwSuccess)
               this.success(this.object);
            return true;
         }
      };
      
      switch (params.type) {
         case 'natural':
            this.textF.check = function() {
               if (!jQuery(this.object).val().match(/^\d+$/))
                  return false;
               else 
                  return true;
            };
            jQuery(this).keypress(function(e) {
               e = e||window.event;
               if (jQuery.browser.mozilla && ((e.charCode>=48 && e.charCode<=57) || e.charCode==0))
                  return true;
               else if (!(jQuery.browser.mozilla) && ((e.keyCode>=48 && e.keyCode<=57) || e.keyCode<32))
                  return true;
               else
                  return false;
            });
            break;
      }
      
      jQuery(this).blur(function() {
         if (!this.textF.validate(true,true)) {
            if (!jQuery(this).val())
               this.textF.addDefaultValue();
            else if (jQuery(this).val() != this.textF.defaultValue)
               jQuery(this).addClass(this.textF.errorClass);
         }
      });
      
      jQuery(this).focus(function() {
         if (jQuery(this).val() == this.textF.defaultValue)
            this.textF.removeDefaultValue();
         if (this.textF.errorClass)
            jQuery(this).removeClass(this.textF.errorClass);
      });
      
      if (!this.textF.check())
         this.textF.addDefaultValue();
   });
};

jQuery.fn.Select = function(params) {
   return this.each(function() {
      
      var $wrapper = jQuery(this).parent(".desplegable_wrapper");
      var $options = jQuery("li",$wrapper);
      var $list = jQuery("ul",$wrapper);
      var $inHidden = jQuery("input:hidden",$wrapper);
      var $inText = jQuery("input:text",$wrapper);
      var $arrow = jQuery(".select_arrow",$wrapper);
      
      $inText.ocultar = true;
      
      if ($inText.val()=="") {
         $inText.val(params.defaultValue);
         $inText.addClass("defaultValue");
      }
         
      if (jQuery.browser.msie) {
         $list.mousedown(function(e) {
            if (e.target.tagName!="LI")
               $inText.ocultar = false;
            else
               $inText.ocultar = true;
         });
      }
      
      $inText.focus(showOptions);
      $inText.click(showOptions);
      $arrow.click(showOptions);
      $inText.blur(function (e) {
         if ($inText.ocultar)
            hideOptions();
         else {
            $inText.ocultar = true;
            $inText.focus();
         }
      });
      
      $options.hover(
         function() { jQuery(this).addClass("selectable"); },
         function() { jQuery(this).removeClass("selectable"); }
      );
      
      $options.mousedown(function() { selectItem(this); });
      
      if (jQuery.browser.msie)
         $inText.keydown(preventKey);
      else
         $inText.keypress(preventKey);
         
      function selectItem(item) {
         if (!jQuery(item).is(".selected")) {
            $options.removeClass("selected");
            jQuery(item).addClass("selected");
            var aVal = jQuery(item).attr("id").split("_");
            $inHidden.val(aVal[1]);
            $inText.val(jQuery(item).text());
            if ($inText.is(".defaultValue"))
               $inText.removeClass("defaultValue");
            if (params.onChange && params.onChange.constructor == Function)
               params.onChange();
         }
      }
      
      function preventKey(e) {
         var key = e.charCode || e.keyCode || -1;
         if (key != 9)
            return false;
         else
            return true;
      }
      
      function showOptions() {
         if ($list.is(":hidden")) {
            $wrapper.css({zIndex: 100});
            $list.slideDown("fast");
            $inText.get(0).focus();
         }
      }
      function hideOptions() {
         if (!$list.is(":hidden")) {
            $list.hide();
            $wrapper.css({zIndex: 10});
         }
      }
   });
};
   

jQuery.fn.MenuSelect = function(params) {
   return this.each(function() {
      
      var $wrapper = jQuery(this);
      var $list = jQuery("ul",$wrapper);
      var $trigger = jQuery(".trigger",$wrapper);
      var $label = jQuery(".label",$wrapper);
      var $items = jQuery("li",$wrapper);
      var $input = jQuery("input:hidden",$wrapper);
      
      $trigger.click(function(e) {
         $list.show();
         e.stopPropagation();
      });
      
      $items.click(function() {
         var aVal = jQuery(this).attr("id").split("_");
         $input.val(aVal[1]);
         if (params.onSelect && params.onSelect.constructor == Function)
            params.onSelect($input.val(),jQuery(this).text());
      });
      
      $items.hover(
         function() { jQuery(this).addClass("selectable"); },
         function() { jQuery(this).removeClass("selectable"); }
      );
      
      jQuery(document).click(function() { $list.hide(); });
      if (jQuery.browser.msie)
         jQuery(document).keydown(function() { $list.hide(); });
      else
         jQuery(document).keypress(function() { $list.hide(); });
   });
};

jQuery.fn.LocField = function(params) {
   
   return this.each(function() {
      if (!jQuery(this).is('input:text'))
         return;
      $inText = jQuery(this);
      $inHidden = $inText.siblings("input:eq(0)");
      $inFlag = $inText.siblings("input:eq(1)");
      
      $inText.Autocomplete({
         source: '/include/ajax/ac.php?f=1',
         delay: 500,
         autofill: false,
         helperClass: 'desplegable',
         selectClass: 'selectable',
         minchars: 3,
         inputWidth: true,
         multiple: false,
         onSelect: function(data) {
            $inHidden.val(data.cod);
            $inFlag.val("0");
         }
      });
      
      if (jQuery.browser.msie)
         $inText.keydown(preventKey);
      else
         $inText.keypress(preventKey);
      
      $inText.focus(removeDefaultValue);
      $inText.blur(addDefaultValue);
            
      function preventKey(e) {
         if (e.keyCode==13)
            return false;
         else {
            $inFlag.val("1");
            return true;
         }
      }; 
      
      function addDefaultValue() {
         if (params.defaultValue) {
            if (jQuery.trim($inText.val()) == "") {
               $inText.val(params.defaultValue);
               $inText.addClass("defaultValue");
               $inHidden.val("0");
            }
         }
      };
      
      function removeDefaultValue() {
         if ($inText.is(".defaultValue")) {
            $inText.val("");
            $inText.removeClass("defaultValue");
         }
      };
      
      addDefaultValue();
   });
};
   


   
function alertKey(e) {
   e = e||window.event;
   str = "";
   str += "keyCode = "+e.keyCode+"\n";
   str += "charCode = "+e.charCode+"\n";
   str += "which = "+e.which+"\n";
   if (e.altKey)
      str += " ALT ";
   if (e.ctrlKey)
      str += " CTRL ";
   if (e.shiftKey)
      str += " SHIFT ";
   if (e.metaKey)
      str += " META ";
   if (e.isChar)
      str += " CHAR ";
   alert(str);
}
     
function checkEmail(email) {
   return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email));
}