	/*
	Plugin Name: Comment Karma
	Plugin URI: http://cyber-knowledge.net/blog/2006/10/15/wordpress-plugin-rate-your-comments-comment-karma/
	Description: Allows you to rate comments up or down.
	Author: Alex Bailey and Kirk
	Author URI: http://cyber-knowledge.net
	Version: 1.1
	*/ 

	/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	*/

function createXMLHttpRequest(){
	//alert("create request call");
    var xmlhttp = null;
    try {
        // Moz supports XMLHttpRequest. IE uses ActiveX.
        // browser detction is bad. object detection works for any browser
        xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        // browser doesn’t support ajax. handle however you want
        //document.getElementById("errormsg").innerHTML = "Your browser doesnt support XMLHttpRequest.";
        alert("Your browser does not support the XMLHttpRequest Object!");
    }
	//alert("refresh voting");
    return xmlhttp;
	
}

var xhr = createXMLHttpRequest();

function karma(id, action, path, t_prefix, user_ID){
	//alert("karma func called");
	//alert(id);
	//alert(action);
	//alert(path);
	//alert(t_prefix);
    xhr.open('get', 'http\://'+ path +'ck-processkarma.php?id='+ id +'&action='+ action +'&path='+ path +'&prefix='+ t_prefix+'&user_ID='+ user_ID);
    xhr.onreadystatechange = handleResponse;
    xhr.send(null);
}

function handleResponse(){
	//alert("handle response func called");
    if(xhr.readyState == 4){
        var response = xhr.responseText.split('|');
        if(response[0] == 'done'){
            if(response[1]){
                
				/*
                document.getElementById("down-"+response[1]).src        = "http://"+response[3]+'images/gray_down.png';
                document.getElementById("down-"+response[1]).onclick    = '';
                document.getElementById("up-"+response[1]).src          = "http://"+response[3]+'images/gray_up.png';
                document.getElementById("up-"+response[1]).onclick      = '';
		*/		
                //Update the karma number display
                //Grab prefix for minus and positive numbers
                if(!response[2]){
                	alert("Error voting.");
                }
				var vdiv = '<ul><li><span id="up-'+response[1]+'" class="contentRatingPositive" alt="You can only vote once per comment" title="You can only vote once per comment">';
				vdiv += '<span id="'+response[1]+'_up" class="rating_num_up">'+response[2]+'</span></span></li>';
				vdiv += '<li class="contentRated"></li>';
				vdiv += '<li><span id="down-'+response[1]+'" class="contentRatingNegative" alt="You can only vote once per comment" title="You can only vote once per comment" >';
				vdiv += '<span id="'+response[3]+'_down" class="rating_num_down">'+response[3]+'</span></span></li>';
				vdiv += '</ul></div>';
                document.getElementById(response[1]+'_comments_container').innerHTML = vdiv;
            }
        }
        else if(response[0] == 'error')
        {
            var error = 'Error: '+response[1];
        } else {
        	alert(error);
        }
		
    } 
}
