function extractURLs(txt, autoDetectNoProtocol) {
	if(autoDetectNoProtocol == undefined) {
		autoDetectNoProtocol = true;
	}
	var x = prelimURLs(txt, autoDetectNoProtocol);
	var dPathComponent = "(" + myPathRegEx + "*)";
	var myURLPath = '(\\/' + dPathComponent + ')';
	var validHostname = "(" + hostComponent + "[.])+(" + hostComponent + "){1}(\\:[0-9]+){0,1}";
	var laxHostname = "www" + "\\.(" + hostComponent + "[.])+(" + hostComponent + "){1}(\\:[0-9]+){0,1}";
	var noProtocolURL = "(" + laxHostname + '' + myURLPath + '*)';
	var noProtocolURLOptionalPath = "(" + validHostname + myURLPath + "*)";
	var laxProtocolURL = "(" + protocolOptions + "\\:\\/\\/" + noProtocolURLOptionalPath + ")";
	var screwyProtocolURL = "(\\:\\/\\/" + noProtocolURLOptionalPath + ")";
	var noTrailingTag = '';
	if (validArray(x)) {
		var resx = new Array();
		for (var i = 0; i <= x.length - 1; i++) {
			if (!txt.match(RegExp('\\<[^>]*' + RegEscape(x[i]), 'gi'))) {
				pushIfMatch(resx, x[i], laxProtocolURL + noTrailingTag);
				if (autoDetectNoProtocol) {
					pushIfMatch(resx, x[i], screwyProtocolURL + noTrailingTag);
					pushIfMatch(resx, x[i], noProtocolURL + noTrailingTag);
				}
			}
		 }
	}
	if (validArray(resx)) {
		resx = removeDupFixedURLsInArray(resx);
		return resx;
	} else {
		return null;
	}
}
function fixLinkProtocol(lnk) {
	if (!(lnk.match(/^https?:\/\//g)) && !(lnk.match(/^ftps?:\/\//g))) {
		var split = lnk.split('://');
		if (split.length > 1) {
			if (split[0] == 'ftp' || split[0] == 'ftps' || split[0] == 'http' || split[0] == 'https') {
			  return split[0] + '://' + lnk;
			} else {
			  return 'http://' + split[1];
			}
		 } else {
			return 'http://' + lnk;
		 }
	} else {
		return lnk;
	}
}

function validArray(arr) {
	return (arr && (arr.length > 0));
}
function RegEscape(text) {
	return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
function prelimURLs(txt,autoDetectNoProtocol) {
	if(autoDetectNoProtocol == undefined) {
		autoDetectNoProtocol = true;
	}
  //s = String(txt);
	s = txt;
	var simplepath = '([^\\s])';
	var protocol = '(' + protocolOptions + '\\:\\/\\/)' + simplepath + '+';
	var partial = '(\\:\\/\\/)' + simplepath + '+';
	var noprotocol = '(www\\.)' + simplepath + '+';
	var x = new Array();
	x = myconcat(x, s.match(new RegExp(protocol, 'g')));
	if (autoDetectNoProtocol == "true") {
		x = myconcat(x, s.match(new RegExp(partial, 'g')));
		x = myconcat(x, s.match(new RegExp(noprotocol, 'g')));
	}
  return x;
}
function myconcat(arr, newarr) {
	if (newarr) {
		return arr.concat(newarr);
	} else {
		return arr;
	}
}
function pushIfMatch(arr, s, regtext) {
	var reg = new RegExp(regtext);
	var a = s.match(reg);
	if (a) {
		for (var i = 0; i < arr.length; i++) {
			if (fixLinkProtocol(a[0]) == fixLinkProtocol(arr[i])) {
				return;
			}
		}
	arr.push(a[0]);
	}
}
function removeDupFixedURLsInArray(arr) {
	var newarr = [];
	for (var i = 0; i < arr.length; i++) {
	 for (var j = i + 1; j < arr.length; j++) {
		if (fixLinkProtocol(arr[i]) == fixLinkProtocol(arr[j])) {
		  j = ++i;
		}
		}
		newarr.push(arr[i]);
	}
	return newarr;
}

var hostComponent =  "([a-zA-Z0-9]{2,}|([a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]))";
var myPathRegEx = "[a-zA-Z0-9\u0430-\u044F\u0410-\u042F\\%\\$\\-\\?\\_\\.\\+\\!\\*\\'\\(\\)\\,\\;\\:\\@\\&\\=\\+\\~\\#]";
var protocolOptions = '(https?|ftps?)';

