function openChat( r )
{
    window.open("http://bbs.gamer.com.tw/chatroom/index.php?r="+r,"Chatroom","width=760,height=500,menubar=0,location=0,resizable=1").focus();
}

function getTime()
{
    return (new Date()).getTime();
}

function isIE()
{
    return navigator.appName == "Microsoft Internet Explorer";
}

function redir( url )
{
    rand = getTime();
    if( url.indexOf("?") == -1 )
    {
        location.href = url+"?rand="+rand;
    }
    else
    {
        location.href = url+"&rand="+rand;
    }
}

function setStatus( st )
{
}

function help(title)
{
    location.href = "http://user.gamer.com.tw/help";
}

function getCookie( key )
{
    key = '; '+key+"=";
    var cookietxt = '; '+document.cookie;
    start = cookietxt.indexOf( key, 0);
    if( start == -1 ) return "";
    end = cookietxt.indexOf( ";", start+1 );
    if( end == -1 ) end = cookietxt.length;
    if( isIE() )
    {
        return unescape(cookietxt.substring( start+key.length, end ));
    }
    else
    {
        return cookietxt.substring( start+key.length, end );
    }
}

function setCookieNew(key, value, domain, path, expires, secure){
  if(!(key || value)){
    return false;
  }

  var ck = key + '=' + escape(value);

  if(domain){
    ck += ';domain=' + domain;
  }
  if(expires){
    ck += ';expires=' + expires;
  }
  if(path){
    ck += ';path=' + path;
  }
  if(secure){
    ck += ';secure';
  }

  document.cookie = ck;

  return true;
}

function setCookie( key, value )
{
    if( isIE() )
    {
        document.cookie = escape(key)+"="+escape(value);
    }
    else
    {
        document.cookie = key+"="+value;
    }
}

function delCookie( key )
{
    document.cookie = key+"=NONE; expires=Tue, 01-01-1980 00:00:00 GMT";
}

function board(bid)
{
    location.href="/cgi-bin/brdAnnounce?brd="+bid;
}

function finger(uid)
{
    window.open( "/cgi-bin/finger?uid="+uid );
}

function login( from )
{
   w = window.open( "/sys/login.php?from="+from, "LOGIN", "width=180,height=200,toolbar=0,menubar=0,statusbar=0" );
   w.focus();
}

function bim()
{
   w = window.open('http://msg.gamer.com.tw/BIM/','BIM','width=280,height=400,menubar=0,resizable=1');
   w.focus();
}

function documentWrite(output){
  document.write(output);
}

function setWide(){
  delCookie('HP');
  location.href = "http://www.gamer.com.tw/";
}

function getInterface(){
  //W3C DTD 設定
  if( document.documentElement.scrollTop || document.body.offsetHeight == document.body.clientHeight ){
    return document.documentElement;
  }else{
    return document.body;
  }
}

function toCode62(n){
  var code62Map = ['0','1','2','3','4','5','6','7','8','9',
                   'A','B','C','D','E','F','G','H','I','J','K','L','M',
                   'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                   'a','b','c','d','e','f','g','h','i','j','k','l','m',
                   'n','o','p','q','r','s','t','u','v','w','x','y','z'];
  return code62Map[n];
}

function fromCode62(code){
  if(code == '-'){
    return -1;
  }

  var n = code.charCodeAt(0);

  if(n >= 48 && n <= 57){       // 0-9
    n -= 48;
  }
  else if(n >= 65 && n <= 90){  // A-Z
    n -= 55;
  }
  else if(n >= 97 && n <= 122){ // a-z
    n -= 61;
  }
  else{
    n = -1;   // exception
  }

  return n;
}

function generateCkGamerAdString(ck, cookiePos, adIndex){
  var CK_GAMER_AD_LENGTH = 16;

  var ckStr = ck, i;
  if(ck == ''){
    for(i = 0;i < CK_GAMER_AD_LENGTH;i++){
      ckStr += '-';
    }
  }

  ckStr = ckStr.substring(0, cookiePos) + toCode62(adIndex) + ckStr.substring(cookiePos + 1);

  return escape(ckStr);
}

// cookiePos: zero based
function getAdIndex(cookiePos, adCount){
  if(adCount < 1 || cookiePos < 0 || cookiePos > 64){
    return 0;
  }

  var ckGamerAd = getCookie('ckBahaAd');
  var adIndex = 0;
  if(ckGamerAd == ''){
    adIndex = Math.round(Math.random() * (adCount - 1));
  }
  else{
    adIndex = fromCode62(ckGamerAd.charAt(cookiePos));
    if(adIndex < 0){
      adIndex = Math.round(Math.random() * (adCount - 1));
    }
    else{
      adIndex = ++adIndex % adCount;
    }
  }

  document.cookie = 'ckBahaAd=' + generateCkGamerAdString(ckGamerAd, cookiePos, adIndex) + 
                    ';expires=' + new Date().toDateString() + ' 23:59:59 UTC+0800;domain=.gamer.com.tw';

  return adIndex;
}
