/********************** Global constants ********************/
//*** NO LONGER NEEDED - SET IN HEADER.JSPF ****
//var SERVER_NAME = "localhost:8080"; //"dev.torontopubliclibrary.ca";
//var SITE_ROOT = "/endecaApp";
//var SITE_PAGES = "/endecaApp/pages";
/** ******************** Initial page load methods ******************* */
window.onload = function() {
Common.onPageLoad();
setTimeout("Common.allOtherPageLoads();", 100);
};
window.onunload = function() {
// GUnload();
}
window.onresize = function() {
// Widgets.positionEditButtons();
};
Common = {}
Common.onPageLoad = function() {
Common.enableMerchPreviewMode();
Common.checkJavascriptEnabled();
Common.browserCompatibilityFixes();
Common.displayMemberLinks();
}
Common.allOtherPageLoads = function() {
Widgets.onPageLoad();
Accessibility.onPageLoad();
Branches.onPageLoad();
Search.onPageLoad();
Share.onPageLoad();
Calendar.onPageLoad();
Details.onPageLoad();
}
Common.goBack = function() {
history.go(-1);
return false;
}
// Get the current location encoded
Common.getCurrentLocationEncoded = function() {
var currentLocation = document.URL;
return escape(currentLocation);
}
Common.checkJavascriptEnabled = function() {
var enabledElems = Common.getElementsByClassName(document, "*",
"elem_jsEnabled");
var disabledElems = Common.getElementsByClassName(document, "*",
"elem_jsDisabled");
Common.setSessionCookie("jsCapable", "true", "/");
// alert(enabledElems.length);
for ( var i = 0; i < enabledElems.length; i++) {
enabledElems[i].style.display = "block";
}
for ( var i = 0; i < disabledElems.length; i++) {
disabledElems[i].style.display = "none";
}
}
Common.browserCompatibilityFixes = function() {
// Header links in IE6
if (Common.checkIfIEVersion(6)) {
if (document.getElementById("page-banner") != null)
document.getElementById("page-banner").style.marginTop = "-37px";
if (document.getElementById("find-your-way") != null)
document.getElementById("find-your-way").style.paddingBottom = "0px";
}
}
// If Nmrf param exists in querystring (merch preview mode), append it to all
// links also
Common.enableMerchPreviewMode = function() {
var filter = Common.getQueryParameter("Nmrf");
if (filter != '') {
var links = document.getElementsByTagName("A");
for ( var i = 0; i < links.length; i++) {
var url = links[i].href;
if (url.indexOf("Nmrf") == -1) {
if (url.indexOf("?") == -1)
links[i].href = url + '?Nmrf=' + filter;
else
links[i].href = url + '&Nmrf=' + filter;
}
}
}
}
/** ************************ Browser / DOM methods *********************** */
Common.getElementsByClassName = function(oElm, strTagName, strClassName) {
if (oElm != null) {
if (oElm.getElementsByClassName)
return oElm.getElementsByClassName(strClassName);
else {
var arrElements = (strTagName == "*" && document.all) ? document.all
: oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var elementClass;
for ( var i = 0; i < arrElements.length; i++) {
elementClass = arrElements[i].getAttribute("className");
if (elementClass == null)
elementClass = arrElements[i].attributes["className"];
if (oRegExp.test(elementClass)) {
arrReturnElements.push(arrElements[i]);
}
}
return (arrReturnElements);
}
}
}
Common.getQueryParameter = function(name) {
return Common.getKeyValue(window.location.href, name);
}
// Returns the value for a specific key in the passed in keyvalue pair string
Common.getKeyValue = function(pairs, key) {
if (pairs != null) {
if (pairs.substring(0, 1) != "?")
pairs = "?" + pairs;
var regkey = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + regkey + "=([^]*)";
var regex = new RegExp(regexS);
var results = regex.exec(pairs);
if (results != null)
return results[1];
}
return "";
}
// This function compiles a keyvalue string from all form elements using their
// name as key.
// Makes checkbox values of same name space separated.
Common.getFormKeyValuePairs = function(form) {
var returnstring = "";
var currName = "";
for ( var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type == "text"
|| form.elements[i].type == "textarea") {
returnstring += form.elements[i].name + "="
+ form.elements[i].value + ","
} else if (form.elements[i].type == "radio") {
if (form.elements[i].checked)
returnstring += form.elements[i].name + "="
+ form.elements[i].value + ","
} else if (form.elements[i].type == "checkbox") {
if (form.elements[i].checked) {
if (form.elements[i].name = currName) {
returnstring += returnstring.substring(0,
returnstring.length - 1)
+ " " + form.elements[i].value + ","
} else {
returnstring += form.elements[i].value + "=1,"
}
currName = form.elements[i].name;
}
} else if (form.elements[i].type == "select-one") {
returnstring += form.elements[i].name
+ "="
+ form.elements[i].options[form.elements[i].selectedIndex].text
+ ","
}
}
if (returnstring.length > 0)
returnstring = returnstring.substring(0, returnstring.length - 1);
return returnstring;
}
// This function sets all form elements by name(key) based on the keyvalue pair
// string passed in.
// Assumes checkbox values are space separated.
Common.setFormKeyValuePairs = function(form, pairstring) {
if (pairstring != null && form != null) {
var pairs = pairstring.split(",");
for ( var i = 0; i < pairs.length; i++) {
var key = pairs[i].split("=")[0];
var value = pairs[i].split("=")[1];
var elems = form.elements[key];
if (elems[0] == undefined)
break;
if (elems[0].type == "radio") {
for ( var n = 0; n < elems.length; n++) {
if (elems[n].value == value)
elems[n].checked = true;
}
} else if (elems[0].type == "checkbox" || elems.type == "checkbox") {
for ( var n = 0; n < elems.length; n++) {
var values = value.split(" ");
for ( var x = 0; x < values.length; x++) {
if (elems[n].value == values[x]) {
elems[n].checked = true;
break;
}
}
}
} else if (elems.type == "select-one") {
for ( var n = 0; n < elems.options.length; n++) {
if (elems.options[n].value == value) {
elems.selectedIndex = n;
break;
}
}
} else if (elems.type == "text" || elems.type == "textarea") {
elems.value = value;
}
}
}
}
Common.getQueryString = function() {
var url = window.location.toString();
if (url.indexOf("?") > -1) {
url.match(/\?(.+)$/);
var params = RegExp.$1;
return (params == "$super" ? "" : "?" + params);
} else {
return "";
}
}
/** ************ Get an elements absolute positioning ************** */
Common.getAbsoluteY = function(oElement) {
var iReturnValue = 0;
while (oElement != null) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
// alert("Y: " + iReturnValue);
return iReturnValue;
}
Common.getAbsoluteX = function(oElement) {
var iReturnValue = 0;
while (oElement != null) {
iReturnValue += oElement.offsetLeft;
oElement = oElement.offsetParent;
}
// alert("X: " + iReturnValue);
return iReturnValue;
}
Common.togglePopup = function(oTarget, oPopup, offsetX, offsetY) {
if (oPopup.style.display == 'block')
Common.hidePopup(oPopup);
else
Common.showPopup(oTarget, oPopup, offsetX, offsetY);
}
Common.showPopup = function(oTarget, oPopup, offsetX, offsetY) {
oPopup.style.left = Common.getAbsoluteX(oTarget) + offsetX + "px";
oPopup.style.top = Common.getAbsoluteY(oTarget) + offsetY + "px";
oPopup.style.display = 'block';
}
Common.hidePopup = function(oPopup) {
oPopup.style.display = 'none';
}
Common.checkIfIE = function() {
var ver = navigator.appVersion;
if (ver.indexOf("MSIE") != -1)
return true;
else
return false;
}
Common.checkIfIEVersion = function(isVersion) {
if (Common.checkIfIE()) {
var version = parseFloat(navigator.appVersion.split("MSIE")[1])
if (version == isVersion)
return true;
else
return false;
} else {
return false;
}
}
Common.toggleDisplay = function(sPanel) {
var oPanel = document.getElementById(sPanel);
if (oPanel.style.display == 'block')
Common.hideBlock(sPanel);
else
Common.showBlock(sPanel);
}
Common.showBlock = function(sPanel) {
var oPanel = document.getElementById(sPanel);
oPanel.style.display = 'block';
}
Common.hideBlock = function(sPanel) {
var oPanel = document.getElementById(sPanel);
oPanel.style.display = 'none';
}
/** ************ Read, write and clear user cookies ************ */
Common.getCookie = function(name) {
var cookies = document.cookie.split(/[; ]+/);
for ( var i = 0; i < cookies.length; i++) {
var aName = cookies[i].substring(0, cookies[i].indexOf('='));
if (aName == name) {
var cookie = cookies[i].substring(cookies[i].indexOf('=') + 1);
return unescape(cookie);
}
}
return null;
}
Common.setCookie = function(name, value, context) {
var expires = new Date();
expires.setDate(expires.getDate() + 365);
var newCookie = name + "=" + escape(value) + ";expires=" + expires
+ ";path=" + context;
document.cookie = newCookie;
}
Common.setSessionCookie = function(name, value, context) {
var newCookie = name + "=" + escape(value) + ";path=" + context; //IE (6, 7, 8 and 9) do not accept Expire=0 when setting a session cookie so that parameter is skipped
document.cookie = newCookie;
}
Common.setCookieExp = function(name, value, context, hours) {
var now = new Date();
expires = hours * 60 * 60 * 1000;
expires = new Date(now.getTime() + expires);
var newCookie = name + "=" + escape(value) + ";expires="
+ expires.toGMTString() + ";path=" + context;
document.cookie = newCookie;
}
Common.setDomainCookieExp = function (name, value, context, domain, hours) {
var now = new Date();
expires = hours * 60 * 60 * 1000;
expires = new Date(now.getTime() + expires);
var newCookie = name + "=" + escape(value) + ";expires="
+ expires.toGMTString() + ";domain=" + domain + ";path=" + context;
document.cookie = newCookie;
}
Common.clearCookies = function(context) {
var res = 1;
if (context == '/') {
res = confirm(
"This will clear any content and accessibility preferences you may have set on the site. Do you want to continue?",
"Reset");
}
if (res) {
var c = document.cookie.split(";");
for ( var i = 0; i < c.length; i++) {
var e = c[i].indexOf("=");
var n = e > -1 ? c[i].substr(0, e) : c[i];
document.cookie = n
+ "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=" + context;
}
location.reload(true);
}
}
Common.deleteCookie = function(name) {
var cookies = document.cookie.split(/[; ]+/);
for ( var i = 0; i < cookies.length; i++) {
var aName = cookies[i].substring(0, cookies[i].indexOf('='));
if (aName == name) {
document.cookie = aName
+ "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
}
}
// location.reload(true);
}
/** ********* Loads the specified page content using Ajax *************** */
Common.loadAjaxComponent = function(filename, params, targetid) {
var ajaxData = params;
var ajaxResponse = jQuery
.ajax( {
url :SITE_ROOT + "/components/" + filename,
data :ajaxData,
dataType :"html",
async :true,
timeout :120000,
success : function(data, textStatus, XMLHttpRequest) {
Common.replaceWithAjax(data, targetid);
},
error : function() {
Common
.replaceWithAjax(
"Sorry--could not load branch holdings. Please try again.",
targetid);
}
})
return ajaxResponse;
}
/** ********* Function for Ajax-based loader above *************** */
Common.replaceWithAjax = function(data, targetid) {
if (targetid != '' && targetid != null) {
var element = document.getElementById(targetid);
if (element != null)
element.innerHTML = data;
}
}
/**
* ***********clears out an input box with
*
* @id **********************
*/
Common.clearInputBox = function(id) {
var element = document.getElementById(id);
element.value = "";
}
/** ************ Member login display for header ***************** */
Common.displayMemberLinks = function() {
var account_url = "http://catalogue.torontopubliclibrary.ca/uhtbin/cgisirsi/x/TPL/0/57/30?";
var login_url = "http://ezproxy.torontopubliclibrary.ca/login?url=http://ezproxy.torontopubliclibrary.ca/sso/myacct";
var user_id = Common.getCookie('user_id');
var password = Common.getCookie('password');
var oLinks = document.getElementById("member-links");
if (oLinks != null) {
var msg = '';
if (Common.getCookie('user_id') != "") {
msg = +'You are logged in | ';
var full_url = account_url + "user_id=" + user_id + "&password="
+ password;
msg = +'Your Account';
} else {
msg = +'Sign In / Register';
}
oLinks.innerHtml = msg;
}
}
/**
* ************ Cookie incrementer & functions for Place Hold history manipulation
* *****************
*/
Common.getStepsSinceHold = function() {
return Common.getCookie("stepsSinceHold");
}
Common.incrementStepsSinceHold = function() {
currentSteps = Common.getCookie("stepsSinceHold");
Common.setCookie("stepsSinceHold", parseInt(currentSteps) + 1, "/");
}
Common.returnFromHold = function() {
historyLength = history.length;
currentSteps = Common.getCookie("stepsSinceHold");
// If their currentSteps is greater than their history length, they've opened a new tab, so we should let the link function instead of returning them via history
debugMessage = "Current Steps: " + currentSteps + " | History length: " + historyLength;
if(currentSteps > historyLength) {
return true;
} else {
Common.setCookie("returnFromHold", "true", "/");
history.go(-currentSteps);
return false;
}
}
Common.processSignOutURL = function(url) {
var signOutLink = document.getElementById('sign-out-link');
var signOutURL = url+Common.getCurrentLocationEncoded();
signOutLink.href = signOutURL;
}
Common.processSignInURL = function(url) {
var signInLink = document.getElementById('sign-in-link');
var signInURL = url+Common.getCurrentLocationEncoded();
var isSignedIn = Common.getCookie("returnFromHold");
if( isSignedIn == "true") {
var serverName = (SERVER_NAME.split(":"))[0];
if(SERVER_NAME.indexOf("localhost") != -1) {
serverName = serverName+":7001"; // include port only if on local install
}
signOutURL = "http://"+serverName+"/signout?returnTo="+Common.getCurrentLocationEncoded();
signInLink.href = signOutURL;
signInLink.innerHTML = "You are signed in. Sign out";
Common.deleteCookie("returnFromHold");
} else {
signInLink.href = signInURL;
}
}