var cgi="/cgi-bin/friend";
var html="/friend";

function getCookieVal(offset){
  var endstr=document.cookie.indexOf(";", offset);
  if(endstr==-1)
    endstr=document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate(date){
  var base=new Date(0);
  var skew=base.getTime(); // dawn of(Unix) time - should be 0
  if(skew > 0)  // Except on the Mac - ahead of its time
    date.setTime(date.getTime() - skew);
}

function GetCookie(name){
  var arg=name + "=";
  var alen=arg.length;
  var clen=document.cookie.length;
  var i=0;
  while(i < clen){
    var j=i + alen;
    if(document.cookie.substring(i, j)==arg)
      return getCookieVal(j);
    i=document.cookie.indexOf(" ", i) + 1;
    if(i==0) break; 
  }
  return null;
}

function SetCookie(name,value,expires,path,domain,secure){
  document.cookie=name + "=" + escape(value) +
   ((expires) ? "; expires=" + expires.toGMTString() : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
}

function DeleteCookie(name,path,domain){
  if(GetCookie(name)){
    document.cookie=name + "=" +
     ((path) ? "; path=" + path : "") +
     ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//將頁面redirect到某個網頁
function go_service(option){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(4*60*60*1000));
  SetCookie("idle","1",expireDate,"/");
  if(option==0){
    document.location.href=cgi+"/service/modifystatus.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==1){
    document.location.href=cgi+"/service/modifystatus.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==2){
    document.location.href=cgi+"/service/blacklist.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==3){
    document.location.href=cgi+"/service/modifyinfo.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==4){
    document.location.href=cgi+"/service/phonebook.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==5){
    document.location.href=cgi+"/service/querycall.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==6){
    document.location.href=html+"/service/delmember.htm";
  }else if(option==7){
    document.location.href=cgi+"/service/queryid.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==8){
    document.location.href=cgi+"/service/querysms.pl?r="+Math.floor(Math.random()*10000);
  }else if(option==9){
    document.location.href=cgi+"/service/download?r="+Math.floor(Math.random()*10000);
  }else if(option==10){
    document.location.href=cgi+"/content.pl";
  }else if(option==11){
    document.location.href="http://856.tccdata.com.tw/cgi-bin/friend/content.pl";
  }

}
function go_addgb(option,subclass,replypost){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(4*60*60*1000));
  SetCookie("idle","1",expireDate,"/");
  document.location.href=(cgi+"/webboard/addgb.pl?class="+option+"&subclass="+subclass+"&replypost="+replypost);
}

function go_im(option,chatid){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(4*60*60*1000));
  SetCookie("idle","1",expireDate,"/");
  if(option==0){
    document.location.href=(cgi+"/im/chatroom.pl?r="+Math.floor(Math.random()*10000));
  }else if(option==1){
    document.location.href=(cgi+"/im/chatroom.pl?r="+Math.floor(Math.random()*10000));
  }else if(option==2){
    document.location.href=(cgi+"/im/viewchat.pl?r="+Math.floor(Math.random()*10000));
  }else if(option==3){
    document.location.href=(cgi+"/im/addchat.pl?chatid="+chatid+"&r="+Math.floor(Math.random()*10000));
  }else if(option==4){
    document.location.href=(cgi+"/im/delchat.pl?chatid="+chatid+"&r="+Math.floor(Math.random()*10000));
  }else if(option==5){
    document.location.href=(cgi+"/im/modifyrecv.pl?r="+Math.floor(Math.random()*10000));
  }else if(option==6){
    document.location.href=(cgi+"/im/chat.pl?chatid="+chatid+"&r="+Math.floor(Math.random()*10000));
  }else if(option==7){
    document.location.href=(cgi+"/im/viewaudience.pl?r="+Math.floor(Math.random()*10000));
  }else if(option==8){
    document.location.href=(cgi+"/im/addchat.pl?random=1&name="+chatid+"&r="+Math.floor(Math.random()*10000));
  }else if(option==9){
    document.location.href=(cgi+"/im/enterchat.pl?r="+Math.floor(Math.random()*10000));
  }else if(option==10){
    document.location.href=(cgi+"/im/modifynickname.pl?r="+Math.floor(Math.random()*10000));
  }
}
function go_sms(option,name){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(4*60*60*1000));
  SetCookie("idle","1",expireDate,"/");
  if(option==0){
    document.location.href=(cgi+"/sendsms/message.pl?assignid="+name+"&r="+Math.floor(Math.random()*10000));
  }else if(option==1){
    document.location.href=(cgi+"/sendsms/message.pl?assignid="+name+"&r="+Math.floor(Math.random()*10000));
  }else if(option==2){
    document.location.href=(cgi+"/sendsms/addsmscan.pl?r="+Math.floor(Math.random()*10000));
  }
}
function go_imedia(option,name){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(4*60*60*1000));
  SetCookie("idle","1",expireDate,"/");
  if(option==0){
    document.location.href=(cgi+"/imedia/privateroom.pl");
  }else{
    document.location.href=(cgi+"/imedia/privateroom.pl?name="+name);
  }
}
function go_content(val){
  if(val==1)
    alert("您已登出856會員");
  else if(val==2)
    alert("您已登出");
  else
    alert("請先登入會員!!!");
  var expireDate=new Date();
  FixCookieDate(expireDate);
  document.location.href=(cgi+"/content.pl?r="+Math.floor(Math.random()*10000));
}


//檢查是否已登入
function chkc(option){
  val=GetCookie("IDnumber");  
  val2=GetCookie("idle");
  if(!val){
    document.location.href=(cgi+"/login.pl?hidNext=0&option="+option+"&error=1");
  }else if(!val2){
    document.location.href=(cgi+"/login.pl?hidNext=0&option="+option+"&error=1&idle=1");
  }
}
function chkgbc(option,subclass,replypost){
  val=GetCookie("IDnumber");  
  val2=GetCookie("idle");
  if(!val){
    document.location.href=(cgi+"/login.pl?hidNext=1&option="+option+"&subclass="+subclass+"&replypost="+replypost+"&error=1");
  }else if(!val2){
    document.location.href=(cgi+"/login.pl?hidNext=1&option="+option+"&subclass="+subclass+"&replypost="+replypost+"&error=1");
  }
}
function chkim(option,name){
  //for 856 Member
  val=GetCookie("IDnumber");  
  val2=GetCookie("idle");
  val3=GetCookie("audience");
  if(val3){
    
  }else{
    if(!val){
      document.location.href=(cgi+"/login.pl?hidNext=2&option="+option+"&name="+name+"&error=1");
    }else if(!val2){
      document.location.href=(cgi+"/login.pl?hidNext=2&option="+option+"&name="+name+"&error=1&idle=1");
    }
  }
}
function chkaudience(option,name){
  //for not 856 Member
  val2=GetCookie("idle");
  val3=GetCookie("audience");
  if(!val3){
    document.location.href=(cgi+"/login.pl?hidNext=2&option="+option+"&name="+name+"&error=1");
  }else if(!val2){
    document.location.href=(cgi+"/login.pl?hidNext=2&option="+option+"&name="+name+"&error=1&idle=1");
  }
}
function chksms(option,name){
  val=GetCookie("IDnumber");  
  val2=GetCookie("idle");
  val3=GetCookie("audience");
  if(option==1){
    if(val3){
    
    }else{
      if(!val){
        document.location.href=(cgi+"/content.pl?hidNext=3&option="+option+"&name="+name+"&error=1");
        //document.location.href=(cgi+"/login.pl?hidNext=3&option="+option+"&name="+name+"&error=1");
      }else if(!val2){
        document.location.href=(cgi+"/content.pl?hidNext=3&option="+option+"&name="+name+"&error=1&idle=1");
        //document.location.href=(cgi+"/login.pl?hidNext=3&option="+option+"&name="+name+"&error=1&idle=1");
      }
    }
  }else{
    if(!val){
      document.location.href=(cgi+"/login.pl?hidNext=3&option="+option+"&error=1");
    }else if(!val2){
      document.location.href=(cgi+"/login.pl?hidNext=3&option="+option+"&error=1&idle=1");
    }
  }
}
function chkimedia(option,name){
  //for 856 Member
  val=GetCookie("IDnumber");  
  val2=GetCookie("idle");
  val3=GetCookie("audience");
  if(val3){
    
  }else{
    if(!val){
      document.location.href=(cgi+"/login.pl?hidNext=4&option="+option+"&name="+name+"&error=1");
    }else if(!val2){
      document.location.href=(cgi+"/login.pl?hidNext=4&option="+option+"&name="+name+"&error=1&idle=1");
    }
  }
}


function del_member(){
  alert("已取消會員資格,請重新申請!!");
  document.location.href=(cgi+"/content.pl?r="+Math.floor(Math.random()*10000));
}
function go2log(){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime()+(4*60*60*1000));
  SetCookie("idle","1",expireDate,"/");
}
function set_rank(){
  rankstartnum=GetCookie("Rank");
  if(rankstartnum==null)
    rankstartnum=0;
  rankstartnum++;
  if(rankstartnum>=10)
    rankstartnum=0;
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(24*60*60*1000));
  SetCookie("Rank",rankstartnum,expireDate,"/");
}
function get_vote(option,subclass,name){
  var str="c"+option+"s"+subclass+"n"+name;
  var val=GetCookie("voted");
  if(val!= null){
  	var splitstr=val.split(",");
  	for(i=0;i<splitstr.length;i++)
  		if(splitstr[i]==str)
  		  return 1;
  }
  return 0;
}
function voted(option,subclass,name){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(4*24*60*60*1000));
  val=GetCookie("voted");
  if(val==null)
    val='';
  val=val+"c"+option+"s"+subclass+"n"+name+',';
  SetCookie("voted", val, expireDate,"/");
}
function setWeb(){
  SetCookie("856web","1","","/");
}
function setIdle(){
  var expireDate=new Date();
  FixCookieDate(expireDate);
  expireDate.setTime(expireDate.getTime() +(20*60*1000));
  SetCookie("idle","1",expireDate,"/"); 
}
