var AccessInput = new Class({

	initialize: function(input, mask) {
        this.input = input;
        this.mask = mask;
		this.messages = new Hash();
		
        this.input.addEvent("keypress", this.keypress.bind(this));
        this.input.addEvent("keyup", this.keyup.bind(this));
        this.input.addEvent("focus", this.focus.bind(this));
        this.input.addEvent("blur", this.blur.bind(this));	
		return this;
    },
    
    setLabel: function(label) {
    	this.label = label;
    	this.input.value = label;
    	return this;
    },
    
    setName: function(name) {
    	this.name = name;
    	this.input.name = name;
    	return this;
    },
    
    addMessage: function(type, message) {
    	this.messages.set(type, message);
    	return this;
    },
    
    release: function() {
    	this.input.name = null;
        this.input.value = null;

    	this.input.removeEvents("keypress");
        this.input.removeEvents("keyup");
        this.input.removeEvents("focus");
        this.input.removeEvents("blur");
    },
    
    focus: function(event) {
    	if (this.input.value.trim() == this.label) { 
            this.input.value = "";
        }
    	this.mask.focus(event);
    },

    blur: function(event) {
    	this.mask.blur(event);
        if (this.input.value.trim() == "") {
            this.input.value = this.label;
        }
    },

    keypress: function(event) {
    	this.mask.keypress(event);
    },

    keyup: function(event) {
    	this.mask.keyup(event);
    },
    
    isEmpty: function() {
        return this.messages.has("required") && this.input.value.trim() == this.label;
    },

    isValid: function() {
        return this.messages.has("validate") && this.mask.isValid(this.input.value);
    },

    validation: function() {
    	//alert(this.messages.has("empty") + "  " + this.input.value.trim() == this.label);
    	var messages = new Array();
        if (this.isEmpty()) {
            messages.include(this.messages.get("required"));
        } else if (!this.isValid()) {
            messages.include(this.messages.get("validate"));
        }
        return messages;
    }
    
});
