/**
 * Global stat variables. I know this is bad practice, but whatever.
 */
var hpBase, atkBase, defBase, spAtkBase, spDefBase, spdBase;
var pkmnName;

/**
 * Set base stats onpageload
 */
window.onload = function() {
  getPokemonId();
  getStats();
  
  levelChange();
}

//given a nature, return whether or not it is +/-/1 for the given stat
function getNatureValue(nature, stat){
    if ( (nature == "hardy") || (nature == "docile") || (nature == "serious") ||  (nature == "bashful") ||  (nature == "quirky") ){
        //neutral natures, woot
        return 1;
    }
    else if (nature == "lonely") {
        //+atk, -def
        if (stat == "atk"){
            return 1.1;
        }
        else if (stat == "def"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "brave") {
        //+atk, -spd
        if (stat == "atk"){
            return 1.1;
        }
        else if (stat == "spd"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "adamant") {
        //+atk, -spAtk
        if (stat == "atk"){
            return 1.1;
        }
        else if (stat == "spAtk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "naughty") {
        //+atk, -spDef
        if (stat == "atk"){
            return 1.1;
        }
        else if (stat == "spDef"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "bold") {
        //+def, -atk
        if (stat == "def"){
            return 1.1;
        }
        else if (stat == "atk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "relaxed") {
        //+def, -spd
        if (stat == "def"){
            return 1.1;
        }
        else if (stat == "spd"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "impish") {
        //+def, -spAtk
        if (stat == "def"){
            return 1.1;
        }
        else if (stat == "spAtk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "lax") {
        //+def, -spDef
        if (stat == "def"){
            return 1.1;
        }
        else if (stat == "spDef"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "timid") {
        //+spd, -atk
        if (stat == "spd"){
            return 1.1;
        }
        else if (stat == "atk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "hasty") {
        //+spd, -def
        if (stat == "spd"){
            return 1.1;
        }
        else if (stat == "def"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "jolly") {
        //+spd, -spAtk
        if (stat == "spd"){
            return 1.1;
        }
        else if (stat == "spAtk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "naive") {
        //+spd, -spDef
        if (stat == "spd"){
            return 1.1;
        }
        else if (stat == "spDef"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "modest") {
        //+spAtk, -atk
        if (stat == "spAtk"){
            return 1.1;
        }
        else if (stat == "atk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "mild") {
        //+spAtk, -def
        if (stat == "spAtk"){
            return 1.1;
        }
        else if (stat == "def"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "quiet") {
        //+spAtk, -spd
        if (stat == "spAtk"){
            return 1.1;
        }
        else if (stat == "spd"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "rash") {
        //+spAtk, -spDef
        if (stat == "spAtk"){
            return 1.1;
        }
        else if (stat == "spDef"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "calm") {
        //+spDef, -atk
        if (stat == "spDef"){
            return 1.1;
        }
        else if (stat == "atk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "gentle") {
        //+spDef, -def
        if (stat == "spDef"){
            return 1.1;
        }
        else if (stat == "def"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "sassy") {
        //+spDef, -spd
        if (stat == "spDef"){
            return 1.1;
        }
        else if (stat == "spd"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else if (nature == "careful") {
        //+spDef, -spAtk
        if (stat == "spDef"){
            return 1.1;
        }
        else if (stat == "spAtk"){
            return 0.9;
        }
        else {
            return 1;
        }
    }
    else {
        return 5;
    }
}

function natureChange(){
    //recalculate everything, new nature.
    calculateHP();
    calculateAttack();
    calculateDefense();
    calculateSpAttack();
    calculateSpDefense();
    calculateSpeed();
}

function levelChange(){
    //relcalculate everything, new level.
    calculateHP();
    calculateAttack();
    calculateDefense();
    calculateSpAttack();
    calculateSpDefense();
    calculateSpeed();
}
    
//this is where we're going to attempt to calculate stat stuff... D:
function calculateHP(){
    var iv = new Number(document.hp.hpIV.value);
    
    var base = hpBase;
    base = 2 * base;
    
    var ev = document.hp.hpEV.value;
    ev = ev/4;
        
    var final = iv + base + ev;
    final += 100;
    
    var lvl = document.prereqs.level.value;
    
    final = final * lvl;
    final = final / 100;
    final += 10;
        
    final = Math.floor(final);
        
    //set to stat
    document.hp.hpStat.value = final;
}
    
//attack
function calculateAttack(){
    var iv = new Number(document.atk.atkIV.value);
    
    var base = atkBase;
    base = 2 * base;
    
    var ev = new Number(document.atk.atkEV.value);
    ev = ev/4;
        
    var final = iv + base + ev;
        
    var lvl = new Number(document.prereqs.level.value);
        
    final = final * lvl;
    final = final / 100;
    final += 5;
        
    final = Math.floor(final);
    
    //nature value
    n = getNatureValue(document.prereqs.nature.options[document.prereqs.nature.selectedIndex].value, "atk");
        
    final = n * final;
    final = Math.floor(final);
        
    //set to stat
    document.atk.atkStat.value = final;
}
    
//def
function calculateDefense(){
    var iv = new Number(document.def.defIV.value);
        
    var base = defBase;
    base = 2 * base;
        
    var ev = new Number(document.def.defEV.value);
    ev = ev/4;
        
    var final = iv + base + ev;
        
    var lvl = new Number(document.prereqs.level.value);
        
    final = final * lvl;
    final = final / 100;
    final += 5;
        
    final = Math.floor(final);
        
    //nature value
    n = getNatureValue(document.prereqs.nature.options[document.prereqs.nature.selectedIndex].value, "def");
        
    final = n * final;
    final = Math.floor(final);
        
    //set to stat
    document.def.defStat.value = final;
}

//spd
function calculateSpeed(){
    var iv = new Number(document.spd.spdIV.value);
        
    var base = spdBase;
    base = 2 * base;
        
    var ev = new Number(document.spd.spdEV.value);
    ev = ev/4;
        
    var final = iv + base + ev;
        
    var lvl = new Number(document.prereqs.level.value);
        
    final = final * lvl;
    final = final / 100;
    final += 5;
        
    final = Math.floor(final);
        
    //nature value
    n = getNatureValue(document.prereqs.nature.options[document.prereqs.nature.selectedIndex].value, "spd");
        
    final = n * final;
    final = Math.floor(final);
        
    //set to stat
    document.spd.spdStat.value = final;
}

//spAtk
function calculateSpAttack(){
    var iv = new Number(document.spAtk.spAtkIV.value);
        
    var base = spAtkBase;
    base = 2 * base;
        
    var ev = new Number(document.spAtk.spAtkEV.value);
    ev = ev/4;
        
    var final = iv + base + ev;
        
    var lvl = new Number(document.prereqs.level.value);
        
    final = final * lvl;
    final = final / 100;
    final += 5;
        
    final = Math.floor(final);
        
    //nature value
    n = getNatureValue(document.prereqs.nature.options[document.prereqs.nature.selectedIndex].value, "spAtk");
        
    final = n * final;
    final = Math.floor(final);
        
    //set to stat
    document.spAtk.spAtkStat.value = final;
}

//spDef
function calculateSpDefense(){
    var iv = new Number(document.spDef.spDefIV.value);
        
    var base = spDefBase;
    base = 2 * base;
        
    var ev = new Number(document.spDef.spDefEV.value);
    ev = ev/4;
        
    var final = iv + base + ev;
        
    var lvl = new Number(document.prereqs.level.value);
        
    final = final * lvl;
    final = final / 100;
    final += 5;
        
    final = Math.floor(final);
        
    //nature value
    n = getNatureValue(document.prereqs.nature.options[document.prereqs.nature.selectedIndex].value, "spDef");
        
    final = n * final;
    final = Math.floor(final);
        
    //set to stat
    document.spDef.spDefStat.value = final;
}

//ajax function to get base stats
function getStats(){    
    var xmlhttp, curStatIdx;
    
    if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    //firefox?
    if (navigator.appCodeName == "Mozilla"){
        asynchronous = false;
    }
    else {
        asynchronous = true;
    }
    
    xmlhttp.onreadystatechange=function(){        
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            getStatsCallback(curStatIdx, xmlhttp);
        }
    }
    
    for (curStatIdx = 1; curStatIdx < 7; ++curStatIdx){
        xmlhttp.open("GET", "getStat.php?pkmnName=" + pkmnName + "&statId=" + curStatIdx, asynchronous);
        xmlhttp.send();
        
        if(!asynchronous){
            getStatsCallback(curStatIdx, xmlhttp);
        }
    }
}

function getStatsCallback(curStatIdx, xmlhttp){
    if (curStatIdx == 1){
        hpBase = xmlhttp.responseText;
    }
    else if (curStatIdx == 2){
        atkBase = xmlhttp.responseText;
    }
    else if (curStatIdx == 3){
        defBase = xmlhttp.responseText;
    }
    else if (curStatIdx == 4){
        spAtkBase = xmlhttp.responseText;
    }
    else if (curStatIdx == 5){
        spDefBase = xmlhttp.responseText;
    }
    else {
        spdBase = xmlhttp.responseText;
    }
}

function getPokemonId(){
    pkmnName = document.getElementById('pokemonName').innerHTML;
}
