// JavaScript Document - Expand or collapse items based on their CSS class setting
/*Jamie Zabel - Website Coordinator
PrimeWest Health
December 2009 */

function navLoad(){
/*event fires off when page loads*/


/*Expand Navigation*/	
	//get item to expand from cookie and assign to navExpand
	var expandCookieName= readCookie('pwhExpandCookie');
	var navExpand = document.getElementById(expandCookieName +"Subs");
	
	//expand selected item
	if (navExpand) {	
		navExpand.className="show";
	}

/*Highlight Proper item */
	navHighlight();
}


function expandCollapse (navLevel) {
/*event fires when a category with sublevels is clicked, it either collapses or expands and highlights the clicked category*/

	//hide any expanded nav
		hideNav();

	//Now show the new nav level
		var subnavLevel = navLevel + "Subs" //Subs is added so that it expands the sub level and not the item sent
		var selectedNavLevel = document.getElementById(subnavLevel); 
		

		if (selectedNavLevel.className=="hide") {
			selectedNavLevel.className="show";
			//Set expandCookie 
			createCookie('pwhExpandCookie', navLevel ,0);
		} /*else {
			selectedNavLevel.className="hide";
			//erase the expandCookie
			eraseCookie('pwhExpandCookie');
		}*/
		
		
		
//Create Highlight Cookie because Nav Changed 
	navChange(navLevel, 'Group');
	//Highlight Selected Section	
}

function navChange(navItemToHighlight, navType){
/*event fires off when a category with no subcategory or a subcategory is clicked to highlight the click category*/
	
	//Clear existing highlight
	var unhighlight = readCookie('pwhHighlightCookie');
	var unhighlightType = readCookie('pwhHighlightTypeCookie');  //Current CSS class level

	if (unhighlightType !== null){
		 // place field to highlight into navItem var
		var navItem = document.getElementById(unhighlight);
		//change CSS code to clear highlight
			navItem.className=unhighlightType;	
	}
	
	//Set highlightCookie
	createCookie('pwhHighlightCookie', navItemToHighlight ,0);
	createCookie('pwhHighlightTypeCookie', navType ,0);	//Current CSS class level

	
	navHighlight();
	
	if(navType=="NonGroup"){
		hideNav();
	}
	//new page will then load
}

function tabChange(){
	//clear cookies because tab changed
		eraseCookie('pwhExpandCookie');
		eraseCookie('pwhHighlightCookie');
		eraseCookie('pwhHighlightTypeCookie');
}

//highlights from cookie
function navHighlight() {
/* highlights categories that are saved in the cookies this is called from page load, expand/collapse, & navChange*/
	//get cookie information
	var highlightCookie = readCookie('pwhHighlightCookie');
	var highlightTypeCookie = readCookie('pwhHighlightTypeCookie');  //Current CSS class level

	if (highlightCookie !== null ){
		 // place field to highlight into navItem var
		var navItem = document.getElementById(highlightCookie);
		//change CSS code to highlight
		if(navItem != null){ //check if it's not null highlight the class 
			 navItem.className= highlightTypeCookie + " SelectedHighlight";	 }
		else { //it's null, clear the cookie

			eraseCookie('pwhExpandCookie');
			eraseCookie('pwhHighlightCookie');
			eraseCookie('pwhHighlightTypeCookie');
		}
	}
}

function hideNav(){
	//check to see if there currently is an expanded nav item and collapse it
		var expandedCookieName= readCookie('pwhExpandCookie');
		var navExpanded = document.getElementById(expandedCookieName +"Subs");
		if (navExpanded) {	
			navExpanded.className="hide";
			eraseCookie('pwhExpandCookie');
		}
}

/*code adapted from http://www.quirksmode.org/js/cookies.html*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

function printerFriendly(){
	/*var blnSheetNotChanged = true;*/
	var urlWQueryString = self.location +'?PF=True'
	wndwPrintFriendly = window.open(''+urlWQueryString, '', 'location=0, toolbar=1, scrollbars=1');
		
	/*for(x=1;x<5;x++){
		wndwPrintFriendly.document.getElementById('cssScreen').href = '/pwhstyle_print.css'; 
	}*/
		return;
	
}

function expandCollapseList(contentID){
	//Change item to expand collapse and return the arrow type
		var listItem = document.getElementById(contentID);
		if (listItem && listItem.className=="expandedContent" ) {	
			listItem.className="collapsedContent";
			return "collapseArrow";
		}
		else if (listItem && listItem.className=="collapsedContent" ) {	
			listItem.className="expandedContent";
			return "expandArrow";
		}
		else if (listItem && listItem.className=="expandedContentLvl2" ) {	
			listItem.className="collapsedContentLvl2";
			return "collapseArrowLvl2";
		}
		else if (listItem && listItem.className=="collapsedContentLvl2" ) {	
			listItem.className="expandedContentLvl2";
			return "expandArrowLvl2";
		}
}
function mouseoverUnderline(classItem){
	//var underlineItem = document.getElementById(classItem);
	var currentClass = 	classItem.className;
	classItem.className= currentClass + " linkUnderline";
}
function mouseoutUnderline(classItem){
	//var underlineItem = document.getElementById(classItem);
	var currentClass = 	classItem.className;
	var newClass = currentClass.split(" ");
	classItem.className= newClass[0];
}

function searchWebsite()
{ //placed on web search box to call the search page and pass the criteria

    var criteria = document.getElementById('txtSearch').value;
    if (criteria == "") {
        //txtSearch is blank check to see if they are on the Search page and use txtSearchCriteria
        criteria = document.getElementById('txtSearchCriteria').value;
    }
var theSearchPage= '/Search/Search.aspx?criteria=' + criteria;
window.location.assign(theSearchPage);


}
