//Function to get classes form document
function getElementsByClass(className) {
	var classElements = new Array();
	var els = document.getElementsByTagName('*');
	var elslength = els.length;
	var pattern = new RegExp(className);

	for (i = 0; i < elslength; i++) {
		if (pattern.test(els[i].className)) {
			classElements[classElements.length] = els[i];
		}
	}

	return classElements;
}

//Replace search words with bold words
function displaySearchBold(search) {
	var searchwords = search.split(' ');
	var topics = getElementsByClass('topics');
	var urls = getElementsByClass('url');

	//Replace all search words
	for (x = 0; x < searchwords.length; x++) {

		//Replace words in URL's with regular expression
		for (y = 0; y < urls.length; y++) {
			var newurl = urls[y].innerHTML;
			var re = new RegExp(searchwords[x], 'g')
			newurl =  newurl.replace(re, '<strong>' + searchwords[x] + '</strong>');
			urls[y].innerHTML = newurl;
		}

		//Replace words in TOPIC's with multiple regular expressions
		for (y = 0; y < topics.length; y++) {
			var newtopic = topics[y].innerHTML;
			var re1 = new RegExp('(^'+searchwords[x]+',)', 'g')
			var re2 = new RegExp('( '+searchwords[x]+',)', 'g')
			var re3 = new RegExp('( '+searchwords[x]+' )', 'g')
			newtopic = newtopic.replace(re1, '<strong>' + searchwords[x] + '</strong>,');
			newtopic = newtopic.replace(re2, ' <strong>' + searchwords[x] + '</strong>,');
			newtopic = newtopic.replace(re3, ' <strong>' + searchwords[x] + ' </strong>');
			topics[y].innerHTML = newtopic;
		}
	}
}

//Set focus to an element in the form
function setFocus(elementid){
	var element=document.getElementById(elementid);
	if (element!=null ){
		element.focus();
	}
}