if(document.getElementsByName("keywords")[0].content.indexOf("player")>-1){
    main();
}

function main(){
    var tables = document.getElementsByTagName("table");
    var abilities = {"-":0};
    var skills = {};
    var stats = {};
    var features = {};
    var masteries = [];
    var EXP = {};

    for(i=0;i<tables.length;i++){
        var header_names = "";
        var header_row  =  tables[i].children[0].children[0];
        //Iterate over the thead -> tr -> th elements
        for(k=0;k<header_row.children.length;k++){
            header_names += header_row.children[k].innerHTML.trim();
        }

        if(checkheaders(header_names,["Ability","Score","Modifier"])==3){
            abilities.DOM = tables[i];
        } else if(checkheaders(header_names,["Ability","Skill","Bonus","EXP"])==4){
            skills.DOM = tables[i];
        } else if(checkheaders(header_names,["Stat","Value"])==2){
            stats.DOM = tables[i];
        } else if(checkheaders(header_names,["Feature","EXP","Description"])==3){
            features.DOM = tables[i];
        } else if(checkheaders(header_names,["Mastery","EXP","Requirements","Type","Description"])==5){
            masteries.push({"DOM":tables[i]});
        }
    }

    function checkheaders(string, keywords){
        var output = 0;
        for(j=0;j<keywords.length;j++){
            if(string.indexOf(keywords[j])>-1){
                output += 1;
            }
        }
        return output;
    }

    function align(cell,how="center"){
        cell.className = cell.className.replace("leftalign","");
        cell.className = cell.className.replace("rightalign","");
        cell.className = cell.className.replace("centeralign","");
        cell.className += " "+how+"align";

    }

    for(i=0;i<abilities.DOM.children[1].children.length;i++){
        //Iterate over the rows of the abilities table
        var row = abilities.DOM.children[1].children[i];
        var ability = row.children[0].innerHTML.trim();
        abilities[ability] = parseInt(row.children[2].innerHTML.trim());
    }

    var row_modifier = "";
    for(i=0;i<skills.DOM.children[1].children.length;i++){
        //Iterate over rows of the skills table.
        var row = skills.DOM.children[1].children[i];
        var bonus;
        var exp;
        if(row.children[0].innerHTML.trim() in abilities){
            row_modifier = row.children[0].innerHTML.trim();
            skill = row.children[1];
            bonus = row.children[2];
            exp = row.children[3];
        } else {
            skill = row.children[0];
            bonus = row.children[1];
            exp = row.children[2];
        }

        if(isNaN(parseInt(exp.innerHTML.trim()))){
            exp.innerHTML = "0";
        }
        align(exp);
        align(bonus);

        EXP[skill.innerHTML.trim()] = parseInt(exp.innerHTML.trim());
        var skill_bonus = Math.floor(parseInt(exp.innerHTML.trim())/2) + abilities[row_modifier];
        bonus.innerHTML = "  "+skill_bonus+"  ";
        skills[skill.innerHTML.trim()] = skill_bonus;
    }

    for(i=0;i<stats.DOM.children[1].children.length;i++){
        var row = stats.DOM.children[1].children[i];
        var stat = row.children[0];
        var value = row.children[1];
        var exp = row.children[2];
        align(value);
        align(exp);

        stats[stat.innerHTML.trim()] = value;

        var exp_value = exp.innerHTML.trim();

        if(isNaN(exp_value)){
            if(exp_value=="-"){
                exp_value = 0;
            } else if(exp_value.indexOf("d")>-1) {
                stats["HD Improvement"] = parseInt(exp_value.substring(exp_value.indexOf("d")+1,exp_value.length));
                stats["HD Increase"] = parseInt(exp_value.substring(0,exp_value.indexOf("d")));
                exp_value = stats["HD Improvement"]+stats["HD Increase"];
            } else {
                exp_value = 0;
                exp.innerHTML = "0";
            }

        }
        EXP[stat.innerHTML.trim()] = exp_value;
    }

    for(i=0;i<features.DOM.children[1].children.length;i++){
        var row = features.DOM.children[1].children[i];
        var feature = row.children[0];
        var exp_str = row.children[1].innerHTML.trim();
        var exp = parseInt(exp_str.substring(0,exp_str.indexOf("/")));
        features[feature.innerHTML.trim()] = parseInt(exp_str.substring(exp_str.indexOf("/")+1,exp_str.length));
        EXP[feature.innerHTML.trim()] = exp;
    }

    for(j=0;j<masteries.length;j++){
        var mastery = masteries[j];
        for(i=0;i<mastery.DOM.children[1].children.length;i++){
            var row = mastery.DOM.children[1].children[i];
            var feature = row.children[0];
            var exp_str = row.children[1].innerHTML.trim();
            var exp = 0;
            if(exp_str.indexOf("/")>-1){
                exp = parseInt(exp_str.substring(0,exp_str.indexOf("/")));
            } else {
                exp = parseInt(exp_str);
            }
            EXP[feature.innerHTML.trim()] = exp;
        }
    }

    var HP = 1 + Math.floor((skills["Persistence"]+skills["Toughness"])/2);
    if(HP<1){
        HP = 1;
    }
    for(h=1;h<=EXP["HP"];h++){
        if(h%5==0 && abilities["CON"]>=1){
            HP += abilities["CON"];
        } else {
            HP += 1;
        }
    }
    stats["HP"].innerHTML = "  "+HP+"  ";

    if(!("HD Increase" in stats) && !("HD Improvement" in stats)){
        stats["HD Increase"] = 0;
        stats["HD Improvement"] = 0;

    }
    var hd = [Math.floor(stats["HD Increase"]/2)+1];
    hd.push(4+2*Math.floor(stats["HD Improvement"]/2));
    stats["Hit Dice"].innerHTML = hd[0]+"d"+hd[1];


    var AC = 8 + Math.ceil(skills["Toughness"]/3) + Math.ceil(skills["Evasion"]/3);
    stats["AC"].innerHTML = "  "+AC+"  ";

    var exp_total = [0,parseInt(stats["EXP Total"].innerHTML.trim())];
    for(key in EXP){
        var temp_exp = parseInt(EXP[key]);
        if(!isNaN(temp_exp)){
            exp_total[0] += temp_exp;
        }
    }
    stats["EXP Total"].innerHTML = (exp_total[1]-exp_total[0])+"/"+exp_total[1];

}
