
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 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 readMessagesForAlert (restartTimer) {
   var url = xmlBaseURL + league_id + '_chat.xml?random=' + get_random_string();
   try {
      makeHttpRequest(url, 'parseChatXMLForAlert', 1);
   } catch (e) {
      url = window.location.protocol + "//" + window.location.host + "/fflnetdynamic" + year + "/" + league_id + '_chat.xml?random=' + get_random_string();
      makeHttpRequest(url, 'parseChatXMLForAlert', 1);
   }
   if (restartTimer) {
      chatTimer = setTimeout("readMessagesForAlert(true)", checkEverySeconds * 1000 );
   } 
}

function parseChatXMLForAlert (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) { 
      if(lastMessageCheck=="Alert sent") loadpopup('1');
     } else {
      if(lastMessageCheck=="Alert sent") loadpopup('2');
     }
    chatServerTime=lastMessageTime 
    }
   }
}