var counter = 0;
var counterOk = 0;
var ignDevices = 1;

function paintIt()
{
  var parameter = window.location.href.split("?")[1];
	var trs = document.getElementsByTagName("tr");
	var tds = document.getElementsByTagName("td");
	var h1 = document.getElementsByTagName("h1")[0];
	var legend = document.getElementsByTagName("div")[2];
	var str = h1.firstChild.nodeValue;
	// if selected display mode "notImplemented": 
	// show all testcases with no implementation
	
	if(parameter == "notImplemented")
	{
			// loop through all rows
			
      for(i=0;i<trs.length;i++)
			{
          var trds = trs[i].getElementsByTagName("td");
          var okCounter = 0;
          var naCounter = 0;
          var columnCounter = trds.length;

					// loop through all columns to count 'OK's and 'N/A's for this row
          // -ignDevices is to ignore the last device

          for(j=0;j<columnCounter-ignDevices;j++){
            if(trds[j+1].firstChild.nodeValue.slice(0, 2) == "OK" || trds[j+1].firstChild.nodeValue.slice(0, 7) == "PARTIAL"){
                okCounter++;
            }
            else if(trds[j+1].firstChild.nodeValue.slice(0, 3) == "N/A"){
                naCounter++;
            }
          }
					
					// if no 'OK' is found and not all of the fields are 'N/A', hide this row!
          
					if(okCounter > 0 || naCounter >= columnCounter){
              trs[i].setAttribute("style","display:none");
              counterOk++;
          }
					
					// if this row is not to be hidden, hide the actual browser columns, since they are not interesting here!
					
          else{
              if(trds[0]){
                  counter++;
                  for(k=1;k<trds.length;k++){
                      trds[k].setAttribute("style","display:none");
                  }
              }
          }
      }

			// save title
			
			str = "No implementation currently exists for " + counter + " of "+ (counter+counterOk) +" testcases";
      
  }
  else
  {
	
		// if selected display mode "implemented1": 
		// show all testcases with one or more implementations
	
    if(parameter == "implemented1")
		{
      
			// loop through all rows

     	for(i=0;i<trs.length;i++)
			{
          var trds = trs[i].getElementsByTagName("td");
          var okCounter = 0;
          var columnCounter = trds.length;

					// count all 'OK's and 'PARTIAL's
          // -ignDevices is to ignore the last device

          for(j=0;j<columnCounter-ignDevices;j++)
					{
            if(trds[j+1].firstChild.nodeValue.slice(0, 2) == "OK" || trds[j+1].firstChild.nodeValue.slice(0, 7) == "PARTIAL")
						{
                okCounter++;
            }
          }

					// if no 'OK' or 'PARTIAL' found, hide row!

          if(okCounter < 1 && trds.length>0)
					{
              trs[i].setAttribute("style","display:none");
          }
          else
					{
              counterOk++;
					}
      }
			
			// save title

		  str = "Test cases for which one or more implementations exist: "+counterOk;
    }

		// if selected display mode "implemented2": 
		// show all testcases with two or more implementations

    else if(parameter == "implemented2")
		{
      
			// loop through all rows

     	for(i=0;i<trs.length;i++)
			{
          var trds = trs[i].getElementsByTagName("td");
          var okCounter = 0;
          var columnCounter = trds.length;
          
					// count all 'OK's and 'PARTIAL's
          // -ignDevices is to ignore the last device

          for(j=0;j<columnCounter-ignDevices;j++)
					{
            if(trds[j+1].firstChild.nodeValue.slice(0, 2) == "OK" || trds[j+1].firstChild.nodeValue.slice(0, 7) == "PARTIAL")
						{
                okCounter++;
            }
          }

					// if less than two 'OK's or 'PARTIAL's found, hide row!

          if(okCounter < 2 && trds.length>0)
					{
              trs[i].setAttribute("style","display:none");
          }
          else
					{
              counterOk++;
					}
      }

			// save title
				
		  str = "Test cases for which two or more implementations exist: "+counterOk;
    }
        

		// for each table cell, apply (bg-) color according to it's given value:

    for(i=0;i<tds.length;i++){
      var testStr = tds[i].firstChild.nodeValue;
      if(testStr.slice(0, 2) == "OK")
          tds[i].setAttribute("style","background:#3AA346;color:#FFF;");
      else if(testStr.slice(0, 4) == "FAIL")
          tds[i].setAttribute("style","background:#E02442;color:#FFF;");
      else if(testStr.slice(0, 7) == "PARTIAL")
          tds[i].setAttribute("style","background:#bc0;color:#FFF;");
      else if(testStr.slice(0, 6) == "DOABLE")
          tds[i].setAttribute("style","background:#bc0;color:#FFF;");
      else if(testStr.slice(0, 3) == "N/A")
          tds[i].setAttribute("style","background:#ddd;color:#fff;");
      else if(testStr.slice(0, 4) == "test")
          tds[i].setAttribute("style","background:#bbb;color:#fff;");
      else if(testStr.slice(0, 4) == "must")
          tds[i].setAttribute("style","background:#bbb;color:#fff;");
      else if(testStr.slice(0, 3) == "not")
          tds[i].setAttribute("style","background:#bbb;color:#fff;");
      else if(testStr.slice(0, 1) == "?")
          tds[i].setAttribute("style","background:#bbb;color:#fff;");
    }
		
		// write formerly saved title
		
		h1.firstChild.nodeValue = str;
    
		legend.setAttribute("style","display:inline");
  }
}