//リクエスト処理（itemSearch）
function itemSearch(obj, mode) {
  var url = "http://www.franklinpa.net/itemSearch.php";
  var paramList = "";

  with(obj.form){
    paramList += "keyword=" + encodeURIComponent(txtWord.value);
    paramList += "&page=" + txtPage.value;
    paramList += "&sort=" + encodeURIComponent(slctSort.value);
    switch(mode){
      case "simple":
        paramList += "&mode=simple";
        break;
      case "detail":
        paramList += "&shopCode=" + txtShopCode.value;
        paramList += "&genreId=" + txtGenreId.value;
        paramList += "&catalogCode=" + txtCatalogCode.value;
        paramList += "&hits=" + txtHits.value;
        paramList += "&minPrice=" + txtMinPrice.value;
        paramList += "&maxPrice=" + txtMaxPrice.value;
        paramList += "&availability=" + slctAvailability.value;
        paramList += "&field=" + slctField.value;
        paramList += "&carrier=" + slctCarrier.value;
        paramList += "&imageFlag=" + slctImageFlag.value;
        paramList += "&orFlag=" + slctOrFlag.value;
        paramList += "&NGKeyword=" + encodeURIComponent(txtNGKeyword.value);
        paramList += "&genreInformationFlag=" + slctGenreInformationFlag.value;
        paramList += "&purchaseType=" + slctPurchaseType.value;
        paramList += "&mode=detail";
        //詳細表示クリア
        $("state").innerHTML = "";
        $("result").innerHTML = "";
        break;
    }
  }

  new Ajax.Request(url,{
      method: 'get',
      onSuccess: sucRequest,
      onFailure: errRequest,
      parameters: paramList
  });


  //リクエスト失敗処理（itemSearch）
  function errRequest(){
    alert("データを取得できません。\n" + url + "／" + paramList);
  }


  //リクエスト成功処理（itemSearch）
  function sucRequest(data){
    var html = ""; var state = "";

    var response = data.responseXML.getElementsByTagName('Response');
    var status = getValue(response[0], 'Status');

    if(status == "Success"){
      var first = getValue(response[0], 'first');
      var last = getValue(response[0], 'last');
      var count = getValue(response[0], 'count');
      var page = getValue(response[0], 'page');
      var pageCount = getValue(response[0], 'pageCount');
      state += "データを取得しました。";
      state += "<br />";
      state += page + "ページ目／" + pageCount + "ページ中　　";
      state += first + "〜" + last + "件目／" + count + "件中";
      state += "<br />";
    }else{
      var statusMsg = getValue(response[0], 'StatusMsg');
      alert("データが見つかりません。\n" + status + "／" + statusMsg);
      return;
    }

    var item = response[0].getElementsByTagName('Item');

    for(i = 0; i < item.length; i++){
      //値を取得
      var itemNameValue = getValue(item[i], 'itemName');
      var itemPriceValue = getValue(item[i], 'itemPrice');
      var affiliateUrlValue = getValue(item[i], 'affiliateUrl');
      var mediumImageUrlValue = getValue(item[i], 'mediumImageUrl');
      var itemCaptionValue = getValue(item[i], 'itemCaption');
      var shopOfTheYearFlagValue = getValue(item[i], 'shopOfTheYearFlag');
      var reviewCountValue = getValue(item[i], 'reviewCount');
      var reviewAverageValue = getValue(item[i], 'reviewAverage');
      var shopNameValue = getValue(item[i], 'shopName');
      var shopUrlValue = getValue(item[i], 'shopUrl');

      //HTML作成
      html += "<p>";

      html += "<a href='" + affiliateUrlValue + "'>";
      html += itemNameValue + "</a>";
      html += "<br />";

      html += "<tt>";
      html += "<font color='gold'><b>";
      if (reviewAverageValue == 0.0)
          html += "？？？　　　";
      else if (reviewAverageValue > 4.5)
          html += "★★★★★　";
      else if (reviewAverageValue > 4.0)
          html += "★★★★　　";
      else if (reviewAverageValue > 3.5)
          html += "★★★　　　";
      else if (reviewAverageValue > 3.0)
          html += "★★　　　　";
      else
          html += "★　　　　　";

      if (shopOfTheYearFlagValue == 1){
          html += "ショップ・オブ・ザ・イヤー受賞";
      }
      html += "</b></font>";
      html += "<br />";

      html += "評価：" + reviewAverageValue + "点　レビュー：" + reviewCountValue + "件　";
      html += "販売店：" + "<a href='" + shopUrlValue + "'>" + shopNameValue + "</a>";
      html += "</tt>";
      html += "<br />";

      html += "<b>" + itemPriceValue + "円</b>";
      html += "<br />";

      if(mediumImageUrlValue != ""){
        html += "<a href='" + affiliateUrlValue + "'>";
        html += "<img src='"+ mediumImageUrlValue + "' align='right'>";
        html += "</a>";
      }else{
        html += "商品画像なし";
      }
      html += "<br />";

      if(itemCaptionValue.length >= 200){
        html += itemCaptionValue.substring(0, 200);
      }else{
        html += itemCaptionValue;
      }

      html += "</p>";
      //パラグラフ外でクリア
      html += "<br clear='right'/>";
      html += "<hr />";

    }

    //結果表示
    switch(mode){
      case "simple":
        subWindow('楽天商品｜シンプル検索', state, html);
        break;
      case "detail":
        $("state").innerHTML = state;
        $("result").innerHTML = html;
        break;
    }
  }
  return false;
}


//ＸＭＬのＶＡＬＵＥ取得
function getValue(xmlData, tagName) {
  var tmp = xmlData.getElementsByTagName(tagName);
  var tmpValue;

  //子要素を取得
  var cn = tmp[0].childNodes;

  //値がある場合は取得
  if(cn.length >= 1) {
    tmpValue = tmp[0].firstChild.nodeValue;
  }else{
    tmpValue = "";
  }
  return tmpValue;
}


//サブウィンドウ表示
function subWindow(title, state, html){
  var subWindow = window.open("","","resizable=yes,scrollbars=yes,width=800,height=800");

  subWindow.document.write("<html><head>");
  subWindow.document.write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
  subWindow.document.write("<link rel='stylesheet' type='text/css' href='./lib/index.css'>");
  subWindow.document.write("<title>");
  subWindow.document.write(title);
  subWindow.document.write("</title></head><body>");
  subWindow.document.write(state);
  subWindow.document.write(html);
  subWindow.document.write("</body></html>");
  subWindow.document.close();
}