// Javascript Auto Hyperlink Script
// Give it a string and it will automatically add hyperlinks to any links it finds

// Feel free to modify but please leave some sort of thanks or recognition!

// by james <at> bandit.co.nz
// 05/05/08
// special thanks to nik <at> codetocustomer.com for the regex help
function autoLink(what) {
	str = what; out = ""; url = ""; i = 0;
	do {
		url = str.match(/((https?:\/\/)?([a-z\-]+\.)*[\-\w]+(\.[a-z]{2,4})+(\/[\w\_\-\?\=\&\.]*)*(?![a-z]))/i);
		if(url!=null) {
			// get href value
			href = url[0];
			if(href.substr(0,7)!="http://") href = "http://"+href;
		
			// where the match occured
			where = str.indexOf(url[0]);
		
			// add it to the output
			out += str.substr(0,where);
		
			// link it
			out += '<a href="'+href+'" target="_blank">'+url[0]+'</a>';
		
			// prepare str for next round
			str = str.substr((where+url[0].length));
		} else {
			out += str;
			str = "";
		}
	} while(str.length>0);
	return out;
}