/*
psumultisearch.js
Penn State Multiple Search

Author: 	Jason Tremblay <jjt152@psu.edu>
Copyright: 	2005 The Department of Integrative Arts
			The Pennsylvania State University
License:	This code may be freely copied and modified for any purpose. Credit is appreciated, but not required.

Description:
Converts a simple PSU search form to a Multi-search form.  The simple search form must have, at minimum, an ID so it can be targeted from this script, a single text box, and a single submit button.  Additional search options can easily be added to the global searchOptions array.  DOM capabilities of the browser are checked in the loadFunction, so the form is only changed in modern browsers.  

This code was heavily influenced by the ideas in "Unobtrusive Javascript" by Christian Heilmann (http://www.onlinetools.org/articles/unobtrusivejavascript)

addEVent function:
Author:		Scott Andrew LePera
Location:	http://www.scottandrew.com/weblog/articles/cbs-events


*/

var searchFormId = 'PSUMultiSearch';
var searchForm;
var searchTerms;
var searchType;
var searchSubmit;
var searchOptions = new Array();
	searchOptions['psu'] = new Array ();
		searchOptions['psu']['label'] = 'Penn State Web';
		searchOptions['psu']['method'] = 'GET';
		searchOptions['psu']['action'] = 'http://search-results.aset.psu.edu/search';
		searchOptions['psu']['queryname'] = 'q';
		searchOptions['psu']['hiddenfields'] = new Array();
			searchOptions['psu']['hiddenfields']['client'] = 'PennState';
			searchOptions['psu']['hiddenfields']['proxystylesheet'] = 'PennState';
			searchOptions['psu']['hiddenfields']['output'] = 'xml_no_dtd';
			searchOptions['psu']['hiddenfields']['site'] = 'PennState';
	searchOptions['ph'] = new Array ();
		searchOptions['ph']['label'] = 'Penn State People';
		searchOptions['ph']['method'] = 'GET';
		searchOptions['ph']['action'] = 'http://www.psu.edu/cgi-bin/ldap/ldap_query.cgi';
		searchOptions['ph']['queryname'] = 'cn';
		searchOptions['ph']['hiddenfields'] = new Array();
	searchOptions['dept'] = new Array ();
		searchOptions['dept']['label'] = 'Penn State Departments';
		searchOptions['dept']['method'] = 'POST';
		searchOptions['dept']['action'] = 'http://www.psu.edu/cgi-bin/ldap/dept_query.cgi';
		searchOptions['dept']['queryname'] = 'dept_name';
		searchOptions['dept']['hiddenfields'] = new Array();
	searchOptions['www'] = new Array ();
		searchOptions['www']['label'] = 'The World Wide Web';
		searchOptions['www']['method'] = 'GET';
		searchOptions['www']['action'] = 'http://www.google.com/search';
		searchOptions['www']['queryname'] = 'q';
		searchOptions['www']['hiddenfields'] = new Array();
			searchOptions['www']['hiddenfields']['ie'] = 'UTF-8';
			searchOptions['www']['hiddenfields']['oe'] = 'UTF-8';


// ADD THE PAGE LOAD EVENT
var loadFunction = function() {
	// Check for the necessary DOM functions
	if (document.getElementById && document.createTextNode) {
		multiSearchSetup(searchFormId);
	}
}
addEvent(window, 'load', loadFunction);


/*------------------------------*
 * RE-USABLE ADD EVENT FUNCTION *
 *------------------------------*/
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	} 
}


/*-----------------------------*
 * RE-WRITE TARGET SEARCH FORM *
 *-----------------------------*/
// Assume a search form that begins with a single text field and submit button
function multiSearchSetup(formId)
{
	// Set global search form variable
	searchForm = document.getElementById(formId);
	
	// Walk the child nodes of the form
	for (i=0;i<searchForm.childNodes.length;i++) {
		
		// Only look at element nodes
		if (searchForm.childNodes[i].nodeType == 1) {
			switch (searchForm.childNodes[i].getAttribute('type').toLowerCase()) {
				
				// store a reference to the first text input element
				case 'text':
					if (!searchTerms) {
						searchTerms = searchForm.childNodes[i];
						break;
					}
	
				// store a reference to the first submit element
				case 'submit':
					if (!searchSubmit) {
						searchSubmit = searchForm.childNodes[i];
						break;
					}
				
				// delete everything else
				default:
					searchForm.removeChild(searchForm.childNodes[i]);
					break;
			}
		}
	}

	// Create the drop-down menu
	searchType = document.createElement('select');
	searchType.setAttribute('id','searchType');
	searchType.setAttribute('name','searchType');

	// Create the options and add to menu node
	var optionNodes = new Array();
	for (var key in searchOptions) {
		var newNode = document.createElement('option');
		var newTextNode = document.createTextNode(searchOptions[key]['label']);
		newNode.setAttribute('value',key);
		newNode.appendChild(newTextNode);
		searchType.appendChild(newNode);
	}
	
	// Add the search select element
	searchForm.insertBefore(searchType,searchSubmit);
	
	// Change the text of the submit button
	searchSubmit.setAttribute('value','Search');
	
	// Change the action of the form
	searchForm.setAttribute('action','javascript:void(doMultiSearch());');
}


/*---------------------------*
 * EXECUTE THE CHOSEN SEARCH *
 *---------------------------*/
function doMultiSearch()
{
	// get the value of the selected search type
	var search = searchOptions[searchType[searchType.selectedIndex].value];
	
	// special case for ph search
	// if a userid is entered, change the queryname
	if (searchType[searchType.selectedIndex].value == 'ph') {
		var re = /^[a-z]{3}[0-9]{1,4}$/i;
		if (re.test(searchTerms.value)) {
			search['queryname'] = 'uid';
		}
	}
	
	// create a new form
	var newSearchForm = document.createElement('form');
	newSearchForm.setAttribute('action',search['action']);
	newSearchForm.setAttribute('method',search['method']);
	newSearchForm.setAttribute('style','display:none;');
	
	// add search terms
	var termsNode = document.createElement('input');
	termsNode.setAttribute('type','hidden');
	termsNode.setAttribute('name',search['queryname']);
	termsNode.setAttribute('value',searchTerms.value);
	newSearchForm.appendChild(termsNode);
	
	// add hidden fields
	var hidden = search['hiddenfields'];
	for (var key in hidden) {
		//alert (key+" : "+hidden[key]);
		var node = document.createElement('input');
		node.setAttribute('type','hidden');
		node.setAttribute('name',key);
		node.setAttribute('value',hidden[key]);
		newSearchForm.appendChild(node);
	}
	
	// add the new form to the document
	document.appendChild(newSearchForm);
	
	// submit the form
	newSearchForm.submit();

}