function format_telno(num, convert_mode, include_zero_in_intl_uk) {
	var output = '';
	// Strip anything that's not 0123456789+#X
	num = num.replace(/[^0-9\+#X]/g, '');

	// Convert ^([^0\+]) to +\1
	num = num.replace(/^([^0\+])/g, '+$1');

	// Convert 00# to +#
	num = num.replace(/^00/g, '+');

	// Strip the (0) from +44(0)## numbers
	num = num.replace(/^\+440/, '+44');

	if (num.match(/^0/)) {
		if (convert_mode == 'uk_to_intl') {
			if (include_zero_in_intl_uk) {
				output = '+44(0)';
			} else {
				output = '+44';
			}

			if (num.match(/^02/)) {
				output += '2' + num.substr(2, 1) + ' ' + num.substr(3, 4) + '-' + num.substr(7, 4);
			} else if (num.match(/^011[3-8]/)) {
				output += '11' + num.substr(3, 1) + ' ' + num.substr(4, 3) + '-' + num.substr(7, 4);
			} else if (num.match(/^01[2-9]1/)) {
				output += '1' + num.substr(2, 1) + '1 ' + num.substr(4, 3) + '-' + num.substr(7, 4);
			} else if (num.match(/^01[2-9]/)) {
				output += '1' + num.substr(2, 3) + ' ' + num.substr(5, 3) + '-' + num.substr(8, 3);
			} else if (num.match(/^07/)) {
				output += '7' + num.substr(2, 3) + ' ' + num.substr(5, 3) + '-' + num.substr(8, 3);
			} else if (num.match(/^0[3-68]/)) {
				output += num.substr(1, 3) + ' ' + num.substr(4, 3) + '-' + num.substr(7, 4);
			} else if (num.match(/^05/)) {
				output += '5' + num.substr(2, 1) + ' ' + num.substr(3, 4) + '-' + num.substr(7, 4);
			} else if (num.match(/^09/)) {
				output += '9' + num.substr(2, 3) + ' ' + num.substr(5, 3) + '-' + num.substr(8, 3);
			} else {
				output = num;
			}
		} else {
			output = '(0';

			if (num.match(/^02/)) {
				output += '2' + num.substr(2, 1) + ') ' + num.substr(3, 4) + '-' + num.substr(7, 4);
			} else if (num.match(/^011[3-8]/)) {
				output += '11' + num.substr(3, 1) + ') ' + num.substr(4, 3) + '-' + num.substr(7, 4);
			} else if (num.match(/^01[2-9]1/)) {
				output += '1' + num.substr(2, 1) + '1) ' + num.substr(4, 3) + '-' + num.substr(7, 4);
			} else if (num.match(/^01[2-9]/)) {
				output += '1' + num.substr(2, 3) + ') ' + num.substr(5, 3) + '-' + num.substr(8, 3);
			} else if (num.match(/^07/)) {
				output += '7' + num.substr(2, 3) + ') ' + num.substr(5, 3) + '-' + num.substr(8, 3);
			} else if (num.match(/^0[3468]/)) {
				output += num.substr(1, 3) + ') ' + num.substr(4, 3) + '-' + num.substr(7, 4);
			} else if (num.match(/^05/)) {
				output += '5' + num.substr(2, 1) + ') ' + num.substr(3, 4) + '-' + num.substr(7, 4);
			} else if (num.match(/^09/)) {
				output += '9' + num.substr(2, 3) + ') ' + num.substr(5, 3) + '-' + num.substr(8, 3);
			} else {
				output = num;
			}
		}
	} else if (num.match(/^\+44/)) {
		if (convert_mode == 'intl_to_uk') {
			output = '(0';

			if (num.match(/^\+442/)) {
				output += '2' + num.substr(4, 1) + ') ' + num.substr(5, 4) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+4411[3-8]/)) {
				output += '11' + num.substr(5, 1) + ') ' + num.substr(6, 3) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+441[2-9]1/)) {
				output += '1' + num.substr(4, 1) + '1) ' + num.substr(6, 3) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+441[2-9]/)) {
				output += '1' + num.substr(4, 3) + ') ' + num.substr(7, 3) + '-' + num.substr(10, 3);
			} else if (num.match(/^\+447/)) {
				output += '7' + num.substr(4, 3) + ') ' + num.substr(7, 3) + '-' + num.substr(10, 3);
			} else if (num.match(/^\+44[3468]/)) {
				output += num.substr(3, 3) + ') ' + num.substr(6, 3) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+445/)) {
				output += '5' + num.substr(4, 1) + ') ' + num.substr(5, 4) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+449/)) {
				output += '9' + num.substr(4, 3) + ') ' + num.substr(7, 3) + '-' + num.substr(10, 3);
			} else {
				output = num;
			}
		} else {
			if (include_zero_in_intl_uk) {
				output = '+44(0)';
			} else {
				output = '+44';
			}

			if (num.match(/^\+442/)) {
				output += '2' + num.substr(4, 1) + ' ' + num.substr(5, 4) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+4411[3-8]/)) {
				output += '11' + num.substr(5, 1) + ' ' + num.substr(6, 3) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+441[2-69]1/)) {
				output += '1' + num.substr(4, 1) + '1 ' + num.substr(6, 3) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+447/)) {
				output += '7' + num.substr(4, 3) + ' ' + num.substr(7, 3) + '-' + num.substr(10, 3);
			} else if (num.match(/^\+44[3468]/)) {
				output += num.substr(3, 3) + ' ' + num.substr(6, 3) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+445/)) {
				output += '5' + num.substr(4, 1) + ' ' + num.substr(5, 4) + '-' + num.substr(9, 4);
			} else if (num.match(/^\+449/)) {
				output += '9' + num.substr(4, 3) + ' ' + num.substr(7, 3) + '-' + num.substr(10, 3);
			} else {
				output = num;
			}
		}
	} else if (num.match(/^\+/)) {
		output = '+';

		if (num.match(/\+1/)) {
			output += '1.' + num.substr(2, 3) + '.' + num.substr(5, 3) + '.' + num.substr(8, 4);
		} else {
			output = num;
		}
	} else {
		output = num;
	}

	return output;
}
