//<script>
// overWrite SafeView functions
if (typeof(NetisQuery) == "function")
{
	NetisQuery.prototype.showPage__base = NetisQuery.prototype.showPage;
	NetisQuery.prototype.showPage = function(pageID, bReload)
	{
		var ar = pageID.split(",");
		if (ar.length > 1)
		{
			var pageID = ar.shift();
			this.setReturnPage(ar.join(","), bReload);
			this.submitTo(pageID);
			return;
		}
		var oTopbar = getTopbar();
		switch (pageID)
		{
			case "search" : oTopbar.setAdvancedSearchTab(); break;
			case "results": oTopbar.showResults(bReload, this); break;
			case "document": oTopbar.showDoc(this); break;
			case "generalSearch": oTopbar.sendQuery(); break;
			case "toc": top.doOnTreeClick(top.oFocused); break;
			default:
				if (pageID.match(/sfvw_document/i))
				{
					oTopbar.setCurrentTab("document");
					var doc = oTopbar.getTab("document");
					var w = Compat.getWindow(doc);
					var f = w.createFrame("doc");
					this.submitFrame = f.contentWindow.document;
				}
				this.showPage__base(pageID, bReload);
				break;
		}
	}

	NetisQuery.prototype.showDoc__base = NetisQuery.prototype.showDoc;
	NetisQuery.prototype.showDoc = function(docNo)
	{
		var id = "doc_" + docNo + "_of_" + this.resultsCount; //Math.round(Math.random() * 0xFFFFFFFF);
		top.History.addItem(id);
		var oTopbar = getTopbar();
		var doc = oTopbar.getTab("document");
		var w = Compat.getWindow(doc);
		var f = w.createFrame(id);
		this.submitFrame = f.contentWindow.document;
		this.showDoc__base(docNo);
	}

	NetisQuery.prototype.showCollectionProducts = function()
	{
		var oTopbar = getTopbar();
		oTopbar.showCollectionProducts(this);
	}

	NetisQuery.prototype.showDocumentProducts = function(titleID)
	{
		var oTopbar = getTopbar();
		oTopbar.showDocumentProducts(titleID, this);
	}

	NetisQuery.prototype.goBack = function()
	{
		var oTopbar = getTopbar();
		oTopbar.showPreviousTab();
	}

	NetisQuery.prototype.showLogin__base = NetisQuery.prototype.showLogin;
	NetisQuery.prototype.showLogin = function(returnPage, bReload)
	{
		var oTopbar = getTopbar();
		if (oTopbar == null)
			document.location.href = this.appRoot + "?ret=" + encodeURIComponent(returnPage);
		else
			oTopbar.showLogin(returnPage, bReload, this);
	}

	function checkStandalone()
	{
		if (window != top)
			return;
		var p = document.location.pathname;
		var s = document.location.search;
		if (!p.match(/sfvw_document/i) || !s.match(/\btoolbar=no/))
			return;
		var r = q.appRoot;
		if (p.substr(0, r.length).toLowerCase() == r.toLowerCase())
			p = p.substr(r.length);
		document.location.href = r + "?ret=" + encodeURIComponent(p + s);
	}

	try { checkStandalone(); } catch (e) { }
}

function getTopbar()
{
	var doc = getContainerDocument();
	var w = doc.parentWindow;
	return w.Compat.getFrame("topbar");
}

function isLoggedIn()
{
	var oTopbar = getTopbar();
	return (oTopbar.isLoggedIn());
}

function showPopup(url, features, onReturn)
{
	try {
		var href = document.location.href;
		url = makeAbsolute(href.substr(0, href.lastIndexOf("/")), url);
		Compat.showModalDialog(url, null, features, onReturn);
	} catch (e) {
		alert("Please enable popups on this website");
	}
}

function testRet(ret)
{
	if (ret)
		OnGotoInfoPage();
	else
		OnGotoResultList();
}

function testRetRL(ret)
{
	if (ret)
		OnGotoInfoPage();
	else
		OnGotoSearchScreen();
}

function OnEndPreview()
{
	// submitFrame may change when we're in SafeView, so reset it.
	q.submitFrame = document;
	if (getViewType() == "ResultList") 
		q.submitTo("endpreviewResultList.aspx");
	else
		q.submitTo("endPreview.aspx?titleID=" + titleID + "&resultsCount=" + q.resultsCount);
}

function OnEndTime()
{
	OnEndPreview();
}

function OnHasNoRight(rightNo)
{
	var message = "You do not have the right to " + GetRightName(rightNo) + " this title."
	if (q.regMode == 2) // e-commerce on 
	{
		message += "\n\nClick OK to browse a list of products for \npurchase that include this title.";
		if (confirm(message))
			OnGotoInfoPage();
	}
	else
		alert(message);
}

function getSafeViewObject()
{
	// IE test
	try { window.external.Test(); return window.external; } catch (e) { }
	// Mozilla test
	//  Because of the way result list is organized (resultlistInner in an <IFRAME> inside resultlistClient),
	//  we may need to go up the window hierarchy until we reach the safeview-protected document.
	for (var w = window; true; w = w.parent)
	{
		var doc = w.document;
		var o = doc.getElementsByTagName("object");
		for (var i = 0; i < o.length; i++)
			if (o[i].type == "application/x-safeview")
				return o[i];
		if (w == w.parent)	// top of hierarchy reached, safeview not found.
			break;
	}
	return null;
}

function getContainerDocument()
{
	var oSafeView = getSafeViewObject();
	if (oSafeView == null || oSafeView.Parent == null)
		return document;
	var doc = oSafeView.Parent.document;
	return (doc == null) ? oSafeView.Parent.Document : doc;
}

function addClass(node, className)
{
	if (node.nodeType != 1)
		return;
	var s = node.className;
	node.className = (s.length) ? s + " " + className : className;
}

function goDoc(docNo, elem)
{
	try {	
		if (elem != null && getParentRow != null && (elem = getParentRow(elem)) != null)
		{
			for (var node = elem.firstChild; node != null; node = node.nextSibling)
				addClass(node, "rlvisited");
		}
	}
	catch (e) {	 }
	// Save result selection before leaving this page
	q.docNo = docNo;
	var oTopbar = getTopbar();
	oTopbar.showDoc(q);
}

function _openDoc(docId, target)
{
	var qTemp = q.makeCopy(globalSettings | resultSettings);
	qTemp.newSearch();
	qTemp.key = "searchDoc";
	qTemp.addParsedTerm(qTemp.makeTerm("DocId", docId));
	
	var oRequest = XmlHttp.createNew();
	var sURL = makeAbsolute(qTemp.appRoot, "NetisUtils/srvrutil_xmlquery.aspx?key=" + qTemp.key);
	oRequest.open("POST", sURL, false);
	oRequest.send(qTemp.getQueryXML());

	var doc = oRequest.responseXML;
	var root = doc.documentElement;
	switch (root.tagName)
	{
		case "login" :
			alert(MSG_LOGIN_FIRST);
//			Compat.getFrame("topbar").showLogin("toc.aspx");
			return;
			break;
		case "error": throw "Server error:\n\n" + root.text;		// the server threw an exception
		case "info": throw root.text;								// server wants to tell us something
		case "query": break;										// proceed with parsing
		default: throw "Unexpected server response";
	}
	
	var total = parseInt(root.getAttribute("total"));
	qTemp.qid = root.getAttribute("qid");
	switch (total)
	{
		case 0:
			alert("The requested document not found [" + docId + "]");
			break;
		case 1:
			// in case of 1 result - keep the original result list in topbar frame
			Compat.getFrame("topbar").parent.saveQueryParameters(q);
			qTemp.showDoc(1);
			break;
		default:
			if (confirm(total + " matching documents found. Display search results?"))
				qTemp.showResults();
			break;
	}
}

function initTemplatesCombo()
{
	function onTemplatesLoaded(xmlTemplates)
	{
		if (xmlTemplates.documentElement == null)
			return;
		var oTemplates = xmlTemplates.selectNodes("/templates/template");
		var numTemplates = oTemplates.length;
		if (numTemplates < 2)
		{
			templatesCombo.setAttribute("view", "0");
			return;
		}
		for (var i = 0; i < numTemplates; i++) {
			var o = oTemplates.item(i);
			var sName = o.getAttribute("name");
			var sTitle = o.getAttribute("title");
			if (sTitle == null || sTitle == "")
				sTitle = sName;
			var oOption = new Option(sTitle + " display", sName);
			templatesCombo.options.add(oOption);
			if (sName == q.template)
				oOption.selected = true;
		}
		// Templates combo is hidden by default.
		// Only show it if more than one template is available.
		templatesCombo.style.display = "inline";
	}

	var templatesCombo = document.getElementById("templates");
	if (templatesCombo != null)
		XmlDoc.createIsland("srvrutil_templatesList.aspx?dbID=" + q.dbID, onTemplatesLoaded);
}

function OnPluginNotSupported()
{
    var oTopbar = getTopbar();
    oTopbar.disableCurrentTab();
    oTopbar.showPageInMainFrame("srvrutil_notSupported.aspx");
}

function GoPrint()
{
	var sURL = "mrkt_print.aspx?dbID=" + q.dbID + "&template=" + q.template;
	var args = { resultList: ResultList.get(), query: q.makeCopy(), selection: getResultSelection() };
	var features = Compat.fixDialogSize({ width: 350, height: 235, heightIE7: 235, heightIE: 265 });

	Compat.showModalDialog(sURL, args, features);
}

