/*
 * Utility Script
 */

function GetXmlHttpObject(handler)
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
	objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
	return objXMLHttp
}

function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function onLoadStats()
{

	var getVars = getUrlVars();
	if (getVars["pilotName"] != undefined)
	{
		changeTab(3);
		document.getElementById("txtPilot").value = decodeURIComponent(getVars["pilotName"]);
		updatePilot();
	}
	else if (getVars["corpName"] != undefined)
	{
		changeTab(3);
		document.getElementById("txtCorp").value = decodeURIComponent(getVars["corpName"]);
		updateCorp();
	}

}
/*
 * Menu Control
 */
var tab = 1;
function changeTab(tabno)
{
	if (tabno == 1)
	{
		hideAllTabs();
		clearTabColor();
		document.getElementById("tab1").style.color = "#FFFFFF";
		document.getElementById("generalstats").style.display = "block";
		tab = 1;
	}
	else if (tabno == 2)
	{
		hideAllTabs();
		clearTabColor();
		document.getElementById("tab2").style.color = "#FFFFFF";
		document.getElementById("pilotcorpstats").style.display = "block";
		tab = 2;
	}
	else if (tabno == 3)
	{
		hideAllTabs();
		clearTabColor();
		document.getElementById("tab3").style.color = "#FFFFFF";
		document.getElementById("customstats").style.display = "block";
		tab = 3;
	}
}
function hideAllTabs()
{
		document.getElementById("generalstats").style.display = "none";
		document.getElementById("pilotcorpstats").style.display = "none";
		document.getElementById("customstats").style.display = "none";
}
function clearTabColor()
{
	document.getElementById("tab1").style.color = "#FF9320";
	document.getElementById("tab2").style.color = "#FF9320";
	//document.getElementById("tab3").style.color = "#FF9320";
}

/*
 * General Stats Script
 */
$g1type = "total";
$g1timeperiod = "7day";
$g2type = "total";
$g2timeperiod = "7day";
$g4type = "total";
$g4timeperiod = "30day";
$g3timeperiod = "30day";
function updateGraphType($chartid, $roottype, $type)
{
	if ($chartid == "chart1")
	{
		$g1type = $type;
		$url = "./charts/militia-" + $roottype + "-" + $g1type + "-" + $g1timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	else if ($chartid == "chart4")
	{
		$g1type = $type;
		$url = "./charts/militia-" + $roottype + "-" + $g4type + "-" + $g4timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	else
	{
		$g2type = $type;
		$url = "./charts/militia-" + $roottype + "-" + $g2type + "-" + $g2timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	updateChartTypeOptions();
}
function updateGraphTimePeriod($chartid, $roottype, $timeperiod)
{
	if ($chartid == "chart1")
	{
		$g1timeperiod = $timeperiod;
		$url = "./charts/militia-" + $roottype + "-" + $g1type + "-" + $g1timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	else if ($chartid == "chart2")
	{
		$g2timeperiod = $timeperiod;
		$url = "./charts/militia-" + $roottype + "-" + $g2type + "-" + $g2timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	else if ($chartid == "chart4")
	{
		$g4timeperiod = $timeperiod;
		$url = "./charts/militia-" + $roottype + "-" + $g4type + "-" + $g4timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	else if ($chartid == "chart3")
	{
		$g3timeperiod = $timeperiod;
		$url = "./charts/militia-" + $roottype + "-" + $g3timeperiod + ".png";
		document.getElementById($chartid).src = $url;
	}
	updateChartTimePeriodOptions();
}
function updateChartTypeOptions()
{
	var highlightcolour = "#FF9320";
	var normalcolour = "#FFFFFF";
	if ($g1type == "total")
	{
		document.getElementById("chart1total").style.color = normalcolour;
		document.getElementById("chart1permember").style.color = highlightcolour;
	}
	else
	{
		document.getElementById("chart1total").style.color = highlightcolour;
		document.getElementById("chart1permember").style.color = normalcolour;
	}
	if ($g2type == "total")
	{
		document.getElementById("chart2total").style.color = normalcolour;
		document.getElementById("chart2permember").style.color = highlightcolour;
	}
	else
	{
		document.getElementById("chart2total").style.color = highlightcolour;
		document.getElementById("chart2permember").style.color = normalcolour;
	}
}
function updateChartTimePeriodOptions()
{
	var highlightcolour = "#FF9320";
	var normalcolour = "#FFFFFF";
	if ($g1timeperiod == "7day")
	{
		document.getElementById("chart17d").style.color = normalcolour;
		document.getElementById("chart130d").style.color = highlightcolour;
	}
	else
	{
		document.getElementById("chart17d").style.color = highlightcolour;
		document.getElementById("chart130d").style.color = normalcolour;
	}
	if ($g2timeperiod == "7day")
	{
		document.getElementById("chart27d").style.color = normalcolour;
		document.getElementById("chart230d").style.color = highlightcolour;
	}
	else
	{
		document.getElementById("chart27d").style.color = highlightcolour;
		document.getElementById("chart230d").style.color = normalcolour;
	}
	if ($g3timeperiod == "7day")
	{
		document.getElementById("chart37d").style.color = normalcolour;
		document.getElementById("chart330d").style.color = highlightcolour;
	}
	else
	{
		document.getElementById("chart37d").style.color = highlightcolour;
		document.getElementById("chart330d").style.color = normalcolour;

	}
	if ($g4timeperiod == "7day")
	{
		document.getElementById("chart47d").style.color = normalcolour;
		document.getElementById("chart430d").style.color = highlightcolour;
	}
	else
	{
		document.getElementById("chart47d").style.color = highlightcolour;
		document.getElementById("chart430d").style.color = normalcolour;
	}
}

/*
 * Pilot Stats Script
 */
function ajaxLoadPilotDivContent(pilotName)
{
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) 
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			document.getElementById("pilotKillStats").innerHTML = xmlHttp.responseText;
			ajaxLoadPilot2DivContent(pilotName);
		} 	
	};

	xmlHttp.open("GET","fwstats-pilot-kill-stats.php?pilotName=" + encodeURIComponent(pilotName) + "&sid="+Math.random(),true);
	xmlHttp.send(null);

}
function ajaxLoadPilot2DivContent(pilotName)
{
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) 
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			document.getElementById("pilotVPStats").innerHTML = xmlHttp.responseText;
			document.getElementById("btnPilotSearch").enabled = true;

		} 	
	};

	xmlHttp.open("GET","fwstats-pilot-vp-stats.php?pilotName=" + encodeURIComponent(pilotName) + "&sid="+Math.random(),true);
	xmlHttp.send(null);

}	

function loadingCustomStats(divid)
{
	
	document.getElementById(divid).style.height = "100px";
	document.getElementById(divid).innerHTML = "<img src=\"ajax-loader.gif\" style=\"margin-top: 20px;margin-left: 200px\" title=\"Loading Statistics...\">";

}

function hidePilotPanel()
{
	document.getElementById("pilotStatsHeader").style.display = "none";
	document.getElementById("pilotStats").style.display = "none";
}

function updatePilot()
{
	//clear current status
	document.getElementById("btnPilotSearch").enabled = false;

	//Hide currently open stats panels
	hidePilotPanel();
	loadingCustomStats("pilotKillStats");
	loadingCustomStats("pilotVPStats");
	document.getElementById("pilotStatsHeader").style.display = "block";
	document.getElementById("pilotStats").style.display = "block";

	document.getElementById("pilotname").innerHTML =  "- " + document.getElementById("txtPilot").value;

	//Right, lets grab the statistics using a spot of ajax magic.
	ajaxLoadPilotDivContent(document.getElementById("txtPilot").value);
}

/*
 * Corp Stats Script
 */
function ajaxLoadCorpDivContent(corpName)
{
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) 
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			document.getElementById("corpKillStats").innerHTML = xmlHttp.responseText;
			document.getElementById("btnCorpSearch").enabled = true;
			ajaxLoadCorp2DivContent(corpName);
		} 	
	};

	xmlHttp.open("GET","fwstats-corp-kill-stats.php?corpName=" + encodeURIComponent(corpName) + "&sid="+Math.random(),true);
	xmlHttp.send(null);

}
function ajaxLoadCorp2DivContent(corpName)
{
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) 
	{
		alert ("Browser does not support HTTP Request")
		return
	} 

	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			document.getElementById("corpVPStats").innerHTML = xmlHttp.responseText;
			document.getElementById("btnCorpSearch").enabled = true;

		} 	
	};

	xmlHttp.open("GET","fwstats-corp-vp-stats.php?corpName=" + encodeURIComponent(corpName) + "&sid="+Math.random(),true);
	xmlHttp.send(null);

}	

function loadingCustomStats(divid)
{
	
	document.getElementById(divid).style.height = "100px";
	document.getElementById(divid).innerHTML = "<img src=\"images/ajax-loader.gif\" style=\"margin-top: 20px;margin-left: 200px\" title=\"Loading Statistics...\">";

}

function hideCorpPanel()
{
	document.getElementById("corpStatsHeader").style.display = "none";
	document.getElementById("corpStats").style.display = "none";
}

function updateCorp()
{
	//clear current status
	document.getElementById("btnCorpSearch").enabled = false;

	//Hide currently open stats panels
	hideCorpPanel();
	loadingCustomStats("corpKillStats");
	loadingCustomStats("corpVPStats");
	document.getElementById("corpStatsHeader").style.display = "block";
	document.getElementById("corpStats").style.display = "block";

	document.getElementById("corpname").innerHTML =  "- " + document.getElementById("txtCorp").value;

	//Right, lets grab the statistics using a spot of ajax magic.
	ajaxLoadCorpDivContent(document.getElementById("txtCorp").value);
}
