var sCaptcha;
var iBlogRefreshTimeout;

function count_blog_comments(iPostId)
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/count_blog_comments.php?blog_posts_id=' + iPostId);
        xmlHttp.onreadystatechange = function myFunction()
        {
            if (ready_to_process())
            {
                if (xmlHttp.responseText == '0')
                {
                    document.getElementById('count_comments').innerHTML = 'Aucun Commentaire pour le moment';
                }
                else if (xmlHttp.responseText == '1')
                {
                    document.getElementById('count_comments').innerHTML = '1 Commentaire';
                }
                else
                {
                    document.getElementById('count_comments').innerHTML = xmlHttp.responseText + ' Commentaires';
                }
            }
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('count_blog_comments(' + iPostId + ')', 250);
    }
}

function initiate_blog(iBlogPost)
{
    setTimeout('get_captcha_code()', 1000);
    count_blog_comments(iBlogPost);
    
    iBlogRefreshTimeout = setTimeout('initiate_blog(' + iBlogPost + ')', 60000);
}

function get_captcha_code()
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/get_captcha_code.php');
        xmlHttp.onreadystatechange = function myFunction()
        {
            if (ready_to_process())
            {
                if (xmlHttp.responseText)
                {
                    sCaptcha = xmlHttp.responseText;
                }
            }
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('get_captcha_code()', 250);
    }
}

function submit_comment(form)
{
    sFormCode = hex_md5(form.code.value.toUpperCase());
    
    if (form.name.value == '')
    {
        alert('Veuillez specifier votre nom');
        form.name.focus();
        return false;
    }
    
    if (form.email.value == '')
    {
        alert('Veuillez specifier votre adresse email');
        form.email.focus();
        return false;
    }
    
    if (form.subject.value == '')
    {
        alert('Veuillez fournir le sujet de votre commentaire');
        form.subject.focus();
        return false;
    }
    
    if (form.comment.value == '')
    {
        alert('Veuillez fournir le contenu de votre commentaire');
        form.comment.focus();
        return false;
    }
    
    if (sCaptcha != sFormCode)
    {
        alert('Le code de securite n\'est pas correct. Essayez a nouveau');
        return false;
    }
    
    sQuery = create_post_query(form);
    
    xmlHttp.open('GET', '/ajax_commands/post_comment.php?' + sQuery);
    xmlHttp.onreadystatechange = function myFunction()
    {
        if (ready_to_process())
        {
            if (xmlHttp.responseText == 'ok')
            {
                form.reset();
                document.getElementById('comments_box').style.display = 'none';
                document.getElementById('sucess_comment').style.display = '';
            }
        }
    }
    xmlHttp.send(null);
}

function vote_for(iCommentId)
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/cast_vote.php?blog_comments_id=' + iCommentId + '&answer=for');
        xmlHttp.onreadystatechange = function myFunction()
        {
            if (ready_to_process())
            {
                aReport = xmlHttp.responseText.split('|');
                
                document.getElementById('comment_' + iCommentId + '_for').innerHTML = aReport[0];
                document.getElementById('comment_' + iCommentId + '_against').innerHTML = aReport[1];
                
                document.getElementById('vote_result').innerHTML = 'Vous etes en accord avec ce commentaire';
                
                document.getElementById('icon_vote_for').style.display = 'none';
                document.getElementById('icon_vote_against').style.display = 'none';
                document.getElementById('text_vote_for').style.display = 'none';
                document.getElementById('text_vote_against').style.display = 'none';
            }
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('vote_for(' + iCommentId + ')', 250);
    }
}

function vote_against(iCommentId)
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/cast_vote.php?blog_comments_id=' + iCommentId + '&answer=against');
        xmlHttp.onreadystatechange = function myFunction()
        {
            if (ready_to_process())
            {
                aReport = xmlHttp.responseText.split('|');
                
                document.getElementById('comment_' + iCommentId + '_for').innerHTML = aReport[0];
                document.getElementById('comment_' + iCommentId + '_against').innerHTML = aReport[1];

                document.getElementById('vote_result').innerHTML = 'Vous etes en desaccord avec ce commentaire';
                
                document.getElementById('icon_vote_for').style.display = 'none';
                document.getElementById('icon_vote_against').style.display = 'none';
                document.getElementById('text_vote_for').style.display = 'none';
                document.getElementById('text_vote_against').style.display = 'none';
            }
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('vote_against(' + iCommentId + ')', 250);
    }
}

function report_abuse(iCommentId)
{
    if (ready_to_accept())
    {
        xmlHttp.open('GET', '/ajax_commands/report_abuse.php?blog_comments_id=' + iCommentId);
        xmlHttp.onreadystatechange = function myFunction()
        {
            if (ready_to_process())
            {
                if (xmlHttp.responseText == 'ok')
                {
                    document.getElementById('blog_comment_' + iCommentId).style.display = 'none';
                }
            }
        }
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('report_abuse(' + iCommentId + ')', 250);
    }
}