/*
 %Description---------------------------------------------------------------

 This is the Temple of Dins common javascript file as adapted for the 
 Jolly Boys Cricket Club.
  
 %Amendment_History---------------------------------------------------------

 Date:    23/05/1997
 Author:  UberPope Raptor Ramjet KSC
 Details: 
 
 Initial version shoddily cobbled together.

 ---------------------------------------------------------------------------
*/

// **************************************************
// Global variables
// **************************************************

var g_domain = 'jollyboyscc';
var g_doman_suffix = 'org.uk';
var g_timerID = 0;
var g_timerCount = 0;


// **************************************************
// Breaks out of enclosing frames
// **************************************************

function breakFrames() {
	if (window != top) {
		top.location.href = location.href
	};
};


// **************************************************
// Breaks out of enclosing frames
// **************************************************

function newBreakFrames() {
	var idx = "0";

	if (top.location.href == "http://www.templeofdin.demon.co.uk/todindex.htm") {
		// Do nothing
		idx = "1";

	} else if (top.location.href == "http://www.templeofdin.demon.co.uk/jollyboys.htm") {
		// Do nothing
		idx = "1";

	} else {
		idx = "0";
	} ;

	// If we've been elcosed in someone elses frame break out.
	if (idx == "0") {
		window.status = "Not from Temple of Din";
		// alert(top.location.href);
		// if (window != top) top.location.href = location.href;
	} else {
		window.status = "From Temple of Din";
	} ;

};

// **************************************************
// Welcome message
// **************************************************

function showWelcomeMessage() {
	var ss1 = new Array(20)
	ss1[0] = "Welcome to the Temple of Din"
	ss1[1] = "Welcome to the Thimble of Dill"
	ss1[2] = "Welcome to the Tadpole of Dung"
	ss1[3] = "Welcome to the Tumbler of Dahl"
	ss1[4] = "Guaranteed free of worthwhile or educational content"
	ss1[5] = "Please stop picking your nose, this is a religious site."
	ss1[6] = "Who ate the last of the chocolade biscuidos ???"
	ss1[7] = "12 electrons were lost whilst loading this page"
	ss1[8] = "Check out the Movie Meister"
	ss1[9] = "Don't blame me.... the Devil made me do it"
	ss1[10] = "Love is the Law, Love under Will"
	ss1[11] = 'Hail Eris !  All hail Discordia ! Praise "Bob" !'
	ss1[12] = "Todays favourite animal is the Cat"
	ss1[13] = "Todays favourite insect is the Walrus"
	ss1[14] = "Help... I'm being held prisoner in your browser's status bar..."
	ss1[15] = "Todays lucky number is 2,145.023"
	ss1[16] = "Todays lucky colour is nine gazelles"
	ss1[17] = "Todays favourite animal is the antelope"
	ss1[18] = "This page is made from hand picked electrons."
	ss1[19] = "This page has been constructed for idiots, by idiots"

	var now = new Date();
	var sec = now.getSeconds();
	var msg = ss1[sec % 19];

	//Show a random message

	window.status=msg;

};

// **************************************************
// Display a random message on a WATK page load.
// **************************************************

function showWATKMessage() {
	var ss1 = new Array(20)
	ss1[0] = "We are the nutters, we come from the sea..."
	ss1[1] = "Walter and the Knobheads, Punk Never Ends"
	ss1[2] = "Walter and the Knobheads, Poulton-Le-Fyldes Premier Punk Band"
	ss1[3] = "Walter and the Knobheads use Dolphin Friendly Tuners"
	ss1[4] = "This page is guaranteed to be free of worthwhile or educational content"
	ss1[5] = "Please stop picking your nose, this is a religious site."
	ss1[6] = "Walter and the Knobheads, Poulton-Le-Fyldes Premier Punk Band"
	ss1[7] = "12 electrons were lost whilst loading this page"
	ss1[8] = "Walter and the Knobheads, Poulton-Le-Fyldes Premier Punk Band"
	ss1[9] = "Don't blame the Knobheads.... the Devil made us do it"
	ss1[10] = "Love is the Law, Love under Will"
	ss1[11] = 'Hail Eris !  All hail Discordia ! Praise "Bob" !'
	ss1[12] = "Todays favourite animal is the Cat"
	ss1[13] = "Todays favourite insect is the Walrus"
	ss1[14] = "Help... I'm being held prisoner in your browser's status bar..."
	ss1[15] = "Todays lucky number is 2,145.023"
	ss1[16] = "Todays lucky colour is nine gazelles"
	ss1[17] = "Todays favourite animal is the Carleton antelope"
	ss1[18] = "This page is made from hand picked electrons."
	ss1[19] = "This page has been constructed for idiots, by idiots"

	var now = new Date()
	var sec = now.getSeconds()
	var msg = ss1[sec % 20]

	//Show a random message

	window.status=msg

};

// **************************************************
// Display a random message on Jolly Boys page load.
// **************************************************

function showJBMessage() {
	var ss1 = new Array(11)
	ss1[0] = 'Hip Hip Hooray, for the Knights of the Jolly "J"'
	ss1[1] = "Love is the Law, Love under Will"
	ss1[2] = 'Hail Eris !  All hail Discordia ! Praise "Bob" !'
	ss1[3] = "Could this be the year the Jolly Boys move off last place ?"
	ss1[4] = "When oh when will victory be ours ?"
	ss1[5] = "Help... I'm being held prisoner in your browser's status bar..."
	ss1[6] = "Todays lucky number is 2,145.023"
	ss1[7] = "Todays favourite animal is the frowl (part frog, part owl... all nuts)"
	ss1[8] = "This page is made from hand picked electrons."
	ss1[9] = "This page has been constructed for idiots, by idiots"
	ss1[10] = "Every Jolly Boy is tested and certified as being gallon ready"

	var now = new Date()
	var sec = now.getSeconds()
	var msg = ss1[sec % 10]

	//Show a random message
	window.status=msg

};


// **************************************************
// Email function
// **************************************************

function mailMe(object,recipient) {
    var em_add = 'mailto:' + recipient + '@' + g_domain + "." + g_domain_suffix;

    object.encoding = 'text/plain';
    object.action = em_add ;
    object.method = 'POST';

};


// **************************************************
// This script checks whether my home server is up 
// by trying to load an image from it.  If it is then 
// redirect to the audio dungeon page.  If not then
// simply show a small "sorry" message.
// **************************************************
function testServerIsUp2() {
//	alert("Setting timeout");
	window.status="Testing for Temple of Din webserver, please wait...";
	g_timerID = setTimeout("testServerCallback()",1000);
}

function testServerCallback() {
	var ImageObject = new Image();
	var Link = "todaudiodungeon.php"

	ImageObject.src= "images/dragon01.gif";

	if (ImageObject.height > 0){
		//alert("Moving page");
		clearTimeout(g_timerID);
		window.location = Link;

	} else if (g_timerCount < 1) {
		// Rety in 5 seconds
		window.status="Temple of Din webserver didn't respond, retrying again in 5 seconds";
		//alert("Temple of Din webserver didn't respond, retrying again in 5 seconds");
		g_timerCount += 1 ;
		window.status="Server down, retrying in 10 seconds";
  	g_timerID = setTimeout("testServerCallback()",5000);

	} else {
		// Counter too large, give up...
		alert("Sorry but the Temple of Din webserver didn't respond, this means it's probably offline");
		clearTimeout(g_timerID);
	};
}

function testServerIsUp1() {
	var ImageObject = new Image();
	var Link = "http://templeofdin.dyndns.org"

	ImageObject.src= "images/dragon01.gif";

	if (ImageObject.height > 0){
		window.location = Link;
	};
}


// This function chooses one of the liks pages
// **************************************************

function viewRandomLinksPage() {

	var now = new Date()
	var sec = now.getSeconds()
	var idx = [sec % 3]

	if (idx == "0")
		{window.location.href = "todlinkshead.htm";target="main frame";}	

	else if (idx == "1")
		{window.location.href = "todlinkseyeandear.htm";target="main frame";}	

	else if (idx == "2")
		{window.location.href = "todlinksutilities.htm";target="main frame";}	

	else if (idx == "666")
		{window.location.href = "mmzombiedream.htm";target="main frame";}	

};

/****************************************************************************
 Random link function
 *****************************************************************************/

function viewRandomLink() {
   var sslinks = new Array(18);
   
   sslinks[0] = "http://www.subgenius.com"
   sslinks[1] = "http://www.rawilson.com"
   sslinks[2] = "http://www.junkpile.demon.co.uk/prin1.htm"
   sslinks[3] = "http://www.deoxy.org/learyraw.htm"
   sslinks[4] = "http://www.anxietyculture.com/"
   sslinks[5] = "http://www.catnoise.co.uk"
   sslinks[6] = "http://www.private-eye.co.uk"
   sslinks[7] = "http://www.mozilla.org"
   sslinks[9] = "http://www.slackware.com"
   sslinks[10] = "http://www.gunsanddope.com/"
   sslinks[11] = "http://www.slashdot.org/"
   sslinks[12] = "http://www.knplogic.co.uk/are_u_mad.html"
   sslinks[13] = "http://www.frozentech.com/content/livecd.php"
   sslinks[14] = "http://chordfind.com/"
   sslinks[15] = "http://www.no35.dial.pipex.com/KillingJoke/Promos.shtml"
   sslinks[16] = "http://www.framleyexaminer.com/"
   sslinks[17] = "http://freespace.virgin.net/paul.renold/shongers_2/launch.htm"
   sslinks[18] = "http://www.subgenius.com"

   var now = new Date();
   var sec = now.getSeconds();
   var link = sslinks[sec % 17];

//   window.location.href = link;
//   top.location=link;
   window.open(link);

};

/****************************************************************************
 Test function
 ****************************************************************************/

function testAlert() {
   alert("At least the javascript got called...");
};

/****************************************************************************
 This function detects IE and gets all pompous and sniffy about it.
 ****************************************************************************/

function detectIE() {
	var MSIE = (navigator.userAgent.indexOf('MSIE') != -1);
	
	var msg1 = "Good grief !  You appear to be using a Microsoft Browser...  " + 
               "Is this because you're at work and you're forced to use it ? " + 			
	           "Or are you some sort of luddite ? or do you just like being " + 
	           "subjectd to continual pop-ups and spyware " + 
	           "infestation ?<br><br>  " +
				  "You're probably also wondering why the page doesn't look very well styled.  " +
				  "That's simply down to the fact that I cannot be bothered putting in all the " +
				  "extra code required to support for a retarded browser that doesn't play " +
				  "nicely by supporting <a href=\"http://www.w3.org/\">W3C</a> standards for " +
				  "<a href=\"http://www.w3schools.com/css/\">CSS</a>.<br><br>" +
				  "All I can say is that I'd strongly suggest you <a href=";
	var url = "http://www.mozilla.org/products/firefox/";
	var msg2 = ">Get Firefox</a> instead !  It'll improve your browsing experience and " + 
	           "make your computer more secure to boot." +
	           "<br><br>And don't say you've not been warned when some malicious site " +
	           "installs a dialler or keyboard logger and you end up with huge bills to pay..." ;
	
	var msg = "<p class=" + '"' + "GetFirefox" + '"' + ">" +msg1 + '"' + url + '"' + msg2 + "</p>";
	
	if (MSIE == false) {
//		document.write('<br>Good... no IE');
	} else {
		document.write(msg);
	};

}

/****************************************************************************
 This function walks the results table and attempts to highlight the next 
 game. 
*****************************************************************************/
function showCurrentFixture() {
	var counter = 0;

	// variable to say we've found the next game;
	var nextGameFound = false;
	
	// If the borwsers DOM supports it we can try to style the current weeks game 
	// a little differently.
	if (document.getElementById) {
		// Get a reference to the results table
		var id ="ResultsTable";
		var table = document.getElementById(id);  

		// Walk cells
		var cells = table.getElementsByTagName("td");  
		
		//window.status = cells.length;
		//alert("Cells " + cells.length);

		for(iCell = 0; iCell < cells.length; iCell++){          
			// Have we already found the next game ?
			if (!nextGameFound) {
				var contents = cells[iCell].innerHTML ;
				
				//window.status = contents;
				
				//alert("Row " + iRow + ", Cell " + iCell + " = " + contents);
				//alert(" Cell " + iCell + " = " + contents);

				//counter = counter + 1;
				
				//if (counter > 30) {
				//	alert("ABORTING....");
				//	return true;
				//}
				
				if (typeof(contents) != 'undefined') {
					// If the cell is a match date it will be in one of two formats either:
					// "dd/mm/yyyy" or "dayname dd/mm/yyyy".  If the contents contain a
					// space we therefore look for the last " " character and extract the 
					// end of the buffer.
					var index = contents.lastIndexOf(" ");
					
					if (index > 0) {
						//var length = contents.length - index;
						//alert("Length " + length);
						contents = contents.slice(index + 1);
					}
					
					//alert("Contents [" + contents + "]");
				
					//if (isDate(contents) != 'moose') {
					if (isDate(contents)) {
						// Is the date this week ?
						//window.status = "It's a date (1) " + contents;
						//alert("It's a date (1) " + contents);
						
						if (isNextGame(contents)) {
							// The date of the game is after today so we've found the 
							// next game and we set a style to highlight it.
							nextGameFound = true;
						
							//cells[iCell].bgcolor = 'yellow';
							//cells[iCell].color = 'green';
							cells[iCell].className = 'NextGame';
							
							cells[iCell].innerHTML = "<span class='NextGame'>" + cells[iCell].innerHTML + "</span>";
							cells[iCell + 4].innerHTML = "<span class='NextGame'>This is the next game:</span><br>" + cells[iCell + 4].innerHTML ;

						}
					}
				}
			}
		}
	}
	
	// Show a random message
	//showJBMessage();

	//	alert("Done");

}

/****************************************************************************
 This function will determine whether a buffer contains a date or not

 n.b. This is a very primitive function that is only designed to work
 with dates held in a fixed "dd/mm/yyyy" format (which is the only format 
 we use !)
*****************************************************************************/

function isDate(datevalue) {
	// For our purposes a date must be in "dd/mm/yyyy" format
	if (datevalue.length != 10) {
		return false;
	}
	
	// Walk the string inspecting a character at a time.
	for (i2 = 0; i2 < datevalue.length; i2++){ 
		var char = datevalue.charAt(i2);
		//window.status = char;

		// Only allow "0" to "9" or "/" characters.
		if (((char < "0") || (char > "9")) && (char != "/")) {
			//alert(char);
			return false;
		}
		

	}
	
	// If we get this far it's probably a date...
	return true;

}

/*
// Test for a date value
function isDate(testDate) { 
	return (new Date(testDate)!='NaN' || 'moose'); 
}
*/

/****************************************************************************
 This function checks whether a given game date is the next game.  It was 
 during the coding of this function that I decided that I hate javascripts 
 date handling :)
*****************************************************************************/

function isNextGame(gameDate) {
	var dtToday  = new Date;
	var date1 = gameDate + "";
	
	// Get parts of date.  NOe the subtraction of 1 from the month due to javascript
	// "interesting" use of 0 for first month (but 1 for first day ?????)
	var gameDay = date1.slice(0,2);
	var gameMonth = date1.slice(3,5) - 1;
	var gameYear = date1.slice(6,10)
	
	// Convert to strings.
	gameDay = gameDay + '';
	gameMonth = gameMonth + '';
	gameYear = gameYear + '';
	
	// Use the horrible javascript date constructor...
	var dtGame  = new Date(gameYear, gameMonth, gameDay, 23, 59, 59);


	//var msg = "Date 1 " + date1 + "\n" + 
	//          "gameDate " + gameDay + "/" + gameMonth + "/" + gameYear + "\n" + 
	//		    "dtToday " + dtToday + "\n" + 
	//		    "dtGame  " + dtGame + "\n" + 
	//		    "Typeof = " + typeof(date1) + "\n\n";

	// Compare game date with today
	if (dtGame < dtToday) {
		//alert (msg + "Game is less");
		return false;

	} else {
		//alert (msg + "Game is greater or equal");
		return true;

	}	
	
}
