// Allows toggling of items

function changeClassP (lnk, stateDefault, stateChanged) {
	var parentLnk = lnk.parentNode;
	parentLnk.className = (parentLnk.className == stateChanged) ? stateDefault : stateChanged;
}
// sample link javascript:changeClass(this, 'collapsed', 'expanded');	

function changeClassSelf (targetID, stateDefault, stateChanged) {
	var tarID = targetID;
	tarID.className = (tarID.className == stateChanged) ? stateDefault : stateChanged;	
}
function toggle(id){ 
    tarID = document.getElementById(id);
    tarID.style.display = (tarID.style.display == 'block') ? 'none' : 'block';
}
//make one div appear/disappear - change class of the link
function swap(tarLnk, idHide, idShow){
	tarIDHide = document.getElementById(idHide);
	tarIDShow = document.getElementById(idShow);
	tarIDHide.style.display = (tarIDHide.style.display == 'none') ? 'block' : 'none';
	tarIDShow.style.display = (tarIDShow.style.display == 'block') ? 'none' : 'block';
	tarLnk.className = (tarLnk.className == 'ih-expLrgColl') ? 'ih-expLrgExp' : 'ih-expLrgColl';
}

//expanding link - makes the current link expand and show another div

function toggleLnk(tarLnk, id){
	tarID = document.getElementById(id);
    //tarID.style.display = (tarID.style.display == 'block') ? 'none' : 'block';
    tarID.className = (tarID.className == 'ih-show') ? 'ih-hidden' : 'ih-show';
	tarLnk.className = (tarLnk.className == 'ih-expandColl') ? 'ih-expandLnk' : 'ih-expandColl';
}
function show(id){
	showID = document.getElementById(id);
	showID.style.display = "block";
}

function hide(id){
	hideID = document.getElementById(id);
	hideID.style.display = "none";	
}

function srchGO(searchType) {
    var newLocation = "";
    if (searchType=="symptom")
    {
    	newLocation = "SearchResults.aspx?s=Symptoms&k=";
		newLocation +=document.getElementById('ih-symptomSrchInput').value;
    } else if (searchType=="main") {
    	if ($('#ih-srchInput').val()=='Search Intermountain' || $('#ih-srchInput').val()=='')
	  	{
	  		newLocation = "/SearchIntermountain/Pages/SearchResults.aspx";
	  	}
		else
		{
			newLocation = "/SearchIntermountain/Pages/SearchResults.aspx?k=";
			newLocation +=document.getElementById('ih-srchInput').value;
			newLocation +="&s=Intermountain,Classes,Facilities,Providers,SelectHealth,KidsHealth"
		}
	} else if (searchType=="results") {
		newLocation = "/SearchIntermountain/Pages/SearchResults.aspx?k=";
		newLocation +=document.getElementById('ih-resultsInput').value;
		newLocation+="&s=";
		if (document.getElementById('SearchIntermountain').checked)
		{
			 newLocation+="Intermountain";
		}
		if (document.getElementById('SearchClasses').checked)
		{
			if (newLocation.substring(newLocation.length-3,newLocation.length)!='&s='){newLocation+=','}
			newLocation+="Classes";
		}
		if (document.getElementById('SearchFacilities').checked)
		{
			if (newLocation.substring(newLocation.length-3,newLocation.length)!='&s='){newLocation+=','}
			newLocation+="Facilities";
		}
		if (document.getElementById('SearchProviders').checked)
		{
			if (newLocation.substring(newLocation.length-3,newLocation.length)!='&s='){newLocation+=','}
		 	newLocation+="Providers";
		}
		if (document.getElementById('SelectHealth').checked)
		{
			if (newLocation.substring(newLocation.length-3,newLocation.length)!='&s='){newLocation+=','}
		 	newLocation+="SelectHealth";
		}
		if (document.getElementById('SearchKidsHealth').checked)
		{
			if (newLocation.substring(newLocation.length-3,newLocation.length)!='&s='){newLocation+=','}
		 	newLocation+="KidsHealth";
		}

	} else if (searchType=="healthresource") {
		if (document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00'))
		{
			document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00').onclick= function() {return false;};
		}
		newLocation = "SearchResults.aspx?s=HealthResources&k=";
		newLocation +=document.getElementById('ih-hrSrchInput').value;
	} else if (searchType=="PCMChealthresource") {
		if (document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00'))
		{
			document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00').onclick= function() {return false;};
		}
		newLocation = "SearchResults.aspx?s=PCMCHealthResources&k=";
		newLocation +=document.getElementById('ih-hrSrchInput').value;
	} else if (searchType=="classes") {
		if (document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00'))
		{
			document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00').onclick= function() {return false;};
		}
		newLocation = "SearchResults.aspx?s=Classes&k=";
		newLocation +=document.getElementById('ih-hrSrchInput').value;
	} else if (searchType=="event") {
		if (document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00'))
		{
			document.getElementById('ctl00$PlaceHolderMain$HealthTopicBrowser1$ctl00').onclick= function() {return false;};
		}
		newLocation = "/SearchIntermountain/Pages/SearchResults.aspx?s=Classes&k=";
		newLocation +=document.getElementById('ih-hrSrchInput').value;
	}
	else if (searchType=='form') {
		if (document.activeElement.id=="ih-symptomSrchInput" || document.activeElement.id=="ih-srchInput" || document.activeElement.id=="ih-resultsInput" || document.activeElement.id=="ih-hrSrchInput")
	 	{
	 		return false;
	 	} else {
	 		return _spFormOnSubmitWrapper();
	 	}
	}
	window.location.replace(newLocation);
}
//Search Toggle

function srchToggle() {
	//Locates buttons
	togID_1 = document.getElementById('ih-tog1');
	togID_2 = document.getElementById('ih-tog2');
	//Identifies Text of Link
	togVal_1 = togID_1.innerHTML;
	togVal_2 = togID_2.innerHTML;
	//Locate Search Components
	searchClass = document.getElementById('ih-btnSrchSwitch');
	searchInput = document.getElementById('ih-srchInput');
	//Change content
	togID_1.onclick=srchP1;
	togID_2.onclick=srchP2;
	//Change functions
	function srchP1(){
		searchClass.className = 'ih-btnDdSrch';
		searchInput.value = togVal_1;
		searchInput.select();
	}
	function srchP2(){
		searchClass.className = 'ih-hos-btnDdSrch';
		searchInput.value = togVal_2;
		searchInput.select();
	}
}


function GlobalOnLoad()
{
	srchToggle();
	//var webPartHTML = $("#ctl00_PlaceHolderMain_WebPartZoneRight").html();
	//if (webPartHTML!=null && webPartHTML.replace(/^\s+|\s+$/g,"")!='')
	//{
		//$("#ctl00_PlaceHolderMain_WebPartZoneRight").toggle();
	//	$('#ctl00_PlaceHolderMain_ihcColumn').addClass('ih-colHasFeat');
	//}
	//var webPartHTML2 = $("#WebPartZoneRight").html();
	//if (webPartHTML2!=null && webPartHTML2.replace(/^\s+|\s+$/g,"")!='')
	//{
	//	$("#WebPartZoneRight").toggle();
	//	$('#ih-cColumn').addClass('ih-colHasFeat');
	//}

	$('#ih-srchInput').focus(function() {
	  if ($('#ih-srchInput').val()=='Search Intermountain')
	  	{
	  		$('#ih-srchInput').select();
	  	}
	});	
	//fix http/https links for /formscore
	$('.ih-pgContWrap a').each(function(index) {
		var linkHREF = $(this).attr('href');
		if (linkHREF!=null)
		{
	    	if (linkHREF.indexOf('/formscore')!=-1)
	    	{
	    		linkHREF = linkHREF.substring(linkHREF.indexOf('/formscore'));
	    		$(this).attr('href', 'https://intermountainhealthcare.org' + linkHREF)
	    	}
	    	if (linkHREF.indexOf('/rms2')!=-1)
	    	{
	    		linkHREF = linkHREF.substring(linkHREF.indexOf('/rms2'));
	    		$(this).attr('href', 'https://intermountainhealthcare.org' + linkHREF)
	    	}
    	}
  	});
};

// opens external windows - best
function popUp(strURL,strType,strHeight,strWidth) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	if (strType=="symptom") strOptions='toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=1,resizable=1,width=640,height=473,top=25,left=50';
	window.open(strURL, 'newWin', strOptions);
}

//link example: <a href="URL" onclick="popUp(this.href,'elastic',600,750); return false;" target="newWin">This is my link</a>

//Email this page
	function mailpage()
	{
	mail_str = "mailto:?subject=Please read: " + document.title;
	mail_str += "&body=I thought you might be interested in the " + document.title;
	mail_str += ". You can view it at, " + location.href; 
	location.href = mail_str;
	} 


//Cookie Creator

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=/";
}

//Cookie Reader
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;
}

//font-changer
function fontSize(setSize) {
	var fontSmall = document.getElementById('ih-fontSmall');
	var fontMed = document.getElementById('ih-fontMed');
	var fontLrg = document.getElementById('ih-fontLrg');
	var fontApply = document.getElementById('ih-pgContent');
	if (setSize == 'small') {
			fontSmall.className='ih-currentFont';
			fontMed.removeClass();
			fontLrg.removeClass();
			fontApply.className='ih-copySmall';
		}	else if (setSize == 'large') {
			fontSmall.removeClass();
			fontMed.removeClass();
			fontLrg.className='ih-currentFont';
			fontApply.className='ih-copyLrg';
		} else {
			fontSmall.removeClass();
			fontMed.className='ih-currentFont';
			fontLrg.removeClass();
			fontApply.className='ih-copyMed';
		}
}

function changeFontSize(setSize) {
		if (setSize) {
			// define CSS
			fontSize(setSize);
			createCookie("textSize", setSize, 4641);
		}
	}

function newFavorite(){
	var pgURL = window.location.href;
	var pgTitle = document.title; 
	var noSupport = "Press Ctrl+D to bookmark this page";
	
	if (pgTitle=='') pgTitle=pgURL;
	if (document.all)
		window.external.AddFavorite(pgURL,pgTitle);
	else alert(noSupport);
}