Proto.UI.autopassword = Class.create({
        process:false,
        initialize: function() 
        {
          
        this.options = Object.extend({
                className:'Proto_autopassword',
                element:false,
                text:"Auto",
                secondElement:false,
                length:9,
                show:true,
                onUpdate:function(){}
                }, arguments[0] || { });
        if(!this.options.element){ throw("Element no found for autopassword"); }
        this.create();
        
        },
        create:function()
        {
          this.wrapper = new Element("div",{'class':this.options.className});
          this.options.element.wrap(this.wrapper);
          if(this.options.secondElement)
                {
                this.wrapper2 = new Element("div",{'class':this.options.className});
                this.options.secondElement.wrap(this.wrapper2);
                if(this.options.show){ this.options.secondElement.type="password"; }
                }
           if(this.options.show){ this.options.element.type="password"; }
          this.button = new Element("div",{'class':this.options.className+"-button"});
          this.wrapper.insert(this.button);this.button.update(this.options.text);
          this.button.observe("mousedown",function(e){Event.stop(e);this.process=true;this.gen();}.bind(this));
          this.button.observe("mouseup",function(e){Event.stop(e);this.process=false;}.bind(this));
        },
        gen:function()
        {
             if(this.options.show){ this.options.element.type="text";
                this.options.secondElement.type="text"; }
                
                new PeriodicalExecuter(function(pe) {
                if(!this.process){ pe.stop();}
                this.options.element.value=this.random_pass();
                if(this.options.secondElement)
                {
                this.options.secondElement.value=this.options.element.value;
               //if(!this.options.show){ this.button.update(this.options.element.value); }
                }
                this.options.onUpdate.bind(this).call();
              }.bind(this), 0.1);
             
        },
        random_pass:function()
        {
          var tokens=[];
          for (var j = 0; j < this.options.length; j++){
          tokens[tokens.length]=String.fromCharCode(parseInt((Math.floor(Math.random()*26)+65).toString(16),16));
          tokens[tokens.length]=String.fromCharCode(parseInt((Math.floor(Math.random()*26)+97).toString(16),16));
          tokens[tokens.length]=Math.floor(Math.random()*10)
          }
          tokens.sort(function() {return 0.5 - Math.random();}) 
          tokens.length=this.options.length;
          return tokens.join('');

        }
      })