	var formChanged = true;	
	var debugCount = 0;
	var activeRow = 0;
	var checkFirstResult = false;


	function ajaxSetup() {
	    Ajax.Responders.register({
	      onCreate: function() {
	        $('indicator').show();
	      },
	      onComplete: function() {
	        $('indicator').hide();
	        
	        // if one test is returned, show the detail automatically
	        var countText = document.getElementById("searchFormResultCount").innerHTML;
	        var firstId = document.getElementById("firstResultId").innerHTML;
	        if (countText=='1 test' && checkFirstResult) {
        	  activateTestRow(firstId);
        	  checkFirstResult = false;
	        }
	        
	      }
    });
		
    // rico setup
		ajaxEngine.registerRequest("search", "/catalog/products.jsp");
		ajaxEngine.registerRequest("detail", "/catalog/detail.jsp");
		ajaxEngine.registerAjaxElement('searchFormCategories');
		ajaxEngine.registerAjaxElement('searchFormDiseases');
		ajaxEngine.registerAjaxElement('detailInner');
		ajaxEngine.registerAjaxElement('detailLink');
		ajaxEngine.registerAjaxElement('printLink');
		ajaxEngine.registerAjaxElement('searchFormResults');
		ajaxEngine.registerAjaxElement('searchFormResultCount');
		ajaxEngine.registerAjaxElement('exception');
		ajaxEngine.registerAjaxElement('firstResultId');
	}
	
	// loads or reloads the product list
	function getDetail(id) {
		ajaxEngine.sendRequest('detail',
								   "id=" + id);
		debug("id=" + id);
	}

	function respondToInput(fromLeftNav,kValFromNav,cValFromNav) {
		if (fromLeftNav == 'true'){
			document.getElementById("keyword").value = kValFromNav;
			submitSearchFromNav(cValFromNav);
		}
		if (formChanged) {
			if (fromLeftNav == 'true'){
				submitSearchFromNav(cValFromNav);
			}else{
				submitSearch();
			}
			formChanged = false;			
		}
		setTimeout('respondToInput()',1000); // reruns every second
	}

	// comment out last line to enable request debugging
	function debug(msg) {
		debugCount++; 
		msg += "<br/>" + "Loads: " + debugCount;
		//document.getElementById('exception').innerHTML = msg;
	}

	function submitSearch() {
		var kw = document.searchForm.keyword.value;
		var c = checksToList('category');
		var d = checksToList('disease');
		debug("keyword=" + kw + "<br/>" +
		  "category=" + c + "<br/>" + 
		  "disease=" + d);
		ajaxEngine.sendRequest('search',
		   "keyword=" + kw,
		   "category=" + c,
		   "disease=" + d);
		checkFirstResult = true;
	}	
	
	function submitSearchFromNav(cValFromNav) {
		var kw = document.searchForm.keyword.value;
		var c = cValFromNav;
		var d = checksToList('disease');
		debug("keyword=" + kw + "<br/>" +
		  "category=" + c + "<br/>" + 
		  "disease=" + d);
		ajaxEngine.sendRequest('search',
		   "keyword=" + kw,
		   "category=" + c,
		   "disease=" + d);
		checkFirstResult = true;
	}
	
	// clears the search criteria and reloads
	function clearSearch() {
		// hide the details page if showing
		hideDetails();
		// clear lastname and keyword fields
		document.searchForm.reset();
		// clear ajax checkboxes
		uncheckAll('category');
		uncheckAll('disease');	
		// reload data
		submitSearch();			
	}
	
	function checksValInCheckBox(name,idString) {
		var reachedEnd = false;
		var count = 1;
		do {
			if (document.getElementById(name+'_'+count)) {
				el = document.getElementById(name+'_'+count);
				if (el.value == idString){
					el.checked = true;
				}
			} else {
				reachedEnd = true;
			}
			count++;
		} while (!reachedEnd);
	}
	
	// takes in a multi-checkbox form field and outputs a 
	// comma-separated string containing the selected values.
	// because the checkboxes are loaded dynamically with ajax,
	// we must use the DOM to access them.
	function checksToList(name) {
		var s = '';
		var reachedEnd = false;
		var count = 1;
		do {
			if (document.getElementById(name+'_'+count)) {
				el = document.getElementById(name+'_'+count);
				if (el.checked) {
					if (s=='') s = el.value;
					else s += ',' + el.value;
				}
			} else {
				reachedEnd = true;
			}
			count++;
		} while (!reachedEnd);
		return s;
	}

	// unchecks all the checkboxes in a group
	function uncheckAll(name) {
		var s = '';
		var reachedEnd = false;
		var count = 1;
		do {
			if (document.getElementById(name+'_'+count)) {
				el = document.getElementById(name+'_'+count);
				el.checked = false;
			} else {
				reachedEnd = true;
			}
			count++;
		} while (!reachedEnd);
	}

	// gets the selected value of a dropdown. 
	// because the selects are loaded dynamically with ajax,
	// we must use the DOM to access them
	function getSelectedValue(name) {
		if (!document.getElementById(name)) return '';
		var el = document.getElementById(name);
		for (i=0; i<el.childNodes.length; i++) {
			if (el.childNodes[i].selected) {
				return el.childNodes[i].value;
			}
		}
		return '';
	}

	// clears a dynamic select box 
	function clearSelect(name) {
		if (!document.getElementById(name)) return '';
		var el = document.getElementById(name);
		el.selectedIndex = 0;
	}
	
	// code for test catalog

	function activateTestRow (id) {
		document.getElementById("detailPaneDiv").value = '';
		getDetail(id);
		deactivateRow();
		document.getElementById("testRow" + id).className += ' on';
		document.getElementById("leftTestCatalogCol").className = 'detailPane';
		document.getElementById("searchPaneDiv").style.display='none';
		document.getElementById("detailPaneDiv").style.display='block';
		activeRow = id;
		resetPrintForm(id);
	}

	function deactivateRow() {
		if(document.getElementById('testRow' + activeRow)) {
			document.getElementById("testRow" + activeRow).className = document.getElementById("testRow" + activeRow).className.replace(' on','');
		}
	}

	function hideDetails() {
		document.getElementById("leftTestCatalogCol").className = 'searchPane';
		document.getElementById("detailPaneDiv").style.display='none';
		document.getElementById("searchPaneDiv").style.display='block';
		deactivateRow();
	}

	// submits print form on detail pane
	function submitPrint() {		
		//alert(document.frmPrint.id.value + "\n" + document.frmPrint.price.value + "\n" + document.frmPrint.pricehelp2.value);
		document.frmPrint.submit();
	}
	
	// initialize or reset print form
	function resetPrintForm(id) {
			document.frmPrint.price.value = '';
			document.frmPrint.pricehelp2.value = '';
			document.frmPrint.id.value = id;
	}
	
	// show pricing notes when a price is selected in the details pane
	function showPriceHelp(type) {
		var help = document.getElementById("priceHelp");
		if (type=='client') {
			help.innerHTML = "Please note that the price listed is for established clients " + 
							 "only and may change without notice.";
		} else if (type=='copay') {
			help.innerHTML = "Please note that the price listed is the patient's responsibility " + 
							"if he or she has chosen to participate in Athena's Patient Advocate " + 
							"Program, as participation in that Program means that Athena expects " + 
							"to receive the majority of the payment from the patient's insurer. " + 
							"For more information about this program, please visit " + 
							"<a href='/content/ordering'>" + 
							"Ordering & Billing</a> " + 
							"or call Athena's Patient Advocate Department at " + 
							"<nobr>1-800-394-4493.</nobr>"; 
		} else if (type=='list') {
			help.innerHTML = "Please note that the price listed may change without notice.";
		} else if (type=='medicare') {
			help.innerHTML = "Please note that the price listed is the patient's " + 
							 "responsibility if Medicare denies reimbursement based on medical " + 
							 "necessity grounds and if Athena has an Advance Beneficiary Notice " + 
							 "signed by the patient on file. Please call Athena's Reimbursement " + 
							 "Services at <nobr>1-800-394-4493</nobr> if you have any questions.";
		} else {
			help.innerHTML = '';
		}
		// set selected pricing data in print form
		var inputId = document.getElementById("priceFormProductId");
		var option = document.getElementById("price_" + type);
		if (option) {
			document.frmPrint.price.value = option.innerHTML;
			document.frmPrint.pricehelp2.value = help.innerHTML;
			document.frmPrint.id.value = inputId.value;
		} else {
			document.frmPrint.price.value = '';
			document.frmPrint.pricehelp2.value = '';
			document.frmPrint.id.value = inputId.value;
		}
	}