function updateFlag() {
    var user_flag = parseInt($("#input_flag").val());
    $("#input_flagopp").val(user_flag == 1 ? 6 : 1)
}

function updateHeroes(mode) {
    var user_castle = parseInt($("#input_choose_castle"+mode).val());
    
    $("#input_random_castle" + mode).attr("disabled", true);
    $("#input_choose_hero" + mode).attr("disabled", true);
    $("#input_random_hero" + mode).attr("disabled", true);
    
    if (user_castle > 0) {
        //Ustawiam drugi zamek tak samo jak pierwszy
        $("#input_random_castle" + mode).val(user_castle);
        //wypełniam selecta bohatera 1 wszystkimi bohaterami i losowym gdy zamek jest losowy
        //lub  bohaterami z konkretnego zamku i losowym gdy zamek nie jest losowy
        $("#input_choose_hero" + mode).load("ajax/modules/report_index/getheroes.php", {id_castle : user_castle}, function() {
            $("#input_random_castle" + mode).attr("disabled", false);
            $("#input_choose_hero" + mode).attr("disabled", false);
            $("#input_random_hero" + mode).attr("disabled", false);
        });
    }
    else {
        $("#input_random_castle" + mode).val(0);
        $("#input_choose_hero" + mode).val(0);
        $("#input_random_hero" + mode).val(0);
    }
}

function updateSecondHero(value, mode) {
    if (value > 0) {
        $("#input_random_hero" + mode).val(value).attr("disabled", false);
    }
    else {
        $("#input_random_hero" + mode).val(0).attr("disabled", true);
    }
}


/**
 * @param {Object} type
 * Possible values
 *     choose
 *     random
 * 
 * 
 * @param {Object} user
 * Possible values
 *     1 - opponent
 *     0 - user
 */
function getCastle(type, user) {
    $("#pretty_choosing").html('');
    
    $("#loader").ajaxStart(function() {
       $(this).show();
    });
    
    $("#loader").ajaxStop(function() {
       $(this).hide();
    });

    $("#pretty_choosing").load("ajax/modules/report_index/getcastle.php", {
        type : type,
        user : user
    });
    
    $("#pretty_choosing").toggle(400);
    
    /*$("#pretty_choosing").mousedown(function() {
        $("#pretty_choosing").hide(400);
    });*/
}

function setCastle(type, user, id_castle, castle_name) {
    id_castle = parseInt(id_castle);
    
    //Ustaw obrazek wybranego zamku
    $("#report_" + type + "_castle" + (user == 1 ? 'opp' : '')).attr("src", "images/h3/castles/" + castle_name + "_random.jpg");
    //Ustaw id wybranego zamku
    $("#input_" + type + "_castle" + (user == 1 ? 'opp' : '')).val(id_castle);
    
    //Zresetuj bohatera
    $("#report_" + type + "_hero" + (user == 1 ? 'opp' : '')).attr("src", id_castle == 11 ? "images/h3/random/random_heroes.jpg" : "images/h3/random/random_hero.jpg");
    $("#input_" + type + "_hero" + (user == 1 ? 'opp' : '')).val(id_castle == 11 ? 164 : 0);
    
    if (type == "choose") {
        //Resetuj
        $("#report_random_castle" + (user == 1 ? 'opp' : '')).attr("src", "images/h3/random/random_castle.jpg");
        $("#report_random_hero" + (user == 1 ? 'opp' : '')).attr("src", "images/h3/random/random_hero.jpg");
        $("#input_random_castle" + (user == 1 ? 'opp' : '')).val(0);
        $("#input_random_hero" + (user == 1 ? 'opp' : '')).val(0);
        
        
        
        //Jeśli wybrano zamek random
        //$("#report_random_castle" + (user == 1 ? 'opp' : '')).css("visibility", id_castle == 11 ? "visible" : "hidden");
        //$("#report_random_hero" + (user == 1 ? 'opp' : '')).css("visibility", id_castle == 11 ? "visible" : "hidden");
        
        //$("#report_random_title" + (user == 1 ? 'opp' : '')).css("visibility", id_castle == 11 ? "visible" : "hidden");
    }

    $("#pretty_choosing").hide(400);
}
/**
 * 
 * @param {Object} type
 * @param {Object} user
 * @param {Object} all   czy wyświetlić wszystkich bohaterów
 */
function getHero(type, user, all) {
    var id_castle = parseInt($("#input_" + type + "_castle" + (user == 1 ? 'opp' : '')).val());
    
    if(id_castle == 0)
        return alert(lang['error_please_choose_castle']);
    
    $("#pretty_choosing").html('');
    
    $("#loader").ajaxStart(function() {
       $(this).show();
    });
    
    $("#loader").ajaxStop(function() {
       $(this).hide();
    });

    $("#pretty_choosing").load("ajax/modules/report_index/getheroes.php", {
        type      : type,
        user      : user,
        id_castle : id_castle,
        all       : all
    });
    
    $("#pretty_choosing").toggle(400);
    
    /*$("#pretty_choosing").mousedown(function() {
        $("#pretty_choosing").hide(400);
    });*/
}

function setHero(type, user, id_hero, hero_name, castle_name) {
    $("#report_" + type + "_hero" + (user == 1 ? 'opp' : '')).attr("src", "images/h3/heroes/small/" + castle_name + "/" + hero_name + ".jpg");
    $("#input_" + type + "_hero" + (user == 1 ? 'opp' : '')).val(id_hero);
    $("#pretty_choosing").hide(400);
}

function getFlag(user) {
    $("#pretty_choosing").html('');
    
    $("#loader").ajaxStart(function() {
       $(this).show();
    });
    
    $("#loader").ajaxStop(function() {
       $(this).hide();
    });

    $("#pretty_choosing").load("ajax/modules/report_index/getflag.php", {
        user : user
    });
    
    $("#pretty_choosing").toggle(400);
    
    $("#pretty_choosing").mousedown(function() {
        $("#pretty_choosing").hide(400);
    });
}

function setFlag(user, id_flag, flag_name) {
    $("#report_flag" + (user == 1 ? 'opp' : '')).attr("src", "images/h3/flags/" + flag_name + ".jpg");
    $("#input_flag" + (user == 1 ? 'opp' : '')).val(id_flag);
}

function checkAvatar() {
    $("#loader").ajaxStart(function() {
       $(this).show();
    });
    $("#loader").ajaxStop(function() {
       $(this).hide();
    });
    
    $("#report_login_opp").attr("disabled", true);
    $("#report_mode").attr("disabled", true);

    $.get("ajax/modules/report_index/getavatar.php", {id : $("#report_login_opp").val()}, 
        function (data) {
            $("#avatar_useropp").attr("src", data);
            $("#report_login_opp").attr("disabled", false);
            $("#report_mode").attr("disabled", false);
        }
    );
}

function setMaps() {
    $("#report_mode").attr("disabled", true);
    $("#report_maps").attr("disabled", true);
    $("#report_login_opp").attr("disabled", true);
    
    $("#report_maps").load("ajax/modules/report_index/getmaps.php", {
        id : $("#report_mode").val()
    }, function() {
        $("#report_mode").attr("disabled", false);
        $("#report_maps").attr("disabled", false);
        $("#report_login_opp").attr("disabled", false);
    });
}

var selHonor = 4;
function setHonor(number) {
    if (selHonor == number)
        return;
        
    $("#honor" + selHonor).attr("class", "played_with_honour");
    $("#honor" + number).attr("class", "played_with_honour pwh_selected");

    $("#input_honor").val(number);

    selHonor = number;
}

function checkReportForm() {
    var inputs = [
        $("#input_choose_castle").val(),
        $("#input_choose_hero").val(),
        $("#input_flag").val(),
        $("#input_random_castle").val(),
        $("#input_random_hero").val(),
        
        $("#input_choose_castleopp").val(),
        $("#input_choose_heroopp").val(),
        $("#input_flagopp").val(),
        $("#input_random_castleopp").val(),
        $("#input_random_heroopp").val(),
        
        $("#report_login_opp").val(),
        $("#report_win_loss").val(),
        $("#report_mode").val(),
        $("#report_maps").val()
    ];
    
    var inputs_len = inputs.length;

    for (var i = 0; i < inputs_len; i++) {
        if (inputs[i] == 0) {
            alert(lang['fill_all_firlds']);
            return;
        }
    }

    document.formReport.submit();
}

