/*

Written  by: Dwilkinsjr
Written for: Pixelpost 1.6.0 www: http://www.pixelpost.org/
Based on: Unobtrusive AJAX Star Rating Bar ( http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar )
By: Ryan Masuga
Note: This file handles actions and requests for rating bars.

    <!--
     
          __         _ ____   _              _
     ____/ /      __(_) / /__(_)___  _____  (_)____
    / __  / | /| / / / / //_/ / __ \/ ___/ / / ___/    AJAX RATINGS - RATING.JS v1.0
   / /_/ /| |/ |/ / / / ,< / / / / (__  ) / / /        LAST UPDATED - OCT/08/2007
   \__,_/ |__/|__/_/_/_/|_/_/_/ /_/____/_/ /_/
                 http://dwilkinsjr.com/___/
                                       
                                       
    -->

Contact: dwilkinsjr@dwilkinsjr.com
Copyright (c) 2007 <http://wwww.dwilkinsjr.com>

License: http://www.gnu.org/copyleft/gpl.html

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.

*/

var xmlhttp

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false
	}
}

function myXMLHttpRequest() {
	var xmlhttplocal;
	try {
		xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
			xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
		} catch (E) {
			xmlhttplocal=false;
		}
	}
	
	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
		try {
			var xmlhttplocal = new XMLHttpRequest();
		} catch (e) {
			var xmlhttplocal=false;
			alert('couldn\'t create xmlhttp object');
		}
	}
	return(xmlhttplocal);
}

function dk_sndReq(vote,id_num,ip_num,units,rateinfo,imgwth,lang,name) {
	var theUL = document.getElementById('unit_ul'+id_num);
	
	theUL.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'addons/_ajaxratings/ajax_ratings/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units+'&r='+rateinfo+'&i='+imgwth+'&l='+lang+'&n='+name);
    xmlhttp.onreadystatechange = dk_handleResponse;
    xmlhttp.send(null);	
}

function dk_handleResponse() {
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;
			var update = new Array();
			
			if(response.indexOf('|') != -1) {
                update = response.split('|');
                dk_changeText(update[0], update[1]);
                dk_changeText(update[2], update[3]);
				dk_changeText(update[4], update[5]);
            }
		}
	}
}

function dk_changeText(div2show,text) {
	// Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};
    
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

var ratingAction = {
	'a.rater' : function(element){
		element.onclick = function(){
	
			var parameterString = this.href.replace(/.*\?(.*)/, "$1");
			var parameterTokens = parameterString.split("&");
			var parameterList = new Array();
			
			for (j = 0; j < parameterTokens.length; j++) {
				var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
				var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
				parameterList[parameterName] = parameterValue;
			}
			var theratingID = parameterList['q'];
			var theVote = parameterList['j'];
			var theuserIP = parameterList['t'];
			var theunits = parameterList['c'];
			var therateInfo = parameterList['r'];
			var theimageW = parameterList['i'];
			var thelang = parameterList['l'];
			var thename = parameterList['n'];
			
			//alert('dk_sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+','+therateInfo+','+theimageW+','+thelang+','+thename+')'); return false;
			
			dk_sndReq(theVote,theratingID,theuserIP,theunits,therateInfo,theimageW,thelang,thename); return false;		
		}
	}	
};
Behaviour.register(ratingAction);

