/**
 * @author Administrator
 */
var captchafirst = 1;
function getcaptchafirst(){
    if (captchafirst) {
        getcaptcha();
        captchafirst = 0;
    }
}

function getcaptcha(){
    dojo.xhrPost({
        handleAs: "json",
        url: '/forum/captcha/',
        timeout: 5000,
        sync: true,
        load: function(responseObject, ioArgs){
            dojo.byId('captchaimg').innerHTML = '<img src="/images/captchaimg/' + responseObject.captchaId + '.png" border="0" title="看不清，点击更新验证码." onclick="getcaptcha();"/>';
        },
        error: function(responseObject, ioArgs){
            alert('十分抱歉，系统出现未知错误。');
        },
        timeout: function(responseObject){
            alert('十分抱歉，系统超时！');
        }
    })
}

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
function open_reply(cid, tid){
    captchafirst = 1;
    TheDlg = new dijit.Dialog({
        title: "发表回复",
        style: "width: 606px",
        href: "/forum/replydialog/cid/" + cid + "/tid/" + tid
    });
    TheDlg.onLoad = function(){
        this._position();
        var sprytextfield1 = new Spry.Widget.ValidationTextField("sprycaptcha", "none", {
            minChars: 5,
            maxChars: 5,
            validation: checkcaptcha,
            useCharacterMasking: true,
            regExpFilter: /^[A-Za-z0-9]+$/
        });
		ckeditor = CKEDITOR.replace( 'ckeditor', {
			customConfig : 'http://css.rshining.com/scripts/ckeditor_forum_config.js'
		});
    }
    TheDlg.show();
    dojo.connect(TheDlg, "hide", TheDlg, function(e){
        CKEDITOR.remove(ckeditor);
        TheDlg.destroyDescendants();
		TheDlg.destroy();
    });
}

function open_replyposter(cid, tid, poster, floor){
    captchafirst = 1;
    TheDlg = new dijit.Dialog({
        title: "发表回复",
        style: "width: 606px",
        href: "/forum/replydialog/cid/" + cid + "/tid/" + tid + "/poster/" + encodeURI(poster) + "/floor/" + floor
    });
    TheDlg.onLoad = function(){
        this._position();
        var sprytextfield1 = new Spry.Widget.ValidationTextField("sprycaptcha", "none", {
            minChars: 5,
            maxChars: 5,
            validation: checkcaptcha,
            useCharacterMasking: true,
            regExpFilter: /^[A-Za-z0-9]+$/
        });
		ckeditor = CKEDITOR.replace( 'ckeditor', {
			customConfig : 'http://css.rshining.com/scripts/ckeditor_forum_config.js'
		});
    }
    TheDlg.show();
    dojo.connect(TheDlg, "hide", TheDlg, function(e){
        CKEDITOR.remove(ckeditor);
        TheDlg.destroyDescendants();
		TheDlg.destroy();
    });
}

var ckeditor;
function open_newthread(cid){
    captchafirst = 1;
    TheDlg = new dijit.Dialog({
        title: "发表新主题",
        style: "width: 606px",
        href: "/forum/newthreaddialog/cid/" + cid
    });
    TheDlg.onLoad = function(){
        this._position();
        var sprytextfield2 = new Spry.Widget.ValidationTextField("sprycaptcha", "none", {
            minChars: 5,
            maxChars: 5,
            validation: checkcaptcha,
            useCharacterMasking: true,
            regExpFilter: /^[A-Za-z0-9]+$/
        });
        var sprytextfield3 = new Spry.Widget.ValidationTextField("spryarticleT", "none", {
            minChars: 4,
            maxChars: 50,
            validateOn: ["blur"]
        });
		ckeditor = CKEDITOR.replace( 'ckeditor', {
			customConfig : 'http://css.rshining.com/scripts/ckeditor_forum_config.js'
		});
    }
    TheDlg.show();
    dojo.connect(TheDlg, "hide", TheDlg, function(e){
		CKEDITOR.remove(ckeditor);
        TheDlg.destroyDescendants();
		TheDlg.destroy();
    });
}

function open_editdialog(articleid){
    captchafirst = 1;
    TheDlg = new dijit.Dialog({
        title: "编辑帖子",
        style: "width: 606px",
        href: "/forum/editarticledialog/articleid/" + articleid
    });
    TheDlg.onLoad = function(){
        this._position();
        var sprytextfield4 = new Spry.Widget.ValidationTextField("sprycaptcha", "none", {
            minChars: 5,
            maxChars: 5,
            validation: checkcaptcha,
            useCharacterMasking: true,
            regExpFilter: /^[A-Za-z0-9]+$/
        });
        var sprytextfield5 = new Spry.Widget.ValidationTextField("spryarticleT", "none", {
            minChars: 4,
            maxChars: 50,
            validateOn: ["blur"]
        });
		ckeditor = CKEDITOR.replace( 'ckeditor', {
			customConfig : 'http://css.rshining.com/scripts/ckeditor_forum_config.js'
		});
    }
    TheDlg.show();
    dojo.connect(TheDlg, "hide", TheDlg, function(e){
        CKEDITOR.remove(ckeditor);
        TheDlg.destroyDescendants();
		TheDlg.destroy();
    });
}

function open_delarticledialog(articleid, iffirst) {
	TheDlg = new dijit.Dialog({
        title: "删除帖子",
        style: "width: 300px",
        href: "/forum/delarticledialog/articleid/" + articleid + "/iffirst/" + iffirst
    });
    TheDlg.show();
    dojo.connect(TheDlg, "hide", TheDlg, function(e){
        CKEDITOR.remove(ckeditor);
        TheDlg.destroyDescendants();
		TheDlg.destroy();
    });
}

function check(){
    var isValid = false;
    //var FCKeditorright = checkFCKeditor();
	if (Spry.Widget.Form.validate(dojo.byId('form'))) {
		isValid = true;
    }
//    if (FCKeditorright) {
//        if (Spry.Widget.Form.validate(dojo.byId('form'))) {
//            isValid = true;
//        }
//    }
    return isValid;
}

function checkcaptcha(){
    var inputcaptcha = dojo.byId('captcha').value;
    var isValid = false
    dojo.xhrPost({
        content: {
            captchacode: inputcaptcha
        },
        handleAs: "json",
        url: '/forum/checkcaptcha/',
        timeout: 5000,
        sync: true,
        load: function(responseObject, ioArgs){
            if (responseObject.isright) {
                isValid = true;
            }
            else {
                isValid = false;
            }
        },
        error: function(responseObject, ioArgs){
            isValid = false;
        },
        timeout: function(responseObject){
            isValid = false;
        }
    });
    return isValid;
}

function checkFCKeditor(){
    var content = FCKeditorAPI.GetInstance("FCKeditor").GetXHTML();
    content = content.replace(/<[^>].*?>/g, "");
    content = content.replace(/\s/g, "");
    content = content.replace(/&nbsp;/g, "");
    if (content.length < 10 || content.length > 10000) {
        alert('您的帖子长度不符合要求. \n系统限制字符数：10-10000字节.');
        isValid = false;
    }
    else {
        isValid = true;
    }
    return isValid;
}
