//terry(10Dec2007): Add search text to querystring
//terry(19Jun2008): updates for sidebar navigation
//terry(05Nov2008): updates for cms 8.9 upgrade
var qSearchCtrlID = '';
function initQSearch(searchCtrlID)
{
	if (document.forms.item('IronPointForm'))
	{
		//add addQSearch() function to form onsubmit  
		document.forms.item('IronPointForm').onsubmit = addQSearch;
		
		//terry(23Jan2008): 
		//	The onsubmit event handler will not always respond to the document.forms.item('IronPointForm').submit() method
		//	(if javascript is used to call form.submit()). Therefore, I've added submit routing
		//	to execute addQSearch() and originalsubmit when the form is submitted.
		document.forms.item('IronPointForm').originalsubmit = document.forms.item('IronPointForm').submit; 
		document.forms.item('IronPointForm').submit = onsubmitrouter; 
		
		//terry(05Nov2008): need to find the search input textbox
		var formInputs = document.getElementById('IronPointForm').getElementsByTagName("input");
		for (var i = 0; i < formInputs.length; i++)
		{
			if (formInputs.item(i).id.indexOf(searchCtrlID) > -1 )
				qSearchCtrlID = formInputs.item(i).id;
		}
	}
	
	/**** terry(19Jun2008): side navigation rebuild ****/
	
	//check for unordered list menu created by cms
	if (document.getElementById('sectionMenuElementID_0'))
	{
		//replace side navigation elements
		setMainNav('nav_mayor.gif', 'pageid396');				//mayor
		setMainNav('nav_onlineservices.gif', 'pageid393');		//online services
		setMainNav('nav_servicedir.gif', 'pageid394');			//service directory
		setMainNav('nav_contacts.gif', 'pageid395');			//contacts
		setMainNav('nav_atabby.gif', 'pageid348');				//@ abbotsford
		setMainNav('nav_whatsnew.gif', 'pageid397');			//what's new
		
		/*** code below references ip-ulaccordion.js (cms code) ***/
		
		var anchortags = document.getElementById('sectionMenuElementID_0').getElementsByTagName("a");
		var anchorClicked = null;
		var href = document.location.href;
		
		//cms might generate page by PageFactory.aspx and PageID querystring variable
		var pageid = getQueryVariable('PageID');
		if (pageid)
		{
			//reset href to match the sidebar nav anchor.href
			var intIndex = document.location.href.indexOf('PageFactory');
			href = document.location.href.substring(0,intIndex) + 'Page' + pageid + '.aspx';
		}
		
		//find anchor tag in sidebar nav
		for (var i=0; i<anchortags.length; i++)
		{
			if (anchortags[i].href.toLowerCase() == href.toLowerCase())
				anchorClicked = anchortags[i];
			if (anchorClicked) break;
		}

		if (anchorClicked)
		{
			//set ipf-active class on nodes
			setActiveNodes(anchorClicked.parentNode);
		
			var arrActiveLI = getElementsByClass("ipf-active", document.getElementById('sectionMenuElementID_0'), 'li');
		
			//get all ul tags from top-level li that has class ipf-active
			var arrUL = arrActiveLI[0].getElementsByTagName("ul");
		
			//set rel attribute on all active ul tags
			for (var i=0; i<arrUL.length; i++)
			{
				var strClass = arrUL[i].parentNode.className;
				if (strClass.indexOf("ipf-active") > -1)
					arrUL[i].setAttribute("rel", "open");
			}
		}
		
		//call ip-ulaccordion.js createTree for nav construction
		ddtreemenu.createTree("sectionMenuElementID_0", true);
	}
}

//terry(23Jan2008)
function onsubmitrouter(){ this.onsubmit();this.originalsubmit() } 

function addQSearch() 
{ 
	if (document.getElementById(qSearchCtrlID))
	{
		//addQSearch() is called when the form is submitted.
		
		//get current querystring
		var strAction = document.forms.item('IronPointForm').action;
		
		//terry(12Mar2009): check if form submission is a search request
		//	addQSearch() may be called on any postback (e.g. event calendar page)
		//	and might blow up the page by adding the qSearch parameter.
		//	Therefore, if the default "Search" in the search box is listed,
		//	we assume this is not a search and ignore the postback. 
		if (document.getElementById(qSearchCtrlID).value != 'Search')
		{
			//check if qSearch is already in the querystring
			//Note: qSearch is assumed to be the last parameter
			var intIndex = strAction.indexOf('&qSearch');
			if (intIndex > -1)
				strAction = strAction.substring(0,intIndex);
			
			//get search text and trim
			var strSearch = document.getElementById(qSearchCtrlID).value.replace(/^\s*/, "").replace(/\s*$/, "");;
			
			//replace whitespace with +
			strSearch = strSearch.replace(/\s+/g,'+');
			
			if (strSearch != '')
			{
				//create search parameter to add to url
				strAction = strAction + '&qSearch=' + strSearch;
			}
			
			//reset querystring
			document.forms.item('IronPointForm').action = strAction;
		}
	}
}

//terry(19Jun2008)
function setMainNav(imgNav, pageId)
{
	var elMain = document.getElementById(pageId);
	var elNav = document.createElement('img');
	elNav.setAttribute('src','/Sites/4/templates/images/' + imgNav);
	elNav.setAttribute('border','0');
	//elNav.setAttribute('width','155');
	elMain.childNodes[0].childNodes[0].insertBefore(elNav, elMain.childNodes[0].childNodes[0].childNodes[0]);
	elMain.childNodes[0].childNodes[0].removeChild(elMain.childNodes[0].childNodes[0].childNodes[1]);
}

//terry(19Jun2008): getElementsByClass by Dustin Diaz
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|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//terry(19Jun2008)
function setActiveNodes(nodeEl)
{
	if (nodeEl.id.length > 0)
	{
		//alert(nodeEl.id);
		var space = "";
		if (nodeEl.className.length > 0) space = " ";
		nodeEl.className += space + "ipf-active";
		setActiveNodes(nodeEl.parentNode.parentNode);
	}
	else
	{
		if (nodeEl.parentNode.parentNode.id == "sectionMenuElementID_0")
			nodeEl.parentNode.className += " ipf-active";
	}
}

//terry(19Jun2008)
function getQueryVariable(variable) 
{  
	var query = document.location.search.substring(1);  
	var vars = query.split("&");  
	for (var i=0;i<vars.length;i++) 
	{    
		var pair = vars[i].split("=");    
		if (pair[0].toLowerCase() == variable.toLowerCase())
			return pair[1];
	} 
}