// ========================= Copyright Epistema 2002 ========================
//
// JSQuizzCommon
//
// Javascript core for EpiStudio
//
// $Revision: 1.5 $
//
// $Log: JSQuizzCommon.js,v $
// Revision 1.5  2003/04/22 09:49:51  bertrand
// Change the Page number inside the page as well
//
// Revision 1.4  2003/04/18 16:45:51  bertrand
// no message
//
// Revision 1.3  2003/01/30 09:23:44  bertrand
// no message
//
// Revision 1.2  2002/11/13 14:54:42  bertrand
// ID 58 : Fixed a bug when pressing refresh on the table of content
//
// ==========================================================================


// index :
// -1 = sommaire
// -2 = Introduction


// IE don't know undefined
if (document.all) {
	// IE
	var undefined = null;
}

// -----------------------------------------------------------------------

function IntTo2DigitString(a)
{
  if (a < 10) return "0"+a;
  return a;
}

// -----------------------------------------------------------------------

function RoundToNdp(X, N)
{
	var T = Number('1e' + N);
	return Math.round(X*T)/T;
}

function MyRound(X)
{
	if(X < 1)
	{
		return RoundToNdp(X,2);
	} else if(X < 5) {
		return RoundToNdp(X,1);
	} else {
		return RoundToNdp(X,0);
	}
}

// -----------------------------------------------------------------------
// Get a parameter from a url
// return null if the parameter doesn't exist
// -----------------------------------------------------------------------
function GetParameter(url,param) {
	param += "=";

	var pos1 = url.indexOf(param);
	// check if the parameter exist
	if( pos1 == -1 )
		return null;
	else
		pos1 += param.length;

	var pos2 = url.indexOf("&",pos1);

	if(pos2 == -1) {
		// we want the last parameter
		return url.substr(pos1);
	} else {
		return url.substring(pos1,pos2);
	}
}

// ----------------

function GetQuizz_Init() {
	var url = window.parent.location.toString();
	return unescape( GetParameter( url, "quizz_init") );
}

// =======================================================================
// QuizzManager
// =======================================================================

function PageData() {
	this.Title = 0;
	this.Name = 0;
	this.bIsImage = false;
}

function WordData() {
	this.Mot = "";
}

// -----------------------------------------------------------------------
//
//           -----=====~~~~~{ Object QuizzManager }~~~~~=====-----
//
// -----------------------------------------------------------------------

function QuizzManager()
{
	this.index = -2;

	this.bLoaded = false;

	// The following values are always valid, they are
	// hardcoded in _ManagerFrame
	this.QuizzFrame = null;
	this.tabPage = new Array();

	this.motsCount = 0;
	this.mots = new Array();

	this.IsEpistudio = true;
	this.IsEasyquizz = false;


	this.pageString = 'Page ';
	this.outOfString = ' sur ';
	this.tocString = 'Sommaire';

	// ----------------------------------------------------------- //

	// functions :


	this.SetCookie = function(sName, sValue)
	{
		document.cookie = sName + "=" + escape(sValue);
	};

	// Retrieve the value of the cookie with the specified name.
	this.GetCookie = function(sName)
	{
		// cookies are separated by semicolons
		var aCookie = document.cookie.split("; ");
		for (var i=0; i < aCookie.length; i++)
		{
			// a name/value pair (a crumb) is separated by an equal sign
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0])
				return unescape(aCrumb[1]);
		}

		// a cookie with the requested name does not exist
		return null;
	};

	// This function checks the existence of a cookie for this quizz
	// If the cookie is non existent, then we create one
	// If the cookie is existent, there has been a refresh done, so we
	// get back the runtime values of the quizz that would have been
	// saved during the unload
	this.InitManager = function()
	{
		var SavedIndex;

		SavedIndex = this.GetCookie("CurrentPage");

		if (SavedIndex != null)
		{
			// There's a cookie present, but we check the current page
			// in QuizzFrame. If it is the introduction,
			// then we remove the cookie, as it's supposedly a new session.
			if (this.QuizzFrame != null)
			{
				var pageName = String(this.QuizzFrame.location);
				var lastPosSlash = pageName.lastIndexOf("/");
				pageName = pageName.substring(lastPosSlash + 1);

				if (pageName == "_introduction.htm")
				{
					this.SetCookie("expires", "Thu, 01-Jan-70 00:00:01 GMT");
					this.bLoaded = true;
					return;
				}
			}

			this.index = Number(SavedIndex);

			var pageName = String(this.QuizzFrame.location);
			var lastPosSlash = pageName.lastIndexOf("/");
			pageName = pageName.substring(lastPosSlash + 1);

	  	var newPageName = this.GetCurrentLink();

	  	if (newPageName != pageName)
			  this.QuizzFrame.location = newPageName;

			// reset the image correctly
			if (this.index >= 0)
				if (this.tabPage[this.index].bIsImage )
					this.QuizzFrame.document.images[0].src = this.tabPage[this.index].Name;

			// reset the title
			this.ResetTitleAndShowNavigationArrows();
		}

		this.bLoaded = true;
	};

	// This function saves the runtime values of the quizz into a cookie
	this.UnloadManager = function()
	{
		this.SetCookie("CurrentPage", this.index);
		this.QuizzFrame.parent.normalExit();
	};

	this.MoveToPreviousPage = function()
	{
		return this.MoveToPage(this.index);
	};

	this.MoveToNextPage = function()
	{
		return this.MoveToPage(this.index + 2);
	};

	this.MoveToPageName = function(pagename)
	{
		var l = "_sommaire.htm";
		this.index = -1;

		if (pagename == "_sommaire.htm")
		{
			this.index = -1;
			l = "_sommaire.htm";
		}
		else
		{
			for (var i=0; i < this.tabPage.length; i++)
			{
				if (this.tabPage[i].Name == pagename)
				{
					l = pagename;
					this.index = i;
					break;
				}
			}
		}

		this.ResetTitleAndShowNavigationArrows();

		return l;
	};


	this.MoveToPage = function(p) // 0 = TOC , 1 = first page
	{
		var l = "";

		this.index = p - 1;

		if (this.index < -2)
			this.index = -2;

		if (this.index == -1)
			l = "_sommaire.htm";
		else if (this.index == -2)
			l = "_introduction.htm";
		else
		{
			var limit = this.tabPage.length;

			if (this.index >= limit)
				this.index = limit - 1;

			if (! this.tabPage[this.index].bIsImage )
				l = this.tabPage[this.index].Name;
			else
				l = "_image.htm?index=" + this.index;
		}

		this.ResetTitleAndShowNavigationArrows();

		return l;
	};

	this.GetCurrentLink = function()
	{
		var l = "";

		if (this.index == -1)
			l = "_sommaire.htm";
		else if (this.index == -2)
			l = "_introduction.htm";
		else
		{
			var limit = this.tabPage.length;
			if( this.index < limit)
			{
				if (this.tabPage[this.index].bIsImage )
					l = "_image.htm?index=" + this.index;
				else
					l = this.tabPage[this.index].Name;
			}
		}

		return l;
	};

	this.GetImageName = function(index)
	{
		var l = "";

		if (! this.tabPage[index].bIsImage )
			l = "";
		else
			l = this.tabPage[index].Name;

		return l;
	};

	this.GetCurrentTitle = function()
	{
		var l = "&nbsp;";

		if (this.index >= 0)
		{
			l = this.tabPage[this.index].Title;

			if (l.length != 0)
				l = l;
			else
				l = "&nbsp;";
		}

		if (this.index == -1)
		{
			l = this.tocString;
		}

		return l;
	};


	this.GetCurrentPageNumber = function()
	{
		var l = "&nbsp;";

		if (this.index >= 0)
		{
			if (l.length != 0)
				l = this.pageString + (this.index + 1) + this.outOfString + this.tabPage.length;
			else
				l = "&nbsp;";
		}

		return l;
	};

	this.IsLeftNavVisible = function()
	{
		if (this.index == -2)
			return "hidden";
		else
			return "visible";
	};

	this.IsRightNavVisible = function()
	{
		if (this.index == this.tabPage.length - 1)
			return "hidden";
		else
			return "visible";
	};

	this.SetPage = function(PageNumber, Name, Title, bIsImage)
	{
		this.tabPage[PageNumber] = new PageData();
		this.tabPage[PageNumber].Name = Name;
		this.tabPage[PageNumber].Title = Title;
		this.tabPage[PageNumber].bIsImage = bIsImage;
	};

  this.InsertWord = function(Mot)
	{
		this.mots[this.motsCount] = new WordData();
		this.mots[this.motsCount].Mot = Mot;

		this.motsCount++;
	};

	this.ResetTitleAndShowNavigationArrows = function()
	{
		if(window.parent.frames['managerframe'].document.getElementById("flechegauchebas") != null)
		{
			window.parent.frames['managerframe'].document.getElementById("flechegauchebas").style.visibility = this.IsLeftNavVisible();
		}

		if(window.parent.frames['managerframe'].document.getElementById("flechedroitebas") != null)
		{
			window.parent.frames['managerframe'].document.getElementById("flechedroitebas").style.visibility = this.IsRightNavVisible();
		}

		if(window.parent.frames['mainframe'].document.getElementById("flechegauchehaut") != null)
		{
			window.parent.frames['mainframe'].document.getElementById("flechegauchehaut").style.visibility = this.IsLeftNavVisible();
		}

		if(window.parent.frames['mainframe'].document.getElementById("PageHTML") != null)
		{
			window.parent.frames['mainframe'].document.getElementById("PageHTML").innerHTML = this.GetCurrentPageNumber();
		}

		if(window.parent.frames['mainframe'].document.getElementById("flechedroitehaut") != null)
		{
			window.parent.frames['mainframe'].document.getElementById("flechedroitehaut").style.visibility = this.IsRightNavVisible();
		}
	};

	this.renderKeyWord = _Quizz_Manager_renderKeyWord;
	this.RenderNode =    _Quizz_Manager_RenderNode;
}

