var theCosts = new Array();

function editThis(theID, inputType, focID) {
	
	theDiv = document.getElementById(theID);
	
	divWidth = theDiv.offsetWidth-2;
	divHeight = theDiv.offsetHeight-3;
	

	if (theDiv.firstChild.nodeType == 3) {
		//  a text node, so use input field
		exCont = theDiv.innerHTML;
	
		theDiv.innerHTML = "";
		
		if (inputType == 1) {
			// do input box
		
			theInput = createNamedElement('input','editfield');
			theInput.setAttribute("ID",theID+"_edit");
			theInput.setAttribute("value",exCont);
			theInput.style.cssText = "font-family:calibri,sans-serif;background-color:#555; border:none; color:#fff;font-size:0.9em;height:"+divHeight+"px;width:"+divWidth+"px";
			if (theID.indexOf("cost") > -1) {
				theInput.style.cssText += ";text-align:right";
			}

		} else {
			// do text area
			theInput = createNamedElement('textarea','editfield');
			theInput.setAttribute("ID",theID+"_edit");
			//theInput.setAttribute("value",exCont);
			theInput.value = exCont;
			theInput.style.cssText = "font-family:calibri,sans-serif;background-color:#555; border:none; color:#fff;font-size:0.9em;height:"+divHeight+"px;width:"+divWidth+"px";

		}
		
		
		theDiv.appendChild(theInput);
		
		document.getElementById(theID+"_edit").onblur = function() {
			saveEdit(theID, focID);
		}
		document.getElementById(theID+"_edit").focus();
	}

}


function createNamedElement(type, name) {
   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 saveEdit(theID, focID) {
	editBox = document.getElementById(theID+"_edit");
	theEntry = "";
	armyID = document.getElementById("armyID");
		
	if (armyID && armyID.value != "") {
		aID = armyID.value;
	} else {
		aID = "";
	}
	
	if (editBox) {
		theEntry = editBox.value;
		// unit number
		if (theID == "army_title") {
			uNum = 0;
			uComp = "";
		} else {
			uNum = theID.substr(4,theID.indexOf("_")-4);
			uComp = theID.substring(theID.indexOf("_")+1,theID.indexOf("~"));
		}
			
		// save it
		saveEntry(uComp, theEntry, uNum, aID, focID);
	}
	
	// turn off editing on the box
	theBox = document.getElementById(theID);
	theBox.removeChild(theBox.lastChild);
	theBox.innerHTML = theEntry;

	if (uComp && uComp == "cost") 
		getTotal();
}

function getTotal() {


	theDivs = document.getElementsByName('cost');

	
	
	curFoc = -1;
	curFocTotal = 0;
	armyTotal = 0;
	
	for (divs in theDivs) {
		if (theDivs[divs].id) {
		
			tmp = theDivs[divs].id.split('~');
			
			focID = tmp[1];
			
			if (focID != curFoc) {
				// save last total
				if (curFoc > -1) {
					focTot = document.getElementById(curFoc+"_total");
					focTot.innerHTML = curFocTotal;
				}
				curFoc = focID;
				curFocTotal = 0;
			}
			// add this cost
			if (theDivs[divs].innerHTML == parseInt(theDivs[divs].innerHTML)) {
				curFocTotal += parseInt(theDivs[divs].innerHTML);
				armyTotal += parseInt(theDivs[divs].innerHTML);
			}
		}	
	}	
	if (curFoc > -1) {
		focTot = document.getElementById(curFoc+"_total");
		focTot.innerHTML = curFocTotal;
	}
				
	document.getElementById('army_total').innerHTML = armyTotal;
}






// AJAXY STUFF
function saveEntry(thebox, str, num, aID, focID) { 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	// encode string for safe passing
	safestr = escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	
	var url="modules/armylists.php?task=saveupdate&armyID="+aID+"&id="+num+"&unitdetail="+thebox+"&entry="+safestr+"&focID="+focID;


	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("unfb").innerHTML=xmlHttp.responseText;

		armyID = document.getElementById('armyID');
		armyTitle = document.getElementById('army_title');
		tmp = xmlHttp.responseText.split("_");
		
		if (armyID.value == "" && tmp[0] != "") {
			armyID.value = tmp[0];
		}

		if (armyTitle.innerHTML == "Army Name" && tmp[1] != "" ) {
			armyTitle.innerHTML = tmp[1];
		}
		
	} 
}

function GetXmlHttpObject() {
	var 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;
}

