var nonFreeSites = [
				   'ingenta',
				   'blackwell',
				   'ncbi.nlm.nih.gov',
				   'metapress.com',
				   'proquest.com',
				   'jstor.org',
				   'get_jstor',
				   'journals.cambridge.org',
				   'oup.co.uk',
				   'kluwer',
				   'springer',
				   'oupjournals.org'
				   ];


function nonfree(url) {
	for (var i=0; i < nonFreeSites.length; i++) {
		var re = new RegExp(nonFreeSites[i]);
		if (re.exec(url)) { return true;}
	}
	return false;
}

function getAbstract(name,year,de) {
    if (document.NavForm.pi.value == "ovid") {  
	var str = "((" + name + ") in AU) and (PY=" + year +") and (DE=" + de + ") and (DT: PHIL = CONTRIBUTION or DT:PHIL= JOURNAL-ARTICLE OR DT:PHIL=MONOGRAPH)";
	document.ovid.ss.value = str;
	document.ovid.action = prep("http://web5.silverplatter.com") + "/webspirs/doAndShowSearch.ws";
	document.ovid.submit();
    } else {
	document.NavForm.any.value = "AU=(" + name + ") and PY=" + year + " and DE=(" + de + ")";
        document.NavForm.action = document.NavForm.action1.value + "." + document.NavForm.ezproxy.value + document.NavForm.action2.value;
        document.NavForm.submit();
    }
}

function prep(v) {
	var c = readCookie('ez-server');
	if (c.length > 0) { 
		return v + c;
	} else {
		return v;
	}
}

function getPage(server,page) {
	
//   var rp = page.slice(11);
//   window.location = prep(server) + "/url?sa=U&q=" + escape(rp);
   var rp = page.slice(18);
   var ar = rp.match(/([^\/]+)(\/.*)/);
   var server = RegExp.$1;
   var page = RegExp.$2
   if (nonfree(server)) {
	   window.location = "http://" + prep(server) + page;
   } else {
	  	window.location = "http://" + server + page;   
   }
}

function configure(url) {
	hide('failure'); hide('success');
	var ar = url.match(/www\.jstor\.org(\.[^\/]+\.[^\/]+)/);
	
	if (ar == null) {
		show('failure');
	} else { 
		createCookie('ez-server',RegExp.$1,20000);
		show('success');
	}
}

function get_jstor(author, date, journal, volume) {
	var t = "/search/ArticleLocatorSearch?ArticleTitle=&Author=";
	t += escape(author);
	t += "&JournalTitle=&ISSN=" + journal;
	t += "&MonthSeason=&Day=&Year=" + date + "&vo=" + volume;
	t += "&is=&StartPage=&si=1&hp=25&Search=Search";
    window.location = "http://" + prep("www.jstor.org") + t;
}

//http://www.jstor.org/search/CitationSearch?ArticleTitle=&Author=bealer&JournalTitle=0022362x&ISSN=&MonthSeason=&Day=&Year=1994&vo=91&is=&StartPage=&si=1&hp=25&Search=Search

function get_google(url) {
   var ar = url.match(/http\:\/\/([^\/]+)(\/.*)/);
   var server = RegExp.$1;
   var page = RegExp.$2

}


function get_crossref(author, date, journal, volume, page) {
	document.form1.auth.value = author;
	document.form1.year.value = date;
	document.form1.title.value = journal;
	document.form1.volume.value = volume;
	document.form1.page.value = page;
//	document.form1.action = "http://" + prep("www.crossref.org") + "/guestquery/";
	document.form1.action = "http://" + "www.crossref.org" + "/guestquery/";
	form1.submit();
}

function goToPage(url) {
	var ar = url.match(/http\:\/\/([^\/]+)(\/.*)/);
   var server = RegExp.$1;
   var page = RegExp.$2
   if (nonfree(server)) {
	   window.location = "http://" + prep(server) + page;
   } else {
	  	window.location = "http://" + server + page;   
   }
}

/*
function get_crossref(author, date, journal, volume) {
	var t = "/guestquery/?search_type=journal&auth=";
	t += escape(author);
	t += "&title=" + escape(journal);
	t += "&year=" + escape(date) + "&volume=" + escape(volume) + "&multi_hit=on&view_records=Search";
    window.location = "http://" + prep("www.crossref.org") + t;
}
*/
function writeOptions() {
    document.write('<input onchange="save()" type="text" name="ezproxy" size=50 maxlength=255 value="')
    document.write(readCookie('ez-server'));
    document.write('">');
    document.write('<input onchange="save()" type="checkbox" name="googleproxy" size=50 value="checked" maxlength=255 ')
    document.write(readCookie('googleproxy'));
    document.write('> use for Google Scholar (try this only if you cannot configure your library settings on Google\'s preference page).');
}

function show(id) {
    if (document.getElementById(id)) { document.getElementById(id).style.display = "block"};
}
function hide(id) {
    if (document.getElementById(id)) { document.getElementById(id).style.display = "none"};	
}

function save() {
    createCookie('ez-server',document.NavForm.ezproxy.value,20000);
    createCookie('googleproxy',document.NavForm.googleproxy.value,20000);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
