var debug = false;

var checkEverySeconds = 5;

function makeHttpRequest(url, callback_function, return_xml) {
   var http_request = false;

   if (window.XMLHttpRequest) {
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
           http_request.overrideMimeType('text/xml');
       }
   } else if (window.ActiveXObject) {
       try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
           try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
       }
   }

   if (!http_request) {
       alert('Unfortunately your browser doesn\'t support this feature.');
       return false;
   }
   if (callback_function) {
     http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
               if (return_xml) {
                   eval(callback_function + '(http_request.responseXML)');
               } else {
                   eval(callback_function + '(http_request.responseText)');
               }
           }
       }
     }
   }
   http_request.open('GET', url, true);
   http_request.send(null);
}

function parsePlayerXML (playerXML) {
   var players = playerXML.getElementsByTagName("player");
   for (var i = 0; i < players.length; i++) {
      var pid = players[i].getAttribute("id");
      if (debug) {
         alert("got XML data back for pid = " + pid);
      }
      var attributes = players[i].attributes;
      for (var j = 0; j < attributes.length; j++) {
         var lookFor = attributes[j].name + "_" + pid;
         if (document.getElementById(lookFor)) {
            document.getElementById(lookFor).innerHTML = attributes[j].value;
         }
      }
      if (players[i].getAttribute("points").length > 0) {
         playerScore[pid] = players[i].getAttribute("points");
      }
      if (players[i].getAttribute("gameSecondsRemaining").length > 0) {
         playerGameSecondsRemaining[pid] = players[i].getAttribute("gameSecondsRemaining");
      }
      if (document.getElementById("player_" + pid)) {
         var node = document.getElementById("player_" + pid);
         var currentClass = node.getAttribute("class");
         if (currentClass == null || currentClass.length == 0) {
            currentClass = node.getAttribute("className");
         }
         var newClass = players[i].getAttribute("class");
         if (currentClass != newClass) {
            node.setAttribute("class", newClass);
            node.setAttribute("className", newClass);
         } else if (newClass == null || newClass.length == 0) {
            node.removeAttribute("class");
            node.removeAttribute("className");
         }
      }
      if (document.getElementById("points_" + pid) && players[i].getAttribute("explanation").length > 0) {
         var node = document.getElementById("points_" + pid);
         node.innerHTML = "<A HREF=\"javascript:void(0);\" onClick=\"alert(this.title);\" TITLE='" + players[i].getAttribute("explanation") + "'>" + parseFloat(playerScore[pid]).toFixed(precision) + "</A>";
      }
      if (document.getElementById("details_" + pid) && players[i].getAttribute("explanation").length > 0) {
         var node = document.getElementById("details_" + pid);
         var details = players[i].getAttribute("explanation").replace(/&#xA;/gi, "<br />");
         // &amp;#xA; = <br />
         node.innerHTML = details;
      }
   }
   players = null;
}

function formatAsMinutes (seconds) {
   var minutes = Math.floor(seconds / 60);
   var localSeconds = (seconds % 60);
   if (localSeconds.toString().length == 1) {
      localSeconds = "0" + localSeconds.toString();
   }
   return parseInt(minutes) + ":" + localSeconds;
}

function updateFranchiseData () {

   if (debug) {
      alert("inside updateFranchiseData");
   }
   var numFranchises = 0;
   var totalFranchiseScore = 0;
   for (var fid in franchiseScores) {
      var franchise_score = 0;
      var yet_to_play = 0;
      var currently_playing = 0;
      var seconds_remaining = 0;
      if (franchiseStarters[fid]) {
         numFranchises++;
         var playersArray = franchiseStarters[fid].split(",");
         for (var j = 0; j < playersArray.length; j++) {
            if (playersArray[j].length > 0) {
               if (playerScore[playersArray[j]]) {
                  franchise_score += parseFloat(playerScore[playersArray[j]]);
               }
               if (playerGameSecondsRemaining[playersArray[j]] > 0) {
                  seconds_remaining += parseInt(playerGameSecondsRemaining[playersArray[j]]);
                  if (playerGameSecondsRemaining[playersArray[j]] < 3600) {
                     currently_playing++;
                  } else {
                     yet_to_play++;
                  }
               }
            }
         }
      }
      franchiseScores[fid] = parseFloat(franchise_score).toFixed(precision);
      totalFranchiseScore += parseFloat(franchiseScores[fid]);

      if (document.getElementById("yet_to_play_" + fid)) {
         document.getElementById("yet_to_play_" + fid).innerHTML = yet_to_play;
      }
      if (document.getElementById("currently_playing_" + fid)) {
         document.getElementById("currently_playing_" + fid).innerHTML = currently_playing;
      }
      if (document.getElementById("minutes_remaining_" + fid)) {
         document.getElementById("minutes_remaining_" + fid).innerHTML = formatAsMinutes(seconds_remaining);
      }
   }
   if (numFranchises > 0) {
      franchiseScores['AVG'] = parseFloat(totalFranchiseScore / numFranchises).toFixed(precision);
   }
}

function reloadActivePlayers () {

   var gotData = false;
   if (debug) {
      alert("inside reloadActivePlayers, secondsSincePageLoaded = " + secondsSincePageLoaded);
   }
   for (var pid in playerGameStarts) {
      if (playerGameStarts[pid] <= (currentServerTime + secondsSincePageLoaded) && playerGameSecondsRemaining[pid] > 0) {
         var url = xmlBaseURL + league_id + '_' + pid + '_' + week + '.xml' + '?random=' + get_random_string();
         try {
            makeHttpRequest(url, 'parsePlayerXML', 1);
            gotData = true;
         } catch (e) {
            url = window.location.protocol + "//" + window.location.host + "/fflnetdynamic" + year + "/" + league_id + '_' + pid + '_' + week + '.xml?random=' + get_random_string();
            makeHttpRequest(url, 'parsePlayerXML', 1);
            gotData = true;
         }
      }
   }
   if (gotData) {
      updateFranchiseData();
      updateFranchiseScores();
   }
   secondsSincePageLoaded += checkEverySeconds;
   setTimeout("reloadActivePlayers()", checkEverySeconds * 1000);
}

function parseLiveScoringSummaryXML (liveScoringSummaryXML) {
   var franchises = liveScoringSummaryXML.getElementsByTagName("franchise");
   for (var i = 0; i < franchises.length; i++) {
      var fid = franchises[i].getAttribute("id");
      var score = franchises[i].getAttribute("score");
      franchiseScores[fid] = score;
   }
   franchises = null;
   updateFranchiseScores();
}

function reloadLiveScoringSummary (exportProgram, week) {
   if (debug) {
      alert("inside reloadLiveScoringSummary with exportProgram = " + exportProgram + ", league_id = " + league_id + ", week = " + week);
   }
   var url = exportProgram + "?L=" + league_id + "&TYPE=liveScoring&W=" + week;
   makeHttpRequest(url, 'parseLiveScoringSummaryXML', 1);
   setTimeout("reloadLiveScoringSummary('" + exportProgram + "','" + week + "')", 90 * 1000);
   url = null;
}

function updateFranchiseScores () {
   if (document.getElementById) {
      for (var fid in franchiseScores) {
         for (var i = 1; i < 10; i++) {
            var thisScore = document.getElementById("fid_" + fid + i);
            if (thisScore) {
               thisScore.innerHTML=parseFloat(franchiseScores[fid]).toFixed(precision);
            } else {
               break;
            }
         }
      }
   }
}

function toggle_display (id) {
   var table = document.getElementById(id);
   if (table) {
      var children = table.getElementsByTagName("TBODY");
      var new_status = '-';
      for (var i = 0; i < children.length; i++) {
         var display = children[i].style.display ? '' : 'none';
         if (display == 'none') {
            new_status = '+';
         }
         children[i].style.display = display;
         var url = window.location.protocol + "//" + window.location.host + "/" + year + "/save_setting?L=" + league_id + "&TITLE=DISPLAY&VALUE=" + id + ":" + display;
         makeHttpRequest(url);
      }
      // change the innerHTML to [-] or [+] accordingly...
      var spans = table.getElementsByTagName("SPAN");
      for (var i = 0; i < spans.length; i++) {
         var currentClass = spans[i].getAttribute("className");
         if (currentClass == null || currentClass.length == 0) {
            currentClass = spans[i].getAttribute("class");
         }
         if (currentClass != null && currentClass.length > 0 && currentClass.indexOf("module_expand") >= 0) {
            spans[i].innerHTML = "[" + new_status + "]";
         }
      }
   }
}

function show_tab (tab_id) {

   var done = false;
   var counter = 0;
   while (! done) {
      var this_tab_content = document.getElementById("tabcontent" + counter);
      var this_tab = document.getElementById("tab" + counter);
      if (! this_tab_content) {
         done = true;
      } else {
         if (counter == tab_id) {
            this_tab_content.style.display = '';
            this_tab.className = "currenttab";
         } else {
            this_tab_content.style.display = 'none';
            this_tab.className = "";
         }
      }
      counter++;
   }
   location.hash = tab_id;
}

function set_focus () {
   if (document.login && document.login.PASSWORD) {
      document.login.PASSWORD.focus();
   } else if (document.SELECT_FRANCHISE && document.SELECT_FRANCHISE.SELECT) {
      document.SELECT_FRANCHISE.SELECT.focus();
   } else if (document.chat && document.chat.chat && ! document.getElementById("body_ajax_ld")) {
      // don't set the focus in the live draft room!
      document.chat.chat.focus();
   }
}

function test_scoring_window (url, rule) {
    var testScoring = window.open(url + escape(rule),"test_scoring","height=500,width=450,scrollbars=yes,toolbar=no,status=no,resizable=yes,menubar=no");
    testScoring.focus();
}

function help_window (program) {
    var createHelp = window.open("http://www.myfantasyleague.com/help/nojs/" + program + ".html","help_window","height=500,width=450,scrollbars=yes,toolbar=no,status=no,resizable=yes,menubar=no");
    createHelp.focus();
}

function chat_window (url) {
    var chatWindow = window.open(url,"chat_window","height=500,width=450,scrollbars=yes,toolbar=no,status=no,resizable=yes,menubar=no");
    chatWindow.focus();
}

function faq_answer_window (faq_id, category, subcategory) {
    var url = baseURLDynamic + "/" + year + "/show_faq?L=" + league_id;
    if (typeof faq_id != "undefined") {
       url += "&FAQ_ID=" + faq_id;
    }
    if (typeof category != "undefined") {
       url += "&CATEGORY=" + encodeURIComponent(category);
    }
    if (typeof subcategory != "undefined") {
       url += "&SUBCATEGORY=" + encodeURIComponent(subcategory);
    }
    var faq_window = window.open(url, "faq_window","height=500,width=450,scrollbars=yes,toolbar=no,status=no,resizable=yes,menubar=no");
    faq_window.focus();
}

function add_my_links () {
   var url = location.href;
   var title = document.title;
   if (title.substr(0, "Fantasy Football: ".length) == "Fantasy Football: ") {
      title = title.substr("Fantasy Football: ".length);
   }
   var newURL = window.location.protocol + "//" + window.location.host + "/" + year + "/add_my_links?L=" + league_id + "&URL=" + escape(url) + "&TITLE=" + escape(title);
   self.location = newURL;
}

function postMessage () {

   var field = document.getElementById("chat_text_field");
   var to_fid = "";
   if (field.form.TO_FID) {
      to_fid = field.form.TO_FID.options[field.form.TO_FID.selectedIndex].value;
   }

   if (field.value.length > 0) {
     var url = window.location.protocol + "//" + window.location.host + "/" + year + "/chat_save?L=" + league_id + "&MESSAGE=" + escape(field.value) + "&TO_FID=" + to_fid + "&random=" + get_random_string();
     field.value = '';
     makeHttpRequest(url);
     url = null;
     chatRead = 0;
     setTimeout("readMessages(false)", 250);
   }
}

function get_random_string () {
   var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
   var string_length = 8;
   var randomstring = '';
   for (var i=0; i<string_length; i++) {
      var rnum = Math.floor(Math.random() * chars.length);
      randomstring += chars.substring(rnum,rnum+1);
   }
   return randomstring;
}

function readMessages (restartTimer) {

   var url = xmlBaseURL + league_id + '_chat.xml?random=' + get_random_string();
   try {
      makeHttpRequest(url, 'parseChatXML', 1);
   } catch (e) {
      url = window.location.protocol + "//" + window.location.host + "/fflnetdynamic" + year + "/" + league_id + '_chat.xml?random=' + get_random_string();
      makeHttpRequest(url, 'parseChatXML', 1);
   }
   url = null;

   if (typeof chatRead == "undefined") {
      chatRead = 0;
   }
   chatRead++;
   if (chatRead < 360 && restartTimer) {
      setTimeout("readMessages(true)", checkEverySeconds * 1000);
   } else if (chatRead >= 360) {
      // do we need to notify the customer that this has stopped?
      var chat_table = document.getElementById("league_chat");
      var table_body;
      if (chat_table) {
         table_body = chat_table.getElementsByTagName("TBODY");
      } else {
         table_body = document.getElementsByTagName("TBODY");
      }
      var body_rows = table_body[0].getElementsByTagName("TR");
      var new_row = document.createElement("TR");
      new_row.setAttribute("id", "pausedMessage");
      var cell = document.createElement("TD");
      cell.setAttribute("colspan", 2);
      cell.innerHTML = "<span class=\"warning\">Chat suspended due to inactivity - refresh to resume.</span>";
      new_row.appendChild(cell);
      table_body[0].insertBefore(new_row, body_rows[1]);
      chatRead = 0;
   }
}

function time_difference (oldTime, newTime) {
   var time_diff = newTime - oldTime;

   if (time_diff < 60) {
      time_diff = null;
      return "less than a minute ago";
   }
   var retval = "";
   var days = Math.floor(time_diff / (24 * 60 * 60));
   if (days > 0) {
      retval = days + " day";
   }
   if (days > 1) {
      retval += "s";
   }
   time_diff -= (days * 24 * 60 * 60);
   var hours = Math.floor(time_diff / (60 * 60));
   if (hours > 0) {
      if (retval.length > 0) {
        retval += ", ";
      }
      retval += hours + " hour";
   }
   if (hours > 1) {
      retval += "s";
   }
   time_diff -= (hours * 60 * 60);
   var minutes = Math.floor(time_diff / 60);
   if (minutes > 0) {
      if (retval.length > 0) {
        retval += ", ";
      }
      retval += minutes + " minute";
   }
   if (minutes > 1) {
      retval += "s";
   }
   retval += " ago";
   days = null;
   minutes = null;
   time_diff = null;
   return retval;
}

function parseVisitorsXML (visitorsXML) {
   var visitors = visitorsXML.getElementsByTagName("visit");
   var num_visitors = 0;
   var visitor_list_array = new Array();
   for (var i = 0; i < visitors.length; i++) {
      var fid = visitors[i].getAttribute("franchise_id");
      var timestamp = visitors[i].getAttribute("timestamp");
      var last_visit = document.getElementById("last_visit_" + fid);
      if (timestamp >= currentServerTime - (5 * 60)) {
         num_visitors++;
         visitor_list_array.push(franchiseDatabase['fid_' + fid].name);
         if (last_visit) {
            last_visit.setAttribute("class", "franchise_online");
            last_visit.setAttribute("className", "franchise_online");
            last_visit.innerHTML = time_difference(timestamp, currentServerTime);
         }
      } else {
         if (last_visit) {
            last_visit.setAttribute("class", "franchise_offline");
            last_visit.setAttribute("className", "franchise_offline");
            last_visit.innerHTML = time_difference(timestamp, currentServerTime);
         }
      }
      fid = null;
      timestamp = null;
      last_visit = null;
   }
   var visitor_count = document.getElementById("visitor_count");
   if (visitor_count && visitor_count.innerHTML != num_visitors) {
      // if (num_visitors > visitor_count.innerHTML) {
         // play_audio_clip("anotherleaguemateisonline");
      // }
      visitor_count.innerHTML = num_visitors;
   }
   var visitor_list = document.getElementById("visitor_list");
   if (visitor_list) {
      if (visitor_list.type == "select") {
         visitor_list.length = 0;
         for (var i = 0; i < visitor_list_array.length; i++) {
            visitor_list.options[visitor_list.length] = new Option(visitor_list_array[i]);
         }
      } else {
         var innerHTML = "";
         for (var i = 0; i < visitor_list_array.length; i++) {
            if (innerHTML.length > 0) {
               innerHTML += ", ";
            }
            innerHTML += visitor_list_array[i];
         }
         visitor_list.innerHTML = innerHTML;
      }
   }
   visitors = null;
   num_visitors = null;
   visitor_list_array = null;
}

function parseChatXML (chatXML) {
   var messages = chatXML.getElementsByTagName("message");

   var chat_table = document.getElementById("league_chat");
   var table_body;
   if (chat_table) {
      table_body = chat_table.getElementsByTagName("TBODY");
   } else {
      table_body = document.getElementsByTagName("TBODY");
   }

   var do_clear = false;
   var action = chatXML.getElementsByTagName("messages")[0].getAttribute("action");
   if (action && action == "clear") {
      do_clear = true;
   }

   var body_rows = table_body[0].getElementsByTagName("TR");
   var second_row = body_rows[1];
   var newMessage = 0;

   // todo - compare each message's "to" value to the current franchise_id - if it exists, and it's the same
   // display it, otherwise, don't
   if (! do_clear) {
      var messagesDisplayed = 0;
      for (var i = 0; i < messages.length; i++) {
         if (messagesDisplayed > displayMessages) {
            break;
         }
         var id = messages[i].getAttribute("id");
         if (! document.getElementById(id)) {
            var this_fid = messages[i].getAttribute("franchise_id");
            var to_fid = messages[i].getAttribute("to");
            if (to_fid != null) {
               // don't display it to folks who haven't logged in.
               if (typeof franchise_id == "undefined") {
                  // displayMessages++;
                  continue;
               }
               // don't display it unless i'm the sender or receiver!
               if (to_fid != franchise_id && this_fid != franchise_id) {
                  // displayMessages++;
                  continue;
               }
            }
            var message = messages[i].getAttribute("message");
            var posted = messages[i].getAttribute("posted");
            var new_row = document.createElement("TR");
            new_row.setAttribute("id", id);
            new_row.setAttribute("title", "Posted: " + posted);
            var make_it_bold = false;
            if (to_fid != null) {
               make_it_bold = true;
            }
            var by_cell = document.createElement("TD");
            by_cell.innerHTML = (make_it_bold ? "<b>" : "") + franchiseDatabase['fid_' + this_fid].name + (make_it_bold ? "</b>" : "");
            new_row.appendChild(by_cell);
            var message_cell = document.createElement("TD");
            message_cell.innerHTML = (make_it_bold ? "<b>" : "") + message + (make_it_bold ? "</b>" : "");
            new_row.appendChild(message_cell);
            table_body[0].insertBefore(new_row, second_row);
            if (typeof franchise_id != "undefined" && this_fid != franchise_id) {
               newMessage++;
            }
         }
         messagesDisplayed++;
      }

      body_rows = table_body[0].getElementsByTagName("TR");
      for (var i = body_rows.length; i > displayMessages + 1; i--) {
         if (body_rows[i] && body_rows[i].getAttribute("id")) {
            table_body[0].deleteRow(i);
         }
      }


      body_rows = table_body[0].getElementsByTagName("TR");
      for (var i = 1; i < body_rows.length; i++) {
         if (body_rows[i] && body_rows[i].getAttribute("id") && body_rows[i].getAttribute("id") == "loadingchatdata") {
            table_body[0].deleteRow(i);
         }
      }
   } else {
      while (body_rows.length > 2) {
         table_body[0].deleteRow(1);
         body_rows = table_body[0].getElementsByTagName("TR");
      }
   }

   body_rows = table_body[0].getElementsByTagName("TR");
   for (var i = 1; i < body_rows.length; i++) {
      var this_class = "eventablerow";
      if (i % 2 == 1) {
         this_class = "oddtablerow";
      }
      body_rows[i].setAttribute("className", this_class);
      body_rows[i].setAttribute("class", this_class);
   }
   messages = null;
   if (newMessage >= 1 && newMessage <= 2) {
      play_audio_clip("ohoh", "chat_audio_clip");
      setTimeout("document.chat.chat.focus()", 100);
   }
}

function toggle_chat_audio () {
   var icon = document.getElementById("chat_audio_icon");
   if (icon.src == baseURLStatic + "/sound-off-16x16.png") {
     icon.src = baseURLStatic + "/sound-16x16.png";
     icon.title = "Chat Audio On";
     icon.alt = "Chat Audio On";
     leagueAttributes['voice'] = 'david';
   } else {
     icon.src = baseURLStatic + "/sound-off-16x16.png";
     icon.title = "Chat Muted";
     icon.alt = "Chat Muted";
     leagueAttributes['voice'] = '';
   }
}

function set_up_double_click_events () {
   var tables = document.getElementsByTagName("TABLE");
   for (var i = 0; i < tables.length; i++) {
      var thisClass = tables[i].getAttribute("className");
      var thisId = tables[i].getAttribute("id");
      if (! thisClass || thisClass.length == 0) {
         thisClass = tables[i].getAttribute("class");
      }
      if (thisClass && (thisClass.indexOf("report") >= 0 || thisClass.indexOf("playoffbracket") >= 0)) {
         var captions = tables[i].getElementsByTagName("CAPTION");
         for (var j = 0; j < captions.length; j++) {
            if (typeof moduleExpand == "undefined" || moduleExpand == 'Doubleclick') {
               captions[j].ondblclick=function() {
                  toggle_display(this.parentNode.getAttribute("id"));
               }
            } else if (typeof moduleExpand != "undefined" && moduleExpand == 'PlusMinus') {
               // add the + sign here...
               var span = document.createElement("span");
               span.setAttribute("className", "module_expand");
               span.setAttribute("class", "module_expand");
               span.setAttribute("style", "visibility: visible;");
               span.setAttribute("href", "javascript:void(0);");
               span.onclick=function() {
                  toggle_display(this.parentNode.parentNode.getAttribute('id'));
               }
               span.innerHTML = "[-]";
               captions[j].insertBefore(span, captions[j].firstChild);
               // alert("innerHTML: " + captions[j].innerHTML);
           }
         }
      }
   }
}

function updateVisitTracker () {
   var visit_tracker = document.getElementById("visit_tracker");
   if (visit_tracker) {
      var url = baseURLDynamic + "/" + year + "/visit?L=" + league_id + "&random=" + get_random_string();
      visit_tracker.src = url;
      url = null;
   }
}

function updateOnlineStatus () {
   var url = xmlBaseURL + league_id + '_visitors.xml?random=' + get_random_string();
   try {
      makeHttpRequest(url, 'parseVisitorsXML', 1);
   } catch (e) {
      url = window.location.protocol + "//" + window.location.host + "/fflnetdynamic" + year + "/" + league_id + '_visitors.xml?random=' + get_random_string();
      makeHttpRequest(url, 'parseVisitorsXML', 1);
   }
   url = null;
}

function removeListItem() {
   var sourceList = document.getElementById("source_list");
   var destinationList = document.getElementById("destination_list");
   var index = destinationList.selectedIndex;
   if (index == -1) {
     alert("You must first select the item to remove.");
   } else {
      if (sourceList) {
         sourceList.options[sourceList.length] = new Option(destinationList[destinationList.selectedIndex].text, destinationList[destinationList.selectedIndex].value);
      }
      destinationList.options[index] = null;
      if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) <= 4) {
         history.go(0);
      }
   }
}

function addListItems() {
   var sourceList = document.getElementById("source_list");

   while (sourceList.selectedIndex >= 0) {
      addListItem();
   }
}

function removeListItems () {
   var destinationList = document.getElementById("destination_list");
   while (destinationList.selectedIndex >= 0) {
      removeListItem();
   }
}

function addListItem() {
   var sourceList = document.getElementById("source_list");
   var destinationList = document.getElementById("destination_list");

   if (sourceList.selectedIndex >= 0) {
      var holdMultiple = sourceList.multiple;
      sourceList.multiple = false;
      var itemName = sourceList[sourceList.selectedIndex].text;
      var itemValue = sourceList[sourceList.selectedIndex].value;
      destinationList.options[destinationList.length] = new Option(itemName, itemValue);
      sourceList.options[sourceList.selectedIndex] = null;
      sourceList.multiple = holdMultiple;
      if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) <= 4) {
         history.go(0);
      }
   }
}

function moveItemUp (times) {
   var destinationList = document.getElementById("destination_list");
   if (destinationList.selectedIndex >= 0) {
      if (destinationList.selectedIndex <= times) {
         times = destinationList.selectedIndex;
      }
      for (var i = 0; i < times; i++) {
         moveItem(true);
      }
   }
}

function moveItemDown (times) {
   var destinationList = document.getElementById("destination_list");
   if (destinationList.selectedIndex >= 0) {
      if (destinationList.options.length - destinationList.selectedIndex <= times) {
         times = destinationList.options.length - destinationList.selectedIndex - 1;
      }
      for (var i = 0; i < times; i++) {
         moveItem(false);
      }
   }
}

function moveItem (moveUp) {
   var destinationList = document.getElementById("destination_list");
   var index = destinationList.selectedIndex;
   if (index==-1) {
      alert("You must first select the item to reorder.");
   } else {
      var holdMultiple = destinationList.multiple;
      destinationList.multiple = false;
      var newIndex = index+( moveUp ? -1 : 1);
      if (newIndex<0) newIndex=destinationList.length-1;
      if (newIndex>=destinationList.length) newIndex=0;
      var oldVal = destinationList[index].value;
      var oldText = destinationList[index].text;
      destinationList[index].value = destinationList[newIndex].value;
      destinationList[index].text = destinationList[newIndex].text;
      destinationList[newIndex].value = oldVal;
      destinationList[newIndex].text = oldText;
      destinationList.selectedIndex = newIndex;
      destinationList.multiple = holdMultiple;
      if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) <= 4) {
         history.go(0);
      }
   }
}

function selectAllItems () {
   var destinationList = document.getElementById("destination_list");
   var itemsSelected = false;
   for (var i = 0 ; i < destinationList.length; i++) {
      destinationList.options[i].selected = true;
      itemsSelected = true;
   }
   return itemsSelected;
}

function Player (id, name, position, team, times_available, bye_week, formatted_salary) {
   this.id = id;
   this.name = name;
   this.position = position;
   this.team = team;
   this.times_available = times_available;
   this.bye_week = bye_week;
   this.formatted_salary = formatted_salary;
}

function play_audio_clip(file_name, domid) {

   if (typeof domid == "undefined") {
      domid = "draft_audio_clip";
   }
// alert("inside play_audio_clip with file_name " + file_name + ", and dom_id = " + domid);
   var sound = document.getElementById(domid);
   if (sound && leagueAttributes['voice'] != "none" && leagueAttributes['voice'] != '') {
      var sound_url = "http://www.myfantasyleague.com/sounds/" + leagueAttributes['voice'] + "/" + file_name + ".wav";
      if (typeof current_embed != "undefined") {
         document.body.removeChild(current_embed);
         current_embed = null;
      }
// alert("got a sound_url of " + sound_url);
      if ((current_embed = document.createElement("embed")) == null) {
         alert("cannot play audio!");
      } else {
         current_embed.setAttribute("width","0");
         current_embed.setAttribute("height","0");
         current_embed.setAttribute("hidden","true");
         current_embed.setAttribute("autostart","true");
         current_embed.setAttribute("autoplay","true");
         current_embed.setAttribute("src", sound_url);
         document.body.appendChild(current_embed); 
      }
   }
}

function check_all_checkboxes (object) {
   var isChecked = object.checked;
   var form = object.form;
   for (var i = 0; i < form.elements.length; i++) {
       if (form.elements[i].type == "checkbox") {
          form.elements[i].checked = isChecked;
       }
   }
}

function showhints (this_form) {
   // given a form, show the hints for it.
   var this_id = this_form.id;
   for (var i in mfl_hints[this_id]) {
      var element = document.getElementById(i);
      if (element) {
          var hintSpan = document.createElement("span");
          hintSpan.setAttribute("class", "reportnavigation");
          var hintWord = document.createElement("span");
          hintWord.setAttribute("class", "reportnavigationheader");
          hintWord.appendChild(document.createTextNode(hintword));
          var newHint = document.createTextNode(mfl_hints[this_id][i]);
          hintSpan.appendChild(hintWord);
          hintSpan.appendChild(newHint);
          element.parentNode.appendChild(hintSpan);
      }
      // alert(i + ": " + mfl_hints[this_id][i]);
   }
}

function enable_elements (this_form) {
   // given a form, enable/disable the appropriate child elements
   // based on the status of the various parent_enabled and parent_disabled elements.
   for (var i = 0; i < this_form.elements.length; i++) {
      if (this_form.elements[i].type == "radio" || this_form.elements[i].type == "checkbox") {
         var currentClass = this_form.elements[i].getAttribute("class");
         if (currentClass == null || currentClass.length == 0) {
            currentClass = this_form.elements[i].getAttribute("className");
         }
         if (currentClass && currentClass.length > 0) {
            if (currentClass.indexOf("parent_disabled") >= 0 || currentClass.indexOf("parent_enabled") >= 0) {
               // we've got parent fields - set up event handlers for these fields to enable/disable children appropriately.
               this_form.elements[i].onclick=function() {
                  var currentClass = this.getAttribute("class");
                  if (currentClass == null || currentClass.length == 0) {
                     currentClass = this.getAttribute("className");
                  }
                  if (currentClass && currentClass.length > 0) {
                     if (currentClass.indexOf("parent_disabled") >= 0) {
                        // for all children with the same suffix, disable them.
                        var suffix = currentClass.substr(currentClass.indexOf("parent_disabled") + "parent_disabled".length, 1);
                        for (var i = 0; i < this.form.elements.length; i++) {
                           var newClass = this.form.elements[i].getAttribute("class");
                           if (newClass == null || newClass.length == 0) {
                              newClass = this.form.elements[i].getAttribute("className");
                           }
                           if (newClass && newClass.length > 0 && newClass.indexOf("child" + suffix) >= 0) {
                              this.form.elements[i].disabled = true;
                           }
                        }
                     }
                     if (currentClass.indexOf("parent_enabled") >= 0) {
                        // for all children with the same suffix, enable them.
                        var suffix = currentClass.substr(currentClass.indexOf("parent_enabled") + "parent_enabled".length, 1);
                        for (var i = 0; i < this.form.elements.length; i++) {
                           var newClass = this.form.elements[i].getAttribute("class");
                           if (newClass == null || newClass.length == 0) {
                              newClass = this.form.elements[i].getAttribute("className");
                           }
                           if (newClass && newClass.length > 0 && newClass.indexOf("child" + suffix) >= 0) {
                              this.form.elements[i].disabled = false;
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

function make_editable (object) {
   var current_quote = object.innerHTML;
   var parentNode = object.parentNode;
   var inputNode = document.createElement("input");
   inputNode.setAttribute("type", "text");
   inputNode.setAttribute("size", "50");
   inputNode.setAttribute("maxlength", "200");
   inputNode.setAttribute("value", current_quote);
   inputNode.onblur=save_owner_quote;
   if (parentNode) {
      inputNode.setAttribute("name", parentNode.id);
      parentNode.removeChild(object);
      parentNode.appendChild(inputNode);
   }
}

function save_owner_quote () {
   var parentNode = this.parentNode;
   var newQuote = this.value;

   var this_quote_type = this.name;

   var newTextNode = document.createTextNode(newQuote);
   parentNode.removeChild(this);
   parentNode.appendChild(newTextNode);

   // save the new text here!
   var url = window.location.protocol + "//" + window.location.host + "/" + year + "/save_setting?L=" + league_id + "&TITLE=";
   if (typeof recapWeek != "undefined") {
      url += "RECAP_QUOTE&W=" + recapWeek;
   } else if (typeof previewWeek != "undefined") {
      url += "PREVIEW_QUOTE&W=" + previewWeek;
   } else {
      url += this_quote_type;
   }
   url += "&VALUE=" + encodeURIComponent(newQuote);
   makeHttpRequest(url);
}

function FC_Rendered(domId){
   if (domId == "starterPointsChart") {
      document.position_chart.disabled = false;
      positionChartLoaded = true;
      var chartObj = getChartFromId(domId);
      chartObj.setDataXML(generatePositionXML());
   } else if (domId == "weeklyResultsChart") {
      document.weekly_results.disabled = false;
      weeklyResultsChartLoaded = true;
      var chartObj = getChartFromId(domId);
      chartObj.setDataXML(generateWeeklyResultsXML());
   } else if (domId == "pointsAllowedChart") {
      document.points_allowed_chart.disabled = false;
      pointsAllowedChartLoaded = true;
      var chartObj = getChartFromId(domId);
      chartObj.setDataXML(generatePointsAllowedXML());
   }
   return true;
}


window.onload=function() {
   if (document.getElementById("body_home")) {
      set_up_double_click_events();
      if (document.getElementById("homepagetabs") && document.getElementById("tab2")) {
         new Sortables($('homepagetabs'), {
            onComplete: function() {
               var parent = this.element.parentNode;
               var saveIt = false;
               if (typeof currentTabOrder == "undefined") {
                  currentTabOrder = new Array();
                  for (var i = 0; i < parent.getChildren().length; i++) {
                     currentTabOrder[i] = i;
                  }
               }
               for (var i = 0; i < parent.getChildren().length; i++) {
                  var seqno = parent.getChildren()[i].id.substr("tab".length);
                  if (currentTabOrder[i] != seqno) {
                     saveIt = true;
                     currentTabOrder[i] = seqno;
                  }
               }
               if (saveIt) {
                  // alert("new tab order would be saved here...");
                  var url = window.location.protocol + "//" + window.location.host + "/" + year + "/save_setting?L=" + league_id + "&TITLE=TABORDER&VALUE=" + currentTabOrder.join();
                  makeHttpRequest(url);
               }
            }
         });
      }
      // window.addEvent('domready', function() {
         // new Sortables('.homepagemodule', {
            // onComplete: function() {
               // alert("what a drag: " + this.element.parentNode.id);
            // }
         // });
      // });
   } else {
      set_focus();
   }
   if (document.getElementById("loadingchatdata")) {
      readMessages(true);
      if (document.getElementById("chat_text_field")) {
         document.getElementById("chat_text_field").onkeypress=function(event) {
            var code = event && event.which ? event.which : window.event.keyCode;
            if (code == 13 || code == 3) {
               postMessage();
               return false;
            }
         }
      }
   }
   if (document.getElementById("visit_tracker") && typeof visitTrackerInterval == "undefined") {
      visitTrackerInterval = setInterval('updateVisitTracker()', 60 * 1000);
   }
   if (document.getElementById("visitor_count") || document.getElementById("last_visit_0000") || document.getElementById("visitor_list") && typeof onlineStatusInterval == "undefined") {
      updateOnlineStatus();
      onlineStatusInterval = setInterval('updateOnlineStatus()', 60 * 1000);
   }
   if (document.getElementById("body_ajax_ld")) {
      play_audio_clip("welcometoyourlivedraft");
      if (typeof draftStatusInterval == "undefined") {
         draftStatusInterval = setInterval('readDraftStatus()', checkEverySeconds * 1000);
      }
      if (typeof draftLogInterval == "undefined") {
         // initialize the draft log...
         readDraftLog();
         draftLogInterval = setInterval('readDraftLog()', checkEverySeconds * 1000);
      }
      load_player(document.pick_form.PLAYER_PICK);
   }
   if (document.getElementById("player_photo")) {
      display_photo(0);
      // only do this on a day other than sunday!
      if ((new Date()).getDay() != 0) {
         setInterval("next_photo()", 2.5 * 1000);
      }
   }
   if (document.forms.length > 0) {
      for (var i = 0; i < document.forms.length; i++) {
         var currentClass = document.forms[i].getAttribute("class");
         if (currentClass == null || currentClass.length == 0) {
            currentClass = document.forms[i].getAttribute("className");
         }
         if (currentClass && currentClass.length > 0 && currentClass.indexOf("enable_elements") >= 0) {
            enable_elements(document.forms[i]);
         }
         if (currentClass && currentClass.length > 0 && currentClass.indexOf("showhints") >= 0) {
            showhints(document.forms[i]);
         }
      }
   }
}

window.onunload=function() {
   if (document.getElementById("visit_tracker")) {
      clearInterval(visitTrackerInterval);
   }
   if (document.getElementById("visitor_count") || document.getElementById("last_visit_0000") || document.getElementById("visitor_list")) {
      clearInterval(onlineStatusInterval);
   }
   if (document.getElementById("body_ajax_ld")) {
      clearInterval(draftStatusInterval);
      clearInterval(draftLogInterval);
   }
}

