/*
Page:           rating.js
Created:        Aug 2006
Last Mod:       Mar 11 2007
Handles actions and requests for rating bars.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See readme.txt for full credit details.
--------------------------------------------------------- */

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	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 ap_small_sndReq(vote,id_num,ip_num,units) {
	var ap_small_theUL = document.getElementById('ap_small_unit_ul'+id_num); // the UL
	
	// switch UL with a loading div
	ap_small_theUL.innerHTML = '<div class="ap_small_loading"></div>';
	
    xmlhttp.open('get', 'http://www.wonderfulinfo.com/rating/ap_small/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);
    xmlhttp.onreadystatechange = ap_small_handleResponse;
    xmlhttp.send(null);	
}

function ap_small_handleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var ap_small_response = xmlhttp.responseText;
        var ap_small_update = new Array();

        if(ap_small_response.indexOf('|') != -1) {
            ap_small_update = ap_small_response.split('|');
            ap_small_changeText(ap_small_update[0], ap_small_update[1]);
        }
		}
    }
}

function ap_small_changeText( ap_small_div2show, ap_small_text ) {
    // Detect Browser
    var ap_small_IE = (document.all) ? 1 : 0;
    var ap_small_DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {ap_small_DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (ap_small_DOM) {
        var ap_small_viewer = document.getElementById(ap_small_div2show);
        ap_small_viewer.innerHTML = ap_small_text;
    }  else if(ap_small_IE) {
        document.all[ap_small_div2show].innerHTML = ap_small_text;
    }
}

/* =============================================================== */
var ap_small_ratingAction = {
		'a.ap_small_rater' : function(element){
			element.onclick = function(){

			var ap_small_parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
			var ap_small_parameterTokens = ap_small_parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
			var ap_small_parameterList = new Array();

			for (j = 0; j < ap_small_parameterTokens.length; j++) {
				var ap_small_parameterName = ap_small_parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
				var ap_small_parameterValue = ap_small_parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
				ap_small_parameterList[ap_small_parameterName] = ap_small_parameterValue;
			}
			var ap_small_theratingID = ap_small_parameterList['q'];
			var ap_small_theVote = ap_small_parameterList['j'];
			var ap_small_theuserIP = ap_small_parameterList['t'];
			var ap_small_theunits = ap_small_parameterList['c'];
			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			ap_small_sndReq(ap_small_theVote,ap_small_theratingID,ap_small_theuserIP,ap_small_theunits); return false;		
			}
		}
		
	};
ap_small_Behaviour.register(ap_small_ratingAction);
