/*extern  make, maker, form_and_attach */


// -----------------------------------------------------------------------------------
function district_scores_table_row_calc(scores, teamnames, team1) {

        //  alert("Enter district_scores_table_row_calc() ...");
        var td = maker("td");
        var celltext = [];
        var attrib = [];
   
        for (var team2=0; team2<scores.length; team2++) {

            if (team1==team2) {celltext[team2] = ""; attrib[team2] = "self"; continue;}  // Teams don't play themselves
            
            var s1 = scores[team1][team2];
            var s2 = scores[team2][team1];

            celltext[team2] = "";
            attrib[team2] = "notplayed";
             if (!isNaN(s1)) {   // s1 is a number, and we have a score      
                celltext[team2] =  s1 + "-" + s2; 
                if (s1 > s2) { attrib[team2] = "win"; }
                else if (s1 < s2) { attrib[team2] = "loss"; }
                else {attrib[team2] = "tie"; }       
             }          

        }  // end for(team2)
      
  
   // Form the table row for team1: 

   var tditems = [];

   tditems[0] = td({align:"left"}, [teamnames[team1]]) ;
   
   
   var Modern = false;
   
   if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) {Modern = true;}
   if (navigator.userAgent.toLowerCase().indexOf("chrome") != -1) {Modern = true;}
   if (navigator.userAgent.toLowerCase().indexOf("msie 8.0") != -1) {Modern = true;}
   
   // Maybe we can now (in late 2009) assume that all browsers are modern:
   Modern = true;
   
   // alert("userAgent:|" + navigator.userAgent.toLowerCase() + "|");
      
   for (var i=0; i<scores.length; i++) {
      tditems[i+1] = td({ "class":attrib[i] }, [celltext[i]] );
   }


   // var trow = make("TR", { align:"char", char:"-"  }, tditems);
   var trow = make("TR", { align:"center" }, tditems);           
   return(trow);
    
}     // End of function district_scores_table_row_calc(scores, teamnames, team1)



// -----------------------------------------------------------------------------------
function sortheaderrow() {

    alert("sortheaderrow()");
    return;
}



// -----------------------------------------------------------------------------------
function make_district_scores_table(scores, teamnames, table_place) {


//  alert("Enter make_district_scores_table() ...");
var  th = maker("th"); 
                
var trow = [];
       

  
//  Build the data rows:                 
for (var team1=0; team1 < scores.length; team1++) {
        trow[team1] = district_scores_table_row_calc(scores, teamnames, team1);
}  // end for(team1)
            

// Build the header rows:

var headers = [];


// var f1 = make("form", {name:"f1"}, [ make("input", {name:"hb1", type:"button", value:"Does nothing"}, [] )     ]);
// f1.onclick = sortheaderrow;


headers[0] =  make("TR", [ 
                 //    th( [f1] ), 
                     th( [] ),
                     th( {colSpan:"10"}, ["District"] ), 
                     th( {colSpan:"2"}, ["Non-District"] )
                    ] );



var sortmefirst = "sortmefirst_" + table_place;

headers[1] = make( "TR", [th("Team<span id=" + sortmefirst + "></span>"),   
                           th("Austin"), th("Bush"),  th("Clements"), th("Dulles"), th("Elkins"), 
                           th("High."), th("Kemp."), th("Marsh."), th("Travis"), th("Willow."),
                           th("NonD1"), th("NonD2")           
                  ]);

// Build the COLGROUPS and CAPTION:

var colg1 = make( "COLGROUP", {align:"left"}, [] );
var colg2 = make( "COLGROUP", {span:"10", align:"center", width:"20"}, [] );
var colg3 = make( "COLGROUP", {span:"2", align:"center", width:"20"}, []);
var caption = make( "CAPTION", { },["5A Region III District 23 Scores"] );
var mythead = make( "THEAD" );
var mytbody = make("TBODY");

// Form the table and attach the column groups, caption, and empty thead and tbody:

var my_record_table = make( "TABLE", {align:"center"}, [colg1, colg2, colg3, caption, mythead, mytbody ] );
           
my_record_table.className = "sortable";
my_record_table.id = "scores";
my_record_table.border= 5;
my_record_table.rules= "all";
my_record_table.width= 620;
my_record_table.cellpadding= 2;
my_record_table.cellspacing= 2;

var high = 0;  //  Sort the chosen column high-to-low
var numrows = scores.length-2; // Number of rows of trow to insert into the table

// Form the table, with its headers and its rows, with the chosen column sorted,
//   and insert it into the proper place in the document:
form_and_attach(my_record_table, headers, trow, table_place, sortmefirst, numrows, high);

      
}  // End of function make_district_scores_table(scores, teamnames, table_place)





