(function (root, undefined) {

	var n_minChars = 8;
	var n_maxChars = 20;

	var n_space = " ".charCodeAt(0);

	var a_allowedRanges = [
		['(', '('],
		[')', ')'],
		['+', '+'],
		['0', '9'],
		['-', '-']
	];

	a_allowedRanges = Ext.Array.map(a_allowedRanges, function(a_range) {
		return Ext.Array.map(a_range, function(s_char) {
			return s_char.charCodeAt(0);
		});
	});

	Ext.apply(Ext.form.field.VTypes, {
		//  vtype validation function
		phone: function(value_) {

			var value = value_.toLowerCase();

			var n_chars = 0;
			var n_char;

			for (var i = 0, len = value.length; i < len; i++) {

				n_char = value.charCodeAt(i);

				var b_allowed = Ext.Array.some(a_allowedRanges, function(a_range) {
					return Ext.Array.every(a_range, function(n_limit, n_key) {
						return n_key == 0 ? n_char >= n_limit : n_char <= n_limit;
					});
				});

				if (b_allowed) {
					n_chars++;
				}

				// exit if not a space
				else if (i > 0 && n_char != n_space) {
					return false;
				}

				// exit if maxChars is reached
				if (n_chars > n_maxChars) {
					return false;
				}
			}

			return n_chars >= n_minChars;
		},

		// vtype Text property: The error text to display when the validation function returns false
		phoneText: ip2.i18n({
			da: "Fejl i telefonnummer",
			en: "Error in phone number"
		}),

		// vtype Mask property: The keystroke filter mask
		phoneMask: /.*/i
	});
})(this);
