// <script>
// Name:		KnowledgeLibrary.js
// Author:		Colin Turner
// Date:		12 Sep 2003
// Purpose:		JavaScript for the Knowledge Library page
//
// Change History:
// Date			Author			Comments
// 03 Oct 2003	Colin Turner	Added setUnapprovedState()
// 08 Oct 2003	Colin Turner	Altered text that appears in search within
//								Re-perform search when inpUnapproved changes state
// 18 Nov 2003	Colin Turner	Added Content Ref search functionality
// 06 Jan 2004	Colin Turner	[Bug 6133] Correct handling of Search Location 
// Feb 2004		Donna Coles		Changes to make generic
//								If content ref entered with leading spaces, page now displays as intended
//								Search within now correctly resolves if 'search unapproved' has been ticked
// 17 Mar 2004	Donna Coles		explictly declare reContentRef variable
// 26 Mar 2004	Donna Coles		removed ref to .elements as breaks Netscape 7
// 24 May 2004	Colin Turner	Allowed CREF search to find unapproved content.
// 23 Aug 2004	Dominic Wood	Changed getResults to read inpUsage and renamed 
// 02 Dec 2004	Colin Turner	Escape search string to allow for quotes
// 14 Nov 2005	Simon Hewerdine	nodeSelected no longer assumes all divs exist.
// 15 Nov 2005	Simon Hewerdine	resizer now uses a very long window for SupportOnline.
//								Incremented file version.
// 09 Jan 2006	Simon Hewerdine	Remove resizer as enclosing iframe now set to height 100%.
// 03 Feb 2006	Simon Hewerdine	Restore resizer for use in ISOnline and My Library.
// 27 Feb 2006	Simon Hewerdine	Check in nodeSelected for existence of fields before setting them

function resizer()
{	
	windowHeight = document.body.offsetHeight;
	if (windowHeight < 246)
	{
	}
	else
	{
	document.getElementById('kl_frameset').style.height=(windowHeight - 245);
	}
}

//Not used yet but may be required later.
function resizeIframe() 
{
	try {
		var numWindowHeight = top.document.body.offsetHeight
		var oFrame = top.document.getElementById('kl_frameset');

		// Get the document within the frame. This is where you will fail with 'permission denied'
		// Note: IE uses 'contentWindow', Opera uses 'contentDocument', Netscape uses either.
		var oInnerDocRight = (top.frames[0].frames.fraRight.contentDocument) ? top.frames[0].frames.fraRight.contentDocument : top.frames[0].frames.fraRight.document;
		var numMaxHeight = oInnerDocRight.body.scrollHeight;

		// Resize the object to the scroll height of the inner document body. We need  
		// to add a 'fudge' factor to get rid of the scroll bar entirely. 25 seems to work.
		if (numMaxHeight > (numWindowHeight - 245))
		{
			if (oFrame.style.height != (numMaxHeight + 25))
				oFrame.style.height = numMaxHeight + 25;
		}
		else
		{
			if (oFrame.style.height != (numWindowHeight - 245))
				oFrame.style.height=(numWindowHeight - 245);
		}
	}
	catch (e) 
	{
	}
}

function showSummary(pbShow)
{
	// Record whether we want summaries
	if(pbShow == true) {
		document.frmKL.inpSummary.value='True';
	}
	else {
		document.frmKL.inpSummary.value='False';
	}
}

function nodeSelected(pstrTaxonomyNode, pstrRootPath, pstrNodePath)
{
	if (pstrRootPath == pstrNodePath) 
	{
		// Hide all except the the Top div
		oElement = document.getElementById('divTop');
		if (oElement) 
		{
			oElement.innerHTML = pstrRootPath;
		}
		oElement = document.getElementById('divCentre');
		if (oElement) 
		{
			oElement.style.display = "none";
		}
		oElement = document.getElementById('divBottom');
		
		if (oElement) 
		{
			oElement.style.display = "none";
		}
	}
	else 
	{
		// Set the top and bottom of the taxonomy and make all visible
		oElement = document.getElementById('divTop');
		if (oElement) 
		{
			oElement.innerHTML = pstrRootPath;
		}
		oElement = document.getElementById('divBottom');
		if (oElement) 
		{
			oElement.innerHTML = pstrNodePath;
		}
		oElement = document.getElementById('divCentre');
		if (oElement) 
		{
			oElement.style.display = "inline";
		}
		oElement = document.getElementById('divBottom');
		if (oElement) 
		{
			oElement.style.display = "inline";
		}
	}
	
	// Clear the filter
	if (document.frmKL.inpSearchString)
		document.frmKL.inpSearchString.value = '';
	
	// Record a search has been performed
	if (document.frmKL.inpSearchPerformed)
		document.frmKL.inpSearchPerformed.value = 'True';

	// Setup the form values
	if (document.frmKL.inpMode)
		document.frmKL.inpMode.value = 'Browse';
	
	if (document.frmKL.inpTaxonomyNode)
		document.frmKL.inpTaxonomyNode.value = pstrTaxonomyNode;
	
	// Submit the form
	//document.frmKL.submit();
}

function searchWithin()
{
	var strPreview = '';
	var strSearchString = document.frmKL.inpSearchString.value.trim();
		
	//extract regular expressionvalue
	var reContentRef = new RegExp(document.frmKL.inpContentRef.value);
				
	if(strSearchString.length == 0) {
	
		// Prevent the search
		alert('Please enter some text to search for.');
		document.frmKL.inpSearchString.focus();
	}
	else if(reContentRef.test(strSearchString)) {
		
		if (document.frmKL.inpUnapproved.value == 'True')
		{	
			strPreview = '&csAction=Preview';
		}
		// Open the tech doc and be done
		viewTechDoc('/_RMVirtual/Includes/CSRedirect.asp?cref=' + strSearchString + strPreview + '&nav=0');
		return false;
	}
	else {
	
		// Setup the form values
		document.frmKL.inpMode.value = 'SearchWithin';
	
		// Record that we have searched
		document.frmKL.inpSearchPerformed.value = 'True';

		// And get the results
		getResults();
	}
}

function setUnapprovedState(pbChecked)
{
	
	if (pbChecked) {
		
		document.frmKL.inpUnapproved.value = 'True';		
	}
	else {
		document.frmKL.inpUnapproved.value = 'False';	
	}
	
}

function getUnapprovedState()
{
	return document.frmKL.inpUnapproved.value;
}

function getResults()
{
	// Convert the form values into a query string
	var strOption, strQueryString
	
	strQueryString = '?mode=' + document.frmKL.inpMode.value;
	strQueryString += '&sNode=' + document.frmKL.inpTaxonomyNode.value;
	strQueryString += '&searchString=' + escape(document.frmKL.inpSearchString.value);
	strQueryString += '&taxonomyid=' + document.frmKL.inpTaxonomyID.value;
	strQueryString += '&taxonomy=' + document.frmKL.inpTaxonomy.value;
	strQueryString += '&getfolders=true';
	
	strOption = document.frmKL.inpSummary.value;
	if (strOption.length > 0) {
		strQueryString += '&summary=' + strOption;
	}
	
	if(document.frmKL.inpUnapproved.value == 'True' ) {
		strQueryString += '&unapproved=true';
	}
	
	//add usage if exists
	if(document.frmKL.inpUsage && document.frmKL.inpUsage.value.length > 0)
	  strQueryString += '&Usage=' + document.frmKL.inpUsage.value;
	
	// Move the results page to the new target
	frames[0].frames.fraRight.location = '/_RMVirtual/Frames/KnowledgeLibrary/knowledgelibraryresults.asp' + strQueryString;		
}
