var termsWindow;
var formAction = "/cgi-bin/asp/philo/cpt/search3t?";

function setSingleFormElement(toBeSet) {

	//alert(toBeSet);

	var nameAndValue = toBeSet.split("::");

	// We used to replace some stuff with periods, so that it would search as a wildcard
	// because it wouldn't search correctly literally.
	// However, this didn't seem to work right either, so you may need to examine the
	// possibilities on a database-by-database basis and devise a strategy.

	//nameAndValue[1] = nameAndValue[1].replace(/\?|\<|\>|\[|\]|\$|\(|\)/g, '.');

	var fieldName = eval("document.forms[0]." + nameAndValue[0]);

	if (fieldName.type == "text" ) {
		
		if (fieldName.value.length > 0) {
			fieldName.value = fieldName.value + " OR " + nameAndValue[1];
		} else {
			fieldName.value = nameAndValue[1];
		}
			
	}	else if (fieldName.type.indexOf("select") > -1) {		 
			fieldName.selectedIndex=nameAndValue[1];
			
	}	else if (fieldName.type.indexOf("radio") > -1) {
			fieldName.checked = true;
	}

	// Are you using WhizBang? If so, you want to re-construct the human-readable query
	// Otherwise, comment out this next line.

//	formulateQuery();
}


function writeQueryUrl() {
	
	var queryString="";
	
	for (var i = 0; i < document.forms[0].elements.length; i++) {
	
		var element = document.forms[0].elements[i];

		if (element.type) {

			if (element.type.indexOf('text') > -1 || element.type.indexOf('hidden') > -1) {

				queryString+= encodeURI(element.name) + "="+ encodeURI(element.value) + "&";
			}

			if (element.type.indexOf("radio") > -1) {
				if (element.checked) {
					queryString+= encodeURI(element.name) + "=" + encodeURI(element.value) + "&";
				}
			}
		
			if (element.type.indexOf("select") > -1) {

				// ASP uses listboxes for some kinds of terms accumulation... there is no selected index in
				// these, we need to get the value of the hidden field that actually holds the values.

				if (element.name.indexOf("listbox") > -1) {
					hiddenFieldName = element.name.replace(/_listbox/, "");
					queryString += encodeURI(hiddenFieldName) + "=" + encodeURI(hiddenFieldName.value) + "&";
				} else {
					if (element.selectedIndex > -1) {
						queryString += encodeURI(element.name) + "=" + encodeURI(element.options[element.selectedIndex].value) + "&";
					}
				}
			}
		}
	}

	queryString = formAction + queryString;
	
	return queryString;
}

function termsOpen(termName) {
	var UrlString = writeQueryUrl() + termName;
	termsWindow = window.open(UrlString, "termsWindow", "scrollbars=yes,resizable=yes,width=400,height=800,location=yes,left=440");
}
