/**
 * 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(" ");
var siteidList = getValue("siteids_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] || name.indexOf('organisation') != -1) {
	//PGA added 'organisationOnly' and changed 'organisation' to return image and text
		if (name == 'organisationOnly') {
			return accessValuesObject['organisation'];
		} else if (name == 'organisation') {
			var inst_text = accessValuesObject['organisation'];
			var returnTxt = inst_text;
			if (accessValuesObject['org_logo']) {
				returnTxt += "<br\/>";
				if (accessValuesObject['org_logo_link']) {
					returnTxt += "<a href=\"javascript:popup('"+unescape(accessValuesObject['org_logo_link'])+"','popup',750,500);\" title=\""+inst_text+", opens in a new window\">";
				}
				returnTxt += "<img src='"+unescape(accessValuesObject['org_logo'])+"' ";
				returnTxt += "alt='"+inst_text+"' title='' \/>";
				if (accessValuesObject['org_logo_link']) {
					returnTxt += "</a>";
				}
			}
			return returnTxt;
		} else {
			return accessValuesObject[name];
		}
	} else {
		return "not found";
	}
}

// Get the access icon url
function getAccessIcon(subject) {
	var imageURL = "<img src=\"/oso/public/all_images/ak_access.gif\" alt=\"You have free access to the abstracts and keywords of title in this subject collection.\" title=\"You have free access to the abstracts and keywords of title in this subject collection.\" />";
	for (var i=0; i<subjectList.length; i++) {
		if (subject == subjectList[i]) {
			imageURL = "<img src=\"/oso/public/all_images/ft_access.gif\" alt=\"You have access to the full text of items in this subject collection.\" title=\"You have access to the full text of items in this subject collection.\" />";
			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;
}

function getPageAccessOSO(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\" target=\"_blank\" >";
			pageLink = pageLink + link_content + "<\/a>";
			break;
		} else {
			pageLink="<span class=\"text-off\">" + link_content + "<\/span>";
			

		}
	}
	return pageLink;
}


// Get the access rights to use as alt for an access icon
function getAccessRights(subject) {

	var in_pw_date1 = '0'; // earliest date when multiples
	var in_pw_date2 = '1'; // latest date when multiples
        var accessImg = "<img src=\"/oso/public/all_images/ak_access.gif\" alt=\"You have free access to the abstracts and keywords of title in this subject collection.\" title=\"You have free access to the abstracts and keywords of title in this subject collection.\" />";
        var accessText = accessRangeText = accessRangeEnd = "";
	subjectList.sort();  // makes the list alphabetic - pw_ > pwb > pwe > pwr and numeric - 2005 > 2006 > 2007 etc

	for (var i=0; i<subjectList.length; i++) {
                if (subject == subjectList[i]) {
                        accessImg = "<img src=\"/oso/public/all_images/ft_access.gif\" alt=\"You have access to the full text of items in this subject collection\" title=\"You have access to the full text of items in this subject collection\" />";
                        break;
                } else {
			var check_pw = subjectList[i].indexOf("pw_");
			if (check_pw >= 0) {
				// pw_
				var pw_date = subjectList[i].substring(3, 11);
				var pw_subject = subjectList[i].substring(12);
				if (subject == pw_subject) {
					accessImg = "<img src=\"/oso/public/all_images/ak_access_perpetual.gif\" alt=\"You have access to the full text of some items in this subject collection.\" title=\"You have access to the full text of some items in this subject collection.\" />";
					// set dates appropriately...
					if (in_pw_date2 == '1' || pw_date > in_pw_date2) {
						in_pw_date2 = pw_date;
						accessRangeText = "<br/><i>You have access to the full text of titles published to OSO up to and including the " + formatDate(in_pw_date2, "MM, yyyy") + "<!--SPLIT-->";
						accessRangeEnd = " update.</i>";
					}
				}
			} else {
				var check_pwb = subjectList[i].indexOf("pwb");
				if (check_pwb >= 0) {
					// pwb
					for (var sb=16; sb<subjectList[i].length; sb+=14) {
						var isbn = subjectList[i].substring(sb-12,sb+1);
						var pw_subject = getSubjectFromISBN(isbn);
						if (subject == pw_subject) {
							accessImg = "<img src=\"/oso/public/all_images/ak_access_perpetual.gif\" alt=\"You have access to the full text of some items in this subject collection.\" title=\"You have access to the full text of some items in this subject collection.\" />";
							if (accessText == "") accessText = "<br/><i>You have access to the full text of the following titles: </i>";
							accessText += "<br/>"+getAuthorsFromISBN(isbn)+", <a href='/oso/public/content/"+pw_subject+"/"+isbn+"/toc.html'>"+getTitleFromISBN(isbn)+"<\/a>";
						}
					}
				} else {
					var check_pwe = subjectList[i].indexOf("pwe");
					if (check_pwe >= 0) {
						// pwe
						var pw_date = subjectList[i].substring(4, 12);
						var pw_subject = subjectList[i].substring(13);
						if (subject == pw_subject) {
							accessImg = "<img src=\"/oso/public/all_images/ak_access_perpetual.gif\" alt=\"You have access to the full text of some items in this subject collection.\" title=\"You have access to the full text of some items in this subject collection.\" />";
							// set dates appropriately...
							if (in_pw_date1 == '0') {
								if (in_pw_date2 == '1') {
									in_pw_date1 = pw_date;
									in_pw_date2 = pw_date;
									accessRangeText = "<br/><i>You have access to the full text of titles published to OSO in the " + formatDate(in_pw_date1, "MM, yyyy");
									accessRangeEnd = " update.<\/i>";
								}
								if (pw_date > in_pw_date2) {
									in_pw_date1 = pw_date;
									accessRangeText += " update <br/> and titles published to OSO in the " + formatDate(in_pw_date1, "MM, yyyy");
									accessRangeEnd = " update.<\/i>";
								}
							} else {
								in_pw_date2 = pw_date;
								accessRangeText += " and the " + formatDate(in_pw_date2, "MM, yyyy");
								accessRangeEnd = " updates.<\/i>";
							}	
						}
					} else {
						var check_pwr = subjectList[i].indexOf("pwr");
						if (check_pwr >= 0) {
							// pwr
							var pw_date1 = subjectList[i].substring(4, 12);
							var pw_date2 = subjectList[i].substring(13, 21);
							var pw_subject = subjectList[i].substring(22);
							if (subject == pw_subject || pw_subject == '') {
								accessImg = "<img src=\"/oso/public/all_images/ak_access_perpetual.gif\" alt=\"You have access to the full text of some items in this subject collection.\" title=\"You have access to the full text of some items in this subject collection.\" />";
								// set dates appropriately...
								if (in_pw_date1 == '0' || pw_date1 < in_pw_date1) in_pw_date1 = pw_date1;
								if (in_pw_date2 == '1' || pw_date2 > in_pw_date2) in_pw_date2 = pw_date2;
								if (in_pw_date1 != '0' && in_pw_date2 != '1' && accessRangeText.indexOf("<!--SPLIT-->") != -1) {
									var accessRangeBits = accessRangeText.split("<!--SPLIT-->");
									// alert (accessRangeBits[0] + " here? " + accessRangeBits[1]);
									accessRangeText = accessRangeBits[0] + " update <br/> and access to the full text of titles published to OSO from the " + formatDate(pw_date1, "MM, yyyy") + " to " + formatDate(pw_date2, "MM, yyyy") + accessRangeBits[1];
									accessRangeEnd = " updates.<\/i>";
								} else {
									accessRangeText = "<br/><i>You have access to the full text of titles published to OSO from the " + formatDate(in_pw_date1, "MM, yyyy") + " to " + formatDate(in_pw_date2, "MM, yyyy");
									accessRangeEnd = " updates, inclusive.<\/i>";
								}
							}
						}
					}
				}
			}
                }
        }
        return accessImg+accessText+accessRangeText+accessRangeEnd;
}

// Get the access site 
function getAccessSite(siteid) {
	
	for (var i=0; i<siteidList.length; i++) {
		if (siteid == siteidList[i]) {
			return true;
			break;
		}
	}
	return false;
}

// 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\"  alt=\"You have access to the full text for this item.\" title=\"You have access to the full text for this item.\" />";
			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\" alt=\"You have access to the full text for this item.\" title=\"You have access to the 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_full_text.gif\" alt=\"You have access to the full text for this item.\" title=\"You have access to the full text for this item.\" />";
				break;
			}
		}
	}
        return imageURL;
}
function getAbstractAccess(subject, type) {
	var imageURL = "<img src=\"/oso/public/all_images/sr_abstracts_only.gif\" alt=\"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.\" 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.\" />";
	if (cookie=="none"){
		if (type == 'INDEX'){
			imageURL = "<img src=\"/oso/public/all_images/sr_abstracts_only.gif\" alt=\"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.\" 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.\" />";
		} else {
			imageURL = "<img src=\"/oso/public/all_images/sr_abstract.gif\" alt=\"You have access to the abstract and full text for this item.\" 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\" alt=\"You have access to the abstract and full text for this item.\" title=\"You have access to the abstract and full text for this item.\" />";
				break;
			} else if (type == 'TOC'){
				imageURL = "<img src=\"/oso/public/all_images/abstract_rnd.gif\" alt=\"You have free access to the abstracts and keywords of this item.\" title=\"You have free access to the abstracts and keywords of this item.\" />";
			}
		}
	}
        return imageURL;
}

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

}
function foundNotNullOrEmpty(str) {
	return ((str != null) && (str.length > 0) && (str != "not found"));
}
// DS - this, of course should be changed or replaced at any later time
function formatDate(date_str, format) {
	var months =  new Array("January", "February", "March",	"April", "May",	"June",	"July",	"August", "September",
				"October", "November", "December");
	if (format == "MM dd, yyyy") {
        	var tmp_date = date_str.substring(0,4) + "/" + date_str.substring(4,6) + "/" + date_str.substring(6,8);
                var d = Date.parse(tmp_date);
                var date = new Date();
		date.setTime(d);
                var day = date.getDate();
                var month = date.getMonth();
                var year = date.getFullYear();
		return months[month] + " " + day + ", " + year;
	} else if (format == "MM, yyyy") {
        	var tmp_date = date_str.substring(0,4) + "/" + date_str.substring(4,6) + "/" + date_str.substring(6,8);
                var d = Date.parse(tmp_date);
                var date = new Date();
		date.setTime(d);
                var month = date.getMonth();
                var year = date.getFullYear();
		return months[month] + ", " + year;
	}
	return "";
}

/* PGA ISBN function... */
function getSubjectFromISBN(ISBNstring) {
	var bits = tsib[ISBNstring].split("|");
	return getSubject(bits[1]);
}
function getTitleFromISBN(ISBNstring) {
	var bits = tsib[ISBNstring].split("|");
	return bits[0];
}
function getAuthorsFromISBN(ISBNstring) {
	var bits = tsib[ISBNstring].split("|");
	return bits[2];
}
function getSubject(subjLetter) {
	return subjArray[subjLetter];
}
