var Keywords     = "";
var Sort         = "";

var Amazon       = false;
var Rakuten      = false;
var Bidders      = false;
var Storemix     = false;

var SearchIndex  = "";
var Genre        = "";
var Categ        = "";

// 検索処理
function Search(Prefix) {
    // Amazonの処理を実行するための関数リファレンスを定義
    var funcRef_Amazon = function(xml) {
        document.getElementById(Prefix + 'amazonresults').innerHTML = "";
        items = xml.getElementsByTagName('Item');
        buf = "";
        if(items.length>=0) {
            KeywordNode    = xml.getElementsByTagName('Keyword');
            Keyword        = KeywordNode[0].childNodes[0].nodeValue;
            WebURLNode     = xml.getElementsByTagName('WebURL');
            WebURL         = WebURLNode[0].childNodes[0].nodeValue;
            TotalCountNode = xml.getElementsByTagName('TotalCount');
            TotalCount     = TotalCountNode[0].childNodes[0].nodeValue;
            CountNode      = xml.getElementsByTagName('Count');
            Count          = CountNode[0].childNodes[0].nodeValue;
            document.getElementById(Prefix + 'amazoninfo').innerHTML = '<hr /><p><strong><span style="color : #0000FF;">Amazon「' + Keyword + '」の 検索結果</span> ' + TotalCount + ' 件中 ' + Count + ' 件表示</p><p>(全件表示されていない場合は、<a href="' + WebURL + '">こちら</a>からご覧いただけます)</strong></p><hr />\n';
            buf += '<table style="width : 95%; margin : 0em auto 1em auto; font-size : 100%;" summary="AMAZON RESULT" border="0" cellspacing="0" cellpadding="0"><tbody>\n';
            for(i=0;i<items.length;i++) {
                RowNumNode            = items[i].getElementsByTagName('RowNum');
                URLNode               = items[i].getElementsByTagName('URL');
                TitleNode             = items[i].getElementsByTagName('Title');
                ImageURLNode          = items[i].getElementsByTagName('ImageURL');
                FormattedPriceNode    = items[i].getElementsByTagName('FormattedPrice');
                NewFormattedPriceNode = items[i].getElementsByTagName('NewFormattedPrice');
                RowNum                = RowNumNode[0].childNodes[0].nodeValue;
                URL                   = URLNode[0].childNodes[0].nodeValue;
                Title                 = TitleNode[0].childNodes[0].nodeValue;
                ImageURL              = ImageURLNode[0].childNodes[0].nodeValue;
                FormattedPrice        = "";
                NewFormattedPrice     = "";
                if(FormattedPriceNode[0].childNodes.length>0)    FormattedPrice    = FormattedPriceNode[0].childNodes[0].nodeValue;
                if(NewFormattedPriceNode[0].childNodes.length>0) NewFormattedPrice = NewFormattedPriceNode[0].childNodes[0].nodeValue;
                if(NewFormattedPrice=="") {
                    buf += '<tr><td><p>' + RowNum + '</p></td><td style="text-align : center;"><p><a href="' + URL + '"><img src="' + ImageURL + '" alt="' + Title + '" /></a></p></td><td><p><a href="' + URL + '">' + Title + '</a></p></td><td><p style="text-align : right;">' + FormattedPrice + '</p></td></tr><tr><td colspan="4"><p style="text-align : right; font-size : 80%;"><a href="#Logo">ページのトップへ</a></p><hr class="thin"/></td></tr>\n';
                }
                else {
                    buf += '<tr><td><p>' + RowNum + '</p></td><td style="text-align : center;"><p><a href="' + URL + '"><img src="' + ImageURL + '" alt="' + Title + '" /></a></p></td><td><p><a href="' + URL + '">' + Title + '</a></p></td><td><p style="text-align : right;">' + FormattedPrice + '<br/> =&gt; <strong style="color : #FF0000;">' + NewFormattedPrice + '</strong></p></td></tr><tr><td colspan="4"><p style="text-align : right; font-size : 80%;"><a href="#Logo">ページのトップへ</a></p><hr class="thin"/></td></tr>\n';
                }
            }
            buf += '</tbody></table>\n';
        }
        document.getElementById(Prefix + 'amazonresults').innerHTML   = buf;
        document.getElementById(Prefix + 'amazonbookmarks').innerHTML = '<p><a href="shop.html#' + Prefix + 'amazoninfo"><strong>Amazon「' + Keyword + '」の検索結果へ</strong></a></p>';
        ErrMsgNode = xml.getElementsByTagName('ErrMsg');
        if(ErrMsgNode[0].childNodes.length>0) {
            document.getElementById(Prefix + 'amazonerror').innerHTML = '<p><strong style="color : #FF0000;">' + ErrMsgNode[0].childNodes[0].nodeValue + '</strong></p>';
        }
        else {
            document.getElementById(Prefix + 'amazonerror').innerHTML = '';
        }
    }

    // Rakutenの処理を実行するための関数リファレンスを定義
    var funcRef_Rakuten = function(xml) {
        document.getElementById(Prefix + 'rakutenresults').innerHTML = "";
        items = xml.getElementsByTagName('Item');
        buf = "";
        if(items.length>=0) {
            KeywordNode    = xml.getElementsByTagName('Keyword');
            Keyword        = KeywordNode[0].childNodes[0].nodeValue;
            WebURLNode     = xml.getElementsByTagName('WebURL');
            WebURL         = WebURLNode[0].childNodes[0].nodeValue;
            TotalCountNode = xml.getElementsByTagName('TotalCount');
            TotalCount     = TotalCountNode[0].childNodes[0].nodeValue;
            CountNode      = xml.getElementsByTagName('Count');
            Count          = CountNode[0].childNodes[0].nodeValue;
            document.getElementById(Prefix + 'rakuteninfo').innerHTML = '<hr /><p><strong><span style="color : #0000FF;">楽天「' + Keyword + '」の 検索結果</span> ' + TotalCount + ' 件中 ' + Count + ' 件表示</p><p>(全件表示されていない場合は、<a href="' + WebURL + '">こちら</a>からご覧いただけます)</strong></p><hr />\n';
            buf += '<table style="width : 95%; margin : 0em auto 1em auto; font-size : 100%;" summary="RAKUTEN RESULT" border="0" cellspacing="0" cellpadding="0"><tbody>\n';
            for(i=0;i<items.length;i++) {
                RowNumNode            = items[i].getElementsByTagName('RowNum');
                URLNode               = items[i].getElementsByTagName('URL');
                TitleNode             = items[i].getElementsByTagName('Title');
                ImageURLNode          = items[i].getElementsByTagName('ImageURL');
                FormattedPriceNode    = items[i].getElementsByTagName('FormattedPrice');
                RowNum                = RowNumNode[0].childNodes[0].nodeValue;
                URL                   = URLNode[0].childNodes[0].nodeValue;
                Title                 = TitleNode[0].childNodes[0].nodeValue;
                ImageURL              = ImageURLNode[0].childNodes[0].nodeValue;
                FormattedPrice        = "";
                if(FormattedPriceNode[0].childNodes.length>0)    FormattedPrice    = FormattedPriceNode[0].childNodes[0].nodeValue;
                buf += '<tr><td><p>' + RowNum + '</p></td><td style="text-align : center;"><p><a href="' + URL + '"><img src="' + ImageURL + '" alt="' + Title + '" /></a></p></td><td><p><a href="' + URL + '">' + Title + '</a></p></td><td><p style="text-align : right;">' + FormattedPrice + '</p></td></tr><tr><td colspan="4"><p style="text-align : right; font-size : 80%;"><a href="#Logo">ページのトップへ</a></p><hr class="thin"/></td></tr>\n';
            }
            buf += '</tbody></table>\n';
        }
        document.getElementById(Prefix + 'rakutenresults').innerHTML   = buf;
        document.getElementById(Prefix + 'rakutenbookmarks').innerHTML = '<p><a href="shop.html#' + Prefix + 'rakuteninfo"><strong>楽天「' + Keyword + '」の検索結果へ</strong></a></p>';
        ErrMsgNode = xml.getElementsByTagName('ErrMsg');
        if(ErrMsgNode[0].childNodes.length>0) {
            document.getElementById(Prefix + 'rakutenerror').innerHTML = '<p><strong style="color : #FF0000;">' + ErrMsgNode[0].childNodes[0].nodeValue + '</strong></p>';
        }
        else {
            document.getElementById(Prefix + 'rakutenerror').innerHTML = '';
        }
    }

    // Biddersの処理を実行するための関数リファレンスを定義
    var funcRef_Bidders = function(xml) {
        document.getElementById(Prefix + 'biddersresults').innerHTML = "";
        items = xml.getElementsByTagName('Item');
        buf = "";
        if(items.length>=0) {
            KeywordNode    = xml.getElementsByTagName('Keyword');
            Keyword        = KeywordNode[0].childNodes[0].nodeValue;
            WebURLNode     = xml.getElementsByTagName('WebURL');
            WebURL         = WebURLNode[0].childNodes[0].nodeValue;
            TotalCountNode = xml.getElementsByTagName('TotalCount');
            TotalCount     = TotalCountNode[0].childNodes[0].nodeValue;
            CountNode      = xml.getElementsByTagName('Count');
            Count          = CountNode[0].childNodes[0].nodeValue;
            document.getElementById(Prefix + 'biddersinfo').innerHTML = '<hr /><p><strong><span style="color : #0000FF;">ビッダーズ「' + Keyword + '」の 検索結果</span> ' + TotalCount + ' 件中 ' + Count + ' 件表示</p><p>(全件表示されていない場合は、<a href="' + WebURL + '">こちら</a>からご覧いただけます)</strong></p><hr />\n';
            buf += '<table style="width : 95%; margin : 0em auto 1em auto; font-size : 100%;" summary="BIDDERS RESULT" border="0" cellspacing="0" cellpadding="0"><tbody>\n';
            for(i=0;i<items.length;i++) {
                RowNumNode            = items[i].getElementsByTagName('RowNum');
                URLNode               = items[i].getElementsByTagName('URL');
                TitleNode             = items[i].getElementsByTagName('Title');
                ImageURLNode          = items[i].getElementsByTagName('ImageURL');
                FormattedPriceNode    = items[i].getElementsByTagName('FormattedPrice');
                TypeImageURLNode      = items[i].getElementsByTagName('TypeImageURL');
                TypeNode              = items[i].getElementsByTagName('Type');
                RowNum                = RowNumNode[0].childNodes[0].nodeValue;
                URL                   = URLNode[0].childNodes[0].nodeValue;
                Title                 = TitleNode[0].childNodes[0].nodeValue;
                ImageURL              = ImageURLNode[0].childNodes[0].nodeValue;
                TypeImageURL          = TypeImageURLNode[0].childNodes[0].nodeValue;
                Type                  = TypeNode[0].childNodes[0].nodeValue;
                FormattedPrice        = "";
                if(FormattedPriceNode[0].childNodes.length>0)    FormattedPrice    = FormattedPriceNode[0].childNodes[0].nodeValue;
                buf += '<tr><td><p>' + RowNum + '</p></td><td style="text-align : center;"><p><a href="' + URL + '"><img src="' + ImageURL + '" width="64" alt="' + Title + '" /></a></p></td><td><p><a href="' + URL + '">' + Title + '</a></p></td><td><p style="text-align : right;"><img src="' + TypeImageURL + '" alt="' + Type + '" /><br/>' + FormattedPrice + '</p></td></tr><tr><td colspan="4"><p style="text-align : right; font-size : 80%;"><a href="#Logo">ページのトップへ</a></p><hr class="thin"/></td></tr>\n';
            }
            buf += '</tbody></table>\n';
        }
        document.getElementById(Prefix + 'biddersresults').innerHTML   = buf;
        document.getElementById(Prefix + 'biddersbookmarks').innerHTML = '<p><a href="shop.html#' + Prefix + 'biddersinfo"><strong>ビッダーズ「' + Keyword + '」の検索結果へ</strong></a></p>';
        ErrMsgNode = xml.getElementsByTagName('ErrMsg');
        if(ErrMsgNode[0].childNodes.length>0) {
            document.getElementById(Prefix + 'bidderserror').innerHTML = '<p><strong style="color : #FF0000;">' + ErrMsgNode[0].childNodes[0].nodeValue + '</strong></p>';
        }
        else {
            document.getElementById(Prefix + 'bidderserror').innerHTML = '';
        }
    }

    // Storemixの処理を実行するための関数リファレンスを定義
    var funcRef_Storemix = function(xml) {
        document.getElementById(Prefix + 'storemixresults').innerHTML = "";
        items = xml.getElementsByTagName('Item');
        buf = "";
        if(items.length>=0) {
            KeywordNode    = xml.getElementsByTagName('Keyword');
            Keyword        = KeywordNode[0].childNodes[0].nodeValue;
            WebURLNode     = xml.getElementsByTagName('WebURL');
            WebURL         = WebURLNode[0].childNodes[0].nodeValue;
            TotalCountNode = xml.getElementsByTagName('TotalCount');
            TotalCount     = TotalCountNode[0].childNodes[0].nodeValue;
            CountNode      = xml.getElementsByTagName('Count');
            Count          = CountNode[0].childNodes[0].nodeValue;
            document.getElementById(Prefix + 'storemixinfo').innerHTML = '<hr /><p><strong><span style="color : #0000FF;">ストアミックス「' + Keyword + '」の 検索結果</span> ' + TotalCount + ' 件中 ' + Count + ' 件表示</p><p>(全件表示されていない場合は、<a href="' + WebURL + '">こちら</a>からご覧いただけます)</strong></p><hr />\n';
            buf += '<table style="width : 95%; margin : 0em auto 1em auto; font-size : 100%;" summary="STOREMIX RESULT" border="0" cellspacing="0" cellpadding="0"><tbody>\n';
            for(i=0;i<items.length;i++) {
                RowNumNode            = items[i].getElementsByTagName('RowNum');
                URLNode               = items[i].getElementsByTagName('URL');
                TitleNode             = items[i].getElementsByTagName('Title');
                ImageURLNode          = items[i].getElementsByTagName('ImageURL');
                FormattedPriceNode    = items[i].getElementsByTagName('FormattedPrice');
                RowNum                = RowNumNode[0].childNodes[0].nodeValue;
                URL                   = URLNode[0].childNodes[0].nodeValue;
                Title                 = TitleNode[0].childNodes[0].nodeValue;
                ImageURL              = ImageURLNode[0].childNodes[0].nodeValue;
                FormattedPrice        = "";
                if(FormattedPriceNode[0].childNodes.length>0)    FormattedPrice    = FormattedPriceNode[0].childNodes[0].nodeValue;
                buf += '<tr><td><p>' + RowNum + '</p></td><td style="text-align : center;"><p><a href="' + URL + '"><img src="' + ImageURL + '" width="64" alt="' + Title + '" /></a></p></td><td><p><a href="' + URL + '">' + Title + '</a></p></td><td><p style="text-align : right;">' + FormattedPrice + '</p></td></tr><tr><td colspan="4"><p style="text-align : right; font-size : 80%;"><a href="#Logo">ページのトップへ</a></p><hr class="thin"/></td></tr>\n';
            }
            buf += '</tbody></table>\n';
        }
        document.getElementById(Prefix + 'storemixresults').innerHTML   = buf;
        document.getElementById(Prefix + 'storemixbookmarks').innerHTML = '<p><a href="shop.html#' + Prefix + 'storemixinfo"><strong>ストアミックス「' + Keyword + '」の検索結果へ</strong></a></p>';
        ErrMsgNode = xml.getElementsByTagName('ErrMsg');
        if(ErrMsgNode[0].childNodes.length>0) {
            document.getElementById(Prefix + 'storemixerror').innerHTML = '<p><strong style="color : #FF0000;">' + ErrMsgNode[0].childNodes[0].nodeValue + '</strong></p>';
        }
        else {
            document.getElementById(Prefix + 'storemixerror').innerHTML = '';
        }
    }

    // HTTP通信を開始し、完了したら上記関数を実行させる

    // Amazon
    if(Amazon) {
        amzKeywords = Keywords;
        httpRequest("../plaza/xml_tool_amazon.php?dummy=" + Prefix,"Keywords=" + encodeURIComponent(amzKeywords) + "&SearchIndex=" + encodeURIComponent(SearchIndex) + "&Sort=" + encodeURIComponent(Sort),funcRef_Amazon);
    }
    else
    if(!Amazon) {
        document.getElementById(Prefix + 'amazoninfo').innerHTML      = "";
        document.getElementById(Prefix + 'amazonresults').innerHTML   = "";
        document.getElementById(Prefix + 'amazonbookmarks').innerHTML = "";
        document.getElementById(Prefix + 'amazonerror').innerHTML     = "";
    }

    // Rakuten
    if(Rakuten) {
        httpRequest("../plaza/xml_tool_rakuten.php?dummy=" + Prefix,"Keywords=" + encodeURIComponent(Keywords) + "&Genre=" + encodeURIComponent(Genre) + "&Sort=" + encodeURIComponent(Sort),funcRef_Rakuten);
    }
    else
    if(!Rakuten) {
        document.getElementById(Prefix + 'rakuteninfo').innerHTML      = "";
        document.getElementById(Prefix + 'rakutenresults').innerHTML   = "";
        document.getElementById(Prefix + 'rakutenbookmarks').innerHTML = "";
        document.getElementById(Prefix + 'rakutenerror').innerHTML     = "";
    }

    // Bidders
    if(Bidders) {
        httpRequest("../plaza/xml_tool_bidders.php?dummy=" + Prefix,"Keywords=" + encodeURIComponent(Keywords) + "&Categ=" + encodeURIComponent(Categ) + "&Sort=" + encodeURIComponent(Sort),funcRef_Bidders);
    }
    else
    if(!Bidders) {
        document.getElementById(Prefix + 'biddersinfo').innerHTML      = "";
        document.getElementById(Prefix + 'biddersresults').innerHTML   = "";
        document.getElementById(Prefix + 'biddersbookmarks').innerHTML = "";
        document.getElementById(Prefix + 'bidderserror').innerHTML     = "";
    }

    // Storemix
    if(Storemix) {
        httpRequest("../plaza/xml_tool_storemix.php?dummy=" + Prefix,"Keywords=" + encodeURIComponent(Keywords),funcRef_Storemix);
    }
    else
    if(!Storemix) {
        document.getElementById(Prefix + 'storemixinfo').innerHTML      = "";
        document.getElementById(Prefix + 'storemixresults').innerHTML   = "";
        document.getElementById(Prefix + 'storemixbookmarks').innerHTML = "";
        document.getElementById(Prefix + 'storemixerror').innerHTML     = "";
    }
}

