function Mask(mask, align) {
	this.mask = mask;
	this.input = null;
	this.align = align;
	this.limit = (this.mask.substr(0, 1) != '+');
	this.mask = this.mask.replace('+', '');
	if (align == 'right') {
		this.mask = this.mask.reverse();
	}
	this.cleanvalue = '';
}
Mask.prototype.attach = function(input) {
	var self = this;
	input.onkeyup = function(event) {
		return self.doFormMask(event, this);
	}
	if (this.align == 'right') {
		input.style.textAlign = this.align;
	}
	this.input = input;
}
Mask.prototype.doFormMask = function(event, input) {
	var code = this.getCode(event);
	var ch = this.getChar(code);

	if (code == 0 || code == 8 || code == 13) {
		return true;
	}

	this.cleanvalue = input.value;
	input.value = this.doMask();

	return false;
}
Mask.prototype.doMask = function() {
	var maskedvalue = '';

	if (this.align == 'right') {
		this.cleanvalue = this.cleanvalue.reverse();
	}

	for (var i = 0; i < this.cleanvalue.length; i++) {
		maskedvalue = this._doMask(maskedvalue, this.cleanvalue.substr(i, 1));
	}

	if (this.align == 'right') {
		this.cleanvalue = this.cleanvalue.reverse();
		maskedvalue = maskedvalue.reverse();
	}

	return maskedvalue;
}
Mask.prototype._doMask = function(maskedvalue, ch) {
	if (!this.limit || maskedvalue.length < this.mask.length) {
		if (maskedvalue.length < this.mask.length) {
			mch = this.mask.substr(maskedvalue.length, 1);
		} else {
			mch = this.mask.substr(this.mask.length - 1, 1);
		}

		if (mch == '#') {
			if (!isNaN(ch) && ch != ' ') {
				maskedvalue += ch;
			}
		} else {
			maskedvalue += mch;
			maskedvalue = this._doMask(maskedvalue, ch);
		}
	}
	
	return maskedvalue;
}
Mask.prototype.getCode = function(oevent) {
	var code;

	if (navigator.appName == 'Microsoft Internet Explorer') {
		code = event.keyCode;
	} else if (navigator.appName == 'Netscape') {
		code = oevent.which; 
	}
	return code;
}
Mask.prototype.getChar = function(code) {
	var ch = String.fromCharCode(code);
	return ch;
}

