	var xmlDocs = new Array(); //Contains original xml code
	var xmls = new Array(); //Contains modified xml code
	var hitNodes = new Array();  //Contains the xml code of the previously searched proteases
	//onerror=rettrue;
	
	function rettrue(){
		return true;
	}
	
  function loadXML(path){
	  if (window.ActiveXObject)
	  {// code for IE
	    xmlDocs[path]=new ActiveXObject("Microsoft.XMLDOM");
	  }
	  else if (document.implementation.createDocument)
	  {// code for Mozilla, Firefox, Opera, etc.
	    xmlDocs[path]=document.implementation.createDocument("","",null);
	  }
	  else
	  {
	    alert('Your browser cannot handle this script');
	  }
	  xmlDocs[path].async=false;
	  xmlDocs[path].load(path+".xml");
	}
	
	function goto_fam(el, base){
		var fam = document.getElementById(el)
		var opt = fam.options[fam.selectedIndex].text;
		if (opt != '___'){
			window.location = base+"#"+opt
		}
	}
	
	function check_condition(){
		var cond = document.getElementById('condition');
		var svar = document.getElementById('svar');
		var field = document.getElementById('field');
		var fieldOpt = document.getElementById('ff');
		var c = cond.options[cond.selectedIndex].text;
		var svar2 = document.getElementById('svar2');
		if (c == 'containing'){
			svar.style.visibility = '';
			svar.innerHTML = '<input id="input"/> in the field';
			field.style.visibility = '';
			field.innerHTML = "<select id='ff' onchange='check_sp()'><option>(Any field)</option>\n        <option>Code</option>\n        <option>Name</option>\n        <option>Subcellular_location</option>\n        <option>Symbol</option>\n        <option>Entrez_Gene</option>\n        <option>RefSeq</option>\n        <option>Ensembl_ID</option>\n        <option>Locus</option>\n        <option>Status</option>\n        <option>Proteolytic_activity</option>\n        <option>Disease</option>\n        <option>Note</option></select>";
		}
		if (c == 'present in' || cond.options[cond.selectedIndex].text == 'absent in'){
			svar.style.visibility = '';
			svar2.style.visibility = 'hidden';
			field.style.visibility = 'hidden';
			svar.innerHTML = "<select id='ff'><option>Human</option>\n        <option>Chimpanzee</option>\n        <option>Mouse</option>\n        <option>Rat</option></select>\n";
		}
		if (c == 'mutated in a disease'){
			svar.style.visibility = 'hidden';
			field.style.visibility = 'hidden';
			svar2.style.visibility = 'hidden';
		}
	}
	
	function check_sp(){
		var cond = document.getElementById('svar');
		var svar2 = document.getElementById('svar2');
		var fieldOpt = document.getElementById('ff');
		var f = fieldOpt.options[fieldOpt.selectedIndex].text;
		if (cond.style.visibility == 'hidden'){
			svar2.style.visibility = 'hidden';
		}
		else if (f == '(Any field)' || f == 'Symbol' || f == 'Entrez_Gene' || f == 'RefSeq' || f == 'Ensembl_ID' || f == 'Locus' || f == 'Status' || f == 'Proteolytic_activity'){
			svar2.style.visibility = '';
		}
		else{
			svar2.style.visibility = 'hidden';
		}
	}
	
	
	function reset_x(){
		xmls = [];
	}
	function reset(){
		hitNodes = [];
	}
	function resetResults(){
		reset();
		writeTable(hitNodes);
	}
	function search_degradome(){
		var site = document.getElementById('result_table');
		var s = document.getElementById('s');
		var kind = document.getElementById('kind');
		var cond = document.getElementById('condition');
		var svar = document.getElementById('svar');
		var field = document.getElementById('ff');
		var limSp = document.getElementById('limSp');
		var act = s.options[s.selectedIndex].text;
		var firstField = kind.options[kind.selectedIndex].text;
		var secondField = cond.options[cond.selectedIndex].text;
		var thirdField = '';
		var lSp = '';
		if (limSp.parentNode.style.visibility != 'hidden'){
			if (limSp.options[limSp.selectedIndex].text != '(Any species)'){
				lSp = limSp.options[limSp.selectedIndex].text;
			}
		}
		if (svar.firstChild.tagName == 'INPUT'){
			thirdField = svar.firstChild.value;
		}
		if (svar.firstChild.tagName == 'SELECT'){
			var el = svar.firstChild;
			thirdField = el.options[el.selectedIndex].text;
		}
		if (field){
			var fourthField = field.options[field.selectedIndex].text;
		}
		if (firstField == 'proteases'){
			xmls = xmlDocs;
		}
		if (firstField == 'aspartyl-proteases'){
			xmls['asp'] = xmlDocs['asp'];
		}
		if (firstField == 'cysteine-proteases'){
			xmls['cys'] = xmlDocs['cys'];
		}
		if (firstField == 'metalloproteases'){
			xmls['met'] = xmlDocs['met'];
		}
		if (firstField == 'serine-proteases'){
			xmls['ser'] = xmlDocs['ser'];
		}
		if (firstField == 'threonyl-proteases'){
			xmls['thr'] = xmlDocs['thr'];
		}
		//Now xmls is a vector containing the selected xml documents
		if (act == 'Search for'){
			reset();
		}
		if (secondField == 'containing'){
			var tagsToSearch = new Array();
			var result = new Array();
			if (fourthField == '(Any field)'){
				tagsToSearch = ['Code', 'Name', 'Alias', 'Alias_name', 'Subcellular_location', 'Symbol', 'Entrez_Gene', 'RefSeq', 'Ensembl_ID', 'Locus', 'Status', 'Proteolytic_activity', 'Disease'];
			}
			else if (fourthField == 'Name'){
				tagsToSearch = ['Name', 'Alias_name']
			}
			else if (fourthField == 'Symbol'){
				tagsToSearch = ['Symbol', 'Alias']
			}
			else{
				tagsToSearch = [fourthField];
			}
			result = searchByField(xmls, tagsToSearch, thirdField, lSp);
		}
		if (secondField == 'absent in'){
			result = searchByField(xmls, ['Status'], 'Absent', thirdField);
		}
		/*if (secondField == 'present in'){
			result = searchByFieldNot(xmls, ['Status'], 'Absent', thirdField);
		}
		*/
		if (secondField == 'mutated in a disease'){
			result = searchByField(xmls, ['Disease'], '', '');
		}
		if (act == 'Add to search' || act == 'Search for'){
			addToTable(hitNodes, result);
		}
		if (act == 'Keep'){
			if(len(hitNodes) == 0){
				alert('No results to keep');
			}
			else{
				keepInTable(hitNodes, result);
			}
		}
		if (act == 'Remove from search'){
			if (len(hitNodes) == 0){
				alert('No results to remove');
			}
			removeFromTable(hitNodes, result);
		}
		
		location.hash = 'sbox';
	}
	
	function searchByField(xmls, tagsToSearch, fieldValue, limSp){
		var result = new Array();
		var c = document.getElementById('case').checked;
		var r = new RegExp(fieldValue);
		if (!c){
			r = RegExp(fieldValue, 'i');
		}
		var prevCond = true;
		for (var xm in xmls){
			var proteases = xmls[xm].getElementsByTagName('Protease');
			for (var protease = 0; protease < proteases.length; protease++){
				if (proteases[protease].nodeType && proteases[protease].nodeType == 1){
					for (var tts in tagsToSearch){
						var field = proteases[protease].getElementsByTagName(tagsToSearch[tts]);
						for (var eachTag = 0; eachTag < field.length; eachTag++){
							if (limSp){
								var sp = field[eachTag].parentNode;
								var wsp = sp.getElementsByTagName('id');
								if (wsp[0].firstChild.nodeValue != limSp){
									prevCond = false;
								}
								else{
									prevCond = true;
								}
							}
							if (field[eachTag].nodeType == 1){
								if (field[eachTag].firstChild && r.test(field[eachTag].firstChild.nodeValue)){
									if(prevCond){
										//result.push(proteases[protease]);
										var nam = proteases[protease].getElementsByTagName('Name');
										result[nam[0].firstChild.nodeValue] = new Array();
										result[nam[0].firstChild.nodeValue]['code'] = proteases[protease];
										result[nam[0].firstChild.nodeValue]['cc'] = xm;
										result[nam[0].firstChild.nodeValue]['td2'] = field[eachTag].nodeName;
										result[nam[0].firstChild.nodeValue]['td3'] = field[eachTag].firstChild.nodeValue;
									}
								}
							}
						}
					}
				}
			}
		}
		return result;
	}
	
	function len(assoc_array){
		var result = 0;
		for (var a in assoc_array){
			result++;
		}
		return result;
	}
	
	function addToTable(hits, newHits){
		for (var key in newHits){
			hits[key] = newHits[key];
		}
		writeTable(hits);
	}
	
	function keepInTable(hits, newHits){
		for (var key in hits){
			if (!newHits[key]){
				delete hits[key];
			}
		}
		writeTable(hits);
	}
	
	function removeFromTable(hits, newHits){
		for (var key in newHits){
			if (hits[key]){
				delete hits[key];
			}
		}
		writeTable(hits);
	}
	
	function writeTable(hits){
		var site = document.getElementById('result_table');
		var comment = '';
		if (len(hits)>0){
			comment = 'Found '+ len(hits) + ' results<br /><br />';
		}
		var code = '<button onclick="resetResults()">Clear</button> '+comment+'<table class="searchResult">';
		//code += '<th>
		for (var protease in hits){
			code += '<tr>';
			var el = hits[protease]['code'];
			var cClass = hits[protease]['cc'];
			var nam = el.getElementsByTagName('Name')[0].firstChild.nodeValue;
			nam = nosp(nam)
			var dest = cClass+'.html#'+nam.replace(/ /g, '_');
			code += '<td><a href="'+dest+'">'+nam+'</a></td>';
			//var sym = el.getElementsByTagName('Symbol')[0].firstChild.nodeValue;
			var sym = hits[protease]['td2']+'->'+hits[protease]['td3']
			code += '<td>'+sym+'</td>';
			code += '</tr>';
		}
		code += '</table>';
		site.innerHTML = code;
	}
	
  function nosp(n){
	var result = n.replace(/^[\s\n]+/g, '');
	result = result.replace(/[\s\n]+$/g, '');
	result = result.replace(/[\s\n]+/g, '_');
	result = result.replace(/[\.\,\/\(\)\-\#]/g, '');
	result = result.replace(/^\d+/g, '');
	return result;
  }