function createQuery()
{
	var q = new NetisQuery();
	top.getQueryParameters(q);
	q.submitFrame = Compat.getFrame("main").document;
	return q;
}

function showLogin(returnPage)
{
    if(returnPage == null)
        returnPage = "SearchForm.aspx";
    showMainFrame();
	var q = createQuery();
	q.showLogin(returnPage);
}

function doLogout()
{
    showMainFrame();
	Compat.getFrame("main").location.replace("logout.aspx");
}

function goRegister()
{
    showMainFrame();
    var q = createQuery();
    q.submitTo("register.aspx");
}

function toggleLogin()
{
	if (isLoggedIn())
		doLogout();
	else
		showLogin();
}

function showFocused(oFocused)
{
    var MAX_LENGTH = 150;
	var ar = [];
	for (var o = oFocused; o != null ; o = o.parent)
		ar.unshift(o.value);
	var s = ar.join(" / ");

	var s =""
	if(oFocused != null)
	{
		s = oFocused.collections_name;
		if (s.length > MAX_LENGTH)
			s =  s.substr(s.length - MAX_LENGTH) + "...";
	}
	var f = Compat.getFrame("titles").document.getElementById("iFrame1").contentWindow.showFocused;	
    
	if (typeof(f) == "function")
	    f(oFocused);
	else
		advancedSearch("yes");
}

function advancedSearch(bSearch)
{
	showSearchFrame(bSearch);
}

function showSearchFrame(bSearch)
{
    showTree();
    if(bSearch == "yes")
        showSearchTab();
    else if(bSearch == "no")
      showBrowseTab();
    
    var oSidebar = Compat.getFrame("sidebar");
    var sSrc = oSidebar.frameElement.src;
    var isSearch = oSidebar.isSearch; 
    if(isSearch != bSearch)
        oSidebar.initTocElement(bSearch);
}

function showContainerFrame()
{
	lowerframeset.setAttribute("cols", "*, 0, 0");
}

function showMainFrame()
{
    document.getElementById("treeOpen").style.display = "none";
    document.getElementById("treeClose").style.display = "none";
    lowerframeset.setAttribute("cols", "0, *, 0");
}

function goHomePage(url)
{
    top.document.location.href = url;
}

function showPage(page)
{
    try{
        Compat.getFrame("main").frameElement.src = page;
    }
  	catch(e)
  	{
  	    //https...
  	    page = page.replace(/\..+/, "");
        //Compat.getFrame("main").location.replace("httpAction.aspx?action=welcome");
        Compat.getFrame("main").location.replace("httpAction.aspx?action=" + page);
    }
    showMainFrame();
}

function hideTree()
{
    isTreeOpen = false;
    lowerframeset.setAttribute("cols", "*, 0, 0");
    document.getElementById("treeOpen").style.display = "inline";
    document.getElementById("treeClose").style.display = "none";
}

function showTree()
{
    if(lowerframeset == null)
    {
        setTimeout(showTree, 200);
        return;
    }
    isTreeOpen = true;
    lowerframeset.setAttribute("cols", "*, 0, 223px");
    document.getElementById("treeClose").style.display = "inline";
    document.getElementById("treeOpen").style.display = "none";
}

function viewCustPage()
{
    showMainFrame();
  	if (isLoggedIn())
	{
	    try{
	        var q = createQuery();
	        q.submitTo("myaccount.aspx");
	    }
	    catch(e)
	    {
	        //https...
	        Compat.getFrame("main").location.replace("httpAction.aspx?action=myaccount");
	    }
    }
	else
	    showLogin("myaccount.aspx");
}

var lowerframeset;
function init()
{
	setUserName(null);
    lowerframeset = top.document.getElementById("lowerframeset");
}

function doAction()
{
    var action = "";
	action = requestQueryString.split("=")[1];
	if(action.match(/!/))
	    action = action.replace("!" ,"('") + "')";
	else
	    action = action + "()";
	if(isLoggedIn() && action == "toggleLogin()") return;
	eval(action);
}

function showProductList()
{
	try{
        var q = createQuery();
	    q.submitTo("collectioninfo.aspx");
	}
	catch(e)
	{
	    //https...
	    Compat.getFrame("main").location.replace("httpAction.aspx?action=collectioninfo");
	}

	showMainFrame();
}

function SetCookie(sName, sValue)
{	
	path = "/";
	document.cookie = sName + "=" + sValue + "; expires=Fri, 31 Dec 3000 23:59:59 GMT;path=" + path;
}

function setCulture(culture)
{
    if(curCulture == culture) return;
    SetCookie("lang", culture);
    top.location.href = top.location.href;
}

function getancestorID(ancestorName)
{
    var oIFrame = titles.createContainer();
}

function openDafYomi()
{
    /*var q = createQuery();
    q.addTerm("CollectionName", colName);
    q.addTerm("AncestorID", getAncestorID(ancestorName));*/
    var titles = Compat.getFrame("titles");
    var oIFrame = titles.createContainer();
    oIFrame.setAttribute("src", "NetisUtils/DafYomi.html");
    showTree();
}

function showHelpMenu()
{
	var activeFrame = Compat.getFrame("main");
	//in https(store) access to menu in titles frame is denied.
	try{
		if(activeFrame.frameElement.offsetWidth == 0)
			activeFrame = Compat.getFrame("titles");
	}catch(e){return;}
	
	var helpMenu = activeFrame.document.getElementById("helpMenu");
	if(helpMenu == null) return;
	var curleft = "ltr" == "ltr" ? 0 : -90;
	//if there is a scroller reduce it's width
	if(Compat.isIE && activeFrame.document.body.dir == "rtl" && 
		activeFrame.document.body.scrollHeight > activeFrame.document.body.offsetHeight)
		curleft -= 15;
	var o = document.getElementById("helpBullet");
    while(o.offsetParent) 
    {
      curleft += o.offsetLeft;
      if(!o.offsetParent)
        break;
      o = o.offsetParent;
    }
    
    if(o.x)
        curleft += o.x;

	with(helpMenu.style)
	{
		display = "inline";
		left = curleft + "px";
		top = activeFrame.document.body.scrollTop;
	}
	
	setTimeout(hideHelpMenu, 15000);
}

function hideHelpMenu()
{
try{
	var main = Compat.getFrame("main");
	main.document.getElementById("helpMenu").style.display = "none";
	var titles = Compat.getFrame("titles");
	titles.document.getElementById("helpMenu").style.display = "none";
}catch(e){}
}

//show index subjects
function showIndexSubjects()
{
	var titles = Compat.getFrame("titles");
	var indSubjIndex = titles.indSubjID;
	hideTree();
    if(!indSubjIndex || !titles.document.getElementById(indSubjIndex))
	{
		var oIFrame = titles.createContainer(false, true);
		oIFrame.setAttribute("src", "indexSubjects.aspx");
		setTabCaption("savedquery", "Topics Index");
		titles.showIt(titles.tabInd);	
	}
    else
        titles.showIt(indSubjIndex);	
}

function isLoggedIn()
{
	try {
		var xmlhttp = XmlHttp.createNew();
		xmlhttp.open("GET", top.savedQuery.appRoot + "services.aspx?command=isloggedin", false);
		xmlhttp.send("");
		var s = xmlhttp.responseText;
		return (s == "yes");
	} catch(e) {return false;}
}

