/**
 * DP 2003/06/05
 * This reads name value pairs from cookies and puts
 * it into an object so that it can be called in the body.
 */

// This is where we hold what we get from the cookie.
accessValuesObject = new Object();
var cookie = "none";
// Now we can read the cookie.
getCookie();
// Get an array of allowed subjects.
var subjectList = getValue("titles_allow").split(" ");

// This is just for testing.
function setCookie() {
	document.cookie = "critter=" + document.getElementById("value").value;
	window.location.reload(false);
	return false;
}

// This reads the cookie and puts the values into the object.
function getCookie() {
	// Get the list of cookies.
	var cookies = document.cookie.split(";");
        	// Get just the cookie we want by name.
	for (i=0; i<cookies.length; i++) {
		var cookieName = cookies[i].substring(0, cookies[i].indexOf("=", 0));
		cookieName = cookieName.replace(" ", "");
		if (cookieName === "critter_javascript") {
			cookie = cookies[i].substring(cookies[i].indexOf("=", 0)+1, cookies[i].length);
		}
	}

	// Create an array of the name value pairs.
	if (cookie != "none") {
		var values = cookie.split("&");

		// Loop over the array and split out the name value pairs and put them in the object.
		for (i=0; i<values.length; i++) {
			var nameValue = values[i].split("=");
			accessValuesObject[nameValue[0]] = nameValue[1];
		}
	}
}

// Get the specified value.
function getValue(name) {
         if (accessValuesObject[name]) {
	   return accessValuesObject[name];
	} else {
		return "not found";
	}
}

// Get the access icon url
/* 
function getAccessIcon(subject) {
	var imageURL = "/oso/public/all_images/icon_abstract_access.gif";
	for (var i=0; i<subjectList.length; i++) {
		if (subject === subjectList[i]) {
			imageURL = "/oso/public/all_images/icon_full_access.gif";
			break;
		}
	}
	return imageURL;
}
*/
function getAccessIcon(subject) {
	var imageURL = "<img src=\"/oso/public/all_images/ak_access.gif\" title=\"You have free access to the abstracts and keywords of title in this subject collection\" border=\"0\" />";
	for (var i=0; i<subjectList.length; i++) {
		if (subject === subjectList[i]) {
			imageURL = "<img src=\"/oso/public/all_images/ft_access.gif\" title=\"You have access to the full text of items in this subject collection\" border=\"0\" />";
			break;
		}
	}
	return imageURL;
}
function getPageAccess(subject, link, link_content) {
	var pageLink = "";
	for (var i=0; i<subjectList.length; i++) {
//		if (subject === subjectList[i]) {
		if (subject === 'true') {
			pageLink = "<a href=\"" + link + "\" class=\"sublink-as\">";
			pageLink = pageLink + link_content + "</a>";
			break;
		} else {
			pageLink="<span class=\"text-off\">" + link_content + "</span>";
			

		}
	}
	return pageLink;
}

// Get the access text to use as alt for an access icon
function getAccessText(subject) {
	var accessText = "You have access to the abstract for this title";
	for (var i=0; i<subjectList.length; i++) {
		if (subject === subjectList[i]) {
			accessText = "You have access to the full text of this title";
			break;
		}
	}
	return accessText;
}
function getFullTextAccessIndex(subject) {
        var imageURL = "";
	for (var i=0; i<subjectList.length; i++) {
		if (subject === subjectList[i]) {
			imageURL = "<img src=\"/oso/public/all_images/sr_full_text.gif\" border=\"0\" />";
			break;
		}
	}
        return imageURL;
}
function getFullTextAccess(subject, visibility) {
        var imageURL = "";
	if (cookie == "none"){
		if (visibility == "ALL"){
			imageURL="<img src=\"/oso/public/all_images/sr_full_text.gif\" title=\"You have access to the full text for this item.\" border=\"0\" />";
		}
	} else {
		for (var i=0; i<subjectList.length; i++) {
			if (subject === subjectList[i]) {
				imageURL = "<img src=\"/oso/public/all_images/sr_full_text.gif\" title=\"You have access to the full text for this item.\" border=\"0\" />";
				break;
			}
		}
	}
        return imageURL;
}
function getAbstractAccess(subject, type) {
	var imageURL = "<img src=\"/oso/public/all_images/sr_abstracts_only.gif\" title=\"If there is no Full Text link available you do not have access to the full text of this item.  You do however have free access to the abstracts and keywords\" border=\"0\" />";
	if (cookie=="none"){
		if (type == 'INDEX'){
			imageURL = "<img src=\"/oso/public/all_images/sr_abstracts_only.gif\" title=\"If there is no Full Text link available you do not have access to the full text of this item.  You do however have free access to the abstracts and keywords\" border=\"0\" />";

		} else {
			imageURL = "<img src=\"/oso/public/all_images/sr_abstract.gif\" border=\"0\" title=\"You have access to the abstract and full text for this item.\" />";
		}
	} else {
		for (var i=0; i<subjectList.length; i++) {
			if (subject === subjectList[i]) {
				imageURL = "<img src=\"/oso/public/all_images/sr_abstract.gif\" title=\"You have access to the abstract and full text for this item.\" border=\"0\" />";
				break;
			} else if (type == 'TOC'){
				imageURL = "<img src=\"/oso/public/all_images/abstract_rnd.gif\" title=\"You have free access to the abstracts and keywords of this item\" border=\"0\" />";
			}
		}
	}
        return imageURL;
}


// This returns 1 if vales exist or else 0
function cookieExists() {
	if (cookie == "none") {
		return false;
	} else {
		return true;
	}	
}
