// 変数宣言
var mall_tbana = new Array();
var inqmsg = document.getElementById('inqmsg');
var dispid = document.getElementById('dispdata');
var libufcnt = 13;
var ainqkey = '';
var inqamt1 = '';
var inqamt2 = '';
var tmoutid = 0;
var inqkeys = new Array();
var keymh = new Array();

// 商品Xmn通信オブジェクト
function ItemXmn(typenm, cgi) {
    this.typenm = typenm;
    this.cgi = cgi;
    this.iteminqf = 1;
    this.abortf = 0;
    this.dspf = 0;
    this.ary = undefined;
    this.cnt = 0;
    this.xmlhttp = new createXmlHttpRequest();
    this.start = function() {
        if (this.xmlhttp) {
            var xmn = this;
            this.xmlhttp.onreadystatechange = function() {
                if (xmn.xmlhttp && !xmn.abortf &&
                    xmn.xmlhttp.readyState == 4 &&
                    xmn.xmlhttp.status == 200)
                {
                    var i;
                    // タイムアウト解除
                    if (tmoutid) {
                        clearTimeout(tmoutid);
                        tmoutid = 0;
                    }
                    xmn.iteminqf = 0;
                    xmn.ary = xmn.xmlhttp.responseText.split("\n");
                    xmn.cnt = xmn.ary.length - 1; // 暫定件数
                    info_disp(xmn.typenm+'商品データ'+xmn.cnt+'件(暫定)読込完了！！！');
                    set_libuf(xmn); // 検索データ表示バッファ設定
                    bufsetf = 1;    // 未表示バッファあり
                    if (inq_endchk()) {   // 終了チェック関数呼び出し
                        // 再度タイムアウト設定
                        tmoutid = setTimeout("inq_timeout()", 1*1000);
                    }
                }
            }
            //if (kanridirf) info_disp('検索PHP='+this.cgi);
            this.xmlhttp.open("GET", this.cgi, true);
            this.xmlhttp.send(null);
        }
    }
    this.abort = function() {
        if (this.xmlhttp) {
            this.xmlhttp.onreadystatechange = function() {}
            this.xmlhttp.abort();
            this.iteminqf = 0;
            this.abortf = 1;
            info_disp(this.typenm+'検索中断しました！！！');
        }
    }
}

// 検索開始処理
function inq_start(key1, key2, key3) {
    //window.alert('inq_start() Start!');
    //kanridirf = 0;
    msgnoclrf = 0; // 1:メッセージを残して表示
    //dispid.innerHTML = '';   // 表示テーブルクリア
    // 共通変数初期化
    mhstr = new RegExp(">([^<]*)</a>", "i");
    libuforg = new Array();
    libuf = new Array();
    lineno = new Array();
    itemary = new Array();
    itemcntorg = 0;
    itemcnt = 0;
    curpg = 0;
    maxpg = 0;
    xmnary = new Array();
    xmncnt = 0;
    bufsetf = 0;
    inqtmout = 90; // 検索タイムアウト(秒単位)
    tmoutcnt = 0;
    errdspf = 0;
    if (key1.length) inqkeys = inqkeys.concat(key1.split(" "));
    if (key2.length) inqkeys = inqkeys.concat(key2.split());
    if (key3.length) inqkeys = inqkeys.concat(key3.split());
    //alert('key ='+key1+','+key2+','+key3);
    //alert('inqkeys len='+inqkeys.length);
    for (i=0;i<inqkeys.length;i++) {
        keymh[i] = new RegExp(z2h_ascii(inqkeys[i]), "i");
    }

    if (!kanridirf) inqmsg.innerHTML = '';
    //setlines = document.getElementById('setlines')
    saryprc = getdirpath()+'ajaxitem-search.php';
    amazonprc = getdirpath()+'ajamazoninq.cgi';
    biddersprc = getdirpath()+'ajbiddersinq.cgi';
    bidders2prc = getdirpath()+'ajbiddersinq2.cgi';
    rakutenprc = getdirpath()+'ajrakuteninq.cgi';
    yahooprc = getdirpath()+'ajyahooinq.cgi';
    livedoorprc = getdirpath()+'ajlivedoorinq.cgi';
    dennoprc = getdirpath()+'ajdennoinq.cgi';
    banasz = 'width="102"';       // バナー横サイズ
    shopselist = new Array();
    mallselist = new Array();
    try {
        dsplines = document.getElementById('displine').value;
    } catch(e) {
        dsplines = getCookie('csetlines');
        if (dsplines < 1 || isNaN(dsplines)) dsplines = 10;
    }
    shopsenf = '未選択';
    mallsenf = '未選択';

    // 検索条件設定
    var ikey1 = encodeURI(key1);
    var ikey2 = encodeURI(key2);
    var ikey3 = encodeURI(key3);
    var javaid = encodeURI(getjavaid());
    var imaxinq = encodeURI(maxinq);
    var addarg = '';
    if (ikey1.length > 0) addarg += "ikey1="+ikey1;
    if (ikey2.length > 0) addarg += "&ikey2="+ikey2;
    if (ikey3.length > 0) addarg += "&ikey3="+ikey3;
    addarg += "&imaxinq="+imaxinq;
    addarg += "&javaid="+javaid;

    // 検索指定フラグ取得
    inqlocalf = document.getElementById('inqlocal').checked;
    inqamazonf = document.getElementById('inqamazon').checked;
    inqbiddersf = document.getElementById('inqbidders').checked;
    inqrakutenf = document.getElementById('inqrakuten').checked;
    inqyahoof = document.getElementById('inqyahoo').checked;
    inqlivedoorf = document.getElementById('inqlivedoor').checked;
    inqdennof = document.getElementById('inqdenno').checked;
    if (!inqlocalf && !inqamazonf && !inqbiddersf && !inqrakutenf
      && !inqyahoof && !inqlivedoorf && !inqdennof)
    {
        alert('検索サイト未設定!!!');
        return;
    }

    // Xmn通信オブジェクト作成
    // ローカル
    if (inqlocalf) {
        var sarycgi = saryprc+"?"+addarg;
        xmnlocal = new ItemXmn('案内', sarycgi);
        xmnary[xmncnt] = xmnlocal;
        xmncnt++;
    }

    // アマゾン検索
    if (inqamazonf) {
        var amazoncgi = amazonprc+"?"+addarg;
        xmlamazon = new ItemXmn('Amazon', amazoncgi);
        xmnary[xmncnt] = xmlamazon;
        xmncnt++;
    }

    // ビッダーズ検索
    if (inqbiddersf) {
        var bidderscgi = biddersprc+"?"+addarg;
        xmlbidders = new ItemXmn('Bidders', bidderscgi);
        xmnary[xmncnt] = xmlbidders;
        xmncnt++;
    }

    // ビッダーズ２検索
    //if (inqbiddersf) {
    //    var bidders2cgi = bidders2prc+"?"+addarg;
    //    xmlbidders2 = new ItemXmn('Bidders2', bidders2cgi);
    //    xmnary[xmncnt] = xmlbidders2;
    //    xmncnt++;
    //}

    // 楽天検索
    if (inqrakutenf) {
        var rakutencgi = rakutenprc+"?"+addarg;
        xmlrakuten = new ItemXmn('Rakuten', rakutencgi);
        xmnary[xmncnt] = xmlrakuten;
        xmncnt++;
    }

    // Yahoo!ショッピング
    if (inqyahoof) {
        var yahoocgi = yahooprc+"?"+addarg;
        xmlyahoo = new ItemXmn('Yahoo', yahoocgi);
        xmnary[xmncnt] = xmlyahoo;
        xmncnt++;
    }

    // livedoorデパート
    if (inqlivedoorf) {
        var livedoorcgi = livedoorprc+"?"+addarg;
        xmllivedoor = new ItemXmn('livedoor', livedoorcgi);
        xmnary[xmncnt] = xmllivedoor;
        xmncnt++;
    }

    // 電脳卸(stmx)
    if (inqdennof) {
        var dennocgi = dennoprc+"?"+addarg;
        xmldenno = new ItemXmn('stmx', dennocgi);
        xmnary[xmncnt] = xmldenno;
        xmncnt++;
    }

    // 同時検索テスト用
    //var ikey21 = encodeURI('２０Ｑ');
    //var addarg2 = "&ikey1="+ikey21;
    //var sarycgi2 = saryprc+"?"+addarg2;
    //xmnlocal2 = new ItemXmn('案内２', sarycgi2);
    //xmnary[xmncnt] = xmnlocal2;
    //xmncnt++;

    // 同時検索テスト用
    //var ikey31 = encodeURI('ホームスター');
    //var addarg3 = "&ikey1="+ikey31;
    //var sarycgi3 = saryprc+"?"+addarg3;
    //xmnlocal3 = new ItemXmn('案内３', sarycgi3);
    //xmnary[xmncnt] = xmnlocal3;
    //xmncnt++;

    // 同時検索テスト用
    //var ikey41 = encodeURI('プラネタリウム');
    //var addarg4 = "&ikey1="+ikey41;
    //var sarycgi4 = saryprc+"?"+addarg4;
    //xmnlocal4 = new ItemXmn('案内４', sarycgi4);
    //xmnary[xmncnt] = xmnlocal4;
    //xmncnt++;

    // 同時検索テスト用
    //var ikey51 = encodeURI('リージョンフリー');
    //var addarg5 = "&ikey1="+ikey51;
    //var sarycgi5 = saryprc+"?"+addarg5;
    //xmnlocal5 = new ItemXmn('案内５', sarycgi5);
    //xmnary[xmncnt] = xmnlocal5;
    //xmncnt++;

    if (xmncnt < 1) {
        //disp_clr();
        info_disp('検索サイトが設定されてないので、検索できませんでした！！！');
    }

    // タイムアウト設定
    tmoutid = setTimeout("inq_timeout()", 1*1000);

    // 中断処理
    //window.onunload = function() {
    //    inq_abort();
    //}

    // 全検索開始(Xmn通信開始)
    xmnall_start();
}

// 全検索開始(Xmn通信開始)
function xmnall_start() {
    var i;
    var msg = '';
    for (i=0;i<xmnary.length;i++) {
        var xmn = xmnary[i];
        msg += xmn.typenm+':検索開始 '
    }
    info_disp(msg);
    for (i=0;i<xmnary.length;i++) {
        var xmn = xmnary[i];
        xmn.start();
    }
}

// 検索タイムアウト処理
function inq_timeout() {
    var i;
    tmoutcnt++;
    // 未タイムアウト
    if (tmoutcnt < inqtmout) {
        if (bufsetf) {
            //if (kanridirf) info_disp('暫定表示実行');
            inqset_libuf(); // 表示データ抽出
            disp_data();    // データ表示
            bufsetf = 0;    // 未表示バッファなし
        }
        // 再度タイムアウト設定
        tmoutid = setTimeout("inq_timeout()", 1*1000);
        return;
    }
    // タイムアウト処理
    for (i=0;i<xmnary.length;i++) {
        var xmn = xmnary[i];
        if (xmn.iteminqf) {
            info_disp(xmn.typenm+': タイムアウト発生！！！');
            xmn.abort();
        }
    }
    inqstat_dsp();  // 検索情報表示
    setall_libuf(); // データ表示バッファ設定
    disp_data();    // データ表示
}

// 検索強制終了処理
function inq_abort() {
    // 検索中断処理
    for (i=0;i<xmnary.length;i++) {
        var xmn = xmnary[i];
        if (xmn.iteminqf) {
            xmn.abort();
        }
    }
}

// 表示クリア
function disp_clr() {
    inqmsg.innerHTML = '';
    dispid.innerHTML = '';
}

// 検索終了チェック
function inq_endchk() {
    var i;
    var msg;
    for (i=0;i<xmnary.length;i++) {
        if (xmnary[i].iteminqf) {
            inqstat_dsp();  // 検索情報表示
            return 1; // 検索中あり
        }
    }
    //if (kanridirf) info_disp('確定表示実行');
    inqstat_dsp();  // 検索情報表示
    setall_libuf(); // データ表示バッファ設定
    disp_data();    // データ表示
    return 0;
}

// 検索情報表示
function inqstat_dsp() {
    var i;
    var msg = '';
    for (i=0;i<xmnary.length;i++) {
        if (xmnary[i].iteminqf) {
            msg += xmnary[i].typenm+':検索中 ';
            runf = 1;
        } else {
            if (xmnary[i].abortf) {
                msg += xmnary[i].typenm+':タイムアウト ';
            } else {
                msg += xmnary[i].typenm+':'+xmnary[i].cnt+'件 ';
            }
        }
    }
    info_disp(msg);
}

// 全検索データ表示バッファ設定
function setall_libuf() {
    var i;
    for (i=0;i<xmnary.length;i++) {
        set_libuf(xmnary[i]);
    }
    inqset_libuf(); // 表示データ抽出
}

/* 全角英数字を半角英数字に置換する */
function z2h_ascii(src) {
    var str = new String;
    var len = src.length;
    for (var i = 0; i < len; i++) {
        var c = src.charCodeAt(i);
        if (c >= 65281 && c <= 65374 && c != 65340) {
            str += String.fromCharCode(c - 65248);
        } else if (c == 8217) {
            str += String.fromCharCode(39);
        } else if (c == 8221) {
            str += String.fromCharCode(34);
        } else if (c == 12288) {
            str += String.fromCharCode(32);
        } else if (c == 65507) {
            str += String.fromCharCode(126);
        } else if (c == 65509) {
            str += String.fromCharCode(92);
        } else {
            str += src.charAt(i);
        } 
    }
    return str;
}

/* 全角英数字を半角英数字に置換する */
function z2h_word(src) {
  return src.replace(/([Ａ-Ｚａ-ｚ０-９＿])/g,
    function ($0) {
      return String.fromCharCode($0.charCodeAt(0) - 65248);
    });
}

/* 全角数字を半角数字に置換する */
function z2h_desi(src) {
  return src.replace(/([０-９])/g,
    function ($0) {
      return String.fromCharCode($0.charCodeAt(0) - 65248);
    });
}

/* 半角英数字を全角英数字に置換する */
function h2z_word(src) {
  return src.replace(/(\w)/g,
    function ($0) {
      return String.fromCharCode($0.charCodeAt(0) + 65248);
    });
}

// 表示データ抽出
function inqset_libuf() {
    var i, j, y;
    var sobj, mobj;
    // 店舗、モール一覧初期化
    //for (sobj in shopselist) {
    //    shopselist[sobj] = 0;
    //}
    //for (mobj in mallselist) {
    //    mallselist[mobj] = 0;
    //}
    shopselist = new Array();
    mallselist = new Array();

    // 全角英数字を半角英数字に置換する
    //xmlnkey = z2h_ascii(xmlnkey);
    //ainqkey = z2h_ascii(ainqkey);
    xmlnkey = z2h_word(xmlnkey);
    ainqkey = z2h_word(ainqkey);
    // 大文字変換
    xmlnkey = xmlnkey.toUpperCase();
    ainqkey = ainqkey.toUpperCase();
    // 全角ＳＰを半角SPに
    xmlnkey = xmlnkey.replace(/　/g, " ");
    ainqkey = ainqkey.replace(/　/g, " ");
    // 複数のＳＰを１つに
    xmlnkey = xmlnkey.replace(/ +/g, " ");
    ainqkey = ainqkey.replace(/ +/g, " ");
    // 単語毎に分割
    var nary = xmlnkey.split(" ");
    var aary = ainqkey.split(" ");
    //for (i=0;i<nary.length;i++) nary[i] = nary[i].toUpperCase();
    //for (i=0;i<aary.length;i++) aary[i] = aary[i].toUpperCase();

    // 半角数字
    inqamt1 = z2h_desi(inqamt1);
    inqamt2 = z2h_desi(inqamt2);

    //window.alert('nary[0]='+nary[0]+' cnt='+nary.length);
    //window.alert('aary[0]='+aary[0]+' cnt='+aary.length);
    //window.alert('inqamt1='+inqamt1+' inqamt2='+inqamt2);
    // 表示用バッファ設定
    libuf = new Array();
    for (i=0,y=0;i<libuforg.length;i++) {
        var nflg = 0;
        var aflg = 1;
        var itemstr = libuforg[i][10];    // 商品説明
        if (itemstr.length < 1) continue; // 空文字を省く
        for (j=0;j<nary.length;j++) {
            if (nary[j].length < 1) continue;  // 空文字を省く
            // 商品説明で除くチェック
            if (itemstr.search(nary[j]) >= 0) {
                nflg = 1;
                break;
            }
        }
        if (nflg) continue;
        for (j=0;j<aary.length;j++) {
            // 空文字を省く
            if (aary[j].length < 1) continue;  // 空文字を省く
            // 商品説明で含むチェック
            if (itemstr.search(aary[j]) < 0) {
                aflg = 0;
                break;
            }
        }
        if (!aflg) continue;

        // 金額チェック
        if (!isNaN(libuforg[i][2])) {
            if (!isNaN(inqamt1) && inqamt1.length > 0) {
                if (Number(inqamt1) > Number(libuforg[i][2])) continue;
            }
            if (!isNaN(inqamt2) && inqamt2.length > 0) {
                if (Number(inqamt2) < Number(libuforg[i][2])) continue;
            }
        }

        // モール名称を抜き出し
        if (libuforg[i][5].match(mhstr)) {
            mallname = RegExp.$1;
        } else {
            mallname = libuforg[i][5];
        }
        // モールリスト
        if (mallselist[mallname]) {
            mallselist[mallname]++;
        } else {
            mallselist[mallname] = 1;
        }

        // モール選択チェック
        if (mallsenf != '未選択') {
            // 名称を抜き出し
            var mallname;
            if (libuforg[i][5].match(mhstr)) {
                mallname = RegExp.$1;
            } else {
                mallname = libuforg[i][5];
            }
            if (mallname != mallsenf) continue;
        }

        // 店舗名称を抜き出し
        var shopname;
        var mallname;
        if (libuforg[i][4].match(mhstr)) {
            shopname = RegExp.$1;
        } else {
            shopname = libuforg[i][4];
        }
        // 店舗リスト
        if (shopselist[shopname]){
            shopselist[shopname]++;
        } else {
            shopselist[shopname] = 1;
        }

        // 店舗選択チェック
        if (shopsenf != '未選択') {
            // 名称を抜き出し
            var shopname;
            if (libuforg[i][4].match(mhstr)) {
                shopname = RegExp.$1;
            } else {
                shopname = libuforg[i][4];
            }
            if (shopname != shopsenf) continue;
        }

        // 全チェックＯＫ
        libuf[y] = libuforg[i];
        // タイトル一致チェック
        libuf[y][11] = parseFloat(libuf[y][9]);  // 優先表示に報酬率設定
        for (j=0;j<inqkeys.length;j++) {
            if (libuf[y][12].match(keymh[j])) {  // 全角変換商品名マッチ
                libuf[y][11] += 10 - j;   // 優先表示に報酬率アップ
            }
        }
        if (libuf[y][11] < libuf[y][9]) libuf[y][11] = libuf[y][9];
        y++;
    }
    lineno = new Array(libuf.length);
    for (i=0;i<libuf.length;i++) {
        lineno[i] = i;
    }
    itemcnt = libuf.length;
    var iteminqf = 0;
    for (i=0;i<xmnary.length;i++) { // 検索中チェック
        if (xmnary[i].iteminqf) {
            iteminqf = 1;
            break;
        }
    }
    if (itemcnt < 1 && !iteminqf && !errdspf) {
        window.alert('表示できる商品が見つかりませんでした。');
        errdspf = 1;
    }
    maxpg = Math.floor((itemcnt-1)/dsplines);
    // ソート
    // 価格
    //lineno.sort(pric_sort);
    //lineno.sort(pric_rsort);
    // モール
    //lineno.sort(mallno_sort);
    // 率
    lineno.sort(ritsu_rsort);

}

// 検索データ表示バッファ設定
function set_libuf(xmn) {
    var i;
    var cnt = 0;

    if (xmn.iteminqf) return;  // 検索中は処理しない
    if (xmn.abortf) return;    // 中断は処理しない
    if (xmn.dspf) return;      // 設定済みは処理しない
    xmn.dspf = 1;

    for (i=0;i<xmn.cnt;i++) {
        if (xmn.ary[i].length < 1) continue;
        var y = itemcntorg + cnt;
        var cols = xmn.ary[i].split("\t");
        switch (xmn.typenm) {
        case '案内':
        case '案内２':
        case '案内３':
        case '案内４':
        case '案内５':
            if (cols.length < 18) continue;   // 項目数不足
            if (cols[1].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[1]; // 商品番号
            if (itemary[itemno]) continue; // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = cols[1];    // 商品番号
            // 商品バナー
            if (cols[8].length > 0) {     // 商品イメージあり
                libuforg[y][1] = cols[8];    // 商品バナー(イメージ)
            } else {
                libuforg[y][1] = cols[9];    // 商品バナー(テキスト)
            }
            libuforg[y][2] = cols[4];    // 商品価格
            // 商品説明
            if (cols[9].length < 1) { // 商品テキストリンクなし
                libuforg[y][3] = cols[3]+'<br>';    // 商品名
            } else {
                libuforg[y][3] = cols[9]+'<br>';    // 商品バナー(テキスト)
            }
            libuforg[y][3] += cols[6];    // 商品説明
            // 店舗バナー
            if (cols[15].length < 1) { // お店テキストリンクなし
                libuforg[y][4] = cols[14];    // 店舗バナー（イメージ）
            } else {
                libuforg[y][4] = cols[15];    // 店舗バナー（テキスト）
            }
            libuforg[y][5] = mall_tbana[cols[2]];  // モールバナー
            libuforg[y][6] = cols[2];    // モール番号
            libuforg[y][7] = cols[3];    // 商品名
            libuforg[y][8] = cols[11];   // 日付
            libuforg[y][9] = cols[17];   // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;

        case 'Amazon':
            if (cols.length < 8) continue;    // 項目数不足
            if (cols[0].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[0];   // 商品番号
            if (itemary[itemno]) continue;    // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = cols[0];    // 商品番号
            libuforg[y][1] = cols[1];    // 商品バナー(イメージ)
            libuforg[y][2] = cols[2];    // 商品価格
            libuforg[y][3] = cols[3];    // 商品説明
            libuforg[y][4] = cols[4];    // 店舗バナー（イメージ）
            libuforg[y][5] = mall_tbana[cols[5]];  // モールバナー
            libuforg[y][6] = cols[5];    // モール番号
            libuforg[y][7] = cols[6];    // 商品名
            libuforg[y][8] = '';         // 日付(なし)
            libuforg[y][9] = cols[7];    // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;

        case 'Bidders':
        case 'Bidders2':
            if (cols.length < 8) continue;    // 項目数不足
            if (cols[0].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[0];  // 商品番号
            if (itemary[itemno]) continue;    // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = cols[0];    // 商品番号
            libuforg[y][1] = cols[1];    // 商品バナー(イメージ)
            libuforg[y][2] = cols[2];    // 商品価格
            libuforg[y][3] = cols[3];    // 商品説明
            libuforg[y][4] = cols[4];    // 店舗バナー（イメージ）
            libuforg[y][5] = mall_tbana[cols[5]];  // モールバナー
            libuforg[y][6] = cols[5];    // モール番号
            libuforg[y][7] = cols[6];    // 商品名
            libuforg[y][8] = '';         // 日付(なし)
            libuforg[y][9] = cols[7];    // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;

        case 'Rakuten':
            if (cols.length < 8) continue;    // 項目数不足
            if (cols[0].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[0];  // 商品番号
            if (itemary[itemno]) continue;    // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = 'rk'+cols[0];    // 商品番号
            libuforg[y][1] = cols[1];    // 商品バナー(イメージ)
            libuforg[y][2] = cols[2];    // 商品価格
            libuforg[y][3] = cols[3];    // 商品説明
            libuforg[y][4] = cols[4];    // 店舗バナー（イメージ）
            libuforg[y][5] = mall_tbana[cols[5]];  // モールバナー
            libuforg[y][6] = cols[5];    // モール番号
            libuforg[y][7] = cols[6];    // 商品名
            libuforg[y][8] = '';         // 日付(なし)
            libuforg[y][9] = cols[7];    // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;

        case 'Yahoo':
            if (cols.length < 8) continue;    // 項目数不足
            if (cols[0].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[0];  // 商品番号
            if (itemary[itemno]) continue;    // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = 'yh'+cols[0];    // 商品番号
            libuforg[y][1] = cols[1];    // 商品バナー(イメージ)
            libuforg[y][2] = cols[2];    // 商品価格
            libuforg[y][3] = cols[3];    // 商品説明
            libuforg[y][4] = cols[4];    // 店舗バナー（イメージ）
            libuforg[y][5] = mall_tbana[cols[5]];  // モールバナー
            libuforg[y][6] = cols[5];    // モール番号
            libuforg[y][7] = cols[6];    // 商品名
            libuforg[y][8] = '';         // 日付(なし)
            libuforg[y][9] = cols[7];    // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;

        case 'livedoor':
            if (cols.length < 8) continue;    // 項目数不足
            if (cols[0].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[0];  // 商品番号
            if (itemary[itemno]) continue;    // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = 'ld'+cols[0];    // 商品番号
            libuforg[y][1] = cols[1];    // 商品バナー(イメージ)
            libuforg[y][2] = cols[2];    // 商品価格
            libuforg[y][3] = cols[3];    // 商品説明
            libuforg[y][4] = cols[4];    // 店舗バナー（イメージ）
            libuforg[y][5] = mall_tbana[cols[5]];  // モールバナー
            libuforg[y][6] = cols[5];    // モール番号
            libuforg[y][7] = cols[6];    // 商品名
            libuforg[y][8] = '';         // 日付(なし)
            libuforg[y][9] = cols[7];    // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;

        case 'stmx':
            if (cols.length < 8) continue;    // 項目数不足
            if (cols[0].length < 1) continue; // 商品番号が無い
            var itemno = xmn.typenm+'-'+cols[0];  // 商品番号
            if (itemary[itemno]) continue;    // 同じデータ
            itemary[itemno] = 1;
            libuforg[y] = new Array(libufcnt);
            libuforg[y][0] = cols[0];    // 商品番号
            libuforg[y][1] = cols[1];    // 商品バナー(イメージ)
            libuforg[y][2] = cols[2];    // 商品価格
            libuforg[y][3] = cols[3];    // 商品説明
            libuforg[y][4] = cols[4];    // 店舗バナー（イメージ）
            libuforg[y][5] = mall_tbana[cols[5]];  // モールバナー
            libuforg[y][6] = cols[5];    // モール番号
            libuforg[y][7] = cols[6];    // 商品名
            libuforg[y][8] = '';         // 日付(なし)
            libuforg[y][9] = cols[7];    // 報酬率
            libuforg[y][10] = // 商品検索用(TAG削除,半角,大文字変換)
              z2h_ascii(libuforg[y][3].replace(/<[^>]+>/g, '')).toUpperCase();
            libuforg[y][12] = // 商品名検索用(TAG削除,半角)
              z2h_ascii(libuforg[y][7].replace(/<[^>]+>/g, ''));
            cnt++;
            break;
        }
    }
    // 制御変数設定
    xmn.cnt = cnt;  // ダブりを除いた正確な件数
    itemcntorg += cnt;
}

// データ表示処理
function disp_data() {
    var li;
    var st,en;
    var hmsg = '';
    //window.alert('検索件数='+xmnlocal.cnt);

    if (itemcnt < 1) return;  //　商品なし
    st = curpg * dsplines + 1;
    en = st + (dsplines - 1);
    if (en > itemcnt) { en = itemcnt; }
    ds = String(st)+'-'+String(en)+'/'+String(itemcnt);

    hmsg += javaselect();       // 抽出条件表示
    hmsg += javactlbtn(ds, 1);  // 各種コントーロールボタン設定
    hmsg += '<table class="list" width=\"100%\">';

    hmsg += '<tr class="list-head">';
    if (kanridirf) {
        hmsg += '<td class="list-head" nowrap align="center">番号</td>';
    }
    hmsg += '<td class="list-head" nowrap align="center" '+banasz+'>画像</td>';
    hmsg += '<td class="list-head" nowrap align="center">価格</td>';
    hmsg += '<td class="list-head" nowrap align="center">商品名(説明)</td>';
    hmsg += '<td class="list-head" nowrap align="center">店舗名</td>';
    hmsg += '<td class="list-head" nowrap align="center">モール</td>';
    if (kanridirf) {
        hmsg += '<td class="list-head" nowrap align="center">％</td>';
    }
    hmsg += '</tr>';

    for (i=0;i<dsplines;i++) {
        hmsg += disp_line(i);
    }
    hmsg += '</table>';
    hmsg += javactlbtn(ds, 0);  // 各種コントーロールボタン設定

    // 表示
    dispid.innerHTML = hmsg;
    if (!kanridirf)  hrefnostat();   // ステータス表示抑制
}

// １行表示処理
function disp_line(lno) {
    var li;
    var ll = (curpg*dsplines)+lno;
    if (ll < itemcnt) li = lineno[ll];
    else              return '';

    var hmsg = '';
    if (lno % 2) {
        hmsg += '<tr class="list-d2">';
    } else {
        hmsg += '<tr class="list-d1">';
    }
    if (kanridirf) {
        hmsg += '<td class="list-no" align="right">';
        hmsg += libuf[li][0]; // 商品番号
        hmsg += '</td>';
    }

    hmsg += '<td class="list-item" '+banasz+' align="center">';
    hmsg += libuf[li][1]; // 商品バナー
    hmsg += '</td>';

    hmsg += '<td class="list-pri" nowrap align="right">';
    if (libuf[li][2] > 0) {
        hmsg += kanma(libuf[li][2])+'円'; // 商品価格
    } else {
        hmsg += ''; // ０は表示しない
    }
    hmsg += '</td>';

    hmsg += '<td class="list-setu">';
    hmsg += libuf[li][3]+'<br>'; // 商品説明
    hmsg += '</td>';

    hmsg += '<td class="list-shop">';
    hmsg += libuf[li][4]+'<br>'; // 店舗バナー
    hmsg += '</td>';

    hmsg += '<td class="list-mall">';
    hmsg += libuf[li][5]+'<br>'; // モールバナー
    hmsg += '</td>';

    if (kanridirf) {
        var nstr = String(libuf[li][11]);
        if (nstr.length > 6) nstr = nstr.substr(0, 6);
        hmsg += '<td class="list-no" align="right">';
        hmsg += libuf[li][9]+'<br>'; // 報酬率
        hmsg += '('+nstr+')<br>'; // 優先表示率
        hmsg += '</td>';
    }
    hmsg += '</tr>';
    return hmsg;
}

// 表示頁切替処理
function jmp_disp(count) {
    curpg += count;
    if (curpg < 0) { curpg = 0; }
    if (curpg > maxpg) { curpg = maxpg; }
    //window.alert('jmp_disp: maxpg='+String(maxpg));
    //window.alert('jmp_disp: curpg='+String(curpg));
    // データ表示
    disp_data();
}

// 数値カンマ編集
function kanma(val) {
    var nstr;
    var kstr = '';
    var i, pt;

    nstr = String(val);
    for (i=0;i<nstr.length;i++) {
        kstr += nstr.charAt(i);
        pt = nstr.length - i - 1;
        if ((pt > 0) &&  (pt % 3) == 0) {
            kstr += ',';
        }
    }
    return kstr;
}

//デフォルトの順番で表示する
function default_disp() {
    var i;
    //if (confirm("初期に表示されていた順で表示します")) {
    {
        //for (i=0;i<itemcnt;i++) { lineno[i] = i; }  //　レコード順
        lineno.sort(ritsu_rsort);
    }
    // データ表示
    disp_data();
}
// ソート関数(率)
function ritsu_sort(a, b) {
    var ret;
    //ret = libuf[a][9] - libuf[b][9];
    ret = libuf[a][11] - libuf[b][11];
    //alert("["+a+"]="+libuf[a][9]+", ["+b+"]="+libuf[b][9]+" ret="+ret);
    return ret;
}
// 同昇順
function ritsu_rsort(a, b) {
    return (ritsu_sort(b, a));
}

// ソート関数(価格)
function pric_sort(a, b) {
    var ret;
    ret = libuf[a][2] - libuf[b][2];
    //alert("["+a+"]="+libuf[a][2]+", ["+b+"]="+libuf[b][2]+" ret="+ret);
    return ret;
}
// 同昇順
function pric_rsort(a, b) {
    return (pric_sort(b, a));
}
//価格の順に表示する
function pric_sort_disp(flg) {
    if (flg) {
        //if (confirm("価格の高い順でソートします")) {
        {
            lineno.sort(pric_rsort);
        }
    } else {
        //if (confirm("価格の安い順でソートします")) {
        {
            lineno.sort(pric_sort);
        }
    }
    // データ表示
    disp_data();
}

// ソート関数(モール番号)
function mallno_sort(a, b) {
    var ret;
    ret = libuf[a][6] - libuf[b][6];
    //alert("["+a+"]="+libuf[a][6]+", ["+b+"]="+libuf[b][6]+" ret="+ret);
    return ret;
}
// 同昇順
function mallno_rsort(a, b) {
    return (mallno_sort(b, a));
}
//モール順に表示する
function mallno_sort_disp() {
    //if (confirm("モール別でソートします")) {
    {
        lineno.sort(mallno_sort);
    }
    // データ表示
    disp_data();
}

// ソート関数(商品名)
function iname_sort(a, b) {
    var ret;
    ret = cstrcmp(libuf[a][7], libuf[b][7]);
    //alert("["+a+"]="+libuf[a][7]+", ["+b+"]="+libuf[b][7]+" ret="+ret);
    return ret;
}
// 同昇順
function iname_rsort(a, b) {
    return (fname_sort(b, a));
}
// 商品名の順に表示する
function iname_sort_disp(flg) {
    if (flg) {
        //if (confirm("商品名・降順でソートします")) {
        {
            lineno.sort(iname_rsort);
        }
    } else {
        //if (confirm("商品名・昇順でソートします")) {
        {
            lineno.sort(iname_sort);
        }
    }
    // データ表示
    disp_data();
}

// ソート関数(登録日)
function adat_sort(a, b) {
    var ret;
    ret = cstrcmp(libuf[a][8], libuf[b][8]);
    //alert("["+a+"]="+libuf[a][8]+", ["+b+"]="+libuf[b][8]+" ret="+ret);
    return ret;
}
// 同昇順
function adat_rsort(a, b) {
    return (adat_sort(b, a));
}
// 登録日順に表示する
function adat_sort_disp(flg) {
    if (flg) {
        //if (confirm("登録日の新しい順にソートします")) {
        {
            lineno.sort(adat_rsort);
        }
    } else {
        //if (confirm("登録日の古い順にソートします")) {
        {
            lineno.sort(adat_sort);
        }
    }
    // データ表示
    disp_data();
}

// 文字列比較
function cstrcmp(str1, str2, n) {
    var csa;
    var n;

    for (n=0;;n++) {
        // 文字長でチェック
        if (str1.length <= n) {
            if (str2.length <= n) {
                return 0;
            }
            return (-1);
        } else if (str2.length <= n) {
            return 1;
        }
        // 文字コードで比較
        csa = str1.charCodeAt(n) - str2.charCodeAt(n);
        if (csa != 0) {
            return csa;
        }
    }
}

// 各種コントーロールボタン設定
function javactlbtn(stat, dispf) {
    var html =
'<table width="100%">'+
'<tr>'+
' <td>'+
'  <form class="linectl">'+
'  <input type="button" value="トップ" onclick="jmp_disp(-maxpg)">'+
'  <input type="button" value="前頁" onclick="jmp_disp(-1)">'+
'  <input type="button" value="次頁" onclick="jmp_disp(1)">'+
'  <input type="button" value="ラスト" onclick="jmp_disp(maxpg)">'+
'  <span class="ken">'+stat+'</span>　'+
'  <span class="hti">並び順</span>'+
'  <input type="button" value="安い" onclick="pric_sort_disp(0)">'+
'  <input type="button" value="高い" onclick="pric_sort_disp(1)">'+
//'  <input type="button" value="NEW" onclick="adat_sort_disp(1)">'+
//'  <input type="button" value="OLD" onclick="adat_sort_disp(0)">'+
'  <input type="button" value="商品名" onclick="iname_sort_disp(0)">'+
//'  <input type="button" value="モール" onclick="mallno_sort_disp()">'+
'  <input type="button" value="初期" onclick="default_disp()">'+
'  </form>'+
' </td>'+
'</tr>'+
'</table>';
    return  html;
}

// 抽出条件クリア後再表示
function clrbuf_data() {
    xmlnkey = '';
    ainqkey = '';
    inqamt1 = '';
    inqamt2 = '';
    inqset_libuf(); // 表示データ抽出
    redisp_data();  // 再表示表示
}

// モール、店舗選択解除
function reset_data() {
    shopsenf = '未選択';
    mallsenf = '未選択';
    inqset_libuf(); // 表示データ抽出
    redisp_data();  // 再表示表示
}

// 抽出条件設定後再表示
function resetbuf_data(mallf) {
    try {
        xmlnkey = document.getElementById('ninq').value;
    } catch(e) {
        xmlnkey = '';
    }
    try {
        ainqkey = document.getElementById('ainq').value;
    } catch(e) {
        ainqkey = '';
    }
    try {
        inqamt1 = document.getElementById('inqamt1').value;
    } catch(e) {
        inqamt1 = '';
    }
    try {
        inqamt2 = document.getElementById('inqamt2').value;
    } catch(e) {
        inqamt2 = '';
    }
    try {
        shopsenf = document.getElementById('setshops').value;
    } catch(e) {
        shopsenf = '未選択';
    }
    try {
        mallsenf = document.getElementById('setmalls').value;
    } catch(e) {
        mallsenf = '未選択';
    }
    if (mallf) shopsenf = '未選択';
    inqset_libuf(); // 表示データ抽出
    redisp_data();  // 再表示表示
}
// 再表示表示
function redisp_data() {
    errdspf = 0;
    curpg = 0;      // 頁トップへ
    // 制御変数設定
    try {
        dsplines = document.getElementById('displine').value;
    } catch(e) {
        dsplines = getCookie('csetlines');
        if (dsplines < 1 || isNaN(dsplines)) dsplines = 10;
    }
    setCookie("csetlines", dsplines);
    maxpg = Math.floor((itemcnt-1)/dsplines);  // ページ数再計算
    jmp_disp(0); // 表示頁切替処理
}
// 抽出条件表示
function javaselect() {
    var sec1 = '';
    var sec2 = '';
    var sec3 = '';
    var sec4 = '';
    if (dsplines == 10)       sec1 = 'selected';
    else if (dsplines == 30)  sec2 = 'selected';
    else if (dsplines == 50)  sec3 = 'selected';
    else if (dsplines == 100) sec4 = 'selected';
    else                      sec2 = 'selected';

    var html = 
'<table><tr><td align="right">'+
'<select name="setlines" id="displine" onChange="redisp_data()">'+
'<option value="10" '+sec1+'>10</option>'+
'<option value="30" '+sec2+'>30</option>'+
'<option value="50" '+sec3+'>50</option>'+
'<option value="100" '+sec4+'>100</option>'+
'</select>件ずつ'+
' 条件'+
'<input type="text" id="ainq" value="'+ainqkey+'" size=20 maxlength="80">'+
'を含む '+
'<input type="text" id="ninq" value="'+xmlnkey+'" size=20 maxlength="80">'+
'を除く '+
'\\'+
'<input type="text" id="inqamt1" value="'+inqamt1+'" size=8 maxlength="10">'+
'〜\\'+
'<input type="text" id="inqamt2" value="'+inqamt2+'" size=8 maxlength="10"> '+
'<input type="button" value="抽出" onclick="resetbuf_data()"> '+
'<input type="button" value="クリア" onclick="clrbuf_data()">'+
'</td></tr><tr><td>';

    var i;
    var sobj, shopcnt = 0;
    var mobj, mallcnt = 0;
    var selectf;
    var shopnmary = new Array();
    var mallnmary = new Array();

    for (sobj in shopselist) {
        shopnmary[shopcnt] = sobj;
        shopcnt++;
    }
    shopnmary.sort(function(a,b) {
        return shopselist[b] - shopselist[a];
    });
    for (mobj in mallselist) {
        mallnmary[mallcnt] = mobj;
        mallcnt++;
    }
    mallnmary.sort(function(a,b) {
        return mallselist[b] - mallselist[a];
    });

    selectf = '';
    if (mallsenf.indexOf('未選択') >= 0) {
        selectf = 'selected';
    }
    html += '<select name="setmalls" id="setmalls" '+
      'onChange="resetbuf_data(\'mall\')" '+selectf+'>';
    html += '<option value="未選択">モール未選択</option>';
    for (i=0;i<mallnmary.length;i++) {
        var nm = mallnmary[i];
        selectf = '';
        if (mallsenf == nm) {
            selectf = 'selected';
        }
        html += '<option value="'+nm+'" '+selectf+'>'+nm+' ('+mallselist[nm]+
          ')</option>';
    }
    html += '</select>';

    selectf = '';
    if (shopsenf.indexOf('未選択') >= 0) {
        selectf = 'selected';
    }
    html += '<select name="setshops" id="setshops" onChange="resetbuf_data()"'+
      selectf+'>';
    html += '<option value="未選択">店舗未選択</option>';
    for (i=0;i<shopnmary.length;i++) {
        var nm = shopnmary[i];
        selectf = '';
        if (shopsenf == nm) {
            selectf = 'selected';
        }
        html += '<option value="'+nm+'" '+selectf+'>'+nm+' ('+shopselist[nm]+
          ')</option>';
    }
    html += '</select> ';
    html += '<input type="button" value="リセット" onclick="reset_data()">';

    if (kanridirf) {
        html += 'shopcnt='+shopcnt+' mallcnt='+mallcnt;
    }
    html += '</td></tr></table>';

    return html;
}
// 各種コントーロール設定
function info_disp(msg) {
    if (msgnoclrf) inqmsg.innerHTML += msg+'<br>';
    else           inqmsg.innerHTML = msg;
}

