function emptyFormat(l){
	var i, empty = '';
	for(i = 0; i < l; i++) empty += '0';
return empty.slice(1);
}
String.prototype.formatEntry = function(p, g, l, c, a){
	var aux = this.replace(new RegExp('(^0|[^0-9' + (a != null ? a : '') + '])+', 'g'), '');
	while(aux.length < l) aux = '0' + aux;
	aux = (aux.substring(0, p) + g + aux.substring(p)).slice(0, c);
return(aux.replace(/([^0-9])+/g, '') != emptyFormat(c) ? aux : '');
}
String.prototype.formatCurrency = function(c){
	var t = this; t = t.replace(/,/g, '.');
	if(c == undefined) c = 2;
	var p, d = (t.indexOf('.') > -1 ? (t = t.split('.'))[1] : t).substr(0, c);
	for(p = (t = t[0] ? t[0] : '0').length; (p -= 3) >= 1;) t = t.substr(0, p) + '.' + t.substr(p);
return(t + ',' + d + Array(c + 1 - d.length).join(0));
}
String.prototype.formatFloat = function(){
	var t = this; t = t.replace(/\./g, '');
	var p, d = (t = t.split(','))[1];
	for(p = (t = t[0]).length; (p -= 3) >= 1;) t = t.substr(0, p) + '.' + t.substr(p);
return(t + (d == undefined ? '' : ',' + d));
}
String.prototype.formatDateTime = function(k, l, g, c){
	if((!k || k == g) && this.length == 0) return this;
	var t = this; t = (t + k).replace(/(\/|:)+/g, g);
	if(k == g){
		var aux = t.split(g);
		for(var i = 0; i < l.length; i++){
			var s = parseInt(l.substring(i, i + 1), 10);
			if(aux[i]){
				if(s == 4){
					aux[i] = parseInt(aux[i], 10);
					if(aux[i] < 50) aux[i]+= 2000; else if(aux[i] < 100) aux[i]+= 1900;
				}else
					while(aux[i] && aux[i].length < s) aux[i] = '0' + aux[i];
			}
		}
		t = aux.join(g);
	}
return(t.replace(/^(\d{2})(\d)$/g, '$1' + g + '$2').replace(/^(\d{2}[\/:]\d{2})(\d)$/g, '$1' + g + '$2').replace(/^24:/, '00:').slice(0, c));
}
function getSel(){
	var txt = '';
	try{
		if(window.getSelection) txt = window.getSelection();
		else if(document.getSelection) txt = document.getSelection();
		else if(document.selection) txt = document.selection.createRange().text;
	}catch(e){};
return(txt);
}
function format(o, c, m){
	var aux, len, m = o.mask ? o.mask : m ? m : '', tab;
	if(!m) return;
	if(!c) c = '';
	function float(v){
		var l;
		switch(l = v.length){
			case 0: return('');
			case 1: return('0,0' + v);
			case 2: return('0,' + v);
			default: return((v.substring(0, l - 2) + ',' + v.substring(l - 2)));
		}
	}
	switch(m){
		case 'D': o.value = o.value.formatDateTime(c, '22' + (o.len == 10 ? '4' : '2'), '/', o.len); break;
		case 'H': o.value = o.value.formatDateTime(c, '22', ':', o.len); break;
		case 'M':
			aux = (o.value + c).replace(/(^0|[^0-9])+/g, '');
			o.value = float(aux).formatCurrency();
		break;
		case 'C':
			var minus = c == '-' ? (o.value.indexOf('-') == 0 ? '' : '-') : o.value.substring(0,1).replace(/^[0-9]/, '');
			aux = (o.value + (c == '-' ? '' : c)).replace(/(^0|[^0-9])+/g, '');
			o.value = minus + float(aux).formatCurrency();
			if(o.len) o.value = o.value.substring(0, o.len);
		break;
		case '%':
			var minus = c == '-' ? (o.value.indexOf('-') == 0 ? '' : '-') : o.value.substring(0,1).replace(/^[0-9]/, '');
			c = (c == '-') ? '' : c.replace(/[^0-9]/, ',');
			if(o.value.indexOf(',') > -1 && c == ',') c = '';
			o.value = minus + ((o.value.replace(/(^0|[^0-9,])+/g, '') + c).replace(/,+/g, ',')).replace(/^,/, '0,').formatFloat();
			if(o.len) o.value = o.value.substring(0, o.len);
		break;
		case 'P':
			o.value = (o.value + c);
			if(c != '*' && o.value.indexOf('*') == -1){
				o.len = 9;
				o.value = o.value.formatEntry(4, o.value.indexOf('*') == -1 ? '-' : '', 8, 9, '*');
			}else
				o.len = 15;
			o.value = o.value.replace(/^0+/, '').replace(/^-0*/g, '').replace(/[^0-9-*]/, '');
		break;
		//case 'P': o.value = (o.value + c).formatEntry(4, '-', o.len); break;
		case 'Z': o.value = (o.value + c).formatEntry(5, '-', 8, 9); if(c == '0' && !o.value.length) o.value = '0'; break;
		default:
			if(o.len > 0 && (o.jump = jump(o.value, o.len - 1))){
				o.value += c;
			return nextField(o, o.fw);
			}
		return true;
	}
	o.jump = o.isSetJump && jump(o.value, o.len);
	nextField(o, o.fw);
return false;
}
function jump(v, l){ return(l != null && v.length >= l && l > 0); }
function bs(obj, k, x){
	if(k == 8){
		obj.jump = false;
		if(obj.value.length > 0){
			obj.value = obj.value.slice(0, -1);
			format(obj);
		return false;
		}
	}
	if(in_array(k, [9,13,27]) != -1){
		format(obj);
		obj.jump = jump(obj.value, obj.len);
	return(!nextField(obj, obj.fw));
	}
return true;
}
function checkKey(e){
	var k = (isIE ? event.keyCode : e.which), c = getCode(k).toUpperCase();
	if(k != 0 && this.erase){
		this.value = '';
		this.erase = false;
	}
	if(!isIE && !bs(this, k, true)) return false;
	if(in_array(k, [9,13,27]) != -1){ //enter, esc
		format(this);
		this.jump = jump(this.value, this.len);
	return(nextField(this, this.fw));
	}else{
		if(k < 32) return true;
		if(k > 127) return false;
	}
	switch(this.mask){
		//Number
		case 'N': return (c < '0' || c > '9') ? false : format(this, c);
		//Money
		case 'M': return ((c < '0' || c > '9') && in_array(c, ['.',',']) == -1) ? false : format(this, c);
		//Currency
		case 'C': return ((c < '0' || c > '9') && in_array(c, ['.',',','+','-','*','/']) == -1) ? false : format(this, c);
		//Percent
		case '%': return ((c < '0' || c > '9') && in_array(c, ['.',',','+','-','*','/']) == -1) ? false : format(this, c);
		//Date
		case 'D': var i = in_array(c, ['.',',',':','/','-']), v = this.value.length;
			return ((c < '0' || c > '9') && i == -1 || i != -1 && !v) ? false : format(this, (i < 0 ? c : this.glue = '/'));
		//Hour
		case 'H': var i = in_array(c, ['.',',',':','/','-']), v = this.value.length;
			return ((c < '0' || c > '9') && i == -1 || i != -1 && !v) ? false : format(this, (i < 0 ? c : this.glue = ':'));
		//Phone
		case 'P': return (c < '0' || c > '9') && c != '*' ? false : format(this, c);
		//Zip
		case 'Z': return (c < '0' || c > '9') ? false : format(this, c);
	}
return true;
}
function setMask(o, m, s, fw){
	o.jump = false;
	o.erase = true;
	o.isSetJump = (m == 'D' || m == 'H' || s != null) && m != 'Z';
	if(o.onkeypress == null){
		o.fw = fw ? fw : 1;
		o.len = s ? s : null;
		o.glue = '';
		o.mask = m.toUpperCase();
		if(isIE) o.onkeydown = function(e){ return bs(this, event.keyCode); }
		o.onkeypress = checkKey;
		o.onkeyup = function(e){ if(jump(this.value, this.len)) nextField(this, this.fw); }
		// onBlur
		var _blur = typeof(o.onblur) == 'function' ? o.onblur : new Function();
		switch(o.mask){
			case 'D': if(o.len == null) o.len = 10; o.onblur = function(e){ this.value = this.value.formatDateTime('/','224','/',10).replace(/\/$/, ''); }; break;
			case 'H':
				if(o.len == null)
					o.len = 5;
				o.onblur = function(e){ this.value = this.value.formatDateTime(':00','22',':',this.len).replace(/:([0-9])$/, ':0$1').replace(/^:.*/, ''); _blur(); };
			break;
			case 'C': o.onblur = function(e){ this.value = this.value.replace(/,$/, '').replace(/^-0$/, '0'); _blur(); }; break;
			case '%': o.onblur = function(e){ this.value = this.value.replace(/,$/, '').replace(/^-0$/, '0'); _blur(); }; break;
		}
	}
	/* if(o.onclick == null) o.onclick = function(e){ selectField(this); }*/
	if(o.onclick == null) o.onclick = function(e){ this.select(); o.erase = true; }
	//if(o.onselect == null) o.onselect = function(e){ selectField(this); }
return true;
}