
function DHTMLSound(surl) {
  document.getElementById('dummyspan').innerHTML="<embed src='"+surl+"' height=0 width=0 autostart=true loop=false>";
  document.chat.chat.blur();
}

// Start cookie functions which will set the Chat Mode Cookie for one week
function saveChatMode(){
var today = new Date();
var expire = new Date();
var cookieName = 'chatModeCookie' + league_id;
expire.setTime(today.getTime() + 1000*60*60*24*7); 
document.cookie=cookieName+"="+chatMode+";expires="+expire.toGMTString();
}

function get_cookie(Name) {
var search = Name + "="
var returnvalue = ""
 if (document.cookie.length > 0) {
  offset = document.cookie.indexOf(search)
  if (offset != -1) {
   offset += search.length
   end = document.cookie.indexOf(";", offset)
   if (end == -1)
    end = document.cookie.length;
   returnvalue=unescape(document.cookie.substring(offset, end))
  }
 }
 return returnvalue;
}

function getChatMode(){
 var cookieName = 'chatModeCookie' + league_id
 chatMode=get_cookie(cookieName)
}

// End Cookie Functions 

function openpopup(whichOne){
 switch(whichOne) {
  case '1' : { alert ("Your presence is requested in the Chat Room.\n\nClick 'OK' then proceed to the Chat Room if you desire."); break; }
  case '2' : { alert ("You have sent an alert.  You can do this only once per chat session.\n\nClick 'OK' and hopefully your alert will summon someone to the Chat Room."); break; }
 }
}

function loadpopup(which){
 clearInterval(chatTimer)
 switch(which) {
  case '1' : { cookieName='alert_received_popped'; break; }
  case '2' : { cookieName='alert_sent_popped'; break; }
  default  : break;
 }
 if (get_cookie(cookieName)==''){
  openpopup(which)
  switch(which) {
   case '1' : { document.cookie="alert_received_popped=yes"; break; }
   case '2' : { document.cookie="alert_sent_popped=yes"; break; }
   default  : break;
  }
 }
 chatTimer = setTimeout("readMessagesForSound(true)", checkEverySeconds * 1000 );
}

function readMessagesForSound (restartTimer) {
   var url = xmlBaseURL + league_id + '_chat.xml?random=' + get_random_string();
   setAlertIcon()
   try {
      makeHttpRequest(url, 'parseChatXMLForSound', 1);
   } catch (e) {
      url = window.location.protocol + "//" + window.location.host + "/fflnetdynamic" + year + "/" + league_id + '_chat.xml?random=' + get_random_string();
      makeHttpRequest(url, 'parseChatXMLForSound', 1);
   }
   if (restartTimer) {
      chatTimer = setTimeout("readMessagesForSound(true)", checkEverySeconds * 1000 );
   } 
}

function PlayDHTMLSound() {
  DHTMLSound(newPost);
}

function parseChatXMLForSound (chatXML) {
   var messages = chatXML.getElementsByTagName("message")
   if(messages.length>0) {
    var lastMessageId = messages[0].getAttribute("id")
    var lastMessageTime = parseInt(lastMessageId)
    var lastMessagePostedBy = messages[0].getAttribute("franchise_id")
    var lastMessageCheck = messages[0].getAttribute("message")
    if(lastMessageTime>chatServerTime) {
     if(lastMessagePostedBy!=franchise_id) { 
      PlayDHTMLSound();
      if(lastMessageCheck=="Alert sent") loadpopup('1');
     } else {
      if(lastMessageCheck=="Alert sent") loadpopup('2');
      if(chatMode=='on') PlayDHTMLSound();
     }
    chatServerTime=lastMessageTime 
    }
   }
}

function togglesound(mode) {
 if(mode=='') {               // If cookie hasn't been set for user then use default mode
  mode=defaultChatMode 
 }

 clearInterval(chatTimer)

 if(mode=='in') {
  readMessagesForSound(true)
  document.getElementById("chat_sound_on").style.display="none"
  document.getElementById("chat_sound_off").style.display="none"
  document.getElementById("chat_sound_in").style.display="block"
  chatMode='in'
 } else {
  if(mode=='on') {
   readMessagesForSound(true)
   document.getElementById("chat_sound_on").style.display="block"
   document.getElementById("chat_sound_off").style.display="none"
   document.getElementById("chat_sound_in").style.display="none"
   chatMode='on'
  } else {
   document.getElementById("chat_sound_on").style.display="none"
   document.getElementById("chat_sound_off").style.display="block" 
   document.getElementById("chat_sound_in").style.display="none"
   chatMode='off'
  }
 }
 saveChatMode()
}

function postMessageAlert() {
 var url = window.location.protocol + "//" + window.location.host + "/" + year + "/chat_save?L=" + league_id + "&MESSAGE=Alert sent";
 makeHttpRequest(url);
 // do this in a quarter of a second to make sure the write has completed
 setTimeout("readMessages(false)", 250);
}

function setAlertIcon() {
 cookieName='alert_sent_popped'
 if(get_cookie(cookieName)==''){
  document.getElementById("chat_alert_on").style.display="block"
  document.getElementById("chat_alert_off").style.display="none"
 } else {
  document.getElementById("chat_alert_on").style.display="none"
  document.getElementById("chat_alert_off").style.display="block"
 }
}

function info_window() {
 var chatWindow = window.open("http://www.habman.com/mfl/sounds/info.html","info_window","height=500,width=450,scrollbars=no,toolbar=no,status=no,resizable=no,menubar=no");
}