/*
    comments.js
    This file is for use by the comments engine to handle the comments form.

    Dependencies: form-charlimit.js, form.js 
*/
function check_comment_text() {
    var comment_text = document.getElementById('comment_text').value;
    var comment_username = document.getElementById('comment_username').value;
    var comment_email = document.getElementById('comment_email').value;
    var comment_website = document.getElementById('comment_website').value;
    var sURL = '/comments/api/validate/';
    var callback =
    {
        success: function(o) {
            var error = document.getElementById('commenterror');
            var response = new String(o.responseText);
            
            if(response.match(/PROFANITY_TEXT/)){
                error.innerHTML = 'Your comment contains profanity, please edit your comment and re-submit.';
                error.style.display = 'block';
            } else if(response.match(/PROFANITY_USERNAME/)){
                error.innerHTML = 'Your name contains profanity, please edit your name and re-submit.';
                error.style.display = 'block';
            } else if(response.match(/PROFANITY_WEBSITE/)){
                error.innerHTML = 'Your website contains profanity, please edit your website and re-submit.';
                error.style.display = 'block';
            } else if(response.match(/PROFANITY_EMAIL/)){
                error.innerHTML = 'Your email contains profanity, please edit your email and re-submit.';
                error.style.display = 'block';
            } else if(response.match(/EMPTY/)){
                error.innerHTML = 'Your comment must contain text, please edit your comment and re-submit.';
                error.style.display = 'block';
            } else if (response.match(/TEXT_LENGTH/)){
                error.innerHTML = 'Your comment is too long.  Please shorten it and re-submit.';
                error.style.display = 'block';
            } else if (response.match(/USERNAME_LENGTH/)){
                error.innerHTML = 'Your username is too long.  Please shorten it and re-submit.';
                error.style.display = 'block';
            } else if (response.match(/WEBSITE_LENGTH/)){
                error.innerHTML = 'Your website is too long.  Please shorten it and re-submit.';
                error.style.display = 'block';
            } else if (response.match(/EMAIL_LENGTH/)){
                error.innerHTML = 'Your email is too long.  Please shorten it and re-submit.';
                error.style.display = 'block';
            } else {
                submit_comment();
                window.location.reload(); 
            }
        },
        failure: function(o) {  }
    }
    var request = YAHOO.util.Connect.asyncRequest('POST', sURL, callback, "comment_text="+comment_text+"&comment_username="+comment_username
                                                                          +"&comment_website="+comment_website+"&comment_email="+comment_email);
}

function submit_comment() {
    var comment_text = document.getElementById('comment_text').value;
    var comment_username = document.getElementById('comment_username').value;
    var comment_email = document.getElementById('comment_email').value;
    var comment_website = document.getElementById('comment_website').value;
    var service_id = document.getElementById('service_id').value;
    var service_external_id = document.getElementById('service_external_id').value;
    var sURL = '/comments/api/post/';
    
    var callback = 
    {   
        success: function(o) { },
        failure: function(o) { }
    }
     var request = YAHOO.util.Connect.asyncRequest('POST', sURL, callback, "comment_text="+comment_text+"&comment_username="+comment_username
                                                                          +"&comment_website="+comment_website+"&comment_email="+comment_email
                                                                          +"&service_id="+service_id+"&service_external_id="+service_external_id); 
}
