var selectedNanos = new Object();


function profession_check() {
	if (document.getElementById("profession").value == "Trader" || document.getElementById("profession").value == "Agent") {
		document.getElementById("traderdrains").className = "fleft mstats tradershow";
	}
	else {
		document.getElementById("traderdrains").className = "fleft mstats traderhide";
	}
}

function prepareToggle() {
	var span = document.getElementsByTagName("span");
	for (i=0;i<span.length;i++) {
		var link = span[i].getElementsByTagName("a");
		if (link.length > 0) {
			link[0].onclick = function() {
				toggle_vis(this.href);
				return false;
			}
		}
	}
}

function preparePastFuture() {
	var theLinks = document.getElementsByTagName("a");
	for (i=0;i<theLinks.length;i++) {
		if (theLinks[i].className == "showhidea") {
			theLinks[i].onclick = function() {
				pastFuture(this.href);
				return false;
			}

		}
	}
}
function pastFuture(win) {
	win = win.split("/");
	win = win[win.length-1];
	var theDiv = document.getElementById(win);
	if (theDiv.className == "visible") {
		var theClass = "hidden";
		if (win.indexOf("past") > 0)
			var Txt = document.createTextNode("Show past nanos");
		else
			var Txt = document.createTextNode("Show future nanos");
	}
	else {
		var theClass = "visible";
		if (win.indexOf("past") > 0)
			var Txt = document.createTextNode("Hide past nanos");
		else
			var Txt = document.createTextNode("Hide future nanos");
	}
	theDiv.className = theClass;
	var aLink = document.getElementById(win+"_a");
	if (aLink != null) {
		while (child = aLink.firstChild) { var junk = aLink.removeChild(child); }
		aLink.appendChild(Txt);
	}
}

function pastFutureExp(win) {
	win = win.split("/");
	win = win[win.length-1];
	var theDiv = document.getElementById(win);
	var theClass = "visible";
	if (win.indexOf("past") > 0)
		var Txt = document.createTextNode("Hide past nanos");
	else
		var Txt = document.createTextNode("Hide future nanos");
	theDiv.className = theClass;
	var aLink = document.getElementById(win+"_a");
	if (aLink != null) {
		while (child = aLink.firstChild) { var junk = aLink.removeChild(child); }
		aLink.appendChild(Txt);
	}
}
function pastFutureExpHide(win) {
	win = win.split("/");
	win = win[win.length-1];
	var theDiv = document.getElementById(win);
	var theClass = "hidden";
	if (win.indexOf("past") > 0)
		var Txt = document.createTextNode("Show past nanos");
	else
		var Txt = document.createTextNode("Show future nanos");
	theDiv.className = theClass;
	var aLink = document.getElementById(win+"_a");
	if (aLink != null) {
		while (child = aLink.firstChild) { var junk = aLink.removeChild(child); }
		aLink.appendChild(Txt);
	}
}

function toggle_vis(win) {
	win = win.split("/");
	win = win[win.length-1];
	var text_window = win+'_text';
	var divname = win+'_div';
	var theSpan = document.getElementById(text_window);
	var theText = theSpan.getElementsByTagName("a")[0];
	var textName = theText.firstChild;
	var label = textName.nodeValue.substr(2);
	while (child = theText.firstChild) { var junk = theText.removeChild(child); }
	var theTable = document.getElementById(win);
	if (theTable.className == "visible") {
		theTable.className = "hidden";
		var txt = document.createTextNode("» "+label);
		theText.appendChild(txt);
	}
	else {
		theTable.className = "visible";
		var txt = document.createTextNode("« "+label);
		theText.appendChild(txt);
	}
	return false;
}


function toggle_window(win) {
	var the_window = 'show_all_' + win;
	document.getElementById(the_window).innerHTML = document.getElementById(win).style.display ? "Hide future nanos" : "Show future nanos";
	document.getElementById(win).style.display = document.getElementById(win).style.display ? "" : "none";
}

function quickNav() {
	var theSelect = document.getElementById("quickNav");
	location.href = "#"+theSelect.value+"_div";
	theSelect.selectedIndex = 0;
}

function hideAllF() {
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if (divs[i].className == "visible" && divs[i].parentNode.tagName == "FIELDSET") {
			//divs[i].className = "hidden";
			toggle_vis(divs[i].id);
		}
	}
}
function showAllF() {
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if (divs[i].className == "hidden" && divs[i].parentNode.tagName == "FIELDSET") {
			//divs[i].className = "visible";
			toggle_vis(divs[i].id);
		}
	}
}

function showAllPastF() {
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if  (divs[i].id.substr(-5) == "_past") {
			pastFutureExp("/"+divs[i].id);
		}
	}
}
function showAllFutureF() {
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if  (divs[i].id.substr(-7) == "_future") {
			pastFutureExp("/"+divs[i].id);
		}
	}
}
function hideAllPastF() {
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if  (divs[i].id.substr(-5) == "_past") {
			pastFutureExpHide("/"+divs[i].id);
		}
	}
}
function hideAllFutureF() {
	var divs = document.getElementsByTagName("div");
	for (i=0;i<divs.length;i++) {
		if  (divs[i].id.substr(-7) == "_future") {
			pastFutureExpHide("/"+divs[i].id);
		}
	}
}

function prepareNavLinks() {
	//var link = document.getElementById("hide_a");
	if (link = document.getElementById("hide_a")) {
		link.onclick = function() {
			hideAllF();
			return false;
		}
	}
	//var link = document.getElementById("show_a");
	if (link = document.getElementById("show_a")) {
		link.onclick = function() {
			showAllF();
			return false;
		}
	}
	//var link = document.getElementById("past_e");
	if (link = document.getElementById("past_e")) {
		link.onclick = function() {
			showAllF();
			showAllPastF();
			return false;
		}
	}
	//var link = document.getElementById("future_e");
	if (link = document.getElementById("future_e")) {
		link.onclick = function() {
			showAllF();
			showAllFutureF();
			return false;
		}
	}
	//var link = document.getElementById("both_e");
	if (link = document.getElementById("both_e")) {
		link.onclick = function() {
			showAllF();
			showAllPastF();
			showAllFutureF();
			return false;
		}
	}
	//var link = document.getElementById("past_c");
	if (link = document.getElementById("past_c")) {
		link.onclick = function() {
			//hideAllF();
			hideAllPastF();
			return false;
		}
	}
	//var link = document.getElementById("future_c");
	if (link = document.getElementById("future_c")) {
		link.onclick = function() {
			//hideAllF();
			hideAllFutureF();
			return false;
		}
	}
	//var link = document.getElementById("both_c");
	if (link = document.getElementById("both_c")) {
		link.onclick = function() {
			//hideAllF();
			hideAllPastF();
			hideAllFutureF();
			return false;
		}
	}
}


window.onload = function() {
	startUp();
	prepareToggle();
	preparePastFuture();
	prepareNavLinks();
}

function startUp() {
	//boldOnHover("bla");
	document.getElementById("modButton").onclick = function() {
		modifyNo("all");
		return false;
	}
	profession_check();
	var aLinks = document.getElementsByTagName("a");
	for (i=0;i<aLinks.length;i++) {
		if (aLinks[i].className == "singleadd") {
			aLinks[i].onclick = function() {
				modifyNo(this.href);
				return false;
			}
		}
	}
	document.getElementById("resetForm").onclick = function() {
		location.href = "index.php";
		return false;
	}
	document.getElementById("profession").onchange = function() {
		profession_check();
	}
}

function boldOnHover(notusedatall) {
	var frameDiv = document.getElementsByTagName("div");
	for (i=1;i<frameDiv.length;i++) {
		if (frameDiv[i].className == "frame") {
			frameTr = frameDiv[i].getElementsByTagName("tr");
			for (c=1;c<frameTr.length;c++) {
				frameTr[c].onmouseover = function() {
					//this.style.fontWeight = "bold";
					//this.style.background = "#FFFFFF";
				}
				frameTr[c].onmouseout = function() {
					//this.style.fontWeight = "normal";
					//this.style.background = "";
				}
			}
		}
	}
}

function modifyNo(modWhat) {
	var boxes = document.getElementsByTagName("input");
	if (modWhat == "all") {
		var modNo = parseInt(document.getElementById("modno").value);
		if (isNaN(modNo)) modNo = 0;
		for (i=0;i<boxes.length;i++) {
			if (boxes[i].className == "box") {
				if (boxes[i].name != "level" && boxes[i].name != "spec") {
					var newVal = modNo+parseInt(boxes[i].value);
					boxes[i].setAttribute("value",newVal);
				}
			}
		}
	}
	else {
		modWhat = modWhat.split("/");
		modWhat = modWhat[modWhat.length-1];
		var modNo = prompt("Modify "+modWhat+" by how much? (Prefix with - to subtract)");
		var modNo = parseInt(modNo);
		if (isNaN(modNo)) modNo = 0;
		for(i=0;i<boxes.length;i++) {
			if (boxes[i].className == "box" && modWhat == boxes[i].name) {
				var newVal = modNo+parseInt(boxes[i].value);
				boxes[i].setAttribute("value",newVal);
			}
		}
	}
	return false;
}


function toggleRowSelect(aoid) {
	var row = document.getElementById('nano_'+aoid);
	if (row.className == 'nanorow') {
		row.className = 'nanorow selRow';
		window.selectedNanos[aoid] = 'set';
	}
	else {
		delete window.selectedNanos[aoid];
		row.className = 'nanorow';
	}
}

function submitNanoList() {
	//alert("Currently not working.\nRedoing orgbot and havn't fixed this part yet.\nWill do it sometime after aug 30th");
	//return false;
	var charName;
	var len = 0;
	for (var theIndex in selectedNanos) {
		len++;
	}
	if (len > 0) {
		if (charName = prompt('Character name? RK1 only!')) {
			selectedNanos['charName'] = charName;
			/*
			var out = 'In array: ';
			for (var theIndex in selectedNanos) {
				out = out+' '+theIndex;
			}
			*/
			
			new Ajax.Request('sendIngame.php', {
				method: 'post',
				parameters: selectedNanos,
				onComplete: function(resp) {
					alert(resp.responseText);
				}
			});
		}
	}
	else
		alert("You havn't selected any nanos.");
}
