/**
 * @description [page navigation - memorize search form information - display database popup information - adjust limits on search form]
 */

if (!Function.prototype.bind) {
	Function.prototype.bind = function(scope){
		var _this = this;
		return function() {
			return _this.apply(scope);
		}
	}
}

function closeTooltip(paramID)
{
var arr1;
arr1 = paramID.split("|");
document.getElementById(arr1[1]).style.display="none";
window.open(arr1[0]);
}

//closes all open popups besides popup tag passed in
function databaseInfoPopup(idPopup){
	var arrPopups=new Array("dbInfopi","dbInfopb","dbInfopa","dbInfope","dbInfopq","dbInfoprint","dbInfocpa","dbInfopsa","dbInfopsb","dbInfopsc","dbInfopsd","dbInfopse","dbInfopsl","dbInfopsn","dbInfopsy","infoALL","dbInfopt","dbInfoph");
	var target = document.getElementById(idPopup);
	
	//close all windows, the nopen popup
	
	if(target.style.display=="none"){	
		for (thisID in arrPopups){
			if(document.getElementById(arrPopups[thisID]) != null){
				document.getElementById(arrPopups[thisID]).style.display="none";	
			}
		}		
		target.style.display="";
	}else{//close popup
	 target.style.display="none";	 
	}
}

function closeInfoPopup(){
	var arrPopups=new Array("dbInfopi","dbInfopb","dbInfopa","dbInfope","dbInfopq","dbInfoprint","dbInfocpa","dbInfopsa","dbInfopsb","dbInfopsc","dbInfopsd","dbInfopse","dbInfopsl","dbInfopsn","dbInfopsy","infoALL","dbInfopt");
	//close all windows, the nopen popup
	for (thisID in arrPopups){
		if(document.getElementById(arrPopups[thisID]) != null){
				document.getElementById(arrPopups[thisID]).style.display="none";	
			}
	}		
}

function toggle_visibility(id,sid) {
   var e = document.getElementById(id);
   var source=document.getElementById(sid); 
   
   if(e.style.display == 'block') {
      e.style.display = 'none';
     source.style.display = 'block';
     } 
   else {
      e.style.display = 'block';
      source.style.display='none'; 
      }
}

function toggleVisibility(id, idLink, lstCaptions) {

	//	specify the target to show/hide
	var target = "#"+id+":first";		
	var targetIsHidden = jQuery(target).is(':hidden');
	
	//	toggle the display
	if(targetIsHidden){
		jQuery(target).css({'display':''});
	}else{
		jQuery(target).css({'display':'none'});
	}
	
	//	if the idLink and lstCaptions are specified (see 'advanced search > cited references') 
	//	then we need to change the html of idLink based on the captions specified in the 
	//	pipe delimited list (lstCaptions)
	if(arguments[1] && arguments[2]){
		objLink = jQuery("#" + idLink);
		arrCaptions = lstCaptions.split("|");
		objLinkHtml = arrCaptions[0];
		if(targetIsHidden){
			objLinkHtml = arrCaptions[1];
		}
		jQuery("#" + idLink).html(objLinkHtml);
	}
}

function caption_more_visibility(caption){
	var object = document.getElementById('moreCaption');
	object.innerHTML = caption.substring(0,20) + ' <a href="##" class="unBoldMore" onclick="caption_less_visibility(\''+caption+'\')" >More</a>';
}   

function caption_less_visibility(caption){
	var object = document.getElementById('moreCaption');
	object.innerHTML = caption + ' <a href="##" class="unBoldLess" onclick="caption_more_visibility(\''+caption+'\')" >Less</a>';
}  

function epObject(idOrObject) {
	if(typeof idOrObject == "string")
	 return document.getElementById(idOrObject);
	else if(typeof idOrObject == "object")
	 return idOrObject;
	else epThrow("Unknown type in epObject.  Please use either an id or an object");
}

function epThrow(message, code) {
	alert("Developer error\n\n" + message);
	throw(code);
	return false;
}

function epSetCookie(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
				+ (expires ? '; EXPIRES=' + expires.toGMTString() : '')
				+ (path ? '; PATH=' + path : '')
				+ (domain ? '; DOMAIN=' + domain : '')
				+ (secure ? '; SECURE' : '');
}

function epGetCookie(cookieName) {
	var cookieValue = null;
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1)
	{
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1)
		{
			cookieValue = unescape(document.cookie.substring(posValue, endPos));
		}
		else
		{
			cookieValue = unescape(document.cookie.substring(posValue));
		}
	}
	return cookieValue;
}

function epHighlightElement(element) {
	element = epObject(element);
	element.style.backgroundColor = "yellow";
}

function epResizeIframe() {
	var myIframe= document.getElementsByTagName('iframe')[0];
    if (navigator.appName == "Microsoft Internet Explorer") {
		myIframe.height = myIframe.contentWindow.document.body.scrollHeight;
	}
	else {
		myIframe.height = myIframe.contentWindow.document.body.offsetHeight;
		//alert("changing frame height to "+ myIframe.height +" pixels");
	}
}

/* For adding/removing HTML ============================================================================================= */

function epClearContent(id) {
	returnValue = false;
 	object = document.getElementById(id);
	object.innerHTML = "";
	returnValue = true;
	return returnValue;
}

function epInsertContent(id, content, overwrite) {
	returnValue = false;
	if(overwrite==null) overwrite=true;
 object = document.getElementById(id);
	if(overwrite) {
 	object.innerHTML = content;
		returnValue = true;
	}
	else {
		if(object.innerHTML.length == 0) {
			object.innerHTML = content;
			returnValue = true;
		}
		else
		returnValue = false;
	}
	return returnValue;
}

function epAppendListItem(idOfList, html) {
	returnValue = false;
	var objList = epObject(idOfList);
	objList.innerHTML += html;
	
	return returnValue;
}

/* Page Navigator Object =============================================================================================== */

function epPageNavigator(url, lstRenderingTargetIDs, totalPageCount, activePageNumber, pagesPerGroup) {
	if(url==null) epThrow("url not specified");
	if(lstRenderingTargetIDs==null) epThrow("lstRenderingTargetIDs not specified");
	if(totalPageCount==null) epThrow("totalPageCount was not specified");
	if(activePageNumber==null) activePageNumber=1;
 if(pagesPerGroup==null) pagesPerGroup=4;
	this.url=url;
	this.lstRenderingTargetIDs = lstRenderingTargetIDs; 
 this.totalPageCount = totalPageCount;
	this.activePageNumber = activePageNumber;
	this.pagesPerGroup = pagesPerGroup;
	/* Calculate useful numbers */
 var offsetFromGroupIndex = this.activePageNumber % this.pagesPerGroup;
	if(offsetFromGroupIndex==0) offsetFromGroupIndex = this.pagesPerGroup;
	this.groupIndex = this.activePageNumber - offsetFromGroupIndex  + 1; /* i.e., first page number in this group */
	this.render = function() {
  var arrTargetIDs = this.lstRenderingTargetIDs.split(",");
		var htmlContent="";
		var htmlClass = "";
		var htmlAnchorOpen = "";
		var htmlAnchorClose = "</a>";
		/* Auto-width isn't working well with our CSS rules, so we'll be explicit */
		var liWidth = 1;
		for(var h=this.groupIndex; h<this.groupIndex+this.pagesPerGroup; h++) {
			if(h==this.activePageNumber) htmlClass="active"; else htmlClass="";
			if(h>this.totalPageCount) htmlClass+=" invisible";
			hAsString = h + "";
			liWidth = (0.5 * hAsString.length) + 0.7;
			htmlAnchorOpen = "<a href=\"" + this.url + h + "\">";
			htmlContent += "<li class=\"" + htmlClass + "\" style=\"width:" + liWidth + "em;\">" + htmlAnchorOpen + h + htmlAnchorClose + "</li>";
		}		
		for(var i=0; i<arrTargetIDs.length; i++) {
			epInsertContent(arrTargetIDs[i], htmlContent);
		}
	}
	this.setRenderTargetIDs = function(targetIDs) {
		this.lstRenderingTargetIDs = targetIDs;
	}
	this.setPagesPerGroup = function(numPages) {
		this.pagesPerGroup = numPages;
	}
	this.first = function() {
		window.location = this.url + "1";
	}
	this.last = function() {
		window.location = this.url + totalPageCount;
	}
	this.scroll = function(direction) {
		if(direction==1) {
			var nextGroupIndex = this.groupIndex + this.pagesPerGroup;
			if(nextGroupIndex <= this.totalPageCount) {
				var pageOffset = (this.activePageNumber%this.pagesPerGroup);
				if(pageOffset == 0) pageOffset = this.pagesPerGroup;
    				nextPageNumber = (this.activePageNumber - pageOffset)+this.pagesPerGroup+1;
				window.location=this.url+nextPageNumber;
				//this.groupIndex = nextGroupIndex;
				//this.render();
			}	
		}
		if(direction==-1) {
			var previousGroupIndex = this.groupIndex - this.pagesPerGroup;
			if(previousGroupIndex > 0) {
				var pageOffset = (this.activePageNumber%this.pagesPerGroup);
				if(pageOffset == 0) pageOffset = this.pagesPerGroup;
				prevPageNumber = this.activePageNumber - pageOffset;
				window.location=this.url+prevPageNumber;
				//this.groupIndex = previousGroupIndex;
				//this.render();
			}
		}
	}	
	this.render();
}
																									
/* Row Controller Object =============================================================================================== */
function epMultipleRowController(htmlTemplate, idOfParentList, itemClass, firstItemClass, lastItemClass) {
	if(htmlTemplate==null) epThrow("htmlTemplate was not defined");
	if(idOfParentList==null) epThrow("idOfParentList was not defined");
	if(itemClass==null) itemClass="";	
	if(firstItemClass==null) firstItemClass="";
	if(lastItemClass==null) lastItemClass="";
	this.htmlTemplate = htmlTemplate;
	this.idOfParentList = idOfParentList;
	this.itemClass = itemClass;
	this.firstItemClass = firstItemClass;
	this.lastItemClass = lastItemClass;
	this.objParentList = epObject(this.idOfParentList);
	this.getItemCount = function() {
		var returnValue = 0;
		for(var index=0; index < this.objParentList.childNodes.length; index++) {
			var thisChild = this.objParentList.childNodes[index];
			if(thisChild.nodeType== 1) {  //ELEMENT_NODE 1 - HTML element object node (i.e., not a text node, etc)
			 returnValue++;
			}
		}
		return returnValue;
	}
	this.getLastItem = function() {
		for(var index=this.objParentList.childNodes.length-1; index >= 0; index--) {
 		var thisChild = this.objParentList.childNodes[index];
 		if(thisChild.nodeType== 1) {  //ELEMENT_NODE 1 - HTML element object node (i.e., not a text node, etc)
 		 return thisChild;
 		}
		}
 	return null;
	}
	this.addRow = function() {
		var itemCount = this.getItemCount();
		var newRowNumber = itemCount + 1;
		var objLastItem = this.getLastItem();
		var newItem = document.createElement("li");
		newItem.className = this.itemClass + " " + this.lastItemClass;
  		newItem.innerHTML = this.htmlTemplate.replace(/@/g,newRowNumber);
		//objLastItem.className = this.itemClass; // Remove the lastItemClass value, in case it's present
		this.objParentList.appendChild(newItem);
		
		if (this.getItemCount() > 2 && this.idOfParentList == "advsLookForLines"){ 
			document.getElementById("idLookForRemoveARow").style.display="";
		}else if (this.getItemCount() > 2 && this.idOfParentList == "advsLimitLines"){ 
			document.getElementById("idLimitsRemoveARow").style.display="";
		}
	}		
	this.removeRow = function() {
		var itemCount = this.getItemCount();
		if(itemCount > 1) {
			var objLastItem = this.getLastItem();
			if(objLastItem!=null) this.objParentList.removeChild(objLastItem);			
		}
		
		if (this.getItemCount() <= 2 && this.idOfParentList == "advsLookForLines"){ 
			document.getElementById("idLookForRemoveARow").style.display="none";
		}else if (this.getItemCount() <= 2 && this.idOfParentList == "advsLimitLines"){ 
			document.getElementById("idLimitsRemoveARow").style.display="none";
		}
	}
	this.rollCall = function() {
		var debugInfo = "";
		for(var index=1; index < this.objParentList.childNodes.length; index++) {
			var thisChild = this.objParentList.childNodes[index];
			if(thisChild.nodeType== 1) {  //ELEMENT_NODE 1 - HTML element object node (i.e., not a text node, etc)
			 debugInfo += thisChild.className + " " + thisChild.nodeType + '\n';
			}
		}
		alert(debugInfo);
	}
}

/* Search Memorizer Object =============================================================================================== */

function epSearchMemorizer(myName,idOfSearchForm,filter,idOfAncestor) {
	this.name = myName;
	this.searchFormID = idOfSearchForm;
	this.searchForm = document.getElementById(this.searchFormID);
	this.filter = filter;
	if(idOfAncestor) this.ancestorID = idOfAncestor; else this.ancestorID="";
	this.memorizeSearch = function(targetURL) {
		var targetURLFieldExists = document.getElementsByName("epsmTargetURL").length;
		//	alert('psycnet3.js epSearchMemorizer.memorizeSeach()');
		if(targetURLFieldExists) {
			document.getElementsByName("epsmTargetURL")[0].value = targetURL;
		}
		else {
			var newField = document.createElement("input");
			newField.type="hidden";
			newField.name="epsmTargetURL";
			newField.value=targetURL;
			newField.style.display="none";
			this.searchForm.appendChild(newField);
		}
		this.searchForm.action="index.cfm?fa=search.memorize&ancestor=" + this.ancestorID;
		this.searchForm.submit();
	}
	this.changeLinks = function() {
		arrAnchors = document.getElementsByTagName("a");
		var strAnchors="";
		for(var i=0; i<arrAnchors.length; i++) {
			var thisAnchor = arrAnchors[i]; 
			strAnchors += thisAnchor.href + thisAnchor.href.indexOf("javascript:") + "\n";
			if(thisAnchor.href.indexOf("javascript:")==-1 && thisAnchor.href.length && (thisAnchor.href.indexOf(this.filter)!=-1 || this.filter.length==0)) {
				thisAnchor.oldLink = thisAnchor.href;
				// Chicanery to conceal our link-switch by setting the status text to the old href.  Firefox disables status text editing by default.  Oh, well.
				thisAnchor.onmouseover=function() {window.status=this.oldLink; return true;} // "this" refers to the anchor, not the search memorizer
				thisAnchor.onmouseout=function() {window.status=""; return true;}
				// Rewrite the actual links
			    thisAnchor.href="javascript:"+ this.name + ".memorizeSearch(\"" + thisAnchor.href + "\");";
				//Might be able to use something like this to solve 1989:
				thisAnchor.onClick="javascript:"+ this.name + ".memorizeSearch(\"" + thisAnchor.href + "\");";				
			}			
		}
	}	
 this.changeLinks();
}

/* Stylesheet Management =============================================================================================== */

function epAddStylesheet(file,title) {
 var newStyle = document.createElement("link");
	newStyle.rel = "stylesheet";
	newStyle.type = "text/css";
	newStyle.media = "screen";
 newStyle.href = file;
	newStyle.title = title;
	var documentHead = document.getElementsByTagName("head")[0];
	documentHead.appendChild(newStyle);
}

function epListStylesheets() {
	var arrStyles = epGetStyleTags();
	for(var i=0; i < arrStyles.length; i++) {
		thisStyle = arrStyles[i];
		alert(thisStyle.getAttribute("href"));
	}
}

function epGetStyleTags() {
	var arrLinkTags = document.getElementsByTagName("link");
	var arrStyles = new Array();
	for(var i=0; i < arrLinkTags.length; i++) {
		thisLinkTag = arrLinkTags[i];
		if(thisLinkTag.getAttribute("rel")=="stylesheet") arrStyles[arrStyles.length]=thisLinkTag;
	}
 return arrStyles;	
}

function epRemoveStylesheet(title) {
	var arrStyles = epGetStyleTags();
	for(var i=0; i < arrStyles.length; i++) {
		thisStyle = arrStyles[i];
		if(thisStyle.title==title) thisStyle.parentNode.removeChild(thisStyle);
	}
}

function epStyleExists(title) {
	var arrStyles = epGetStyleTags();
	for(var i=0; i < arrStyles.length; i++) {
		thisStyle = arrStyles[i];
		if(thisStyle.title==title) return true;	
	}
	return false;
}

function epToggleOOO() { /* Out-of-order */
	if(epStyleExists("outOfOrder")) {
		epRemoveStylesheet("outOfOrder");
		epSetCookie("oooState","off");
	}
	else {
  epAddStylesheet('css/out_of_order.css','outOfOrder');
		epSetCookie("oooState","on");
	}
}

/* URL Object =============================================================================================== */

function epURL(url) {
	this.url = "";
 this.baseURL = "";
	this.parameterString = "";		
	this.arrParameters = new Array();
 this.setURL(url);
}

epURL.prototype = {
	setURL:function(url) {
		var splitURL = "";
		this.url = new String(url);
		this.baseURL = "";
		this.parameterString = "";		
		splitURL = this.url.split("?");
		this.baseURL = splitURL[0];
		if(splitURL.length==2) this.parameterString = splitURL[1];
		this.arrParameters = new Array();
		this.updateParametersArray();
		return true;
	},
	
	updateParametersArray:function() {
		var nvpString = "";
		var nvpSplit = new Array();
		var nvp = new Object();
			nvp.name = "";
			nvp.value = "";
		var arrParams = this.parameterString.split("&");
		this.arrParameters = new Array();
	
		for(index in arrParams) {
			nvpString = arrParams[index];
			nvpSplit = nvpString.split("=");
			nvp.name = nvpSplit[0];
			if(nvpSplit.length==2) nvp.value = nvpSplit[1]; else nvp.value = "";
			this.arrParameters[this.arrParameters.length] = nvp;			
		}
	},
	
	setParameter:function(name, value, overwrite) {
		var foundName = false;
		if(overwrite==null) overwrite = true;
		if(overwrite) {
			for(index in this.arrParameters) {
				if(this.arrParameters[index].name == name) {
					foundName = true;
					this.arrParameters[index].value = value;
				}
			}
		}
		if(!foundName) {
			this.arrParameters[this.arrParameters.length].name = name;
			this.arrParameters[this.arrParameters.length].value = value;
		}
		var newParameterString = "";
		for(index in this.arrParameters) {
			newParameterString += (this.arrParameters[index].name + "=" + this.arrParameters[index].value);
		}
		this.parameterString = newParameterString;
		if(this.parameterString.length) this.url = this.baseURL + "?" + this.parameterString;
		this.setURL(this.url);
		return true;
	},
	
	open:function() {
		window.location = this.url;
	}
}

//displays progress meter after given number of seconds
function setProgressTimer(seconds){
	var milliseconds = seconds * 1000;
	setTimeout("displayProgressMeter()",milliseconds);
}

function displayProgressMeter(){
	document.getElementById("ProgressIndicator").style.display="";
}

function hideProgressMeter(){
	document.getElementById("ProgressIndicator").style.display="none";
}

function printPage() {
window.print();
}

function openUsageStatement(fa){
	window.open(fa,"","width=650,height=320,resizable=yes,scrollbars=yes,toolbar=yes");
}

function openPurchaseConfirmation(fa){
	window.open(fa,"","width=650,height=320,resizable=yes,scrollbars=yes,toolbar=yes");
}

/* Code by Don Bellenger: */
function openTspIndex(thiscmd)
{
	mywindow='TspHelpWindow';
	childWindow = open(thiscmd,mywindow,'resizable=yes,left=0,top=0,width=800,height=550,scrollbars=yes');
	childWindow.focus();
    if (childWindow.opener == null) childWindow.opener = self;
    // set the global handle
    glchildWindow = childWindow;
	// return childWindow;
	return;
}

//gets the clients IE browser version
function getIEBrowserVersion(){	
	var browserVer = 0;
	var arrTemp = "";
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		arrTemp=navigator.appVersion.split("MSIE");
		browserVer=parseFloat(arrTemp[1]);
	}
	return browserVer;
}

function showDialog(id){
	hideSelectBoxesForIE();
	target = document.getElementById(id);
	target.style.display="";
}

function hideDialog(id){
	showSelectBoxesForIE();
	target = document.getElementById(id);
	target.style.display="none";
}

function showSelectBoxesForIE(){ 
	if(getIEBrowserVersion() == 6) {
 	var selectBoxes = document.getElementsByTagName("select");
	 for(var i=0; i < selectBoxes.length; i++) {
	 	selectBoxes[i].style.visibility="visible";
	 }
	}
}

function hideSelectBoxesForIE(){
	if(getIEBrowserVersion() == 6) {
		var selectBoxes = document.getElementsByTagName("select");
		for(var i=0; i < selectBoxes.length; i++) {
			selectBoxes[i].style.visibility="hidden";
		}	
	}
}

function hideLoginDialog(id){
	target = document.getElementById(id);
	target.style.display="none";
	document.getElementById("idUsername"+id).value = "";
	document.getElementById("idPassword"+id).value = "";
}

function removeHighlightTimed(id,seconds){
	if(elementExists(id)){
		target = document.getElementById(id);
		target.style.display="";
		target.style.backgroundColor="yellow";
		setTimeout("target.style.backgroundColor ='white'",seconds*1000);
	}
}

function elementExists(id){
	if (document.getElementById(id) == null){
		return false;
	}else{
		return true;
	}
}

//	possibly used by prototip.js, so will not refactor jQuery-style
function hideElement(elementName){
	el = document.getElementById(elementName);
	el.style.visibility = "hidden";
	el.style.position = "absolute";
}

//	possibly used by prototip.js, so will not refactor jQuery-style
function showElement(elementName){
	el = document.getElementById(elementName);
	el.style.visibility = "visible";
	el.style.position = "";
}

function getValueListFromCollection(jQueryCollection){
	var valueList = "";
	jQuery.each(jQueryCollection, function(index, value)
		{
			valueList += jQuery(value).val() + ",";
		});
	return valueList;
}
