<!------- hide the script from old browsers (biofilm JavaScripts)
// functions for the biofilm & biodiversity activities
// written by Dan Jacobs, Maryland Sea Grant

var Desc, Team, NumCat; // save initial form entries;
var num_stats = 0;
var stat_request = new Array("S","D","D1","D2","H","E"); // indices
var stats = new Array(0,0,0,0,0,0);                      // if selected

var html_top = "<HTML>\n<HEAD>\n";
html_top += "<TITLE>Maryland Sea Grant Marine Education: ";
html_top += "Biofilms & Biodiversity - ";
html_top += "Student Activity: You're The Expert</TITLE>\n";
html_top += "<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"biodiv2.js\">";
s1 = "</SCR"; s2 = "IPT>\n";
html_top += s1 + s2; //fix MIE bug
html_top += "<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" HREF=\"biofilm.css\">\n";

html_top += "</HEAD>\n";

html_top += "<BODY BGCOLOR=\"#FFFFFF\" LINK=\"#336666\" ALINK=\"#339933\" ";
html_top += "VLINK=\"#339933\">\n";
html_top += "<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"6\" WIDTH=\"100%\">\n";
html_top += "<TR>\n";
html_top += "<TD ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
html_top += "<H1>\n";
html_top += "Let's Calculate Biodiversity<BR>\n";
html_top += "You're The Expert\n";
html_top += "</H1>\n";


var html_bot = "<P>\n<HR SIZE=\"2\">\n<\/P>\n";
html_bot += "<DIV ALIGN=\"CENTER\">\n";
html_bot += "<P>\nPage created " + new Date() + "\n</P>\n";
html_bot += "<A HREF=\"index.htm\">Biofilms & Biodiversity</A>&nbsp;&#8226; ";
html_bot += "<A HREF=\"intro.htm\">Introduction</A>&nbsp;&#8226; ";
html_bot += "<A HREF=\"creature.htm\">Featured Creatures</A>&nbsp;&#8226; ";
html_bot += "<A HREF=\"diverse.htm\">";
html_bot += "How To Calculate Biodiversity</A><BR>\n";
html_bot += "Let's Calculate Biodiversity: <A HREF=\"studnt1.htm\">";
html_bot += "Try These Data</A>, <A HREF=\"studnt2.htm\">Virtual Samples</A>, "; 
html_bot += "<A HREF=\"studnt3.htm\">Biodiversity & Depth</A>, ";
html_bot += "<A HREF=\"studnt4.htm\">You're The Expert</A><BR>\n";
html_bot += "<A HREF=\"glossary.htm\">Glossary</A>&nbsp;&#8226; ";
html_bot += "<A HREF=\"links.htm\">Links</A>&nbsp;&#8226; ";
html_bot += "<A HREF=\"resource.htm\">Teacher Resource</A>&nbsp;&#8226; ";
html_bot += "<A HREF=\"../edmatter.htm\">Maryland Sea Grant Education</A>]\n";
html_bot += "</DIV>\n";

html_bot += "</TD>\n";
html_bot += "</TR>\n";
html_bot += "</TABLE>\n";

html_bot += "</BODY>\n</HTML>\n";

function checkNum (input, msg, what) {
  // input - student entry, msg - which variable
  var str = input;
  if (!input)
    { alert(msg + " was not entered.\n\nPlease try again."); return false; }
  for (var i = 0; i < str.length; i++) {
    var ch = str.substring(i, i + 1)
    if (ch < "0" || "9" < ch) { 
      alert(msg + " is not an integer.\n\nPlease try again."); 
      return false; 
    }
  }
  if (what == "NumCat") { 
    if (str <= 1) { // must be > 1
      alert("You need at least 2 categories.\n\nPlease try again."); 
      return false;
    }
  }
  return true;
}

function round_off(value) {
  // round off the nearest 0.001
  var a = Math.round(value*10000);
  var b = Math.round(value*1000) * 10;
  if (a - b >  4) { b += 10; }
  b = Math.round(b/10)/1000;
  if (navigator.appName == "Netscape") {
    if (b < 1.0 && b > 0.1) { b = '0' + b; }
    else if (b > -1 && b < -0.1) { b = '-0' + -1*b; }
  }
  return b;
}

function input(form) {
  var s = ""; // html string
  // check if the number of categories is an integer > 1
  if (!checkNum(form.NumCat.value,'Number of categories',"NumCat")) { return; }
  NumCat = 1 * form.NumCat.value;
  // save the indices requests
  for (i = 0; i < stat_request.length; i++) {
    stats[i] = form[stat_request[i]].checked;
    if (stats[i]) { num_stats++; }
  }
  if (num_stats == 0) {
    s = "You did not select any of the\nbiodiversity indices.\n\n";
    s += "Please try again.";
    alert(s); return;
  }
  if (form.desc.value) {
    Desc = form.desc.value;
    s += "<H2 ALIGN=\"CENTER\">" + Desc + "</H2>\n";
  }
  if (form.team.value) {
    Team = form.team.value;
    s += "<H3 ALIGN=\"CENTER\">By " + Team + "</H3>\n";
  }
  s += "<FORM>\n<CENTER>\n";
  // create table for data entries
  s += "<H3>Please enter the name and number found for each of the ";
  s += NumCat + " categories.</H3>\n";
  s += "<TABLE BORDER=\"2\" CELLSPACING=\"4\" CELLPADDING=\"4\">\n";
  s += "<TR>\n<TH>Category Name</TH>\n<TH># Found</TH>\n</TR>\n";
  for (i = 1; i <= NumCat; i++) {
    s += "<TR>\n<TD ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
    s += "<INPUT TYPE=\"TEXT\" NAME=\"Cat" + i + "\" SIZE=\"30\" MAXSIZE=\"30\"><BR>\n";
    s += "</TD>\n<TD ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
    s += "<INPUT TYPE=\"TEXT\" NAME=\"Num" + i + "\" SIZE=\"5\" MAXSIZE=\"10\"><BR>\n";
    s += "</TD>\n</TR>\n";
  }
  s += "<TR>\n<TD COLSPAN=2 ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
  s += "<INPUT TYPE=BUTTON VALUE=\"";
  if (num_stats == 1) { s += "Start Analysis"; }
  else { s += "Start Analyses"; }
  s += "\" onClick=\"top.calc(this.form)\">";
  s += "&nbsp;&nbsp;";
  s += "<INPUT TYPE=RESET VALUE=\"Clear Entries\"><BR>\n";
  s += "</TD>\n</TR>\n";
  s += "</TABLE>\n";
  s += "</CENTER>\n</FORM>\n";
  newWin=open("","expert");
  newWin.document.write(html_top);
  newWin.document.write(s);
  newWin.document.write(html_bot);
  newWin.document.close();
}

function calc(form) {
  var sum = S = D = D1 = D2 = H = E = 0; 
  // figure out how many rows are needed (exclude header)
  if (NumCat >= num_stats) { var nrow = NumCat + 1; }
  else { var nrow = num_stats; }
  var array_length = nrow + 1;
  var N = new Array([array_length]);
  var C = new Array([array_length]);
  var Pi = new Array([array_length]);
  var Pi2 = new Array([array_length]);
  var PLOGP = new Array([array_length]);
  var Measure = new Array([array_length]);
  var Value = new Array([array_length]);
  for (i = 0; i < array_length; i++) { // initialize elements in each array
    N[i] = C[i] = Pi[i] = Pi2[i] = PLOGP[i] = Measure[i] = Value[i] = "";
  }
  for (i = 1; i <= NumCat; i++) { // check entries and find species richness
    if (!form["Cat" + i].value) { form["Cat" + i].value = "Category " + i; }
    if (!form["Num" + i].value) { form["Num" + i].value = 0; }
    if (!checkNum(form["Num" + i].value,form["Cat" + i].value,"v")) { return; }
    N[i] = Math.round(1 * form["Num" + i].value);
    C[i] = form["Cat" + i].value;
    sum += N[i];
    // species richness (S)
    if (N[i] > 0) { S += 1; }
  }
  C[NumCat + 1] = "<SPAN STYLE=\"font-weight:bold\">Total</SPAN>";
  N[NumCat + 1] = sum;
  // computations - array[0] = total, 1+ = individual values
  // all indices are computed (much easier and quicker execution)
  for ( i = 0; i <= NumCat; i++) { Pi[i] = Pi2[i] = PLOGP[i] = 0; }
  for (i = 1; i <= NumCat ; i++) {
    if (sum > 0) { Pi[i] = N[i]/sum; }
    Pi[0] += Pi[i]; // sum of Pi
    Pi2[i] = Pi[i]*Pi[i];
    D += Pi2[i]; 
    if (Pi[i] > 0) { PLOGP[i] = Pi[i]*Math.log(Pi[i]) }; 
    H += -1 * PLOGP[i];
    Pi[i] = round_off(Pi[i]);
    Pi2[i] = round_off(Pi2[i]);
    PLOGP[i] = round_off(PLOGP[i]);
  }
  Pi[NumCat + 1] = round_off(Pi[0]); // sum of Pi
  // stats array: [0]=S, [1]=D, [2]=D1, [3]=D2, [4]=H, [5]=E);
  var jj = 1;
  if (stats[0]) { Measure[jj] = "S"; Value[jj] = S; jj++; }
  // Simpson's index (D), diversity index (1 - D), recip. Index (1/D)
  D = round_off(D); 
  D1 = round_off(1 - D); 
  if (D > 0) { D2 = round_off(1 / D); } 
  if (stats[1]) { Measure[jj] = "D"; Value[jj] = D; jj++; }
  if (stats[2]) { Measure[jj] = "1 - D"; Value[jj] = D1; jj++; }
  if (stats[3]) { Measure[jj] = "1/D"; Value[jj] = D2; jj++; }
  // Shannon-Wiener (H) & Evenness (E)
  if (S > 1) { E = round_off(H/Math.log(S)); }
  H = round_off(H);
  if (stats[4]) { Measure[jj] = "H"; Value[jj] = H; jj++; }
  if (stats[5]) { Measure[jj] = "E"; Value[jj] = E; }
  // create table of results
  var s = ""; // html string
  if (Desc) { s += "<H2 ALIGN=\"CENTER\">" + Desc + "</H2>\n"; }
  if (Team) { s += "<H3 ALIGN=\"CENTER\">By " + Team + "</H3>\n"; }
  var a = "<TD ALIGN=\"CENTER\" VALIGN=\"MIDDLE\">\n";
  s += "<DIV ALIGN=\"CENTER\">\n<TABLE BORDER=2 CELLSPACING=0 CELLPADDING=4>\n";
  s += "<TR>\n";
  s += a + "<SPAN STYLE=\"font-weight:bold\">Category</SPAN><BR></TD>\n";
  s += a + "<SPAN STYLE=\"font-weight:bold\"># Found</SPAN><BR></TD>\n";
  s += a + "<SPAN STYLE=\"font-weight:bold\">P<SUB>i</SUB></SPAN><BR></TD>\n";
  if (stats[1] || stats[2] || stats[3]) { // Simpson's indices
    s += a + "<SPAN STYLE=\"font-weight:bold\">P<SUB>i</SUB><SUP>2</SUP></SPAN><BR></TD>\n";
  }
  if (stats[4] || stats[5]) { // Shannon-Wiener, Evenness
    s += a + "<SPAN STYLE=\"font-weight:bold\">P<SUB>i</SUB>ln[P<SUB>i</SUB>]</SPAN><BR></TD>\n";
  }
  s += "<TD ROWSPAN=" + eval(nrow + 1) + " BGCOLOR=\"SILVER\"><BR></TD>\n";
  s += a + "<SPAN STYLE=\"font-weight:bold\">Measure</SPAN><BR></TD>\n";
  s += a + "<SPAN STYLE=\"font-weight:bold\">Value</SPAN><BR></TD>\n</TR>\n";
  a = "<TD ALIGN=\"LEFT\" VALIGN=\"MIDDLE\">\n";
  for (i = 1; i <= nrow; i++) { 
    s += "<TR>\n" + a + C[i] + "<BR></TD>\n";
    s += "<TD ALIGN=\"RIGHT\" VALIGN=\"MIDDLE\">\n" + N[i] + "<BR></TD>\n";
    s += a + Pi[i] + "<BR></TD>\n";
    if (stats[1] || stats[2] || stats[3]) { // Simpson's indices
      s += a + Pi2[i] + "<BR></TD>\n";
    }
    if (stats[4] || stats[5]) { // Shannon-Wiener, Evenness
      s += a + PLOGP[i] + "<BR></TD>\n";
    }
    if (Measure[i] != "") { 
      s += a + "<SPAN STYLE=\"font-weight:bold\">" + Measure[i] + "</SPAN><BR></TD>\n";
      s += a + Value[i] + "<BR></TD>\n";
    }
    s += "</TR>\n";
  }
  s += "</TABLE>\n</DIV>\n";
  newWin=open("","expert");
  newWin.document.write(html_top);
  newWin.document.write(s);
  newWin.document.write(html_bot);
  newWin.document.close();
}

//close off comment to hide this script from old browsers----------->


