var searchParams = new SearchParam("","","","","");

function SearchParam(nation, make, model, unit, tact) {
	this.UNIT = 1;
	this.TACT = 2;
	this.type = 0;
	this.nation = nation;
	this.make = make;
	this.model = model;
	this.unit = unit;
	this.tact = tact;
	if ( unit != null && unit.length > 0) {
		type = type | UNIT;
	}
	if ( tact != null && tact.length > 0) {
		type = type | TACT;
	}
}
SearchParam.prototype.setNation = function(nation) {
	this.nation = nation;
}
SearchParam.prototype.setMake = function(make) {
	this.make = make;
}
SearchParam.prototype.setModel = function(model) {
	this.model = model;
}
SearchParam.prototype.setUnit = function(unit) {
	this.unit = unit;
	unit.length > 0 ? this.type = this.type | this.UNIT : this.type = this.type & 0;
}
SearchParam.prototype.setTact = function(tact) {
	this.tact = tact;
	tact.length > 0 ? this.type = this.type | this.TACT : this.type >= 2 ? this.type = this.type -2 : this.type = this.type;
}
SearchParam.prototype.getPositiveMsg = function() {
	switch( this.type ) {
		case 0:
			return "There is no <b>Positive ID</b> when no unit/tactical# are specified";		
		case this.UNIT:
			return "any equipment of the specified category/model whose unit matches";
		case this.TACT:
			return "any equipment of the specified category/model whose tactical# matches";
		case this.UNIT | this.TACT:
			return "any equipment of the specified category/model whose unit <b>AND</b> tactical# match";
	}
}
SearchParam.prototype.getStrongMsg = function() {
	switch( this.type ) {
		case 0:
			return "There is no <b>String Match</b> when no unit/tactical# are specified";
		case this.UNIT:
			return "There is no <b>String Match</b> when only unit is specified";
		case this.TACT:
			return "any equipment of the specified category/model whose tactical# is a <i>partial</i> match";
		case this.UNIT | this.TACT:
			return "any equipment of the specified category/model with a unit match and <i>partial</i> tactical# match <b>OR</b>"+
				" any equipment of the specified category/model with a tactical# match and the unit is <i>unknown</i>";
	}
}
SearchParam.prototype.getWeakMsg = function() {
	switch( this.type ) {
		case 0:
			return "any equipment that matches the equipment type and (if speicifed) equipment model";
		case this.UNIT:
			return "any equipment of the specified category/model where the unit is <i>unknown</i>";
		case this.TACT:
			return "any equipment of the specified category/model where the tactical# is <i>unknown</i>";
		case this.UNIT | this.TACT:
			return "any equipment of the specified category/model where the the unit is <i>unknown</i> and the tactical# is a <i>partial</i> match";
	}
}

function search() {
	var btn = document.getElementById("searchButton");
	btn.disabled = true;
	var xml ="<?xml version='1.0' encoding='UTF-8'?>";
	xml += "<search>";
	
	var el = document.getElementById("nationSearch");
	el = (el.hasChildNodes() ? el.childNodes[0].nodeValue : "");
	if (el.length == 0) { alert("Nation required for search"); btn.disabled = false; return;}
	xml += "<nation>"+el+"</nation>";
	
	el = document.getElementById("makeSearch");
	el = (el.hasChildNodes() ? el.childNodes[0].nodeValue : "");
	if (el.length == 0) { alert("AFV Type required for search"); btn.disabled = false; return;}
	xml += "<make>"+el+"</make>";
	
	el = document.getElementById("modelSearch");
	el = (el.hasChildNodes() ? el.childNodes[0].nodeValue : "");
	xml += "<model>"+el+"</model>";
	
	el = document.getElementById("unitSearch");
	el = el.value;
	xml += "<unit>"+el+"</unit>";
	
	el = document.getElementById("tactSearch");
	el = el.value;
	xml += "<tact>"+el+"</tact>";
	xml += "</search>";
	
	var url = "http://protein.informatics.indiana.edu/newpdb/user.jsp?type=search";
	var rXml = getHttpXml();
	rXml.onreadystatechange = function() {
	    if (rXml.readyState == 4) {
	        if (rXml.status == 200) {
	        clearSearch();
	            displaySearchResults(rXml.responseXML);
	        } else {
	            alert("Error retrieving search results. Search cancelled");
	        }
	        document.getElementById("searchButton").disabled= false;
	    }
	}
	rXml.open("POST", url, true);
	clearSearch();
	var row = document.getElementById("positive");
	var el = document.createElement("tr");
	var el1 = document.createElement("td");
	el1.style.background = "red";
	el1.appendChild(document.createTextNode("Searching..."));
	el.appendChild(el1);
	row.appendChild(el);
	rXml.send(xml);
}
function clearSearch() {
	var t = document.getElementById("positive");
	while (t.hasChildNodes()) {
		t.removeChild(t.childNodes[0]);
	}
	t = document.getElementById("strong");
	while (t.hasChildNodes()) {
		t.removeChild(t.childNodes[0]);
	}
	t = document.getElementById("weak");
	while (t.hasChildNodes()) {
		t.removeChild(t.childNodes[0]);
	}
}
function displaySearchResults(xml)  {
	if (checkError(xml)) {
		return;
	}
	var res = xml.getElementsByTagName("matches")[0];
	var pos = res.childNodes[0];
	window.searchParams.setNation(pos.getAttribute("nation"));
	window.searchParams.setMake(pos.getAttribute("make"));
	window.searchParams.setModel(pos.getAttribute("model"));
	window.searchParams.setUnit(pos.getAttribute("unit"));
	window.searchParams.setTact(pos.getAttribute("tact"));
	
	pos = res.getElementsByTagName("positiveID");
	if (pos.length > 0 && pos[0].childNodes.length > 0) {
		displayMatches("Positive ID", pos[0].childNodes, document.getElementById("positive"));
	}
	var strong = res.getElementsByTagName("strongMatch");
	if (strong.length > 0 && strong[0].childNodes.length > 0) {
		displayMatches("Strong Matches", strong[0].childNodes, document.getElementById("strong"));
	}
	var weak = res.getElementsByTagName("weakMatch");
	if (weak.length > 0 && weak[0].childNodes.length > 0) {
		displayMatches("Weak Matches", weak[0].childNodes, document.getElementById("weak"));
	} 
}
function displayMatches(type, books, rel) {
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.style.background = "#C18028";
	td.setAttribute((window.ActiveXObject ? "colSpan" : "colspan"), "5");
	td.appendChild( document.createTextNode(type));
	td.style.fontWeight = "bold";
	td.style.textAlign = "center";
	td.style.cursor = "help";
	td.onclick = function() {
		var xl = document.getElementById("x0");
		xl.innerHTML = "<b>"+type+"</b> - ";
		var x = document.getElementById("x1");
		x.innerHTML = window.searchParams.nation;
		x = document.getElementById("x2");
		x.innerHTML = window.searchParams.make;
		x.style.background = "#B4CECC";
		x = document.getElementById("x3")
		x.innerHTML = window.searchParams.model;
		if (window.searchParams.model.length > 0) {
			x.style.background = "#B4CECC";
		}
		x = document.getElementById("x4");
		x.innerHTML = window.searchParams.unit;
		if (window.searchParams.unit.length > 0) {
			x.style.background = "#B4CECC";
		}
		x = document.getElementById("x5");
		x.innerHTML = window.searchParams.tact;
		if (window.searchParams.tact.length > 0) {
			x.style.background = "#B4CECC";
		}
		switch (type) {
			case "Positive ID":
				xl.innerHTML += window.searchParams.getPositiveMsg();
				break;
			case "Strong Matches":
				xl.innerHTML += window.searchParams.getStrongMsg();
				break;
			case "Weak Matches" :
				xl.innerHTML += window.searchParams.getWeakMsg();
				break;
			default:
				break;
		}
		var m = document.getElementById("matchText");
		m.style.left = findAbsX(this)+"px";
		m.style.top = findAbsY(this)+"px";
		m.style.visibility = "visible";
	}
	td.onmouse
	tr.appendChild(td);
	rel.appendChild(tr);
	

	for (i = 0; i < books.length; i++) {
	    book = books[i];
	    tr = document.createElement("tr");
	    td = document.createElement("td");
	    rel.appendChild(tr);
	    tr.appendChild(td);
	    td.appendChild( document.createTextNode( book.getAttribute("title")));
	    td.setAttribute("colspan", "5");
	    td.colSpan = "5";
	    td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultBook");
	    tr = document.createElement("tr");
	    rel.appendChild(tr);
	    td = document.createElement("td");
	    td.appendChild( document.createTextNode("Page"));
	    td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultColHeader");
	    tr.appendChild(td);
	    td = document.createElement("td");
	    td.appendChild( document.createTextNode("Unit"));
	    td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultColHeader");
	    tr.appendChild(td);
	    td = document.createElement("td");
	    td.appendChild( document.createTextNode("Tact #"));
	    td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultColHeader");
	    tr.appendChild(td);
	    td = document.createElement("td");
	    td.appendChild( document.createTextNode("Type"));
	    td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultColHeader");
	    tr.appendChild(td);
	    td = document.createElement("td");
	    td.appendChild( document.createTextNode("Model"));
	    td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultColHeader");
	    tr.appendChild(td);
	    
	    pages = book.childNodes;
	    for (j = 0; j < pages.length; j++) {
	        rn = pages[j];
	        tr = document.createElement("tr");
	        rel.appendChild(tr);
	        td = document.createElement("td");
	        td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultPage");
	        tr.appendChild( td);
	        td.appendChild( document.createTextNode("p."+rn.getAttribute("page")));
	        td = document.createElement("td");
	        td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultPage");
	        tr.appendChild(td);
	        td.appendChild( document.createTextNode(rn.getAttribute("unit")));
	        td = document.createElement("td");
	        td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultPage");
	        tr.appendChild(td);
	        td.appendChild( document.createTextNode(rn.getAttribute("tact")));
	        
	        td = document.createElement("td");
	        td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultPage");
	        tr.appendChild(td);
	        td.appendChild( document.createTextNode(rn.getAttribute("make")));
	        td = document.createElement("td");
	        td.setAttribute((window.ActiveXObject ? "className" : "class"), "searchResultPage");
	        tr.appendChild(td);
	        td.appendChild( document.createTextNode(rn.getAttribute("model")));
	    }
	}
}

function checkError(xml) {
	if (xml.childNodes[0].nodeName == "error") {
		alert("ERROR\n\n"+xml.childNodes[0].childNodes[0].childNodes[0].nodeValue+"\n\n"+xml.childNodes[0].childNodes[1].childNodes[0].nodeValue);
		return true;
	} else {
		return false;
	}
}
