﻿//var locationRateProfileXML = '<%=ResolveUrl("~/Xml/RateProfile.aspx?pid={pid}&r={r}&g={g}&u={u}")%>';

var parseSubmitRatingAnswer;

function submitRating(id, profileID, rating)
{
    var url = locationRateProfileXML;
    url = url.replace('{pid}', profileID);
    url = url.replace('{r}', rating);
    url = url.replace('{g}', id);
    url = url.replace('{u}', encodeURI(pageUrl));
    
    getXMLdoc(url, parseSubmitRatingAnswer);
}

parseSubmitRatingAnswer = function (xhr)
{
    var xml = xhr.responseText;
    var xmlAnswer;
    if (window.ActiveXObject)
    {
        xmlAnswer = new ActiveXObject("msxml.DOMDocument");
        xmlAnswer.loadXML(xml);
    } else if (document.implementation && document.implementation.createDocument)
    {
        parser = new DOMParser();
        xmlAnswer = parser.parseFromString(xml, "text/xml");
    }
    
	var answer = xmlAnswer.selectSingleNode("/result/answer");
	var success = answer.attributes[0].value;
	var newrating = answer.attributes[1].value;
	var newrates = answer.attributes[2].value;
	var loginurl = answer.attributes[3].value;
	var alreadyVoted = answer.attributes[4].value;
	var rateControlID = answer.attributes[5].value;
	var submittedRating = answer.attributes[6].value;
	
	if (success == 'true')
	{
	    //setRating(rateControlID, submittedRating);
	    setRating(rateControlID, newrating);
	    updateTotalVotes(rateControlID, newrates);
	}
	else
	{
	    if (alreadyVoted == 'true')
	    {
	        showAlreadyVotedMessage(rateControlID);
	    }
	    else
	    {
	        location.href = loginurl;
	    }
	}
}

function setRating(id, rating)
{
    var obj = $('currentRating' + id);
    var ratingWidth = 100/5*rating;
    obj.style.width = ratingWidth + '%';
}

function updateTotalVotes(id, value)
{
    var obj = $('totalVotes' + id);
    obj.innerHTML = value;
}

function hideCurrentRating(id)
{
    var obj = $('currentRating' + id);
    obj.style.visibility = 'hidden';
}

function showCurrentRating(id)
{
    var obj = $('currentRating' + id);
    obj.style.visibility = 'visible';
}

function showAlreadyVotedMessage(id)
{
    var obj = $('alreadyVoted' + id);
    obj.style.display = 'block';
}

function hideAlreadyVotedMessage(id)
{
    var obj = $('alreadyVoted' + id);
    new Effect.Fade(obj, {duration:0.6});
}