﻿var Topic_ID;
var receiver;
var goaction;
var reasonNo;
var title;
var ThreadID;
var reason;
var okForm;
var cnt = 0;
var maxChr = 300;
var nowChr = 0;
var self;

reason="未選擇";
ThreadID="";


function check(){
	if(nowChr>maxChr) alert("你多輸入了"+(nowChr-maxChr)+"個字。");
}

function mutDel(form,value,n){
    var i = 0;
    var selfNum=0;
    var j = 0;
    //alert(n);
    if (value==3 || value==21){
            maxChr = 100;
        }else{
            maxChr = 300;
        }
    cnt=0;
    if (n=="D" && value==3){ //刪除已經列在焚化爐內的文章
        alert("文章已刪除");
    }else{
        if (n=="C" && value==5){//將已在修改區區的文章重複列入修改區區
            alert("文章已在修改區區內");
        }else{
            i=0;
            while (i < form.elements.length)  {
                if (form.elements[i].name==='TopicID' && form.elements[i].checked){
                    cnt += 1 ;
                    
                 if(form.elements[i].value.substring(0,1)==="i")
                 {     selfNum += 1;
                       //form.elements[i].value=form.elements[i].value.substring(1);
                 }
               }
                     i ++;
                
            }
            
            
            if (cnt > 0){
                title = "文章異動";
                okForm = form;
                goaction = value;
               
               if(cnt==selfNum){
                    okForm.goaction.value = goaction;
                    okForm.self.value="1";
                    document.okform.submit();
               }else
                   popSignFlow2(7);
            }else{
                alert("請至少選擇一篇文章!!");
            }
        }
    }
    
}

//刪除文章(sogi3.0使用)
function mutDelV2(form,value,n){
    var i = 0;
    var selfNum=0;
    var j = 0;
    //alert(n);
    if (value==3 || value==21){
            maxChr = 100;
        }else{
            maxChr = 300;
        }
    cnt=0;
    if (n=="D" && value==3){ //刪除已經列在焚化爐內的文章
        alert("文章已刪除");
    }else{
        if (n=="C" && value==5){//將已在修改區區的文章重複列入修改區區
            alert("文章已在修改區區內");
        }else{
            i=0;
            while (i < $("input[name='TopicID']").length)  {
                if ($("input[name='TopicID']")[i].name==='TopicID' && $("input[name='TopicID']")[i].checked){
                    cnt += 1 ;
                    
                 if($("input[name='TopicID']")[i].value.substring(0,1)==="i")
                 {     
                    selfNum += 1;
                    //form.elements[i].value=form.elements[i].value.substring(1);
                    $("input[name='topicidlist']")[0].value = $("input[name='topicidlist']")[0].value + $("input[name='TopicID']")[i].value.substring(1) + ',';
                 } else {
                    $("input[name='topicidlist']")[0].value = $("input[name='topicidlist']")[0].value + $("input[name='TopicID']")[i].value + ',';
                 }
               }
                     i ++;
                
            }
            
            $("input[name='topicidlist']")[0].value = $("input[name='topicidlist']")[0].value.substr(0, $("input[name='topicidlist']")[0].value.length - 1);
            
            if (cnt > 0){
                title = "文章異動";
                okForm = form;
                goaction = value;
               
               if(cnt==selfNum){
                    okForm.goaction.value = goaction;
                    okForm.self.value="1";
                    document.okform.submit();
               }else
                   popSignFlow2(7);
            }else{
                alert("請至少選擇一篇文章!!");
            }
        }
    }
    
}


function delAll(tit, tid,rr,act){
    if (act==3 || act==21){
            maxChr = 100;
        }else{
            maxChr = 300;
        }
    //alert(encodeURI("？"));
    title = tit;    
    Topic_ID = tid;
    receiver = rr;
    goaction = act;

    popSignFlow2(6);
};

function singleDel(tit, tid, thread_id, rr, act){
    maxChr = 100;
    title = tit;
    Topic_ID = tid;
    ThreadID = thread_id
    receiver = rr;
    goaction = act;
    popSignFlow2(6);
}

function G(id){
    return document.getElementById(id);
};
function GC(t){
   return document.createElement(t);
};
String.prototype.trim = function(){
          return this.replace(/[\[\]\$!%^#\&*|"'><]/,"") 
};
function isIE(){
      return (document.all && window.ActiveXObject && !window.opera) ? true : false;
}
function setHref(url) {  //修正IE無法透過JS轉頁傳遞 HTTP_REFERER 標頭問題
    var isIE = (navigator.appName.indexOf("Microsoft")!=-1) ? true : false;
    //alert("new function!");
    if (!isIE) {
        //Standards based browsers
        parent.location.href = url; // Set target: self. / parent. / top.
    } else {
        var lha = document.getElementById('_lha');
            if(!lha){
                lha = document.createElement('a');
                lha.id = '_lha';
                lha.target = '_parent';  // Set target: for IE
                document.body.appendChild(lha);
            }
            lha.href = url;
            lha.click();
    }
}

//取消 
function cancelSign2(){
    G("sign_div2").style.display = 'none';
    G("cover_div2").style.display = 'none';
    document.body.style.overflow = '';
};
//取代Enter符號
function replacecarriagereturn(textarea,replaceWith)  
 {   
  textarea.value = escape(textarea.value);  
    //encode all characters in text area  
    //to find carriage return character  
   for(i=0; i < textarea.value.length; i++)  
  {   
   //loop through string, replacing carriage return   
   //encoding with HTML break tag  
   if(textarea.value.indexOf("%0D%0A") > -1)  
   {   
    //Windows encodes returns as \r\n hex  
    textarea.value=textarea.value.replace("%0D%0A",replaceWith);  
   }  
   else if(textarea.value.indexOf("%0A") > -1)  
   {   
    //Unix encodes returns as \n hex  
    textarea.value=textarea.value.replace("%0A",replaceWith);  
   }  
   else if(textarea.value.indexOf("%0D") > -1)  
   {   
    //Macintosh encodes returns as \r hex  
    textarea.value=textarea.value.replace("%0D",replaceWith);  
   }  
  }  
  textarea.value=unescape(textarea.value);  
 //decode all characters in text area back  
 }  

 //確定並刪除本文.
function del(){
    if (reason != "未選擇") {
            //找出Enter並取代, 在vb中替換成<br/>     
            if (document.getElementById("delReason").value.length<=100){                
                replacecarriagereturn(document.getElementById("delReason"),"^M");
                var content = document.getElementById("delReason").value.replace(/[\[\]\$!%^#\&*|"'><]/,"") 
                //alert(content);
                G("sign_div2").style.display = 'none';
                G("cover_div2").style.display = 'none';
                document.body.style.overflow = '';                
                var url = "../newforum/Change_article_status.aspx?goaction=" + goaction +"&TopicID=" + Topic_ID + "&topicidlist=" + Topic_ID + "&receiver=" + receiver +  "&reasonNo=" + reasonNo + "&reason=" + reason + "&isMut=1" ;
                if (ThreadID != "") {
                    url = url + "&threadID=" + ThreadID;
                }
                url = url + "&content=";
                url = url + encodeURI(content);            
                //location.href = url;
                //alert(url);
                setHref(url);                
            }else{
                alert("說明請控制在100字以內");
            }
   }else{
        alert("請選擇原因");
   }

};
function argue(){
    if (document.getElementById("argReason").value.trim() != ""){
        maxChr = 300;
        if (document.getElementById("argReason").value.trim().length <= maxChr ){
            replacecarriagereturn(document.getElementById("argReason"),"^M");
            var content = document.getElementById("argReason").value.replace(/[\[\]\$!%^#\&*|"'><]/,"") 
            G("sign_div2").style.display = 'none';
            G("cover_div2").style.display = 'none';
            document.body.style.overflow = '';
            var url = "../newforum/Change_article_status.aspx?goaction=" + goaction +"&TopicID=" + Topic_ID + "&topicidlist=" + Topic_ID + "&receiver=" + receiver +  "&reasonNo=" + reasonNo + "&reason=" + reason + "&isMut=1" ;             
            url = url + "&content=";
            url = url + encodeURI(content);          
            setHref(url);            
        }else{
            alert("說明請控制在" + maxChr + "字以內");
        }
        
    }else{
        alert("列入修改區區必填說明");
    }
};
//多重刪除
function submitForm(){
    var isOk = true;
    var mutiple = true;
    if (goaction==5){
        reason = "修改區";
    }
    if (reason != "未選擇") {
        
        
        
        if (cnt > 1){
            if (goaction==3){
                mutiple = confirm("您確定所有文章違規項目都相同？");
            }else{
                mutiple = confirm("您確定所有文章修改原因都相同？");
            }            
        }
        
        if (mutiple==true){           
            if (goaction==5 ){
                if (document.getElementById("argReason").value.trim()  == ""){         
                isOk = false;
                }
            }
                //alert("content =" + document.getElementById(id="argReason").value + "  " + goaction + "  " + isOk);
                //alert(isOk);
                if (isOk==true){
                    var content;
                    //將表單內的值寫入
                    if (goaction==3 || goaction==21){
                        //alert("多篇刪除" + goaction);
                        replacecarriagereturn(document.getElementById("delReason"),"^M");
                        content = document.getElementById("delReason").value.replace(/[\[\]\$!%^#\&*|"'><]/,"") 
                        //alert(document.getElementById("delReason").value);
                    }else{
                        //alert("多篇修改區");
                        replacecarriagereturn(document.getElementById(id="argReason"),"^M");
                        content = document.getElementById(id="argReason").value.replace(/[\[\]\$!%^#\&*|"'><]/,"") 
                    }
                    if (content.length<=maxChr){
                        //alert(content);
                    okForm.content.value = content;
                    //alert(okForm.content.value);
                    okForm.goaction.value = goaction;
                    okForm.reason.value = reason;
                    okForm.reasonNo.value = reasonNo;
                    okForm.submit();
                    }else{
                        alert("說明必須在" + maxChr + "字以內");
                    }                    
                }else{
                    alert("列入修改區必須填入說明");
                }        
        }else{
        
        }        
    }else{
        alert("請選擇原因");
   }
}

function popCoverDiv2(){
   if (G("cover_div2")) {
    G("cover_div2").style.display = '';
   } else {
    var coverDiv = GC('div');
    document.body.appendChild(coverDiv);
    coverDiv.id = 'cover_div2';
    with(coverDiv.style) {
     position = 'absolute';
     background = '#CCCCCC';
     left = '0px';
     top = '0px';
     var bodySize = getBodySize();
     width = bodySize[0] + 'px';
     height = bodySize[1] + 'px';
     zIndex = 98;
     if (isIE()) {
      filter = "Alpha(Opacity=60)";
     } else {
      opacity = 0.6;
     }
    }
   }
}
function getBodySize(){
   var bodySize = [];
   with(document.documentElement) {
    bodySize[0] = (scrollWidth>clientWidth)?scrollWidth:clientWidth;
    bodySize[1] = (scrollHeight>clientHeight)?scrollHeight:clientHeight;
   }
   return bodySize;
}
function setReason(value, text){
    reasonNo = value;
    reason = text;
}
function countText(){
    
};

function popSign2(az_id){
   var loginDivWidth = 600;
   remainTxt = maxChr;
   var sign_in_flow = ''
        + '<div id="yauclaa" class="msg">'
        + '<h4>文章異動</h4>'
        + '<form method=post  id="delForm" name="delForm" runat="server">'
        +   '<div style="text-align: left;">文章標題：'+ title +'</div><hr/>'
        if (goaction==3 || goaction==21){//刪文
            sign_in_flow = sign_in_flow +'<div class="left">請選擇違規項目<Select onChange="setReason(this.options[this.selectedIndex].value,this.options[this.selectedIndex].label);">'
        +   '<option value=0 label="未選擇" selected >請選擇一項原因</option>'
        +   '<option value=1 label="內容違反智慧財產權">1、內容違反智慧財產權</option>'
        +   '<option value=3 label="張貼買賣文章">2、張貼買賣文章</option>'
        +   '<option value=5 label="與本版主題不符">3、與本版主題不符</option>'
        +   '<option value=4 label="內容語意不明或使用火星文">4、內容語意不明或使用火星文</option>'
        +   '<option value=9 label="含有病毒、惡意語法連結">5、含有病毒、惡意語法連結</option>'
        +   '<option value=6 label="含有同質性或商業網站連結">6、含有同質性或商業網站連結</option>'
        +   '<option value=7 label="涉及情色等不雅字眼">7、涉及情色等不雅字眼</option>'
        +   '<option value=2 label="帶有蓄意攻擊、挑釁等字眼">8、帶有蓄意攻擊、挑釁等字眼</option>'
        +   '<option value=8 label="涉及政治與宗教性話題">9、涉及政治與宗教性話題</option>'
        +   '<option value=10 label="公開留下連絡方式">10、公開留下連絡方式</option>'
        +   '<option value=12 label="重覆張貼文章">11、重覆張貼文章</option>'
        +   '<option value=11 label="其他違規">12、其他違規</option>'
        +   '</Select></div>'
	    +   '<div id="info"></div>'
        +   '<label>違規說明：</label><textarea onkeyup="count(this.value)" id="delReason" style="width: 400px; height: 80px;"></textarea>'
        +   '<center class="txt-imp">詳述刪文說明(100字以內)<br/>$ ! # $ % & * " ' + " '" +' 等符號將會無法顯示</center><hr/><center>';
        //alert("123");
        }else{//修改區
	    sign_in_flow+='<div id="info"></div>';
            sign_in_flow = sign_in_flow +'<label>描述：</label><textarea onkeyup="count(this.value)" id="argReason" style="width: 400px; height: 80px;"></textarea>'
            +  '<center class="txt-imp">必填且詳述改修說明(300字以內)<br/>$ ! # $ % & * " ' + " '" +' 等符號將會無法顯示</center><hr/><center>';                
        }       
        if(az_id==6){ //單篇刪除
            if(goaction==3 || goaction==21){
                sign_in_flow = sign_in_flow +'<input type="button" value="確定並刪除本文"  onclick="del();"/>';
            }else{//單篇修改區
                sign_in_flow = sign_in_flow +'<input type="button" value="確定並將本文列入修改區區"  onclick="argue();"/>';                                
            }
            
        }else{ //多篇刪除
            if(goaction==3 || goaction==21){
                sign_in_flow = sign_in_flow +'<input type="button" value="確定並刪除所選文章" onclick="submitForm();"/>';
            }else{//多篇修改區
                sign_in_flow = sign_in_flow +'<input type="button" value="確定並將所選文章列入修改區" onclick="submitForm();"/>';
            }
        }                                   
        sign_in_flow = sign_in_flow + '<input style="left: 100px; position: relative;" type="button" value="取消" onclick="cancelSign2();"/>'
        +   '</center></form></div>';


   if (G("sign_div2")) {
    G("sign_div2").style.display = '';
    //alert(document.documentElement.scrollTop + document.documentElement.clientHeight/3);
    G("sign_div2").style.top =  document.documentElement.scrollTop + document.body.scrollTop + document.documentElement.clientHeight/3 + 'px';
   } else {
    var signDiv = GC('div');
    document.body.appendChild(signDiv);
    signDiv.id = 'sign_div2';
    signDiv.align = "center";  
    with (signDiv.style) {
     position = 'absolute';
     left = (document.documentElement.clientWidth - loginDivWidth)/4 + 'px';
     top = document.documentElement.scrollTop + document.body.scrollTop + document.documentElement.clientHeight/3 + 'px'; //Chrome 認不得documentElement.scrollTop，所以加上body.scrollTop，body.scrollTop在FF為0
     //alert("left=" + left + ", top=" + top + ", clientHeight=" + document.documentElement.clientHeight + ", scrollTop=" + document.documentElement.scrollTop + ", body.scrollTop=" + document.body.scrollTop);
     
     width = loginDivWidth + 'px';
     zIndex = 99;
     background = '#FFFFFF';
    }
   }
    
    G("sign_div2").innerHTML = sign_in_flow;  
}

function popSignFlow2(az_id) {
   popCoverDiv2();  
   popSign2(az_id);  
   //document.body.style.overflow = "hidden";    
      
}


//刪除自己文章
function delAll3(tit, tid,rr,act){
    title = tit;
    Topic_ID = tid;
    receiver = rr;
    goaction = act;
    popSignFlow3(6);
};

function popSignFlow3(az_id) {
   popSign3(az_id);  
   //document.body.style.overflow = "hidden";        
}

function popSign3(az_id){
        
     if(az_id==6){ //單篇刪除
          if(goaction==3 || goaction==21)
             del2();
         else//單篇修改區
             argue2();                                   
     }                                 
}

//刪除自己文章
function del2(){       
    var url = "../newforum/Change_article_status.aspx?goaction=" + goaction +"&TopicID=" + Topic_ID + "&topicidlist=" + Topic_ID + "&self=1";
    if (ThreadID != "") {
          url = url + "&threadID=" + ThreadID;
    }
         setHref(url);     
};

//自己文章列入修改區
function argue2(){
    var url = "../newforum/Change_article_status.aspx?goaction=" + goaction +"&TopicID=" + Topic_ID  + "&topicidlist=" + Topic_ID + "&self=1";        
    setHref(url);            
};

//單篇刪除自己文章
function singleDel3(tit, tid, thread_id, rr, act){
    title = tit;
    Topic_ID = tid;
    ThreadID = thread_id
    receiver = rr;
    goaction = act;
    popSignFlow3(6);
}
function count(value){
	nowChr = value.length;
	document.getElementById("info").innerHTML="目前已輸入"+nowChr+"個字，最多可輸入"+maxChr+"個字。";
	check();
}


