/*extern  make, maker, form_and_attach */


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

        //  alert("Enter district_standings_table_row_calc() ...");
        var td = maker("td");

        var ps=0; // points scored, overall
        var pspg = 0;  // points scored per game, overall
        var pa=0; // point allowed, overall
        var papg = 0;  // points scored per game, overall
        var mov = 0;   // margin of victory
        var movpg = 0; // margin of victory/game
        
        var w=0; // wins, overall
        var l=0; // losses, overall
        var t=0; // ties, overall
        var gp=0; // games played, overall
        
        var wd=0; // district wins
        var ld=0; // district losses
        var tdt=0; // district ties
        
        var psd=0; // points scored, district
        var pspdg = 0;  // points scored per district game               
        var pad=0; // points allowed, district
        var papdg = 0;  // points allowed per district game
        var movd = 0;   // margin of victory in district
        var movpdg = 0; // margin of victory/district game        
        
        var gpd=0; // games played, district
        var wp;    // winning percentage
        var wpd;   // winning percentage, district
            
        
            
        for (var team2=0; team2<scores.length; team2++) {

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

             if (!isNaN(s1)) {   // s1 is a number, and we have a score
                       
                if ( (team1 <= 9) && (team2 <=9)) { // District game
                    gp++; gpd++;
                    ps += s1; psd += s1; 
                    if (s1 > s2) {w++; wd++;}
                    else if (s1 < s2) {l++; ld++;}
                    else { t++; tdt++; }
                    pa += s2;
                    pad += s2;
                }
                
                else { // Non-district game
                    gp++;
                    ps += s1;
                    if (s1 > s2) {w++;}
                    else if (s1 < s2) {l++;}
                    else { t++;}
                    pa += s2;
                }  // End distinguish between District and non-district games
                              
             }          
             else {       // s1 is not a number, so game hasn't been played yet  
                 var j=0; 
             }   // End if (!isNaN(s1))   We have a score
             

            mov = ps - pa;
            movd = psd - pad;
            
            if (gp > 0) {
                wp = (w + t/2) / gp;
                pspg = ps / gp;
                papg = pa / gp;
                movpg = mov / gp;            
            }
            else {
                wp = 0;
                pspg = 0;
                papg = 0; 
                movpg = 0;                          
            }
        
            if (gpd > 0) {
                wpd = (wd + tdt/2) / gpd;
                pspdg = psd / gpd;
                papdg = pad / gpd;
                movpdg = movd / gpd;           
            }
            else {
                wpd = 0;
                pspdg = 0;
                papdg = 0;
                movpdg = 0;             
            }
 
        }  // end for(team2)
      
/****
   alert("w, l, t, wp, ps, pa, wd, ld, tdt, wpd, psd, pad: " + w + " " + l + " " +
          t + " " + wp + " " + ps + " " + pa + " " + wd + " " + ld + " " +
          tdt + " " + wpd + " " + psd + " " + pad );
******/    

    var fixed_wp = wp.toFixed(3);
    var fixed_wpd = wpd.toFixed(3);
    var fixed_pspg = pspg.toFixed(1);
    var fixed_papg = papg.toFixed(1);
    var fixed_pspdg = pspdg.toFixed(1);
    var fixed_papdg = papdg.toFixed(1);
    var fixed_movpg = movpg.toFixed(1);
    var fixed_movpdg = movpdg.toFixed(1);
    

   // Form the table row for team1: 
      // Form the table row for team1: 
   var z = "1";
   var zones = {   Bush: 2, 
                   Dulles: 1, 
                   Austin: 2, 
                   Kempner: 2, 
                   Hightower: 1, 
                   Clements: 1, 
                   Elkins: 1, 
                   Marshall: 1, 
                   Travis: 2, 
                   Willowridge: 2 } ;

   z = zones[teamnames[team1]]; 
           
   var trow = make("TR", {align:"center"},
                      [ td({align:"left", "class":"level" + z}, [teamnames[team1]]), 
                        td(""+w), td(""+l), td(""+t), td(""+fixed_wp), td(""+ps), td(""+pa), td(""+mov),
                             td(""+fixed_pspg), td(""+fixed_papg), td(""+fixed_movpg),
                             td(""+wd), td(""+ld), td(""+tdt), td(""+fixed_wpd), td(""+psd), td(""+pad), td(""+movd),
                             td(""+fixed_pspdg), td(""+fixed_papdg), td(""+fixed_movpdg)
                              ] );
    return(trow);
}     // End of function district_standings_table_row_calc(scores, teamnames, team1)




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

//  alert("Enter make_district_standings_table() ...");
var  th = maker("th"); 
                
var trow = [];
       
       
//  Build the data rows:                 
for (var team1=0; team1 < scores.length; team1++) {
        trow[team1] = district_standings_table_row_calc(scores, teamnames, team1);
}  // end for(team1)
            

// Build the header rows:
var headers = [];

headers[0] = make("TR", [ 
                        th([""]), 
                        th( {colSpan:"10"}, ["Overall"]  ), 
                        th( {colSpan:"10"}, ["District"] )
                    ] );
                    
headers[1] =  make("TR", [ 
                        th([""]), 
                        th( {colSpan:"7"}, [""]  ), 
                        th( {colSpan:"3"}, ["Per Game"] ),
                        th( {colSpan:"7"}, [""]  ), 
                        th( {colSpan:"3"}, ["Per Game"] )                      
                    ] );

var sortmefirst = "sortmefirst_" + table_place;
headers[2] = make( "TR", [th("Team"),   th("W"), th("L"), th("T"), th("%"), th("PS"), th("PA"), th("M"),
                                    th("PS/g"), th("PA/g"), th("M/g"),
                                th("W"), th("L"), th("T"), th("%<span id=" + sortmefirst + "></span>"), th("PS"), th("PA"), th("M"),
                                    th("PS/g"), th("PA/g"), th("M/g")
                  ]);

// Build the COLGROUPS and CAPTION:

var colg1 = make( "COLGROUP", {align:"left"}, [] );
var colg2 = make( "COLGROUP", {span:"10", align:"center", width:"40"}, [] );
var colg3 = make( "COLGROUP", {span:"10", align:"center", width:"40"}, [] );
var caption = make( "CAPTION", ["5A Region III District 23 Standings - Extended Table"] );
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", className:"sortable", id:"standings", border:"3", width:"600",  cellpadding:"2", cellspacing:"2"}, 
                                [colg1, colg2, colg3, caption, mythead, mytbody ] );
                                

var high = 1;  //  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_standings_table(scores, teamnames, table_place)




