// Project:    Web Reference Database (refbase) <http://www.refbase.net>
// Copyright:  Matthias Steffens <mailto:refbase@extracts.de> and the file's
//             original author(s).
//
//             This code is distributed in the hope that it will be useful,
//             but WITHOUT ANY WARRANTY. Please see the GNU General Public
//             License for more details.
//
// File:       ./javascript/show.js
// Repository: $HeadURL$
// Author(s):  Matthias Steffens <mailto:refbase@extracts.de>
//
// Created:    27-May-07, 00:54
// Modified:   $Date$
//             $Author$
//             $Revision$

// This Javascript allows to dynamically include references from a refbase database within your own
// HTML pages. Function 'showRefs()' expects a query string of 'show.php' parameter=value pairs. More
// info about the 'show.php' API: <http://linking.refbase.net/>, <http://bibliographies.refbase.net/>

// Adopted from sample code by Neil Saunders:
// <http://nsaunders.wordpress.com/2007/02/20/my-first-ajax-for-bioinformatics-page/>

var xmlHTTP;

// List of authors whose names will be printed in bold face:
var authorNames = new Array(
                             /(Herndl, G\.J\.)/g,
							 /(Reinthaler, T\.)/g,
                             /(Winter, C\.)/g
                           );

function showRefs(query) {
	if (query == "none") {
		document.getElementById("refs").innerHTML = "References will be listed here.";
	}
	else {
		//var url = "http://www.microbial-oceanography.eu/refbase/show.php";
        var url = "http://www.microbial-oceanography.eu/refbase/show.php";
		url = url + "?"             + query;
		url = url + "&client="      + "inc-refbase-1.0"; // "inc-" indicates include mechanisms
		url = url + "&wrapResults=" + "0"; // output only a partial document structure containing solely the search results

		// set defaults if some params were not given in the query:
		// (empty param values trigger the database defaults)
		if (query.search(/submit=(Display|Cite|Export|Browse)?(?=&|$)/) == -1)
			url = url + "&submit=Cite"; // note that, currently, only 'submit=Cite' is fully supported

		if (query.search(/showLinks=[01]/) == -1)
			url = url + "&showLinks=1";

		if (query.search(/showRows=\d+/) == -1)
			url = url + "&showRows=100";

		if (query.search(/startRecord=\d+/) == -1)
			url = url + "&startRecord=1";

		if (query.search(/citeStyle=\w+/) == -1)
			url = url + "&citeStyle=Deep%20Sea%20Res"; // the specified citation style must have a matching entry within the 'styles' MySQL table

		if (query.search(/citeOrder=\w+/) == -1)
			url = url + "&citeOrder=year"; // possible values: "author", "year", "type", "type-year"

		if (query.search(/without=/) == -1)
			url = url + "&without=dups";

		getURL(url);
	}
}

function getURL(url) {
	xmlHTTP = getXMLHTTPObject();

	if (xmlHTTP == null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	xmlHTTP.onreadystatechange = stateChanged;
	xmlHTTP.open("GET", url, true);
	xmlHTTP.send(null);
}

function stateChanged() {
	document.getElementById("refs").innerHTML = "Fetching references from literature database... " + "<img src='img/refbase/progress.gif'>";

	if (xmlHTTP.readyState == 4 || xmlHTTP.readyState == "complete") {
		var response = xmlHTTP.responseText;

		if (!response) {
			document.getElementById("refs").innerHTML = "No data returned!";
		}
		else {
			document.getElementById("refs").innerHTML = highlightAuthors(response);
		}
	}

}

function getXMLHTTPObject() {
	var xmlHTTP = null;

	try {
		// Firefox, Opera 8.0+, Safari:
		xmlHTTP = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer:
		try {
			xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHTTP;
}

function highlightAuthors(sourceText) {
	for (var i = 0; i < authorNames.length; i++) {
		if (sourceText.search(authorNames[i]) > 0)
			sourceText = sourceText.replace(authorNames[i], "<b>$1</b>");
	}

	return sourceText;
}

