// JavaScript Document
// By Christof Daetwyler, M.D. 2007
// christof@piripirei.net
// http://piripirei.net

var OcularFundusArray = new Array();
var myMediaField;
var myMovieCell;
var movieNumber;
var myAssessment;
var myOldID = 0;

function findOcularFundusArray(findWhat)
{
 var myTable = this.document.getElementById("tableText").innerHTML;
  // 0-Nr	1-Title 2-Patient	3-Description	4-Interpretation
var watch_table = top.frames['leftFrame'].document.getElementById("table_watch");
watch_table.innerHTML = "";
if (findWhat == "mySearch") {findWhat = top.frames['mainFrame'].document.getElementById("find").value}
top.frames['leftFrame'].document.getElementById("searchTerm").innerHTML =  findWhat;
if (findWhat == " ") {top.frames['leftFrame'].document.getElementById("searchTerm").innerHTML =  "ALL"}
 var myLine;
 myOldID = 0;
 var WatchStr = "";
 var myMediaField = "";
 OcularFundusArray=myTable.split("*CRLF*");
 for(i = 0; i < OcularFundusArray.length-1; i++)
 {
	 var itemFound = OcularFundusArray[i].indexOf(findWhat);
	 if (itemFound > 0) 
    {
	 var markFind = '<span class="style2">' + findWhat + '</span>';
	 var myNewText = replace(OcularFundusArray[i],findWhat,markFind);
	 OcularFundusArray[i]=myNewText.split("*TAB*"); 
	  var watch_row = watch_table.insertRow(watch_table.rows.length);
      var watch_cell = watch_row.insertCell(0);
	  watch_cell.setAttribute("id", OcularFundusArray[i][0]);
      var watch_link = top.frames['leftFrame'].document.createElement('a');
      watch_link.href= "javascript:showMedia('" + OcularFundusArray[i][0] + "', '" + OcularFundusArray[i][1] + "', '" + OcularFundusArray[i][2] + "', '" + OcularFundusArray[i][3] + "', '" + OcularFundusArray[i][4] + "')";
      watch_link.innerHTML = "<img src='thumbs/" + OcularFundusArray[i][0] + ".GIF' border='0'>" + OcularFundusArray[i][1];
  	  watch_cell.appendChild(watch_link);	
 
      var watch_anchor = top.frames['leftFrame'].document.createElement('a');
      watch_anchor.href= "name = " + OcularFundusArray[i][0];
      watch_cell.appendChild(watch_anchor);
	 }
  } 
   movieNumber=OcularFundusArray.length-1;
}


function createOcularFundusArray()
{
 var myTable = this.document.getElementById("tableText").innerHTML;
  // 0-Nr	1-Title 2-Patient	3-Description	4-Interpretation
var watch_table = top.frames['leftFrame'].document.getElementById("table_watch");
 var myLine;
 var WatchStr = "";
 var myMediaField = "";
 OcularFundusArray=myTable.split("*CRLF*");
 for(i = 0; i < OcularFundusArray.length-1; i++)
 {
	  OcularFundusArray[i]=OcularFundusArray[i].split("*TAB*"); 
	  
	  var watch_row = watch_table.insertRow(watch_table.rows.length);
      var watch_cell = watch_row.insertCell(0);
	  watch_cell.setAttribute("id", OcularFundusArray[i][0]);
      var watch_link = top.frames['leftFrame'].document.createElement('a');
      watch_link.href= "javascript:showMedia('" + OcularFundusArray[i][0] + "', '" + OcularFundusArray[i][1] + "', '" + OcularFundusArray[i][2] + "', '" + OcularFundusArray[i][3] + "', '" + OcularFundusArray[i][4] + "')";
      watch_link.innerHTML = "<img src='thumbs/" + OcularFundusArray[i][0] + ".GIF' border='0'>" + OcularFundusArray[i][1];
  	  watch_cell.appendChild(watch_link);	
 
      var watch_anchor = top.frames['leftFrame'].document.createElement('a');
      watch_anchor.href= "name = " + OcularFundusArray[i][0];
      watch_cell.appendChild(watch_anchor);
  } 
   movieNumber=OcularFundusArray.length-1;
}

function showTitleScreen()
{
	top.frames['mainFrame'].document.getElementById("titleCell").style.display = "none";
    top.frames['mainFrame'].document.getElementById("pictureCell").style.display = "none";
    top.frames['mainFrame'].document.getElementById("patientCell").style.display = "none";
    top.frames['mainFrame'].document.getElementById("descriptionCell").style.display = "none";
    top.frames['mainFrame'].document.getElementById("interpretationCell").style.display = "none";
    top.frames['mainFrame'].document.getElementById("introCell").style.display = "inline";
	top.frames['mainFrame'].document.getElementById("interpretationOpener").style.display = "none"
}

function showMedia(name,title,patient,description,interpretation)
{
	// titleCell, pictureCell, descriptionCell

    if (top.frames['leftFrame'].document.getElementById(myOldID)) {top.frames['leftFrame'].document.getElementById(myOldID).style.backgroundColor="#000000"};
    top.frames['leftFrame'].document.getElementById(name).style.backgroundColor='#330000';
    myOldID = name;
	top.frames['mainFrame'].document.getElementById("myID").innerHTML = name;
		
	myPicture = '<img src="media/'+name+'.JPG">';
	top.frames['mainFrame'].document.getElementById("titleCell").innerHTML =  title;
    top.frames['mainFrame'].document.getElementById("patientCell").innerHTML = replace(patient,"*BR*","<BR />");
	top.frames['mainFrame'].document.getElementById("pictureCell").innerHTML = myPicture;
	top.frames['mainFrame'].document.getElementById("descriptionCell").innerHTML = replace(description,"*BR*","<BR />");
	
	//alert(interpretation);
	if (interpretation == "")
	{top.frames['mainFrame'].document.getElementById("interpretationOpener").style.display = "none"}
	else
	{top.frames['mainFrame'].document.getElementById("interpretationOpener").style.display = "inline"}
	
	top.frames['mainFrame'].document.getElementById("interpretationCell").innerHTML = replace(interpretation,"*BR*","<BR />");
	
	top.frames['mainFrame'].document.getElementById("titleCell").style.display = "inline";
    top.frames['mainFrame'].document.getElementById("patientCell").style.display = "inline";
    top.frames['mainFrame'].document.getElementById("descriptionCell").style.display = "inline";
    top.frames['mainFrame'].document.getElementById("pictureCell").style.display = "inline";
    top.frames['mainFrame'].document.getElementById("interpretationCell").style.display = "none";
    top.frames['mainFrame'].document.getElementById("introCell").style.display = "none";
}

function showInterpretation()
{
	top.frames['mainFrame'].document.getElementById("interpretationCell").style.display = "inline";
	}

 function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
