	var xmlHttp
	var globaltype
	var basictab
	var advancedtab
	var helptab
	var fieldstab
	var advsrchfldcount
	window.onload=cacheMainTabs;
	function cacheMainTabs() {
		basictab=document.getElementById("maindiv").innerHTML;//cache the basic tab on page load
		var url="search_tabAdvanced.php";
		xmlHttp=initXmlHttpObject()//preload the advanced tab
		if (xmlHttp==null){
		  alert ("Your browser does not support AJAX! Please consider updating.");
		  return;
		} 
		xmlHttp.onreadystatechange=preloadAdvancedTab;
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
	function preloadAdvancedTab() {
		if (xmlHttp.readyState == 4){ //response is complete
			 if (xmlHttp.status == 200){ //response was successful
				 advancedtab = xmlHttp.responseText;
			 }
			 else{
				//document.getElementById("useralert").innerHTML = 'Preload failed: '+xmlHttp.status +' '+xmlHttp.statusText;
			 }
		}
		return true;	
	}
	function showTab(type){
		document.getElementById("useralert").innerHTML = '<strong>Loading...</strong><br>';
		var cachedtab=null;
		globaltype = type;

		if (type=='basic'){
			var url="search.php?frm=tab";
			cachedtab=basictab;
		 }
		 else if (type=='advanced'){
			var url="search_tabAdvanced.php";
			cachedtab=advancedtab;
		 }
		 else if (type=='help'){
			 var url="search_tabHelp.php";
			 cachedtab=helptab;
		 }
		 else if (type=='fields'){
			 var url="shared_tabFields.php?ref=srch";
			 cachedtab=fieldstab;
		 }
		else{
		//error handling
		} 
		if(cachedtab==null){ //nothing cached request the tab using ajax
			xmlHttp=initXmlHttpObject()
			if (xmlHttp==null){
			  alert ("Your browser does not support AJAX! Please consider updating.");
			  return;
			} 
			xmlHttp.onreadystatechange=handleTabResponse;
			xmlHttp.open("GET",url,true)
			xmlHttp.send(null)
		}
		else{ //set maindiv's innerHTML to the cached tab 
			document.getElementById("maindiv").innerHTML = cachedtab;
			if (globaltype=='advanced'){// initiate the ability to add fields if we are on the advanced tab
				advsrchfldcount = 0;
				document.getElementById('addCondition').onclick = addCondition;
				addCondition(true);
			}
		}
	}
	function handleTabResponse(){ 	
		 if (xmlHttp.readyState == 4){ //response is complete
			 if (xmlHttp.status == 200){ //response was successful
			    var response
				 response = xmlHttp.responseText;
			   document.getElementById("maindiv").innerHTML = response;
			   if (globaltype=='basic'){// cache the returned tab for later use to reduce the number of server calls
					basictab=response;
				}
				else if (globaltype=='advanced'){
					advancedtab=response;
					// initiate the ability to add fields
					advsrchfldcount = 0;
					document.getElementById('addCondition').onclick = addCondition;
					addCondition(true);
				}
				 else if (globaltype=='help'){
					 	helptab=response;
				 }
				else if (globaltype=='fields'){
					   fieldstab=response;
				}
			 }
			 else{
				document.getElementById("maindiv").innerHTML = xmlHttp.status +' '+xmlHttp.statusText;
			 }
		}
		return true;
	}
	function removeCondition(spanid) {
		var spanParent = document.getElementById('conditions');
		var span = document.getElementById(spanid);
		if(span){
			spanParent.removeChild(span);			
		}
	}
	function addCondition(first) {
			var insertHere = document.getElementById('conditions');
			var newFields = document.createElement('span');
			newFields.id=advsrchfldcount+'_newFields';
			newFields.style.display="block";
			newFields.style.textAlign="right";
			var formelementidInput = createNamedElement('input', advsrchfldcount);
			formelementidInput.type="hidden";
			formelementidInput.value="id";
			newFields.appendChild(formelementidInput);
			var logicSelect = createNamedElement('select', advsrchfldcount+'_logic');
			var logicOption1 = logicSelect.appendChild(document.createElement('option'));
			var logicOption2 = logicSelect.appendChild(document.createElement('option'));
			logicOption1.value="AND";
			logicOption2.value="OR";
			logicOption1.innerHTML="AND";
			logicOption2.innerHTML="OR";
			if(first==true){
				logicSelect.style.display="none";
			}
			newFields.appendChild(logicSelect);
			var nameSelect = createNamedElement('select', advsrchfldcount+'_name');
			if(json_searchlist){
				i=1;
				for (sqlalias in json_searchlist) {	
					var nameOption = nameSelect.appendChild(document.createElement('option'));
					if (i == 1){
						nameOption.selected="selected";
					}
					nameOption.value=sqlalias;
					nameOption.innerHTML=json_searchlist[sqlalias].outputname;
					i++
				}
			}
			newFields.appendChild(nameSelect);
			var operatorSelect = createNamedElement('select', advsrchfldcount+'_operator');		
			if(json_searchoperators){
				i=1;
				for (operator in json_searchoperators) {	
					var operatorOption = operatorSelect.appendChild(document.createElement('option'));
					if (i == 1){
						operatorOption.selected="selected";
					}
					operatorOption.value=operator;
					operatorOption.innerHTML=json_searchoperators[operator];
					i++
				}
			}
			newFields.appendChild(operatorSelect);		
			var valueInput = createNamedElement('input', advsrchfldcount+'_value');
			valueInput.type="text";
			valueInput.size="20"
			newFields.appendChild(valueInput);
			newFields.innerHTML += '<input value="Remove condition" onclick="removeCondition(\''+advsrchfldcount+'_newFields\')" type="button">';
			
			insertHere.appendChild(newFields);
			advsrchfldcount++;			
	}
	function createNamedElement(type, name) {// ie fails, another hack
		  var element = null;
		  // Try the ie way; this fails on standards-compliant browsers
		  try {
			element = document.createElement('<'+type+' name="'+name+'">');
		  } catch (e) {
		  }
		  if (!element || element.nodeName != type.toUpperCase()) {
			// Non-IE browser; use canonical method to create named element
			element = document.createElement(type);
			element.name = name;
		  }
		  return element;
	}
	function submitForm(parameters) {
	 	 xmlHttp=initXmlHttpObject()
		if (xmlHttp==null){
		  alert ("Your browser does not support AJAX! Please consider updating.");
		  return;
		}
		document.getElementById('useralert').innerHTML = '<strong>Processing your request...</strong>';
		xmlHttp.onreadystatechange=alertResult;
		xmlHttp.open("POST",'processSearch.php?'+parameters, true)
		//xmlHttp.send()
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttp.send(parameters)
	}	
	function alertResult() {
	  if (xmlHttp.readyState == 4) {
		 if (xmlHttp.status == 200) {
			 if(xmlHttp.responseText){
				var result = xmlHttp.responseText;
				document.getElementById('useralert').innerHTML = result; 
				 if(document.getElementById('hiddenSubmit')){
					document.getElementById('hiddenSubmit').submit();
				 }
			 }else{
				 document.getElementById('useralert').innerHTML = '<strong>No response returned </strong> '+xmlHttp.status + ' '+xmlHttp.statusText;
			 }
		 } else {
			document.getElementById('useralert').innerHTML = '<strong>There was a problem with the request:</strong> '+xmlHttp.status +' '+xmlHttp.statusText;
		 }
	  }
	}
	function get(theform){
		var reqStr = "";	
		for(i=0; i < theform.elements.length; i++){
			isformObject = false;		
			switch (theform.elements[i].tagName){
				case "INPUT":
					switch (theform.elements[i].type){
					case "text":
					case "hidden":
						reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value);
						isformObject = true;
						break;
					
					case "checkbox":
						if (theform.elements[i].checked){
							reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
						}else{
							reqStr += theform.elements[i].name + "=";
						}
						isformObject = true;
						break;
					
					case "radio":
						if (theform.elements[i].checked){
							reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
							isformObject = true;
						}
				}
				break;
			
				case "TEXTAREA":		
					reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value);
					isformObject = true;

					break;		
					case "SELECT":
					var sel = theform.elements[i];
					reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
					isformObject = true;
					break;
			}
			if ((isformObject) && ((i+1)!= theform.elements.length)){
				reqStr += "&";
			}
		}
		//alert(reqStr);
	  	submitForm(reqStr);
	} 
	function initXmlHttpObject(){
		xmlHttp=null;
		try{// Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
		  }
		catch (e){// Internet Explorer
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e){
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}