//
//  Licensed Materials - Property of IBM
//  5724I83
//  (C) Copyright IBM Corp. 1995, 2008 All Rights Reserved.
//
// ***** DO NOT EDIT THIS FILE *****

utf = new function()
{
    this.unpackUTF16 = function(_str)
    {
        var i, utf16=[];
        for (i=0; i<_str.length; i++)
        {
            utf16[i] = _str.charCodeAt(i);
        }
        return utf16;
    }
    
    this.unpackChar = function(_str) 
    {
        var utf16 = this.unpackUTF16(_str);
        var i,n, tmp = [];
        for (n=i=0; i<utf16.length; i++)
        {
            if (utf16[i]<=0xff) tmp[n++] = utf16[i];
            else {
                tmp[n++] = utf16[i] >> 8;
                tmp[n++] = utf16[i] &  0xff;
            }
        }
        return tmp;
    }
    
    this.packChar  =
    this.packUTF16 = function(_utf16)
    {
        var i, str = "";
        for (i in _utf16)
        {
            str += String.fromCharCode(_utf16[i]);
        }
        return str;
    }

    this.unpackUTF8 = function(_str)
    {
       return this.toUTF8( this.unpackUTF16(_str) );
    }

    this.packUTF8 = function(_utf8)
    {
        return this.packUTF16( this.toUTF16(_utf8) );
    }
    
    this.toUTF8 = function(_utf16)
    {
        var utf8 = [];
        var idx = 0;
        var i, j, c;
        for (i=0; i<_utf16.length; i++)
        {
            c = _utf16[i];
            if (c <= 0x7f)
            {
                utf8[idx++] = c;
            }
            else if (c <= 0x7ff)
            {
                utf8[idx++] = 0xc0 | (c >>> 6 );
                utf8[idx++] = 0x80 | (c & 0x3f);
            }
            else if (c <= 0xffff)
            {
                utf8[idx++] = 0xe0 | (c >>> 12 );
                utf8[idx++] = 0x80 | ((c >>> 6 ) & 0x3f);
                utf8[idx++] = 0x80 | (c & 0x3f);
            }
            else
            {
                j = 4;
                while (c >> (6*j)) j++;
                utf8[idx++] = ((0xff00 >>> j) & 0xff) | (c >>> (6*--j) );
                while (j--) 
                utf8[idx++] = 0x80 | ((c >>> (6*j)) & 0x3f);
            }
        }
        return utf8;
    }
    
    this.toUTF16 = function(_utf8)
    {
        var utf16 = [];
        var idx = 0;
        var i,s;
        for (i=0; i<_utf8.length; i++, idx++)
        {
            if (_utf8[i] <= 0x7f)
            {
                utf16[idx] = _utf8[i];
            }
            else 
            {
                if ( (_utf8[i]>>5) == 0x6)
                {
                    utf16[idx] = ( (_utf8[i] & 0x1f) << 6 )
                                 | ( _utf8[++i] & 0x3f );
                }
                else if ( (_utf8[i]>>4) == 0xe)
                {
                    utf16[idx] = ( (_utf8[i] & 0xf) << 12 )
                                 | ( (_utf8[++i] & 0x3f) << 6 )
                                 | ( _utf8[++i] & 0x3f );
                }
                else
                {
                    s = 1;
                    while (_utf8[i] & (0x20 >>> s) ) s++;
                    utf16[idx] = _utf8[i] & (0x1f >>> s);
                    while (s-->=0) utf16[idx] = (utf16[idx] << 6) ^ (_utf8[++i] & 0x3f);
                }
            }
        }
        return utf16;
    }
    
    this.URLencode = function(_str)
    {
        return _str.replace(/([^a-zA-Z0-9_\-\.])/g, function(_tmp, _c)
            { 
                if (_c == "\x20") return "+";
                var tmp = utf.toUTF8( [_c.charCodeAt(0)] );
                var c = "";
                for (var i in tmp)
                {
                    i = tmp[i].toString(16);
                    if (i.length == 1) i = "0"+ i;
                    c += "%"+ i;
                }
                return c;
            } );
    }

    this.URLdecode = function(_dat)
    {
        _dat = _dat.replace(/\+/g, "\x20");
        _dat = _dat.replace( /%([a-fA-F0-9][a-fA-F0-9])/g, 
                function(_tmp, _hex){ return String.fromCharCode( parseInt(_hex, 16) ) } );
        return this.packChar( this.toUTF16( this.unpackUTF16(_dat) ) );
    }
}//-->

var hpbY=new Object();

  hpbY.ContentLoader=function(div
        , appId
        , affeId
        , NumOfshowItems
        , affiliate_type
        , query
        , strFormat
        , imageType
        , bMediumImage
        , strSort
        , strMinPrice
        , strMaxPrice
        , strCategory
        , strAvailability
        , strDiscount
        , strShipping
        , strPrice
        , strShop
        , nDesMaxLen
        , callBackFuncName
        , nUpdatingInterval
        , nPage) {

  if( this.div != null)
  {
    document.body.removeChild(this.div);
  }							
  this.div = div;
  this.appId = appId;
  this.affeId = affeId;
  this.NumOfshowItems = NumOfshowItems;
  this.paramString = "";
  this.showPrice = true;
  this.postage = true;
  this.showTax = true;
  this.showCaptions = true;
  this.mediamImage = true;
  this.affiliate_type = affiliate_type;
  this.queryOrd = query;
  this.query = utf.URLencode(query);
  this.request=null;
  this.strFormat = strFormat;
  this.bHorizontal = true;
  if( strFormat == 'vertical')
	this.bHorizontal = false;
  this.imageType = imageType;
  this.bMediumImage = bMediumImage;
  this.nDescriptionMaxLen = nDesMaxLen;
  this.strSort = strSort;
  this.strMinPrice = strMinPrice;
  this.strMaxPrice = strMaxPrice;
  this.strCategory = strCategory;
  this.strAvailability = strAvailability;
  this.strDiscount = strDiscount;
  this.strShipping = strShipping;
  this.strPrice = strPrice;
  this.strShop = strShop;
  this.callBackFuncName = callBackFuncName;
  this.nUpdatingInterval = nUpdatingInterval;
  this.nPage = nPage;
  
  if( strSort != "" && strSort != null) {
    this.paramString += '&sort=' + utf.URLencode(strSort);
  }

  if( strMinPrice != "" && strMinPrice != null) {
    this.paramString += '&price_from=' + strMinPrice;
  }
  
  if( strMaxPrice != "" && strMaxPrice != null) {
    this.paramString += '&price_to=' + strMaxPrice;
  }

  if( strCategory != "" && strCategory != null) {
    this.paramString += '&category_id=' + strCategory;
  }

  if( strAvailability != "" && strAvailability != null) {
    this.paramString += '&availability=' + strAvailability;
  }

  if( strDiscount != "" && strDiscount != null) {
    this.paramString += '&discount=' + strDiscount;
  }

  if( strShipping != "" && strShipping != null) {
    this.paramString += '&shipping=' + strShipping;
  }

  this.showAffeliate();
}

var loaderY;
hpbY.ContentLoader.prototype={
  showAffeliate:function(){
    var httpPost = false;
    loaderY=this;
    var offset = (this.nPage-1) * this.NumOfshowItems;
    var reqString = 'http://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch?'
        + 'appid=' + this.appId 
        + '&affiliate_id=' + this.affeId 
        + '&affiliate_type=' + this.affiliate_type 
        + '&query=' + this.query
        + '&hits=' + this.NumOfshowItems
        + '&offset=' + offset
        + '&callback=' + this.callBackFuncName;        

    if(this.paramString.length>0)
    {
      reqString += this.paramString;
    }

    var script = document.createElement( 'script' );
    script.type = 'text/javascript';
    script.charset = 'UTF-8';
    script.src = reqString;
    document.getElementsByTagName("head").item(0).appendChild( script );
  }
}	

function getImageY(imageFormat, item) {
    var begin;
    var end;
    var strOutput = '';
    if(item==null){
        return '';
    } else if( imageFormat == 'no' )
    {
        return '';
    } else if( imageFormat == 'large') {
        var img = '';
        if(item.Image){
            if(item.Image.Medium==null || item.Image.Medium==''){
                img = item.Image.Small;
            } else {
                img = item.Image.Medium;
            }
        }
        if(img!=''){
            strOutput = '<IMG align="left" class="hpb-affiliate-image" src="' +
                img +
                '" border="0" align="left">';
        }
    } else { // imageFormat == 'small'
        var img = '';
        if(item.Image){
            if(item.Image.Small==null || item.Image.Small==''){
                img = item.Image.Medium;;
            } else {
                img = item.Image.Small;
            }
        }
        if(img!=''){
            strOutput = '<IMG align="left" class="hpb-affiliate-image" src="' +
                img +
                '" border="0">';
        }
    }
    return strOutput;
}

function getNameY(item) {
    var begin;
    var end;
    var strOutput = '';
    begin = '<SPAN class="hpb-affiliate-name">';
    end = '</SPAN><BR>';

    var strValue = '';
    if(item!=null){
        if(item.Name!=null && item.Name!=''){
            strValue = item.Name;
        }
    }
    
    if( strValue != '')
    {
        strOutput = begin;
        strOutput += strValue;
        strOutput += end;
    }
    return strOutput;
}

function getPriceY(item) {
    var begin;
    var end;
    var strOutput = '';
    begin = '<SPAN class="hpb-affiliate-price">';
    end = '</SPAN><BR>';

    var strValue = '';
    if(item!=null){
        var tax = '';
        if(item.Price!=null && item.Price!=''){
            strValue = item.Price._value + '円';
        }
        if(item.PriceLabel!=null &&
           item.PriceLabel._attributes &&
           item.PriceLabel._attributes.taxIncluded){
           tax = item.PriceLabel._attributes.taxIncluded;
        }
        if(tax=='true'){
            strValue += ' (消費税込)';
        }
    }
    
    if( strValue != '')
    {
        strOutput = begin;
        strOutput += strValue;
        strOutput += end;
    }
    return strOutput;
}

function getShopY(item) {
    var begin;
    var end;
    var strOutput = '';
    begin = '<SPAN class="hpb-affiliate-shop">';
    end = '</SPAN><BR>';

    var strValue = '';
    if(item!=null){
        if(item.Store!=null && item.Store.Name!=null && item.Store.Name!=''){
            strValue = item.Store.Name;
        }
    }
    
    if( strValue != '')
    {
        strOutput = begin;
        strOutput += strValue;
        strOutput += end;
    }
    return strOutput;
}

function getDescriptionY(item, nMaxLen) {
    if( nMaxLen == 0)
    {
        return '';
    }
    var begin;
    var end;
    var strOutput = '';
    begin = '<SPAN class="hpb-affiliate-description">';
    end = '</SPAN>';
    var strValue='';
    if(item!=null){
        if(item.Description!=null && item.Description!=''){
            strValue = item.Description;
        }
    }

    strOutput = begin;
    if( strValue != '')
    {
        var strTemp = '';
        strTemp = strValue;
        if( strTemp.length > nMaxLen) {
            strTemp	= strTemp.substr( 0, nMaxLen);
            strTemp += '...';
        }
        strOutput +=  strTemp;
    }
    strOutput += end;
    return strOutput;
}

function getText(node) {
    if( node == null)
        return '';
    if (typeof node.textContent != 'undefined') {
        return node.textContent;
    }
    else if (typeof node.innerText != 'undefined') {
        return node.innerText;
    }
    else if (typeof node.text != 'undefined') {
        return node.text;
    }
    else {
        switch (node.nodeType) {
        case 3:
        case 4:
            return node.nodeValue;
            break;
        case 1:
        case 11:
            var innerText = '';
            for (var i = 0; i < node.childNodes.length; i++) {
                innerText += getText(node.childNodes[i]);
            }
            return innerText;
            break;
        default:
            return '';
        }
    }
}

function getJLinkY(item)
{
    var strValue = item.Url;
    if( strValue != '')
    {
        var strOutput = ' onclick="hpbGoTo(';
        strOutput += "'";
        strOutput += strValue;
        strOutput += "'";
        strOutput += ')"';
        return strOutput;
    }
    return "";
}

function getItemY(nItem, item, bHorizontal, imageType, imageFormat, nDesMaxLen, nItemWidth, bPrice, bShop){
  var strItem;
  var itemBegin;
  var itemMid;
  var itemEnd;
  var strWidth = '';
  if(nItemWidth > 0)
      strWidth = ' width="'+ nItemWidth +'"';

  var strOutput;
  if( bHorizontal) {
     itemBegin = '<TD valign="top"' + strWidth ;
     itemMid = '><DIV class="hpb-affiliate-block" style="cursor : pointer;">';
     itemEnd = '</DIV></TD>';
  } else {
     itemBegin = '<TR><TD' + strWidth;
     itemMid = '><DIV class="hpb-affiliate-block" style="height : 100px;cursor : pointer;">';
     itemEnd = '</DIV></TD></TR>';
  }
  
  strItem = itemBegin;
  strItem += getJLinkY( item);
  strItem += itemMid;
  strItem += getNameY(item);
  strItem += getImageY( imageFormat, item);
  if(bPrice){
    strItem += getPriceY(item);
  }
  if(bShop){
    strItem += getShopY(item);
  }
  strItem += getDescriptionY(item, nDesMaxLen);
  strItem += itemEnd;
  return strItem;
}

function getCreditY(bHorizontal, imageFormat) {
    var strOutput;

    if(imageFormat == 'large') {
        if(bHorizontal) {
            strOutput = '<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --><a href="http://developer.yahoo.co.jp/about"><img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn2_105_17.gif" width="105" height="17" title="Webサービス by Yahoo! JAPAN" alt="Webサービス by Yahoo! JAPAN" border="0" style="margin:15px 15px 15px 15px"></a><!-- End Yahoo! JAPAN Web Services Attribution Snippet -->';
        } else {
            strOutput = '<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --><a href="http://developer.yahoo.co.jp/about"><img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn2_88_35.gif" width="88" height="35" title="Webサービス by Yahoo! JAPAN" alt="Webサービス by Yahoo! JAPAN" border="0" style="margin:15px 15px 15px 15px"></a><!-- End Yahoo! JAPAN Web Services Attribution Snippet -->';
        }
    } else if(imageFormat == 'small') {
        if(bHorizontal) {
            strOutput = '<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --><a href="http://developer.yahoo.co.jp/about"><img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn2_105_17.gif" width="105" height="17" title="Webサービス by Yahoo! JAPAN" alt="Webサービス by Yahoo! JAPAN" border="0" style="margin:15px 15px 15px 15px"></a><!-- End Yahoo! JAPAN Web Services Attribution Snippet -->';
        } else {
            strOutput = '<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --><a href="http://developer.yahoo.co.jp/about"><img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn2_88_35.gif" width="88" height="35" title="Webサービス by Yahoo! JAPAN" alt="Webサービス by Yahoo! JAPAN" border="0" style="margin:15px 15px 15px 15px"></a><!-- End Yahoo! JAPAN Web Services Attribution Snippet -->';
        }
    } else {
        strOutput = '<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --><span style="margin:15px 15px 15px 15px"><a href="http://developer.yahoo.co.jp/about">Webサービス by Yahoo! JAPAN</a></span><!-- End Yahoo! JAPAN Web Services Attribution Snippet -->';
    }

    return strOutput;
}

function getStatus(xmlData)
{
    var numberStatusMsgs = xmlData.getElementsByTagName("Status");
    var strStatusMsg = numberStatusMsgs.length > 0 ? numberStatusMsgs[0] : null;
    var strsStatus = getText(strStatusMsg);
    return strsStatus;
}

function getErrorMsg(xmlData)
{
    var numberStatusMsgs = xmlData.getElementsByTagName("StatusMsg");
    var strStatusMsg = numberStatusMsgs.length > 0 ? numberStatusMsgs[0] : null;
    var strMsg = getText(strStatusMsg);
    var strStatus = getStatus(xmlData)
    strMsg = strStatus +' :' +strMsg;
    return strMsg;
}

function showTableY(partId, result){
// callback sub function for JSONP 
    try
    {
        var form = document.getElementById(partId);
        if( form == null) return;
        var bHorizontal = true;
        if( form.productformat.value == 'vertical') bHorizontal = false;
        var bMedium = true;
 
        var affiliate_type = form.affiliate_type.value;
        var retData = null;

        if(affiliate_type == 'yid') {
            retData = result.ResultSet;
        }

        if (retData == null) return;

        var numberItems = result.ResultSet[0].Result;
        var len = form.number.value;

        if( numberItems==null || numberItems[0] == null || result.ResultSet[0] == null) {
            if(this.div!=null){
              this.div.innerHTML = '見つかりませんでした。';
            }
            nPage = 1;
            form.page.value = nPage;
            if( form.updatinginterval.value > 0)
            {
              var nCount = form.updatinginterval.value;
              self.setTimeout('OnRefreshY(\"'+partId+'\")', 1000 * nCount);
            }
            return;
        }
        var tableBegin;
        var tableEnd;
        var strOutput;

        if( bHorizontal) {
            tableBegin = '<TABLE border="0"><TBODY><TR>';
            tableEnd = '</TR></TBODY></TABLE>';
        } else {
            tableBegin = '<TABLE border="0"><TBODY>';
            tableEnd = '</TBODY></TABLE>';
        }

        var bPrice = false;
        var bShop = false;
        if(form.showPrice != null && form.showPrice.value=='1'){
             bPrice = true;
        }
        if(form.showShop != null && form.showShop.value=='1'){
             bShop = true;
        }
        strOutput = tableBegin;
        for (i=0; i<len; i++) {
            if(numberItems[i] == null){
              break;
            }
            strOutput += getItemY( i, numberItems[i]
                , bHorizontal
                , form.imagetype.value
                , form.imageformat.value
                , form.descriptionmaximum.value
                , form.itemwidth.value
                , bPrice
                , bShop);
        }
        strOutput += tableEnd;

        if(form.showCredit.value == 'true')
            strOutput += getCreditY(bHorizontal, form.imageformat.value);
		
        var partdiv=document.getElementById(partId + "_div");
        partdiv.innerHTML = strOutput;
		
        var nPage = Math.ceil(eval(retData.firstResultPosition / retData.totalResultsReturned)) + 1;
        var nTotalPage = Math.ceil(eval(retData.totalResultsAvailable / retData.totalResultsReturned));
        if(nPage > nTotalPage || nPage > 100) {
            nPage = 1;
        }
        form.page.value = nPage;

        if( form.updatinginterval.value > 0)
        {
            var nCount = form.updatinginterval.value;
            self.setTimeout('OnRefreshY(\"'+partId+'\")', 1000 * nCount);
        }
        return;
    }
    catch(e)
    {
        var partdiv=document.getElementById(partId + "_div");
        if(partdiv!=null && partdiv.div!=null){
           partdiv.div.innerHTML = e;
        }
        alert(e);
    }
}

function hpbGoTo(strURL) {
    if (strURL != "" ) {
        document.location.href = strURL;
    }
};


function affeliateItemsY(div, appId, affeId, NumOfshowItems, affiliate_type, query, bHorizontal, imageType, bMediumImage, strSort, strMinPrice, strMaxPrice, strCategory, strAvailability, strDiscount, strShipping, strPrice, strShop, nDesMaxLen, callBackFuncName, nUpdatingInterval, nPage) {
    div.innerHTML = "検索...";
	
    new hpbY.ContentLoader(div, appId, affeId, NumOfshowItems, affiliate_type, query, bHorizontal, imageType, bMediumImage, strSort, strMinPrice, strMaxPrice, strCategory, strAvailability, strDiscount, strShipping, strPrice, strShop, nDesMaxLen, callBackFuncName, nUpdatingInterval, nPage);
}

function OnSearchButtonY(strFormName) {
    var form = document.getElementById(strFormName);
    if( form == null
        || form.page == null
        || form.query == null
        || form.query.value == ""
        || form.thread == null)
    { 
        return;
    }
    form.page.value = 1;  // reset to start page
    form.thread.value = eval(form.thread.value) + 1; // count up thread number
    doSearchNextY(strFormName);

    return false;
}

function OnRefreshY(strFormName){
    // process for timer refresh

    var form = document.getElementById(strFormName);
    if( form == null || form.thread == null) returh;

    if(form.thread.value > 1) { // stop thread over than one thread
       form.thread.value = eval(form.thread.value) -1;
       return;
    }
    doSearchNextY(strFormName);
}

function doSearchNextY(strFormName){
    var form = document.getElementById(strFormName);
    if( form == null) return;
    if( form.appid == null
        || form.affiliate_id == null
        || form.number == null
        || form.affiliate_type == null
        || form.query == null
        || form.productformat == null
        || form.imageformat == null
        || form.imagetype == null
        || form.sort == null
        || form.descriptionmaximum == null
        || form.updatinginterval == null
        || form.page == null
        )
    {
        alert('アフィリエイト部品の設定が正しくありません');
        return;
    }

    var div=document.getElementById(strFormName + "_div");
    if( div == null)
    {
        div = document.createElement("div");
        div.setAttribute("id", strFormName + "_div");
        form.appendChild(div);
    }

    var callBackName = "showTable" + strFormName;

    var strMinPrice = "";
    var strMaxPrice = "";
    var categoryId = "";
    var sAvailability = "";
    var sDiscount = "";
    var sShipping = "";
    var sPrice = "";
    var sShop = "";
    if(form.showShop != null){
        sShop = form.showShop.value;
    }
    if(form.showPrice != null){
        sPrice = form.showPrice.value;
    }
    if(form.availability != null){
        sAvailability = form.availability.value;
    }
    if(form.discount != null){
        sDiscount = form.discount.value;
    }
    if(form.shipping != null){
        sShipping = form.shipping.value;
    }
    if(form.category_id != null){
        categoryId = form.category_id.value;
    }
    if(form.price_to != null){
        strMaxPrice = form.price_to.value;
    }
    if(form.price_from != null){
        strMinPrice = form.price_from.value;
    }
    if(form.price_to != null){
        strMaxPrice = form.price_to.value;
    }
    if(form.category_id != null){
        categoryId = form.category_id.value;
    }
    if(form.showShop != null){
        sShop = form.showShop.value;
    }
    if(form.showPrice != null){
        sPrice = form.showPrice.value;
    }
    if(form.availability != null){
        sAvailability = form.availability.value;
    }
    if(form.discount != null){
        sDiscount = form.discount.value;
    }
    if(form.shipping != null){
        sShipping = form.shipping.value;
    }

    affeliateItemsY( div
        , form.appid.value
        , form.affiliate_id.value
        , form.number.value
        , form.affiliate_type.value
        , form.query.value
        , form.productformat.value
        , form.imageformat.value
        , form.imagetype.value
        , form.sort.value
        , strMinPrice
        , strMaxPrice
        , categoryId
        , sAvailability
        , sDiscount
        , sShipping
        , sPrice
        , sShop
        , form.descriptionmaximum.value
        , callBackName
        , form.updatinginterval.value
        , form.page.value);
}
