// JavaScript Document

function newMsgCheck() {
	// check messages in the background and update menu bar
	
	msgCheck= new XMLHttpRequest ;
	infoURL = 'http://' + self.location.hostname + '/common/msgcheck.php?forum' ;
	
	msgCheck.open("GET",infoURL,false) ;
	msgCheck.send(null) ;
	
	messageCounter = msgCheck.responseText ;

	flag = document.getElementById('msgFlag') ;
	menuLabel = document.getElementById("msgCounter")  ;
	menuCounter = document.getElementById("msgCount") ;
	
	if ( messageCounter == 0 ) {
		menuLabel.innerHTML = '' ;
		flag.innerHTML = '' ;		
	} else {
		if (menuLabel.innerHTML == '') {
			// inbox was empty, so play a sound
			player = document.getElementById("msgAudio") ;
			player.innerHTML = '<embed src="/common/message.wav" type="audio/x-wav" autostart="true" width="0" height="0" enablejavascript="true">' ;
		}
		menuLabel.innerHTML = ' (' + messageCounter + ')' ;
		flag.innerHTML = '<img src="/common/images/newMsg.png" align="right"/>' ;
		menuCounter.value = msgCount ;
	}
	
	
}

function startMsgCheck() {
	// start a countdown, and periodically check new messages, and initialise the menu with the BLUF id
	
	blufid = get_cookie('BLUFforumid') - 10000 ;
	if ( blufid > 0 ) {
		menuid = document.getElementById('blufNo') ;
		menuid.innerHTML = blufid ;
	}
	
	newMsgCheck() ;
	interval = 60 ; // seconds
	
	window.setInterval("newMsgCheck()",interval * 1000) ;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}