var chi = new Array();
chi[0] = "和";
chi[1] = "或";
chi[2] = "前后区的类型选择出现错误!";
chi[3] = "您选择的";
chi[4] = "号码不足,";
chi[5] = "数据分析范围,";
chi[6] = "请继续选择";
chi[7] = "根据大乐透历史 ";
chi[8] = "—";
chi[9] = "期";
chi[10] = "分析:";
chi[11] = "全按";
chi[12] = "跟随";
chi[13] = "数量";
chi[14] = "排序";
chi[15] = "号码";
chi[16] = "开出";
chi[17] = "同时";
chi[18] = "后";
chi[19] = "统计:";
chi[20] = "按照";
chi[21] = "出现";
chi[22] = "合计";
chi[23] = "第";
chi[24] = "看详情";
chi[25] = "的数据列表:";
chi[26] = "前区";
chi[27] = "后区";
for (var i = 0; i < chi.length; i++) { chi[i] = unescape(chi[i]); }

var current = h2b[0];
var f1 = document.getElementById("fm1");

function show_num() {
    bulid_ri();
}

function check_s1() {
    document.getElementById("result").style.display = "none";
    for (var i = 1; i < 36; i++) {
        var zeroi = add_zero(i);
        document.getElementById("chk_red" + zeroi).checked = false;
        document.getElementById("btn_red" + zeroi).className = "bigball grey";
    }
    for (var j = 1; j < 13; j++) {
        var zeroj = add_zero(j);
        document.getElementById("btn_blue" + zeroj).className = "bigball grey";
        document.getElementById("cir_blue" + zeroj).checked = false;
    }
    document.getElementById("nums").value = "";
}

function bulid_ri() {
    for (var i = 0; i < h2b.length; i++) {
        document.getElementById("r5").options[i] = new Option(h2b[i][2], i);
        document.getElementById("r6").options[i] = new Option(h2b[i][2], i);
    }
    document.getElementById("r5").options[i - 1].selected = true;
}

function bulid_ci(type) {
    var obj, innerHTML, limit;
    innerHTML = "";
    limit = (type == "red") ? "1:35" : "1:12";
    limit = limit.split(":");

    for (var i = limit[0]; i <= limit[1]; i++) {
        innerHTML += "<input type='button' class='bigball grey' id='btn_" + type + add_zero(i) + "' name='bt_" + type + "' value='" + add_zero(i) + "' onclick='check_c1(this);'>";
        innerHTML += "<input type='checkbox' name='c1_" + type + "' id='chk_" + type + add_zero(i) + "' value='" + add_zero(i) + "' style='display:none'>";
        switch (type) {
            case "0": if (i % 11 == 0) innerHTML += "<br/>"; break;
            case "1": if (i % 8 == 0) innerHTML += "<br/>"; break;
        }
    }
    obj = document.getElementById("sel_" + type);
    obj.innerHTML = innerHTML;
}

function bulid_ciradio(type) {
    var obj, innerHTML, limit;

    innerHTML = "";
    limit = (type == "0") ? "1:35" : "1:12"; limit = limit.split(":");

    for (var i = limit[0]; i <= limit[1]; i++) {
        innerHTML += "<input type='button' class='bigball grey' id='btn_" + type + add_zero(i) + "' name='bt_" + type + "' value='" + add_zero(i) + "' onclick='check_c1radio(this);'>";
        innerHTML += "<input type='radio' name='c1_" + type + "' id='cir_" + type + add_zero(i) + "' value='" + add_zero(i) + "' style='display:none' >";
        switch (type) {
            case "0": if (i % 11 == 0) innerHTML += "<br>"; break;
            case "1": if (i % 8 == 0) innerHTML += "<br>"; break;
        }
    }
    obj = document.getElementById("sel_" + type);
    obj.innerHTML = innerHTML;
}

function check_c1(obj) {
    var type = get_radio(document.getElementsByName("s1"));
    var num = document.getElementById("nums").value.split(",").length - 1;
    if (type == "red") {
        if (document.getElementById("chk_" + type + obj.value).checked == false) {
            obj.className = "bigball redball";
            document.getElementById("chk_" + type + obj.value).checked = true;
        } else {
            obj.className = "bigball grey";
            document.getElementById("chk_" + type + obj.value).checked = false;
        }
        if (document.getElementById("chk_" + type + obj.value).checked) {
            if (document.getElementById("nums").value.search(obj.value) < 0) document.getElementById("nums").value += obj.value + ",";
        } else {
            document.getElementById("nums").value = document.getElementById("nums").value.replace(obj.value + ",", "");
        }
    }
    if (document.getElementById("nums").value.length > 18) {
        alert("最多只能选择6个红球号!");
        obj.className = "bigball grey";
        document.getElementById("nums").value = document.getElementById("nums").value.replace(obj.value + ",", "");
        return false;
    }
}

function check_c1radio(obj) {
    var type = get_radio(document.getElementsByName("s1"));
    if (type == "blue") {
        if (document.getElementById("btn_" + type + obj.value).className != "bigball redball") {
            for (var i = 1; i < 13; i++) {
                var zeroi = add_zero(i);
                document.getElementById("btn_" + type + zeroi).className = "bigball grey";
            }
            if (document.getElementById("cir_" + type + obj.value).checked == false) {
                obj.className = "bigball redball";
                document.getElementById("cir_" + type + obj.value).checked = true;
                document.getElementById("nums").value = obj.value + ",";
            }
        }
    }
    //alert(document.getElementById("nums").value);        
}

function input_t1(obj) {
    var type = $("input[name='r1']:checked").val();
    var limit;
    limit = (type == "0") ? "1:35" : "1:12"; limit = limit.split(":");

    obj.value = obj.value.replace(/\D/gi, "");
    try {
        if (obj.value.length == 2) {
            if (Math.round(obj.value) >= limit[0] && Math.round(obj.value) <= limit[1]) {
                f1.start.focus();
            } else {
                obj.value = "";
            }
        }
    } catch (e) { }
}

function leave_t1() {
    var type = $("input[name='r1']:checked").val();
    var obj = eval("f1.t1_" + type);
    var num = "";

    for (var i = 0; i < obj.length; i++) {
        if (obj[i].value != "" && num.search(obj[i].value + ",") < 0) num += obj[i].value + ",";
    }
    f1.nums.value = num;
    //check_input();
}

function change_s2(str) {
    var obj = document.all.s3;
    for (var i = 0; i < obj.length; i++) { obj[i].innerHTML = (str == "or") ? chi[1] : chi[0]; }
}

function check_input() {
    var obj;
    var type = $("input[name='s1']:checked").val();
    var nums = f1.nums.value.split(",");

    obj = (type == "red") ? f1.c1_red : f1.c1_blue;
    alert(obj.length);
    for (var i = 0; i < obj.length; i++) {
        obj[i].checked = false;
        obj[i].nextSibling.className = "";
    }
    for (var i = 0; i < nums.length - 1; i++) {
        obj[Math.round(nums[i]) - 1].checked = true;
        obj[Math.round(nums[i]) - 1].nextSibling.className = "check_" + type;
    }

    obj = (type == "red") ? f1.t1_red : f1.t1_blue;
    switch (type) {
        case "red": for (var i = 0; i < obj.length; i++) { obj[i].value = ""; } break;
        case "blue": obj.value = ""; break;
    }
    switch (type) {
        case "red": for (var i = 0; i < nums.length - 1; i++) { obj[Math.round(i)].value = nums[i]; } break;
        case "blue": obj.value = nums[0]; break;
    }
}

function check_start() {
    var type = $("#c1").attr("class");
    if (type != "hide") {
        type = "0";
    } else {
        type = "1";
    }
    var ball = "";
    if (type == 0) {
        var items = $("input[name=" + "gs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    } else {
        var items = $("input[name=" + "hgs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    }
    var r1 = $("input[name='r1']:checked").val();
    var isstart = true;
    if (type != "0" && type != "1") { alert(chi[2]); isstart = false; }
    if (ball == "") { alert(chi[3] + chi[4] + chi[6]); isstart = false; }
    if (r1 == "") { alert(chi[3] + chi[5] + chi[6]); isstart = false; }

    if (isstart) {
        num_start();
    }
}

var result = new Array();
function format_result() {
    for (var i = 0; i <= 35; i++) {
        result[i] = new Array();
        for (var j = 0; j <= 35; j++) {
            result[i][j] = "";
        }
    }
}

function num_start() {
    format_result();
    document.getElementById("result").style.display = "none";

    var type = $("#c1").attr("class");
    if (type != "hide") {
        type = "0";
    } else {
        type = "1";
    }
    var ball = "";
    if (type == 0) {
        var items = $("input[name=" + "gs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    } else {
        var items = $("input[name=" + "hgs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    }

    var nums = ball.split(",").sort();
    var s2 = (type == "0") ? "or" : "and";
    switch (s2) {
        case "and": analy_start(type, 0, nums); break;
        case "or": for (var i = 0; i < nums.length; i++) { analy_start(type, i, ("," + nums[i]).split(",")); } break;
    }

    //iframe.location.href = "tools_count.asp?act=follow";
    detail = "";
    sort_start();
    show_result();
}

function analy_start(type, id, nums) {
    var isTrue = true;
    var num_red;
    var r1 = $("input[name='r1']:checked").val();
    var h2b_limit;

    switch (r1) {
        case "1": h2b_limit = h2b.length - 1 + ":" + 0; break;
        case "2": h2b_limit = 100 + ":" + 0; break;
        case "3": var r2 = get_select('r2'); var r3 = get_select('r3'); h2b_limit = r2 + ":" + r3; break;
    }
    h2b_limit = h2b_limit.split(":");
    h2b_limit[0] = Math.round(h2b_limit[0]);
    h2b_limit[1] = Math.round(h2b_limit[1]);

    for (var i = h2b_limit[0]; i > h2b_limit[1]; i--) {
        switch (type) {
            case "0":
                isTrue = balance_start(nums, h2b[i][0]);
                if (isTrue) {
                    num_red = h2b[i - 1][0].split(",");
                    for (var j = 0; j < num_red.length; j++) {
                        result[id][Math.round(num_red[j])] += i + ",";
                    }
                }
                break;
            case "1":
                isTrue = balance_start(nums, h2b[i][1]);
                if (isTrue) {
                    num_red = h2b[i - 1][1].split(",");
                    for (var j = 0; j < num_red.length; j++) {
                        result[id][Math.round(num_red[j])] += i + ",";
                    }
                }
                break;
        }
    }
}

function balance_start(u_nums, h_nums) {
    var isTrue = false;
    for (var i = 0; i < u_nums.length; i++) {
        if (Math.round(u_nums[i]) > 0) {
            if (h_nums.search(u_nums[i]) < 0) {
                isTrue = false;
                break;
            } else {
                isTrue = true;
            }
        }
    }
    return isTrue;
}

function sort_start() {
    var type = $("#c1").attr("class");
    if (type != "hide") {
        type = "0";
    } else {
        type = "1";
    }
    var limit = (type == "0") ? "1:35" : "1:12"; limit = limit.split(":");

    for (var i = 0; i < result.length; i++) {
        for (var j = limit[0]; j <= limit[1]; j++) {
            result[i][0] += add_zero(result[i][j].split(",").length - 1) + "," + add_zero(j) + "||";
        }
        result[i][0] = result[i][0].split("||").sort();
        for (var j = 1; j < result[i][0].length; j++) {
            result[i][0][j] = result[i][0][j].split(",");
            result[i][0][j] = Math.round(result[i][0][j][1]);
        }
        result[i][0] = result[i][0].join(",");
    }
}

function show_result() {
    var type = $("#c1").attr("class");
    if (type != "hide") {
        type = "0";
    } else {
        type = "1";
    }
    var typelimit = (type == "0") ? "1:35" : "1:12"; typelimit = typelimit.split(":");
    var qiu = (type == "0") ? chi[26] : chi[27];
    var s2 = (type == "0") ? "or" : "and";
    var ball = "";
    if (type == 0) {
        var items = $("input[name=" + "gs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    } else {
        var items = $("input[name=" + "hgs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    }

    var nums = ball.split(",").sort();
    var limit = (s2 == "or") ? "1:" + (nums.length) : "1:0"; limit = limit.split(":");

    var r1 = $("input[name='r1']:checked").val();
    var h2b_limit;
    switch (r1) {
        case "1": h2b_limit = h2b[h2b.length - 1][2] + ":" + h2b[0][2]; break;
        case "2": if (h2b.length - 1 > 100) h2b_limit = h2b[99][2] + ":" + h2b[0][2];
            else h2b_limit = h2b[h2b.length - 1][2] + ":" + h2b[0][2];
            break
        case "3": var r2 = get_select('r2'); var r3 = get_select('r3'); h2b_limit = h2b[r2][2] + ":" + h2b[r3][2]; break;
    }
    h2b_limit = h2b_limit.split(":");

    var innerHTML = "";
    innerHTML += "<div class=\"snavbox\">";
    innerHTML += "<div class=\"snav\">";
    if (type != "0") {
        innerHTML += "<a class=\"active\">跟随";
        for (var i = 0; i < nums.length; i++) {
            if (i == 0) {
                innerHTML += nums[i];
            } else {
                innerHTML += "+" + nums[i];
            }
        }
        innerHTML += "</a>";
    }
    else {
        for (var i = 0; i < nums.length; i++) {
            if (i == 0) {
                innerHTML += "<a class=\"active\">跟随" + nums[i] + "</a>";
            } else {
                innerHTML += "<a class=\"\">跟随" + nums[i] + "</a>";
            }
        }
     }
    innerHTML += "</div><div class=\"button\"><a class=\"left\"></a><a class=\"right\"></a></div></div>";
    for (var i = limit[0] - 1; i <= limit[1]; i++) {
        show_concrete(i, "id");
    }
    detail += "</div>";
    innerHTML += detail;
    $("#result").html(innerHTML);
    $("#result").show();
    $(".snav").find("a").click(function () {
        $(".snav").find("a").attr('class', '');
        $(".num-result").find("ul").attr('class', 'hide');
        $(".swiper").find("section").attr('class', 'hide');
        $(this).attr('class', 'active');
        $(".swiper").find("section").eq($(this).index()).attr('class', '');
        $(".num-result").find("ul").eq($(this).index()).attr('class', '');
    });

    $(".snav").find("a").click(function () {
        $(".snav").find("a").attr('class', '');
        $(".ssq-gs").find("ul").attr('class', 'hide');
        $(this).attr('class', 'active');
        $(".snav").offset({ left: "4rem" });
        $(".ssq-gs").find("ul").eq($(this).index()).attr('class', '');
    });
    $('.hidden').hide();
    $(".ssq-gs").find("li").click(function () {
        $(this).next().siblings("li").find("a").attr('class', '');
        $(this).find("dl").slideDown();
        $(this).find("a").attr('class', 'blue');
        $(this).siblings("li").find("dl").slideUp();

    });
    location.hash = "#href_result";
}
function show_type(i, type) {
    show_concrete(i, type);
    var content = detail;
    var n = "list" + i;
    $("ul[name='" + n + "']").html(content);
}
function show_alltype(b, e, type) {
    if (b == '0' && e == '0') {
        show_type(0, type);
    } else {
        for (var i = b; i <= e; i++) {
            show_type(i, type);
        }
    }
}
var detail = "";
function show_concrete(h, type) {
    var ctype = $("#c1").attr("class");
    if (ctype != "hide") {
        ctype = "0";
    } else {
        ctype = "1";
    }
    var limit = "";
    var ball = "";
    if (ctype == 0) {
        var items = $("input[name=" + "gs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    } else {
        var items = $("input[name=" + "hgs" + "]");
        for (var i = 0; i < items.length; i++) {
            if ($(items[i]).prop("checked"))
                ball = ball + $(items[i]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    }

    var nums = ball.split(",").sort();

    switch (ctype) {
        case "0": for (var j = 35; j >= 1; j--) { limit += "," + j; } break;
        case "1": for (var j = 12; j >= 1; j--) { limit += "," + j; } break;
    }
    if (type == "num") limit = result[h][0];
    limit = limit.split(",");
    if (h == 0) {
        detail += "<div class=\"ssq-gs\"><ul>"
    } else {
        detail += "<ul class=\"hide\">"
    }
    for (var j = limit.length - 1; j > 0; j--) {
        detail += "<li>";
        detail += add_zero(limit[j]);
        switch (ctype) {
            case "1":
                detail += (ctype == "1") ? " " + chi[12] + " " : " " + chi[12] + chi[17] + chi[21]
                detail += nums.join("+").substr(0, nums.join("+").length);
                break;
            default:
                detail += " " + chi[12] + " " + nums[h];
                break;
        }
        detail += chi[22] + "<span>";
        detail += (result[h][limit[j]].split(",").length - 1);
        detail += "</span>" + chi[9];
        detail += "<a class=\"bold\">" + chi[24] + "</a>";
        show_resultlist(h + 1, limit.length - j)
        detail += info;
        detail += "</li>";
    }
    detail += "</ul>";
}
var info;
function show_resultlist(i, j) {
    info = "";
    var type = $("#c1").attr("class");
    if (type != "hide") {
        type = "0";
    } else {
        type = "1";
    }
    var s2 = (type == "0") ? "or" : "and";
    var ball = "";
    if (type == 0) {
        var items = $("input[name=" + "gs" + "]");
        for (var y = 0; y < items.length; y++) {
            if ($(items[y]).prop("checked"))
                ball = ball + $(items[y]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    } else {
        var items = $("input[name=" + "hgs" + "]");
        for (var y = 0; y < items.length; y++) {
            if ($(items[y]).prop("checked"))
                ball = ball + $(items[y]).val().replace("\n", "").trim() + ",";
        }
        ball = ball.substr(0, ball.length - 1);
    }

    var nums = ball.split(",").sort();
    var list = result[i-1][j].split(",");
    var temp_nums1, temp_num2;
    var temp_red, temp_blue, temp_orders;

    temp_nums1 = (i == 0) ? nums : ("," + nums[i-1]).split(",");
    temp_nums2 = add_zero(j);

    var innerHTML = "<dl>";
    for (var i = 0; i < list.length - 1; i++) {
        temp_orders = h2b[list[i]][2];
        temp_red = h2b[list[i]][0];
        temp_blue = h2b[list[i]][1];
        for (var j = 1; j < temp_nums1.length; j++) {
            switch (type) {
                case "0": temp_red = temp_red.replace(temp_nums1[j], "<b class=\"wred\">" + temp_nums1[j] + "</b>"); break;
                case "1": temp_blue = temp_blue.replace(temp_nums1[j], "<b class=\"wred\">" + temp_nums1[j] + "</b>"); break;
            }
        }
        innerHTML += "<dt>";
        innerHTML += chi[23] + " " + temp_orders + " " + chi[9] + " ";
        innerHTML += temp_red.split(",").join(" ") + " + " + temp_blue.split(",").join(" ");
        innerHTML += "</dt><dt>";

        temp_orders = h2b[list[i] - 1][2];
        temp_red = h2b[list[i] - 1][0];
        temp_blue = h2b[list[i] - 1][1];
        switch (type) {
            case "0": temp_red = temp_red.replace(temp_nums2, "<b class=\"wred\">" + temp_nums2 + "</b>"); break;
            case "1": temp_blue = temp_blue.replace(temp_nums2, "<b class=\"wblue\">" + temp_nums2 + "</b>"); break;
        }
        innerHTML += chi[23] + " " + temp_orders + " " + chi[9] + " ";
        innerHTML += temp_red.split(",").join(" ") + " + " + temp_blue.split(",").join(" ");
        innerHTML += "</dt>";
    }
    innerHTML += "</dl>";
    info = innerHTML;
}

function add_zero(vars) {
    var tempVars = new String(vars).split(",");
    for (var i = 0; i < tempVars.length; i++) {
        tempVars[i] = (Math.round(tempVars[i]) <= 9) ? "0" + Math.round(tempVars[i]) : tempVars[i];
    }
    return tempVars.join(",");
}

function get_select(obj) {
    var qi = $("select[name='" + obj + "']  option:selected").val();
    return qi;
}

function chinese_type(str) {
    switch (str) {
        case "0": return chi[26]; break;
        case "1": return chi[27]; break;
    }
}