function buildQuery() {
	var select_component = "select chrlocus, ";
	var condition_component = "";
	var order_component = "";
	var element = null;
	
	for (id in formValues) {
		element = document.getElementById(id);

		if (element.value) {		
			if (id=="est" || id=="lcmsexpt" || id=="lcmspep" || id=="targmtp" || id=="mpdfm" || id=="pred1mit" || id== "proidconf" || id=="mpredconf" || id == "highconf") {
				condition_component += id + " >= " + element.value + " and ";
			}
			else if (id=="mwtfirst" || id=="residuefirst" || id=="pifirst" || id=="gravyfirst") {
				var baseword = id.replace("first","");
				var otherElement = document.getElementById(baseword + "last");

				if (otherElement.value) {
					condition_component += baseword + " between " + element.value + " and " + otherElement.value + " and ";
				}
			}
			else if (id=="mwtlast" || id=="residuelast" || id=="pilast" || id=="gravylast") {
			}
			else if (id=="chrlocus" || id=="name") {
				condition_component += id + " like %" + element.value + "% and ";
			}
			else if (id=="chromosome") {
				condition_component += id + " = "  + element.value + " and ";
			}
			else if (id=="description") {
				condition_component += "(" + id + " like %" + element.value + "% or comment like %" + element.value + "% or tigr like %" + element.value + "%) and ";
			}
			else if (id=="theold" || id=="theset" || id=="mito" || id=="group1" || id=="group2" || id=="group3" || id== "predotar1") {
				condition_component += id + " like " + element.value + " and ";
			}
			else if (id=="order") {
				order_component = " order by " + element.value + " desc";
			}
			else if (id=="seqlist") {
				//convert the list into a properly formed one
				var loci_string = element.value;
				loci_string = strReplace(";" ,",",loci_string);
				loci_string = strReplace(" " ,",",loci_string);
				loci_string = strReplace("\t",",",loci_string);
				loci_string = strReplace("\n",",",loci_string);
				loci_string = strReplace("\r",",",loci_string);		
				loci_string = strReplace(",,",",",loci_string);
				
				condition_component += "chrlocus in " + loci_string + " and ";
			}
			else if (id=="submit" || id=="columns") {
			}
			else {
				condition_component += id + " like %" + element.value + "% and ";
			}
		}
	}
	if (select_component == "") return;
	
	var columns = document.getElementById("columns");
	
	for (var i=0; i<columns.options.length; i++) {
		if (columns.options[i].selected) {
			select_component += columns.options[i].value + ", ";
		}
	}
	if (select_component.search("name") == -1) select_component += "name, ";
	if (select_component.search("description") == -1) select_component += "description, ";
	
	select_component = select_component.slice(0,select_component.length-2) + " from ampdb ";
	
	if (condition_component == "") {
		alert("You haven't specified any search criteria yet");
		return;
	}
	
	condition_component = "where " + condition_component.slice(0,condition_component.length-4);
	
	var query = select_component + condition_component + order_component;
	document.getElementById("formData").value = query;
	document.getElementById("mainForm").submit();
}
//Replace all occurrences of a substring in a string
function strReplace(s,r,str) {
	while (str.search(s) != -1) {
		str = str.replace(s,r);
	}
	return str;
}