var searchfor; // 0 = search for books
               // 1 = search for articles
               // 2 = search for databases
               // 3 = (no longer used, used to be search Library web site)

function loadUp() {
	var search = getCookie('home_search');
	var by = getCookie('home_by');
	if (search != null) {
		if (search == 3) { // 3 was for the web site search that is no longer used here
			searchfor = 0;
		}
		else {
			searchfor = search;
		}
	}
	else {
		searchfor = 0;
	}

	changeMenu(searchfor);

	if (by != null) {
		document.getElementById('searchtype').options[by].selected = true;
	}
	else {
		document.getElementById('searchtype').options[0].selected = true;
	}

	changeSubjectMenu();
}

function inputboxFocus() {
	document.getElementById('inputbox').focus();
}

function subjectMenuFocus() {
	document.getElementById('subjectdrop').focus();
}

function changeAction() {
	saveSearchCookie('home_search',searchfor);
	saveSearchCookie('home_by',document.getElementById('searchtype').selectedIndex);
	var windowname="nulsearch";
	var newwindow = '';
	if (searchfor == 0) {
  	var url = 'http://nucat.library.northwestern.edu/cgi-bin/Pwebrecon.cgi';
  	var inputbox = document.getElementById('inputbox').value;
  	var nst = document.getElementById('searchtype').selectedIndex;
  	var stype = document.getElementById('searchtype')[nst].value;
  	if (stype == 'CMD') { // do guided keyword
  		newwindow = window.open(url + '?SAB1=' + escape(inputbox) + '&BOOL1=all+of+these&FLD1=Keyword+Anywhere+%28GKEY%29&GRP1=AND+with+next+set&SAB2=&BOOL2=all+of+these&FLD2=Keyword+Anywhere+%28GKEY%29&GRP2=AND+with+next+set&SAB3=&BOOL3=all+of+these&FLD3=Keyword+Anywhere+%28GKEY%29&DB=local&CNT=50', 'nulsearch','width=675,height=550,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
		}
		else {
  		newwindow = window.open(url + '?Search_Arg=' + escape(inputbox) + '&Search_Code=' + stype + '&DB=local&CNT=50', 'nulsearch','width=675,height=550,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
		}
	}
	else if (searchfor == 1) {
  	var url = 'http://er.library.northwestern.edu/V/';
  	var inputbox = document.getElementById('inputbox').value;
 		var nst = document.getElementById('searchtype').selectedIndex;
  	var stype = document.getElementById('searchtype')[nst].value;
  	newwindow = window.open(url + '?func=quick-1-check1&mode=advanced&find_code_2=' + stype + '&find_request_2=' + escape(inputbox) + '&group_number=000002452', 'nulsearch','width=675,height=550,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
	}
	else if (searchfor == 2) {
  	var url = 'http://er.library.northwestern.edu/V/';
  	var inputbox = document.getElementById('inputbox').value;
  	var subjectmenu = document.getElementById('subjectdrop').value;
  	var nst = document.getElementById('searchtype').selectedIndex;
  	var stype = document.getElementById('searchtype')[nst].value;
  	if (stype == 'subject') { // do subject search
  		if (subjectmenu == '') {
  			alert('Please select a subject');
  		}
  		else {
				newwindow = window.open(url + '?func=find-db-1-locate&mode=locate&F-WCL=&F-WPB=&F-WFL=' + subjectmenu + '&F-WRD=&F-WTY=&restricted=all', 'nulsearch', 'width=675,height=550,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
			}
		}
		else {
			if (inputbox == '') {
  			alert('Please enter a title');
			}
			else {
		  	newwindow = window.open(url + '?func=find-db-1-locate&mode=locate&F-WCL=' + escape(inputbox) + '&F-WPB=&F-WFL=&F-WRD=&F-WTY=&restricted=all', 'nulsearch', 'width=675,height=550,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
		  }
	  }
	}
/*else if (searchfor == 3) {
  	var url = 'http://search.northwestern.edu/query.html';
  	var inputbox = document.getElementById('inputbox').value;
  	newwindow = window.open(url + '?qt=' + escape(inputbox) + '&col=nwulib%20nwuliber%20nwulibin&nwuhd=http://www.library.northwestern.edu/searchsite/nul_hd.html&nwuft=http://www.library.northwestern.edu/searchsite/nul_ft.html', 'nulsearch', 'width=675,height=550,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes');
	} */

	if (window.focus && newwindow != '') {
		newwindow.focus();
	}

	return false;
}

function changeMenu(sf) {
	if (sf != null) {
		searchfor = sf; // allows changing of the searchfor variable for others
	}

	if (searchfor == 0) {
		changeTabs('books');
		document.getElementById('searchtype').length = 3;
		document.getElementById('searchtype').options[0] = new Option("Keyword", "CMD", false, false);
		document.getElementById('searchtype').options[1] = new Option("Author", "NAME", false, false);
		document.getElementById('searchtype').options[2] = new Option("Title", "TALL", false, false);
		document.getElementById('searchtype').disabled = false;
	}
	else if (searchfor == 1) {
		changeTabs('articles');
		document.getElementById('searchtype').length = 4;
		document.getElementById('searchtype').options[0] = new Option("Keyword", "WRD", false, false);
		document.getElementById('searchtype').options[1] = new Option("Author", "WAU", false, false);
		document.getElementById('searchtype').options[2] = new Option("Subject", "WSU", false, false);
		document.getElementById('searchtype').options[3] = new Option("Title", "WTI", false, false);
		document.getElementById('searchtype').disabled = false;
	}
	else if (searchfor == 2) {
		changeTabs('databases');
		document.getElementById('searchtype').length = 2;
		document.getElementById('searchtype').options[0] = new Option("Subject", "subject", false, false);
		document.getElementById('searchtype').options[1] = new Option("Title", "title", false, false);
		document.getElementById('searchtype').disabled = false;
	}
/*else if (searchfor == 3) {
		document.getElementById('searchtype').length = 1;
		document.getElementById('searchtype').options[0] = new Option("Keyword", "keyword", false, false);
		document.getElementById('searchtype').disabled = true;
	}	*/

	changeSubjectMenu();

	return true;
}

function changeTabs(tabname) {
	if (tabname == 'books') {
		document.getElementById('tab_books_journals').src = 'images/home/einstein/tab_books_journals_on.gif';
		document.getElementById('tab_articles').src = 'images/home/einstein/tab_articles_off.gif';
		document.getElementById('tab_databases').src = 'images/home/einstein/tab_databases_off.gif';
	}
	else if (tabname == 'articles') {
		document.getElementById('tab_books_journals').src = 'images/home/einstein/tab_books_journals_off.gif';
		document.getElementById('tab_articles').src = 'images/home/einstein/tab_articles_on.gif';
		document.getElementById('tab_databases').src = 'images/home/einstein/tab_databases_off.gif';
	}
	else if (tabname == 'databases') {
		document.getElementById('tab_books_journals').src = 'images/home/einstein/tab_books_journals_off.gif';
		document.getElementById('tab_articles').src = 'images/home/einstein/tab_articles_off.gif';
		document.getElementById('tab_databases').src = 'images/home/einstein/tab_databases_on.gif';
	}

	return false;
}

function changeSubjectMenu() {
		var r = document.getElementById('searchtype').selectedIndex;

		if (searchfor == 2 && r == 0) { //database subject search
			showSubjectMenu();
			subjectMenuFocus();
		}
		else {
			showInputBox();
			inputboxFocus();
		}
}

function showSubjectMenu() {
	document.getElementById('inputbox').style.display = 'none';
	document.getElementById('subjectdrop').style.display = 'inline';
}

function showInputBox() {
	document.getElementById('inputbox').style.display = 'inline';
	document.getElementById('subjectdrop').style.display = 'none';
}

function saveSearchCookie(name,value) {
	document.cookie = name + '=' + value;
}

function getCookie(name) {
	var dcookie = document.cookie.split("; ");
	for (var i=0; i < dcookie.length; i++){
		var crumb = dcookie[i].split("=");
		if (name == crumb[0]) {
			return unescape(crumb[1]);
		}
	}
	return null;
}

