function searchTermSimple() {
	var url = window.location.href;
	var find = document.getElementsByName("find")[0].value;

	// clear num returned cookie
	eraseCookie('ELRCNUMRETURNED');

	// Create the new script element and assign the a src attribute
	var newScriptsimpleSearch = new Element('script', { 'src': 'http://apps.exploratorium.edu/nsdl/research/saveSearchTerm.php?find='+find+'&url='+url});
			
	// Put the new script into the page
	document.getElementsByTagName('head').item(0).appendChild(newScriptsimpleSearch);
	
	return (false);
}

function searchCallback(response){
	var term_ID = response.term_ID;

	if(term_ID){
		document.cookie ="ELRCSEARCHID="+term_ID+";path=/;domain=.exploratorium.edu";
		//saveSessionID();
		
		var session_ID = readCookie('ELRCSESSIONID');
		var term_ID = readCookie('ELRCSEARCHID');
		var JSESSIONID = readCookie('JSESSIONID');
		var ELRCPERSONID = readCookie('ELRCPERSONID');
		
		if(session_ID == null){
			// Create the new script element and assign the a src attribute
			var newScriptSessionID = new Element('script', { 'src': 'http://apps.exploratorium.edu/nsdl/research/saveSessionID.php?term_ID='+term_ID+'&JSESSIONID='+JSESSIONID+'&ELRCPERSONID='+ELRCPERSONID});
				
			// Put the new script into the page
			document.getElementsByTagName('head').item(0).appendChild(newScriptSessionID);
		}
		
		if(session_ID != null){
			// Create the new script element and assign the a src attribute
			var newScriptFullSessionID = new Element('script', { 'src': 'http://apps.exploratorium.edu/nsdl/research/saveSessionID.php?term_ID='+term_ID+'&session_ID='+session_ID+'&JSESSIONID='+JSESSIONID+'&ELRCPERSONID='+ELRCPERSONID});
				
			// Put the new script into the page
			document.getElementsByTagName('head').item(0).appendChild(newScriptFullSessionID);
		}
	}
}

function searchTermAdv() {
	var category = [];
	var grade = [];
	var resType = [];
	var url = window.location.href;
	var find = document.getElementsByName("find")[0].value;
	var numselect = document.getElementsByName("itemsPerPage")[0];
	var itemsPerPage = numselect.options[numselect.options.selectedIndex].value;
	var cat = document.getElementsByName("category");
	var grd = document.getElementsByName("gradeLevels");
	var rt = document.getElementsByName("educationalResourceType");
	
	for(i=0; i<cat.length;i++){
		if(cat[i].checked == true){
			category.push(cat[i].value);
		}
	}
	for(i=0; i<grd.length;i++){
		if(grd[i].checked == true){
			grade.push(grd[i].value);
		}
	}
	for(i=0; i<rt.length;i++){
		if(rt[i].checked == true){
			resType.push(rt[i].value);
		}
	}
	
	// clear num returned cookie
	eraseCookie('ELRCNUMRETURNED');

	// Create the new script element and assign the a src attribute
	var newScriptadvSearch = new Element('script', { 'src': 'http://apps.exploratorium.edu/nsdl/research/saveSearchTerm.php?find='+find+'&url='+url+'&itemsPerPage='+itemsPerPage+'&category='+[category]+'&grade='+[grade]+'&resType='+[resType]});
			
	// Put the new script into the page
	document.getElementsByTagName('head').item(0).appendChild(newScriptadvSearch);
	
	return (false);
}

function sessionCallback(response){
	var session_ID = response.session_ID;

	if(session_ID){
		document.cookie ="ELRCSESSIONID="+session_ID+";path=/;domain=.exploratorium.edu";
	}
	if($('searchForm')){
		$('searchForm').submit();
	} else {
		$('simpleSearchForm').submit();
	}
}

function saveSessionID(){
	var session_ID = readCookie('ELRCSESSIONID');

	var term_ID = readCookie('ELRCSEARCHID');
	if(session_ID == null){
	
		// Create the new script element and assign the a src attribute
		var newScriptSessionID = new Element('script', { 'src': 'http://apps.exploratorium.edu/nsdl/research/saveSessionID.php?term_ID='+term_ID});
			
		// Put the new script into the page
		document.getElementsByTagName('head').item(0).appendChild(newScriptSessionID);
	}
	
	if(session_ID != null){
		// Create the new script element and assign the a src attribute
		var newScriptFullSessionID = new Element('script', { 'src': 'http://apps.exploratorium.edu/nsdl/research/saveSessionID.php?term_ID='+term_ID+'&session_ID='+session_ID});
			
		// Put the new script into the page
		document.getElementsByTagName('head').item(0).appendChild(newScriptFullSessionID);
	}
}