var objToUpdate;
var rating;
var xmlHttp;
var thisResourceID;
// creates an XMLHttpRequest instance
function createXMLHttpRequestObject()
{
  // try to instantiate the native XMLHttpRequest object
  try
  {
    // create an XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch(e) { }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}


function rateResource(resourceType, thisObj, thisRating, resourceID) 
{
	objToUpdate = thisObj;
    rating = thisRating;
    thisResourceID = resourceID;
	
    var url = "/includes/ajax/RateResource.asp?ResourceType=" + resourceType + "&Rating=" + rating + "&ResourceID=" + resourceID;
    //window.alert(url)
	try{
	    xmlHttp =  createXMLHttpRequestObject();

		xmlHttp.onreadystatechange = callback;	
		
		//if safari, xmlHttp.open("get",url,true);
	    //else xmlHttp.open("get",url,false);
		var browser=navigator.appName
		var b_version=navigator.appVersion
		var version=parseFloat(b_version)
		if (b_version.indexOf("Safari") > 0)  {
			xmlHttp.open("get",url,false);
		}
		else{
			xmlHttp.open("get",url,true);
		}
		xmlHttp.send(null);
	} catch (e) {
		//window.alert("you need a newer browser.");
	}

}

function callback(){
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
		    adjustRatingStars();
		}
	}
}

function adjustRatingStars()
{
    var obj = document.getElementById(objToUpdate);
    
	switch(rating)
	{
	    case .5:
			obj.className = "rating halfstar";   
			obj.style.backgroundPosition = "0px -272px";
			break;
		case 1:
			obj.className = "rating onestar";   
			obj.style.backgroundPosition = "0px -96px";
			break;
		case 1.5:
			obj.className = "rating onehalfstar";   
			obj.style.backgroundPosition = "0px -288px";
			break;    
		case 2:
			obj.className = "rating twostar";
			obj.style.backgroundPosition = "0px -112px";
			break;
		case 2.5:
			obj.className = "rating twohalfstar";
			obj.style.backgroundPosition = "0px -304px";
			break;
		case 3:
			obj.className = "rating threestar";
			obj.style.backgroundPosition = "0px -128px";
			break;
		case 3.5:
			obj.className = "rating threehalfstar";
			obj.style.backgroundPosition = "0px -320px";
			break;
		case 4:
			obj.className = "rating fourstar";
			obj.style.backgroundPosition = "0px -144px";
			break;
		case 4.5:
			obj.className = "rating fourhalfstar";
			obj.style.backgroundPosition = "0px -336px";
			break;
		case 5:
			obj.className = "rating fivestar";
			obj.style.backgroundPosition = "0px -160px";
			break;
		default:
			alert("in default");
			obj.className = "rating nostar";
			obj.style.backgroundPosition = "0px 0px";
	}
	
	if (document.getElementById('ratedtext_' + thisResourceID) != null)
    {
        document.getElementById('ratedtext_' + thisResourceID).innerHTML = "(Thanks for rating!)"
    }
    
    if (document.getElementById('ratedtext2_' + thisResourceID) != null)
    {
        document.getElementById('ratedtext2_' + thisResourceID).innerHTML = "(Thanks for rating!)"
    }
	
	//window.alert("Thank you for your rating!");
}
