// Many thanks to Andrew Burnett for his advice. Please see www.logicarchitect.co.uk for a POWERFUL EXCEL SYSTEMS DEVELOPMENT TOOLSET.

var DescriptionVisible=false;
var DoNothing=false;
var Itemfound=false;

function getItem(itemId)
{
Itemfound=false;
 if (document.getElementById) 
		{
		if(document.getElementById(itemId)!= null) 
			{
			Itemfound=true;
			return document.getElementById(itemId);
			}
		else 
			{
		return false;
			}
		}
	if (document.all) {Itemfound=true; return document.all[itemId];}
	return false;
 }

// ----------------------------------------------------------------------------------- //

function restoreTags(text0)
		{
//Restores tags that have been HTML escaped.
//Handles pairs of matched tags or single tags with a closing /.
//Restores the <> characters round the tag and pairs of single/double quotes within
//but no other special characters are converted.
//Warning - if it looks like a tag, it is treated as a tag!

 while (true) {
  var text1 = text0.replace(/&lt;(([a-z]+)( +[a-z]+=(&quot;|&#39;).*?\4)*? *)&gt;(.*?)&lt;\/\2&gt;/ig,'<$1>$5</$2>');
  if (text1 == text0) break;
  text0 = text1;
  }
 text0 = text0.replace(/&lt;(([a-z]+)( +[a-z]+=(&quot;|&#39;).*?\4)*? *)\/&gt;/ig,'<$1/>');
 while (true) {
  text1 = text0.replace(/<([a-z]+( +[a-z]+=("|').*?\3)*? +[a-z]+=)&quot;(.*?)&quot;(( +[a-z]+=(&quot;|&#39;).*?\7)*? *\/?)>/ig,'<$1"$4"$5>');
  text1 = text1.replace(/<([a-z]+( +[a-z]+=("|').*?\3)*? +[a-z]+=)&#39;(.*?)&#39;(( +[a-z]+=(&quot;|&#39;).*?\7)*? *\/?)>/ig,"<$1'$4'$5>");
  if (text1 == text0) break;
  text0 = text1;
  }
 return text0;
 		}

// ----------------------------------------------------------------------------------- //

function removeTags(text0)
		{

//Removes tags from a text string.
//Handles pairs of matched tags or single tags with a closing /.
 while (true) {
  var text1 = text0.replace(/<([a-z]+)( +[a-z]+=("|').*?\3)*? *>(.*?)<\/\1>/ig,'$4');
  if (text1 == text0) break;
  text0 = text1;
  }
 text0 = text0.replace(/<[a-z]+( +[a-z]+=("|').*?\2)*? *\/>/ig,' ');
 return text0;
		}

// ----------------------------------------------------------------------------------- //

function ShowDescription(eventID)
{

 var balloon = '<SKIP>';
 var evstatus = ' ';

// Now retrieve the status information from Andrew's database...

//This returns the date in the format yyyy-mm-dd.
//Errors are displayed in the centre of the red border at the bottom of the web page.
 var re = /^E(\d\d)(\d\d)(\d\d)$/;
 if (!re.test(eventID)) {
  getItem('errmsg').innerHTML = 'eventID wrong format ' + eventID;
  }
 else {
  var match = re.exec (eventID);
  var date = '20' + match[1] + '-' + match[2] + '-' + match[3];

//The external file is returned as an object Events whose properties are
//the field names and each contains an array of values for that field;
//also a property 'index' which is indexed on the file key (date)
//and contains the index number of the record for that key.
  if (window.Events === undefined) {
   getItem('errmsg').innerHTML = 'Events object is not set up';
   }
  else {
   var index = Events.index[date];
   if (index !== undefined) {
    if (Events.Status[index]) {
     evstatus = restoreTags(Events.Status[index]);
     if (Events.Balloon[index]) {
      balloon = restoreTags(Events.Balloon[index]);
      }
     else {
      balloon = '<none>';
  }}}}}

// The retrieved status information is now processed...
	if (balloon=='<SKIP>')	
			{
			/* do nothing */
			return false;
			}
		
	else if (balloon=='<none>')		
			{
			getItem("divdescription");
			if (Itemfound) getItem("divdescription").innerHTML='';
			getItem(eventID);
			if (Itemfound) getItem(eventID).innerHTML=evstatus + '</a>';
			DescriptionVisible=false;
			return false;
			}
	else
			{

			getItem(eventID);
			if (Itemfound) getItem(eventID).innerHTML='<a href="booking.html" class="popup"><img src="files/bubble.gif" width="18" height="15" border="0" title="' + removeTags(balloon) + '" alt="' + removeTags(balloon) + '"/>&nbsp;' + evstatus + '</a>';
			getItem("divdescription");
			if (Itemfound) getItem("divdescription").innerHTML=balloon;
			DescriptionVisible=true; 
			return false;
			}

}
// ----------------------------------------------------------------------------------- //

function InitialiseStatus()
{
// This places the messages onto the web page at load time. CHANGE EVERY TIME EVENTS.HTML is updated

	ShowDescription('E091030');
	ShowDescription('E091031');
	ShowDescription('E091127');
	ShowDescription('E100115');
	ShowDescription('E100212');

	ShowDescription('E100407');
	ShowDescription('E100409');
	ShowDescription('E100410');
	ShowDescription('E100413');
	ShowDescription('E100414');
	ShowDescription('E100417');
	ShowDescription('E100418');
	ShowDescription('E100420');
	ShowDescription('E100421');
	ShowDescription('E100423');
	ShowDescription('E100424');


	getItem("divdescription").innerHTML="";   /*  clear the balloon message window     */

	DescriptionVisible=false;
}

// ----------------------------------------------------------------------------------- //


function MoveDescription(e)
		{
		if (DescriptionVisible)
			{
			if (document.all?true:false)
				{
				getItem("divdescription").style.left = event.clientX + document.documentElement.scrollLeft +10+"px";
				getItem("divdescription").style.top = event.clientY + document.documentElement.scrollTop
+10+"px";
				}
			else
				{
				getItem("divdescription").style.left = e.pageX +10+"px";
				getItem("divdescription").style.top = e.pageY +10+"px";
				}
			getItem("divdescription").style.visibility="visible";
			}
		}

// ----------------------------------------------------------------------------------- //

function HideDescription()
		{
		DescriptionVisible=false;
		getItem("divdescription").style.visibility="hidden";
		getItem("divdescription").style.left="-999px";
		getItem("divdescription").style.width='';
		}

// ----------------------------------------------------------------------------------- //
