/*=====================================*\
| The following adds three new methods	|
| to the basic JavaScrip "String" class.|
\*=====================================*/
String.prototype.trimL = function() {return this.replace( /^\s+/g, "" );}
String.prototype.trimR = function() {return this.replace( /\s+$/g, "" );}
String.prototype.trim  = function() {tmp=this.trimR();return tmp.trimL();}

/*=========================================================*\
|
| In order to use this, the page must perform the following:
| 1) The page sets up the areas of the screen to be used
| 2) Then the page creates an instance of the Controller
| 3) Then it repeatedly calls CreateItem()
| 4) And finally it calls Init()
|
| It should look something like this:
|
|(1)
|	<div id="myDetailArea"></div>
|
|	<script>
|(2)	var myController = new SCreenController("myDetailArea");
|(3)	myController.CreateItem(...);
|	myController.CreateItem(...);
|	myController.CreateItem(...);
|(4)	myController.Init();
|	</script>
|
| From then on the page doesn't have to do anything at all
|
\*=========================================================*/

function screenController(   DetailAreaName){

this.detailAreaName	= DetailAreaName;
this.Specialties	= new Array();	//.specialityName+Sort
this.bySpecialtiesInfo	= new Array();	//.specialityName+sort .docName+Sort .contentDesc .inNewWindow .url .signInRequired .exitPage
this.byTitlesInfo	= new Array();	//                     .docName+Sort .contentDesc .inNewWindow .url .signInRequired .exitPage
this.infoToDisplay	= null;
//this.alphabet		= new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
this.filterType		= 1;		//1="All Journals & Newsletters" 2="Full-Text Journals"
this.selectedGroup	= "";

this.debugging		= false;
//this.debugging		= true;
this.debugWindow	= null;


//================================================================
this.CreateItem = function(Specialty, Name, ContentDesc, ShowInNewWindow, SignInRequired, Url, ExitPage, HTMLid)
//================================================================
	{
	var objRaw		= new Object;
	objRaw.Name		= Name;
	objRaw.ContentDesc	= ContentDesc;
	objRaw.ShowInNewWindow	= ShowInNewWindow;
	objRaw.SignInRequired	= SignInRequired;
	objRaw.url		= Url;
	objRaw.exitPage		= ExitPage;
	objRaw.HTMLid		= HTMLid;

	objRaw.docName		= Name;
	this.composeUrl(objRaw);
	objRaw.Specialties	= new Array;
	objRaw.Specialties[0]	= Specialty;

	var objRaw		= new Object;
	objRaw.Name		= Name;
	objRaw.ContentDesc	= ContentDesc;
	objRaw.ShowInNewWindow	= ShowInNewWindow;
	objRaw.SignInRequired	= SignInRequired;
	objRaw.url		= Url;
	objRaw.exitPage		= ExitPage;
	objRaw.HTMLid		= HTMLid;

	objRaw.docName		= Name;
	this.composeUrl(objRaw);
	objRaw.Specialties	= new Array;
	objRaw.Specialties[0]	= Specialty;
	Specialty = Specialty.replace(/&amp;/g,"&");
	var SpecialtyUpper = Specialty.toUpperCase();
	var NameUpper = Name.toUpperCase();

	//----------------------------------
	//always add this to SpecialtiesInfo
	//----------------------------------
	var objSpl			= new Object;
	objSpl.specialtyName		= Specialty;
	objSpl.specialtyNameSort	= SpecialtyUpper;
	objSpl.docName			= Name;
	objSpl.docNameSort		= NameUpper;
	objSpl.contentDesc		= ContentDesc;
	objSpl.inNewWindow		= (ShowInNewWindow == "true");
	objSpl.url			= Url;
	objSpl.signInRequired		= (SignInRequired == "true");
	objSpl.exitPage			= ExitPage;
	objSpl.HTMLid			= HTMLid;
	this.composeUrl(objSpl);

	this.bySpecialtiesInfo[this.bySpecialtiesInfo.length] = objSpl;

	//---------------------------------------------------------
	//add this Specialty to Specialties if it not already there
	//---------------------------------------------------------
	var found = false;
	for(var x = 0; x < this.Specialties.length; x++)
		if (SpecialtyUpper == this.Specialties[x].specialtyNameSort)
			{
			found = true;
			break;
			}

	//if it's not there, add it to Specialties
	if (found == false)
		{
		var objSpl			= new Object;
		objSpl.specialtyName		= Specialty;
		objSpl.specialtyNameSort	= SpecialtyUpper;

		this.Specialties[this.Specialties.length] = objSpl;
		}

	//----------------------------------------
	//add to byTitlesInfo if not already there
	//----------------------------------------
	var found = false;
	for(var x = 0; x < this.byTitlesInfo.length; x++)
		{
		if (NameUpper == this.byTitlesInfo[x].docNameSort)
			{
			found = true;
			break;
			}
		}

	//if it's not there, add it to byTitlesInfo
	if (found == false)
		{
		var objDoc			= new Object;
		objDoc.specialtyName		=
		objDoc.specialtyNameSort	= NameUpper.substring(0,1);
		objDoc.docName			= Name;
		objDoc.docNameSort		= NameUpper;
		objDoc.contentDesc		= ContentDesc;
		objDoc.inNewWindow		= (ShowInNewWindow == "true");
		objDoc.url			= Url;
		objDoc.signInRequired		= (SignInRequired == "true");
		objDoc.exitPage			= ExitPage;
		objDoc.HTMLid			= HTMLid;
		this.composeUrl(objDoc);

		this.byTitlesInfo[this.byTitlesInfo.length] = objDoc;
		}
	}


//================================================================
this.composeUrl = function(obj)
//================================================================
	{
	if (obj.exitPage != "" && obj.url.indexOf(obj.exitPage) == -1)
		obj.url = obj.exitPage + "?url=" + obj.url + "&name=" + this.encode(obj.docName);
	var tag = document.getElementById(obj.HTMLid);
	if (tag)
		{
		if (!document.all)
			{
			var attr = tag.attributes.getNamedItem("sdcTags");
			if (attr) tag["sdcTags"] = attr.nodeValue;
			}
		obj.sdcTags = tag.sdcTags;
		}
	}

//================================================================
this.Init = function()
//================================================================
	{
	this.debug("-----------------------[ before sorting ]--------------------------------");
	this.dumpData();

	//-----------------------------------------------------------
	// Before we sort the alphabetic list, let's make a few more.
	// (We waited until all the creats and overrides are done.)
	//-----------------------------------------------------------
	if (this.debugging) this.debug("scanning bySpecialtiesInfo for name changes");
	for (var x = 0; x < this.bySpecialtiesInfo.length; x++)
	{
		var oldItem = this.bySpecialtiesInfo[x];
		if (oldItem.docNameSort.substr(0,4) == "THE ")
		{
			var oldName		= oldItem.docNameSort;
//By commenting out the line below, the sort remains a simple straight alphabetical sort.
//Removing the comment changes the sort into an ignore-leading-The special sort.
//			oldItem.docNameSort	= oldItem.docNameSort.substr(4);
			if (this.debugging) this.debug("changed [" + oldName + "] to [" + oldItem.docNameSort + "]");
		}
	}


	//--------------------------
	//sort DocumentsInfo by name
	//--------------------------
	var DocumentCollator = function(item1, item2)
		{
//		if (item1.specialtyNameSort < item2.specialtyNameSort) return (-1);
//		if (item1.specialtyNameSort > item2.specialtyNameSort) return ( 1);
		if (item1.docNameSort       < item2.docNameSort      ) return (-1);
		if (item1.docNameSort       > item2.docNameSort      ) return ( 1);
		return(0);
		}
	this.bySpecialtiesInfo.sort(DocumentCollator);

	this.debug("-----------------------[ after sorting ]--------------------------------");
	this.dumpData();

	this.GenerateScreen();
	}

//================================================================
this.GenerateScreen = function()
//================================================================
	{
	this.GetInfoToDisplay();
	this.GenerateDetail();

	}

//================================================================
this.GetInfoToDisplay = function()
//================================================================
	{ if (this.filterType == 1)	//show the entire list
	  {
	    this.infoToDisplay = this.bySpecialtiesInfo;
	  }
	  if (this.filterType == 2)	//or only show the full-text items
	  {
		this.infoToDisplay = new Array();
		for(var x = 0; x < this.bySpecialtiesInfo.length; x++)
		  if (this.bySpecialtiesInfo[x].contentDesc == "Full-Text")
			this.infoToDisplay[this.infoToDisplay.length] = this.bySpecialtiesInfo[x];
	  }
      }




//================================================================
this.GenerateDetail = function()
//================================================================
	{
	var strHTML = "<div id="+ this.detailAreaName;
        strHTML += "<div><ul class='ListOfLinks'>";
	var prevspecialtyName = "";

	for (var x = 0; x < this.infoToDisplay.length; x++)
		{
		var detail = this.infoToDisplay[x];	//.specialtyName .docName .contentDesc .inNewWindow .url
		if (this.selectedGroup == "" || this.selectedGroup == detail.specialtyName)
		{	var strClassInfo = (detail.signInRequired == true && showLocks == true)?" class=\"locked\"":"";
			var strTargetInfo = (detail.inNewWindow == true)?" target=\"_blank\"":"";
			var strJournalName = this.deriveJournalName(detail.docName);
			var strDescription = this.deriveJournalDescription(detail.docName);
			var strTrailerInfo = (detail.contentDesc == "")?"":"<span class=\"trailer\">(" + detail.contentDesc + ")</span>";
			var strTags = (detail.sdcTags)?(" sdcTags=\"" + detail.sdcTags + "\""):"";

                        strHTML += "<li" + strClassInfo + "><a href=\"" + detail.url + "\"" + strTargetInfo + strTags + ">" + strJournalName + "</a>" + strTrailerInfo + "</li>";
			if (strDescription != "")
				strHTML += "<div class='trailer' >" + strDescription + "</div>";
			}
		}

	strHTML += "</ul></div>";
       // alert(strHTML);
	var objTag = document.getElementById(this.detailAreaName);
	objTag.innerHTML = strHTML;
	}

//================================================================
// The following data definitions and the two routines
// "deriveJournalName" and "deriveJournalDescription" are only
// temporary.  They were added for R31 and the plan is that they
// will be removed for R32 when the derivations will no longer be
// necessary and the descriptions will come from the database.
//================================================================
	this.PubMedJournals = new Array
		(
		"Annals of Internal Medicine&nbsp;",
		"JAMA - Journal of American Medical Association",
		"The Lancet",
		"The New England Journal of Medicine",
		"Cancer"
		);
	this.nameTrailer	= " - via MD Consult - MerckMedicus Edition";
	this.MDdescription	= "(via MD Consult - Merck<i>Medicus</i> Edition)";
	this.PMdescription	= "(PubMed abstracts from the last 30 days)";
//================================================================
this.deriveJournalName = function (journalName)
//================================================================
	{
	var pos = journalName.indexOf(this.nameTrailer);
	if (pos == -1)
		return journalName;
	else
		return journalName.substr(0, pos);
	}

//================================================================
this.deriveJournalDescription = function (journalName)
//================================================================
	{
	if (journalName.indexOf(this.nameTrailer) != -1)
		return this.MDdescription;
	for (var x = 0; x < this.PubMedJournals.length; x++)
		if (journalName == this.PubMedJournals[x])
			return(this.PMdescription);
	return "";
	}

//================================================================
this.SetFilter	= function(newVal)
//================================================================
	{
	this.filterType = newVal;
	this.GenerateScreen();
	}

//================================================================
this.encode = function(orig)
//================================================================
	{alert("encode");
	ret = orig.replace(/  /g," ");		//change multiple spaces to a single space
	ret = ret.replace(/ /g,"+");		//change a single space to a plus-sign
	ret = ret.replace(/(&amp;|&)/g,"%26");	//change "&amp;" or "&"
	//need to investivate the possible use of "ret = ret.escape();"
//	ret = ret.replace(/;/g,"%3B");		//change a semi-colon
//	ret = ret.replace(/:/g,"%3A");		//change a colon
//	ret = ret.replace(/"/g,"%22");		//change a double quotation mark
	return (ret);
	}

//================================================================
this.debug = function(text)
//================================================================
	{
	if (this.debugging == false) return;

	var Options
		= "directories=0"
		+ ", height=" + Math.floor(window.screen.height - 150)
		+ ", left=0"
		+ ", location=0"
		+ ", menubar=1"
		+ ", resizeable=1"
		+ ", scrollbars=1"
		+ ", status=0"
		+ ", titlebar=1"
		+ ", toolbar=1"
		+ ", top=20"
		+ ", width=" + (window.screen.width - 13);
	if (this.debugWindow == null)
		{
		this.debugWindow = window.open("", "hostLog", Options, false);
		if (this.debugWindow == null) return;
		this.debugWindow.document.writeln("<body style=\"font-family:arial; font-size:9pt;\">");
		}
	this.debugWindow.document.writeln(text + "<br/>");
	}

//================================================================
this.debugHTML = function(text)
//================================================================
	{
	if (this.debugging == false) return;

	var Options
		= "directories=0"
		+ ", height=" + Math.floor(window.screen.height - 150)
		+ ", left=0"
		+ ", location=0"
		+ ", menubar=1"
		+ ", resizeable=1"
		+ ", scrollbars=1"
		+ ", status=0"
		+ ", titlebar=1"
		+ ", toolbar=1"
		+ ", top=20"
		+ ", width=" + (window.screen.width - 13);
	if (this.debugWindow == null)
		{
		this.debugWindow = window.open("", "hostLog", Options, false);
		if (this.debugWindow == null) return;
		this.debugWindow.document.writeln("<body style=\"font-family:arial; font-size:9pt;\">");
		}
	this.debugWindow.document.writeln(text);
	}

//================================================================
this.dumpData = function()
//================================================================
	{
	this.debug("===========");
	this.debug("Specialties (" + this.Specialties.length + ")");
	this.debug("===========");
	for(var x = 0; x < this.Specialties.length; x++)
		{
		var doc = this.Specialties[x];
		this.debug("[" + doc.specialtyName + "]");
		}

	this.debug("=================");
	this.debug("bySpecialtiesInfo (" + this.bySpecialtiesInfo.length + ")");
	this.debug("=================");
	this.debugHTML("<table border='1' style=\"font-family:arial; font-size:8pt;\">");
	var strHeader	= "<tr>"
			+ "<th>speciality Name</th>"
			+ "<th>doc Name</th>"
			+ "<th>content Desc</th>"
			+ "<th>in New Window</th>"
			+ "<th>url</th>"
			+ "<th>sign In Required<th>"
			+ "<th>exit Page<th>"
			+ "<th>url Source<th>"
			+ "</tr>";
	for(var x = 0; x < this.bySpecialtiesInfo.length; x++)
		{
		if (x % 20 == 0) this.debugHTML(strHeader);
		var doc = this.bySpecialtiesInfo[x];
		this.debugHTML("<tr>");
		this.debugHTML("<td>" + doc.specialtyName + "</td>");
		this.debugHTML("<td>" + doc.docName + "</td>");
		this.debugHTML("<td>" + doc.contentDesc + "</td>");
		this.debugHTML("<td>" + doc.inNewWindow + "</td>");
		this.debugHTML("<td>" + doc.url + "</td>");
		this.debugHTML("<td>" + doc.signInRequired + "<td>");
		this.debugHTML("<td>" + doc.exitPage + "<td>");
		this.debugHTML("<td>" + doc.urlSource + "<td>");
		this.debugHTML("</tr>");
		}
	this.debugHTML("</table>");

	this.debug("============");
	this.debug("byTitlesInfo (" + this.byTitlesInfo.length + ")");
	this.debug("============");
	this.debugHTML("<table border='1' style=\"font-family:arial; font-size:8pt;\">");
	var strHeader	= "<tr>"
			+ "<th>specialty Name</th>"
			+ "<th>specialty Name Upper</th>"
			+ "<th>doc Name</th>"
			+ "<th>doc Name Upper</th>"
			+ "<th>content Desc</th>"
			+ "<th>in New Window</th>"
			+ "<th>url</th>"
			+ "<th>sign In Required<th>"
			+ "<th>exit Page<th>"
			+ "<th>url Source<th>"
			+ "</tr>";
	for(var x = 0; x < this.byTitlesInfo.length; x++)
		{
		if (x % 20 == 0) this.debugHTML(strHeader);
		var doc = this.byTitlesInfo[x];
		this.debugHTML("<tr>");
		this.debugHTML("<td>" + doc.specialtyName + "</td>");
		this.debugHTML("<td>" + doc.specialtyNameSort + "</td>");
		this.debugHTML("<td>" + doc.docName + "</td>");
		this.debugHTML("<td>" + doc.docNameSort + "</td>");
		this.debugHTML("<td>" + doc.contentDesc + "</td>");
		this.debugHTML("<td>" + doc.inNewWindow + "</td>");
		this.debugHTML("<td>" + doc.url + "</td>");
		this.debugHTML("<td>" + doc.signInRequired + "<td>");
		this.debugHTML("<td>" + doc.exitPage + "<td>");
		this.debugHTML("<td>" + doc.urlSource + "<td>");
		this.debugHTML("</tr>");
		}
	this.debugHTML("</table>");

	if (this.infoToDisplay != null)
		{
	this.debug("=============");
	this.debug("infoToDisplay (" + this.infoToDisplay.length + ")");
	this.debug("=============");
	this.debugHTML("<table border='1' style=\"font-family:arial; font-size:8pt;\">");
	var strHeader	= "<tr>"
			+ "<th>specialty Name</th>"
			+ "<th>specialty Name Upper</th>"
			+ "<th>doc Name</th>"
			+ "<th>doc Name Upper</th>"
			+ "<th>content Desc</th>"
			+ "<th>in New Window</th>"
			+ "<th>url</th>"
			+ "<th>sign In Required<th>"
			+ "<th>exit Page<th>"
			+ "<th>url Source<th>"
			+ "</tr>";
	for(var x = 0; x < this.infoToDisplay.length; x++)
		{
		if (x % 20 == 0) this.debugHTML(strHeader);
		var doc = this.infoToDisplay[x];
		this.debugHTML("<tr>");
		this.debugHTML("<td>" + doc.specialtyName + "</td>");
		this.debugHTML("<td>" + doc.specialtyNameSort + "</td>");
		this.debugHTML("<td>" + doc.docName + "</td>");
		this.debugHTML("<td>" + doc.docNameSort + "</td>");
		this.debugHTML("<td>" + doc.contentDesc + "</td>");
		this.debugHTML("<td>" + doc.inNewWindow + "</td>");
		this.debugHTML("<td>" + doc.url + "</td>");
		this.debugHTML("<td>" + doc.signInRequired + "<td>");
		this.debugHTML("<td>" + doc.exitPage + "<td>");
		this.debugHTML("<td>" + doc.urlSource + "<td>");
		this.debugHTML("</tr>");
		}
	this.debugHTML("</table>");
		}

	}




// the following is the close-of-object bracket
}
