/*
 *  copyright(c) activecore,Inc. 2005-2011
 * 
 *  This software is licensed by activecore, Inc.
 *  You CAN NOT use this file except in compliance with the License.
 *
 *  Product  : ac propoza
 *  Version  : 1.1 
 *  REV      : 3
 *  Last Update: 2011/02/02
 *
 *  Modification History
 *  ----------  -----------------------------------------------------
 *  2010/05/26  REV1 add cat02/cat03
 *  2010/07/15  REV2 add contents_id/seg_id/_contents_click
 *  2010/02/02  REV3 add ac_clip parameter to ppz request
 */
function _PPZ_encode(str){
    var s0, i, s, u;
    s0 = "";                
    for (i = 0; i < str.length; i++){  
        s = str.charAt(i);
        u = str.charCodeAt(i);          
        if (s == " "){s0 += "+";}    
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f 
                || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) 
                || ((u >= 0x61) && (u <= 0x7a))){   
                s0 = s0 + s;            
            }
            else {                  
                if ((u >= 0x0) && (u <= 0x7f)){     
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){     
                    s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else if (u > 0x7ff){        
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {                     
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}

function _PPZ() {

   this.ppz_host='ppz01.a-cast.jp';
   this.auth='ORh6mJsxD4nQf';
   this.api_pw='';
   this.charset='UTF-8';
   this.ac='';
   this.cid=0;
   this.rid=0;
   this.level='1';
   this.price='n';
   this.stock='n';
   this.point='n';
   this.type='p';
   this.div_id = '';
   this.rows=10;
   this.item_id='';
   this.cat='';
   this.cat2='';
   this.cat3='';
   this.format='json';
   this.expire_time=3600;
   this.cb='ppz_draw';
   this.ref='';
   this.customer_id='';
   this.exclude_order_history='';
   this.alt_html=null;
   this.clickProcess=false;
   this.timeout=10*1000;
   this.click_interval=500;
   
   // this.c_contents_id='';
   this.c_item_id='';	
   this.c_name='';
   this.c_cat01='';
   this.c_status='';
   this.c_sales_date='';
   this.c_title='';	
   this.c_desc='';
   this.c_image_url='';
   this.c_image_alt='';
   this.c_link_url='';
   this.c_cart_url='';	
   this.c_price='';
   this.c_real_price='';
   this.c_stock_cnt='';
   this.c_point='';	
   this.c_point_rate='';
   
   this.contents_id='';
   this.seg_id='';
   
}

_PPZ.prototype = {

   _getCookie: function() {
      var cn =  arguments[0];
      var get_data = document.cookie;
      var cv = new Array();
      var gd = get_data.split(";");
      var i;
      for (i = 0; i < gd.length; i++) { 
         var a = gd[i].split("=");
         a[0] = a[0].replace(" ","");
         cv[a[0]] = a[1];
      }
      if (cv[cn]) return cv[cn];
      else return "";
   }
   ,
   _setCookie: function() {
      var cn = arguments[0];
      var val = arguments[1];
      document.cookie = cn + "=" + val + "; path=/; expires=Thu, 1-Jan-2030 00:00:00 GMT;";
   }
   ,
   _fillZero: function() {
      var digits = 5;
      var s = arguments[0];
      if (s == null || s == undefined) {
         return s;
      }
      return Array((digits + 1) - s.toString().split('').length).join('0') + s;
   }
   ,
   _click: function() {
      var _now = new Date();
      var _linkUrl = arguments[0];
      var _proto = 'http:';
      if (window.location.protocol == 'https:') {
         _proto = 'https:';
      }  
      var _dir = '/c' + this._fillZero(this.cid);
      var _img = new Image();
      _img.src = _proto + '//' + this.ppz_host + _dir + '/click.gif?' 
              + 'cid=' + this.cid  
              + '&rid=' + this.rid 
              + '&item_id=' + arguments[1] 
              + '&host=' + escape(window.location)
              + '&ac=' + this.ac
              + '&type=' + this.type
              + '&auth=' + this.auth	
              + '&rand=' + Math.round(Math.random() * _now.getTime())
              ;
       // alert(_img.src);       
       var _isError = false;
       var _isOnload = false;       
       _img.onerror = function(){ 
          _isError = true; 
       };
       _img.onload = function(){ 
          _isOnload = true; 
       };
       if (_img.complete == true) {
          location.href = _linkUrl;
          return;
       }
       var _timerId = setInterval(function(){
          if (_img.complete == true || _isOnload == true || _isError == true) {
             clearInterval(_timerId);
             location.href = _linkUrl;
          }
       },this.click_interval);
       
       return;
   }
   ,
     _contents_click: function() {
      var _now = new Date();
      var _linkUrl = arguments[0];
      var _proto = 'http:';
      if (window.location.protocol == 'https:') {
         _proto = 'https:';
      }  
      var _dir = '/c' + this._fillZero(this.cid);
      var _img = new Image();
      _img.src = _proto + '//' + this.ppz_host + _dir + '/click.gif?' 
              + 'cid=' + this.cid  
              + '&rid=' + this.rid 
              + '&contents_id=' + arguments[1] 
              + '&host=' + escape(window.location)
              + '&ac=' + this.ac
              + '&type=' + this.type
              + '&auth=' + this.auth	
              + '&rand=' + Math.round(Math.random() * _now.getTime())
              ;
       // alert(_img.src);       
       var _isError = false;
       var _isOnload = false;       
       _img.onerror = function(){ 
          _isError = true; 
       };
       _img.onload = function(){ 
          _isOnload = true; 
       };
       if (_img.complete == true) {
          location.href = _linkUrl;
          return;
       }
       var _timerId = setInterval(function(){
          if (_img.complete == true || _isOnload == true || _isError == true) {
             clearInterval(_timerId);
             location.href = _linkUrl;
          }
       },this.click_interval);
       
       return;
   }
   
}

_PPZ.prototype.timeoutHandler = function() {
   // alert(this.timerId);
   clearInterval(this.timerId);
   var _id = document.getElementById("PPZ_RID" + this.rid);
   if (_id == null) {
     var _div = document.getElementById(this.div_id);
     if (_div != null && this.alt_html != null) {
        _div.innerHTML = this.alt_html;
     }
   }
}


_PPZ.prototype.request = function() {
   
   var _now = new Date();
   if (this.ac == '') {
      this.ac = this._getCookie('ac');
   }
   // var _ac_ppz = this._getCookie('ac_ppz');
   var _proto = 'http:';
   if (window.location.protocol == 'https:') {
      _proto = 'https:';
   }  
   var _src_proto = 's' + 'rc="' + _proto;
   var _dir = '/c' + this._fillZero(this.cid);
   var _ua = navigator.userAgent;
   if (_ua == '') { _ua = 'N/A'; } else { _ua = escape(_ua); }
   
   var ref=escape(document.referrer);
   try {
       if( parent != self ){
          ref=escape(top.document.referrer);
       }
   } catch(e) {
       // this.ref = escape(document.referrer);
   }
   
   var _rid = this.rid;
   var _div_id = this.div_id;
   var _alt_html = this.alt_html
   var _timerId = setInterval(function(){
          clearInterval(_timerId);
          var _id = document.getElementById("PPZ_RID" + _rid);
          if (_id == null) {
             var _div = document.getElementById(_div_id);
             if (_div != null && _alt_html != null) {
                _div.innerHTML = _alt_html;
             }
          }
   },this.timeout);
   
   var _c_items = '';
   //if ((this.c_contents_id != '' && this.c_contents_id != undefined) || (this.c_item_id != '' && this.c_item_id != undefined)) {
   //   _c_items += '&c_contents_id=' + this.c_contents_id;
   if (this.c_item_id != '' && this.c_item_id != undefined) {
      _c_items += '&';
      _c_items += _PPZ_makeParamValue('c_item_id', this.c_item_id);
      _c_items += _PPZ_makeParamValue('c_name', this.c_name);
      _c_items += _PPZ_makeParamValue('c_cat01', this.c_cat01);
      _c_items += _PPZ_makeParamValue('c_status', this.c_status);
      _c_items += _PPZ_makeParamValue('c_sales_date', this.c_sales_date);
      _c_items += _PPZ_makeParamValue('c_title', this.c_title);
      _c_items += _PPZ_makeParamValue('c_desc', this.c_desc);
      _c_items += _PPZ_makeParamValue('c_image_url', this.c_image_url);
      _c_items += _PPZ_makeParamValue('c_image_alt', this.c_image_alt);
      _c_items += _PPZ_makeParamValue('c_link_url', this.c_link_url);
      _c_items += _PPZ_makeParamValue('c_cart_url', this.c_cart_url);
      _c_items += _PPZ_makeParamValue('c_price', this.c_price);
      _c_items += _PPZ_makeParamValue('c_real_price', this.c_real_price);
      _c_items += _PPZ_makeParamValue('c_stock_cnt', this.c_stock_cnt);
      _c_items += _PPZ_makeParamValue('c_point', this.c_point);
      _c_items += _PPZ_makeParamValue('c_point_rate', this.c_point_rate);
   }
   // REV3
   var _clip = this._getCookie('ac_clip');
   var _url = '<scr' + 'ipt type="text/jav' + 'ascript" ' 
      + 'language="JavaScript" ' + _src_proto + '//' + this.ppz_host + _dir + '/query?'
      + 'cid=' + this.cid
      + '&charset=' + this.charset
      + '&rid=' + this.rid
      + '&rows=' + this.rows
      + '&host=' + escape(window.location)
      + '&item_id=' + this.item_id
      + '&contents_id=' + _PPZ_encode(this.contents_id) 
      + '&seg_id=' + this.seg_id
      + '&cat=' + this.cat
      + '&cat2=' + this.cat2
      + '&cat3=' + this.cat3
      + '&type=' + this.type
      + '&cb=' + this.cb
      + '&ac=' + this.ac
      + '&format=' + this.format
      + '&expire_time=' + this.expire_time
      + '&ua=' + _ua
      + '&level=' + this.level
      + '&price=' + this.price
      + '&stock=' + this.stock
      + '&point=' + this.point
      + '&div_id=' + this.div_id
      + '&auth=' + this.auth
      + '&timer_id=' + _timerId
      + '&customer_id=' + this.customer_id
      + '&exclude_order_history=' + this.exclude_order_history
      + '&referrer=' + ref
      + _c_items
      + '&api_pw=' + this.api_pw
      + '&ac_clip=' + _clip // REV3
      + '&rand=' + Math.round(Math.random() * _now.getTime())
      + '"></' + 'scr' + 'ipt>' 
      ;

   document.write(_url);
   
}

function _PPZ_makeParamValue(name, value) {
	if (value == '' || value == undefined) {
		return '';
	}
	return '&' + name + '=' + _PPZ_encode(value);
}

function _PPZ_formatPrice(_str) {
   var _num = new String(_str).replace(/,/g, "");
   while(_num != (_num = _num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
   return _num;
}

