/************************************************************
 * Common JavaScript functions for building forum headers   *
 * and footers. The functions in this file are accessed by  *
 * the custom forum JavaScript files.                       *
 *                                                          *
 * Please note that the JavaScript files do not create the  *
 * heirachy links (e.g. Jedi Council Forums » The Star      *
 * Wars Films » Attack of the Clones »). This must be done  *
 * in the forum header IMMEDIATELY AFTER the <script> tags. *
 ************************************************************/ 




var forumName = '';
var numberOfForumLinks = 0;
var numberOfForumMods = 0;
var forumLinks = '';
var modLinks = '';
var currentForumLink = 1;
var currentMod = 1;
var firstPass = true;
var PMLinkForumName = '';
var modOnline = '';
var forumLinksPosition = 1;
var modLinksPosition = 1;
var useStylesheet = false;
var announcement = '';
var numberOfAnnouncements = 0;
var currentAnnouncement = 1;
var epiiibool = 0;
var scrollx = 0;

//initialise global variables. Do not edit these values.



//outputs the custom header or footer for the forum
//(called from individual forum JS files).
function outputForumHeaderFooter()
{

  
	//if the header is to be output this time...
	if (firstPass)
	{

		if (epiiibool != 0)
		{
			outputCountdown();
		}
		
		if (numberOfAnnouncements != 0)
		{
			outputAnnouncements();
		}

		//if the forum links are to be shown in the header...
		if ((forumLinksPosition == 1) || (forumLinksPosition == 3))
		{
			outputForumLinks();
		}
    
		//if the moderator links are to be shown in the header...
		if ((modLinksPosition == 1) || (modLinksPosition == 3))
		{
			outputModLinks();
		}

		//set the firstPass indicator to false, so the footer is output next time	
		firstPass = false;

	}
 	//else if the footer is to be output this time...
	else
	{
		//if the forum links are to be shown in the footer...
		if ((forumLinksPosition == 2) || (forumLinksPosition == 3))
		{
			outputForumLinks();
		}
		//if the mod links are to be shown in the footer...
		if ((modLinksPosition == 2) || (modLinksPosition == 3))
		{
      			outputModLinks();
    		}
  	}
}

function outputCountdown ()
{
	var today = new Date;
	var Future0 = new Date;
	ThisYear = 2005;

	var FutureMonth = 4;
	var FutureDate = 19;
	var faraevent = "Revenge of the Sith";


	Future0.setMonth(FutureMonth, FutureDate);
	Future0.setFullYear(ThisYear);

	today1 = Date.parse(today);
	future1 = Date.parse(Future0);
	secleft = future1 - today1;
	miliday = 24 * 60 * 60 * 1000;
	daysleft = secleft/miliday;
	daysleftint = Math.round(daysleft);

	if (daysleftint > 0)
	{
		document.write('Only ', daysleftint, ' days left until ', faraevent , '!' + '</td></tr><tr 					align=left valign=middle><td class=MainMenuRow>');
	}
}

function outputAnnouncements()
{
	//outputs the Announcements
	document.write(announcement + '</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function addAnnouncement(announcementText)
{
	//builds the announcements in HTML one at a time (called from individual forum JS files).

	if (scrollx == 0)
	{
		announcement = announcement + '<b>Announcement: </b>' + announcementText;
	}
	else 
	{
		announcement = announcement + '<marquee behavior=slide><b>Announcement: </b>' + announcementText + '</marquee>';
	}	
	//if more announcements need to be built...
	if (currentAnnouncement < numberOfAnnouncements)
	{
		// output a break line
		announcement = announcement + '<br>';

		//increment the announcement number
		currentAnnouncement++;
	}
	
}


function outputForumLinks()
{
    //outputs the forum links
  document.write(forumName+' Forum Links:'+forumLinks+
                 '</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function outputModLinks()
{
    //outputs the mod links
    
  document.write('Forum Moderators:');
  document.write(modLinks);
  document.write('</td></tr><tr align="left" valign="middle"><td class="MainMenuRow">');
}




function addForumLink(forumLinkURL, forumLinkText)
{
	//builds the forum links HTML one at a time (called from individual forum JS files).

	//if this is the first set of passes of this function...
	if (firstPass)
	{
		forumLinks = forumLinks+' <a class="MainMenuLink" href="'+forumLinkURL+'">'+
			forumLinkText+'</a>';
			//build the link for this forum link

		//if more forum links need to be built...
		if (currentForumLink < numberOfForumLinks)
		{
			//output a seperating pipe
			forumLinks=forumLinks+' |';

			//increment the forum link number
			currentForumLink++;
		}
	}
}




function addMod(modUsername, modUserID, modUserTitle)
{
	//builds the link for one moderator at a time (called from individual forum JS files).

    	//if this is the first set of passes of this function...
	if (firstPass)
	{
		//if the mod stylesheet is to be used...
    		if (useStylesheet == true)
		{
     			modClass='mod-'+modUserID;
        		//set the modClass to use the appropriate style in the mod stylesheet
		}
    		else
    		{
      			modClass='MainMenuLink';
        		//set the modClass to use the default MainMenuLink class
    		}


		modLinks=modLinks+' <a href="http://boards.theforce.net/user.asp?usr='+modUserID+
			'" title="'+modUserTitle+'" class="'+modClass+'">'+modUsername+
                	'</a>';
		//build the link for this mod, and append it onto the string containing the rest of them

		//if more mods still need to be built...
		if (currentMod < numberOfForumMods)
		{
      			modLinks=modLinks+' |';
        		//output a seperating pipe

      			currentMod++;
        		//increment the mod number
		}
	}
}


function addModo(modUsername, modUserID, modUserTitle)
{
	//builds the link for one moderator at a time (called from individual forum JS files).

	//if this is the first set of passes of this function...
	if (firstPass)
	{
		//if this is the first mod to be built...
		if (currentMod == 1)
		{
			PMLinkForumName = strReplaceAll(forumName,' ','+');
			//Swap the spaces in the forum name for +s so that it is compatible with the URL syntax

			PMLinkForumName = strReplaceAll(PMLinkForumName,'&','and');
			//Swap any &s in the forum name for 'and's so that it is compatible with the URL syntax
		}

		//if the mod stylesheet is to be used...    
		if (useStylesheet == true)
		{
      			modClass='mod-'+modUserID;
        		//set the modClass to use the appropriate style in the mod stylesheet
		}
		else
		{
			modClass='MainMenuLink';
			//set the modClass to use the default MainMenuLink class
		}


		modOnline = strReplaceAll(modUsername,' ','%20');
		//Swap the spaces in the moderator name for %20s so that it is compatible with the URL syntax
    
		modLinks=modLinks+' <a href="http://boards.theforce.net/pm_send.asp?usr='+modUserID+
			'&message_subject='+PMLinkForumName+
			'+Forum&message_body=Please+give+details+of+the+problem+here"'+
			' title="'+modUserTitle+'" class="'+modClass+'">'+modUsername+
			'</a> <a class="MainMenuLink" href="http://boards.theforce.net/users_online.asp?alphasort='+modOnline+
			'">(Online?)</a>';
                      
		//build the link for this mod, and append it onto the string containing the rest of them

		//if more mods still need to be built...
		if (currentMod < numberOfForumMods)
		{
			modLinks=modLinks+' |';
			//output a seperating pipe

			currentMod++;
			//increment the mod number

		}
	}
}

function addModl(modUsername, modUserID, modUserTitle)
{
	//builds the link for one moderator at a time (called from individual forum JS files).

	//if this is the first set of passes of this function...
	if (firstPass)
	{
		//if this is the first mod to be built...
		if (currentMod == 1)
		{
			PMLinkForumName = strReplaceAll(forumName,' ','+');
			//Swap the spaces in the forum name for +s so that it is compatible with the URL syntax

			PMLinkForumName = strReplaceAll(PMLinkForumName,'&','and');
			//Swap any &s in the forum name for 'and's so that it is compatible with the URL syntax
		}

		//if the mod stylesheet is to be used...    
		if (useStylesheet == true)
		{
      			modClass='mod-'+modUserID;
        		//set the modClass to use the appropriate style in the mod stylesheet
		}
		else
		{
			modClass='MainMenuLink';
			//set the modClass to use the default MainMenuLink class
		}


		modOnline = strReplaceAll(modUsername,' ','%20');
		//Swap the spaces in the moderator name for %20s so that it is compatible with the URL syntax
    
		modLinks=modLinks+' <a href="http://boards.theforce.net/user.asp?usr='+modUserID+ 
                         '" title="'+modUserTitle+'" class="'+modClass+'">'+modUsername+'</a>';                         
              
		//build the link for this mod, and append it onto the string containing the rest of them

               //adds the final mods online link... 
                if (currentMod == numberOfForumMods) 
                { 
                         modLinks=modLinks+' | <a class="MainMenuLink"                                                                        href="http://boards.theforce.net/UsersOnline.aspx?mods=yes">(Mods Online)</a>'; 
                } 
  

		//if more mods still need to be built...
		if (currentMod < numberOfForumMods)
		{
			modLinks=modLinks+' |';
			//output a seperating pipe

			currentMod++;
			//increment the mod number

		}
	}
}



function strReplaceAll(string,text,by)
{
	// Replaces text with 'by' in string

	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0))
	{
		return string;
	}
	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0,txtLength)))
	{
		return string;
	}
	if (i == -1)
	{
		return string;
	}

	var newstr = string.substring(0,i) + by;

	if (i+txtLength < strLength)
	{
		newstr += strReplaceAll(string.substring(i+txtLength,strLength),text,by);
	}

	return newstr;
}
