/**
* Make post request
*
* @param {String} url
* @param {String} stream
* @param {Function} callback
* @return Void
*/
function ajaxPost(url, stream, callback) {
    ajaxRequest(url, stream, callback, 'POST');
}

/**
* Make get request
*
* @param {String} url
* @param {Function} callback
* @return Void
*/
function ajaxGet(url, callback) {
    ajaxRequest(url, '' , callback, 'GET');
}

/**
* Make ajax request
*
* @param {String}   url
* @param {String}   stream
* @param {Function} callback
* @param {String}   method
* @return Void
*/
function ajaxRequest(url, stream, callback, method) {
    // Set method
    if(typeof method != 'string') {
        method = 'POST';
    }

    // New XML request object
    var xmlhttp = new XMLHttpRequest();

    // Open
    xmlhttp.open(method, url, true);

    // Set header
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    // Set onreadystate function
    xmlhttp.onreadystatechange=function() {
        if(xmlhttp.readyState == 4) {
            // Make callback with response
            callback(xmlhttp.responseText);
        }
    };

    // Send
    xmlhttp.send(stream);
}

/**
 * Get element
 *
 * @param {String} id
 * @return Mixed
 */
function $(id) {
    var obj = document.getElementById(id);
    if (typeof obj == 'undefined') {
        return null;
    }

    return obj;
}

/**
 * Report comment
 *
 * @param {Number} articleId
 * @param {Number} commentId
 * @param {Number} sub
 * @param {Number} sec
 * @return Void
 */
function reportComment(articleId, commentId, sub, sec) {
    var c = confirm("Sobimatu kommentaar\n\nOled kindel, et valitud kommentaar on sobimatu?")
    if (c === true){
        var s = 'http://www.saartehaal.ee/report_bad_comment.php?article_id=' + articleId + '&comment_id=' + commentId + '&sub=' + sub + '&sec=' + sec;
        ajaxGet(s, function(response) {
            $('rbc_' + articleId + '_' + commentId).style.color =           '#C8C8C8';
            $('rbc_' + articleId + '_' + commentId).style.backgroundColor = '#F5F5F5';
            $('rbc_' + articleId + '_' + commentId).disabled = true;
        });
    }
}
