function showtagbox() {
	var atb=document.getElementById("addtagbox");
	atb.style.display="block";
}

function getElementsByClass(searchClass,node,tag) {
	var classElements=new Array();
	
	if (node==null) { node = document; }
	if (tag==null) { tag = '*'; }
	
	var els=node.getElementsByTagName(tag);
	var elsLen=els.length;
	var pattern=new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	
	for (i=0,j=0;i<elsLen;i++) {
		if (pattern.test(els[i].className)) {
			classElements[j]=els[i];
			j++;
		}
	}
	
	return classElements;
}

function addtagtodisplay(tv) {
	var tags=getElementsByClass("tag",document,"a");
	var i;
	
	if (tv=="") { return; }
	
	for (i=0;i<tags.length;i++) {
		if (tags[i].innerHTML==tv) { return; }
	}
	
	var l=document.createElement("a");
	l.href="/tags/"+tv;
	l.className="tag";
	l.appendChild(document.createTextNode(tv));
	
	if (tags.length!=0) {
		tags[tags.length-1].parentNode.insertBefore(l,tags[tags.length-1].nextSibling);
		var tx=document.createTextNode(", ");
		tags[tags.length-1].parentNode.insertBefore(tx,tags[tags.length-1].nextSibling);
	} else {
		var t=document.getElementById("tag-none");
		t.parentNode.insertBefore(l,t.nextSibling);
		t.style.display="none";
	}
}

function submittag() {
	var tb=document.getElementById("tagbox");
	var tbv=tb.value;
	
	if (tbv!="") {
		xmlhttp.open("POST","/addtag",true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				/*document.getElementById("spellcheckbutton").value=scbv;
				document.getElementById("spellcheckbutton").disabled=false;
				eddoc.execCommand("readonly",false,true);
				ed.disabled=false;*/
				if (xmlhttp.status==200) {
					addtagtodisplay(xmlhttp.responseText);
				} else {
					alert("Error while adding tag: error "+xmlhttp.status+": "+xmlhttp.statusText);
				}
			}
		}
		xmlhttp.send("desc="+escape(tbv)+"&id="+escape(document.getElementById("soldierid").innerHTML));
	}
	
	var atb=document.getElementById("addtagbox");
	atb.style.display="none";
}

function addsoldier(t,s) {
	var tr=document.createElement("tr");
	var td,a,n,pt,pic,i,img;
	
	n=s.getAttribute("firstname")+" "+s.getAttribute("surname");
	
	pic="";
	for (i=0;i<s.childNodes.length;i++) {
		if (s.childNodes[i].nodeName=="picture") {
			pic=s.childNodes[i].firstChild.nodeValue;
		}
	}
	
	tr.setAttribute("vwmid",s.getAttribute("id"));
	
	td=document.createElement("td");
	if (pic!="") {
		img=document.createElement("img");
		img.setAttribute("src",location.protocol+"//images.burntcomma.com/32h/"+pic);
		img.setAttribute("alt","");
		a=document.createElement("a");
		a.setAttribute("href","/"+s.getAttribute("id"));
		a.appendChild(img);
		td.appendChild(a);
	}
	tr.appendChild(td);
	
	td=document.createElement("td");
	a=document.createElement("a");
	a.setAttribute("href","/"+s.getAttribute("id"));
	a.appendChild(document.createTextNode(n));
	td.appendChild(a);
	tr.appendChild(td);
	
	td=document.createElement("td");
	td.appendChild(document.createTextNode(s.getAttribute("age")));
	tr.appendChild(td);
	
	td=document.createElement("td");
	td.appendChild(document.createTextNode(s.getAttribute("rank")));
	tr.appendChild(td);
	
	td=document.createElement("td");
	for (i=0;i<s.childNodes.length;i++) {
		if (s.childNodes[i].nodeName=="medal") {
			img=document.createElement("img");
			img.setAttribute("alt",s.childNodes[i].getAttribute("desc"));
			img.setAttribute("src",location.protocol+"//images.burntcomma.com/"+s.childNodes[i].getAttribute("id"));
			td.appendChild(img);
		}
	}
	tr.appendChild(td);
	
	td=document.createElement("td");
	a=document.createElement("a"); a.setAttribute("href","/edit/"+s.getAttribute("id")); a.appendChild(document.createTextNode("Edit"));
	td.appendChild(a);
	td.appendChild(document.createTextNode(", "));
	a=document.createElement("a"); a.setAttribute("href","/delete-confirm/"+s.getAttribute("id")); a.appendChild(document.createTextNode("Delete"));
	td.appendChild(a);
	tr.appendChild(td);
	
	t.appendChild(tr);
}

function updatesearchxml(xml) {
	var rb=document.getElementById("numresults");
	var t=document.getElementById("maintable");
	
	var r=xml.getElementsByTagName("numresults")[0].firstChild.nodeValue;
	if (rb.firstChild&&rb.firstChild.nodeType==3) { rb.removeChild(rb.firstChild); }
	rb.appendChild(document.createTextNode(r+" found."));

	for (i=0;i<t.childNodes.length;i++) {
		if (t.childNodes[i].nodeName.toLowerCase()=="tbody") {
			t=t.childNodes[i];
			i=t.childNodes.length;
		}
	}
	
	for (c=t.firstChild;t.firstChild;c=t.firstChild) {
		t.removeChild(c);
	}
	
	var s=xml.getElementsByTagName("soldier");
	for (i=0;i<s.length;i++) {
		addsoldier(t,s[i]);
	}
}

function updatesearch(s) {
	xmlhttp.open("POST","/search",true);
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4&&xmlhttp.status==200) {
			updatesearchxml(xmlhttp.responseXML);
		}
	}
	
	xmlhttp.send("q="+s);
}

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}