<!--//BEGIN Script

// this script must be used for onclicks to make sure the onsubmit() is called
function callSubmit(form)	{
	var testForm = combineFormFields(form);
	if (testForm)	{
		form.submit();
	}
}

// used for the advanced search, onsubmit it disables any queries that have not been selected
function disableQueries(form)	{
	var filtered = false;

	// iterate the form
	for (var i=0; i < form.elements.length; i++) {
		var element = form.elements[i];
		
		// if at least one is checked we know that the idea is to filter
		if (element.type == 'checkbox' && element.checked)	{
			filtered = true;
		}
	}
	
	if (filtered)	{
		for (var i=0; i < form.elements.length; i++) {
			var element = form.elements[i];
			
			if (element.type == 'checkbox')	{
				element.onclick();
			}
		}
	}
}

// adds month/year together for NEMS queries
function controlForm(form)	{
	var myMonth=form.elements['month'].value;
	
	if (form.elements['SYear'].value == 'ALL')	
		{
			var myYear=1996;
		}
		else    
		{		
			var myYear=form.elements['SYear'].value;
		}	
	var datefrom=myMonth+ "/01/" + myYear;
	//alert (datefrom);
	if (document.getElementById("datefrom"))
		{
			document.getElementById("datefrom").value=datefrom;
		}
	else
		{
			var txt = document.createElement("input");
			txt.setAttribute("type", "hidden");
			txt.setAttribute("name", "datefrom");
			txt.setAttribute("id", "datefrom");
			txt.setAttribute("value", datefrom);
			form.appendChild(txt);
		}
		var type;
	if (document.getElementById("events").checked){type=1;}
	if (document.getElementById("news").checked){type=2;}
	
	if (document.getElementById("typesearch"))
		{
			document.getElementById("typesearch").value=type;
		}
	else
		{	
			var txt2 = document.createElement("input");
			txt2.setAttribute("type", "hidden");
			txt2.setAttribute("name", "maintype");
			txt2.setAttribute("id", "typesearch");	
			txt2.setAttribute("value", type);			
			form.appendChild(txt2);
		}		
	callSubmit(form);
}
function controlForm2(form)	{
	var myMonth=form.elements['month'].value;
	
	if (form.elements['SYear'].value == 'ALL')	
		{
			var myYear=1996;
		}
		else    
		{		
			var myYear=form.elements['SYear'].value;
		}	
	var datefrom=myMonth+ "/01/" + myYear;	
	if (document.getElementById("datefrom"))
		{
			document.getElementById("datefrom").value=datefrom;
		}
	else
		{
			var txt = document.createElement("input");
			txt.setAttribute("type", "hidden");
			txt.setAttribute("name", "datefrom");
			txt.setAttribute("id", "datefrom");
			txt.setAttribute("value", datefrom);
			form.appendChild(txt);
		}		
	callSubmit(form);
}

// the main function that puts the queries together
function combineFormFields(form)	{

	var element, currQuery, queryParams, queryNum, nvPair, server, isisQuote, isValue;
	
	//make sure there is at least one field
	isValue = false;

	//Holds each query as we build it
	currQuery = new Array();

	// the query we are on
	queryNum = -1;
	
	// the server system
	server = '';
	
	//whether to quote params (isis only)
	isisQuote = '';

	// initialize the first query
	currQuery[0] = null;

	// a human readable parameter set sent forward to the response
	form.elements.humanQuery.value = '';
	humanRead = form.elements.humanQuery;

	// an array that holds name/value pairs as each value is processed so we can reprocess them for later queries
	queryParams = new Array();

	// iterate the form
	for (var i=0; i < form.elements.length; i++) {

		element = form.elements[i];
		
			//alert(element.name);
		
		// first check if the element is required but no value was given
		if (element.title == 'required' && element.value.length == 0)	{
			alert('Required field ' + element.name + ' is missing.');
			return false;
		}

		// if name is query we are at start of a new query set
		if (element.name == 'query')	{

			// zero out the previous query if set to disabled
			if (currQuery[queryNum] != null)	{
				if (currQuery[queryNum].disabled)
					 currQuery[queryNum].value='';
			}

			// if ISIS we want to remove trailing *"
			if (server == 'isis')	{
				//currQuery[queryNum].value = currQuery[queryNum].value.substring(0,currQuery[queryNum].value.length - 2);
				currQuery[queryNum].value += "E-N\"*\"A-N\"";
				//reset isisQuote
				isisQuote = '';
			}

			// increment the query number
			queryNum++;

			// set query to query input element
			currQuery[queryNum] = element;
			
			// reset its value
			currQuery[queryNum].value = '';
			
			//add the host name if FIGIS
			if (form.elements[i + 1].value.indexOf('/figis/') == 0)
					currQuery[queryNum].value = 'http://' + location.host;

			// set the value to the hidden server input element
			currQuery[queryNum].value += form.elements[i + 1].value;
			
			//reset the serverType
			server = element.alt;
			
			//special set up for ISIS systems like FAOLEX
			if (server == 'isis')	{
			
				currQuery[queryNum].value += '&query=\"';
				// add a quote around parameters if isis
				isisQuote = '$\"*\"';
			}
			
		}
		
		// it's not a query element so add parameters on to the query 
		else	{

			// as long as it's not a submit button, and is not the server value, and is not the xsl value, and is not disabled, and has a value if it's the first query, and if a radio button it is checked, then add it to the string
			if (currQuery[queryNum]!=null && element.type != 'submit' && element.type != 'image' && element.type != 'file' && element.type != 'button' && element.type != 'password' && element.type != 'reset' && element.name != 'server' && element.name != 'humanQuery' && element.name != 'q' && element.name != 'scope' && element.name != 'loadMenu' && element.name != 'xsl' && (queryNum > 0 || element.value.length > 0) && !((element.type=='radio' || element.type=='checkbox') && ! element.checked) && !element.disabled)	{

				// set isValue to true to indicate there is at least one value
				isValue = true;
				
				// be sure to escape any spaces and pluses
				// the name/value pair
				nvPair = element.name + '=' + encodeURIComponent(element.value);

				// build an array of params we are adding to be used in other iterations of other queries
				if (element.value.length > 0)	{
					queryParams[queryParams.length] = nvPair;
				}
				
				// if a second or greater query and has no value, we need to iterate the previous params to find the name/value pair we want
				else	{
					// set the name value, which may be equal to the "alt" value of the current element, since we may have differently named parameters that use the same actual value, as "title" in EIMS and "inTitle" in NEMS
					if (element.alt.length > 0)
						nvPair = element.alt + "=";
					else
						nvPair = element.name + "=";
						// iterate the form looking for a match on "<name>="
					for (var j=0; j < queryParams.length; j++) {
						if (queryParams[j].indexOf(nvPair) != -1)	{
							nvPair = element.name + queryParams[j].substring(queryParams[j].indexOf('='));
						}
					}
				}
				

				// add an ampersand if necessary
				if (currQuery[queryNum].value.lastIndexOf('?') != currQuery[queryNum].value.length - 1 && server != 'isis')	{
					currQuery[queryNum].value += '&';
				}

				// add the name/value pair
				currQuery[queryNum].value += nvPair + isisQuote;

				//create human readable version if hasn't already been added
				if (humanRead.value.indexOf(nvPair) == -1)	{

					// write the param name
					humanRead.value += '&' + element.name + '='

					// write the param value -- for selections we have to get the text value
					if (element.options)	{
						humanRead.value += element.options[element.selectedIndex].text
					}
					else	{
						humanRead.value += element.value;
					}
				}
				//alert(currQuery[queryNum].value);
			}
		}
	}
	// now that we're all done we need to fix any remaining items that normally get cleaned up before iterating the next query in the query set

	// zero out the previous query if set to disabled
	if (currQuery[queryNum] != null)	{
		if (currQuery[queryNum].disabled)
			 currQuery[queryNum].value='';
	}
			
	// if ISIS we want to remove trailing *"
	if (server == 'isis')	{
		//currQuery[queryNum].value = currQuery[queryNum].value.substring(0,currQuery[queryNum].value.length - 2);
		currQuery[queryNum].value += "E-N\"*\"A-N\"";
	}
	//alert(currQuery[queryNum].value);
	
	//alert(currQuery[queryNum].value);

	
	if (! isValue)	{
		alert('There are no query values being sent!');
		return false;
	}
	else	{
		return true;
	}
}


// this function hides whatever is inside the element having the id given as a value for the layout parameter and disables the query element having the id given as a value for the query parameter
function hideQuery(layout, query)	{
	document.getElementById(layout).style.display='none';
	document.getElementById(query).disabled=true;
}

function showQuery(layout, query)	{
	document.getElementById(layout).style.display='';
	document.getElementById(query).disabled=false;
}

// this function switches the search to a Google search
function selectSearch(form,rad)	{
	if (document.getElementById(rad).checked) {
	// we make sure to fix the fields otherwise with back button can get wrong on subsequent search
		form.action = '/figis/website/MultiQueryAction.do';
		form.method='post';
		callSubmit(form);
	}
	// google search
	else {
		//new_window('','google',tl,lo,di,st,mn,sc,rs,wd,hi);
		form.action = '/figis/website/FIGoogle.do';
		form.q.value = form.title.value;
		form.method='get';
		//form.target='google';
		form.submit();
	}
}
//END Script-->
