<!-- listing_rating.js -->
<!-- 5 star rating system that updates -->
<!-- 06/15/2010 CHR chnaged preSet to be a hash so we can use on summary pages -->


	var sMax = 5;	// Isthe maximum number of stars
	var holder; 	// Is the holding pattern for clicked state
	var preSet = new Hash();; 	// Is the PreSet value onces a selection has been made
	var rated;

	// Rollover for image Stars //
	function rating(num){

		group = num.id.substring(0,num.id.lastIndexOf("_")+1);

		if ($('rateStatus'+group)) {
			$('rateStatus'+group).innerHTML = "";
		}

		if(!rated){
			s = num.id.replace(group, ''); // Get the selected star
			a = 0;
			for(i=1; i<=sMax; i++){		
				if(i<=s){
					document.getElementById(group+i).className = "on";
					holder = a+1;
					a++;
				}else{
					document.getElementById(group+i).className = "";
				}
			}
		}
	} // end rsting

	// For when you roll out of the the whole thing //
	function off(me){
		group = me.id.substring(0,me.id.lastIndexOf("_")+1);

		if ($('rateStatus'+group)) {
			$('rateStatus'+group).innerHTML = "";
		}

		if(!rated){
			if(!preSet.get(group)){	
				for(i=1; i<=sMax; i++){		
					document.getElementById(group+i).className = "";
				}
			}else{
				rating(preSet.get(group));
			}
		}
	} // end off

	// When you actually rate something //
	function rateIt(me, site, cd_mls, cd_contact){
		if(!rated){
			//$("rateStatus").innerHTML = $("ratingSaved").innerHTML + " :: "+me.title;
			preSet.set(me.id.substring(0,me.id.lastIndexOf("_")+1), me);

			sendRate(me, site, cd_mls, cd_contact);
			rating(me);
		}
	} // end rateIt

	// Send the rating information somewhere using Ajax or something like that.
	function sendRate(sel, site, cd_mls, cd_contact){
		group = sel.id.substring(0,sel.id.lastIndexOf("_")+1);

		var rv =  {
		    MS_CONTEXT: "VOW_SHOPPING_CART_UPDATE",
		    WANTS: "[ { js: 'status', key: '<%= Globals._RESPONSE_CODE %>', as: 'string' } ]",
	 	    "cd_Contact": cd_contact,
	 	    "cd_MLS": cd_mls,
	 	    "CSC_CD_RATING": sel.id.replace(group, ''),
			"SITE": site
		};

		new Ajax.Request("/vp/JSONQueryServlet", 
		    {
			parameters: $H(rv).toQueryString(),
			onComplete: function(results, request) {
			    try {
					var json = "var reply = " + results.responseText + ";";
					eval(json);
					if ((reply['status'] == '2') || (reply['status'] == '1')) {
					    alert('Rating Failed!');
						//$('status').innerHTML = "Error retrieving listing";
					} else {
						$('rateStatus'+group).innerHTML = "Rating Saved!";
					}
			    } catch (e) {}
			},
			onFailure: function(){
				alert('Rating Failed!');
			}
		});

	} //end sendRate

	// When you actually rate something //
	function setInitialRating(me){
		if (me) {
			preSet.set(me.id.substring(0,me.id.lastIndexOf("_")+1), me); 
			rating(me);
		}
	}


