// set global variables; they will be null if not yet defined in query string
var g_sType = getSearchVal('type'), g_iRole = parseInt(getSearchVal('role')), g_oRole = g_sType != null && g_iRole != null ? getRoles(g_sType,g_iRole) : false;

// helper functions
function assignment(iUniqueID,sName,sDescription,sURL,sTooltip)
{
	this.uniqueid = iUniqueID;			// make each entry unique to allow items to be added/removed later
	this.displayname = sName;			// display name for the program
	this.description = sDescription;	// Brief description to show underneath the link; inserted using a <br /> tag but basic formatting can be applied using inline HTML tags
	this.URL = sURL;					// URL link for the program
	this.tooltip = sTooltip;			// Text to show as a tool tip; can be formatted using inline HTML tags
}

function role(sUniqueID,sName,sAssigned,sTooltip)
{
	this.uniqueid = sUniqueID;			// make each entry unique to allow items to be added/removed later
	this.displayname = sName;			// display name for the role
	this.assignments = sAssigned;		// commma-delimited list of unique IDs for content assigned to a given role
	this.tooltip = sTooltip;			// Text to show as a tool tip; can be formatted using inline HTML tags
}

function getAssignments()
{
	var i=0, a = new Array();

	// see function assignment() above for parameter descriptions
	// if URL parameter set to false, item is treated as a heading; if it doesn't contain chexweb, it must be a CIS course description reference

	/*** IMPORTANT: If using descriptions, an apostrophe or single quote MUST be preceeded by two backslashes ( \\ )
					or replaced by the HTML equivalent (") or you will see an error such as:

					Error: Expected ')'
	***/

	/*** WBTs ***/

	// define default tooltip for links; can still override for a given entry
	var sDefaultTooltip = "Click to open in a new window<br /><br /><strong>IMPORTANT:</strong> This training requires a CHEX logon and password.<br /><br />If you do not know your CHEX logon and password, contact your educator or the Seattle Children\\'s Service Desk at (206) 987-1111."

	a[i++] = new assignment(1,"Web-Based Training (WBT)",false);

	a[i++] = new assignment(2,"Inpatient CIS Fundamentals WBT","This course is a prerequisite for all Inpatient CIS training.","http://chexweb.mzinga.com/app/servlet/goTo?Page=CourseInfo&DirectLinkID=1637657",sDefaultTooltip);
	a[i++] = new assignment(3,"Inpatient CIS Orders WBT","This course is required for all CIS users who work with patient orders.","http://chexweb.mzinga.com/app/servlet/goTo?Page=CourseInfo&DirectLinkID=1585444",sDefaultTooltip);
	a[i++] = new assignment(4,"EasyScript Fundamentals WBT","This course is required ONLY if you write or work with prescriptions electronically using EasyScript.</p><strong>Note:</strong> If you do not work with patient prescriptions, you can skip this course.","http://chexweb.mzinga.com/app/servlet/goTo?Page=CourseInfo&DirectLinkID=1650918",sDefaultTooltip);
	a[i++] = new assignment(5,"PACS Web Fundamentals WBT","This course is required ONLY if you view patient images online.</p><strong>Note:</strong> If you do not view images electronically, you can skip this course.","http://chexweb.mzinga.com/app/servlet/goTo?Page=CourseInfo&DirectLinkID=1637727",sDefaultTooltip);
	a[i++] = new assignment(6,"Ambulatory CIS Fundamentals WBT (Required)","This course is a prerequisite for all Ambulatory CIS training.","http://chexweb.mzinga.com/app/servlet/goTo?Page=CourseInfo&DirectLinkID=1708324",sDefaultTooltip);
	a[i++] = new assignment(7,"Ambulatory CIS Orders WBT (Required)","Not Yet Available","",sDefaultTooltip);

	// Downtime links
	/*
	a[i++] = new assignment(2,"Inpatient CIS Fundamentals WBT","This course is a prerequisite for all Inpatient CIS training.","http://courses.chexweb.com/hosted/sea/istraining/cis/inpatient_fundamentals/index.htm",sDefaultTooltip);
	a[i++] = new assignment(3,"Inpatient CIS Orders WBT","This course is required for all CIS users who work with patient orders.","http://courses.chexweb.com/hosted/sea/istraining/cis/inpatient_orders/index.htm",sDefaultTooltip);
	a[i++] = new assignment(4,"EasyScript Fundamentals WBT","This course is required ONLY if you write or work with prescriptions electronically using EasyScript.</p><strong>Note:</strong> If you do not work with patient prescriptions, you can skip this course.","http://courses.chexweb.com/hosted/sea/istraining/cis/easyscript/index.htm",sDefaultTooltip);
	a[i++] = new assignment(5,"PACS Web Fundamentals WBT","This course is required ONLY if you view patient images online.</p><strong>Note:</strong> If you do not view images electronically, you can skip this course.","http://courses.chexweb.com/hosted/sea/istraining/cis/pacsweb/index.htm",sDefaultTooltip);
	a[i++] = new assignment(6,"Ambulatory CIS Fundamentals WBT (Required)","This course is a prerequisite for all Ambulatory CIS training.","http://courses.chexweb.com/hosted/sea/istraining/cis/ambulatory_fundamentals/index.htm",sDefaultTooltip);
	a[i++] = new assignment(7,"Ambulatory CIS Orders WBT (Required)","Not Yet Available","",sDefaultTooltip);
	*/

	/*** ILTS ***/

	// start ILT uniqueIDs at 51 to give LOTS of breathing room for new WBT materials
	a[i++] = new assignment(51,"Instructor-Led Classroom Training",false);

	a[i++] = new assignment(52,"Inpatient CIS Instructor-Led Training","Regularly-scheduled training is available for nursing staff and respiratory therapists.","http://wbt.seattlechildrens.org/classes/inpatient.asp","Click to open in a new window");
	a[i++] = new assignment(53,"Inpatient CIS Instructor-Led Training By Request (Optional)","If you feel that you require additional instruction after completing the Web-Based Training, instructor-led training is available on request.","http://wbt.seattlechildrens.org/classes/inpatient.asp#MD","Click to open in a new window");
	
	a[i++] = new assignment(54,"Ambulatory CIS Instructor-Led Training","Regularly-scheduled training is available for most user roles.","http://wbt.seattlechildrens.org/classes/ambulatory.asp","Click to open in a new window");
	a[i++] = new assignment(55,"Ambulatory CIS Instructor-Led Training By Request (Optional)","If you feel that you require additional instruction after completing the Web-Based Training, instructor-led training is available on request.","http://wbt.seattlechildrens.org/classes/ambulatory.asp","Click to open in a new window");

	return a;
}

// map roles to getCourses() data above (must maintain roles list by hand from CIS Training Matrix!)
function getRoles(sType,iRoleNum)
{
	var i=0, a = new Array();

	if (sType == "inpt")	// inpatient first
	{
		a[i++] = new role(101,"Administrative (Business Services,Health Information, Insurance Processing, Transcription)","1,2,3,51,53");
		a[i++] = new role(102,"Audiologist","1,2,3,51,53");
		a[i++] = new role(103,"Care Coordinator","1,2,3,51,53");
		a[i++] = new role(104,"Chaplain","1,2,3,51,53");
		a[i++] = new role(105,"Child Life","1,2,3,51,53");
    	a[i++] = new role(106,"Diet Secretary","1,2,3,51,53");
		a[i++] = new role(107,"Diet Tech","1,2,3,51,53");
		a[i++] = new role(108,"Dietitian","1,2,3,51,53");
		a[i++] = new role(109,"ECG Tech","1,2,3,51,53");
		a[i++] = new role(110,"Echo Tech","1,2,3,51,53");
		a[i++] = new role(111,"EEG Tech","1,2,3,51,53");
		a[i++] = new role(112,"Interpreter Services","1,2,3,51,53");
		a[i++] = new role(113,"Lab Assistant","1,2,3,51,53");
		a[i++] = new role(114,"Lab Tech","1,2,3,51,53");
		a[i++] = new role(115,"LPN","1,2,3,5,51,52");
		a[i++] = new role(116,"Medical Student","1,2,3,5,51,53");
		a[i++] = new role(117,"Medication Intake Coordinator","1,2,3,4,51,53");
		a[i++] = new role(118,"Nurse Assistant","1,2,3,51,52");
		a[i++] = new role(119,"Nurse Tech","1,2,3,51,52");
		a[i++] = new role(120,"Nursing Student","1,2,3,51,52");
		a[i++] = new role(121,"OT/PT","1,2,3,51,53");
		a[i++] = new role(122,"Patient Navigator","1,2,51,53");	// only role without orders for now
		a[i++] = new role(123,"Pharmacist","1,2,3,4,51,53");
		a[i++] = new role(124,"Pharmacy Tech","1,2,3,51,53");
		a[i++] = new role(125,"Provider (MD/NP/PA/Fellow/Resident)","1,2,3,4,5,51,53");
		a[i++] = new role(126,"Radiology Tech","1,2,3,5,51,52");
		a[i++] = new role(127,"Research (Non-Clinical)","1,2,3,51,53");
		a[i++] = new role(128,"Respiratory Therapist","1,2,3,4,5,51,53");
		a[i++] = new role(129,"RN","1,2,3,5,51,52");
		a[i++] = new role(130,"RN - Hemodialysis","1,2,3,5,51,52");
		a[i++] = new role(131,"Security","1,2,3,51,53");
		a[i++] = new role(132,"Social Work","1,2,3,51,53");
		a[i++] = new role(133,"Unit Coordinator","1,2,3,51,53");
		a[i++] = new role(134,"Utilization Review - IPC","1,2,3,51,53");
	}
	else	// ambulatory
	{
		a[i++] = new role(101,"Administrative (Business Services,Health Information, Insurance Processing, Transcription)","1,6,51,55");
		a[i++] = new role(102,"Audiologist","1,6,51,54");
		a[i++] = new role(108,"Dietitian","1,6,51,54");
		a[i++] = new role(110,"ECG Tech","1,6,51,55");
		a[i++] = new role(111,"Echo Tech","1,6,51,55");
		a[i++] = new role(112,"EEG Tech","1,6,51,55");
		a[i++] = new role(201,"FSR","1,6,51,55");
		a[i++] = new role(202,"Genetic Counselor","1,6,51,54");
		a[i++] = new role(113,"Lab Assistant","1,6,51,55");
		a[i++] = new role(114,"Lab Tech","1,6,51,55");
		a[i++] = new role(203,"Medical Assistant","1,6,4,51,54");
		a[i++] = new role(204,"Orthotist","1,6,51,54");
		a[i++] = new role(121,"OT/PT","1,6,51,55");
		a[i++] = new role(125,"Provider (MD/NP/PA/Fellow/Resident)","1,6,4,5,51,54");
		a[i++] = new role(205,"Psychologist","1,6,51,54");
		a[i++] = new role(206,"Pulmonary Function","1,6,51,54");
		a[i++] = new role(126,"Radiology Tech","1,6,5");
		a[i++] = new role(127,"Research (Non-Clinical)","1,6,51,54");
		a[i++] = new role(129,"RN","1,6,4,5,51,54");
		a[i++] = new role(130,"RN - Hemodialysis","1,6,4,5,51,54");
		a[i++] = new role(131,"Security","1,6,51,54");
		a[i++] = new role(132,"Social Work","1,6,51,55");
		a[i++] = new role(207,"Speech Therapy","1,6,51,54");
	}

	return !isNaN(iRoleNum) ? a[parseInt(iRoleNum)] : a;
}

function getObj(o)
{
	 if (typeof(o) == 'string')
	 	o = window.document.getElementById(o);
	return o;	 
}

function getSearchVal(sParam)
{
	var s = window.location.search, sVal = null;
	
	// remove separator char from Query String, if necessary
	if (s.charAt(0) == '?')
		s = s.substring(1,s.length);

	// don't even bother if param is not found in Query String
	if (s.indexOf(sParam) != -1)
	{
		if (s.indexOf('&') == -1) 	// if no "&", only a single value was passed
			sVal = s.split('=')[1];
		else
		{
			var i=0, a = s.split('&');
			while (i < a.length && sVal == null)
			{
				if (a[i].indexOf(sParam) != -1)
					sVal = a[i].split('=')[1];
				i++;
			}
		}
	}

	return sVal;	
}

function goToPage(s,sVars)
{
	window.location.href = s + (sVars ? '?' + sVars : '');
}

function addPageNav(sTag)
{
	// autobuild navigation elements for all specified tags in a page; if no tag specified, use H2
	if (!sTag)
		sTag = 'H2';

	var a = window.document.getElementsByTagName(sTag), a2 = window.document.getElementsByTagName('A'), oUL, oLI, oA, oLinkText;

	// add new unordered list to placeholder div
	oUL = document.createElement("ul");
   	getObj('navmenu').appendChild(oUL);

	// loop through each H2 tag to create menu & insert anchor for each menu item/return to top link
	for (var i=0;i<a.length;i++)
	{
		// add each menu item
		oLI = window.document.createElement("li");
		oUL.appendChild(oLI);

		// add link inside LI tag
		oA = window.document.createElement('<a href="#section'+(i+1) + '"></a>');
		oLI.appendChild(oA);

		oLinkText = window.document.createTextNode(a[i].innerText);
		oA.appendChild(oLinkText);

		// add "back to top" links if more than 4 sections; pagetop anchor already in master template
		if (a.length > 4 && i > 0)
		{
			oA = window.document.createElement('<a class="linktotop" href="#pagetop"></a>');
			a[i].parentElement.insertBefore(oA,a[i]);
			oA.className = "linktotop";

			oLinkText = window.document.createTextNode('Back to top');
			oA.appendChild(oLinkText);
		}

		// add anchor to page just before the H2 tag; supply full HTML structure to avoid IE bug
		oA = window.document.createElement('<a name="#section'+(i+1) + '"></a>');
		a[i].parentElement.insertBefore(oA,a[i]);
	}
}

function showAssignmentsForRole()
{	
	var aAssignmentsByRole = g_oRole.assignments.split(','), aAssignmentData = getAssignments(), i,j,iUID,sOutput = '',s;

	// get each assigned course from role data
	for (i=0;i<aAssignmentsByRole.length;i++)
	{
		j=0;
		s = '';
		while (j < aAssignmentData.length && s == '')
		{
			// check for match
			iUID = parseInt(aAssignmentData[j].uniqueid);
			if (iUID && iUID == parseInt(aAssignmentsByRole[i]))
			{
				// check if heading or data element
				if (!aAssignmentData[j].URL)	//heading
					s = (i > 0 ? '</table><p>&nbsp;</p>' : '') + '<table width="100%" class="coursecontent" border="1" cellpadding="5" cellspacing="0"><tr><th class="moduletitle">' + aAssignmentData[j].displayname + '</th></tr>';
				else	// data element
					s = '<tr><td><a href="' + aAssignmentData[j].URL + '" target="_blank"' + (aAssignmentData[j].tooltip ? ' onmouseover="Tip(\'' + aAssignmentData[j].tooltip + '\')" onmouseout="UnTip()"' : '') + '>' + aAssignmentData[j].displayname + '</a>' + (aAssignmentData[j].description ? '<br /><br />' + aAssignmentData[j].description : '') + '</td></tr>';
			}
			j++;
		}

		sOutput += s;
	}

	window.document.write(sOutput + '</table>');
}

function showRoles(sType)
{
	// get the roles data and split into 3 unordered lists (roles table is 3 columns)
	var aRoles = getRoles(sType), s0=s1=s2 = '';
	var iSplit = Math.ceil(aRoles.length/3), iTemp;	// split into 3 columns for usability; use ceil so last column can be shorter than rest if need

	// build guts of each list, assume it will display as unordered list so add li tags
	for (var i=0;i<iSplit;i++)
	{
		// write first column of roles
		s0 += '<li><a href="3_courses.htm?type=' + sType + '&role=' + i + '">' + aRoles[i].displayname + '</a></li>';

		// write second column of roles, if possible
		iTemp = i+iSplit;
		if (iTemp < aRoles.length)
			s1 += '<li><a href="3_courses.htm?type=' + sType + '&role=' + (i+iSplit) + '">' + aRoles[i+iSplit].displayname + '</a></li>';

		// write third column of roles, if possible
		iTemp = i+(iSplit*2);
		if (iTemp < aRoles.length)
			s2 += '<li><a href="3_courses.htm?type=' + sType + '&role=' + (i+(iSplit*2)) + '">' + aRoles[i+(iSplit*2)].displayname + '</a></li>';
	}

	// build output; create additional columns only if needed
	var s = '<table id="userroles"><tr><td><ul>' + s0;
	if (s1)
		s+= '</ul></td><td><ul>' + s1;
	if (s2)
		s+= '</ul></td><td><ul>' + s2;
	s+= '</ul></td></tr></table>';
	
	window.document.write(s);
}

function showRoleInfo()
{	
	window.document.write('<p><strong>Type:</strong> ' + (g_sType == "amb" ? "Ambulatory" : "Inpatient") + '<br /><strong>Role:</strong> ' + g_oRole.displayname + '</p>');	
}
