var http;
var divid = "";
var dividExt = "";
var idExt = 0;
var strServerRoot = "";
var nAutoLoadAvailability = 0;
var nHighlightActiveTitle = 0;
var tableid = "";

function GetCookie(name)
{
    var cookies = document.cookie.split("; ");
    
    for (var index = 0; index < cookies.length; index++)
    {
        var crumb = cookies[index].split("=");
        if (name == crumb[0]) 
            return unescape(crumb[1]);
    }
    
    return null;
}

function SetServerRoot(value)
{
    strServerRoot = value;
}

function SetAutoLoadAvailability(value)
{
    nAutoLoadAvailability = value;
}

function SessionTimedOut()
{
    document.body.style.cursor = "default";
    
    if (null == GetCookie("SPOOF"))
    {
        alert("Your session has timed out.");
        window.location = strServerRoot + "default.aspx";
    }
    else
    {
        external.TimeoutSession();
    }
}

function handleHttpResponseContent() 
{
	if (http.readyState == 4)
	{ 
	    if (http.responseText.indexOf("POWERPAC-ERROR:TIMEOUT") != -1)
	    {
	        SessionTimedOut();
	        return;
	    }

		document.getElementById(divid).innerHTML = http.responseText;
        document.body.style.cursor = "default";
	}
}

function handleHttpResponseContentInitial() 
{
	if (http.readyState == 4)
	{ 
	    if (http.responseText.indexOf("POWERPAC-ERROR:TIMEOUT") != -1)
	    {
	        SessionTimedOut();
	        return;
	    }

		document.getElementById(divid).innerHTML = http.responseText;
        ShowExtendedOptions(idExt)
	}
}

function ShowExtendedOptions(pos)
{
    http = getHTTPObject();
    divid = "options_" + pos;

    http.open("GET", strServerRoot + "search/components/extendedoptions.aspx?pos=" + pos, true);
	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function handleHttpResponseTitleUpdate() 
{
	if (http.readyState == 4)
	{ 
	    if (http.responseText.indexOf("POWERPAC-ERROR:TIMEOUT") != -1)
	    {
	        SessionTimedOut();
	        return;
	    }

		document.getElementById(divid).innerHTML = http.responseText;
	
		http = getHTTPObject();
		divid = 'dashTitleList'; 

		http.open("GET", strServerRoot + "search/components/titlelistdash.aspx", true);
		http.onreadystatechange = handleHttpResponseContent;
		http.send(null);
        document.body.style.cursor = "wait";
	}
}

function AddToList(pos, controlnumber)
{
	http = getHTTPObject();
	
    divid = 'addtolist_' + pos; 
	document.getElementById(divid).innerHTML = "";

    http.open("GET", strServerRoot + "search/components/titlelistlink.aspx?action=add&pos=" + pos + "&cn=" + controlnumber, true);
	http.onreadystatechange = handleHttpResponseTitleUpdate;
	http.send(null);
    document.body.style.cursor = "wait";
}

function RemoveFromList(pos, controlnumber)
{
	http = getHTTPObject();
	
    divid = 'addtolist_' + pos; 

    http.open("GET", strServerRoot + "search/components/titlelistlink.aspx?action=remove&pos=" + pos + "&cn=" + controlnumber, true);
	http.onreadystatechange = handleHttpResponseTitleUpdate;
	http.send(null);
    document.body.style.cursor = "wait";
}

function IsThisTitleExpanded(id)
{
    var style;

	if (document.getElementById)
        style = document.getElementById('content_' + id).style;
	else if (document.all)
		style = document.all['content_' + id].style;
	else if (document.layers)
		style = document.layers['content_' + id].style;

    if (style.display == "block")
        return true;
    else
        return false;
}

function ShowContent(pos, show, highlight, isbn) 
{
    if (IsThisTitleExpanded(pos) == false)
        ExpandThisTitle(pos);
       
	if (show != "" && highlight != 0)
		HighlightThisTitle(pos);

	http = getHTTPObject(); 

    divid = "content_" + pos;

    if (show == "localavail0" || show == "autoavail0")
        http.open("GET", strServerRoot + "search/components/availability.aspx?level=local&pos=" + pos + "&morelink=0", true);
    else if (show == "localavail1" || show == "autoavail1")
        http.open("GET", strServerRoot + "search/components/availability.aspx?level=local&pos=" + pos + "&morelink=1", true);
    else if (show == "systemavail")
        http.open("GET", strServerRoot + "search/components/availability.aspx?level=system&pos=" + pos, true);
    else if (show == "details" || show == "autodetails")
        http.open("GET", strServerRoot + "search/components/details.aspx?pos=" + pos, true);
    else if (show == "marc")
        http.open("GET", strServerRoot + "search/components/marc.aspx?pos=" + pos, true);
    else if (show == "similar")
        http.open("GET", strServerRoot + "search/components/similartitles.aspx?isbn=" + isbn, true);
    else
	{
		document.getElementById(divid).innerHTML = "";
		return;
    }

    if (show == "autoavail0" || show == "autoavail1" || "autodetails")
    {
        idExt = pos;
        http.onreadystatechange = handleHttpResponseContentInitial;
    }
    else
	    http.onreadystatechange = handleHttpResponseContent;
	
	http.send(null);
    document.body.style.cursor = "wait";
}

function HighlightThisTitle(id)
{
	tds = document.getElementsByTagName("td");

	for (i=0; i<tds.length; i++) 
	{
		thisTD = tds[i];
		if (thisTD.id.indexOf('srcp_' + id) == 0)
			thisTD.className = "ResultsCoverPaneHigh";
		else if (thisTD.id.indexOf('srcp_') == 0)
			thisTD.className = "ResultsCoverPane";

		if (thisTD.id.indexOf('srtp_' + id) == 0)
			thisTD.className = "ResultsTitlePaneHigh";
		else if (thisTD.id.indexOf('srtp_') == 0)
			thisTD.className = "ResultsTitlePane";
	}
}

function SetDisplayAttribute(id, attr)
{
	if (document.getElementById)
	{
	    document.getElementById('expanded_' + id).style.display = attr;
		document.getElementById('button_less_' + id).style.display = attr;
	    document.getElementById('options_' + id).style.display = attr;
		document.getElementById('content_' + id).style.display = attr;
	}
	else if (document.all)
	{
		document.all['expanded_' + id].style.display = attr;
		document.all['button_less_' + id].style.display = attr;
		document.all['options_' + id].style.display = attr;
		document.all['content_' + id].style.display = attr;
	}
	else if (document.layers)
	{
		document.layers['expanded_' + id].style.display = attr;
		document.layers['button_less_' + id].style.display = attr;
		document.layers['options_' + id].style.display = attr;
		document.layers['content_' + id].style.display = attr;
	}
}


function ExpandThisTitle(id)
{
    SetDisplayAttribute(id, "block");
    
    location.hash = '#__pos' + id;
}


function CollapseThisTitle(id)
{
    SetDisplayAttribute(id, "none");
}


function toggleDetails(id, bMoreLink)
{
	HighlightThisTitle(id);

    if (IsThisTitleExpanded(id))
    {
        CollapseThisTitle(id);
        document.getElementById('content_' + id).innerHTML = "";
    }
    else
    {
        ExpandThisTitle(id);
        if (nAutoLoadAvailability == 1)
        {
            if (bMoreLink == 0)
                ShowContent(id, "autoavail0", 1);
            else
                ShowContent(id, "autoavail1", 1);
        }
        else
            ShowContent(id, "autodetails", 1);
    }
}

function forceOpenAvail(id)
{
    if (IsThisTitleExpanded(id))
 	    ShowContent(id, 'localavail', 1);
    else
        toggleDetails(id, 1);
}


function ShowSyndetics(pos, ids, key, file) 
{
	HighlightThisTitle(pos);

	http = getHTTPObject(); 

    divid = "content_" + pos;

    http.open("GET", strServerRoot + "search/components/syndetics.aspx?ids=" + ids + "&key=" + key + "&file=" + file, true);

	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function ShowBakerAndTaylor(pos, ids, key) 
{
	HighlightThisTitle(pos);

	http = getHTTPObject(); 

    divid = "content_" + pos;

    http.open("GET", strServerRoot + "search/components/bakerandtaylor.aspx?ids=" + ids + "&key=" + key, true);

	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function ToggleNC(clusterid)
{        
	http = getHTTPObject();
	divid = "dashNarrowSearch";
    http.open("GET", strServerRoot + "search/components/narrowsearchdash.aspx?clusterid=" + clusterid, true);
	http.onreadystatechange = handleHttpResponseContent;
	http.send(null);
    document.body.style.cursor = "wait";
}

function ToggleDashList(divid)
{
    var element;

	if (document.getElementById)
        element = document.getElementById(divid);
	else if (document.all)
		element = document.all[divid];
	else if (document.layers)
		element = document.layers[divid];
		
    if (element.style.display == "block")
        element.style.display = "none";
    else
        element.style.display = "block";
}

