	/*
   Plugin Name: Comment Rating
	Plugin URI: http://wealthynetizen.com/wordpress-plugin-comment-rating/
	Description: Allows visitors to rate the comments on your blog in a Like vs. Dislike fashion.  Clickable images and ratings are automatically inserted into each comment.  Comments disliked too much by readers will be hidden in a click-to-show link. 
	Author: Bob King
	Author URI: http://wealthynetizen.com
	Version: 2.3.2
	*/ 

	/*
	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(){
    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!");
    }
    return xmlhttp;
}

var xhr = createXMLHttpRequest();

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

function handleResponse(){
    if(xhr.readyState == 4){
        var response = xhr.responseText.split('|');
        var karmanumber = response[2];
       
        if(response[0] == 'done'){
            if(response[1]){
                //Changes the thumbs to dull gray and disable the action
                if (response[4] == 'up') {
                   document.getElementById("up-"+response[1]).src   = "http://"+response[3]+'images/checkmark.png';
				}
                else {
                   document.getElementById("up-"+response[1]).src   = "http://"+response[3]+'images/gray_up.png';				
                }
                document.getElementById("up-"+response[1]).onclick      = '';
                //Update the karma number display
                if(!response[2]){
                	alert("Response has no value");
                }
                var karmanumber = response[2];

                //The below line is commented out because there is no karma number atm.
                if (document.getElementById("karma-"+response[1]+"-"+response[4]) != null) {
					  // Commented to include the text
//                   document.getElementById("karma-"+response[1]+"-"+response[4]).innerHTML = karmanumber;
				   // Added clause to include the difference

//				   document.getElementById("ahref-"+response[1]).innerHTML = "<b>You and "+ newkarmanumber + "</b> people like this post";
				   
				   // to remove the Like anchor text after rendering
				   document.getElementById("ahref-"+response[1]).innerHTML = "&nbsp;";

				   if(karmanumber == 1 ){
				   document.getElementById("karma-"+response[1]+"-up").innerHTML = "You like this comment";}
				   else if(karmanumber > 1 ){
				   document.getElementById("karma-"+response[1]+"-up").innerHTML = "You and "+ (karmanumber-1) + " more people like this comment";
				   }
                document.getElementById("karma-"+response[1]+"-up").style.padding='4px';
				document.getElementById("karma-"+response[1]+"-up").style.border='0px solid rgb(153, 153, 153)';
				document.getElementById("number-"+response[1]+"").innerHTML = "";
                document.getElementById("number-"+response[1]).style.padding='0px';
				document.getElementById("number-"+response[1]).style.border='0px solid rgb(153, 153, 153)';
				}

				// deal with the single value total
                if (document.getElementById("karma-"+response[1]+"-total") != null) {
	                 document.getElementById("karma-"+response[1]+"-total").innerHTML = response[5];
    
                }

            } else {
                alert("WTF ?");
            }
        }
        else if(response[0] == 'error')
        {
            var error = 'Error: '+response[1];
            // alert(error);
        } else {
        	// alert("Reponse: "+response[0]);
            // alert("Karma not changed, please try again later.");
        }
    }
}

var crToggleComment = 0;

function crSwitchDisplay(id) {
   if (crToggleComment % 2 == 0) { crShowdiv(id); }
   else { crHidediv(id); }
   crToggleComment++;
}

// hide <div id='a2' style="display:none;"> tagged div ID blocks
function crHidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

// show <div id='a2' style="display:none;"> tagged div ID blocks
// <a href="javascript:crShowdiv('a2');">show a2</a>

function crShowdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

