function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(action) {
    http.open('get', 'rpc.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function vote(article, vote, size) {
    http.open('get', 'vote.php?article_id='+article+'&vote='+vote+'&size='+size);
    http.onreadystatechange = handleVoteResponse;
    http.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
        }
        else {
        	alert(response);
        }
    }
}

function handleVoteResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            var votes = update[1];
            
            var voteClass = "votes"; 
            if(votes > 0) {
            	votes = "+" + votes;
            }
            else if(votes == 0) {
            	voteClass += "-zero";
            }
            if(votes < 0) {
            	voteClass += "-negative";
            }
            document.getElementById(update[0]).innerHTML = votes;
            document.getElementById(update[0]).className = voteClass;
        }
        else {
        	alert(response);
        }
    }
}

