var req;

/*
 * Function to create AJAX object for both IE, and W3C complianted browser.
 */
function loadXMLDoc(url, params) 
{
    // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest)
  {
    try
    {   
      req = new XMLHttpRequest();
    }
    catch (e)
    {
      req = false;
    }
  }
  else if (window.ActiveXObject)
  {
    try
    {
      req = new ActiveXObject("Msxml2.XMLHTTP.4.0");
    }
    catch(e)
    {
      try
      {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        req = false;
      }
    }
  }
  
  if (req)
  {
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.send("");
  }
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200)
        {
            try
            {
           		var xmlDocument = req.responseXML;
           		
           		processXML(xmlDocument);
           		
           		
              //var responseText = req.responseText;
              //alert(responseText)
              //checkResponse(responseText);
            }
            catch (e)
            {
            	alert(e);
	          //var binaryText = req.responseBody
	          //BinaryToString(binaryText);
            }
        }
     } 
}

function processXML(xmlDocument)
{
  
  	var docRoot = xmlDocument.getElementsByTagName("module")[0];
	var moduleId = docRoot.childNodes[0].firstChild.data;
	var userId = docRoot.childNodes[1].firstChild.data;
	var rating = docRoot.childNodes[2].firstChild.data;
	var ratingsArr = new Array(5);	
	var starFreqs = xmlDocument.getElementsByTagName("int");
	var i = 0;
	for (i = 0; i < 5; i++) {
		ratingsArr[i] = starFreqs[i].childNodes[0].data;
	}
	var total = xmlDocument.getElementsByTagName("totalVotes")[0].childNodes[0].data;	
	var average = xmlDocument.getElementsByTagName("averagerating")[0].childNodes[0].data;
	
	updateModuleRating(moduleId, ratingsArr, total, average);

}

function doRating(url, moduleId, userId, rating) 
{	
	params = "?moduleId=" + moduleId + "&userId=" + userId + "&rating=" + rating;
	url = url + params;
	loadXMLDoc(url);
}

function updateModuleRating(moduleId, ratingsArr, total, average)
{
	var i = 0;
	var xml = "<module>";
	xml = xml + "<ownerid>" + moduleId + "</ownerid>";
	xml = xml + "<ratings>";
	for (i = 0; i < 5; i++) {
		xml = xml + "<int>" + ratingsArr[i] + "</int>";
	}
	xml = xml + "</ratings>";
	xml = xml + "<averagerating>" + average + "</averagerating>";
	xml = xml + "<totalvotes>" + total + "</totalvotes>";
	xml = xml + "</module>";
	
	var moduleRating = document.getElementById("module_" + moduleId);
	moduleRating.value = xml;
	
	updateStars("module_" + moduleId);
	
}

function updateModuleRatingWS(moduleId, xml)
{	
	var moduleRating = document.getElementById("module_" + moduleId);
	moduleRating.value = xml;	
	updateModules();
}

function getXML(moduleId)
{
	return document.getElementById("module_" + moduleId).value;
}

function updateBlogEntryRating(BlogEntryId, xml)
{	
	var eRating = document.getElementById("be_" + BlogEntryId);
	eRating.value = xml;
	updateStars("be_" + BlogEntryId);	
	//updateModules();
}	


