// アクセスログ取得(拒否URL指定OK)
//   All Rights reserved, Copyright(C)2002-2009 Hiroyuki Murata.
//

  var refurl;
  var refurl2;
  var kwd = "";
  var kyohi = 0;
  var url = "";
  var chkword = "";
  var title = "";

// アクセスログを記録するURLを確認

  url = "&X1="+document.URL;

// ページのタイトルを確認

  title = "&X2="+encodeURL(document.title);

// フレーム内か否かでのreferrerの違いを吸収

  refurl = document.referrer;

// "?"はCGIの引数とのデリミタなので"~"に変換

  refurl2 = refurl.replace("?","~");

// "&"をデリミタとして文字列分割
  check1 = refurl2.split("&");

  googleimg = 0;

// 2個以上に分割できれば、"&"を含む
// 検索エンジンからのアクセスの可能性が高い
  if (check1.length > 1) {

// 検索エンジン判定 はじまり
//   nifty？ "Text="を探す
    if (check1[0].match(/search.nifty.com/i)) {
        chkword="Text=";
//	もし"Text="が見当たらない時は"q="に変更する ★
        if (refurl2.indexOf(chkword) < 0) {
            chkword="q=";
        }

//      もし画像検索なら"imgurl,prev="を探す
//                       prevはさらにq=xxxxに展開できる
        if (check1[0].match(/imagesearch/i)) {
            chkword="imgurl=";
            chkword2="prev=";
            chkword3="q=";
            googleimg = 1;
        }
    }

//   yahoo？ "p="を探す
    if (check1[0].match(/search.yahoo.co.jp/i)) {
        chkword="p=";
    }

//   biglobe？ "q="を探す
    if (check1[0].match(/search.biglobe.ne.jp/i)) {
        chkword="q=";
    }

//   excite？ "search="を探す
    if (check1[0].match(/excite.co.jp/i)) {
        chkword="search=";
    }

//   gooleの画像？ "imgurl,prev="を探す ★全部
//                 prevはさらにq=xxxxに展開できる
//    if (check1[0].match(/images.google./i)) {
//        chkword="imgurl=";
//        chkword2="prev=";
//        chkword3="q=";
//        googleimg = 1;
//    }

//   goo？ "MT="を探す
    if (check1[0].match(/search.goo.ne.jp/i)) {
        chkword="MT=";
    }

//   livedoor？ "q="を探す
    if (check1[0].match(/search.livedoor.com/i)) {
        chkword="q=";
    }

//   msn？ "q="を探す
    if (check1[0].match(/search.msn.co.jp/i)) {
        chkword="q=";
    }

//   infosheek？ "qt="を探す
    if (check1[0].match(/search.www.infoseek.co.jp/i)) {
        chkword="qt=";
    }

//   rakuten？ "qt="を探す ★
    if (check1[0].match(/websearch.rakuten.co.jp/i)) {
        chkword="qt=";
    }

//   goole？ "q="を探す ★
    if (check1[0].match(/.google./i)) {
        chkword="q=";

//      もし画像検索なら"imgurl,prev="を探す ★
//                       prevはさらにq=xxxxに展開できる ★
        if (check1[0].match(/imgurl/i)) {
            chkword="imgurl=";
            chkword2="prev=";
            chkword3="q=";
            googleimg = 1;
        }
    }

//   luna？ "q="を探す
    if (check1[0].match(/luna.tv/i)) {
        chkword="q=";
    }

//   gendama？ "Keywords="を探す
    if (check1[0].match(/search.gendama.jp/i)) {
        chkword="Keywords=";
    }

//   ecnavi？ "Keywords="を探す
    if (check1[0].match(/point.ecnavi.jp/i)) {
        chkword="Keywords=";
    }
// 検索エンジン判定 ここまで

    if (chkword == "") {
//    検索エンジン以外の時の検索ワードサーチ
      point = 0;

      for (i = 0; i < check1.length; i++) {
//      キーワードは何番目にあるかはわからないので
//      エンコードされている情報を探してみる
//      ※エンコードされている情報は先頭文字は"%"である

//      "="をデリミタとして、文字列を分割する
        check2 = check1[i].split("=");

        if (check2[1].charAt(0) == '%') {
//        エンコードされた文字列？

//        文字列がヒットしたポイントを記憶する
          point = i;
//        検索ワードを引き渡す。
          kwd = "&KWD="+check2[1];

//        文字列サーチのループから脱出
          break;
        }
      }

//   検索エンジンからのアクセスであれば、キーワードを示す
//   文字列を探す
    } else {
      point = 0;

      for (i = 0; i < check1.length; i++) {
//      サーチ文字列の位置を取得する
        if (i == 0) {
//        先頭には検索サイトのURLが含まれているので、"~"をデリミタと
//        して文字列を再分割
          check3 = check1[0].split("~");

//        サーチ文字列の位置を取得する
          hitpoint = check3[1].indexOf(chkword); 
        } else {

//        サーチ文字列の位置を取得する
          hitpoint = check1[i].indexOf(chkword); 
        }

//      サーチ文字列があった＆先頭カラム以外
//      ※googleには"aq="というキーワードがあり、これを誤認しない
//        ようにするため、先頭カラム以外のヒットは除外
        if (hitpoint == 0) { 
//        文字列がヒットしたポイントを記憶する
          point = i;

//        "="をデリミタとして、検索ワードを求める
          check2 = check1[i].split("=");

//        "="の右側にある検索ワードを引き渡す。
          kwd = "&KWD="+check2[1];

          if (googleimg == 1) {
//          googleの画像検索の時は、元の検索キーワードを調べてみる
            for (j = i + 1; j < check1.length; j++) {
//            後ろの文字列を探してみる
              if (check1[j].indexOf(chkword2) >= 0) {
//              "="をデリミタとして、検索ワードを求める
                check3 = check1[j].split("=");
//              画像検索のための検索ワードをデコードする
                kensaku1 = decodeURL(check3[1]);
//              "?"はCGIの引数とのデリミタなので"~"に変換
                kensaku2 = kensaku1.replace("?","~");
//              "&"をデリミタとして文字列分割
                kensaku3 = kensaku2.split("&");

                for (k = 0; k < kensaku3.length; k++) {
//                引数の数だけループ
                  if (kensaku3[k].indexOf(chkword3) >= 0) {
//                  "q="がある？

//                  "="をデリミタとして、検索ワードを求める
                    kensaku4 = kensaku3[k].split("=");

//                  検索ワードが求まったら、検索ワード+画像のURLを設定する
                    kwd = "&KWD="+kensaku4[1]+"+"+check2[1];

//                  検索ワードサーチのループから脱出
                    break;
                  }
                }

//              文字列サーチのループから脱出
                break;
              }
            }
          }

//         文字列サーチのループから脱出
          break;
        }
      }
    }
//    検索エンジン以外の時の検索ワードサーチ ★
  } else {
      point = 0;

//      キーワードは何番目にあるかはわからないので
//      エンコードされている情報を探してみる
//      ※エンコードされている情報は先頭文字は"%"である

//      "="をデリミタとして、文字列を分割する
        check2 = check1[0].split("=");

        if (check2.length > 1) {
          if (check2[1].charAt(0) == '%') {
//          エンコードされた文字列？
//          検索ワードを引き渡す。
            kwd = "&KWD="+check2[1];
          }
        }
// ★
  }

// 直リンクを拒否するURLを記載
  if (refurl.match(/alphabetpress.english.net/i)) {
    kyohi = 1;
  }
  if (refurl.match(/konyoku.info/i)) {
    kyohi = 1;
  }
  if (refurl.match(/megabbs.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/mako.press.ne.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/hatena.ne.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/www2.sfc-js.keio.ac.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/blockedReferrer/i)) {
    kyohi = 1;
  }
  if (refurl.match(/Hidden-Referrer/i)) {
    kyohi = 1;
  }
  if (refurl.match(/unknownorigin/i)) {
    kyohi = 1;
  }
  if (refurl.match(/konyoku.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/paseri.ne.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/keys-keys.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/www.a5.to/i)) {
    kyohi = 1;
  }
  if (refurl.match(/wadaino.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/alcoholtown.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/onsen55.net/i)) {
    kyohi = 1;
  }
  if (refurl.match(/fhamakko.nifty.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/yusuke-horn.cocolog-nifty.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/mixi.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/mypage.home.ne.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/ime.nu/i)) {
    kyohi = 1;
  }
  if (refurl.match(/2ch.net/i)) {
    kyohi = 1;
  }
  if (refurl.match(/pukiwiki/i)) {
    kyohi = 1;
  }
  if (refurl.match(/date2.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/jp.unweb1.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/www.japaneselink.info/i)) {
    kyohi = 1;
  }
  if (refurl.match(/www.fukaoist.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/onsen.blog1.biz/i)) {
    kyohi = 1;
  }
  if (refurl.match(/www.blog-headline.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/ringocake1234.hp.infoseek.co.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/buzzurl.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/wandara.net/i)) {
    kyohi = 1;
  }
  if (refurl.match(/eroy.8-bit.jp/i)) {
    kyohi = 1;
  }
  if (refurl.match(/172.17.100.70/i)) {
    kyohi = 1;
  }
  if (refurl.match(/fukaois.com/i)) {
    kyohi = 1;
  }
  if (refurl.match(/kanpeki.inucara.net/i)) {
    kyohi = 1;
  }

  if (kyohi == 0) {
// カウンタを表示＆アクセスログ記録
    document.write("<IMG src=\"http://moon.cafe.coocan.jp/cgi-bin/ac.cgi?UN=moon&PA=meisho&DG=K2&DP=6&REF="+check1[0]+kwd+url+title+"\">\n");
  } else {
// 直リンクサイトからはAll Aboutへナビゲート。
    window.open("http://allabout.co.jp/", "_top");
  }

/*  Function Equivalent to java.net.URLEncoder.encode(String, "UTF-8")
    Copyright (C) 2002, Cresc Corp.
    Version: 1.0
*/
function encodeURL(str){
    var s0, i, s, u;
    s0 = "";                // encoded str
    for (i = 0; i < str.length; i++){   // scan the source
        s = str.charAt(i);
        u = str.charCodeAt(i);          // get unicode of the char
        if (s == " "){s0 += "+";}       // SP should be converted to "+"
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
                s0 = s0 + s;            // don't escape
            }
            else {                  // escape
                if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){     // quaternary byte format (extended)
                    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){        // triple byte format
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {                      // double byte format
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}

/*  Function Equivalent to java.net.URLDecoder.decode(String, "UTF-8")
    Copyright (C) 2002, Cresc Corp.
    Version: 1.0
*/

function decodeURL(str){
    var s0, i, j, s, ss, u, n, f;
    s0 = "";                // decoded str
    for (i = 0; i < str.length; i++){   // scan the source str
        s = str.charAt(i);
        if (s == "+"){s0 += " ";}       // "+" should be changed to SP
        else {
            if (s != "%"){s0 += s;}     // add an unescaped char
            else{               // escape sequence decoding
                u = 0;          // unicode of the character
                f = 1;          // escape flag, zero means end of this sequence
                while (true) {
                    ss = "";        // local str to parse as int
                        for (j = 0; j < 2; j++ ) {  // get two maximum hex characters for parse
                            sss = str.charAt(++i);
                            if (((sss >= "0") && (sss <= "9")) || ((sss >= "a") && (sss <= "f"))  || ((sss >= "A") && (sss <= "F"))) {
                                ss += sss;      // if hex, add the hex character
                            } else {--i; break;}    // not a hex char., exit the loop
                        }
                    n = parseInt(ss, 16);           // parse the hex str as byte
                    if (n <= 0x7f){u = n; f = 1;}   // single byte format
                    if ((n >= 0xc0) && (n <= 0xdf)){u = n & 0x1f; f = 2;}   // double byte format
                    if ((n >= 0xe0) && (n <= 0xef)){u = n & 0x0f; f = 3;}   // triple byte format
                    if ((n >= 0xf0) && (n <= 0xf7)){u = n & 0x07; f = 4;}   // quaternary byte format (extended)
                    if ((n >= 0x80) && (n <= 0xbf)){u = (u << 6) + (n & 0x3f); --f;}         // not a first, shift and add 6 lower bits
                    if (f <= 1){break;}         // end of the utf byte sequence
                    if (str.charAt(i + 1) == "%"){ i++ ;}                   // test for the next shift byte
                    else {break;}                   // abnormal, format error
                }
            s0 += String.fromCharCode(u);           // add the escaped character
            }
        }
    }
    return s0;
}


