var GroupQuickInvite = {
    _timer : null,
    
    invite : function(){
        if($F("inviteTo").strip() == "" || $F("inviteTo").strip() == "请输入朋友的恒知ID或者电子邮件，使用逗号分隔"){
            this._setInviteMessage("请输入朋友的恒知ID或者电子邮件", false);
            this._timer = setTimeout(function(){
                $("groupInviteMsg").update("");
                $("groupInviteMsg").className = "clear";
                $("groupInviteLoading").className = "clear";
            },3000);
            return false;
        }
        new Ajax.Request("/Group.do?action=GROUP_INVITE", {
            parameters : {
                "inviteTo" : $F("inviteTo").strip(),
                "url" : $("url").getValue()
            },
            requestHeaders : {
                Accept: 'application/json'
            },
            onCreate : function(){
                $("groupInviteLoading").className = "";
                $("groupInviteLoading").update('<img src="/images/loading.gif">');
            },
            onSuccess : function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    this._setInviteMessage("邀请已发送", true);
                    $("inviteTo").value = "请输入朋友的恒知ID或者电子邮件，使用逗号分隔";
                    $("inviteTo").style.color = "#999";
                }else{
                    this._setInviteMessage(json.MESSAGE, false);
                }
                
                this._timer = setTimeout(function(){
                    $("groupInviteMsg").update("");
                    $("groupInviteMsg").className = "clear";
                    $("groupInviteLoading").className = "clear";
                },3000);
            }.bind(this),
            onComplete : function(){
                $("groupInviteLoading").update("");
            }
        })
    },

    _setInviteMessage : function(msg, type){
        $("groupInviteMsg").update(msg);
        if(type){
            $("groupInviteMsg").className = "ok_message";
        }else{
            $("groupInviteMsg").className = "wrong_message";
        }
    }
}

var GroupBasicInfo = {
    request_url : '/Group.do?action=CHECK_GROUP_URL',
    org_announcement : null,
    org_description   : null,
    checkUrl : function() {
        var url = $F("url").strip();
        if(url == ""){
            this._setMessage("群组网址不允许为空", false);
            return false;
        }
        if(url.match(/[^A-Za-z0-9._-]/) != null || url.length > 200){
            this._setMessage("群组网址只能包含数字,字母,.,_,-,长度不能超过200", false);
            return false;
        }
        new Ajax.Request(this.request_url, {
            parameters : {
                "url" : url
            },
            requestHeaders : {
                Accept: 'application/json'
            },
            onCreate : function(){
                
            }.bind(this),
            onSuccess : function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    this._setMessage("群组网址有效", true);
                }else{
                    this._setMessage(json.MESSAGE, false);
                }
            }.bind(this),
            onComplete : function(){
                
            }.bind(this)
        });
        return false;
    },

    checkForm : function() {
        if($F("title").strip() == ""){
            this._setMessage("请填写群组名称", false);
            $("title").focus();
            return;
        }
        if($F("url").strip() == ""){
            this._setMessage("请填写群组网址", false);
            $("url").focus();
            return;
        }
        if($F("description").strip() == ""){
            this._setMessage("请填写群组简介", false);
            $("description").focus();
            return;
        }
        if($F("url").strip().match(/[^A-Za-z0-9._-]/) != null || $F("url").strip().length > 200){
            this._setMessage("群组网址只能包含数字,字母,.,_,-,长度不能超过200", false);
            $("url").focus();
            return;
        }
        if($("description").innerHTML.strip().length > 10000){
            this._setMessage("简介过长", false);
            $("description").focus();
            return;
        }
        if($("official").checked) {
            if($F("officialOrgName").strip() == "") {
                this._setMessage("请填写官方组织名称", false);
                $("officialOrgName").focus();
                return;
            }
            if($F("officialOrgPhone").strip() == "") {
                this._setMessage("请填写官方联系电话", false);
                $("officialOrgPhone").focus();
                return;
            }
            var officialLink = $F("officialOrgWeb").strip();
            if(officialLink != "") {
                var linkArrar = officialLink.split("://", 2);
                if(linkArrar.size()==2){
                    var address = linkArrar[1].strip();
                    var protocal = linkArrar[0].toLowerCase();
                    if(protocal=="http"||protocal=="https"){
                        officialLink = protocal+"://"+address;
                    }else{
                        this._setMessage("网址应用http://或者https://开始", false);
                        $("officialOrgWeb").focus();
                        return;
                    }
                } else{
                    officialLink = "http://"+officialLink;
                }
            }
        }
        $("officialOrgWeb").setValue(officialLink);
        $("groupBasicForm").submit();
        return;
    },
    btnClick : function(url){
        document.location.href=url;
    },
    _setMessage : function(msg, type){
        $("message").update(msg);
        if(type){
            $("message").className = "ok_message";
        }else{
            $("message").className = "wrong_message";
        }
    },
    copy_code : function(copyText){
        if (window.clipboardData){
            copyText.select();
            window.clipboardData.setData("Text", copyText.value);
        }
        else {
            var flashcopier = 'flashcopier';
            if(!document.getElementById(flashcopier)){
                var divholder = document.createElement('div');
                divholder.id = flashcopier;
                document.body.appendChild(divholder);
            }
            document.getElementById(flashcopier).innerHTML = '';
            copyText.select();
            var divinfo = '<embed src="/swf/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(copyText.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
            document.getElementById(flashcopier).innerHTML = divinfo;
        }
    },
    updateDescription : function(url){
        var description = $("descript").getValue();//myEditor.getEditorHTML();//
        //alert("value ="+myEditor.getEditorHTML());
        //if(description.trim().length ==0 ){
        //    show_error_msg('descript_error','公告不能为空');
        //   return;
        //}

        if(description.trim().length > 10000 ){
            show_error_msg('descript_error','简介过长');
            return;
        }
        GroupBasicInfo.org_description = description;
        // if(announcement == oldAnnouncement){
        //    return;
        //}

        new Ajax.Request("/Group.do?action=SET_DESCRIPT",
        {
            parameters : {
                "url" : url,
                "description" : description
            },
            requestHeaders : {
                Accept: 'application/json'
            },
            onCreate : function(){
                $("descript_error").style.display="";
                $("descript_error").update("<img src='/images/032.gif'>");
            },
            onSuccess : function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    document.location.href="/groups/"+url;
                //$('descript_content').update(URLReplace(TextReplace(json.description)));
                //$("descript").value =json.description;
                //$("announcement_date").update(json.announcementDate);
                //$("announcement_date").style.display="";
                //$('descript_div').style.display ="none";
                //$('descript_content').style.display ="";
                }else{
            //$("announcement").setValue(oldAnnouncement);
            }
            }
        });
    },
    updateAnnouncement : function(url){
        //var oldAnnounce = $("issuance_content").getValue();
        var announcement = $("announcement").getValue();
        
        if(announcement.trim().length ==0 ){
            show_error_msg('issuance_error','公告不能为空');
            return;
        }
        if(announcement.trim().length > 2000 ){
            show_error_msg('issuance_error','公告过长');
            return;
        }
        GroupBasicInfo.org_announcement = announcement;
        // if(announcement == oldAnnouncement){
        //    return;
        //}
        new Ajax.Request("/Group.do?action=SET_ANNOUNCE",
        {
            parameters : {
                "url" : url,
                "announcement" : announcement
            },
            requestHeaders : {
                Accept: 'application/json'
            },
            onCreate : function(){
                $("issuance_error").style.display="";
                $("issuance_error").update("<img src='/images/032.gif'>");
            },
            onSuccess : function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    document.location.href="/groups/"+url;
                //alert(json.announcementDate);
                //$('issuance_content').update(URLReplace(TextReplace(announcement)));
                //$("announcement").update(announcement);
                //$("announcement_date").update(json.announcementDate);
                //$("announcement_date").style.display="";
                //$('issuance_div').style.display ="none";
                //$('issuance_content').style.display ="";
                }else{
                    show_error_msg('issuance_error',json.MESSAGE);
                //$("announcement").setValue(oldAnnouncement);
                }
            }
        });
    },

    quitGroup : function(url){
        if(!confirm("确定退出?")){
            return;
        }
        document.location.href = "/Group.do?action=QUIT_GROUP&&url=" + url;
    },

    removeGroup : function(url){
        if(!confirm("确定删除?")){
            return;
        }
        document.location.href = "/Group.do?action=REMOVE_GROUP&&url=" + url;
    },

    deleteGroupMember:function(memberId,url){
        if(!confirm("确定删除?")){
            return;
        }
        document.location.href = "/Group.do?action=REMOVE_GROUPMEMBER&memberId="+memberId+"&url="+url;
    },
    rejectGroupMember : function(memberId,url){
        if(!confirm("确定拒绝?")){
            return;
        }

        document.location.href = "/Group.do?action=REJECT_MEMBER&memberId="+memberId+"&url="+url;
    },
    pagination: function(page){
        $("group_page_num").value = page;
        var keywordInput = $("group_keyword");
        if(keywordInput != null) {
            if(keywordInput.value.trim() == "可按群组的名称及简介搜索") {
                keywordInput.value = "";
            }
        }
        $("group_search_form").submit();

    },  
    group_onmouseover: function(id){
        $(id).style.display ="block";
    },
    group_onmouseout: function(id){
        $(id).style.display ="none";
    },
    redirectPage : function(url){
        document.location.href ="/groups/"+url;
    },
    issuance : function(content,is_div,error_div,time_div){
        $(content).style.display ="none";
        $(is_div).style.display="";
        $(error_div).style.display ="none";
        if(time_div !=null){
            $(time_div).style.display="none";
        }      
        if(time_div == null){
            $("announcement_date").style.display="";
        }
         
    },
    descript : function(content,is_div,error_div){
        $(content).style.display ="none";
        $(is_div).style.display="";
        $(error_div).style.display ="none";
    },
    updateJoinInfo : function(url){
        new Ajax.Request("/Group.do?action=JOIN_GROUP",
        {
            parameters: {
                "url":url
            },
            requestHeaders : {
                Accept:'application/json'
            },
            onCreate: function(){
                $('joinInfo').update("<img src='/images/loading.gif' border='0'>");
            },
            onSuccess : function(transport){
                var json = transport.responseText.evalJSON(true);
                //alert(json.RETURN);
                if(json.RETURN){
                    $('joinInfo').className="ok_message";
                    $('joinInfo').update("成功加入群组");
                }else{
                    $('joinInfo').className="wrong_message";
                    $('joinInfo').update(json.MESSAGE);
                //$('joinInfo').update("加入群组失败");
                }

            }
        });
    },
    memberInvite : function(title,url){
        document.location.href ="/mail.jsp?title="+title+"&group="+url+"&action=invite#compose";
    },
    toggleOfficialStatus : function() {
        var name = $("officialOrgName");
        var url =  $("officialOrgWeb");
        var phone = $("officialOrgPhone");
        if($("official").checked) {
            name.enable();
            url.enable();
            phone.enable();
        }else {
            name.disable();
            url.disable();
            phone.disable();
        }
    }
}

var GroupSlide = {
    deleteSlide : function(groupUrl, slideId){
        //alert("Delete group="+groupUrl+" slideId="+slideId);
        if(!confirm("确定删除?")){
            return;
        }
        document.location.href = "/Group.do?action=DELETE_GROUPSLIDE&url="+groupUrl+"&slideId="+slideId;
    },
    saveSlide: function(){
        if($('title').value ==''){
            show_error_msg("setting_error","请输入名称");
            return;
        }else{
            $('submit_btn').disabled = true;
            $("setting_error").style.display ="none";
        }

        $('slide_setting_form').submit();
    }
}
