//set search box to nothing on first focus.
var sValue;
var custom_labels = '';

function replaceCustomLabelItem(param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");

    //check if the item already exists in the custom label
    if (custom_labels.match(re))
	//replace the item
        return custom_labels.replace(re,'$1' + param + "=" + value + '$2');
    else
	//add the item
        return custom_labels + '&' + param + "=" + value;
}

function getQuerystring(key, default_)
{
  //if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function updateCustomLabelFromQueryString() {
	//get the 'ns_mchannel' value from the querystring if it exists
	var ns_mchannel_value = getQuerystring('ns_mchannel',null);
	if (ns_mchannel_value != null) {
		//replace the value in the custom label (or append if it does not exist)
		custom_labels = replaceCustomLabelItem('ns_mchannel',ns_mchannel_value);
	}
	
	
	//get the 'ns_source' value from the querystring if it exists
	var ns_source_value = getQuerystring('ns_source',null);
	if (ns_source_value != null) {
		//replace the value in the custom label (or append if it does not exist)
		custom_labels = replaceCustomLabelItem('ns_source',ns_source_value);
	}
	
	//get the 'ns_campaign' value from the querystring if it exists
	var ns_campaign_value = getQuerystring('ns_campaign',null);
	if (ns_campaign_value != null) {
		//replace the value in the custom label (or append if it does not exist)
		custom_labels = replaceCustomLabelItem('ns_campaign',ns_campaign_value);
	}

	//get the 'ns_linkname' value from the querystring if it exists
	var ns_linkname_value = getQuerystring('ns_linkname',null);
	if (ns_linkname_value != null) {
		//replace the value in the custom label (or append if it does not exist)
		custom_labels = replaceCustomLabelItem('ns_linkname',ns_linkname_value);
	}

	//get the 'ns_fee' value from the querystring if it exists
	var ns_fee_value = getQuerystring('ns_fee',null);
	if (ns_fee_value != null) {
		//replace the value in the custom label (or append if it does not exist)
		custom_labels = replaceCustomLabelItem('ns_fee',ns_fee_value);
	}

}

function getSearchTrackingData() {
	//get the 'keyword' value from the querystring if it exists
	var keyword_value = getQuerystring('keyword',null);
	if (keyword_value != null) {
		//find the element called 'SearchResultTotal'
		var e = document.getElementById('SearchResultTotal');
		if (e != null)
			return '&ns_search_term=' + keyword_value + '&ns_search_result=' + e.innerHTML;
                                else
			return '&ns_search_term=' + keyword_value + '&ns_search_result=0';
	} else {
                    return '';
                }
}

function clearSearch(sValue){
	if(!sValue._haschanged){
  	sValue.value=''
	};
	sValue._haschanged=true;
}

//bookmark page
function addToFavorites() {
	var thisTitle = document.title; // get title of the page
	var thisURL = document.URL; // get url of the page
	
	var txt = "Please bookmark this site"; 
	if (window.sidebar) { 
	  // Mozilla Firefox Bookmark		
	  window.sidebar.addPanel(thisTitle, thisURL,"");	
	} 
	else if (window.external) { 
	  	// IE Favorite		
	  	window.external.AddFavorite(thisURL, thisTitle); 
	}	
	
	else if (window.opera && window.print) { 
	  	// Opera Hotlist		
		txt += " using (Ctrl+T)"; 
		window.alert(txt);
	} 
	else
	{ 
		// if its netscape then pop up a alert message for the time being
		txt += " using (Ctrl+D)"; 
		window.alert(txt);
	} 
}

//output centre details show / hide button 
function drawCentreShowHideButton(showLabel, hideLabel){
	document.write('<div class="showHideCourses" id="showCourseLink">');
	document.write("<a href=\"javascript:hideDiv('showCourseLink');showDiv('CentreAvailCourses');showDiv('hideCourseLink');\"><span>" + showLabel + "</span></a>");
	document.write('</div>');
	document.write('<div class="showHideCourses" id="hideCourseLink">');
	document.write("<a href=\"javascript:hideDiv('CentreAvailCourses');hideDiv('hideCourseLink');showDiv('showCourseLink');\"><span>" + hideLabel + "</span></a>");
	document.write('</div>');
  document.write('<script type="text/javascript">');
  document.write("hideDiv('hideCourseLink');");
  document.write('</script>');
}

//open external links in new window
function externalLinks() {
	AddCss("jsactive");
	PrepareAllLinks("contentWrapper");
}


function AddCss(fileName){
	var l=CreateEl("link");
	l.setAttribute("type","text/css");
	l.setAttribute("rel","stylesheet");
	l.setAttribute("href","/includes/styles/"+fileName+".css");
	l.setAttribute("media","screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}

function CreateEl(t,c){
	var x=document.createElement(t);
	x.className=c;
	x.style.display="block";
	return(x);
}

function PrepareAllLinks(alink) {
  var links, i;
  if (document.getElementById(alink)) {
    links=document.getElementById(alink).getElementsByTagName("a");
  	for(i=0;i<links.length;i++) Prepare(links[i]);
  }
}

function Prepare(alink){
	var t;
  	t=alink.className;
  	if (t == "extLink" ) {
	  alink.setAttribute("target","_blank");
	}
}

window.onload = externalLinks;

function hideDiv(divID){
  if (document.getElementById(divID)) document.getElementById(divID).style.display = "none";
}

function showDiv(divID){
  if (document.getElementById(divID)) document.getElementById(divID).style.display = "block";
}
