function ltrim(s){ // left trim spaces from a string
	for (var i=0; i<s.length && s.charCodeAt(i)==32; i++);
	return s.slice(i);
};
function rtrim(s){ // right trim spaces from a string
	for (var i=s.length-1; i>0 && s.charCodeAt(i)==32; i--);
	return s.slice(0,i+1);
};
function trim(s){
	return ltrim(rtrim(s));
};
function selectValue(s) {
	return (s.selectedIndex >-1) ? s.options[s.selectedIndex].value : '';
};
function setSelected(s,v) {
	for (var j=0; j<s.options.length; j++) {
		if (s.options[j].value == v) {
			s.options[j].selected = true;
			break;
		};
	};
};
function radioValue(s) {
	for (var j=0; j<s.length; j++)
		if (s[j].checked)
			return s[j].value;

	return "";
};
function myStatus(s) {
	setTimeout("self.status='" + s.title + "'",1);
};
function setCookie(c, v)
{
// setCookie("name","value") john nowlin 4/2004 jnowlin@ccla.lib.fl.us
	if (getCookie(c) != v)
		document.cookie = c + '=' + escape(v) + "; path=/";
};

function getCookie(cName)
{
// value =  getCookie("name") will return cookie value john nowlin 7/2003 jnowlin@ccla.lib.fl.us
	var cookies = '; ' + trim(unescape(document.cookie)) + ";";
	var cookieValueStart = cookies.indexOf("; " + cName + "=");

	if (cookieValueStart == -1) { // not in cookies
		return '';

	} else {	// parse out cookie value
		cookieValueStart += cName.length + 3; // add length of '; Name='
		return cookies.slice(cookieValueStart, cookies.indexOf(";", cookieValueStart));
	};
};

function getOrSetCookie(cookieName, cookieValue)
{
// to set cookie call getOrSetCookie("name","value") will set document cookie
// to get cookie call value = getOrSetCookie("name") will return cookie value
// john nowlin 7/2003 jnowlin@ccla.lib.fl.us

	if (typeof cookieValue != 'undefined') {	// set cookie
		document.cookie = cookieName + '=' + escape(cookieValue) + "; path=/";
		
	} else {
		var cookies = '; ' + trim(unescape(document.cookie)) + ";";
		var cookieValueStart = cookies.indexOf("; " + cookieName + "=");

		if (cookieValueStart == -1) { // cookieName is not in cookies
			return '';

		} else {	// parse out cookie value
			cookieValueStart += cookieName.length + 3; // add length of '; Name='
			return cookies.slice(cookieValueStart, cookies.indexOf(";", cookieValueStart));
		};
	};
};

function queryString(s)
// returns search string value from URL.  Pass in field and returns value
// for example: http://xyz.com/test.htm?a=alpha&b=beta
//   QueryString('a') returns 'alpha'
{
	var searchString = "&" + unescape(window.location.search).slice(1) + "&";
	var searchValueStart = 	searchString.indexOf("&" + s + "=");
	
	if (searchValueStart == -1) {
		return ''

	} else {
		searchValueStart += s.length + 2;  // add length of '&name='
		return searchString.slice(searchValueStart, searchString.indexOf("&", searchValueStart));
	};
};

var formObjects = new Array;

function formObject(o, c, d)
// sets a form object (o) to query string or cookie (c) or default (d)
// determines how to set value based on object type
// example: setSelect(document.form1.local_base, 'college')
// john nowlin 11-2003 jnowlin@ccla.lib.fl.us
{
// save the object, cookie/query
	this.o = o;
	this.c = c;

//object save function used onSubmit
	this.save = function ()
	{
		switch(this.o.type)
		{
			case 'select-one':
				setCookie(this.c, selectValue(this.o));
				break;
				
			case 'text':
				setCookie(this.c, this.o.value);
				break;
				
			default:
				switch(this.o[0].type)
				{
					case 'radio':
						for (j=0; j<this.o.length; j++)
							if (this.o[j].checked)
							{
								setCookie(this.c, this.o[j].value);
								break;
							};
						break;
				};
		};
	};

// set the form object
	this.set = function(setVal)
	{	
		// decide what type of object it is
		switch(this.o.type)
		{
			case 'select-one':
				this.f = this.o.form.name;
				setSelected(this.o, setVal);
				break;

			case 'text':
				this.f = this.o.form.name;
				this.o.value = setVal;
				break;

			default:
				switch(this.o[0].type)
				{
					case 'radio':
						this.f = this.o[0].form.name;
						for (j=0; j<this.o.length; j++)
						{
							if(this.o[j].value == setVal)
							{
								this.o[j].checked = true;
								break;
							};
						};
						break;
				};
		};
	};
	
// reset to query string or default. ignore cookie
	this.clear = function()
	{
		this.set(this.d);
	};
		
// to enable object clearing save the query or default in this.d
	var a=queryString(c);
	this.d=a;
	saveForReset(c,a);
	
	if (a=='')
	{
		a=getCookie(c);
		if (typeof d != 'undefined')
		{
			this.d=d;
			if (a=='')
			{
				a=d;
			};
		};
	};
	
// execute the object set
	this.set(a);
};

function clearFormValues()
// clear all formObjects values to cookie
{
	for (var i=0; i<formObjects.length; i++)
	{
		formObjects[i].clear();
		formObjects[i].save();
	};
};

function saveFormValues(f)
// save all formObjects values to cookie for a specific form pass the form (f) object into saveFormValues.
// for example onSubmit='saveFormValues(this);'
{
	for (var i=0; i<formObjects.length; i++) {
		if (formObjects[i].f == f.name) {
			formObjects[i].save();
		};
	};
};

function setFormValue(o, c, d)
// adds a form object to the formObjects array
// pass object(o), cookie/query (c) name and default (d)
{
	formObjects[formObjects.length] = new formObject(o, c, d);
};

// borrowed from Harvard by way of MNPALS, shortened by john 10-2003 extracts the a href information from the number link
function recordLink(s) {
	document.write(s.slice(0,s.indexOf(">")+1));
};

// sets the display attribute based on browser type
// pass the div ID (o) and the display value (d)
function objDisplay(o,d)
{
	if (document.getElementById) { // newer broswers
		document.getElementById(o).style.display=d
  } else if (document.all) { // IE4+5
		document.all(o).style.display=d
  } else if (document.layers) {  //NS4
		document.layers[o].display=d
  };
};

function saveForReset(c,s)
// save the cookie name and reset value in a cookie to allow clearing all cookies at logoff
{
	var sfr = getCookie('sfr');
	if (sfr.indexOf(',' + c + ':') == -1)
	{
		sfr += ',' + c + ":" + s;
		setCookie('sfr', sfr);
	};
}

function resetAllCookies()
// use the saved values to clear all cookies
{
	var sfr = getCookie('sfr').split(',');
	for (var i=1; i < sfr.length; i++)
	{
		var s = sfr[i].split(':');
		setCookie(s[0], s[1]);
	};
};
function cclaWindow(l,n) { // open a window at location (l) with name (n)
	var w=window.open('',n,'width=800,height=500,menubar=no,scrollbars=yes,resizable=yes,location=no');
	w.location=l; // handle#a
};

locFirst = true;
addHead = true;
function cclaLocLimit(s) { // starts the limiting process
	if (locFirst && (s == '<a href=?loc>l</a>' || s == '<a href=?pst>p</a>')) {
		locFirst = false;
		document.write ('</table><div style="display:none"><table>');
	};
};

function addLocHead() {
	if (addHead) {
		document.write ('<tr class=tr1><td><b>Location</b></td><td><b>Collection</b></td><td><b>Call Number</b></td></tr>');
		addHead = false;
	};
};

function cclaItemCleanup() {
		 // check the item links (?pst) for limiting to selected (?loc) colleges. the parse the links for display
		 // look in /fcc01/tab/edit_field.eng for ~1
		 // <a href=x>College~1Collection~2CallNumber~3Note</a>
		var locs = new Array;
		var doclink, i, j1, j2;
		var linkSplit = new Array;
		var noLocs = true;

		for (i=16; i<document.links.length; i++) { // read item links
			doclink = document.links[i].toString();

			switch (doclink.substr(doclink.indexOf("?"), 4)) {

				case "?loc": // save ?loc links
					i++;
					doclink = document.links[i].toString();
					locs[doclink.slice(doclink.indexOf("sub_library=")+12)] = true;
					addLocHead()
					noLocs = false;
					break;

				case "?pst": // check ?pst links against ?loc links
					doclink = document.links[i+1].toString(); // look ahead for possible missing link
					if (doclink.substr(doclink.indexOf("?"), 4) == "?pst")
						break;

					i++;
					if (noLocs || locs[doclink.slice(doclink.indexOf("sub_library=")+12)]) {

						if (navigator.appName != "Netscape")
							linkSplit = document.links[i].innerText.split('~') // parse link text
						else
							linkSplit = document.links[i].text.split('~');

						addLocHead()
						document.write ('<tr><td>Check availability at &nbsp;&nbsp;<a href=' + doclink + '>' + linkSplit[0] + '</a>'); // write college link

						j1 = 1; // split other items into columns
						for (j2=1; j2<3; j2++) { // columns in 1st row
							document.write ('</td><td>')

							while (j1 < linkSplit.length && linkSplit[j1].slice(0,1) == j2) { // find matching array elements
								document.write (linkSplit[j1].slice(1) + '&nbsp;');
								j1++;
							};
						};

						document.write('</td></tr>');

						if (j1 < linkSplit.length && linkSplit[j1].slice(0,1) == 3) { // write note line
							document.write ('<tr><td></td><td colspan=3>Note: ' + linkSplit[j1].slice(1) + '</td></tr>');
						};
					};
					break;

				case "?all": // show all items link
					i++;
					document.write ('<tr><td>Check availability at &nbsp;&nbsp;<a href=' + document.links[i].toString() + '>All Colleges</a></td></tr>');
					break;

				default: // cleanup between bibs on short results
					if (!addHead) {
						locs = [];
						addHead = true;
					};
			};
		};
};

lb_select = new Array (
["0000","ALL","All Community Colleges"],
["0200","BEC","Brevard Community College"],
["0201","BECCO","Brevard/Cocoa"],
["0202","BECME","Brevard/Melbourne"],
["0204","BECPB","Brevard/Palm Bay"],
["0203","BECTV","Brevard/Titusville"],
["0300","BOC","Broward Community College"],
["0400","CCC","Central Florida Community College"],
["0403","CCCCI","Central Florida/Citrus"],
["0402","CCCOC","Central Florida/Ocala"],
["0500","CJC","Chipola College"],
["0600","DBC","Daytona Beach Community College"],
["0601","DBCDA","Daytona Beach/Daytona"],
["0604","DBCWE","Daytona Beach/West"],
["0700","ECC","Edison College"],
["0703","ECCCH","Edison/Charlotte"],
["0702","ECCCO","Edison/Collier"],
["0704","ECCHE","Edison/Hendry-Glades"],
["0701","ECCLE","Edison/Lee"],
["0800","FJC","Florida Community College at Jacksonville"],
["0805","FJCDE","Florida CC at Jacksonville/Deerwood"],
["0804","FJCDO","Florida CC at Jacksonville/Downtown"],
["0801","FJCKE","Florida CC at Jacksonville/Kent"],
["0806","FJCNA","Florida CC at Jacksonville/Nassau"],
["0802","FJCNO","Florida CC at Jacksonville/North"],
["0803","FJCSO","Florida CC at Jacksonville/South"],
["0900","FKC","Florida Keys Community College"],
["1000","GCC","Gulf Coast Community College"],
["1100","HCC","Hillsborough Community College"],
["1101","HCCBR","Hillsborough/Brandon"],
["1101","HCCDA","Hillsborough/Dale Mabry"],
["1103","HCCPL","Hillsborough/Plant City"],
["1102","HCCYB","Hillsborough/Ybor City"],
["1200","IRC","Indian River Community College"],
["1201","IRCFO","Indian River/Ft. Pierce"],
["1203","IRCSA","Indian River/St. Lucie West"],
["1300","LCC","Lake City Community College"],
["1400","LSC","Lake-Sumter Community College"],
["1401","LSCLE","Lake-Sumter/Leesburg"],
["1403","LSCSO","Lake-Sumter/South Lake"],
["1402","LSCSU","Lake-Sumter/Sumter"],
["1500","MJC","Manatee Community College"],
["1501","MJCBR","Manatee/Bradenton"],
["1502","MJCVE","Manatee/Venice"],
["1600","MDC","Miami Dade College"],
["1607","MDCEE","Miami Dade/Entrepreneurial"],
["1608","MDCHI","Miami Dade/Hialeah"],
["1605","MDCHO","Miami Dade/Homestead"],
["1606","MDCIN","Miami Dade/InterAmerican"],
["1602","MDCKE","Miami Dade/Kendall"],
["1604","MDCME","Miami Dade/Medical Center"],
["1601","MDCNO","Miami Dade/North"],
["1603","MDCWO","Miami Dade/Wolfson"],
["1700","NJC","North Florida Community College"],
["1800","OWC","Okaloosa-Walton College"],
["1900","PCC","Palm Beach Community College"],
["1901","PCCCE","Palm Beach/Lake Worth"],
["1902","PCCEI","Palm Beach/Palm Beach Gardens"],
["1905","PCCBE","Palm Beach/Belle Glade"],
["2000","PHC","Pasco-Hernando Community College"],
["2001","PHCEA","Pasco-Hernando/East"],
["2002","PHCNO","Pasco-Hernando/North"],
["2003","PHCWE","Pasco-Hernando/West"],
["2100","PJC","Pensacola Junior College"],
["2102","PJCMI","Pensacola/Milton"],
["2104","PJCDT","Pensacola/Downtown"],
["2101","PJCPE","Pensacola/Pensacola"],
["2103","PJCWA","Pensacola/Warrington"],
["2200","PKC","Polk Community College"],
["2202","PKCLA","Polk/Lakeland"],
["2201","PKCWI","Polk/Winter Haven"],
["2300","SSC","St. Johns River Community College"],
["2302","SSCOR","St. Johns River/Orange Park"],
["2301","SSCPA","St. Johns River/Palatka"],
["2303","SSCSA","St. Johns River/St. Augustine"],
["2400","SPC","St. Petersburg College"],
["2409","SPCAL","St. Petersburg/Allstate"],
["2402","SPCCL","St. Petersburg/Clearwater"],
["2408","SPCHE","St. Petersburg/Health Education Center"],
["2403","SPCSA","St. Petersburg/St. Petersburg"],
["2404","SPCSE","St. Petersburg/Seminole Community Library"],
["2405","SPCTA","St. Petersburg/Tarpon Springs"],
["2500","SNC","Santa Fe Community College"],
["2600","SCC","Seminole Community College"],
["2601","SCCSA","Seminole/Sanford"],
["2603","SCCOV","Seminole/Oviedo"],
["2700","SOC","South Florida Community College"],
["2800","TCC","Tallahassee Community College"],
["2900","VCC","Valencia Community College"],
["2902","VCCEA","Valencia/East"],
["2903","VCCOS","Valencia/Osceola"],
["2901","VCCWE","Valencia/West"],
["2905","VCCWI","Valencia/Winter Park"],
["0000","ALL","All Community Colleges"],
["","USM01","USM01"]);

// setup college and campus pulldown
function set_lb_select (s,v) {
	for (var i in lb_select)
		s.options[s.length] = new Option(lb_select[i][2], lb_select[i][1]);
	if (typeof v != 'undefined')
		setFormValue(s,v);
};
// setup the reserve room pulldown
function set_lr_select (s,v) { 
	for (var i=1; i<lb_select.length-2; i++)
//		if (lb_select[i][0].slice(-2) != "00" || lb_select[i+1][0].slice(-2) == "00" ) // campuses or single campus college
		if (lb_select[i][0].slice(-2) == "00") // college only
			s.options[s.length] = new Option(lb_select[i][2], lb_select[i][1] + "30");
	if (typeof v != 'undefined')
		setFormValue(s,v);
};
function cclaChangeSearch(s) {
	// fset is saved form name, s is new. only execute if different
	if (s!=fset) {
		if (fset!='') { // turn off previous form and button
			objDisplay('div_' + fset, 'none');

			if (fset=='r') { // if was on reserve room - show buttons, change tabs
				objDisplay('div_buttons', 'block');
				document.images['tabs'].src = '/www_eng/icon/ccla_hdr_search.gif';
			} else {
				document.images['button_' + fset].src = '/www_eng/icon/ccla_button_' + fset + '.gif'
			};
		};

		//enable new active form
		saveForm = s.slice(-1);
		s = s.slice(0,1);
		if (s=='r') { // if going to reserve room - hide buttons, change tabs
			objDisplay('div_buttons', 'none'); 
			document.images['tabs'].src = '/www_eng/icon/ccla_hdr_reserve.gif';
		} else {

			document.images['button_' + s].src = '/www_eng/icon/ccla_button_' + s + '_active.gif';
			saveSrc = document.images['button_' + s].src;
//			saveForm = s;

			if (s=='k'||s=='b') { // hide numeric and command buttons for keyword and heading
				objDisplay('div_sub_a', 'none');
			} else { // display numeric and command on advanced screen
				objDisplay('div_sub_a', 'inline');
			};
		};
		
		objDisplay('div_' + s, 'block');

/*		switch (s) {
			case 'a':
				document.advanced.request[0].focus();
				break;
			case 'b':
				document.browse.scan_start.focus();
				break;
			case 'c':
				document.ccl.ccl_term.focus();
				break;
			case 'n':
				document.numeric.scan_start.focus();
				break;
			case 'r':
				document.reserve.request[0].focus();
				break;
			default:
				document.keyword.request.focus();
		}; */

		fset = s;
	};
};
function buttonOver (s,t) {
	myStatus(s);
	saveSrc = document.images['button_' + t].src;
	if (fset!=t) {
		document.images['button_' + t].src = '/www_eng/icon/ccla_button_' + t + '_over.gif';
	};
};
function buttonOut (s) {
	self.status='';
	document.images['button_' + s].src = saveSrc;
};
function exampleSrc(s) {
	s.form.example.src='/www_eng/icon/ccla_example_'+selectValue(s)+'.gif';
};

function dbLink(){
// transfer control to the databases and carry library preselect
	var lb='0000';
	var l=getCookie('l');
	if (l=="") l=queryString('l');
	if (l!=""){
		for (var i in lb_select){
			if (lb_select[i][1]==l){
				lb=lb_select[i][0];
				break;
			};
		};
	};
	document.location='https://www.linccweb.org/index.asp?lib_code=flcc' + lb;
};

// setup the search on the numeric screen
function setNumericSearch (s) {
	v=s.find_code.value=selectValue(s.scan_code);
	if (v == "LCC"||v == "DDC"){
		s.func.value = "scan";
	} else {
		s.func.value = "find-b";
		if (v == "SYS"||v == "BAR"){
			s.request.value = s.scan_start.value;
		} else {
			s.request.value = trim(s.scan_start.value) + "?";
		}
	};
};

format_select = new Array (
["","All"],
["AU","Audiobook"],
["AV","Audio/Visual (Videos, DVDs etc)"],
["BK","Books"],
["CF","Computer files"],
["MP","Maps"],
["MN","Music Manuscript"],
["MR","Music Recording"],
["SC","Musical Scores"],
["SE","Serials"]);

// setup format pulldown
function set_format_select (s) {
	for (var i in format_select)
		s.options[s.length] = new Option(format_select[i][1], format_select[i][0]);
};

function setOptionalLimits(s){
	set_format_select(s.filter_request_3);
	setFormValue(s.filter_request_1,'lng');
	setFormValue(s.filter_request_2,'yr');
	setFormValue(s.filter_request_3,'typ');
};
// f is k,b,a,n,c non reserve or rk,rb,ra,rn,rc with reserve active
function initSearchScreen(){
	var f = getCookie('f');
	if (f=="") {
		f = queryString('f');
		if (f=="") {
			setCookie('f','k');
			f = 'k';
		};
	};

	fset='';
	cclaChangeSearch(f);
	
	var doc=document.keyword;
	setTimeout('set_lb_select(document.keyword.local_base,"l")');
	setFormValue(doc.find_code,'i','WRD');
	setFormValue(doc.request,'q');
	setTimeout('setOptionalLimits(document.keyword)');

	var doc=document.browse;
	setTimeout('set_lb_select(document.browse.local_base,"l")');
	setFormValue(doc.scan_code,'ib','SUB');
	setFormValue(doc.scan_start,'qb');

	var doc=document.advanced;
	setTimeout('set_lb_select(document.advanced.local_base,"l")');
	setFormValue(doc.find_code[0],'i0','WRD');
	setFormValue(doc.find_code[1],'i1','WSU');
	setFormValue(doc.find_code[2],'i2','WAU');
	setFormValue(doc.request[0],'q0');
	setFormValue(doc.request[1],'q1');
	setFormValue(doc.request[2],'q2');
	setTimeout('setOptionalLimits(document.advanced)');
	setFormValue(doc.request_op[0],'b0',"AND");
	setFormValue(doc.request_op[1],'b1',"AND");

	var doc=document.numeric;
	setTimeout('set_lb_select(document.numeric.local_base,"l")');
	setFormValue(doc.scan_code,'in','ISBN');
	setFormValue(doc.scan_start,'qn');

	var doc=document.ccl;
	setTimeout('set_lb_select(document.ccl.local_base,"l")');
	setFormValue(doc.ccl_term,'qc');

	var doc=document.reserve;
	setTimeout('set_lr_select(document.reserve.local_base,"l")');
	setFormValue(doc.find_code[0],'ir0','WRD');
	setFormValue(doc.find_code[1],'ir1','WOU');
	setFormValue(doc.find_code[2],'ir2','WIN');
	setFormValue(doc.request[0],'qr0');
	setFormValue(doc.request[1],'qr1');
	setFormValue(doc.request[2],'qr2');
	setFormValue(doc.request_op[0],'b0',"AND");
	setFormValue(doc.request_op[1],'b1',"AND");
	
	switch (f.slice(0,1)) {
		case 'a':
			setTimeout('saveFormValues(document.advanced)');
			break;
		case 'b':
			setTimeout('saveFormValues(document.browse)');
			break;
		case 'c':
			setTimeout('saveFormValues(document.ccl)');
			break;
		case 'n':
			setTimeout('saveFormValues(document.numeric)');
			break;
		case 'r':
			setTimeout('saveFormValues(document.reserve)');
			break;
		default:
			setTimeout('saveFormValues(document.keyword)');
	};
};

searchForm='';
saveForm=getCookie('f');
// if already on search screen display correct form else switch to search screen and then display.
function cclaSearchForm(s){
	if (searchForm != '') {
		cclaChangeSearch(s);
	}else{
		setCookie('f',s);
		document.location = window.location.pathname + '?func=file&file_name=find-b';
	};
};

// set mouseovers on links with titles
function setMouseOvers() {
	for (var i=0; i<document.links.length; i++) {
		var a = document.links[i];
		if (a.title !="" && a['onmouseover'] == null) {
			a['onmouseover']=function anonymous(){myStatus(this)};
			a['onfocus']=function anonymous(){myStatus(this)};
			a['onmousedown']=function anonymous(){myStatus(this)};
			a['onmouseout']=function anonymous(){self.status=''};
		};
	};
};