   var xh = '';
    /*阿阮，這個function不要重覆定義到，要使用$的話我們的頁面都有include prototype.js裡面就有宣告了
    function $(obj){
        return document.getElementById(obj);
    }
    */
function createObject() {
        if (window.XMLHttpRequest) { // 如果可以取得XMLHttpRequest
            xh = new XMLHttpRequest();  // Mozilla、Firefox、Safari 
        }
        else if (window.ActiveXObject) { // 如果可以取得ActiveXObject
            xh = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer
        }
    }

function stkClearMe(control)
{
 control.value = '';
 control.style.color = "black";
}
function stkRestore(control, str)
{
 if (control.value == '') {control.value = str; control.style.color = "#a8a8a8";};
 
}

    //版主私訊 system msg
    function check_sysmsg(){

        if(msg_check(document.sysmsg.subject.value)==false) {
            alert('請輸入標題!!');
            document.sysmsg.subject.focus();
            return false;
        }

        if(document.sysmsg.subject.value.length>30){
            alert('標題過長!!');
            return false;
        }

        if(document.sysmsg.sforum.checked==false && document.sysmsg.ssub.checked==false && document.sysmsg.sgroup.checked==false) {
            alert('請至少勾選一個版群!!');
            document.sysmsg.sforum.focus();
            return false;
        }

/*
        if(document.privmsg_send.mreceive.value.length>30){
            alert('收件人 Id 過長!!');
            return false;
        }
*/

        if (msg_check(document.sysmsg.content.value)==false) {
            alert('請輸入內容!!');
            document.sysmsg.content.focus();
            return false;
        }

        if(document.sysmsg.content.value.length>250){
            alert('內容過長!!');
            return false;
        }

        if(document.sysmsg.sforum.checked == false){
            document.sysmsg.sforum.value = "futile";
        }

        if(document.sysmsg.ssub.checked == false){
            document.sysmsg.ssub.value = "futile";
        }

        if(document.sysmsg.sgroup.checked == false){
            document.sysmsg.sgroup.value = "futile";
        }

        new Ajax.Request("/_admin/system_action.php", {
        parameters: $H({subject: document.sysmsg.subject.value, 
                        content: document.sysmsg.content.value,
                        userip:  document.sysmsg.getip.value,
                        sforum:  document.sysmsg.sforum.value,
                        ssub:    document.sysmsg.ssub.value,
                        sgroup:  document.sysmsg.sgroup.value}).toQueryString(),
        onSuccess: function(transport) {
            if(transport.responseText == "success") {
                show_msg('版主訊息新增成功!!');
                location.href="/admin/system_msg/";
            } else
                show_msg(transport.responseText);
        }

       });
    }


    //精華區文章分類搬移
    function cream_move(tid){
            var arr = document.getElementsByTagName('INPUT');
            var have_data=0
            var have_check = 0;
            var tid_string = "";
            for(var i = 0; i < arr.length; i++){
                if(arr[i].id == tid){
                    have_data++;
                    if(arr[i].checked == true) {
                        have_check++;
                        tid_string += ","+arr[i].value;
                    }
                }
            }
            if(have_data <=0) {
                show_msg('沒有任何精華區文章');
                return false;
            }
            if(have_check <=0) {
                show_msg('您尚未勾選任何精華區文章');
                return false;
            }
            tid_string = tid_string.substr(1,tid_string.length);

            new Ajax.Request("/cream_action.php", {
            parameters: $H({cate_move:      document.cream_list.cream_del.value, 
                            channel:        document.cream_list.channel.value,
                            fid:            document.cream_list.fid.value,
                            userid:         document.cream_list.userid.value,
                            tid_ary:        tid_string,
                            special_no:     document.cream_list.special_no.value,
                            user_ip:        document.cream_list.user_ip.value}).toQueryString(),
            onSuccess: function(transport) {
                if(transport.responseText == "success") {
                    show_msg('精華區文章搬移成功!!');
                    location.reload();
                } else
                    show_msg(transport.responseText);
                }

            });
    }//endof function


    //精華區文章刪除
    function cream_delete(tid){

            var arr = document.getElementsByTagName('INPUT');
            var have_data=0
            var have_check = 0;
            var tid_string = "";
            for(var i = 0; i < arr.length; i++){
                if(arr[i].id == tid){
                    have_data++;
                    if(arr[i].checked == true) {
                        have_check++;
                        tid_string += ","+arr[i].value;
                    }
                }
            }
            if(have_data <=0) {
                show_msg('沒有任何精華區文章');
                return false;
            }
            if(have_check <=0) {
                show_msg('您尚未勾選任何精華區文章');
                return false;
            }
            tid_string = tid_string.substr(1,tid_string.length);

            new Ajax.Request("/cream_action.php", {
            parameters: $H({cream_del:  document.cream_list.cream_del.value, 
                            channel:    document.cream_list.channel.value,
                            fid:        document.cream_list.fid.value,
                            userid:     document.cream_list.userid.value,
                            tid_ary:    tid_string,
                            user_ip:    document.cream_list.user_ip.value}).toQueryString(),
            onSuccess: function(transport) {
                if(transport.responseText == "success") {
                    show_msg('精華區文章刪除成功!!');
                    location.reload();
                } else
                    show_msg(transport.responseText);
                }

            });
    }//endof function

     function cream_delete_signle(fid,tid,userid,user_ip){
     
            new Ajax.Request("/cream_action.php", {
            parameters: $H({cream_del:  '從精華區刪除', 
                            channel:   'forum',
                            fid:        fid,
                            userid:    userid,
                            tid_ary:    tid,
                            user_ip:    user_ip}).toQueryString(),
            onSuccess: function(transport) {
                if(transport.responseText == "success") {
                    show_msg('精華區文章刪除成功!!');
                     location.href="/cream/"+fid;
                } else
                    show_msg(transport.responseText);
                }

            });
    }//endof function
    
    //分子版文章批次加入精華區
    function cream_tjoin(tid){
            var arr = document.getElementsByTagName('INPUT');
            var have_data=0
            var have_check = 0;
            var tid_string = "";
            for(var i = 0; i < arr.length; i++){
                if(arr[i].id == tid){
                    have_data++;
                    if(arr[i].checked == true) {
                        have_check++;
                        tid_string += ","+arr[i].value;
                    }
                }
            }
            if(have_data <=0) {
                show_msg('沒有任何文章');
                return false;
            }
            if(have_check <=0) {
                show_msg('您尚未勾選任何文章');
                return false;
            }
            tid_string = tid_string.substr(1,tid_string.length);

            new Ajax.Request("/cream_action.php", {
            parameters: $H({cream_join:     document.post_admin.cream_join.value, 
                            channel:        document.post_admin.channel.value,
                            fid:            document.post_admin.fid.value,
                            userid:         document.post_admin.userid.value,
                            tid_ary:        tid_string,
                            special_no:     document.post_admin.special_no.value,
                            user_ip:        document.post_admin.user_ip.value}).toQueryString(),
            onSuccess: function(transport) {
                if(transport.responseText == "success") {
                    show_msg('文章加入精華區成功!!');
                    location.reload();
                } else
                    show_msg(transport.responseText);
                }

            });
    }//endof function




    function msg_check(txtval){
        if(txtval.length==0)
            return false;
            var i=0;
            while (i<txtval.length) {
                if (txtval.substring(i,i+1) != ' ')
                    return true;
                    i=i+1;
            }
                return false;
    }





    //留言板張貼檢查程式
    function add_check(){
        var tf = $("Main_Form");
        if(check_null(tf.Subject,"請輸入主題") == false) return false;
        if(check_null(tf.Author,"請輸入作者") == false) return false;
        if(check_null(tf.Content,"請輸入留言內容") == false) return false;
        var bkup = $("Func_Row").innerHTML;
        $("Func_Row").innerHTML = "資料傳輸中，請稍後";
        
        //使用ajax方式存資料，避免按reload造成重覆資料進資料庫的問題
        createObject(); // 建立非同步請求物件
        xh.open("POST", "/member/msgbox/privmsg_action.php",false);  // 開啟連結
        var str = "&Subject="+encodeURIComponent(tf.Subject.value)+"&Author="+encodeURIComponent(tf.Author.value)+"&Content="+encodeURIComponent(tf.Content.value);
        //仍有問題要修改，若有輸入&的符號會造成誤判（嘟嘟說用encodeURI但測試結果仍相同）
        xh.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xh.send(str);  // 傳送請求
        alert(xh.responseText);
        if(xh.responseText != "ok"){
            alert("※※※※　系統訊息　※※※※\r\n\r\n目前無法存入資料庫，請稍後再嘗試");
            $("Func_Row").innerHTML = bkup;
        } else {
            if(confirm("※※※※　系統訊息　※※※※\r\n\r\n已成功張貼留言，\r\n\r\n您要繼續張貼下一筆嗎？")){
                tf.reset();
                $("Func_Row").innerHTML = bkup;
            } else
                location.href="index.php";
        }
        return false;
    }
    
    //判斷輸入欄位是否為空值
    function check_null(obj,msg){
        if(obj.value == ""){
            alert(msg);
            obj.focus();
            return false;
        }
        return true;
    }
    
    //換頁function
    function goto(page){
        location.href=page;
    }
    
    //回上頁
    function goback(){
        history.back();
    }


//test
/*
        new Ajax.Request("site_report_do.php", {
        parameters: $H({reply_type: rbt, 
                        advise_area:tf.advise_area.value,
                        subject:encodeURI(tf.subject.value),
                        memo: encodeURI(tf.memo.value),
                        author:encodeURI(tf.author.value),
                        tid:tf.tid.value,
                        fid:tf.fid.value,
                        my_type:"add",
                        floor:tf.floor.value,
                        ori_tid:tf.ori_tid.value}).toQueryString(),
        onSuccess: function(transport) {
            if(transport.responseText == "success") {
                show_msg('感謝您提供寶貴的資料');
                location.href="/";
            } else
                show_msg(transport.responseText);
        }
        */

    function del_msg_check() {
        var has_checked = false;
        $A($$("input.sel_del")).each(function(s){
            if(s.checked == true) has_checked = true;
        });
        if($A($$("input.sel_del")).length == 0) {
            alert("沒有資料可以刪除");
        } else if(!has_checked){
            alert("您尚未勾選任何資料");
        }
        return has_checked;
    }

    // privmsg_send.html 用
    function check_form1(){

        if(msg_check(document.privmsg_send.msubject.value)==false) {
            alert('請輸入標題!!');
            document.privmsg_send.msubject.focus();
            return false;
        }

        if(document.privmsg_send.msubject.value.length>30){
            alert('標題過長!!');
            return false;
        }

        if(msg_check(document.privmsg_send.mreceive.value)==false) {
            alert('請輸入收件人!!');
            document.privmsg_send.mreceive.focus();
            return false;
        }

        if(document.privmsg_send.mreceive.value.length>30){
            alert('收件人 Id 過長!!');
            return false;
        }

        if (msg_check(document.privmsg_send.mcontent.value)==false) {
            alert('請輸入內文!!');
            document.privmsg_send.mcontent.focus();
            return false;
        }

        if(document.privmsg_send.mcontent.value.length>250){
            alert('內文過長!!');
            return false;
        }

    }
    
    function account_chk(val){
        var reg = /^[a-z0-9_]{4,16}$/;
        return reg.test(val);
    }
    
    function Add_01(member_id,mid,sys_sid){
    
        var msg = document.privmsg_send.msg.value;
        var msubject = document.privmsg_send.msubject;
        var mreceive = document.privmsg_send.mreceive;
        var mcontent = document.privmsg_send.mcontent;
        
        if(msubject.value.length == 0){
            alert('請填入標題喔!');
            return false;
        }
        
        if(msubject.value.length > 30){
            alert('標題不得超過30個字元喔!');
            return false;
        }
        
        if (!account_chk(mreceive.value)){
            alert("您輸入的收件者名稱有問題");
            return false;
        }
        
        if(mcontent.value.length == 0){
            alert('請填入內文喔!');
            return false;
        }
        
        if(mcontent.value.length > 250){
            alert('內文不得超過250個字元喔!');
            return false;
        }

        if(gcode_test("Gcode_Space") == 0){
            return false;
        }

        new Ajax.Request("/member/msgbox/privmsg_action.php", {
            parameters: $H({
                msubject: document.privmsg_send.msubject.value, 
                mreceive: document.privmsg_send.mreceive.value,
                mcontent: document.privmsg_send.mcontent.value,
                id_pchome : member_id,
                draft_del : $('draft_del').value,
                draft_send: '1',
                del_kind  : '',
                mid       :mid,
                sys_sid   :sys_sid
                
                }).toQueryString(),

              onSuccess: function(transport) {
                if(transport.responseText == "yes"){
                    alert('寄件成功！');
                    window.location ='/msg/send/';
                }
                
                else if(transport.responseText == "no"){
                    alert('查無此人，寄件失敗！');
                }
                
                else if(transport.responseText == "收件人不存在 - 200"){
                    alert('查無此人，寄件失敗！');
                }
                
               
                else if(transport.responseText == "send msg ok - 011"){
                    alert('寄件成功');
                    window.location ='/msg/send/';

                }

                else if(transport.responseText == "收件人收訊超過限制 - 404"){
                    alert('寄送失敗！對方的信箱已滿，無法寄送！');
                }
                
                else if(transport.responseText == "寄件失敗"){
                    alert('寄送失敗！您今天的寄送數量已達上限～請明天再寄喔！-test');
                }
                
                else if(transport.responseText == "被收訊者列入黑名單無法發送訊息 - 402"){
                    //alert('由於你被對方列為黑名單，目前無法傳送訊息給他。');
                    location.href='/oops/inblacklist/2&uid='+$F("mreceive");
                }
                
                else if(transport.responseText == "標題或內文過長!!"){
                    alert('內文過長!!');
                }
                
                else if(transport.responseText == "草稿寄件成功!"){
                    alert('草稿寄件成功');
                    window.location ='/msg/sendbox/';
                }
                
                else if(transport.responseText == "草稿寄件失敗, 請洽管理員!!"){
                    alert('草稿寄件失敗, 請洽管理員!!');
                }

                else if(transport.responseText == ""){
                    
                }
                else {
                  //  document.write(transport.responseText);
                    show_msg(transport.responseText);
                }
              }
            }
       );

/*   
        if(msg == 'yes'){
            alert('寄件成功');
            window.location ='/msg/send/';
        }
*/
    }
    