function lightbox(maxSize)
{
    var lightbox = document.getElementById('lightbox');
    var popupbox = document.getElementById('popupbox');
    var loadMsg = document.getElementById('lightboxLoading').innerHTML;

    popupbox.innerHTML = '<img src="' + path + '/images/loading.gif" /><br />' + loadMsg;

    hideSelects('hidden');

    // Find out how far down the screen the user has scrolled.
    var new_top = YAHOO.util.Dom.getDocumentScrollTop();

    //Get the height of the document
    var documentHeight = YAHOO.util.Dom.getDocumentHeight();

    var winW;
    var winH;
    if (document.body && document.body.offsetWidth) {
    	winW = document.body.offsetWidth;
    	winH = document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
    	winW = document.documentElement.offsetWidth;
    	winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
    	winW = window.innerWidth;
    	winH = window.innerHeight;
    }
    	
    if (maxSize == true) {
        var popupBoxStyleLeft = 20;
    	popupbox.style.left= popupBoxStyleLeft + 'px';
    	popupbox.style.width= winW - 60;
    	popupbox.style.height = winH - 80;
    }
    else {
    	
    	popupbox.style.width='30%';
    	popupbox.style.left='35%';
    }
    lightbox.style.height= documentHeight + 'px';
    lightbox.style.display='block';
    popupbox.style.display='block';
    // NLI: reduce distance from top of window
    popupbox.style.top = new_top + 50 + 'px';
    
    
    
    
}
function resizeImageLightbox()
{
	
    var lightbox = document.getElementById('lightbox');
    var popupbox = document.getElementById('popupbox');
    var popupImgDiv = document.getElementById('popupImgDiv');
    var popupImg = document.getElementById('popupImg');
    var popupboxContent = document.getElementById('popupboxContent');

    if (popupImg.offsetWidth > 1) {
    popupbox.style.width=popupImg.offsetWidth;
    } else {
        popupbox.style.width='50%';
        popupboxContent.style.paddingLeft='20px';
    }
    
    //Get the new height of the document after the image has loaded
    var documentHeight = YAHOO.util.Dom.getDocumentHeight();
    lightbox.style.height= documentHeight + 'px';
        
    //changed this:
    //var documentWidth = YAHOO.util.Dom.getDocumentWidth();
    var winW;
    if (document.body && document.body.offsetWidth) {
    	winW = document.body.offsetWidth;
    }
    if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
    	winW = document.documentElement.offsetWidth;
    }
    if (window.innerWidth) {
    	winW = window.innerWidth;
    }
    	
    var documentWidth = winW;
    var popupBoxStyleLeft = (documentWidth - popupbox.offsetWidth) / 2;
    popupbox.style.left= popupBoxStyleLeft + 'px';
    // alert(documentWidth + ' - ' + popupImgDiv.offsetWidth +  ' - ' + popupBoxStyleLeft);

}

function hideLightbox()
{
    var lightbox = document.getElementById('lightbox');
    var popupbox = document.getElementById('popupbox');

    hideSelects('visible');
    lightbox.style.display='none';
    popupbox.style.display='none';
}

function hideSelects(visibility)
{
    selects = document.getElementsByTagName('select');
    for (i = 0; i < selects.length; i++) {
        selects[i].style.visibility = visibility;
    }
}

function toggleMenu(elemId)
{
    var o = document.getElementById(elemId);
    o.style.display = o.style.display == 'block' ? 'none' : 'block';
}

function getElem(id)
{
    if (document.getElementById) {
        return document.getElementById(id);
    } else if (document.all) {
        return document.all[id];
    }
}

function filterAll(element)
{
    // Go through all elements
    var e = getElem('searchForm').elements;
    var len = e.length;
    for (var i = 0; i < len; i++) {
        //  Look for filters (specifically checkbox filters)
        if (e[i].name == 'filter[]' && e[i].checked != undefined) {
            e[i].checked = element.checked;
        }
    }
}

function jsEntityEncode(str)
{
    var new_str = str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#039;');
    return new_str;
}

// Taken from http://stackoverflow.com/questions/1933602/how-to-getelementbyclass-instead-of-getelementbyid-with-javscript and http://www.dustindiaz.com/top-ten-javascript/
function getElementsByClassName(node,classname) {
  if (node.getElementsByClassName) { // use native implementation if available
    return node.getElementsByClassName(classname);
  } else {
    return (function getElementsByClass(searchClass,node) {
        if ( node == null ) {
          node = document;
        }
        var classElements = [],
            els = node.getElementsByTagName("*"),
            elsLen = els.length,
            pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

        for (i = 0, j = 0; i < elsLen; i++) {
          if ( pattern.test(els[i].className) ) {
              classElements[j] = els[i];
              j++;
          }
        }
        return classElements;
    })(classname, node);
  }
}

// Process Google Book Search json response & update the DOM.
function ProcessGBSBookInfo(booksInfo) {
    ProcessBookInfo(booksInfo, 'gbs');
}

// Process Open Library json response & update the DOM.
function ProcessOLBookInfo(booksInfo) {
    ProcessBookInfo(booksInfo, 'ol');
}

// Function shared between GBS and Open Library
function ProcessBookInfo(booksInfo, provider) {
    var expandedProvider = "";
    if (provider == "gbs") {
        expandedProvider = "Google Book Search";
    } else {
        expandedProvider = "the Open Library";
    }
    for (isbn in booksInfo) {
        var bookInfo = booksInfo[isbn];
        if (bookInfo) {
            if (bookInfo.preview == "full" || bookInfo.preview == "partial") {
                var classNameConcat = provider + isbn;
                var elements = getElementsByClassName(document, classNameConcat), n = elements.length;
                for (var i = 0; i < n; i++) {
                    var e = elements[i];
                    if (e.style.display == 'none') {
                        var recordTitle = e.getAttribute("name");
                        e.onclick = new Function("getLightbox('Search', 'BookPreview', '" + provider + "', '" + bookInfo.preview_url + "', '" + recordTitle + "');return false;");
                        // set the link
                        e.href = bookInfo.preview_url;

                        // Set a tool-tip indicating the preview level
                        if (bookInfo.preview == "full") {
                            e.setAttribute('title', 'View online: Full view Book Preview from ' + expandedProvider);
                        } else {
                            e.setAttribute('title', 'View online: Limited preview from ' + expandedProvider );
                        }

                        //show the element
                        e.style.display = '';
                    }
                }
            }
        }
    }
}

// Function to process Hathi Trust json response & update the DOM.
function ProcessHTBookInfo(booksInfo) {
    for (a in booksInfo) {
        var bookInfo = booksInfo[a];
        var itemsArray = bookInfo.items;
        var e = document.getElementById(a);
        if (e != null && e != undefined) {
            for (var i = 0; i < itemsArray.length; i++) {
                if (e.style.display == 'none') {
                    if (bookInfo.items[i].rightsCode == "pd" || bookInfo.items[i].rightsCode == "world") {
                        e.href = bookInfo.items[i].itemURL;
                        e.style.display = '';
                    }
                }
            }
        }
    }
}

// Function to check all a form's checkboxes
// Supply form ID and input class
function checkAll(form, field)
{
    var getForm = getElem(form).getElementsByTagName('input');
    for (i = 0; i < getForm.length; i++) {
        if (getForm[i].attributes['class'] && getForm[i].attributes['class'].nodeValue == field) {
            getForm[i].checked = true ;
        }
    }
}

// Function to uncheck all a form's checkboxes
// Supply form ID and input class
function uncheckAll(form, field)
{
    var getForm = getElem(form).getElementsByTagName('input');
    for (i = 0; i < getForm.length; i++) {
        if (getForm[i].attributes['class'] && getForm[i].attributes['class'].nodeValue == field) {
            getForm[i].checked = false;
        }
    }
}

// Toggle check on all a form's checkboxes
// Supply form ID and input class
function toggleCheck(selector, form, field)
{
    var toggle = (selector.checked == true)?true:false;
    var getForm = getElem(form).getElementsByTagName('input');
    for (i = 0; i < getForm.length; i++) {
        if (getForm[i].attributes['class'] && getForm[i].attributes['class'].nodeValue == field) {
            getForm[i].checked = toggle;
        }
    }
}

// Gets all the checked checkboxes from a form, creates a search url and passes to lightbox
// Supply form ID, input class and mode (makeString = ids are concatenated with '+', ,makeArray = ids are passed as an array)
function processIds(form, field, mode, module, action, id, lookfor, message, followupModule, followupAction, followupId, params) {
    var setMode = (mode != '' && mode != 'undefined')?mode:'makeString';
    var getForm = getElem(form).getElementsByTagName('input');
    var postParams = [];
    if (getForm) {
        var ids = [];
        var x = 0;
        for (i = 0; i < getForm.length; i++) {
            if (getForm[i].attributes['class'] && getForm[i].attributes['class'].nodeValue == field && getForm[i].checked == true) {
            ids[x] = getForm[i].attributes['value'].nodeValue;
            x++;
            }
        }
        if (ids.length > 0) {
            var idValue = '';
            if (setMode == 'makeString') {
                postParams = ids.join('+');
            }
            else {
                for (i=0; i<ids.length; i++) {
                    postParams += encodeURIComponent("ids[]") + "=" + encodeURIComponent(ids[i]) + "&";
                }
            }
        }
        else {
            postParams = "";
        }

        // If an array of extra parameters was sent in, add it to the POST string:
        var extraParams = (params != '' && params != 'undefined')?params:false;
        if (extraParams && setMode != 'makeString') {
            for (i in extraParams) {
                postParams += encodeURIComponent(i) + "=" + encodeURIComponent(extraParams[i]) + "&";
            }
        }
    }
    getLightbox(module, action, id, lookfor, message, followupModule, followupAction, followupId, postParams);
}


// NLI: functions
	//Set & clear suggestions in homepage empty searchbox
    function styleDefault(e) {
    var el = document.getElementById(e);
    el.defaultValue ="Keyword(s) ..."; 
    if (el.defaultValue==el.value) { 
			el.style.color = "#9F9F9F";

		} 
	resetDefault(el)
    }

	function clearDefault(el) {
		if (el.defaultValue==el.value) { 
			el.value = "" ;
			el.style.color = "#000" ;
		}
	}

	function resetDefault(el) {
		 if (el.value == "") { 
			el.value = el.defaultValue ;
			el.style.color = "#9F9F9F" ;
		 }
	}
	
	function cleanCallNumber() {
		
		var selectEl = document.getElementById('searchSelectBasic'); 
	     	if (selectEl.value === 'CallNumber') {
	     	var el = document.getElementById('searchBoxBasic');
			var strVal = el.value;
			strVal = strVal.replace(/\(/g, '');
			strVal = strVal.replace(/\)/g, '');
			el.value = strVal;
	
		}
	}
	
  //get a URL parameter by name
  function gup( name ) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// used to hide small bookcovers from full-record page	
function checkFullRecImgSize()
{
	var tmpFullRecBookCover = document.getElementById('fullRecBookCover');

    if (tmpFullRecBookCover.height > 130) {
    	tmpFullRecBookCover.style.display='block';
    }
}

function loadjsfile(filename){
	 var fileref=document.createElement('script');
	 fileref.setAttribute("type","text/javascript");
	 fileref.setAttribute("src", filename);
	 
	 if (typeof fileref!="undefined")
	  document.getElementsByTagName("head")[0].appendChild(fileref)
	}
