/*
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 xmlhttp2
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp2=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp2=false
	  }
	 }
	@else
	 xmlhttp2=false
	@end @*/
	if (!xmlhttp2 && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp2 = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp2=false
	 }
	}
	function myXMLHttpRequest2() {
	  var xmlhttplocal2;
	  try {
	    xmlhttplocal22= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal2= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal2=false;
	  }
	 }

	if (!xmlhttplocal2 && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal2 = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal2=false;
	  alert('couldn\'t create xmlhttp2 object');
	 }
	}
	return(xmlhttplocal2);
}

function sndReq2(vote,id_num,units,restaurants_id) {
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
	
    xmlhttp2.open('get', 'rpc.php?j='+vote+'&q='+id_num+'&c='+units+'&r='+restaurants_id);
    xmlhttp2.onreadystatechange = handleResponse2;
    xmlhttp2.send(null);	
}

function handleResponse2() {
  if(xmlhttp2.readyState == 4){
		if (xmlhttp2.status == 200){
       	
        var response = xmlhttp2.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText2(update[0], update[1]);
        }
		}
    }
}

function changeText2( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    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"); // onclick="sndReq2('j=1&q=2&t=127.0.0.1&c=5');
			var parameterTokens = parameterString.split("&"); // onclick="sndReq2('j=1,q=2,t=127.0.0.1,c=5');
			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 theunits = parameterList['c'];
			var restaurants_id = parameterList['r'];
			
			//alert('sndReq2('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			sndReq2(theVote,theratingID,theunits,restaurants_id); return false;		
			}
		}
		
	};
	
	
Behaviour2.register(ratingAction);
