var currentRow = null;
var currentSortBy = null;

function transformTableData()
{
    oXMLDOM = document.all.participantsXML.XMLDocument;
    oXSLDOM = document.all.tableXSL.XMLDocument;
    newHTML = oXMLDOM.transformNode(oXSLDOM);
    document.all.tableBody.innerHTML = newHTML;
}

function sort(sortBy,dataType)
{
   XSLIsland = document.all.tableXSL.XMLDocument;
   var objSelect =
     XSLIsland.selectSingleNode("//xsl:sort/@select");

   objSelect.nodeValue = sortBy;

   var objOrder =
     XSLIsland.selectSingleNode("//xsl:sort/@order");

   if (currentSortBy == sortBy){
     if (objOrder.nodeValue == "ascending") {
       objOrder.nodeValue = "descending";
     } else {
       objOrder.nodeValue = "ascending";
     }
   } else {
     objOrder.nodeValue = "ascending";
   }

   currentSortBy = sortBy;
   var objDataType =
     XSLIsland.selectSingleNode("//xsl:sort/@data-type");
   objDataType.nodeValue = dataType;

   transformTableData();
   selectRow(document.getElementsByTagName('TR').item(1));
}

function keyCheck(keyCode)
{
   switch (keyCode)
     {
     //enter
     case 13:
       selectCurrentRow();
       break;
     //up arrow
     case 38:
       try{
         selectRow(currentRow.previousSibling);
       } catch(e){}
       break;
     //down arrow
     case 40:
       try{
         selectRow(currentRow.nextSibling);
       } catch(e){}
       break;
     }
}

function selectRow(row)
{
   if(row == null){
     try{
       currentRow.style.backgroundColor = "#ccffff";
     } catch(e){}
     try{
       currentRow.nextSibling.style.backgroundColor = "#ffffff";
     } catch(e){}
     try{
       currentRow.previousSibling.style.backgroundColor = "#ffffff";
     } catch(e){}
   } else {

     try{
       row.style.backgroundColor = "#ccffff";
     } catch(e){}
     try{
        currentRow.style.backgroundColor = "#ffffff";
     } catch(e){}
     parent.currentRow = row;
     currentRow = row;
   }
}

function selectCurrentRow()
{
     XMLIsland = document.all.participantsXML.XMLDocument;
     alert("Raw XML of selected row: \n\n " + 
 	XMLIsland.selectSingleNode("//Participant[@id='" + 
 	currentRow.childNodes(0).innerText + "']").xml);
}


