/**
 * JavaScript: PageBack handling scripts. 
 * @version: 1.0
 * @author: Buddhika Nalin Fernando
 * Date created: 28/05/2007
 * Date modified: 28/05/2007
 * Copyright: Codegen International (Pvt) Ltd.
 */

/**
 * How to use this js:
 * 		Each Page should be given a unique name.
 *		At each Page an array with the name g_aBackStopped should be 
 * 			defined and it should be filled with the restrcted page
 *			Names. If the Last Page have a name in this array the 
 *			browser will be forwarded.
 */

/**
 * Stops Backing to the Page.
 * This function loops(starts when page got active after a back button click) 
 * and checks the StablePage Cookie. If the StablePage is in the list of 
 * restricted Pages, browser is forced to branch forward.
 * @param   p_sLoadingPage       	Name of the CurrentPage.<br>
 */

var g_aBackStopped;

function stopBack( p_sLoadingPage, p_asBackStoppedPages )	
{
	g_aBackStopped = p_asBackStoppedPages;
	loopBack( p_sLoadingPage );
}

function loopBack( p_sLoadingPage )
{
	var sLastPage = getCookie('StablePage');
	if ((navigator.userAgent.indexOf("Opera")>=0)||
		 (navigator.userAgent.indexOf("MSIE")<0))
	{ 
		setTimeout('loopBack("'+p_sLoadingPage+'")',200);
	}
//	alert('Cookie='+sLastPage );
//	writeLog(sLastPage);
	if(sLastPage==p_sLoadingPage)
	{
		return;
	}
	if(checkBackStopped(sLastPage))
	{	
		history.go(+1);
	}
	else
	{
		//alert('StablePage='+p_sLoadingPage );
		setCookie('StablePage',p_sLoadingPage,1);
	}
}

function initBack(p_sLoadingPage)
{
//	alert('StablePage='+p_sLoadingPage );
	setCookie('StablePage',p_sLoadingPage,1);
//	writeLog(p_sLoadingPage);
}

function checkBackStopped(p_sLastPage)
{
	var iPageIndex = 0;
	var iPageCount = g_aBackStopped.length;
	
	for(;iPageIndex<iPageCount;iPageIndex++)
	{
		if(g_aBackStopped[iPageIndex]==p_sLastPage)
		{
			return true;
		}
	}
	return false;
}

function writeLog(p_sText)
{
	var oTd = document.getElementById('Data');
	oTd.innerHTML=oTd.innerHTML+' '+p_sText;
}
