/**
 * Comments
 *
 * @package    wsCat Jx
 * @version    1.0
 * @since      14.02.2009
 * @copyright  2006-2009 5Dev
 * @link       http://5dev.com
 */
function Msg_Comments(err, form) 
{ 
    this.__construct(err, form); 
}

Msg_Comments.prototype = {
    
    mAjaxFlag: false,
    mChInP:    false,
    mErr:      null,
    errFlag:   false,
    mForm:     null,

    __construct: function(err, form)
    {
        this.mErr  = err;
        this.mForm = form;
    },

    ShowErr: function(flag, id)
    {
        if (flag)
        {
            _v(id + '_err1').className     = 'active_error';
            _v(id + '_err2').style.display = 'inline';

            if (!this.errFlag)
            {
                this.mForm.elements['CI[' + id + ']'].focus();
                this.errFlag = true;
            }
        }
        else
        {
            _v(id + '_err1').className     = '';
            _v(id + '_err2').style.display = 'none';
        }
    },

    CheckCode: function(id, code)
    {
        var req = new JsHttpRequest();
        req.onreadystatechange = function()
        {
            if (4 == req.readyState)
            {
                if (true == req.responseJS.errCode)
                    oCmt.ShowErr(true, 'code');
                else
                {
                    oCmt.ShowErr(false, 'code');

                    if (!oCmt.errFlag && !oCmt.mChInP)
                       oCmt.mForm.submit();
               
                    oCmt.mAjaxFlag = false;
                }
            }
        }
        
        req.caching = false;
        req.open('GET', '/index.php', true);
        req.send({gate: 'ajax', what: 'cmt', mod: 'captcha', id: id, code: code});
    },

    CheckComment: function(type)
    {
        if (null == type) type = 1;

        this.mChInP  = true;
        var arr      = this.mForm.elements;
        this.errFlag = false;

        if (5 > arr['CI[code]'].value.length)
            this.ShowErr(true, 'code');
        else
        {
            this.ShowErr(false, 'code');
            if (1 == type)
            {
                this.CheckCode(arr['CI[code_id]'].value, arr['CI[code]'].value);
                this.mAjaxFlag = true;
            }
        }

        if (1 > arr['CI[message]'].value.length)
            this.ShowErr(true, 'message');
        else
        {
            var regMess1 = /<[^>]+?on[a-z\s]+=[^>]+>/i;
            var regMess2 = /<[^>]*?(javascript|onclick|onmouseover|onmouseout|onfocus|onblur|onkeypress|onkeyup|onkeydown)[^>]*?>/i;

            if (regMess1.test(arr['CI[message]'].value)
                || regMess2.test(arr['CI[message]'].value)
               )
                this.ShowErr(true, 'message');
            else
                this.ShowErr(false, 'message');
        }

        this.mChInP = false;

        return (!this.mAjaxFlag) ? !this.errFlag : false;
    },

    AddComment: function()
    {
        if (this.CheckComment(1))
            this.mForm.submit();
    },

    PreviewComment: function()
    {       
        if (this.CheckComment(2))
        {
            entered_name    = _v('postcomment_entered_name').value;
            entered_comment = _v('postcomment_entered_comment').value;
    
            _v('div_postcomment_preview').style.display = 'block';
            _v('div_postcomment').style.display = 'none';
            
            _v('postcomment_preview_entered_comment').innerHTML = entered_comment;
            _v('postcomment_preview_entered_name').innerHTML = entered_name;
        }
    },
    
    EditComment: function()
    {
        _v('div_postcomment_preview').style.display = 'none';
        _v('div_postcomment').style.display = 'block';
        _v('div_postcomment_preview').style.background = '#ffffff';
    }
}