// lsmonitor_all.js: common script for all live scoring monitors

var ls_ind = "X";
var ls_last_mess = "";
var ls_last_cutinfo = "";
var auto_refresh=false;
var ref_secs=0;
var ctd = "TD1";
var html_string = "";
var score_col  = false;
var group_ties = false;
var curr_page = "";
var curr_page_height = "0";
sel_players = new Array;
photos = new Object;
    photos.LEADER_PHOTO_id = "";
    photos.LEADER_PHOTO_text = "";
    photos.LOWRND_PHOTO_id = "";
    photos.LOWRND_PHOTO_text = "";
var timeout_id = "null";
var wap_window;
var cardwin;
var linkwin;
var photo_transitions = true;
var transition_type = "12";
var photo_div_width  = 160;
var photo_div_height = (tic_params.photo_height - 0) + 35;

matchplay_tournament = false;
main_tourn     = new Object;
main_data      = new Array;
main_data_ids  = new Array;
selp_tourn     = new Object;
selp_data      = new Array;
selp_data_ids  = new Array;
round_tourn    = new Object;
round_data     = new Array;
round_data_ids = new Array;
prev_tourn     = new Object;
prev_data      = new Array;
prev_data_ids  = new Array;
var cround     = "";

function is_ns()
{
   if (document.all)
       return(false)
   else
       return(true);
}
           
function set_button(bid, bval)
{
   if (document.getElementById(bid) != null)
       document.getElementById(bid).value = bval;
}

function write_dhtml(did, ddata)
{
   if (document.getElementById(did) != null)
       document.getElementById(did).innerHTML = ddata;
}

function write_frame_mess(divid, mess)
{
   var dhtml = "";
   dhtml += "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=10 ALIGN=CENTER WIDTH=300>" +
            "<TR><TD CLASS=TD0L>" + mess + "</TD></TR></TABLE>";
   write_dhtml(divid, dhtml);
}

function return_player(scoreboard_type, ix)
{
   var data = self[scoreboard_type + "_data"];
   var ids  = self[scoreboard_type + "_data_ids"];
   pobj = new Object;
   if ((typeof data == "undefined") || (ix >= data.length))
       return(pobj);
   var d_e  = data[ix].split("/~");
   for (var i=0; i<d_e.length; i++)
       pobj[ids[i]] = ((d_e[i] == "") ? "&nbsp;" : d_e[i]);
   return(pobj);
}

function get_standard_url_params()
{
// tic_params.groupties = ((options.groupties) ? "Y" : "N");
   var params = "";
   params = params + "~season=" + tic_params.season;
   params = params + "~style=" + tic_params.style;
   params = params + "~groupties=" + tic_params.groupties;
   params = params + "~bordersize=" + tic_params.bordersize;
   params = params + "~pr=" + tic_params.pr;
   params = params + "~textout=" + tic_params.textout;
   params = params + "~cardlink=" + tic_params.cardlink;
   params = params + "~winfocus=" + tic_params.winfocus;
   params = params + "~";
   return(params);
}

function show_scorecard(player_ref)
{
   if ((typeof cardwin != "object") || (cardwin.closed)) 
       cardwin = window.open("", "LSCARDWIN",
                             "toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=400,", true)
   else
       cardwin.focus();
   cardwin.location = tic_params.cgipath + "tmcard.cgi" +
                                "?tourn=" + tourn.code +
                                "~player=" + player_ref +
                                "~round=" + tourn.curr_round +
                                get_standard_url_params();
}

function open_linkwin(url)
{
   if ((typeof linkwin != "object") || (linkwin.closed)) 
       linkwin = window.open("", "LSLINKWIN",
                             "toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=640,height=400,", true)
   else
       linkwin.focus();
   linkwin.location = url;
}

function show_scoreboard(sb_params)
{
   open_linkwin(tic_params.cgipath + "tmscores.cgi" +
                          "?tourn="  + tourn.code +
                          "~params=" + sb_params +
                          get_standard_url_params());
}

function load_tic()
{
   open_linkwin(tic_params.cgipath + "tmtic.cgi?" +
                          get_standard_url_params());
}

function load_report(repno)
{
   var r=reports[repno].split("/~");
   open_linkwin(r[1]);
}

function write_matchplay_head(title_only, pix)
{
   var dhtml = "";
   var tot_cols = 13;
   var nflags = (options.shownats == "F");
   if (nflags)  tot_cols += 2;
   var p = return_player("main", pix);
   if (typeof p.round_head == "undefined")
       p.round_head = "?";      
   var rhead  = "";
   cround = p.round_head;
   if  (p.round_head == "?")
       rhead = "&nbsp;"
   else
   if  (p.round_head == "Q")
       rhead = "Quarter-Finals"
   else
   if  (p.round_head == "S")
       rhead = "Semi-Finals"
   else
   if ((p.round_head == "T") && (title_only))
       rhead = "Third Place Play-Off"
   else
   if ((p.round_head == "F") || (p.round_head == "T"))
       rhead = "Final";
   if  (rhead == "")
   {
       cround = p.round;
       rhead = "Round " + p.round;
   }
   dhtml += "<TR><TD colspan=" + tot_cols + " nowrap class=" +
            ((title_only) ? "HEAD2" : "HEAD1") + ">" + rhead + "</TD></TR>";
   if (title_only)
       return(dhtml);
   dhtml += "<TR>" +
            "<TH CLASS=THC>Match</TH>" +
            "<TH CLASS=THC" + ((nflags) ? " colspan=2" : "") + ">Player</TH>" +
            "<TH CLASS=THC nowrap colspan=3>Last 3</TH>" +
            "<TH CLASS=THC>Score</TH>" +
            "<TH CLASS=THC>Holes</TH>" +
            "<TH CLASS=THC>Score</TH>" +
            "<TH CLASS=THC nowrap colspan=3>Last 3</TH>" +
            "<TH CLASS=THC" + ((nflags) ? " colspan=2" : "") + ">Player</TH>" +
            "<TH CLASS=THC>Card</TH>" +
            "</TR>";
   ctd = "";
   return (dhtml);
}

function get_res_symbol(hres)
{
   var dhtml = "";
   var hcol = "black";
   var hsym = "";
   if (hres == "D")
   {  hcol = "#aaaaaa;"; hsym = "H"; }
   if (hres == "W")
   {  hcol = "green"; hsym = "W"; }
   if (hres == "L")
   {  hcol = "red"; hsym = "L"; }
   if (hsym == "")
       dhtml += "&nbsp;"
   else
//     dhtml += "<DIV style='color:" + hcol + ";" +
//              "font-size:22px;font-weight:normal;padding:0px;'>&#x25ef;" +
//              "<DIV style='margin-top:-22px;" +
//              "font-size:8px;font-weight:bold;'>" + hsym + "</DIV>" +
//              "</DIV>";
       dhtml += "<DIV style='color:" + hcol + ";" +
                "border:1px solid " + hcol + " ;font-size:75%;padding:1px;'>" +
                hsym + "</DIV>";
   return (dhtml);
}

function get_matchplay_l3h(l3h)
{
   var dhtml = "";
   for (var i=0; i<3; i++)
   {
       dhtml += "<TD CLASS=" + ctd + "C NOWRAP style='";
       if (i==0) dhtml += "border-right-width:0px;";
       if (i==1) dhtml += "border-left-width:0px;border-right-width:0px;";
       if (i==2) dhtml += "border-left-width:0px;";
       dhtml += "'>";
       if (i >= l3h.length)
           dhtml += "&nbsp;"
       else
           dhtml += get_res_symbol(l3h.charAt(i));
       dhtml += "</TD>";
   }
   return (dhtml);
}

function get_matchplay_player(p, pno, ix)
{
   var nflags = (options.shownats == "F");
   var pname = p["name_" + pno];
   var prep  = p["represents_" + pno];
   var pnat  = p["nationality_" + pno];
   var pseed = p["seeding_" + pno];
   var pstat = p["status_" + pno];
   var pscore= p["full_score_" + pno];
   var pl3h  = p["score_holes_" + pno];
   var pattr = "";
   if (pstat == "W") pattr += "font-weight:bold;";
   if (pstat == "L") pattr += "color:#666666;";
   var showscore = ((pstat != "L") && (pstat != "D"));
   if (!showscore) pscore = "&nbsp;";
   var dhtml = "";
   if ((pno == "2") && (p.holes != "0"))
   {
       dhtml += "<TD CLASS=" + ctd + "C NOWRAP style='" + pattr + "'>" + pscore + "</TD>";
       dhtml += get_matchplay_l3h(pl3h);
   }
   if ((nflags) && (pno == 1))
       dhtml += add_flag_image (pnat, "flag_mplay_" + pno + "_" + ix, true);
   var bordstyle = ((pno == 1) ? "border-left" : "border-right");
   dhtml += "<TD CLASS=" + ctd + "L NOWRAP style='" + pattr +
            ((nflags) ? bordstyle + "-width:0px;" : "") + "'>";
   dhtml += pname;
   if ((pseed != "0") && (pseed != "&nbsp;"))
       dhtml += "&nbsp;<SUP>(" + pseed + ")</SUP>";
   if (prep != "&nbsp;")
       dhtml += "<DIV style='font-size:80%;text-align:center;font-weight:normal;'>" + prep + "</DIV>";
   dhtml += "</TD>";
   if ((nflags) && (pno == 2))
       dhtml += add_flag_image (pnat, "flag_mplay_" + pno + "_" + ix, false);
   if ((pno == "1") && (p.holes != "0"))
   {
       dhtml += get_matchplay_l3h(pl3h);
       dhtml += "<TD CLASS=" + ctd + "C NOWRAP style='" + pattr + "'>" + pscore + "</TD>";
   }
   return (dhtml);
}

function write_matchplay_result(sb_type, ix)
{
   var dhtml = "";
   var nflags = (options.shownats == "F");
   var p = return_player(sb_type, ix);
   if (p.round_head == "&nbsp;")
       p.round_head = cround;
   if (p.round_head != cround)
       dhtml += write_matchplay_head(true, ix);
   dhtml += "<TR>";
   ctd = ((ctd == "TD1") ? "TD2" : "TD1");
   dhtml += "<TD CLASS=" + ctd + "C NOWRAP>" + p.match_no + "</TD>";
   dhtml += get_matchplay_player(p, "1", ix);
   if (p.holes == "0") 
       dhtml += "<TD CLASS=" + ctd + "C NOWRAP colspan=9>Starts " + p.r_start_time + "</TD>"
   else
       dhtml += "<TD CLASS=" + ctd + "C NOWRAP>" +
                "<DIV style='" +
                ((p.complete == "Y") ? "border:1px solid #666666;" : "color:darkblue;") +
                "padding:1px;margin:1px;'>" +
                p.holes +
                "</DIV>" +
                "</TD>";
   dhtml += get_matchplay_player(p, "2", ix);
   dhtml += "<TD CLASS=" + ctd + "G><A HREF=\"JavaScript:show_scorecard('" + p.member_no_1 + "');\">&#187;&#187;</A></TD>";
   dhtml += "</TR>";
   return (dhtml);
}

function write_scoreboard_head(sb_type)
{
   var dhtml = "";
   score_col = false;
   var data_entries = self[sb_type + "_tourn"].data_entries;
   for (var i=0; i<data_entries; i++)
   {
       var p=return_player(sb_type, i);
       if (p.score != "&nbsp;")
           score_col = true;
   }
   if ((sb_type == "main") && (options.leaderboardonly))
   {
       var choles = tourn.course_holes.split("/");
       for (var i=0; i<21; i++)
       {
           var chole = choles[i].split(":");
           var j = i + 1;
           if (j == 19) j = "out"
           if (j == 20) j = "in"
           if (j == 21) j = "tot"
           tourn["ch_par_" + j] = chole[0];
           tourn["ch_len_" + j] = chole[1];
       }
   }
   var htype = sb_type;
   if (sb_type == "selp")
       htype = "main";
   tot_cols = 4;
   if (score_col)        tot_cols += 1;
   var nflags = (options.shownats == "F");
   if (nflags)  tot_cols += 1;
   if (htype == "main")  tot_cols += 5;
   if ((htype == "main") && (options.leaderboardonly)) tot_cols += 20;
   if (htype == "prev")  tot_cols += 3;
   if (htype == "round") tot_cols += 5;
   dhtml += "<TR><TD colspan=" + tot_cols + " nowrap class=HEAD";
   if (sb_type == "main")
       dhtml += "1>Tournament Leaderboard";
   if (sb_type == "round")
       dhtml += "2 style='padding:3px;'>Leading Scores for Round " + tourn.curr_round;
   if (sb_type == "selp")
       dhtml += "2 style='padding:3px;'>Selected Player's Scores";
   if (sb_type == "prev")
       dhtml += "2 style='padding:3px;'>Leaders at the End of Round " + (tourn.curr_round - 1);
   dhtml += "</TD></TR>";
   dhtml += "<TR>";
   dhtml += "<TH CLASS=THC ROWSPAN=2>Pos.</TH>";
   if (score_col)
       dhtml += "<TH CLASS=THC ROWSPAN=2>Score</TH>";
   dhtml += "<TH CLASS=THC ROWSPAN=2>Par</TH>";
   if (nflags)
       dhtml += "<TH CLASS=THC ROWSPAN=2 COLSPAN=2>Name</TH>"
   else
       dhtml += "<TH CLASS=THC ROWSPAN=2>Name</TH>";
   if (htype == "main")
       dhtml += "<TH CLASS=THC COLSPAN=" + ((options.leaderboardonly) ? 23 : 5) + ">Round " + tourn.curr_round + "</TH>";
   if (htype == "prev")
       dhtml += "<TH CLASS=THC COLSPAN=2>Round " + tourn.curr_round + "</TH>" +
                "<TH CLASS=THC ROWSPAN=2>Now</TH>";
   if (htype == "round")
       dhtml += "<TH CLASS=THC ROWSPAN=2>Hole</TH>" +
                "<TH CLASS=THC ROWSPAN=2 COLSPAN=3>Last 3</TH>" +
                "<TH CLASS=THC ROWSPAN=2>Now</TH>";
   dhtml += "<TH CLASS=THC ROWSPAN=2 STYLE='LINE-HEIGHT: 0.8'>C<br>a<br>r<br>d</TH>" +
            "</TR>" + "<TR>";
   if (htype == "main")
   {
       dhtml += "<TH CLASS=THC>Hole</TH>" +
                "<TH CLASS=THC>Par</TH>";
       if (options.leaderboardonly)
       {
           for (var i=1; i<=9; i++)
               dhtml += "<TH CLASS=THC>&nbsp;" + i + "&nbsp;<DIV style='border:1px solid #404040;'>" + tourn["ch_par_" + i] + "</DIV></TH>";
           dhtml += "<TH CLASS=THC nowrap>Front<DIV style='border:1px solid #404040;'>" + tourn["ch_par_out"] + "</DIV></TH>";
           for (var i=10; i<=18; i++)
               dhtml += "<TH CLASS=THC>" + i + "<DIV style='border:1px solid #404040;'>" + tourn["ch_par_" + i] + "</DIV></TH>";
           dhtml += "<TH CLASS=THC nowrap>Back<DIV style='border:1px solid #404040;'>" + tourn["ch_par_in"] + "</DIV></TH>";
           dhtml += "<TH CLASS=THC nowrap>Round<DIV style='border:1px solid #404040;'>" + tourn["ch_par_tot"] + "</DIV></TH>";
       }
       else
           dhtml += "<TH CLASS=THC COLSPAN=3>Last 3</TH>";
   }
   if (htype == "prev")
       dhtml += "<TH CLASS=THC>Hole</TH>" +
                "<TH CLASS=THC>Par</TH>";
   dhtml += "</TR>";
   ctd = "";
   return (dhtml);
}

function add_flag_image(nat, flag_id, flag_left)
{
   var dhtml = "";
   var flag_image = tic_params.flags_base + "flag_" + nat.toLowerCase() + ".gif";
   dhtml += "<TD id='" + flag_id + "' CLASS=" + ctd + "C NOWRAP " +
            "style='" + ((flag_left) ? "border-right-width:0px;" : "border-left-width:0px;") +
            "padding:1px;'>" +
            "<IMG src='" + flag_image + "' " +
            "style='width:25px; height:15px; border:1px solid gray;' " +
            "onerror='document.getElementById(\"" + flag_id + "\").innerHTML=\"(" + nat + ")\";'>" + "</TD>";
   return(dhtml);
}

function write_scoreboard_line(sb_type, ix)
{
   var dhtml = "";
   var p = return_player(sb_type, ix);
   var nflags = (options.shownats == "F");
   dhtml += "<TR>";
   if ((!group_ties) || (p.tied_no == 1))
   {
       ctd = ((ctd == "TD1") ? "TD2" : "TD1");
       var pos_req = true;
   }
   else
       var pos_req = false;

   if (pos_req)
   {
       if ((group_ties) && (p.tied_count > 1))
           rowspanstr=" ROWSPAN=" + p.tied_count
       else
           rowspanstr="";
       dhtml += "<TD CLASS=" + ctd + "L" + rowspanstr + ">" + p.pos + p.tied + "</TD>";
       if (score_col)
           dhtml += "<TD CLASS=" + ctd + "C" + rowspanstr + ">" + p.score + "</TD>";
       dhtml += "<TD CLASS=" + ctd + "C NOWRAP" + rowspanstr + ">" + p.vspar + "</TD>";
   }

   dhtml += "<TD CLASS=" + ctd + "L NOWRAP>" + p.name + "</TD>";
   if (nflags)
       dhtml += add_flag_image (p.nationality, "flag_" + sb_type + "_" + ix, false);

   var dtype = sb_type;
   if (sb_type == "selp")
       dtype = "main";

   if ((dtype == "main") && (p.r_holes == 0))
       dhtml += "<TD CLASS=" + ctd + "L colspan=" + ((options.leaderboardonly) ? 23 : 5) + " NOWRAP>" + "Starts " + p.r_start_time + "</TD>"
   else
   if ((dtype == "prev") && (p.r_holes == 0))
       dhtml += "<TD CLASS=" + ctd + "L colspan=3 NOWRAP>" + "Starts " + p.r_start_time + "</TD>"
   else
   {
       var param = p.r_holes;
       if (p.r_holes == 0)
           param = "&nbsp;"
       else
           if (p.r_holes == 18)
               param = "F";
       dhtml += "<TD CLASS=" + ctd + "C><SPAN STYLE='background-color:white;'>" + param + "</SPAN></TD>";
       var param = p.r_below;
       if (dtype != "round")
           dhtml += "<TD CLASS=" + ctd + "C NOWRAP>" + param + "</TD>";
       if (options.leaderboardonly)
       {
           var stylestr=ctd + "C";
           var cs = p.r_card.split("/");
           for (var j=1; j<=9; j++)
           {
               var cse = cs[j-1].split(":");
               var ws=cse[0];
               var wb=cse[1];
               var stylestr=ctd + "C";
               if (wb < -1)  stylestr="TDSCEA"
               else
               if (wb < 0)   stylestr="TDSCBI"
               else
               if (wb > 1)   stylestr="TDSCDB"
               else
               if (wb > 0)   stylestr="TDSCBO";
               dhtml += "<TD CLASS=" + stylestr + " title='Hole " + j + ": Par " + tourn["ch_par_" + j] + " (" + tourn["ch_len_" + j] + ")'>" + ws + "</TD>";
           }
           var wb=p.r_card_out;
           var stylestr=ctd + "C";
           dhtml += "<TD CLASS=" + stylestr + "><SPAN style='border:" + ((wb=="&nbsp;") ? "0" : "1") + "px solid #808080;padding-left:1px;padding-right:1px;'>" + wb + "</SPAN></TD>";
           for (var j=10; j<=18; j++)
           {
               var cse = cs[j-1].split(":");
               var ws=cse[0];
               var wb=cse[1];
               var stylestr=ctd + "C";
               if (wb < -1)  stylestr="TDSCEA"
               else
               if (wb < 0)   stylestr="TDSCBI"
               else
               if (wb > 1)   stylestr="TDSCDB"
               else
               if (wb > 0)   stylestr="TDSCBO";
               dhtml += "<TD CLASS=" + stylestr + " title='Hole " + j + ": Par " + tourn["ch_par_" + j] + " (" + tourn["ch_len_" + j] + ")'>" + ws + "</TD>";
           }
           var stylestr=ctd + "C";
           var wb=p.r_card_in;
           dhtml += "<TD CLASS=" + stylestr + "><SPAN style='border:" + ((wb=="&nbsp;") ? "0" : "1") + "px solid #808080;padding-left:1px;padding-right:1px;'>" + wb + "</SPAN></TD>";
           dhtml += "<TD CLASS=" + stylestr + ">" + p.r_card_tot + "</TD>";
       }
       else
       if (dtype != "prev")
       {
           for (var j=1; j<4; j++)
           {
               var vname="r_l3h_score_" + j;
               var ws=p[vname];
               var vname="r_l3h_below_" + j;
               var wb=p[vname];
               var stylestr=ctd + "C";
               if (wb < -1)  stylestr="TDSCEA"
               else
               if (wb < 0)   stylestr="TDSCBI"
               else
               if (wb > 1)   stylestr="TDSCDB"
               else
               if (wb > 0)   stylestr="TDSCBO";
               dhtml += "<TD CLASS=" + stylestr + ">" + ws + "</TD>";
           }
       }
       if ((dtype == "round") || (dtype == "prev"))
           dhtml += "<TD CLASS=" + ctd + "C NOWRAP>" + p.t_below + "</TD>";
   }
   dhtml += "<TD CLASS=" + ctd + "G><A HREF=\"JavaScript:show_scorecard('" + p.member_no + "');\">&#187;&#187;</A></TD>";
   dhtml += "</TR>";
   if ((options.showphotos) && (p.pos == "1"))
       if (typeof photos[p.member_no] == "undefined")
       {
           var photo_src = tic_params.photo_base + p.member_no + tic_params.photo_suff;
           photos[p.member_no] = new Image();
           photos[p.member_no].src = photo_src;
       }
   if ((options.leaderboardonly) && (!options.groupties))
       dhtml += "<TR><TD class=TD0C colspan=" + tot_cols + " height=4></TH>";
//   alert(dhtml);
   return (dhtml);
}

function write_scoreboard(divid, sb_type)
{
   var dhtml = "";
   group_ties = options.groupties;
   if (sb_type == "selp")
       group_ties = false;
   if (tic_params.bordertype == 'S')
       dhtml += "<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=2 ALIGN=CENTER STYLE='background-color:white;'>"
   else
       dhtml += "<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 ALIGN=CENTER CLASS=MAIN STYLE='background-color:white;border-collapse:collapse;font-size:100%;'>";
   dhtml += ((matchplay_tournament) ? write_matchplay_head(false, 0) : write_scoreboard_head(sb_type));
   var data_entries = self[sb_type + "_tourn"].data_entries;
   for (var i=0; i<data_entries; i++)
       dhtml += ((matchplay_tournament) ? write_matchplay_result(sb_type, i) : write_scoreboard_line(sb_type, i));
   dhtml += "</TABLE>";
   if (sb_type != "selp")
       dhtml += "<DIV CLASS=TD0C style='padding:4px;'><A HREF=\"JavaScript:show_scoreboard('";
   if (sb_type == "main")
       dhtml += tic_params.mainparams;
   if (sb_type == "round")
       dhtml += tic_params.roundparams;
   if (sb_type == "prev")
       dhtml += tic_params.prevparams;
   if (sb_type != "selp")
       dhtml += "');\">Full Scoreboard</A></DIV>";
   write_dhtml(divid, dhtml);
}

function make_params_string()
{
   var p_string="";
   p_string += options.mon_style + "!";
   p_string += options.sb_limit  + "!";
   p_string += ((options.leaderboardonly) ? "Y!" : "N!");
   p_string += ((options.showphotos)      ? "Y!" : "N!");
   p_string += options.shownats + "!";
   p_string += ((options.groupties)       ? "Y!" : "N!");
   p_string = p_string + options.auto_mins + "!";
   for (var i=0; i<sel_players.length; i++)
       p_string = p_string + sel_players[i] + "-";
   p_string = p_string + "!";
   return(p_string);
}

function get_data()
{
   var p_string=make_params_string();
   frames["LSMONDATA"].location = tic_params.cgipath + "tmlsmon.cgi?type=data" +
                               "~tourn=" + tourn.code +
                               "~params=" + p_string +
                               "~season=" + tic_params.season +
                               "~datatime=" + main_tourn.last_updated_stamp +
                               "~";
}

function process_refresh_button(verbose)
{
   if (ls_ind == "N")
       get_data()
   else
   {
       if (auto_refresh)
       {
           set_button("B_REFRESH", "Start Auto-Refresh");
           if (verbose)
               alert("Scoreboard automatic updating terminating");
           auto_refresh=false;
           reset_countdown(false);
           window.status="Auto-Refresh Cancelled";
       }
       else
       {
           set_button("B_REFRESH", "Cancel Auto-Refresh");
           if (verbose)
               alert("Scoreboard automatic updating starting");
           auto_refresh=true;
       }
       get_data();
   }
}

function loaded_picture(image_id)
{
   var iprop = image_id + "_PHOTO"
   var pprop = iprop + "_text";
   var tprop = image_id + "_TEXT"
   write_dhtml(tprop, "<br>" + photos[pprop]);
}

function default_picture(image_id)
{
   var iprop = image_id + "_PHOTO"
   var pprop = iprop + "_text";
   photos[pprop] = "";
   if (document.images[iprop].src != tic_params.photo_null)
       document.images[iprop].src  = tic_params.photo_null;
}

function run_photo_transition(target_div, photo_text, member_no, member_name, member_nat, filter_trans)
{
   if (typeof photos[member_no] != "undefined")
   {
       var photo = photos[member_no];
       var photo_ok = photo.complete;
       if ((!is_ns()) && (!photo.fileSize > 0))
           photo_ok = false;
   }
   else
       var photo_ok = false;
   var newdiv="";
   newdiv += "<DIV style='width:" + photo_div_width + "px;height:" + photo_div_height + "px;'><IMG src='";
   if (photo_ok)
   {
       var pheight = tic_params.photo_height;
       if (typeof photo.height != "undefined")
          if (photo.height < tic_params.photo_height)
              pheight = photo.height;
       newdiv += photo.src + "' height=" + pheight;
   }
   else
       newdiv += tic_params.photo_null + "'";
   newdiv += ">";
   if (photo_ok)
   {
       newdiv += "<DIV class=FOOT1>" +
                 "<TABLE border=0 cellspacing=0 cellpadding=2 align=center>" +
                 "<TR>" +
                 "<TD class=FOOT1 style='font-weight:bold;'>" + member_name + "</TD>";
       if (options.shownats == "F")
       {
           var flag_image = tic_params.flags_base + "flag_" + member_nat.toLowerCase() + ".gif";
           newdiv += "<TD style='border-left-width:0px;padding:0px;'>" +
                     "<IMG src='" + flag_image + "' " +
                     "style='width:25px; height:15px; border:1px solid gray;'>" +
                     "</TD>";
       }
       newdiv += "</TR></TABLE>" +
                 "<DIV class=FOOT1>" + photo_text + "</DIV>" +
                 "</DIV>";
   }
   newdiv += "</DIV>";
   if (filter_trans)
   {
       var divid = document.all[target_div];
       divid.filters[0].Apply(); 
       divid.innerHTML=newdiv;
       divid.filters[0].Play();
   }
   else
   {
       write_dhtml(target_div, newdiv);
   }
}

function update_headings_photo(image_id, member_no, member_name, member_nat, full_id, photo_text)
{
   if (is_ns())
       photo_transitions = false;
// seems to be a problem with transitions - disable!
   photo_transitions = false;
   var iprop = image_id + "_PHOTO"
   var tprop = image_id + "_TEXT"
   var pprop = iprop + "_id";
   if (photos[pprop] == full_id)
       return(false);
   photos[pprop] = full_id;
   if (photo_transitions)
       run_photo_transition(iprop + "_DIV", photo_text, member_no, member_name, member_nat, true)
   else
       run_photo_transition(iprop + "_DIV", photo_text, member_no, member_name, member_nat, false);
   return(true);
}

function get_random_leader(max_no)
{
   var x = 99;
   while (x > max_no)
   {
       var rstr = "x" + Math.random();
       var x = rstr.charAt(4) - 0;
   }
   return(x);
}

function update_headings_photos()
{
   var f_req = true;
   if (main_tourn.data_entries < 1)
       f_req = false;
   if (f_req)
   {
       var i=0; var cpos="1";
       while ((cpos == "1") && (i < main_tourn.data_entries))
       {
           var p = return_player("main", i);
           cpos = p.pos;
           if (cpos == "1")
               ++i;
       }
       if (i > 0)
       {
           --i;
           var p = return_player("main", get_random_leader(i));
           var new_id = p.member_no + p.pos + p.tied;
           var tourn_complete = (main_tourn.winner_decided == "Y");
           if (p.tied == "=")
               if (tourn_complete)
                   var text_str = "<B>Joint Tournament Winner</B>"
               else
                   var text_str = "<I>Tournament Co-Leader</I>"
           else
               if (tourn_complete)
                   var text_str = "<B>Tournament Winner</B>"
               else
                   var text_str = "<I>Tournament Leader</I>";
           update_headings_photo("LEADER", p.member_no, p.name, p.nationality, new_id, text_str);
       }
   }
   var f_req = true;
   if (options.leaderboardonly)
       f_req = false;
   if (tourn.curr_round < 2)
       f_req = false;
   if (round_tourn.data_entries < 1)
       f_req = false;
   if (f_req)
   {
       var i=0; var cpos="1";
       while ((cpos == "1") && (i < round_tourn.data_entries))
       {
           var p = return_player("round", i);
           cpos = p.pos;
           if (cpos == "1")
               ++i;
       }
       if (i > 0)
       {
           --i;
           var p = return_player("round", get_random_leader(i));
           var new_id = p.member_no + p.pos + p.tied;
           if (p.tied == "=")
               var text_str = "<I>Shares low score of the day</I>"
           else
               var text_str = "<I>Low score of the day</I>";
           update_headings_photo("LOWRND", p.member_no, p.name, p.nationality, new_id, text_str);
       }
   }
}

function update_headings()
{
   if (typeof main_tourn.last_updated != "undefined")
   {
       write_dhtml("D_LAST_UPDATED",    main_tourn.last_updated);
       write_dhtml("D_LAST_UPDATED_CR", main_tourn.last_updated);
   }
   if (typeof main_tourn.ls_message != "undefined")
       var newmess=main_tourn.ls_message
   else
       var newmess="";
   if (newmess != ls_last_mess)
   {
       if (newmess == "")
           write_dhtml("LS_MESS", "");
       else
       {
           if ((newmess.length > 1) && (newmess.charAt(0) == "#"))
           {
               var mscroll = true;
               var mtext = newmess.substring(1, newmess.length);
           }
           else
           {
               var mscroll = false;
               var mtext = newmess;
           }
           if (is_ns())
               mscroll = false;
           if (mscroll)
               write_dhtml("LS_MESS",
                           "<marquee direction=left behaviour=scroll " +
                           "scrollamount=5 scrolldelay=100 class=HEAD2 " +
                           "style='background-color:transparent; color:darkred;'>" +
                           mtext + "</marquee>")
           else
               write_dhtml("LS_MESS",
                           "<SPAN CLASS=HEAD2 STYLE='background-color:transparent; color:darkred;'>" +
                           mtext + "</SPAN>");
       }
       ls_last_mess = newmess;
   }
   if (typeof main_tourn.cut_info != "undefined")
       var newcutinfo=main_tourn.cut_info
   else
       var newcutinfo="";
   if (newcutinfo != ls_last_cutinfo)
   {
       if (newcutinfo == "")
           write_dhtml("LS_CUT", "");
       else
           write_dhtml("LS_CUT", "<SPAN CLASS=FOOT1>" + main_tourn.cut_info + "</SPAN>");
       ls_last_cutinfo = newcutinfo;
   }
   if (typeof main_tourn.live_scoring != "undefined")
       var newlsind=main_tourn.live_scoring
   else
       var newlsind="N";
   if (newlsind != ls_ind)
   {
       ls_ind = newlsind;
       if (ls_ind == "Y")
       {
           write_dhtml("D_LS_STATUS", "LIVE SCORING IN OPERATION");
           write_dhtml("D_LS_STATUS_CR", "LIVE SCORING IN OPERATION");
           set_button("B_REFRESH", "Start Auto-Refresh");
           if ((typeof tic_params.nomessages == "undefined") || (tic_params.nomessages != "Y"))
               alert ("Live scoring is currently in operation.\n\n" +
                      "When you close this message the scoreboard(s) will " +
                      "be updated automatically every " + options.auto_mins +
                      " minute(s).\n\n" +
                      "A countdown to the next update is given in the status " +
                      "bar at the bottom of the page - note that Mozilla (FireFox) users " +
                      "must have \"Change status bar text\" enabled in their " +
                      "advanced javascript options for this feature to be operative.");
           process_refresh_button(false);
           return(0);
       }
       else
       {
           write_dhtml("D_LS_STATUS", "Live Scoring is not in Operation");
           write_dhtml("D_LS_STATUS_CR", "Live Scoring is not in Operation");
           set_button("B_REFRESH", "Refresh Scores");
           if ((typeof tic_params.nomessages == "undefined") || (tic_params.nomessages != "Y"))
               alert ("Live scoring is not currently in operation.\n\n" +
                      "Live scoring is either not applicable at this time or has " +
                      "been suspended. The 'Refresh Scores' button can be used to " +
                      "check whether live scoring has been resumed.");

       }
   }
   if (options.showphotos)
       update_headings_photos();
   if (ls_ind != "Y")
       auto_refresh=false;
}

function refresh_countdown()
{
   if (ref_secs < 1)
   {
       reset_countdown(false);
       window.status='Refreshing (reload monitor if countdown does not appear here!)';
       get_data();
   }
   else
   {
       window.status='Next refresh in ' + ref_secs + ' (seconds)';
       --ref_secs;
   }
}

function reset_countdown(restart_countdown)
{
   if (timeout_id != "null")
       clearInterval(timeout_id);
   timeout_id = "null";
   if (restart_countdown)
   {
       ref_secs = options.auto_mins * 60;
       timeout_id = setInterval('refresh_countdown()', 1000);
   }
}

function copy_object_to_frame(o_in)
{
   var o_out = new Object;
   for (prop in o_in)
       o_out[prop] = o_in[prop];
   return(o_out);
}

function copy_array_to_frame(a_in)
{
   var a_out = new Array;
   for (var i=0; i<a_in.length; i++)
       a_out[i] = a_in[i];
   return(a_out);
}

function data_loaded()
{
   var dframe = frames["LSMONDATA"];
   matchplay_tournament = false;
   if (typeof dframe.main_tourn == "object")
   {
       main_tourn    = copy_object_to_frame (dframe.main_tourn);
       main_data     = copy_array_to_frame  (dframe.main_data);
       main_data_ids = copy_array_to_frame  (dframe.main_data_ids);
   }
   if (typeof dframe.selp_tourn == "object")
   {
       selp_tourn    = copy_object_to_frame (dframe.selp_tourn);
       selp_data     = copy_array_to_frame  (dframe.selp_data);
       selp_data_ids = copy_array_to_frame  (dframe.selp_data_ids);
   }
   if (typeof dframe.round_tourn == "object")
   {
       round_tourn    = copy_object_to_frame (dframe.round_tourn);
       round_data     = copy_array_to_frame  (dframe.round_data);
       round_data_ids = copy_array_to_frame  (dframe.round_data_ids);
   }
   if (typeof dframe.prev_tourn == "object")
   {
       prev_tourn    = copy_object_to_frame (dframe.prev_tourn);
       prev_data     = copy_array_to_frame  (dframe.prev_data);
       prev_data_ids = copy_array_to_frame  (dframe.prev_data_ids);
   }
   if (typeof dframe.mplay_tourn == "object")
   {
       main_tourn    = copy_object_to_frame (dframe.mplay_tourn);
       main_data     = copy_array_to_frame  (dframe.mplay_data);
       main_data_ids = copy_array_to_frame  (dframe.mplay_data_ids);
       matchplay_tournament = true;
   }

   if (typeof main_tourn.doc_title != "undefined")
       if ((typeof tic_params.noheadings == "undefined") || (tic_params.noheadings != "Y"))
           parent.document.title = main_tourn.doc_title;

   if (main_tourn.rounds_played != tourn.curr_round)
   {
       alert("A change in the current round has been detected\n\n" +
             "The page will now be reloaded from the server.");
       p_string = make_params_string();
       this.location = tic_params.cgipath + "tmlsmon.cgi?type=main" +
                       "~tourn=" + tourn.code +
                       "~params=" + p_string +
                       "~noheadings=" + tic_params.noheadings +
                       "~nomessages=" + tic_params.nomessages +
                       get_standard_url_params();
   }

   write_scoreboard("DIV_F1", "main");

   var f_req = true;
   if ((options.leaderboardonly) || (matchplay_tournament))
       f_req = false;
   if (tourn.curr_round < 2)
       f_req = false;
   if (f_req)
       write_scoreboard("DIV_F2", "round");

   if (!matchplay_tournament)
       if (sel_players.length == 0)
           write_frame_mess("DIV_F3",
                            "If you wish to follow the progress of " +
                            "particular players, please select the required " +
                            "players using the 'Options' section at the " +
                            "end of this page.")
       else
           write_scoreboard("DIV_F3", "selp");

   if (f_req)
       write_scoreboard("DIV_F4", "prev");

   update_headings();
   
   if (auto_refresh)
       reset_countdown(true)
   else
       window.status='Auto-Refresh Not Active';
}

function no_data_loaded()
{
   update_headings();
   if (auto_refresh)
       reset_countdown(true)
   else
       window.status='Auto-Refresh Not Active';
}

function string_to_num(instr)
{
   if (instr.length == 0)
       return(-1);
   var out_num = instr - 0;
   if (out_num == "NaN")
       return(-1)
   else
       return(out_num);
}

function process_options()
{
   var j=0;
   var f=document.F_OPTIONS;
   if (!tourn.matchplay)
   {
       for (var i=0; i<f.SB_PLAYERS.options.length; i++)
           if (f.SB_PLAYERS.options[i].selected)
               j++;
       if (j > 10)
       {
           f.SB_PLAYERS.focus();
           alert ("A maximum of 10 players to follow can be selected");
           return(false);
       }
   }
   var sb_mstyl=((tourn.matchplay) ? "F" : f.SB_LSSTYLE.value);
   var sb_limit=((tourn.matchplay) ? 50  : string_to_num(f.TB_SBLIMIT.value));
   if (!((sb_limit > 4) && (sb_limit < 151)))
   {
       f.TB_SBLIMIT.focus();
       alert ("Scoreboard positions limit must be 5 - 150");
       return(false);
   }
   var lb_only = ((tourn.matchplay) ? true : (f.CB_LBONLY.checked));
   var show_photos = ((tourn.matchplay) ? false : (f.CB_PHOTOS.checked));
   var show_nats = f.TB_NATS.value;
   if (tic_params.flags_base == '')
       if (show_nats == "F")
           show_nats = "C";
   var grp_ties = ((tourn.matchplay) ? true : (f.CB_GRPTIES.checked));
   var ref_per=string_to_num(f.TB_REFPER.value);
   if (!((ref_per > 0) && (ref_per < 100)))
   {
       f.TB_REFPER.focus();
       alert ("Auto Refresh period must be 1 - 99 minutes");
       return(false);
   }
   options.mon_style=sb_mstyl;
   options.sb_limit=sb_limit;
   options.leaderboardonly=lb_only;
   options.showphotos=show_photos;
   options.shownats=show_nats;
   options.groupties=grp_ties;
   options.auto_mins=ref_per;
   sel_players.length=0;
   var j=0;
   if (!tourn.matchplay)
       for (var i=0; i<f.SB_PLAYERS.options.length; i++)
       {
           if (f.SB_PLAYERS.options[i].selected)
           {
               var p=players[i].split("/~");
               sel_players[j] = p[0];
               j++;
           }
       }
   p_string = make_params_string();
   new_href = tic_params.cgipath + "tmlsmon.cgi?type=main" +
                                   "~tourn=" + tourn.code +
                                   "~params=" + p_string +
                       "~noheadings=" + tic_params.noheadings +
                       "~nomessages=" + tic_params.nomessages +
                                   get_standard_url_params();
   if (auto_refresh)
       reset_countdown(false);
   window.status = "Reloading ....";
   if (options.mon_style == "M")
       parent.location.href = new_href
   else
       this.location.href = new_href;
   return(true);
}

function browser_ok(monitor_type)
{
   if (document.all)
       return(true);
   if (document.getElementById)
       return(true);
   document.write ("<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0 ALIGN=CENTER WIDTH='80%'>");
   document.write ("<TR><TD CLASS=TD0L>");
   document.write ("<P CLASS=HEAD2>INCOMPATIBLE BROWSER DETECTED</P>");
   document.write ("The browser version you are using does not appear " +
                   "to have the necessary features required to run the " +
                   "live scoring monitor. " +
                   "The recommended browsers needed to use this facility " +
                   "are Internet Explorer&trade; version 5.0 or better, " +
                   "or the Mozilla/FireFox&trade; browser version 6 or better." +
                   "Our apologies for any inconvenience this may have caused.");
   document.write ("<P>" +
                   "We can, however, offer you our mini-leaderboard. This " +
                   "is an auto-refreshing scoreboard that will monitor the " +
                   "live scoring and is compatible with most browsers.");
   document.write ("<P><B>" +
                   "<a href='Javascript:load_mini_monitor();'>" +
                   "Load Mini Live Scoring Monitor" +
                   "</a></B>");
   document.write ("</TD></TR></TABLE>");
   return(false);
}

function wap_information()
{
   if ((typeof wap_window != 'object') || (wap_window.closed))
   {
       wap_window = window.open("", "wapsubwin",
                   "location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes" +
                   ",width=600,height=400,", true);
       wap_window.location.replace(tic_params.wapurl);
   }
}
