/*
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 sndReq(vote,id_num,ip_num,units) {
function sndReq(id_num,vote_cat,vote,ip_num,u_name,u_id,u_wt,units) {
	var theUL = document.getElementById('unit_ul'+id_num+'c'+vote_cat); // the UL
	
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
	
    //xmlhttp.open('get', 'ratings/rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);

    xmlhttp.open('get', 'ratings/rpc.php?n='+id_num+'&c='+vote_cat+'&v='+vote+'&i='+ip_num+'&u='+u_name+'&d='+u_id+'&w='+u_wt+'&x='+units);

    xmlhttp.onreadystatechange = handleResponse;

    xmlhttp.send(null);	

}

function Func1()
{
    var Bogus = 0; 
} 


function handleResponse() {
  if(xmlhttp.readyState != 4){
 	 setTimeout("Func1()", 100);
  }

  if(xmlhttp.readyState == 4){

	if (xmlhttp.status == 200){
        	var response = xmlhttp.responseText;
	        var update = new Array();

	        if(response.indexOf('|') != -1) {
	            update = response.split('|');
	            changeText(update[0], update[1]);

        	}
	}else{
		alert('ERROR: xmlhttp.status='+xmlhttp.status+'. Please refresh page.');
	}
  }
}

function changeText( 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;
    }
}


/* ==========================CMS - Bury================================ */
// Courtesy of SimplytheBest.net - http://simplythebest.net/scripts/
// postnum: which postnumtion
// fld: which Field ID
function toggleT(postnum) {
	chkd = false;
	brow = 'N/A'
	bver = 0
	fld = ''
	///Detect Browser
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ 	//test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
		var ffversion=new Number(RegExp.$1);	 // capture x.x portion and store as a number

		brow = 'FF';
		bver = parseInt(ffversion);
	} else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){	//test for MSIE x.x;
		var ieversion=new Number(RegExp.$1);	// capture x.x portion and store as a number
		brow = 'IE';
		bver = parseInt(ieversion);
	}
	//document.write("You're using " + brow + " " + bver);

	//fldtr = TR1234 = Post TR 
	  fldtr = "TR" + postnum.toString();
	//fldtd = TD1234 = Post TD --NO
	  fldtd  = "TD" + postnum.toString();
	//fldctr = CHKTR1234 = Checkbox TR 
	  fldctr = "CHKTR" + postnum.toString();
	//fldcbx = CHKBX1234 = Checkbox 
	  fldcbx = "CHKBX" + postnum.toString();
	//fldleft = LEFT1234 = Left User Column 
	  fldleft = "LEFT" + postnum.toString();




	switch(brow)
	{
	case 'FF':		//WORKS WITH BOTH!!!!
	case 'IE':
		//document.write("IE");


		if(document.getElementById(fldcbx))
		{

			if(document.getElementById(fldcbx).checked == true) { chkd = true }
		}

		if(document.getElementById(fldtr))
		{
			//alert("View post = " + chkd);
			if (chkd) {	//show post, hide checkbox.
				//eval("document.all."+fld+".style.visibility='visible';");
				document.getElementById(fldtr).style.visibility = 'visible';
				document.getElementById(fldtr).style.display = 'inline';

				document.getElementById(fldleft).style.visibility = 'visible';
				document.getElementById(fldleft).style.display = 'inline';

				//document.getElementById(fldctr).style.visibility = 'hidden';
				//document.getElementById(fldctr).style.display = 'none';


			} else {	//show checkbox, hide post
				//eval("document.all."+fld+".style.visibility='hidden';");
				document.getElementById(fldctr).style.visibility = 'visible';
				document.getElementById(fldctr).style.display = 'inline';

				document.getElementById(fldtr).style.visibility = 'hidden';
				document.getElementById(fldtr).style.display = 'none';

				document.getElementById(fldleft).style.visibility = 'hidden';
				document.getElementById(fldleft).style.display = 'none';

			}
		}
		break;
	default:
		//Alert('Default');
		//document.write("Default");
		tmp2 = document.layers[fld].value.length;
		if(tmp2 >= 1) { chkd = true; }

		if (chkd) {
			eval("document.layers['"+fldtr+"'].visibility='show';");
			eval("document.layers['"+fldtr+"'].display='inline';");

			eval("document.layers['"+fldctr+"'].visibility='hide';");
			eval("document.layers['"+fldctr+"'].display='none';");

		} else {

			eval("document.layers['"+fldctr+"'].visibility='show';");
			eval("document.layers['"+fldctr+"'].display='inline';");

			eval("document.layers['"+fldtr+"'].visibility='hide';");
			eval("document.layers['"+fldtr+"'].display='none';");

		}
		break;
	}

}
/* ==========================CMS - Bury================================ */



/* =============================================================== */
var ratingAction = {
		'a.rater' : function(element){
			element.onclick = function(){

			

			var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
			var parameterTokens = parameterString.split("&"); // onclick="sndReq('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['n'];	//id Number-name
			var thecat = parameterList['c'];	//cat
			var theVote = parameterList['v'];	//vote
			var theuserIP = parameterList['i'];	//user_ip
			var theuserName = parameterList['u'];	//user_name
			var theuserID = parameterList['d'];	//user_id
			var theuserWT = parameterList['w'];	//user_weight
			var theunits = parameterList['x'];	// (units)
			//rec_id
			//user_weight
			//add_date

			//P13541-9-2-192.168.0.250-Big%20Brother-2-3-4)
			theuserName = theuserName.replace("%20", " ");


			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			// sndReq(theVote,theratingID,theuserIP,theunits); 	
			//setTimeout("sndReq(theratingID,thecat,theVote,theuserIP,theuserName,theuserID,theuserWT,theunits)", 10000);
			sndReq(theratingID,thecat,theVote,theuserIP,theuserName,theuserID,theuserWT,theunits);
			return false;		
			}
		}
		
	};
Behaviour.register(ratingAction);
