var xmlHttp

var lv = 5
var pk = 387
var nat = '0'
var adv = 0
var ev_sum = 0
var ev_table = 0
var stat_input = 0
var stat_remain = 0

var hp = 0
var at = 0
var df = 0
var sp = 0
var sa = 0
var sd = 0

var s_hp = 0
var s_at = 0
var s_df = 0
var s_sp = 0
var s_sa = 0
var s_sd = 0

function showAdvanced() {
	if (adv == 0) {
		document.getElementById("advanced_options").style.display = "block"
		document.getElementById("show_advanced").innerHTML = "Hide Advanced Options"
	} else {
		document.getElementById("advanced_options").style.display = "none"
		document.getElementById("show_advanced").innerHTML = "Show Advanced Options"
	}
	adv = invertBool(adv)
}

function invertBool(n) {
	if (n == 0) {
		return 1
	} else {
		return 0
	}
}

function displayStat()
{
	lv = document.getElementById("level").value;
	if (lv > 100 || lv < 1) { 
		document.getElementById("statresult").innerHTML="You must enter a Pokemon's Level between 1 and 100."
		return
	} else if (pk < 0 || pk > 500) { 
		document.getElementById("statresult").innerHTML="You must select a valid Pokemon."
		return
	}


	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
	alert ("Browser does not support HTTP Request")
	return
	} 
	var url="statcalc_dp_ajax.php"
	url=url+"?pokemon="+pk
	url=url+"&level="+lv
		if (nat.charAt(0) != nat.charAt(1)) {
			url=url+"&natpos="+nat.charAt(0)
			url=url+"&natneg="+nat.charAt(1)
		} else {
			url=url+"&natpos=9&natneg=9"
		}
		if (ev_table == 1) {
			url=url+"&evs=1"
			url=url+"&hp="+hp
			url=url+"&at="+at
			url=url+"&df="+df
			url=url+"&sp="+sp
			url=url+"&sa="+sa
			url=url+"&sd="+sd
		}
		if (stat_input == 1) {
			url=url+"&stat_input=1"
			url=url+"&s_hp="+s_hp
			url=url+"&s_at="+s_at
			url=url+"&s_df="+s_df
			url=url+"&s_sp="+s_sp
			url=url+"&s_sa="+s_sa
			url=url+"&s_sd="+s_sd
			stat_remain = 0
		}
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null) 

} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==2 || xmlHttp.readyState==3) {
		document.getElementById("info_status").innerHTML='Loading...';
	}
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("info_status").innerHTML='Please Enter the Information';
		document.getElementById("statresult").innerHTML=xmlHttp.responseText
		if (stat_input == 1) {
			document.getElementById("stat_preview_hp_min").innerHTML = document.getElementById("stat_min_hp").innerHTML;
			document.getElementById("stat_preview_hp_max").innerHTML = document.getElementById("stat_max_hp").innerHTML;
			document.getElementById("stat_preview_at_min").innerHTML = document.getElementById("stat_min_at").innerHTML;
			document.getElementById("stat_preview_at_max").innerHTML = document.getElementById("stat_max_at").innerHTML;
			document.getElementById("stat_preview_df_min").innerHTML = document.getElementById("stat_min_df").innerHTML;
			document.getElementById("stat_preview_df_max").innerHTML = document.getElementById("stat_max_df").innerHTML;
			document.getElementById("stat_preview_sp_min").innerHTML = document.getElementById("stat_min_sp").innerHTML;
			document.getElementById("stat_preview_sp_max").innerHTML = document.getElementById("stat_max_sp").innerHTML;
			document.getElementById("stat_preview_sa_min").innerHTML = document.getElementById("stat_min_sa").innerHTML;
			document.getElementById("stat_preview_sa_max").innerHTML = document.getElementById("stat_max_sa").innerHTML;
			document.getElementById("stat_preview_sd_min").innerHTML = document.getElementById("stat_min_sd").innerHTML;
			document.getElementById("stat_preview_sd_max").innerHTML = document.getElementById("stat_max_sd").innerHTML;
		}
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function updateEVTotal() {
	 hp = Number(document.getElementById("ev_hp").value)
	 at = Number(document.getElementById("ev_at").value)
	 df = Number(document.getElementById("ev_df").value)
	 sp = Number(document.getElementById("ev_sp").value)
	 sa = Number(document.getElementById("ev_sa").value)
	 sd = Number(document.getElementById("ev_sd").value)

	ev_sum = (hp + at + df + sp + sa + sd)

	if (ev_sum > 510) {
		document.getElementById("ev_total").style.color="red";
		document.getElementById("ev_remain").innerHTML="<b>" + (ev_sum - 510) + " EVs</b> must be removed!"
		document.getElementById("ev_bar_outer").style.backgroundColor="white";
		document.getElementById("ev_bar_inner").style.backgroundColor="red";
		document.getElementById("ev_bar_inner").style.width = Math.min(Math.round((ev_sum - 510) * 0.294),150) + "px"
	} else if (ev_sum == 510) {
		document.getElementById("ev_total").style.color="black";
		document.getElementById("ev_remain").innerHTML="All EVs have been distributed!"
		document.getElementById("ev_bar_outer").style.backgroundColor="black";
		document.getElementById("ev_bar_inner").style.backgroundColor="white";
		document.getElementById("ev_bar_inner").style.width = "150px"
	} else {
		document.getElementById("ev_total").style.color="black";
		document.getElementById("ev_remain").innerHTML="<b>" + (510 - ev_sum) + " EVs</b> are left to distribute."
		document.getElementById("ev_bar_outer").style.backgroundColor="black";
		document.getElementById("ev_bar_inner").style.backgroundColor="white";
		document.getElementById("ev_bar_inner").style.width = Math.min(Math.round(ev_sum * 0.294),150) + "px"
	}
	document.getElementById("ev_total").innerHTML=ev_sum + " / 510"
	displayStat();

}

function updateEVStats(hp,at,df,sp,sa,sd) {

	document.getElementById("ev_hp").value = hp
	document.getElementById("ev_at").value = at
	document.getElementById("ev_df").value = df
	document.getElementById("ev_sp").value = sp
	document.getElementById("ev_sa").value = sa
	document.getElementById("ev_sd").value = sd

	updateEVTotal();

}	

function makeEVSet(setid) {

	switch(Number(setid)) {
		case 1:
			updateEVStats(6,252,0,252,0,0);
			break;
		case 2:
			updateEVStats(6,0,0,252,252,0);
			break;
		case 3:
			updateEVStats(252,0,252,0,0,6);
			break;
		case 4:
			updateEVStats(252,0,6,0,0,252);
			break;
		case 5:
			updateEVStats(252,0,129,0,0,129);
			break;
		case 6:
			updateEVStats(6,0,252,0,0,252);
			break;
		case 7:
			updateEVStats(85,85,85,85,85,85);
			break;
		case 0:
		default:
			break;
	}

}

function showEVTable() {
	ev_table = invertBool(ev_table)
	if (ev_table == 1) {
		document.getElementById("ev_config").style.display = "block";
		document.getElementById("stat_config1").style.display = "block";
	} else {
		document.getElementById("ev_config").style.display = "none";
		document.getElementById("stat_config1").style.display = "none";
		document.getElementById("use_stat").checked = '';
		stat_input = 0
	}
	displayStat();
}

function showStatInput() {
	if (nat == '0') {
		alert("You must select a Nature in order to use the direct stat input function!");
		return
	}

	stat_input = invertBool(stat_input)
	if (stat_input == 1) {

		refreshPreviewStats();
		inputStats();
		document.getElementById("stat_config2").style.display = "block";
	} else {
		document.getElementById("stat_config2").style.display = "none";
		displayStat();
	}
}

function inputStats() {

	s_hp = Number(document.getElementById("input_hp").value)
	s_at = Number(document.getElementById("input_at").value)
	s_df = Number(document.getElementById("input_df").value)
	s_sp = Number(document.getElementById("input_sp").value)
	s_sa = Number(document.getElementById("input_sa").value)
	s_sd = Number(document.getElementById("input_sd").value)
	displayStat();

}

function refreshPreviewStats() {

		document.getElementById("input_hp").value = document.getElementById("stat_min_hp").innerHTML
		document.getElementById("input_at").value = document.getElementById("stat_min_at").innerHTML
		document.getElementById("input_df").value = document.getElementById("stat_min_df").innerHTML
		document.getElementById("input_sp").value = document.getElementById("stat_min_sp").innerHTML
		document.getElementById("input_sa").value = document.getElementById("stat_min_sa").innerHTML
		document.getElementById("input_sd").value = document.getElementById("stat_min_sd").innerHTML
		s_hp = Number(document.getElementById("input_hp").value)
		s_at = Number(document.getElementById("input_at").value)
		s_df = Number(document.getElementById("input_df").value)
		s_sp = Number(document.getElementById("input_sp").value)
		s_sa = Number(document.getElementById("input_sa").value)
		s_sd = Number(document.getElementById("input_sd").value)

}

function helpHover(help,set_to) {
	if (set_to == 'on') {
		help.style.backgroundColor = '#FFFFDD';
	} else if (set_to == 'off') {
		help.style.backgroundColor = '#FFDDDD';
	}
}

function openHelp(topic) {
	ev_explain = " This can range from 0 to 255, although 252 is the most you will need. At Level 100, your Pokemon will gain +1 point in a stat for every 4 EVs it has in that stat (so up to 63 if it has 252 EVs).\n\nYou can click \"Fill\" to set the EV to 252 (or put the remaining amount inside), or you can click \"Clear\" to remove all EVs from the stat.\nYou can also click \"Clear All\" on the very bottom of the EVs to remove all EVs from the Pokemon.";
	iv_explain = " If you do not know how many EVs your Pokemon has and if it has participated in any battles, this number will be inaccurate.\n\nYou will need to click \"Update\" after making changes in order for the stat table to display them.";
	stat_start = "These two columns show the Pokemon's minimum and maximum (IV 0 to IV 31) stats of the Pokemon assuming it has ";
	stat_m = "\n\nYou will also see three numbers for each stat (except for HP):\n   The top-most number is if the Pokemon's Nature positively affects the stat (+10%).\n   The middle number is if the Pokemon's Nature does not affect the stat.\n   The bottom-most number is if the Pokemon's Nature negatively affects the stat (-10%).";
	switch(topic) {
		case 'help_pokemon':
			topic = "This is the Pokemon that you are currently viewing stats for. To change it, simply select another Pokemon from the drop down list and the stats will automatically update.";
			break;
		case 'help_level':
			topic = "This is the Level of the Pokemon whose stats you are calculating. This can range from 1 to 100.";
			break;
		case 'help_nature':
			topic = "The Nature of the Pokemon. It is sorted by which stat the Nature positively affects, and then lists all of the Natures that raise that particular stat, along with the stats that are affected negatively.\n\nIf a stat is affected positively, it will be at 110% of its usual amount and a \"+\" will be shown instead of an \"=\".\nIf a stat is affected negatively, it will be at 90% of its usual amount and a \"-\" will be shown instead of an \"=\" in the stat table.\n\nBy default, the stat table will show a wide range of stats, showing both positive and negative Natures.";
			break;
		case 'help_evs':
			topic = "If \"Use EVs for Calculating\" is checked, then you will be able to input the Effort Values (EVs) for your Pokemon.\n\nIf you do not know what EVs are, you should probably avoid using this feature, as it involves rather complex game mechanics used for competitive battling.";
			break;
		case 'help_evs_hp':
			topic = "The amount of EVs set in HP." + ev_explain;
			break;
		case 'help_evs_at':
			topic = "The amount of EVs set in Attack." + ev_explain;
			break;
		case 'help_evs_df':
			topic = "The amount of EVs set in Defense." + ev_explain;
			break;
		case 'help_evs_sp':
			topic = "The amount of EVs set in Speed." + ev_explain;
			break;
		case 'help_evs_sa':
			topic = "The amount of EVs set in Special Attack (Sp. Atk)." + ev_explain;
			break;
		case 'help_evs_sd':
			topic = "The amount of EVs set in Special Defense (Sp. Def)." + ev_explain;
			break;
		case 'help_evs_usage':
			topic = "The total amount of EVs you have allocated for this Pokemon. You can have a maximum of 510 EVs between all 6 of your Pokemon's stats.";
			break;
		case 'help_evs_remain':
			topic = "The number of EVs you have left to allocate. You are allowed to allocate 510 EVs in total.";
			break;
		case 'help_evs_preset':
			topic = "These are just some quick, default, commonly-used EVs spreads. You can select one of these to quickly see a fairly decent allocation of EVs depending on the selection you make.";
			break;
		case 'help_stat_analyzer':
			topic = "If you would like to know the Individual Values (IVs) of this Pokemon from your game, you can use this to do so. If you do not know about EVs or IVs, you should not use this feature.\n\nAll you need to do is enter the Nature of your Pokemon (required, and listed above), enter its EVs, if any (you will preferably want either none, or them logged) (if the Pokemon has done ANY fighting at all and you have not kept track of it, this calculator will NOT BE ACCURATE!!), and lastly enter each of its stats.\nThen, click Update and it will show you the minimum and maximum range that your Pokemon's IVs can be, ranging from 0 to 31. The closer the number gets to 31, the higher the stat is. At Level 100, the accuracy is usually 99% accurate; other levels will often have ranges, although you can get a good idea.\n\nYou can also use the Mini Stat Table to the side to see the Pokemon's Minimum and Maximum stats for the Level, Nature, and EV allocation.\n\nConfusing, yes, but it is very powerful.";
			break;
		case 'help_input_hp':
			topic = "The absolute stat shown on your game of the Pokemon's HP." + iv_explain;
			break;
		case 'help_input_at':
			topic = "The absolute stat shown on your game of the Pokemon's Attack." + iv_explain;
			break;
		case 'help_input_df':
			topic = "The absolute stat shown on your game of the Pokemon's Defense." + iv_explain;
			break;
		case 'help_input_sp':
			topic = "The absolute stat shown on your game of the Pokemon's Speed." + iv_explain;
			break;
		case 'help_input_sa':
			topic = "The absolute stat shown on your game of the Pokemon's Special Attack (Sp. Atk)." + iv_explain;
			break;
		case 'help_input_sd':
			topic = "The absolute stat shown on your game of the Pokemon's Special Defense (Sp. Def)." + iv_explain;
			break;
		case 'help_wild_stat_s':
			topic = stat_start + "0 Effort Values (EVs) for each of the stats.";
			break;
		case 'help_trained_stat_s':
			topic = stat_start + "252 Effort Values (EVs) for each of the stats.";
			break;
		case 'help_wild_stat_m':
			topic = stat_start + "0 Effort Values (EVs) for each of the stats." + stat_m;
			break;
		case 'help_trained_stat_m':
			topic = stat_start + "252 Effort Values (EVs) for each of the stats." + stat_m;
			break;
		case 'help_input_stats':
			topic = "These are the stats of the Pokemon you are calculating IVs for. They reflect the stats that are in the fields above (although you will need to hit the \"Update\" button in order to refresh them).";
			break;
		case 'help_input_ivs':
			topic = "These two columns show the Pokemon's Individual Value (IV) range. The left column shows the lowest possible IV your Pokemon can have with the provided Level, Stats, Nature, and EVs; the right column shows the highest possible IV for the same conditions.\n\nIVs can range between 0 and 31, with 31 being the best (giving an additional +31 to a stat at Level 100).\n\nIf an IV is 99% guarenteed to be a certain number, it will merge the two columns together.\n\nIf the stat says \"Invalid\", then it is impossible to have those stats with the specified criteria. This might mean that you have misentered the data, forgot to set the correct Nature or EVs, or might not have kept track of your Pokemon's EVs.\n\nAgain, this field is primarily for advanced players.";
			break;
		}
	alert(topic)
}