var http_request = false;
var isIe = navigator.appName.charAt(0) == "M";
var isMz = !isIe && document.getElementById;
var stainarr = new Object();
var quantitativearr = new Object();
var elisaarr = new Object();

function CStainData()
{
  this.year = 0;
  this.sample = {}
  this.result = {};
  this.mostfrequent = {};
}
CStainData.prototype.year = 0;
CStainData.prototype.sample = {};
CStainData.prototype.result = {};
CStainData.prototype.mostfrequent = {};

CStainData.prototype.getYear = function() {return this.year; };
CStainData.prototype.getSample = function(key) {return this.sample[key] == "" ? "<br>" : this.sample[key]; };
CStainData.prototype.getResult = function(key) {return this.result[key] == "" ? "<br>" : this.result[key]; };
CStainData.prototype.getMostfrequent = function(key) {return this.mostfrequent[key] == "" ? "<br>" : this.mostfrequent[key]; };

function CQuantitativeData()
{
  this.year = 0;
  this.sample = {};
  this.result = {};
  this.mostfrequent = {};
  this.difference = {};
}
CQuantitativeData.prototype.year = 0;
CQuantitativeData.prototype.sample = {};
CQuantitativeData.prototype.result = {};
CQuantitativeData.prototype.mostfrequent = {};
CQuantitativeData.prototype.difference = {};

CQuantitativeData.prototype.getYear = function() {return this.year; };
CQuantitativeData.prototype.getSample = function(key) {return this.sample[key] == "" ? "<br>" : this.sample[key]; };
CQuantitativeData.prototype.getResult = function(key) {return this.result[key] == "" ? "<br>" : this.result[key]; };
CQuantitativeData.prototype.getMostfrequent = function(key) {return this.mostfrequent[key] == "" ? "<br>" : this.mostfrequent[key]; };
//CQuantitativeData.prototype.getDifference = function(key) {return this.difference[key] == "" ? "<br>" : this.difference[key]; };
CQuantitativeData.prototype.getDifference = function(key) {return this.result[key] == "" ? "<br>" : (this.difference[key] == "" ? "<br>" : this.difference[key]); };

function CElisaData()
{
  this.year = 0;
  this.sample = {};
  this.result = {};
  this.average= {};
  this.sd= {};
  this.sdi= {};
}
CElisaData.prototype.year = 0;
CElisaData.prototype.sample = {};
CElisaData.prototype.result = {};
CElisaData.prototype.average = {};
CElisaData.prototype.sd = {};
CElisaData.prototype.sdi = {};

CElisaData.prototype.getYear = function() {return this.year; };
CElisaData.prototype.getSample = function(key) {return key == "" ? "<br>" : (this.sample[key] == "" ? "<br>" : this.sample[key]); };
CElisaData.prototype.getResult = function(key) {return key == "" ? "<br>" : (this.result[key] == "" ? "<br>" : this.result[key]); };
CElisaData.prototype.getAverage = function(key) {return key == "" ? "<br>" : (this.average[key] == "" ? "<br>" : this.average[key]); };
CElisaData.prototype.getSd = function(key) {return key == "" ? "<br>" : (this.sd[key] == "" ? "<br>" : this.sd[key]); };
//CElisaData.prototype.getSdi = function(key) {return key == "" ? "<br>" : (this.sdi[key] == "" ? "<br>" : this.sdi[key]); };
CElisaData.prototype.getSdi = function(key) {return key == "" ? "<br>" : (this.result[key] == "" ? "<br>" : (this.sdi[key] == "" ? "<br>" : this.sdi[key])); };

// HTTP リクエストの共通設定
function setupHttpRequest()
{ 
  if (window.XMLHttpRequest)
  {
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType)
    {
      http_request.overrideMimeType('text/xml');
    }
  }
  else if (window.ActiveXObject)
  {
    try
    {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
      }
    }
  }

  if (!http_request) {
    alert("Giving up :( Cannot create an XMLHTTP instance");
    return false;
  }
  return true;
}

function loadStain(code)
{
  if (!setupHttpRequest())
    return;
  http_request.open('POST', '/hist/retrieve_ifstain.php', false);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http_request.send(null);

  stainarr.length = 0;
  cnt = 0;
  if (http_request.readyState == 4 && http_request.status == 200)
  {
    if (http_request.responseText == "err")
    {
      return false;
    }
    else
    {
      var xmldoc = http_request.responseXML;
      elems = xmldoc.getElementsByTagName("record");
      for (i=0; i<elems.length; i++)
      {
        obj = elems.item(i);
        ee = obj.getElementsByTagName("year");
        year = ee.item(0).firstChild.data;
        if (stainarr[String(year)] == null)
        {
          stainarr[String(year)] = new CStainData();
          stainarr[String(year)].year = year;
        }
        ee = obj.getElementsByTagName("sample");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          sample = ee.item(0).firstChild.data;
        else
          sample = "";
        ee = obj.getElementsByTagName("samplename");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          samplename = ee.item(0).firstChild.data;
        else
          samplename = "";
        ee = obj.getElementsByTagName("freq");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          freq = ee.item(0).firstChild.data;
        else
          freq = "";
        ee = obj.getElementsByTagName("user");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          user = ee.item(0).firstChild.data;
        else
          user = ""
        stainarr[String(year)].sample[sample] = samplename;
        stainarr[String(year)].result[sample] = user;
        stainarr[String(year)].mostfrequent[sample] = freq;
      } 
    }
  }
}

function loadQuantitative(code, target, moretarget, sample1, sample2, sample3)
{
  if (!setupHttpRequest())
    return;
  var url = 'target='+target;
  if (moretarget != '')
    url += '&moretarget='+moretarget;
  url += '&sample1='+sample1+'&sample2='+sample2+'&sample3='+sample3;
  http_request.open('POST', '/hist/retrieve_ifquantitative.php', false);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http_request.send(url);

  quantitativearr.length = 0;
  cnt = 0;
  if (http_request.readyState == 4 && http_request.status == 200)
  {
    if (http_request.responseText == "err")
    {
      return false;
    }
    else
    {
      var xmldoc = http_request.responseXML;
      elems = xmldoc.getElementsByTagName("record");
      for (i=0; i<elems.length; i++)
      {
        obj = elems.item(i);
        ee = obj.getElementsByTagName("year");
        year = ee.item(0).firstChild.data;
        if (quantitativearr[String(year)] == null)
        {
          quantitativearr[String(year)] = new CQuantitativeData();
          quantitativearr[String(year)].year = year;
        }
        ee = obj.getElementsByTagName("sample");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          sample = ee.item(0).firstChild.data;
        else
          sample = "";
        ee = obj.getElementsByTagName("samplename");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          samplename = ee.item(0).firstChild.data;
        else
          samplename = "";
        ee = obj.getElementsByTagName("freq");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          freq = ee.item(0).firstChild.data;
        else
          freq = "";
        ee = obj.getElementsByTagName("user");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          user = ee.item(0).firstChild.data;
        else
          user = "";
        ee = obj.getElementsByTagName("diff");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          diff = ee.item(0).firstChild.data;
        else
          diff = "";
        quantitativearr[String(year)].sample[sample] = samplename;
        quantitativearr[String(year)].result[sample] = user;
        quantitativearr[String(year)].mostfrequent[sample] = freq;
        quantitativearr[String(year)].difference[sample] = diff;
      } 
    }
  }
}

function loadElisa(code, target, sample1, sample2, sample3)
{
  if (!setupHttpRequest())
    return;
  var url = 'target='+target+'&sample1='+sample1+'&sample2='+sample2+'&sample3='+sample3;
  http_request.open('POST', '/hist/retrieve_elisa.php', false);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http_request.send(url);

  elisaarr.length = 0;
  cnt = 0;
  if (http_request.readyState == 4 && http_request.status == 200)
  {
    if (http_request.responseText == "err")
    {
      return false;
    }
    else
    {
      var xmldoc = http_request.responseXML;
      elems = xmldoc.getElementsByTagName("record");
      for (i=0; i<elems.length; i++)
      {
        obj = elems.item(i);
        ee = obj.getElementsByTagName("year");
        year = ee.item(0).firstChild.data;
        if (elisaarr[String(year)] == null)
        {
          elisaarr[String(year)] = new CElisaData();
          elisaarr[String(year)].year = year;
        }
        ee = obj.getElementsByTagName("sample");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          sample = ee.item(0).firstChild.data;
        else
          sample = "";
        ee = obj.getElementsByTagName("samplename");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          samplename = ee.item(0).firstChild.data;
        else
          samplename = "";
        ee = obj.getElementsByTagName("ave");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          ave = ee.item(0).firstChild.data;
        else
          ave = "";
        ee = obj.getElementsByTagName("user");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          user = ee.item(0).firstChild.data;
        else
          user = "";
        ee = obj.getElementsByTagName("sd");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          sd = ee.item(0).firstChild.data;
        else
          sd = "";
        ee = obj.getElementsByTagName("sdi");
        if (ee != null && ee.item(0) != null && ee.item(0).firstChild != null)
          sdi = ee.item(0).firstChild.data;
        else
          sdi = "";
        elisaarr[String(year)].sample[sample] = samplename;
        elisaarr[String(year)].result[sample] = user;
        elisaarr[String(year)].average[sample] = ave;
        elisaarr[String(year)].sd[sample] = sd;
        elisaarr[String(year)].sdi[sample] = sdi;
      } 
    }
  }
}

function max(a, b)
{
  return a > b ? a : b;
}
function drawStainList(tgtelem, year)
{
  d = new Date();
  maxyear = d.getFullYear();
  if (maxyear > year+4)
    maxyear = year + 4;
  var text = "";
  text = "<table id='staintable' cellspacing='0'>\n"
       + "<tr>\n"
       + "  <th class='histlistheadercell' rowspan='2'><br></th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+year+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+1)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+2)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+3)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+4)+"年度</th>\n"
       + "</tr>\n"
       + "<tr>\n";
  for (y=year; y<=maxyear; y++)
  {
     text = text
       + "  <th class='histlistheadercell'>"+stainarr[String(y)].getSample('a')+"</th>\n"
       + "  <th class='histlistheadercell'>"+stainarr[String(y)].getSample('b')+"</th>\n"
       + "  <th class='histlistheadercell'>"+stainarr[String(y)].getSample('c')+"</th>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <th class='histlistheadercell'><br></th>\n"
       + "  <th class='histlistheadercell'><br></th>\n"
       + "  <th class='histlistheadercell'><br></th>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>報告値</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+stainarr[String(y)].getResult('a')+"</td>\n"
       + "  <td class='histlistcell'>"+stainarr[String(y)].getResult('b')+"</td>\n"
       + "  <td class='histlistcell'>"+stainarr[String(y)].getResult('c')+"</td>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>最頻値</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+stainarr[String(y)].getMostfrequent('a')+"</td>\n"
       + "  <td class='histlistcell'>"+stainarr[String(y)].getMostfrequent('b')+"</td>\n"
       + "  <td class='histlistcell'>"+stainarr[String(y)].getMostfrequent('c')+"</td>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text
       + "</tr>\n"
       + "</table>";

  document.getElementById(tgtelem).innerHTML = text;
}

function drawQuantitativeList(tgtelem, year, sample1, sample2, sample3)
{
  d = new Date();
  maxyear = d.getFullYear();
  if (maxyear > year+4)
    maxyear = year + 4;
  var text = "";
  text = "<table id='quantitativetable' cellspacing='0'>\n"
       + "<tr>\n"
       + "  <th class='histlistheadercell' rowspan='2'><br></th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+year+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+1)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+2)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+3)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+4)+"年度</th>\n"
       + "</tr>\n"
       + "<tr>\n";
  for (y=year; y<=maxyear; y++)
  {
     text = text
       + "  <th class='histlistheadercell'>"+quantitativearr[String(y)].getSample(sample1)+"</th>\n"
       + "  <th class='histlistheadercell'>"+quantitativearr[String(y)].getSample(sample2)+"</th>\n"
       + "  <th class='histlistheadercell'>"+quantitativearr[String(y)].getSample(sample3)+"</th>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <th class='histlistheadercell'><br></th>\n"
       + "  <th class='histlistheadercell'><br></th>\n"
       + "  <th class='histlistheadercell'><br></th>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>報告値</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getResult(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getResult(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getResult(sample3)+"</td>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>最頻値</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getMostfrequent(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getMostfrequent(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getMostfrequent(sample3)+"</td>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>報告値-最頻値(管)</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getDifference(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getDifference(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+quantitativearr[String(y)].getDifference(sample3)+"</td>\n";
  }
  for (y=max(year, maxyear+1); y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text
       + "</tr>\n"
       + "</table>";

  document.getElementById(tgtelem).innerHTML = text;
}

function drawElisaList(tgtelem, year, sample1, sample2, sample3)
{
  d = new Date();
  maxyear = d.getFullYear();
  if (maxyear > year+4)
    maxyear = year + 4;
  var text = "";
  text = "<table id='elisatable' cellspacing='0'>\n"
       + "<tr>\n"
       + "  <th class='histlistheadercell' rowspan='2'><br></th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+year+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+1)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+2)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+3)+"年度</th>\n"
       + "  <th class='histlistheadercell' colspan='3'>"+(year+4)+"年度</th>\n"
       + "</tr>\n"
       + "<tr>\n";
  for (y=year; y<=maxyear; y++)
  {
     text = text
       + "  <th class='histlistheadercell'>"+elisaarr[String(y)].getSample(sample1)+"</th>\n"
       + "  <th class='histlistheadercell'>"+elisaarr[String(y)].getSample(sample2)+"</th>\n"
       + "  <th class='histlistheadercell'>"+elisaarr[String(y)].getSample(sample3)+"</th>\n";
  }
  for (y=maxyear+1; y<year+5; y++)
  {
     text = text
       + "  <th class='histlistheadercell'><br></th>\n"
       + "  <th class='histlistheadercell'><br></th>\n"
       + "  <th class='histlistheadercell'><br></th>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>報告値</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getResult(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getResult(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getResult(sample3)+"</td>\n";
  }
  for (y=maxyear+1; y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>平均値</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getAverage(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getAverage(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getAverage(sample3)+"</td>\n";
  }
  for (y=maxyear+1; y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>SD</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getSd(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getSd(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getSd(sample3)+"</td>\n";
  }
  for (y=maxyear+1; y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text + "</tr>\n<tr>\n"
       + "  <td class='histlistcell'>SDI</td>\n";
  for (y=year; y<=maxyear; y++)
  {
    text = text
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getSdi(sample1)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getSdi(sample2)+"</td>\n"
       + "  <td class='histlistcell'>"+elisaarr[String(y)].getSdi(sample3)+"</td>\n";
  }
  for (y=maxyear+1; y<year+5; y++)
  {
     text = text
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n"
       + "  <td class='histlistcell'><br></td>\n";
  }
  text = text
       + "</tr>\n"
       + "</table>";

  document.getElementById(tgtelem).innerHTML = text;
}

