var xmlhttp;

var myDiv = new Array();
var myProduct = new Array();
var myID = new Array();

var myIDList = new Array();
var myRateList = new Array();

var myCurrentID = null;


var xmlhttp=GetXmlHttpObject();

function buildRatingElement( id , productID ){
	if (xmlhttp==null){
		// AJAX is disabled, so don't show the divs.
		hidediv( id );
	}else{
		// AJAX is enabled, so show the divs.
		showdiv( id );
		// Add this thing to the array.
		myDiv.push( document.getElementById(id) );
		myProduct.push( productID );
		myID.push( id );
		myIDList.push( id );
		myRateList.push( -3 ); // indicates we just want status	
		processNextAJAX();
	}
}

function processNextAJAX(){
	if ( myCurrentID == null ){
		myCurrentID = myIDList.pop();
		rate = myRateList.pop();
		if ( myCurrentID != null ){
			// and request the rating from the server
			requestRating( myCurrentID , rate );		
		}
	}
}


function requestRating(id , rate){
	if (xmlhttp==null) return;

//alert( id+","+rate );
	for (i=0;i<myID.length;i++){
		if ( (myID[i] == id) || (myProduct[i]==id) ){
			var url="getrating.php";
			url=url+"?product="+myProduct[i];
			if ( rate >= 0 && rate <=6 ){ url=url+"&rate="+rate; }
			if ( rate == -8 ) { url=url+"&clear=true"; }
			if ( rate == -5 ) { url=url+"&owned=1"; }
			if ( rate == -6 ) { url=url+"&owned=0"; }
			url=url+"&sid="+Math.random();
			xmlhttp.onreadystatechange=stateChanged;
			xmlhttp.open("GET",url,true);
			xmlhttp.send(null);
		}
	}
}

function setRating( idbase , rating, type ){
	for ( i=1; i<6 ; i++ ){
		if ( i <= rating ){
			setDivClass( idbase+"."+i  , "rating_icon rate_"+type+"on" );
		}else{
			setDivClass( idbase+"."+i  , "rating_icon rate_"+type+"off" );
		}
	}
}

function submitRating( idbase , rating , productID ){
	myIDList.push( productID );
	myRateList.push( rating );
	// not this now queues, so...
	processNextAJAX();
	// and finally we make the div say submitting
	for (i=0;i<myID.length;i++){
		if ( (myProduct[i]==productID) ){
			myDiv[i].innerHTML="submitting...";
		}
	}
}

function submitownRating( idbase , ownership , productID ){
	myIDList.push( productID );
	myRateList.push( -6 + ownership );	// note -6/-5 inidcats a ownership change
	// not this now queues, so...
	processNextAJAX();
	// and finally we make the div say submitting
	for (i=0;i<myID.length;i++){
		if ( (myProduct[i]==productID) ){
			myDiv[i].innerHTML="submitting...";
		}
	}
}


function submitclearRating( idbase , clear , productID ){
	myIDList.push( productID );
	myRateList.push( -8 );	// note -8 inidcats the clear command
	// not this now queues, so...
	processNextAJAX();
	// and finally we make the div say submitting
	for (i=0;i<myID.length;i++){
		if ( (myProduct[i]==productID) ){
			myDiv[i].innerHTML="submitting...";
		}
	}
}

function setDivClass( id , cls ){
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).className = cls;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.className = cls;
		}
		else { // IE 4
			document.all.id.className = cls;
		}
	}
}


function hidediv(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';
		}
	}
}

function showdiv(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';
		}
	}
}

function stateChanged(){
        setTimeout("stateChanged2()",500);
}

function stateChanged2(){
	if (xmlhttp.readyState==4){
		for (i=0;i<myID.length;i++){
			if ( (myID[i] == myCurrentID) || (myProduct[i]==myCurrentID) ){
					myDiv[i].innerHTML=xmlhttp.responseText;
			}
		}
		myCurrentID = null;
		processNextAJAX();
	}
}

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject){
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
