/*
 * ---------------------------------------------------------------------
 * Get the object-reference for the top-most frame control of ProcureApp
 * ---------------------------------------------------------------------
 */
function getRWRootContainerFrame(frm)
{
	if (frm)
	{
		if (frm.RWRootContainerFrame == true)
			return frm;
		else
		{
			if (frm.frames)
			{
				for (var i=0; i < frm.frames.length; i++)
				{
					var sub = getRWRootContainerFrame(frm.frames[i]);
					if (sub)
						return sub;
				}
			}
		}
	}
	return false;
}
intlLib = getRWRootContainerFrame(top);

if (intlLib == false)
	intlLib = top;

var rwContainer = intlLib;

function showSearch(showTabbed, doSearch, objectName, tabConf, isSearchLink, menuContext)
{
	var displayMode, viewParam;
	var solution = rwContainer.solution;
	var searchCmd = servletPath + '/controller/search%3aSearch?HTTP_REQ_docName=' + objectName;
	if(solution == 'Receiving')
		searchCmd = servletPath + '/controller/search%3aSearch?HTTP_REQ_docName=Receipts';
		
	var userContext = getUserContext(objectName, solution);
	var viewContext = getViewContext(objectName, solution);

	searchCmd = searchCmd + '&HTTP_REQ_solution=' + solution;
	
	if(showTabbed == 'true')
	{
		searchCmd = searchCmd + '&HTTP_REQ_SelectedSearchTab=' + objectName + '&HTTP_REQ_tabConf=' + tabConf;
		viewParam = tabConf + '+' + viewContext;
	}
	else
		viewParam = viewContext;

	searchCmd = searchCmd + '&userContext=' + userContext + '&view=' + viewParam;

	if(doSearch == 'true')
	{
		displayMode = 'showResults';
		// NS does not recognize searchStr
		if (navigator.appName == "Microsoft Internet Explorer")
		{		
		  searchCmd = searchCmd + '&HTTP_REQ_searchStr=' + searchStr.value + '&doquickSearch=1&quickSearch='+searchStr.value;
		  if( objectName == 'PurchaseOrders')
		    if(searchStr.value == '')
		  	searchCmd = searchCmd + "&HTTP_REQ_showAll=Y";
		}
	}
	else
		displayMode = 'showSearchFields';
	
	searchCmd = searchCmd + '&HTTP_REQ_displayMode=' + displayMode;
	
	if(!menuContext && objectName == 'Requisitions')
		menuContext = getRequestingMenuContext();

	if(menuContext && menuContext.length != 0)
		searchCmd = searchCmd + '&HTTP_REQ_menuContext=' + menuContext;
	
	if(objectName == 'PurchaseOrders')
	{
		if(solution == 'Receiving')
			searchCmd = searchCmd + "&HTTP_REQ_DisplayType=LineItems";
		else
		  searchCmd = searchCmd + "&HTTP_REQ_DisplayType=PO";
	}
	else if (objectName == 'ASNs')
		searchCmd = searchCmd + '&HTTP_REQ_resultType=ASN';
		
	if(isSearchLink == 'y')
		searchCmd = searchCmd + '&HTTP_REQ_isSearchLink=true';

	searchCmd = searchCmd + '&HTTP_REQ_strCompare=on';

	parent.mainContent.location.href = searchCmd + "&urlId=" + rwContainer.urlId;
}

function getRequestingMenuContext()
{
	if(solution == 'Requesting')
		return 'ProcessedReqs';

	if(solution == 'Purchasing')
		return 'ReqOutbox';

	if(solution == 'Approving')
		return 'ApproverOutbox';

	return 'ProcessedReqs';
}

function getViewContext(objectName, solution)
{
	var viewContext;
	
	// append the user role and object specific view conf name
	if(objectName == 'CatalogItems')
			viewContext = '';
	else if(objectName == 'Requisitions')
	{
		if(solution == 'Approving')
			viewContext = 'ApprovalOutboxList';
		else if(solution == 'Purchasing')
			viewContext = 'RequisitionListForOutbox';
		else
			viewContext = 'RequisitionSearchResultList';
	}
	else if(objectName == 'Projects')
			viewContext = 'ProjectList';
	else if(objectName == 'BlanketOrders')
			viewContext = 'BlanketOrderList';
	else if(objectName == 'PurchaseOrders' || objectName == 'ASNs' || objectName == 'Invoices')
		viewContext = '';
		
	return viewContext;	
}

function getUserContext(objectName, solution)
{
	var userContext;
	
	// append the user role and object specific view conf name
	if(objectName == 'CatalogItems')
			userContext = '';
	else if(objectName == 'Requisitions')
	{
		if(solution == 'Approving')
			userContext = 'approving';
		else if(solution == 'Purchasing')
			userContext = 'purchasing';
		else
			userContext = 'requesting';
	}
	else if(objectName == 'Projects')
			userContext = 'projects';
	else if(objectName == 'BlanketOrders')
			userContext = 'purchasing';
	else if(objectName == 'PurchaseOrders' || objectName == 'ASNs' || objectName == 'Invoices')
		userContext = '';
		
	return userContext;	
}

function setBreadCrumb(isSearchLink, objectName, searchCmd, showTabbed, isDownload)
{
	var objectLabel = getSearchObjectLabel(objectName);
	var solution = rwContainer.solution;
	
	if(isSearchLink == 'y')
	{
		rwContainer.menuName = solution;
		if(isDownload == 'true')
			rwContainer.pageTitle = rwContainer.userNavBarRequisitions;
		else
			rwContainer.pageTitle = rwContainer.userNavBarSearch;
		
		if(showTabbed != 'true' && objectLabel.length != 0)
			rwContainer.pageTitle += " " + objectLabel;

		top.baseSearchURL = searchCmd;
		rwContainer.addBreadCrumb(rwContainer.pageTitle, searchCmd, 'true');
	}
	else
	{
		if(!solution)
		{
			solution = 'Requesting';
			rwContainer.pageTitle = rwContainer.userNavBarRequisitions;
		}
		else
		{
			if(solution == 'Approving')
				rwContainer.pageTitle = rwContainer.userNavBarApprovalOutBox;
			else if(solution == 'Purchasing')
			{
				if(objectName == 'PurchaseOrders')
					rwContainer.pageTitle = rwContainer.userNavBarPurchaseOrders;	
				else if(objectName == 'Requisitions')
					rwContainer.pageTitle = rwContainer.userNavBarRequisitionOutBoxes;
				else if(objectName == 'ASNs')
					rwContainer.pageTitle = rwContainer.userNavBarReceivedAsn;
				else if(objectName == 'Invoices')
					rwContainer.pageTitle = rwContainer.userNavBarInvoices;
				else if(objectName == 'Invoices')
					rwContainer.pageTitle = rwContainer.userNavBarInvoices;
			}
			else if(solution == 'Supplying')
			{
				rwContainer.pageTitle = rwContainer.userNavBarSearch;
				if(objectName == 'PurchaseOrders')
					rwContainer.pageTitle += " " + rwContainer.userNavBarPurchaseOrders;	
				else if(objectName == 'ASNs')
					rwContainer.pageTitle += " " + rwContainer.userNavBarAsns;
				else if(objectName == 'Invoices')
					rwContainer.pageTitle += " " + rwContainer.userNavBarInvoices;
			}
			else
				rwContainer.pageTitle = rwContainer.userNavBarRequisitions;
		}
		
		rwContainer.menuName = solution;
		rwContainer.addBreadCrumb(rwContainer.menuName, '#', 'true');	
		rwContainer.addBreadCrumb(rwContainer.pageTitle, searchCmd);
	}
}


function getSearchObjectLabel(objectName)
{
	var searchObjectLabel = '';
	
	if(objectName == 'CatalogItems')
		searchObjectLabel = rwContainer.userTabCatalogItems;
	else if(objectName == 'Requisitions')
		searchObjectLabel = rwContainer.userTabRequisitions;
	else if(objectName == 'Projects')
		searchObjectLabel = rwContainer.userTabProjects;
	else if(objectName == 'BlanketOrders')
		searchObjectLabel = rwContainer.userTabBlanketOrders;
	else if(objectName == 'PurchaseOrders')
		searchObjectLabel = rwContainer.userTabPurchaseOrders;
	else if(objectName == 'ASNs')
		searchObjectLabel = rwContainer.userTabAsns;
	else if(objectName == 'Invoices')
		searchObjectLabel = rwContainer.userTabInvoices;
	
	return searchObjectLabel;
}

function executeURI(url)
{
  window.location.href = url;
}

// Issue no. 212484 	
if (!top.navBarClosed)
top.navBarClosed = false;

function getSearchParams()
{
	// basic URL parameters
	var params = top.baseSearchURL.substring(top.baseSearchURL.indexOf("?"), top.baseSearchURL.length);

	// form parameters
	var searchFormParams = getFormParams(top.rwFormCache['searchForm']);

	return params + "&" + searchFormParams;
}

function getFormParams(formElements)
{
	var paramStr = "";

	if (formElements)
	{
		for (var name in formElements)
		{
			// add params only if they are user specified criteria
			if(name.indexOf(".text") == -1 && name.indexOf(".oper") == -1 &&
				 name.indexOf("HTTP_REQ_") == -1 && name.indexOf("quickSearch") == -1)
				continue;

			var paramVal = formElements[name];
			if(!paramVal || !paramVal.length || paramVal == ' ')
				continue;

			if(paramStr.length > 0)
				paramStr = paramStr + "&";								
			paramStr = paramStr + name + "=" + formElements[name];
		}
	}

	return paramStr;
}
