function chave_debug(obj){
    var w = window.open('/');
    var A = new Array;

    for(var i in obj) A.push(i);
    A.sort();
    w.document.write("<table border style=\"font: 11px 'ＭＳ ゴシック';\">\n");
    for(var i in A){
        w.document.write("<tr><td>" + A[i] + "</td>");
        w.document.write("<td>" + obj[A[i]] + "</td></tr>\n");
    }
    w.document.write("</table>\n");
}

function chave_action(obj){
    if(CHAVE_LOCK) return false;
    var f = obj.form;
    chave_afocus(obj.getAttribute('clk') ? obj : f);
    var _act = f._action;
    var act = obj.getAttribute('action');

    if(act) _act = act.match(/^(http|\/)/) ? act : _act.replace(/[^\/]*$/,act);
    f.action = chave_check_act(_act);
    var tgt = obj.getAttribute('target');
    if(tgt){
        f.target = tgt;
        f.nolock = 1;
    }else{
        f.target = f._target;
        f.nolock = obj.getAttribute('nolock');
    }
}

function chave_check_act(act){
    if(act == '') return act;
    var reg = '^(https?://' + document.domain.replace(/\./g,'\\.').replace(/\/$/,'') + '(:[0-9]+)?)?/';

    if(act.match(reg)){
        return act;
    }else{
        return 'http://' + document.domain + '/';
    }
}

function chave_afocus(obj){
    var clk = obj.getAttribute('clk');

    if(clk == undefined) clk = '';
    document.cookie = 'mem_clk=' + clk + '; path=/;';

    var cnt = '';
    var no = '';
    var fw = '';
    if(obj.getAttribute('no')){
        cnt = (document.getElementsByName('mem_sr_cnt'))[0].value;
        no  = (((document.getElementsByName('page_no'))[0].value - 1) * 20) + parseInt(obj.getAttribute('no'));
        fw  = (document.getElementsByName('mem_sr_fw'))[0].value;
    }
    document.cookie = 'mem_sr_cnt=' + cnt + '; path=/;';
    document.cookie = 'mem_sr_no='  + no  + '; path=/;';
    document.cookie = 'mem_sr_fw='  + fw  + '; path=/;';
}

function chave_clr(f){
    if(f) return chave__clr(f);
    for(var i = 0; i < document.forms.length; i++) chave__clr(document.forms[i]);
}

function chave__clr(f){
    for(var i = 0; i < f.elements.length; i++){
        var e = f.elements[i];
        if(e.type == 'text' || e.tagName == 'TEXTAREA' || e.tagName == 'SELECT' || e.type == 'password'){
            e.value = '';
        }else if(e.type == 'checkbox' || e.type == 'radio'){
            e.checked = false;
        }else if(e.type == 'file'){
            e.outerHTML = e.outerHTML;
        }
    }
}

function chave_img_off(){this.src = this.src.replace(/\_on.gif$/,'.gif');}

function chave_img_on(){this.src = this.src.replace(/\.gif$/,'_on.gif');}

var CHAVE_IMG_ON = {};
function chave_img_swap(e){
    e.onmouseover = chave_img_on;
    e.onmouseout  = chave_img_off;
    var src = e.src.replace(/\.gif$/,'_on.gif');
    if(!CHAVE_IMG_ON[src]){
        var i = CHAVE_IMG_ON[src] = new Image();
        i.src = src;
    }
}

var CHAVE_ACTION = function(){chave_action(this);}
var CHAVE_AFOCUS = function(){chave_afocus(this);}
var CHAVE_NVL = [];
var CHAVE_NVL_ON = function(){if(this.value == this.getAttribute('nvl')){this.value = '';}this.style.color = '#333333';}
var CHAVE_NVL_OFF = function(){if(this.value == ''){this.value = this.getAttribute('nvl');this.style.color = '#333333';}}
var CHAVE_WFOCUS = function(){
    document.cookie = 'mem_pre=' + CHAVE_G.p_id + '; path=/;';
    document.cookie = 'mem_clk=; path=/;';
    document.cookie = 'mem_sr_cnt=; path=/;';
    document.cookie = 'mem_sr_no=; path=/;';
    document.cookie = 'mem_sr_fw=; path=/;';
}
var CHAVE_LOCK_BTN = [];
var CHAVE_LOCK = 1;
var CHAVE_NAVI = '';
var CHAVE_G = {};
var CHAVE_MAINNAVI_S_PID;

function chave_init(navi,pid,myarea_open_flg,flg){
    if(!CHAVE_NAVI){
        if(!window.name || window.name == '') window.name = 'main';
        CHAVE_G.p_id = pid;
        CHAVE_G.nick_nm_flg = flg;
        if(pid.match(/^[0-9]{8}$/)){
            window.onfocus = function(){setTimeout('CHAVE_WFOCUS()',500)}
            window.onfocus();
            //MYエリア開く
            if(myarea_open_flg == '1') chave_myarea_disp(1);
        }

        //Form設定
        for(var i = 0;i < document.forms.length;i++){
            var f = document.forms[i];
            if(!(f._action = f.action)){
                var act = document.location + '';
                f._action = act.replace(/^https?:\/\/[^\/]+/,'');
            }else{
                f.action = chave_check_act(f.action);
            }
            f._target = f.target;
        }
        var E = document.getElementsByTagName('INPUT');
        for(var i = 0;i < E.length;i++){
            var e = E[i];
            if(e.type == 'text' || e.type == 'password' || e.type == 'file'){
                if(!e.onkeypress) e.onkeypress = function(e){return chave_noenter(e || window.event)};
                if(e.getAttribute('nvl')) CHAVE_NVL.push(e);

            }else if(e.type == 'submit' || e.type == 'image'){
                if(!e.disabled) CHAVE_LOCK_BTN.push(e);
                if(!e.onclick) e.onclick = CHAVE_ACTION;
                if(e.type == 'image' && e.getAttribute('swap')) chave_img_swap(e);
            }else if(e.type == 'button' || e.type == 'reset'){
                if(!e.disabled) CHAVE_LOCK_BTN.push(e);
            }
        }
        var E = document.getElementsByTagName('TEXTAREA');
        for(var i = 0;i < E.length;i++){
            if(E[i].getAttribute('nvl')) CHAVE_NVL.push(E[i]);
        }
        var E = document.getElementsByTagName('SELECT');
        for(var i = 0;i < E.length;i++){
            if(E[i].getAttribute('rainbow')){
                var OPT = E[i].options;
                for(var j = 1; j < OPT.length;j++) OPT[j].className = 'rainbow' + OPT[j].value;
            };
        }

        //リンク
        var A = document.getElementsByTagName('A');
        for(var i = 0;i < A.length;i++) A[i].onfocus = CHAVE_AFOCUS;
        A = document.getElementsByTagName('AREA');
        for(var i = 0;i < A.length;i++) A[i].onfocus = CHAVE_AFOCUS;

        //画像swap
        for(var i = 0;i < document.images.length;i++){
            var img = document.images[i];
            if(img.getAttribute('swap')) chave_img_swap(img);
        }

        //NVL
        for(var i = 0;i < CHAVE_NVL.length;i++){
            var e = CHAVE_NVL[i];
            e.onfocus = CHAVE_NVL_ON;
            e.onblur = CHAVE_NVL_OFF;
            e.onblur();
            //setTimeout('var e = document.all[' + e.sourceIndex + '];e.onfocus();e.onblur();',1000);
        }
        if(!document.body.getAttribute('nofocus')) window.focus();
        CHAVE_NAVI = navi;
    }
    chave_lock(0);
}

function chave_lock(bool){
    CHAVE_LOCK = bool;
    for(var i = 0; i < CHAVE_LOCK_BTN.length; i++){
        CHAVE_LOCK_BTN[i].style.color = bool ? '#EEEEEE' : 'black';
    }
}

//TOP swf
function chave_mainnavi_click(url,target){
    if(url == '#') return;
    document.cookie = 'mem_clk=mainnavi; path=/;';
    url = url.replace(/^\/3w\/prd/,CHAVE_NAVI);
    if(CHAVE_MAINNAVI_S_PID) url = url.replace(/--------/,CHAVE_MAINNAVI_S_PID);
    if(target){
        window.open(url,target);
    }else{
        location.href = url;
    }
}

function chave_noenter(e){return(e.keyCode != 13)}

function chave_now(){
    var now = new Date;
    var Y = 1900 + (now.getYear() % 1900);
    var M = now.getMonth() + 1;
    if(M < 10) M = '0' + M;
    var D = now.getDate();
    if(D < 10) D = '0' + D;
    var h = now.getHours();
    if(h < 10) h = '0' + h;
    var m = now.getMinutes();
    if(m < 10) m = '0' + m;
    var s = now.getSeconds();
    if(s < 10) s = '0' + s;

    return '' + Y + M + D + h + m + s;
}

function chave_onsubmit(f){
    if(!CHAVE_NAVI) return false;
    if(!f.nolock) setTimeout('chave_lock(1)',1);
    for(var i = 0;i < CHAVE_NVL.length;i++) CHAVE_NVL[i].onfocus();
    return true;
}

function chave_mylist(){
    var $_ = document.getElementsByName('btn_mylist');

    $_[1].src = CHAVE_NAVI + '/00/00006100/?p_id=' + CHAVE_G.p_id + '&now=' + chave_now();
    $_[0].src = '/3w/navi/img/act/btn_mylisthetuika_off.gif';
    $_[0].onclick = $_[1].onclick = $_[0].onmouseover = $_[1].onmouseover = null;
}

function chave_myarea_disp(flg){
    var i = document.getElementById('mylistIcon');
    var b = document.getElementById('mylistOpen');
    //var pb = document.getElementById('mylistBottom');
    var gif = '_on.gif';

    if(!i){
        return;
    }else if(flg || i.alt == 'Myエリアを開く'){
        b.style.display = 'block';
        i.src = '/images/myarea/icon-close' + gif;
        i.alt = 'Myエリアを閉じる';

        //pb.src = '/images/myarea/panel_bottom2.gif';
        //pb.height = '13'
    }else if(i.alt == 'Myエリアを閉じる'){
        b.style.display = 'none';
        i.src = '/images/myarea/icon-open' + gif;
        i.alt = 'Myエリアを開く';

        //pb.src = '/images/myarea/panel_bottom.gif';
        //pb.height = '16'
    }

    CookieWrite("Cart_State",i.alt,7,"/");

}

//クッキーを書き込む
function CookieWrite(kword, kdata, kday, path)
{
  if(!navigator.cookieEnabled){    // クッキーが利用可能かどうか
    alert("クッキーへの書き込みができません");
    return;
  }
  if(path==null){
      path="";
  }
  sday = new Date();
  sday.setTime(sday.getTime() + (kday * 1000 * 60 * 60 * 24));
  s2day = sday.toGMTString();
  document.cookie = kword + "=" + escape(kdata) + ";expires=" + s2day + ";path=" + path;
}

//クッキーを読み込む
function CookieRead(kword)
{
  if(typeof(kword) == "undefined")  // キーワードなし
    return "";        // 何もしないで戻る
  kword = kword + "=";
  kdata = "";
  scookie = document.cookie + ";";    // クッキー情報を読み込む
  start = scookie.indexOf(kword);    // キーワードを検索
  if (start != -1){    // キーワードと一致するものあり
    end = scookie.indexOf(";", start);    // 情報の末尾位置を検索
    kdata = unescape(scookie.substring(start + kword.length, end));  // データ取り出し
  }
  return kdata;
}
