/**********************************************************************
 *
 *  Unicode  UTF-8
 *
 *  Copyright (c) 2005 zzz
 *
 **********************************************************************/

function _to_utf8(s) {
  var c, d = "";
  for (var i = 0; i < s.length; i++) {
    c = s.charCodeAt(i);
    if (c <= 0x7f) {
      d += s.charAt(i);
    } else if (c >= 0x80 && c <= 0x7ff) {
      d += String.fromCharCode(((c >> 6) & 0x1f) | 0xc0);
      d += String.fromCharCode((c & 0x3f) | 0x80);
    } else {
      d += String.fromCharCode((c >> 12) | 0xe0);
      d += String.fromCharCode(((c >> 6) & 0x3f) | 0x80);
      d += String.fromCharCode((c & 0x3f) | 0x80);
    }
  }
  return d;
}

function _from_utf8(s) {
  var c, d = "", flag = 0, tmp;
  for (var i = 0; i < s.length; i++) {
    c = s.charCodeAt(i);
    if (flag == 0) {
      if ((c & 0xe0) == 0xe0) {
        flag = 2;
        tmp = (c & 0x0f) << 12;
      } else if ((c & 0xc0) == 0xc0) {
        flag = 1;
        tmp = (c & 0x1f) << 6;
      } else if ((c & 0x80) == 0) {
        d += s.charAt(i);
      } else {
        flag = 0;
      }
    } else if (flag == 1) {
      flag = 0;
      d += String.fromCharCode(tmp | (c & 0x3f));
    } else if (flag == 2) {
      flag = 3;
      tmp |= (c & 0x3f) << 6;
    } else if (flag == 3) {
      flag = 0;
      d += String.fromCharCode(tmp | (c & 0x3f));
    } else {
      flag = 0;
    }
  }
  return d;
}

function _unescape(s) {
  return s.replace(/(%[0-9A-Z][0-9A-Z])/ig, function(e) {
    return String.fromCharCode("0x" + e.charAt(1) + e.charAt(2));
  });
}


var l_cookieName="v_lang";
function setLangCookie(cookieValue) {
  setCookie(l_cookieName,cookieValue);
}

function setCookie(cookieName,cookieValue) {
 document.cookie = cookieName+"="+escape(cookieValue);
}

function readCookie(cookieName) {
  var theCookie=""+document.cookie;
  var ind=theCookie.indexOf(cookieName);
  if (ind==-1 || cookieName=="") return "";
  var ind1=theCookie.indexOf(';',ind);
  if (ind1==-1) ind1=theCookie.length; 
  return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function readLangCookie() {
  return readCookie(l_cookieName);
}

function checkCookieEnabled(){
  testValue=Math.floor(1000*Math.random());
  setCookie("cookieTest",testValue);
  ret=readCookie("cookieTest");
  if(ret==testValue){
    return true;
  }else{
    return false;
  }
}

function myJaCookie(){
  if(checkCookieEnabled()){
    setLangCookie('_ja');
    return true;
  }else{
    alert("Cookie is not enabled in this computer. Enable Cookie Please !");
    return false;
  }
}

function myCnCookie(){
  if(checkCookieEnabled()){
    setLangCookie('_cn');
    return true;
  }else{
    alert("Cookie is not enabled in this computer. Enable Cookie Please !");
    return false;
  }
}


function my_escape(s, c) {
  var r;
  if (s == null) return s;
  if (c == "utf-8")
    r = _escape(_to_utf8(s));
  else if (c == "shift-jis")
    r = _escape(_to_sjis(s));
  else if (c == "euc")
    r = _escape(_to_euc(s));
  else if (c == "jis")
    r = _escape(_to_jis(s));
  return r;
}

function my_unescape(s, c) {
  var r;
  if (s == null) return s;
  if (c == "utf-8")
    r = _from_utf8(_unescape(s));
  else if (c == "shift-jis")
    r = _from_sjis(_unescape(s));
  else if (c == "euc")
    r = _from_euc(_unescape(s));
  else if (c == "jis")
    r = _from_jis(_unescape(s));
  return r;
}


function _escape(s) {
  var c, n, r = "";
  for (var i = 0; i < s.length; i++) {
    c = s.charCodeAt(i);
    if ((c >= 0x30 && c <= 0x39) || (c >= 0x40 && c <= 0x5A) ||
        (c >= 0x61 && c <= 0x7A) || (c == 0x2A) ||
        (c == 0x2D) || (c == 0x2E) || (c == 0x5F)) {
      r += s.charAt(i);
    } else {
      n = c.toString(16).toUpperCase();
      r += "%" + (n.length == 1 ? "0" + n : n);
    }
  }
  return r;
}

function _unescape(s) {
  return s.replace(/(%[0-9A-Z][0-9A-Z])/ig, function(e) {
    return String.fromCharCode("0x" + e.charAt(1) + e.charAt(2));
  });
}
