//<script>
// overWrite SafeView functions
if (typeof(NetisQuery) == "function")
{
	NetisQuery.prototype.showResults__base = NetisQuery.prototype.showResults;
	NetisQuery.prototype.showResults = function()
	{
		var oContainer = Compat.getWindow(getContainerDocument());
		var oTopbar = getTopbar();
		oTopbar.top.saveQueryParameters(this);
		if(this.resultsCount == 1)
		{
			if(!oTopbar.isLoggedIn())
			{
				this.docNo= 1;
				oTopbar.showLogin("document",true, this);
				return;
			}			
			this.submitFrame = oContainer.top.getMainFrameTab("document");
			oTopbar.setDocumentTab(true);
			this.showDoc(1);
		}
		else
		{
			if(!oTopbar.isLoggedIn())
			{
				oTopbar.showLogin("results",true, this);
				return;
			}			
		
			var oResults = oContainer.top.getMainFrameTab("results")
			if(oResults.q)
				if((this.key == oResults.q.key) && (this.key != ""))
				{	
					oTopbar.setResultListTab();
					return;
				}
			
			try { oResults.clearWindow(); } catch (e) { }  // clear existing result list
			this.submitFrame = oResults.document;
			this.showResults__base();
			if(this.resultsCount > 1)
			    oTopbar.setResultListTab();
		}
	}
	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 "myaccount" : oTopbar.showCustProducts(); break;
			case "store" : oTopbar.showCollectionProducts(this); break;
			//case "search" : oTopbar.setAdvancedSearchTab(); break;
			case "search" :if (oTopbar == null)
			                    document.location.href = this.appRoot;
		                   else
			                    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 = MSG_HAS_NO_RIGHT_TO_1 + GetRightName(rightNo) + MSG_HAS_NO_RIGHT_TO_2 + "." ;  
	if (q.regMode == 2) // e-commerce on 
	{
		message += MSG_HASNORIGHTS;
		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 OnLinkClicked(query)
{
	try
	{
		var qLink = q.makeCopy(globalSettings | resultSettings);
		try{
			if ((query == null) || (query == undefined) || (query == ""))
			{
				var HtmlCtl = SafeView.getObject();
				HtmlCtl.Test();
				query = new String(HtmlCtl.GetQuery());
			}
		}catch(e){alert("Error OnLinkClicked - " + e.description);}
		var fields = trim(query).split(";");
		for (i = 0; i < fields.length; i++)
		{
			var fieldStr = new String(fields[i]);
			var fieldVals = fieldStr.split(":");
			// Decodes String objects encoded with the escape method. 
			fieldVals[1] = decodeURI(fieldVals[1]);
			fieldVals[1] = unescape(fieldVals[1]); 
			fieldVals[1] = fieldVals[1].replace(/^\"([^\"]+)\"$/, "[$1]");
			qLink.addTerm(fieldVals[0], fieldVals[1]);
		}
		qLink.sortBy("SortField", 1);
		//Author_search - biblio, ID_Author_Name - authors
		qLink.template = (fieldVals[0].match(/Author_search/)) ? "biblio" : "authors";
		qLink.showResultsDRM();
	}
	catch(e)
	{
		alert(MSG_INVALID_LINK);
	}
}
function trim(str)
{
	return str.replace(/^\s+/,"").replace(/\s+$/,"");
}

function getRes(recordNumber)
{
    var oXmlHttp = XmlHttp.createNew();
    oXmlHttp.open("POST", makeAbsolute(q.appRoot, "NetisUtils/isInShocken.aspx"), false);
    oXmlHttp.send("<root><bookId>" + recordNumber + "</bookId></root>");
    var retVal = oXmlHttp.responseText;
    
    if(retVal > 0)
        return true;
    return false;
} 