function writecookie (cookiename, cookievalue, timeperiod, offset) {
	if (timeperiod != null) {
		// a request to write a persistent cookie
	  var expirationdate = new Date ();
	  offset = offset / 1;
	  var mytimeperiod = timeperiod;
	  switch (mytimeperiod.toLowerCase()) {
	    case "years":
	      expirationdate.setYear(expirationdate.getFullYear()+offset);
	      break;
	    case "months":
	      expirationdate.setMonth(expirationdate.getMonth()+offset);
	      break;
	    case "days":
	      expirationdate.setDate(expirationdate.getDate()+offset);
	      break;
	    case "hours":
	      expirationdate.setHours(expirationdate.getHours()+offset);
	      break;
	    case "minutes":
	      expirationdate.setMinutes(expirationdate.getMinutes()+offset);
	      break;
	    default:
	      alert ("The time period in the cookie-writing request must be years, months, days, hours, or minutes.  What was supplied: " + mytimeperiod)
	      break;
	  }  
	  document.cookie = escape(cookiename) + "=" + escape(cookievalue) + "; expires=" + expirationdate.toGMTString() + "; path=/";
	} else {
		// a request to write a session cookie
		document.cookie = escape(cookiename) + "=" + escape(cookievalue);
	}
} 

function getcookievalue (cookiename) {
  var exp = new RegExp (escape(cookiename) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

function deletecookie( name, path, domain ) {
	if (getcookievalue(name)) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";

}

function showCollection(id,fileloc,type,arc,datatype) {
	var wh = getwindowdimensions()
	wh[0] = wh[0]-50	
	wh[1] = wh[1]-20	
	wh[2] = wh[2]+10	
	wh[3] = wh[3]+10	
	var curl = ""
	arc = escape(arc)
	var attrs = "status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars,resizable,width=" + wh[0] + ",height=" + wh[1] + ",left=" + wh[2] + ",top=" + wh[3]	
	fileloc = escape(fileloc)
	if (type.indexOf("amc") == -1) {
		var curl = webPath + "/fa.jsp"
	} else {
		var curl = webPath + "/a.jsp"
	}
	curl += "?id=" + id + "&fileloc=" + fileloc	+ "&archive=" + arc	
	if (datatype == null) {
		datatype = "";
	}
	curl += "&datatype=" + datatype;
	var cwin = Math.floor(Math.random()*10000)
	cWindow = window.open(curl,cwin,attrs)
	cWindow.focus()
}

function getwindowdimensions() {
	var wh = new Array(4)
	wh[0] = 800;
	wh[1] = 600;
	wh[2] = 20;
	wh[3] = 20;
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appCodeName=="Netscape") {
		  wh[0] = window.innerWidth;
		  wh[1] = window.innerHeight;
		  wh[2] = document.body.x;
		  wh[3] = document.body.y;	
		} else if (navigator.appName.indexOf("Microsoft")!=-1) {
		  wh[0] = document.body.offsetWidth;
		  //wh[1] = document.body.offsetHeight;
		  wh[2] = document.body.offsetLeft;
		  wh[3] = document.body.offsetTop;	  	  	  	  
		} else if (navigator.appCodeName.indexOf("Mozilla")!=-1) {
		  wh[0] = window.innerWidth;
		  wh[1] = window.innerHeight;
		  wh[2] = document.body.offsetLeft;
		  wh[3] = document.body.offsetTop;	  	  
		} 
	}
	return wh
}

function showArchive(id,name) {
	if (name.indexOf("'") != -1) {
		var n = name.split("'")
		name = n.join("%27")
	}	
	var wh = getwindowdimensions()
	wh[0] = wh[0]-150	
	wh[1] = wh[1]-50	
	wh[2] = wh[2]+20	
	wh[3] = wh[3]+20	
	var aurl = webPath + "/ci.jsp?id=" + id + "&archive=" + name
	var attrs = "status=yes,toolbar=yes,menubar=yes,location=yes,scrollbars,resizable,width=" + wh[0] + ",height=" + wh[1] + ",left=" + wh[2] + ",top=" + wh[3]
	var winopen = false
	if (opener != null) {
		if (opener.parent != null) {
			if (opener.parent.aWindow != null) {
				winopen = true
			}
		}
	}
	if (winopen) {
		if (!opener.parent.aWindow.closed && opener.parent.aWindow.location) {
			opener.parent.aWindow.location.href = aurl;
		} else {
			opener.parent.aWindow = window.open(aurl,'awin',attrs);
		}
		opener.parent.aWindow.focus()
	} else {
		if (aWindow == null) {
			aWindow = window.open(aurl,'awin',attrs);			
		} else if (!aWindow.closed && aWindow.location) {
			aWindow.location.href = aurl;
		} else {
			aWindow = window.open(aurl,'awin',attrs);
		}
		aWindow.focus()
	}
}

function searchToggle(srch) {
	var d = document.location.href
	if (d.indexOf("&s=") != -1 || d.indexOf("?s=") != -1) {
		if (d.indexOf("&s=") != -1) {
			var d1 = d.substring(0,d.indexOf("&s="))
			var d2 = d.substring(d.indexOf("&s=")+4)
			d = d1 + d2
		} else {
			var d1 = d.substring(0,d.indexOf("?s="))
			var d2 = d.substring(d.indexOf("?s=")+4)
			d = d1 + "?"+ d2			
		}
	}
	d += "&s=" + srch
	document.location.href = d
}

function narrow(fld,terms,label) {
	var d = document.location.href
	d = removeparameter(d,"st");
	d = removeparameter(d,"act");
	d = removeparameter(d,"nar");
	if (fld.indexOf("contributor") != -1) { 
		terms = terms + "_" + label
	} else {
		terms = "%22" + terms + "%22"
	}
	// encode stray ampersands in terms
	terms = replace(terms,"&","%26");
	d += "&" + fld + "=" + terms
	d += "&nar=1"
	document.location.href = d
}

function dosort(sortorder) {
	var d = document.location.href
	// look for sort and act parameters, and remove them
	d = removeparameter(d,"sort");
	d = removeparameter(d,"act");
	d = removeparameter(d,"nar");
	// add new sort and act parameters	
	d += "&act=sort"
	d += "&sort=" + sortorder
	// if there's a start parameter, take it out (to start at the beginning)
	d = removeparameter(d,"st");	
	document.location.href = d
}

// turn a printer-friendly view on or off
function pf(setting) {
	var d = document.location.href
	d = removeparameter(d,"pf");
	if (setting.indexOf("y") != -1) {
		if (d.indexOf("?") == -1) {
			d = d + "?pf=y"
		} else {
			d = d + "&pf=y"
		}
	}
	document.location.href = d;
}

// remove a parameter from a url
function removeparameter(ref,parm) {
	var len = parm.length + 3
	if (ref.indexOf("&" + parm + "=") != -1 || ref.indexOf("?" + parm + "=") != -1) {
		if (ref.indexOf("&" + parm + "=") != -1) {
			var d1 = ref.substring(0,ref.indexOf("&" + parm + "="))
			var d2 = ref.substring(ref.indexOf("&" + parm + "=")+len)
			if (d2.indexOf("&") != -1) {
				d2 = d2.substring(d2.indexOf("&"))
				ref = d1 + d2 
			} else {
				ref = d1
			}
		} else {
			var d1 = ref.substring(0,ref.indexOf("?" + parm + "="))
			var d2 = ref.substring(ref.indexOf("?" + parm + "=")+len)
			if (d2.indexOf("&") != -1) {
				d2 = d2.substring(d2.indexOf("&")) 
				ref = d1 + "?" + d2
			} else {
				ref = d1
			}					
		}		
	}
	return ref;
}

// get a keyword search and send it
function ksearch(terms) {
	var kurl = webPath + "/s.jsp?q=%22" + terms + "%22"
	if (opener != null) {
		opener.parent.window.focus()
		opener.parent.document.location.href = kurl
	} else {
		document.location.href = kurl
	}
}

// get a contributor search and send it
function csearch(terms,away) {
	window.focus()	
	var curl = webPath + "/s.jsp?"
	// correct apostrophe placeholder
	if (terms.indexOf("'") != -1) {
		var termsarr = terms.split("'")
		terms = termsarr.join("%27")
	}
	if (away.indexOf("contributor") != -1) {
		curl += "contributor=" + terms
	} else {
		terms = terms.substring(terms.indexOf("_")+1)
		curl += "a=%22" + terms + "%22"
	}
	if (document.location.href.indexOf("?s=a") != -1 || document.location.href.indexOf("&s=a") != -1) { 
		curl += "&s=a"
	} else {
		curl += "&s=s"
	}
	document.location.href = curl
}

// show a list of all the contributors
function allcontributors(type) {
	var curl = ""
	var attrs = "status=yes,menubar=yes,location=yes,scrollbars,resizable,width=800,height=600,top=50,left=50"	
	var curl = webPath + "/contributorlist.jsp"
	if (type != null) {
		if (type.indexOf("fa") != -1) {
			curl = webPath + "/facontributorlist.jsp"
		}
	}
	cWindow = window.open(curl,"cwin",attrs)
	cWindow.focus()
}

// show a list of all the contributors
function facet(type) {
	var furl = document.location.href.substring(document.location.href.indexOf("?"))
	furl = webPath + "/facets.jsp" + furl + "&facet=" + type;
	var attrs = "status=yes,menubar=yes,location=yes,scrollbars,resizable,width=275,height=600,top=10,left=10"	
	fWindow = window.open(furl,"fwin",attrs)
	fWindow.focus()
}

// convert a keyword phrase search to a proximity search
function proximity(type) {
	var within = 4
	var disclaimer = "\n\nThis is a system error, not a problem with your search.\n"	
	var phrasemsg = "\nThe last search doesn't include a keyword phrase so it can't be converted into a proximity search."
	phrasemsg += disclaimer
	var wordmsg = "\nThe last search doesn't include keywords so it can't be converted into a proximity search."
	wordmsg += disclaimer	
	var purl = document.location.href
	if (purl.indexOf("q=") != -1) {
		var qs = ""
		var p1 = purl.substring(0,purl.indexOf("q="));
		var p2 = purl.substring(purl.indexOf("q=")+2);
		qs = p2
		if (p2.indexOf("&") != -1) {
			qs = p2.substring(0,p2.indexOf("&"))
			p2 = p2.substring(p2.indexOf("&"))
		} else {
			p2 = ""
		}
		if (type.indexOf("phrase") != -1) {
			var qf = qs.substring(0,3)
			var qsl = qs.length - 3
			var ql = qs.substring(qsl,qs.length)
			if (qf.indexOf("%22") == -1 || ql.indexOf("%22") == -1) {
				alert(phrasemsg)
			} else {
				qs += "%7E" + within
				purl = p1 + "q=" + qs + p2
				document.location.href = purl
			}
		} else {
			qs = "%22" + qs + "%22%7E" + within
			purl = p1 + "q=" + qs + p2
			document.location.href = purl
		}	
	} else {
		alert(wordmsg)
	}
}

// convert a keyword phrase search to a word search
function unphrase() {
	var msg = "\nThe last search doesn't include a keyword phrase so it can't be converted into a word search."
	msg += "\n\nThis is a system error, not a problem with your search.\n"
	var purl = document.location.href
	if (purl.indexOf("q=") != -1) {
		var qs = ""
		var p1 = purl.substring(0,purl.indexOf("q="));
		var p2 = purl.substring(purl.indexOf("q=")+2);
		qs = p2
		if (p2.indexOf("&") != -1) {
			qs = p2.substring(0,p2.indexOf("&"))
			p2 = p2.substring(p2.indexOf("&"))
		} else {
			p2 = ""
		}
		var qf = qs.substring(0,3)
		var qsl = qs.length - 3
		var ql = qs.substring(qsl,qs.length)
		if (qf.indexOf("%22") == -1 || ql.indexOf("%22") == -1) {
			alert(msg)
		} else {
			qs = qs.substring(3)
			qs = qs.substring(0,qs.length-3)
			purl = p1 + "q=" + qs + p2
			document.location.href = purl
		}
	} else {
		alert(msg)
	}
}

// convert a keyword search to a phrase search
function phrase() {
	var msg = "\nThe last search doesn't include keywords so it can't be converted into a word search."
	msg += "\n\nThis is a system error, not a problem with your search.\n"
	var purl = document.location.href
	if (purl.indexOf("q=") != -1) {
		var qs = ""
		var p1 = purl.substring(0,purl.indexOf("q="));
		var p2 = purl.substring(purl.indexOf("q=")+2);
		qs = p2
		if (p2.indexOf("&") != -1) {
			qs = p2.substring(0,p2.indexOf("&"))
			p2 = p2.substring(p2.indexOf("&"))
		} else {
			p2 = ""
		}
		purl = p1 + "q=%22" + qs + "%22" + p2
		document.location.href = purl
	} else {
		alert(msg)
	}
}

// replace a string within a string
function replace(instr, oldstr, newstr) {
	retStr = "";
	tmpstr = "";
	pos1 = 0;
	pos2 = 0;
	len = oldstr.length;
	while (true) {
		pos2 = instr.indexOf(oldstr, pos1);
		if (pos2>-1) {
			tmpstr = instr.substring(pos1, pos2);
			retStr += tmpstr;
			retStr += newstr;
		} else {
			retStr += instr.substring(pos1);
			break;
		}
		pos1 = pos2 + len;
	}
	return retStr;
}

// prompt a survey (up to two times for a session that hasn't seen the survey yet)
function survey() {
	var surveyOn = false // set this boolean to true, to turn on the survey
	var vfirst = 2 // the number of screens before the survey if first prompted
	var vlast = 6 // the number of screens before the survey is prompted for the last time
	if (surveyOn) { 
	  writecookie("testPersistentCookie", "Enabled", "minutes", 1);
	  writecookie("testSessionCookie", "Enabled");
	  if (getcookievalue("testPersistentCookie") == "Enabled") {
	    if (getcookievalue ("survey_answered") != "yes") {
	    	var viewinc = getcookievalue("psurvey_viewinc")
	    	if (viewinc == false) { viewinc = 0 }
	    	viewinc++
	    	writecookie('psurvey_viewinc',viewinc,'years',1)
	    	if (viewinc == vfirst || viewinc == vlast) {	   	
		    	showHide("survey")
	    	}
	  	}
	  } else if (getcookievalue("testSessionCookie")=="Enabled") {
	    if (getcookievalue ("survey_answered") != "yes") {
	    	var viewinc = getcookievalue("tsurvey_viewinc")
	    	if (viewinc == false) { viewinc = 0 }
	    	viewinc++
	    	writecookie('tsurvey_viewinc',viewinc)
	    	if (viewinc == vfirst || viewinc == vlast) {	   	
		    	showHide("survey")
	    	}
	  	}  	
	  }
	} // end if surveyOn 
} // end survey	

// set cookies if the survey is answered, and submit the survey form
function surveyanswered() {
  writecookie("testPersistentCookie", "Enabled", "minutes", 1);
  if (getcookievalue("testPersistentCookie") == "Enabled") {	
		writecookie('survey_answered','yes','years',1)
	} else {
		writecookie('survey_answered','yes')
	}
	document.surveyform.submit();
}

// hide the survey div
function surveyclosed() {
  showHide("survey")
}

// reset survey values [used for debugging]
function resetsurvey() {
  writecookie("testPersistentCookie", "Enabled", "minutes", 1);
  writecookie("testSessionCookie", "Enabled");	
  if (getcookievalue("testPersistentCookie") == "Enabled") {	
		writecookie('survey_answered','no','years',1)
		writecookie('psurvey_viewinc','0','years',1)
	} else if (getcookievalue("testSessionCookie") == "Enabled") {
		writecookie('survey_answered','no')
		writecookie('tsurvey_viewinc','0')			
	}
}

// reset paypal logon form elements
function setform() {
	// if there is a paypal logon form displayed, look for stored cookies to prompt values
	if (document.paypalvalidate) {
		if (getcookievalue("r").length > 0) {
			if (getcookievalue("r").indexOf("y") != -1) {
				document.paypalvalidate.rememberme.checked = true;
				if (getcookievalue("u")) {
					document.paypalvalidate.u.value = getcookievalue("u");
				}
				if (getcookievalue("p")) {
					document.paypalvalidate.p.value = getcookievalue("p");
				}
			}
		}
	}
} // end setform

// remember paypal username and password settings
function checkrememberme(form) {
  writecookie("testPersistentCookie", "Enabled", "minutes", 1);
  writecookie("testSessionCookie", "Enabled");	
	if (form.rememberme.checked) {	
	  if (getcookievalue("testPersistentCookie") == "Enabled") {	
			writecookie('r','y','years',1)
			writecookie('u',form.u.value,'years',1)
			writecookie('p',form.p.value,'years',1)
		} else if (getcookievalue("testSessionCookie") == "Enabled") {
			writecookie('r','y')
			writecookie('u',form.u.value)
			writecookie('p',form.p.value)
		}
	} else {	
	  if (getcookievalue("testPersistentCookie") == "Enabled") {	
	  	writecookie('r','n','years',1)	
		} else if (getcookievalue("testSessionCookie") == "Enabled") {
			writecookie('r','n')
		}
		deletecookie('u')
		deletecookie('p')
	}	
		
	return true
}

/********************************************************************************
Copyright (C) 1999 Thomas Brattli
This script is made by and copyrighted to Thomas Brattli at www.bratta.com
Visit for more great scripts. This may be used freely as long as this msg is intact!
I will also appreciate any links you could give me.
********************************************************************************/
//Default browsercheck, added to all scripts!
function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}
bw=new checkBrowser()
//With nested layers for netscape, this function hides the layer if it's visible and visa versa
function showHide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	if(obj.visibility=='visible' || obj.visibility=='show') obj.visibility='hidden'
	else obj.visibility='visible'
}
//Shows the div
function show(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.visibility='visible'
}
//Hides the div
function hide(div,nest){
	obj=bw.dom?document.getElementById(div).style:bw.ie4?document.all[div].style:bw.ns4?nest?document[nest].document[div]:document[div]:0;
	obj.visibility='hidden'
}

// if in an opened window, close it and give the main window the focus, otherwise go to the home page
function gohome() {
	if (window.opener && !window.opener.closed) {
		opener.parent.window.focus()
	} else {
		document.location.href = webPath + "/index.jsp"
	}
}

// reload the page (from a "try again" link if there is a sporadic error
function again() {
	document.location.href = document.location.href;
}
