function setNavigationMenuItemActive(channel, section, subSection){
	try{
		var itemName = "section-navigation-item";
		itemName += "-" + channel;
		if (section != null) itemName += "-" + section;
		if (subSection != null) itemName += "-" + subSection;
		itemName = itemName.replace(/[^A-Za-z0-9\-]/gi, "_"); //replace non alphanumerics with _
		document.getElementById(itemName).getElementsByTagName('a')[0].className += ' selected';
	} catch(e){}
}


//Add bookmark / Add to favourites - works in FF and IE (jFav jQuery plugin can do this)
function addBookmark(url, title) {
	if (url == null || url.length == 0) url = window.location.href;
	if (title == null || title.length == 0 ) title = document.title;
	
	// user agent sniffing is bad in general, but this is one of the times 
	// when it's really necessary
	var ua = navigator.userAgent.toLowerCase();
	var isKonq = (ua.indexOf("konqueror") != -1);
	var isSafari = (ua.indexOf("webkit") != -1);
	var isMac = (ua.indexOf("mac") != -1);
	var buttonStr = isMac ? "Command/Cmd" : "CTRL";
	var startAdvice = "To bookmark this page, please press the ";
	var endAdvice = " keys at the same time.";

	try{
		if (navigator.appName == 'Microsoft Internet Explorer') {
			window.external.AddFavorite(url, title);
		} else {
			window.sidebar.addPanel(title, url, "");
		}
	} catch(e){
		if (isKonq) {
		  alert(startAdvice + "CTRL and B" + endAdvice);
		} else if (window.opera) {
		  void(0); // do nothing here (Opera 7+)
		} else if (window.home || isSafari) { // Firefox, Netscape, Safari, iCab
		  alert(startAdvice + buttonStr + " and D" + endAdvice);
		} else if (!window.print || isMac) { // IE5/Mac and Safari 1.0
		  alert(startAdvice + buttonStr + " and D" + endAdvice);    
		} else {
		  alert("Your browser does not allow automatic bookmarking,\nplease bookmark this page manually.");
		}
	}
}


function getUrlParameters(){
	var urlParameters = new Array();
	var query = window.location.search.substring(1);
	if (query.length > 0){
		var urlParameterSet = query.split("&");
		var pos = 0;
		for (var i = 0; i < urlParameterSet.length; i++){
			pos = urlParameterSet[i].indexOf("=");
			urlParameters[urlParameterSet[i].substring(0, pos)] = urlParameterSet[i].substring(pos + 1);
		}
	}
	return urlParameters;
}


/** BEGIN Form functions **/
//Call this to blank an input which still has its default text (eg onfocus or onsubmit)
function clearDefaultInput(field){
	if (field.value == field.defaultValue){
		field.value = "";
	}
}


function preFillFormWithUrlParameters(theForm){
	var theForm = jQuery(theForm)[0];
	var urlParameters = getUrlParameters();
	for (param in urlParameters){
		try{
			value = unescape(urlParameters[param]).replace(/\+/gi, ' ');
			param = unescape(param); //used to handle s[param] names
			if (theForm.elements[param] == null) continue; //no point going further if the element's not there
			jQuery(theForm.elements[param]).filter('input:text').val(value);
			jQuery(theForm.elements[param]).filter('input:radio[value="' + value + '"]').attr("checked", "checked"); 
			//TODO checkboxes
			jQuery(theForm.elements[param]).filter('select').selectOptions(value).change(); //change() fires the event (as if a user changed the value) - needed for property prices and motoring
		} catch(e){} //shuddup
	}
}
/** END Form functions **/


//Convert eg 1000000 to 1,000,000
//http://bytes.com/groups/javascript/145113-formatting-number-thousands-separator#post543267
function formatThousandSeparator(n){
	var ts=",", ds="."; // thousands and decimal separators

	var ns = String(n),ps=ns,ss=""; // numString, prefixString, suffixString
	var i = ns.indexOf(".");
	if (i!=-1) { // if ".", then split:
		ps = ns.substring(0,i);
		ss = ds+ns.substring(i+1);
	}
	return ps.replace(/(\d)(?=(\d{3})+([.]|$))/g,"$1"+ts)+ss;
}


/** BEGIN Date functions **/
//http://www.go4expert.com/forums/showthread.php?t=886
function daysInMonth(iMonth, iYear){
	return 32 - new Date(iYear, iMonth, 32).getDate();
}


//http://michaelthompson.org/technikos/holidays.php
function getNthDaynameOfMonth(nTargetday, nMonth, nYear, nTh){
	/*
		Find (for example) the 1st(1) Sunday(0) in June(06) 2009 -
		getNthDaynameOfMonth(0, 06, 2009, 1);
		Use 5 for last
	*/

	nMonth = nMonth - 1; //Months are zero-based, input is human-based

	//Determine from the definition of the holiday the earliest possible date in the month on which it could occur
	var nEarliestDate = 1 + 7 * (nTh - 1);

	//Determine the weekday of the earliest possible date in the month on which the holiday could occur in the given year
	var d = new Date(nYear, nMonth, nEarliestDate);
	var nWeekday = d.getDay();

	//Find the offset between the target weekday and weekday of the earliest possible date in the given year
	var nOffset;
	if (nTargetday == nWeekday){
		nOffset = 0;
	} else {
		if (nTargetday < nWeekday){
			nOffset = nTargetday + (7 - nWeekday);
		} else {
			nOffset = (nTargetday + (7 - nWeekday)) - 7;
		}
	}

	//Calculate the actual date of the holiday using the earliest possible date and the offset for the given year
	var requiredDate = new Date(nYear, nMonth, nEarliestDate + nOffset);
	
	//If we get a value that's beyond this month (eg 5th Monday when there are only 4), go back 7 days
	if (requiredDate.getMonth() > nMonth)
		requiredDate.setDate(requiredDate.getDate() - 7);
	return requiredDate;
}
/** END Date functions **/


/* useful functions, JQuery is preferred but this is here for backwards compatibility */
//http://www.javascriptsearch.com/guides/Advanced/articles/top10JSfunctionsDiaz.html
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); /* CMTBC - fix \s for multiple class handling */
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getElementHeight(elem){
	var elem = document.getElementById(elem);
	if (elem.offsetHeight){
		return elem.offsetHeight;
	} else {
		return elem.style.pixelHeight;
	}
}

function getElementWidth(elem) {
	var elem = document.getElementById(elem);
	if (elem.offsetWidth){
		return elem.offsetWidth;
	} else {
		return elem.style.pixelWidth;
	}
}