function on_click_check(checkbox) {
    if (checkbox.checked) {
        checkbox.value = 1;
    } else {
        checkbox.value = 0;
    }

    page = 1;
    device_search();
}

function on_check_maker(checkbox) {
    update_makers_string();

    var index = checkbox.id.replace('maker', '').replace('check', '');
    if (checkbox.checked) {
        $("#series" + index + "_spacer").css("display", "");
        $("#series" + index).show();
    } else {
        $("[name='model']", "#series" + index).attr("checked", false);
        $("#series" + index + "_spacer").css("display", "none");
        $("#series" + index).hide();
    }

    page = 1;
    device_search();
}

function on_check_series(checkbox) {
    update_makers_string();

    page = 1;
    device_search();
}

function get_checks_conditions(check_name) {
    checks = "";
    $("input:checkbox:checked").each( function() {
        name = $(this).attr("name");
        if (name == check_name) {
            if (checks != "") {
                checks += "+"
            }
            id = $(this).attr("id");
            value = id.replace("check_", "");
            checks += value;
        }
    });

    return checks;
}

function get_makers_conditions() {
    var makers = new Array();
    var series = new Array();

    $("#makers :checked").each(function() {
        var val = $(this).val();
        var name = $(this).attr("name");

        if (name != "model") {
            if (val.indexOf(':') >= 0) {
                indexes = $(this).attr("id").replace('series', '').replace('check', '');
                index_array = indexes.split("-");
                maker_id = index_array[0];

                var vals = val.split(':');
                series_name = vals[1];

                if (series[maker_id] != undefined) {
                    series[maker_id] += "::" + vals[1];
                } else {
                    series[maker_id] = vals[1];
                }
            } else {
                maker_id = $(this).attr("id").replace('maker', '').replace('check', '');
                makers.push(maker_id);
            }
        }
    });

    var conditions = '';
    for (var i = 0; i < makers.length; i++) {
        if (i > 0) {
            conditions += "&&";
        }

        if (series[makers[i]] != undefined) {
            conditions += makers[i] + "::" + series[makers[i]];
        } else {
            conditions += makers[i];
        }
    }

    return conditions;
}

function get_models_conditions() {
    models = "";
    $("input:checkbox:checked").each( function() {
        name = $(this).attr("name");
        if (name == "model") {
            if (models != "") {
                models += "+"
            }
            value = $(this).attr("value");
            models += value;
        }
    });

    return models;
}

function update_makers_string() {
    var makers = new Array();
    var series = new Array();
    $("#makers :checked").each(function() {
        var val = $(this).val();
        var name = $(this).attr("name");

        if (name != "model") {
            if (val.indexOf(':') >= 0) {
                var vals = val.split(':');
                if (series[vals[0]] != undefined) {
                    series[vals[0]] += "・" + vals[1];
                } else {
                    series[vals[0]] = vals[1];
                }
            } else {
                makers.push($(this).val());
            }
        }
    });

    var makers_string = '';
    for (var i = 0; i < makers.length; i++) {
        if (i > 0) {
            makers_string += "・";
        }
        makers_string += makers[i];

        if (series[makers[i]] != undefined) {
            makers_string += "(" + series[makers[i]] + ")";
        }
    }

    if (makers_string == '') {
        makers_string = 'すべてのメーカー';
    }
    $("#makers_string").html(makers_string);
}

function switch_plusminus_img(img, id) {
    $("#" + id + "_open_close").empty();
    if (img == "plus.gif") {
        img = "minus.gif";
    } else {
        img = "plus.gif";
    }

    $("<img src='/img/common/content/" + img + "' alt='' onclick=\"$('#" + id + "_conditions').slideToggle(); switch_plusminus_img('" + img + "', '" + id + "'); return false;\">").appendTo("#" + id + "_open_close");
}

function comma(str) {
    var num = new String(str).replace(/,/g, "");
    while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));

    return num;
}

function select_device(button, id, type) {
    var tmp_device = new Object();
    device_name = $("#device").val();

    tmp_device.maker = device_map[id].maker;
    tmp_device.series = device_map[id].series;
    tmp_device.model = device_map[id].model;
    tmp_device.image = device_map[id].image;
    tmp_device.price = device_map[id].price;
    tmp_device.period_bill = device_map[id].period_bill;
    tmp_device.period_energy = device_map[id].period_energy;
    tmp_device.eco_point = device_map[id].eco_point;
    tmp_device.id = id;

    if (write_cookie(tmp_device, device_name, type)) {

        if (type == "search") {
            refresh_select();
        } else if (type == "mydevice") {
            refresh_mydevice_setup();
            refresh_mydevice_formydevice();
        }
    }
}

// 検討リスト欄から選択したとき
function select_device2(cookieNo) {
    device_name = $("#device").val();

    cookie_data = read_cookie_for_device(device_name);
    index = eval(cookieNo);

    if (cookie_data != null) {
        for (i = 0; i < cookie_data.length; i++) {
            if (cookie_data[i]["cookieNo"] == index) {
                selected_device = cookie_data[i];
                break;
            }
        }
    }
}

function refresh_mydevice_setup() {
    cookies = read_cookie();
    $("#mykaden_choice dt").each( function(i) {
        setup_flg = false;
        setup_text = "未設定";
        device = $(this).attr("id");
        
        for (key in cookies) {
            if (cookies[key]["type"] == "mydevice") {
                if (cookies[key]["device"] == device) {
                    setup_flg = true;
                    setup_text = cookies[key]["maker"] + "・" + cookies[key]["series"] + "・" + cookies[key]["model"];
                    break;
                }
            }
        }

        $("#" + device + "_setup").text(setup_text);
        if (setup_flg == true) {
            $("#" + device + "_dl").removeClass("mykaden_choice_in_grey");
        }
    });
}

function refresh_select() {
    var cookie_max = 4;

    $("#compare_product").empty();
    device_name = $("#device").val();
    tmp_cookie_data = read_cookie_for_device(device_name);
    if (tmp_cookie_data != null) {
        count = tmp_cookie_data.length;
        cookie_data = [];
        j = 0;

        for (i = 0; i < count; i++) {
            if (tmp_cookie_data[i]["type"] != "mydevice") {
                cookie_data[j] = tmp_cookie_data[i];
                j++;
            }
        }

        count = cookie_data.length;
        for (i = 0; i < cookie_max; i++) {
            var liEl = "<li><ul>";
            if (i <= count-1) {
                liEl += "<li><a href=\"#\" onclick=\"select_device2('" + cookie_data[i]["cookieNo"] + "');refresh_list();return false;\">";
                if (cookie_data[i]["image"] != null && cookie_data[i]["image"] != "null" && cookie_data[i]["image"] != "") {
                    liEl += "<img src='/img/data/" + device_name + "/" + cookie_data[i]["image"] + "' alt='' width='82' height='69' /></a></li>";
                } else {
                    liEl += "<img src='/img/no-image.gif' alt='' width='82' height='69' /></a></li>";
                }
                liEl += "<li>" + cookie_data[i]["maker"] + "<br>" + cookie_data[i]["series"] + "<br>" + cookie_data[i]["model"] + "</li>";
                if (cookie_data[i]["period_bill"] == 0) {
                    liEl += "<li>" + "データなし</td>";
                } else {
                    liEl += "<li>" + comma(cookie_data[i]["period_bill"]) + "円</li>";
                }
                liEl += "<li class=\"delete_bt\"><a href=\"#\" onclick=\"delete_cookie(" + cookie_data[i]["cookieNo"] + ", 'search'); refresh_list(); return false;\"><span class=\"none\">削除</span></a></li>";
            } else {
                liEl += "<li><img src=\"/img/common/content/no_select.gif\" alt=\"No Select\" width=\"82\" height=\"180\" /></li>";
            }
            liEl += "</ul></li>";
            $(liEl).appendTo("#compare_product");
        }
    }
}

function refresh_mydevice_forsearch(){
    $("#mykaden_noselect").empty();
    device_name = $("#device").val();
    cookie_data = read_cookie_for_device(device_name);

    var liEl = "";

    if (cookie_data != null) {
        count = cookie_data.length;

        for (i = 0; i < count; i++) {
            if (cookie_data[i]["type"] == "mydevice") {
                liEl += "<li><a href=\"#\" onclick=\"select_device2('" + cookie_data[i]["cookieNo"] + "');refresh_list();return false;\">";
                if (cookie_data[i]["image"] != null && cookie_data[i]["image"] != "null" && cookie_data[i]["image"] != "") {
                    liEl += "<img src='/img/data/" + device_name + "/" + cookie_data[i]["image"] + "' alt='' width='82' height='69' /></a></li>";
                } else {
                    liEl += "<img src='/img/no-image.gif' alt='' width='82' height='69' /></a></li>";
                }
                liEl += "<li>" + cookie_data[i]["maker"] + "<br>" + cookie_data[i]["series"] + "<br>" + cookie_data[i]["model"] + "</li>";
                if (cookie_data[i]["period_bill"] == 0) {
                    liEl += "<li>" + "データなし</td>";
                } else {
                    liEl += "<li>" + comma(cookie_data[i]["period_bill"]) + "円</li>";
                }
                liEl += "<li class=\"delete_bt\"><a href=\"#\" onclick=\"delete_cookie(" + cookie_data[i]["cookieNo"] + ", 'search');\"><span class=\"none\">削除</span></a></li>";
                liEl += "</ul></li>";
                $(liEl).appendTo("#mykaden_noselect");

                selected_device = cookie_data[i];

                return;
            }
        }
    }

    //liEl += "<p class=\"mykaden_noselect_txt\">My家電が未設定です。</p>";
    liEl += "<li><ul><li><p class=\"mykaden_noselect_bt\"><a href=\"/searches/mydevice/" + device_name + "\"><span class=\"none\">設定画面へ</span></a></p></li></ul></li>";
    //liEl += "<li><ul><li><img src=\"/img/common/navi/search_btn.png\" alt=\"No Select\" width=\"82\" height=\"180\"></li></ul></li>";
    $(liEl).appendTo("#mykaden_noselect");

    return;
}

function refresh_mydevice_formydevice() {
    $("#mydevice").empty();
    device_name = $("#device").val();
    cookie_data = read_cookie_for_device(device_name);

    var trEl = "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" class=\"stripeTable my\">";

    if (cookie_data != null) {
        count = cookie_data.length;

        for (i = 0; i < count; i++) {
            if (cookie_data[i]["type"] == "mydevice") {
                trEl += "<tr class=\"tbg01\">";
                trEl += "<th>製品</th><th>年間電気代</th><th>年間消費電力</th></tr>";
                trEl += "<tr class=\"tbg02\"><td><table class=\"table_select_my\"><tr>";
                trEl += "<th style=\"width:84px;\"><p class=\"delete_bt_c\"><a href=\"#\" onclick=\"delete_cookie(" + cookie_data[i]["cookieNo"] + ", 'mydevice'); refresh_list_formydevice(); return false;\"><span class=\"none\">削除</span></a></p></th>";
                if (cookie_data[i]["image"] != null && cookie_data[i]["image"] != "null" && cookie_data[i]["image"] != "") {
                    trEl += "<th><img src='/img/data/" + device_name + "/" + cookie_data[i]["image"] + "' alt='' width='' height='69' /></th>";
                } else {
                    trEl += "<th><img src='/img/no-image.gif' alt='' width='82' height='69' /></th>";
                }
                trEl += "<th style=\"width:240px; text-align:left; padding-left:10px;\">" + cookie_data[i]["maker"] + "<br>" + cookie_data[i]["series"] + "<br>" + cookie_data[i]["model"] + "</th>";
                trEl += "</tr></table></td>";
                if (cookie_data[i]["period_bill"] == 0) {
                    trEl += "<td class=\"no-break\">" + "データなし</td>";
                } else {
                    trEl += "<td class=\"no-break\">" + comma(cookie_data[i]["period_bill"]) + "円</td>";
                }
                if (cookie_data[i]["period_energy"] == 0) {
                    trEl += "<td class=\"no-break\">" + "データなし</td>";
                } else {
                    trEl += "<td class=\"no-break\">" + cookie_data[i]["period_energy"] + "kWh</td></tr></table>";
                }
                $(trEl).appendTo("#mydevice");

                return;
            }
        }
    }

    trEl += "<tr class=\"tbg02\"><td><img src=\"/img/common/content/no_select_wh2.gif\" alt=\"No Select\" /></td></tr></table>";
    $(trEl).appendTo("#mydevice");
}

function thickbox(link) {

    h_size = Math.floor(getBrowserHeight() * 0.8);
    if (link.href) {
        link.href = link.href + "&height=" + h_size;
    }

    var t = link.title || link.name || null;
    var a = link.href || link.alt;
    var g = link.rel || false;
    tb_show(t,a,g);
    link.blur();
    return false;
}

function refresh_list() {
    $("#devices").empty();
    device_map = new Array();
    if (device_list == null) {
        return;
    }
    var device_name = $("#device").val();
    var shop = $("input[name='shop_select']:checked").attr('id');
    var charge = $("input[name='charge_select']:checked").attr('id');
    var id_array = get_cookie_id(device_name);
    var mydevice_data = new Array();
    var check_picture = $("#check_picture").attr("checked");


    $(".pagination").empty();
    if (device_list.total <= 0) {
        $("<span style=\"color: #666; font-weight: bold;\" class=\"txt12\">条件に合う家電が見つかりませんでした。</span>").appendTo("#pagination");
        $("#data_comment").css("display", "none");
        return;
    } else {
        var start = (device_list.page - 1) * device_list.pagesize + 1;
        var end = start + device_list.data.length - 1;
        $("<span style=\"color: #666; font-weight: bold;\" class=\"txt16\">" + device_list.total + "</span><span style=\"color: #666; font-weight: bold;\" class=\"txt12\">件中" + start + "~" + end + "件を表示しています。</span>").appendTo(".pagination");

        // **ページング**

        elPaging = "<div class=\"table_navi\">";
        if (page > 1) {
            elPaging += "<a href=\"#\" onclick=\"page--; device_search(); return false;\" style=\"font-weight: bold;\" class=\"txt12\">前へ</a>";
        } else {
            elPaging += "<span style=\"font-weight: bold;\" class=\"txt12\">前へ</span>";
        }
        elPaging += "<span> | </span>";

        for (var i = 1; i <= Math.ceil(device_list.total / 10); i++) {
            if (Math.abs(page - i) > 4) {
                continue;
            }

            if (page != i) {
                elPaging += "<a href=\"#\" onclick=\"page = " + i + "; device_search(); return false;\" class=\"txt12\" style=\"margin-left: 5px; margin-right: 5px;\">" + i + "</a>";
            } else {
                elPaging += "<span style=\"color: #666; font-weight: bold;\" class=\"txt16\">" + i + "</span>";
            }
        }

        elPaging += "<span> | </span>";
        if (device_list.total > end) {
            elPaging += "<a href=\"#\" onclick=\"page++; device_search(); return false;\" class=\"txt12\" style=\"font-weight: bold;\">次へ</a>";
        } else {
            elPaging += "<span class=\"txt12\" style=\"font-weight: bold;\">次へ</span>";
        }

        $(elPaging).appendTo(".pagination");
        $("#data_comment").css("display", "");
    }

    var tableEl = $("<table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" class=\"stripeTable\">").appendTo("#devices");
    var trEl = "<tr class=\"tbg01\">";
    trEl += "<th>選択</th>";
    trEl += "<th>製品</th>";

    sort_flg = return_sort_flg();

    if (shop != "ecost_plus") {
        price_sort = shop;
    } else {
        price_sort = 'price';
    }

    sort_img = "";
    if (sort_flg[price_sort] == true) {
        order_pic = return_sort_order(price_sort);
        sort_img = "<img src='/img/common/navi/" + order_pic + "' alt='' width='16' height='13' />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th><th><a href=\"#\" onclick=\"on_sort(price_sort);return false;\">本体<br>標準価格<a></th></tr></table></th>";

    sort_img = "";
    if (sort_flg['period_bill'] == true) {
        order_pic = return_sort_order('period_bill');
        sort_img = "<img src='/img/common/navi/" + order_pic + "' alt='' width='16' height='13' />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th><th><a href=\"#\" onclick=\"on_sort('period_bill');return false;\">年間<br>電気代<a></th></tr></table></th>";

    sort_img = "";
    if (sort_flg['period_energy'] == true) {
        order_pic = return_sort_order('period_energy');
        sort_img = "<img src='/img/common/navi/" + order_pic + "' alt='' width='16' height='13' />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th><th><a href=\"#\" onclick=\"on_sort('period_energy');return false;\">年間消費<br>電力量<a></th></tr></table></th>";

    sort_img = "";
    if (sort_flg['eco_point'] == true) {
        order_pic = return_sort_order('eco_point');
        sort_img = "<img src='/img/common/navi/" + order_pic + "' alt='' width='16' height='13' />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th></tr></table></th>";

    trEl += "</tr>";
    $(trEl).appendTo(tableEl);

    for (var i = 0; i < device_list.data.length; i++) {
        var device = device_list.data[i];
        device_map[device.id] = device;

        var exist_flg = false;
        for (var j = 0; j < id_array.length; j++) {
            if (id_array[j] == device.id) {
                exist_flg = true;
                break;
            }
        }

        if (exist_flg == true) {
            class_name = "tbg_select";
        } else {
            if (i % 2 == 0) {
                class_name = "tbg02";
            } else {
                class_name = "tbg03";
            }
        }

        trEl = $("<tr id=\"device" + device.id + "\" class=" + class_name + " />").appendTo(tableEl);
        if (exist_flg == false) {
            $("<td><p class=\"table_choice_bt\"><a href=\"#\" onclick=\"$('#device" + device.id + "').effect('transfer', { to: '#compare_product' }, '400');select_device(this, " + device.id + ", 'search');refresh_list();return false;\"><span class=\"none\">選択</span></a></p></td>").appendTo(trEl);
        } else {
            $("<td><p class=\"table_choice_bt_select\"><span class=\"none\">選択</span></p></td>").appendTo(trEl);
        }

        if (CountLength(device.maker) >= 14) {
            maker_name = "<small>" + device.maker + "</small>";
        } else {
            maker_name = device.maker;
        }

        tdEl = "<td><ul id=\"table_select\">";
        if (check_picture) {
            link_url = "";
            if (device.image != null && device.image != "") {
                img_tag = "<img src='/img/data/" + device_name + "/" + device.image + "' alt='' width='82' height='69' />";
            } else {
                img_tag = "<img src='/img/no-image.gif' alt='' width='82' height='69' />";
            }
            tdEl += "<li><a href='/searches/detail/" + device_name + "/" + device.id + "?keepThis=1&TB_iframe=1&width=610' onclick='return thickbox(this);' title='" + device.model + "' >" + img_tag + "</a></li>";
        }
        device_string = maker_name + "<br>" + device.series + "<br>" + device.model;
        device_string = "<a href='/searches/detail/" + device_name + "/" + device.id + "?keepThis=1&TB_iframe=1&width=610' onclick='return thickbox(this);' title='" + device.model + "' >" + device_string + "</a>";
        tdEl += "<li><table><th><td class='table_product_name'>" + device_string + "</a></th></tr></table></li>";
        tdEl += "</ul></td>";
        $(tdEl).appendTo(trEl);

        if (shop != "ecost_plus") {
            price = device[shop];
            url = device[shop + "_url"];
        } else {
            price = device.price;
        }
        //selected_price = selected_device.price;

        var average_charge = $("#average_charge").val();
        var bill = device.period_bill;

        if (charge == "mydevice_diff") {
            bill_diff = bill - selected_device.period_bill;
        } else {
            bill_diff = bill - average_charge;
        }

        if (device.eco_point > 0) {
            eco_point = device.eco_point;
        } else {
            eco_point = "対象外";
        }

        td_head = "<td class=\"no-break\">";
        tdEl = td_head;

        if (price != null && price >= 0) {
            if (price == 0 || price == null ) {
                if (shop == "ecost_plus") {
                    tdEl += "オープン価格</td>";
                } else {
                    tdEl += "価格情報なし</td>";
                }
            } else {
                if (shop != "ecost_plus") {
                    tdEl += "<a href='" + url + "' target='_blank'>" + comma(price) + "円</a></td>";
                } else {
                    tdEl += comma(price) + "円</td>";
                }
                //tdEl += "<br/><span class=\"red\">" + ((price - selected_price > 0) ? "+" : "") + comma(price - selected_price) + "円</span></td>";
            }
        } else {
            tdEl += "価格情報なし</td>";
        }
        $(tdEl).appendTo(trEl);

        if ((selected_device.id >= 0) && (selected_device.id != device.id)) {
            if (bill > 0) {
                tdEl = td_head + comma(bill) + "円<br/><span class=\"red\">" + ((bill_diff > 0) ? "+" : "") + comma(bill_diff) + "円</span></td>";
            } else {
                tdEl = td_head + "データなし</td>";
            }
            $(tdEl).appendTo(trEl);

            if (device.period_energy > 0) {
                tdEl = td_head + comma(device.period_energy) + " kWh<br/><span class=\"red\">" + ((device.period_energy - selected_device.period_energy > 0) ? "+" : "") + comma(device.period_energy - selected_device.period_energy) + " kWh</span></td>";
            } else {
                tdEl = td_head + "データなし</td>";
            }
            $(tdEl).appendTo(trEl);

/*
            if (eco_point != "対象外") {
                if (selected_device.eco_point > 0) {
                    tdEl = td_head + comma(eco_point) + "<br/><span class=\"red\">" + ((eco_point - selected_device.eco_point > 0) ? "+" : "") + comma(eco_point - selected_device.eco_point) + "</span></td>";
                } else {
                    tdEl = td_head + comma(eco_point) + "<br/><span class=\"red\">-</span></td>";
                }
            } else {
                tdEl = td_head + eco_point + "</td>";
            }
            $(tdEl).appendTo(trEl);
*/
        } else {
            if (bill > 0) {
                if (charge == "average_diff") {
                    tdEl = td_head + comma(bill) + "円<br/><span class=\"red\">" + ((bill_diff > 0) ? "+" : "") + comma(bill_diff) + "円</span></td>";
                } else {
                    tdEl = td_head + comma(bill) + "円</td>";
                }
            } else {
                tdEl = td_head + "データなし</td>";
            }
            $(tdEl).appendTo(trEl);

            if (device.period_energy > 0) {
                tdEl = td_head + comma(device.period_energy) + "kWh</td>";
            } else {
                tdEl = td_head + "データなし</td>";
            }
            $(tdEl).appendTo(trEl);

/*
            if (eco_point != "対象外") {
                tdEl = td_head + comma(eco_point) + "</td>";
            } else {
                tdEl = td_head + eco_point + "</td>";
            }
            $(tdEl).appendTo(trEl);
*/
        }
    }

    $("</table>").appendTo("#devices");
}

function refresh_list_formydevice() {
    $("#devices").empty();
    device_map = new Array();
    if (device_list == null) {
        return;
    }
    var device_name = $("#device").val();
    var id_array = get_cookie_id(device_name);

    $(".pagination").empty();
    if (device_list.total <= 0) {
        $("<span style=\"color: #666; font-weight: bold;\" class=\"txt12\">条件に合う家電が見つかりませんでした。</span>").appendTo("#pagination");
        $("#data_comment").css("display", "none");
        return;
    } else {
        var start = (device_list.page - 1) * device_list.pagesize + 1;
        var end = start + device_list.data.length - 1;
        $("<span style=\"color: #666; font-weight: bold;\" class=\"txt16\">" + device_list.total + "</span><span style=\"color: #666; font-weight: bold;\" class=\"txt12\">件中" + start + "~" + end + "件を表示しています。</span>").appendTo(".pagination");

        // **ページング**

        elPaging = "<div class=\"table_navi\">";
        if (page > 1) {
            elPaging += "<a href=\"#\" onclick=\"page--; device_search(); return false;\" style=\"font-weight: bold;\" class=\"txt12\">前へ</a>";
        } else {
            elPaging += "<span style=\"font-weight: bold;\" class=\"txt12\">前へ</span>";
        }
        elPaging += "<span> | </span>";

        for (var i = 1; i <= Math.ceil(device_list.total / 10); i++) {
            if (Math.abs(page - i) > 4) {
                continue;
            }

            if (page != i) {
                elPaging += "<a href=\"#\" onclick=\"page = " + i + "; device_search(); return false;\" class=\"txt12\" style=\"margin-left: 5px; margin-right: 5px;\">" + i + "</a>";
            } else {
                elPaging += "<span style=\"color: #666; font-weight: bold;\" class=\"txt16\">" + i + "</span>";
            }
        }

        elPaging += "<span> | </span>";
        if (device_list.total > end) {
            elPaging += "<a href=\"#\" onclick=\"page++; device_search(); return false;\" class=\"txt12\" style=\"font-weight: bold;\">次へ</a>";
        } else {
            elPaging += "<span class=\"txt12\" style=\"font-weight: bold;\">次へ</span>";
        }

        $(elPaging).appendTo(".pagination");
        $("#data_comment").css("display", "");
    }

    var tableEl = $("<table border=\"0\" cellpadding=\"10\" cellspacing=\"0\" class=\"stripeTable\">").appendTo("#devices");
    var trEl = "<tr class=\"tbg01\">";
    trEl += "<th>選択</th>";
    trEl += "<th>製品</th>";

    sort_flg = return_sort_flg();

    sort_img = "";
    if (sort_flg['price'] == true) {
        order_pic = return_sort_order('price');
        sort_img = "<img src=\"/img/common/navi/" + order_pic + "\" alt=\"\" width=\"16\" height=\"13\" />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th><th><a href=\"#\" onclick=\"on_sort('price');return false;\">本体<br>標準価格<a></th></tr></table></th>";

    sort_img = "";
    if (sort_flg['period_bill'] == true) {
        order_pic = return_sort_order('period_bill');
        sort_img = "<img src=\"/img/common/navi/" + order_pic + "\" alt=\"\" width=\"16\" height=\"13\" />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th><th><a href=\"#\" onclick=\"on_sort('period_bill');return false;\">年間<br>電気代<a></th></tr></table></th>";

    sort_img = "";
    if (sort_flg['period_energy'] == true) {
        order_pic = return_sort_order('period_energy');
        sort_img = "<img src=\"/img/common/navi/" + order_pic + "\" alt=\"\" width=\"16\" height=\"13\" />";
    }
    trEl += "<th><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><th>" + sort_img + "</th><th><a href=\"#\" onclick=\"on_sort('period_energy');return false;\">年間<br>消費電力<a></th></tr></table></th>";

    trEl += "</tr>";
    $(trEl).appendTo(tableEl);

    for (var i = 0; i < device_list.data.length; i++) {
        var device = device_list.data[i];
        device_map[device.id] = device;

        var exist_flg = false;
        for (var j = 0; j < id_array.length; j++) {
            if (id_array[j] == device.id) {
                exist_flg = true;
                break;
            }
        }

        if (exist_flg == true) {
            class_name = "tbg_select";
        } else {
            if (i % 2 == 0) {
                class_name = "tbg02";
            } else {
                class_name = "tbg03";
            }
        }

        trEl = $("<tr id=\"device" + device.id + "\" class=" + class_name + " />").appendTo(tableEl);
        var tdEl = "";
        if (exist_flg == true) {
            tdEl = "<td><p class=\"table_choice_bt_select\"><span class=\"none\">選択</span></p></td>";
        } else {
            tdEl = "<td><p class=\"table_choice_bt\"><a href=\"#\" onclick=\"$('#device" + device.id + "').effect('transfer', { to: '#mydevice' }, 400);select_device(this, " + device.id + ", 'mydevice');refresh_list_formydevice();return false;\"><span class=\"none\">選択</span></a></p></td>";
        }
        $(tdEl).appendTo(trEl);

        if (CountLength(device.maker) >= 14) {
            maker = "<small>" + device.maker + "</small>";
        } else {
            maker = device.maker;
        }

        link_url = "";
        if (device.link_url != "") {
            link_url = device.link_url;
        }

        tdEl = "<td><ul id=\"table_select\">";

        if (device.image != null && device.image != "") {
            img_tag = "<img src='/img/data/" + device_name + "/" + device.image + "' alt='' width='82' height='69' />";
        } else {
            img_tag = "<img src='/img/no-image.gif' alt='' width='82' height='69' />";
        }

        if (device.link_url != "") {
            tdEl += "<li><a href='/searches/detail/" + device_name + "/" + device.id + "?keepThis=1&TB_iframe=1&width=610' onclick='return thickbox(this);' title='" + device.model + "' >" + img_tag + "</a></li>";
        } else {
            tdEl += "<li>" + img_tag + "</li>";
        }
        device_string = maker + "<br>" + device.series + "<br>" + device.model;
        device_string = "<a href='/searches/detail/" + device_name + "/" + device.id + "?keepThis=1&TB_iframe=1&width=610' onclick='return thickbox(this);' title='" + device.model + "' >" + device_string + "</a>";

        tdEl += "<li><table><th><td class='table_product_name'>" + device_string + "</a></th></tr></table></li>";
        tdEl += "</ul></td>";
        $(tdEl).appendTo(trEl);

        td_head = "<td class=\"no-break\">";
        tdEl = "";
        if (device.price == 0 || device.price == null ) {
            tdEl = td_head + "オープン価格</td>";
        } else {
            tdEl = td_head + comma(device.price) + "円</td>";
        }
        $(tdEl).appendTo(trEl);

        tdEl = "";
        if (device.period_bill == 0 || device.period_bill == null ) {
            tdEl = td_head +  "データなし</td>";
        } else {
            tdEl = td_head + comma(device.period_bill) + "円</td>";
        }
        $(tdEl).appendTo(trEl);

        tdEl = "";
        if (device.period_energy == 0 || device.period_energy  == null) {
            tdEl = td_head +  "データなし</td>";
        } else {
            tdEl = td_head + device.period_energy + "kWh</td>";
        }
        $(tdEl).appendTo(trEl);
    }

    $("</table>").appendTo("#devices");
}

function CLocationSearch(sSource){

    /*　初期化処理　*/
    var sAry = new Array(); /*　アイテム配列を追加 */
    {
        var nCur = 0, nRet = 0;
        var sSearch = sSource.substr(1);
        var sName, sValue;

        nRet = sSearch.indexOf('=', nCur) // 最初の'='を検索
        while (nRet >= 0) {
            // '='以前が変数名
            sName = sSearch.substring(nCur, nRet);
            nCur = nRet + 1;

            // 値を取り出すメソッド
            nRet = sSearch.indexOf('&', nCur); // 変数の区切りを検索
            if (nRet == -1)
                nRet = sSearch.length + 1; // なければ最後まで
            sValue = sSearch.substring(nCur, nRet);

            // 配列に格納
            sAry[sName] = sValue;

            // 次を検索
            nCur = nRet + 1;
            nRet = sSearch.indexOf('=', nCur);
        }
    }

    /* 値 */
    this.valueOf = function(){
        return sSource;
    }

    /* 値を取り出すメソッド */
    this.item = function(sVarname){
        try {
            return sAry[sVarname]; //あれば返す
        }
        catch (e) {
            return ''; //なければ空文字を返す
        }
    }

    /* 要素数 */
    this.length = function() {
        return sAry.length;
    }

    /* toString */
    this.toString = function() {
        return this.length + '';
    }
}

function CountLength(str) {
    var r = 0;
    for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i);
        // Shift_JIS: 0x0 ～ 0x80, 0xa0 , 0xa1 ～ 0xdf , 0xfd ～ 0xff
        // Unicode : 0x0 ～ 0x80, 0xf8f0, 0xff61 ～ 0xff9f, 0xf8f1 ～ 0xf8f3
        if ( (c >= 0x0 && c < 0x81) || (c == 0xf8f0) || (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4)) {
            r += 1;
        } else {
            r += 2;
        }
    }
    return r;
}

function getBrowserHeight() {
        if ( window.innerHeight ) {
                return window.innerHeight;
        }
        else if ( document.documentElement && document.documentElement.clientHeight != 0 ) {
                return document.documentElement.clientHeight;
        }
        else if ( document.body ) {
                return document.body.clientHeight;
        }
        return 300;
}
  

