//台長前台 javascript


// 留言板 ##########################################################################################


//訪客新增留言
function gb_add(){
    $('set_reply').disabled = true;

    // 留言內容長度
    if ($('content').value.length > 2000){
        alert('內容請在2000個字以內，目前' + $('content').value.length + '個字');
        $('content').focus();
        reset_gcode('Gcode_Space');
        $('set_reply').disabled = false;
        return false;
    }

    // 留言禁止空白
    if( trim($('content').value) =='') {
        alert("內容請勿空白");
        $('content').focus();
        reset_gcode('Gcode_Space');
        $('set_reply').disabled = false;
        return false;
    }

    // gcode 驗證
    if(gcode_test("Gcode_Space") == 0){
        $('set_reply').disabled = false;
        return false;
    }


    document.ttimes.action = '/station/action/gb_add.php';
    document.ttimes.submit();
    return;
}


//台長回應留言
function gb_reply(){
    $('admin_reply_button').disabled = true;

    // 回應內容長度
    if ($('s_content').value.length > 2000){
        alert('內容請在2000個字以內，目前' + $('s_content').value.length + '個字');
        $('s_content').focus();
        $('admin_reply_button').disabled = false;
        return false;
    }

    // 回應禁止空白
   // if( trim($('s_content').value) =='') {
   //     alert("內容請勿空白");
   //     $('s_content').focus();
   //     return false;
   // }

    document.ttimes.action = '/station/action/gb_reply.php';
    document.ttimes.submit();
}

//刪除留言
function gb_del(tar_obj){
    // 檢查核取
    var arr = document.getElementsByName(tar_obj);

    var sel = 0;
    for(var i = 0;i<arr.length;i++){
        if (arr[i].checked == true){
            sel++;
        }
    }


    if (sel == 0){
        alert('未勾選項目');
        return;
    }


    document.ttimes.action = '/station/action/gb_del.php';
    document.ttimes.submit();
}




// 文章本體 ##########################################################################################

// 台長前台刪除文章
function del_content(){
    if(confirm('確定要刪除嗎?\n刪除之後無法救了喔！')){
        document.ttimes.action= "/panel/action/content_del.php";
        document.ttimes.submit();
    }
}


// 跳至編輯文章的頁面
function doedit(aid){
    document.ttimes.act.value ="edit";
    document.ttimes.action    ="/panel/article_edit";
    document.ttimes.single_aid.value = aid;
    document.ttimes.submit();
}


// 文章回應 ##########################################################################################


// 台長前台 單文頁 回應文章
function reply() {


    $('set_reply').disabled = true;


    // 去頭尾空白
    document.getElementById('nickname').value = trim(document.getElementById('nickname').value);
    document.getElementById('email').value = trim(document.getElementById('email').value);
    document.getElementById('url').value = trim(document.getElementById('url').value);
    document.getElementById('s_content').value = trim(document.getElementById('s_content').value);


    // 只對沒登入的進行  留言人、mail、url 檢查的部份
    if (document.getElementById('mypaper_sid').value == ''){

        // 檢查非空白的email
        if( document.getElementById('email').value !=''){
            var re = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!re.test(document.getElementById('email').value)){
                alert("你的mail格式不對！");
                reset_gcode('Gcode_Space');
                $('set_reply').disabled = false;
                return false;
            }
        }

        // 留言人禁止空白
        if( document.getElementById('nickname').value==''){
          alert("留言人請勿空白");
          document.getElementById('nickname').focus();
          reset_gcode('Gcode_Space');
          $('set_reply').disabled = false;
          return false;
        }

        //if( trim(document.getElementById('url').value)!='http://'){
          //  alert(document.getElementById('url').value);
          //  return false;
        //}
    }


    if( document.getElementById('s_content').value==''){
      alert("內容請勿空白");
      document.getElementById('s_content').focus();
      reset_gcode('Gcode_Space');
      $('set_reply').disabled = false;
      return false;
    }


    if(strlens(document.getElementById('s_content').value) > 2000){
      alert("內容請用中文1000個字或英文2000個字以內");
      document.getElementById('s_content').focus();
      reset_gcode('Gcode_Space');
      $('set_reply').disabled = false;
      return false;
    }


    // gcode 驗證
    if(gcode_test("Gcode_Space") == 0){
        $('set_reply').disabled = false;
        return false;
    }

    document.ttimes.action="/station/action/reply_add.php";
    document.ttimes.submit();

}



// 刪除單文回應
function reply_del(tar_obj){
    // 檢查核取
    var arr = document.getElementsByName(tar_obj);

    var sel = 0;
    for(var i = 0;i<arr.length;i++){
        if (arr[i].checked == true){
            sel++;
        }
    }


    if (sel == 0){
        alert('未勾選項目');
        return;
    }


    document.ttimes.action = '/station/action/reply_del.php';
    document.ttimes.submit();
}



// 台長回應訪客的回應
function content_reply(){

    $('admin_reply_button').disabled = true;
    // 回應內容長度
    if ($('s_content').value.length > 2000){
        alert('內容請在2000個字以內，目前' + $('s_content').value.length + '個字');
        $('s_content').focus();
        $('admin_reply_button').disabled = false;
        return false;
    }

    // 回應禁止空白
   // if( trim($('s_content').value) =='') {
   //     alert("內容請勿空白");
   //     $('s_content').focus();
   //     return false;
   // }

    document.ttimes.action = '/station/action/reply_reply.php';
    document.ttimes.submit();
}





// 文章推薦 #######################################################################################

// 新增推薦
function recommend_add(mypaper_id,aid,mypaper_sid){

    if (mypaper_sid == ""){
       //  alert('請先登入新聞台Blog，才能推薦文章。');
        if(confirm('請先登入新聞台Blog，才能推薦文章。')){
            location.href= 'https://member.pchome.com.tw/login.html?ref=' + encodeURIComponent(location);
        }
    }else if(mypaper_id == mypaper_sid){
        alert('本人不能自薦');
    }else{
        //alert('有登入');

        new Ajax.Request("/station/action/recommend_add.php",{
            parameters: $H({mypaper_id   : mypaper_id,
                            aid          : aid,
                            mypaper_sid  : mypaper_sid
                          }).toQueryString(),
            onSuccess: function(transport){
              //$('recommend_list_data').innerHTML=transport.responseText;
              //alert(transport.responseText);

              if ( transport.responseText == 'recommend_ok'){
                alert('感謝您的推薦！');
                $A($$('span.recmd_num')).each(function(s) {s.innerHTML = parseInt(s.innerHTML)+1;});
                //window.location.reload();
              }else if(transport.responseText == 'already_recommend'){
                //一篇文章只能推薦一次，不可重覆推薦!!
                window.location.href='/oops.htm?type=station&code=a_04';
              }else{
                window.location.reload();
                //alert(transport.responseText);
              }

            }
        });


    }

}



// 顯示推薦  隱藏回應
function recommend_list(mypaper_id,aid){
    if(document.getElementById("reply_list")!= null){
        document.getElementById("reply_list").className="tab";
    }
    document.getElementById("recommend_list").className="tab_on";
    new Ajax.Request("/station/action/recommend_show.php",{
        parameters: $H({mypaper_id   : mypaper_id,
                        aid          : aid
                      }).toQueryString(),
        onSuccess: function(transport){
            $('recommend_list_data').innerHTML=transport.responseText;
          //alert(transport.responseText);
        }
    });

    //alert(transport.responseText);

    document.getElementById("reply_list_data").style.display='none';
}



// 顯示回應  隱藏推薦
function reply_list(){
    document.getElementById("reply_list").className="tab_on";
    document.getElementById("recommend_list").className="tab";
    document.getElementById("reply_list_data").style.display='';
    $('recommend_list_data').innerHTML="";
}



// 單文轉寄 #######################################################################################



// 檢查輸入值然後跳轉寄處理頁
function send() {
   if(!mail_nick()){ return( false ) ;}
   else if(!mail_receive()){ return( false ) ;}
   else {
   document.ttimes.action="/station/action/mail_sent.php";
   document.ttimes.submit();
   }
}





// 轉寄相關檢查
function mail_nick(){
    if ( document.ttimes.mail_nick.value == "" ){
        alert( "請填寫【您的名字】 !" ) ;
        document.ttimes.mail_s_nick.focus();
        return( false ) ;
    }else if(getLength(document.ttimes.mail_nick.value)>80){
        alert('【您的名字】過長');
        document.ttimes.mail_s_nick.focus;
        return( false ) ;
    }
    return( true ) ;
}

// 轉寄相關檢查
function mail_receive(){
    if ( document.ttimes.mail_receive.value == ""){
        alert( "請填寫【收件人的e-mail】" ) ;
        document.ttimes.mail_receive.focus();
        return( false ) ;
    }else if(getLength(document.ttimes.mail_receive.value)>1000){
        alert('【收件人的e-mail】過長');
        document.ttimes.mail_receive.focus;
        return( false ) ;
    }

    var i=0;
    words = new String(document.ttimes.mail_receive.value)
    var swords = words.split(";");
        while(swords[i]){
            var len = swords[i].length;
            for(var j=0;j<len;j++){
            var c= swords[i].charAt(j);
                if(!((c>="A"&&c<="Z")||(c>="a"&&c<="z")||(c>="0"&&c<="9")||(c=="-")||(c=="_")||(c==".")||(c=="@"))){
                    alert( "您的『電子郵件地址』只能是數字,英文字母及'-','_'等符號,其他的符號都不能使用 !\n");
                    return ( false );
                }
           }
            if((swords[i].indexOf("@")==-1)||(swords[i].indexOf("@")==0)||(swords[i].indexOf("@")==(len-1))){
                alert("您的『電子郵件地址』不合法 !\n");
                document.ttimes.mail_receive.focus();
                return ( false );
            }
            if((swords[i].indexOf("@")!=-1)&&(swords[i].substring(swords[i].indexOf("@")+1,len).indexOf("@")!=-1)){
                alert("您的『電子郵件地址』不合法 !\n");
                document.ttimes.mail_receive.focus();
                return ( false );
            }
            if((swords[i].indexOf(".")==-1)||(swords[i].indexOf(".")==0)||(swords[i].lastIndexOf(".")==(len-1))){
                alert("您的『電子郵件地址』不完全 !\n");
                document.ttimes.mail_receive.focus();
                return ( false );
            }
            i++;
        }
        return( true );
}



// 轉寄專用取字數
function getLength(for_check){
    var I, cnt=0;
    for(I=0;I<for_check.length;I++){
        if(escape(for_check.charAt(I)).length >=4){
            cnt +=2;
        }else{
            cnt++;
        }
    }
    return cnt;
}



// 站台鼓勵 ###########################################################################################

function station_point_add( ){
   document.ttimes.action="/station/action/station_point_add.php";
   document.ttimes.submit();
}

// 其他 ###########################################################################################


// 計算字數
function strlens(text){
    var len =0,i=0;
    text = text.replace(/^\s*/,"");
    text = text.replace(/\s*$/,"");
    text = text.replace(/\r\n*$/,"");
    for(var i=0;i< text.length;i++){
      len += (text.charCodeAt(i) > 255)?2:1;
    }
    return len;
}


// 全選
function select_all(tar_obj){
    var arr = document.getElementsByName(tar_obj);

    for(var i = 0;i<arr.length;i++){
        arr[i].checked = true;
    }

}


//去除字串左邊的空白虛格
function ltrim(instr){
    return instr.replace(/^[\s]*/gi,"");
}

//去除字串右邊的空白虛格
function rtrim(instr){
    return instr.replace(/[\s]*$/gi,"");
}

//去除字串前後的空白虛格
function trim(instr){
    instr = ltrim(instr);
    instr = rtrim(instr);
return instr;
}



//gcode重設
function reset_gcode(Gcode_Space){

    new Ajax.Request("/public_file/Gcode_Api1.php", {
        parameters: $H({abc: "def"}).toQueryString(),
        onSuccess: function(transport) {
            $(Gcode_Space).innerHTML = transport.responseText;
            //Gcode_Space為存放gcode的容器（div or td or 其它tag的id)
        }
    });
}



//gcode驗證用
function gcode_test(Gcode_Space){
    //暫時關閉gcode
    //return 1;



    var flag = 0;
    new Ajax.Request("/public_file/Gcode_Auth.php", {
        parameters: $H({authRandnum: $('authRandnum').value,
                        authRandcode:$('authRandcode').value,
                        authAddr:$('authAddr').value}).toQueryString(),
        asynchronous: false,
        onSuccess: function(transport) {
            if(transport.responseText.substr(0,1) == "1") {
                flag = 1;
                $('Auth_Code').value = transport.responseText.substr(2,transport.responseText.length);
            } else {

                flag = 0;
                alert('驗證失敗！驗證碼重新產生');
                reset_gcode(Gcode_Space);
            }
        }
    });
    return flag;
}

function site_search(num){
    var now_url = location.href;
    var now_id_arr = now_url.split("/");
    now_id = now_id_arr[3];
    var has_checked = false;
    var search_field_str = "";
    $A(document.getElementsByName("search_field"+num)).each(function(s){
        if(s.checked==true){
            search_field_str += s.value+",";
            has_checked = true;
        }
    });
    if($('site_keyword'+num).value == "" || $('site_keyword'+num).value == "請輸入關鍵字") {
        alert('您尚未輸入關鍵字');
        $('site_keyword'+num).focus();
        return false;
    }
    if(has_checked == false) {
        alert('您尚未選擇要用何種方式搜尋');
        return false;
    }
    search_field_str = search_field_str.substr(0,search_field_str.length-1);
    location.href="/"+now_id+"/search/"+encodeURI($('site_keyword'+num).value)+"/?search_fields="+search_field_str;
}