﻿function ReBuildResultsArray(sourceData)
{
    pollResults = new Array();
    
    var answers = sourceData.split('|');
    
    for (var i = 0; i < answers.length; i++)
    {
        choiceData = answers[i].split('/');
        pollResults[i] = new Array(new String(choiceData[0]), parseInt(choiceData[1]));  
    }        
}

function GeneratePollResults()
{
    if (pollResults)
    {
        var pollContentCell = getRef('pollContent');
        var leadingVotes = GetLeadingNbVotes();
        var totalVotes = CountTotalVotes();
        
        if (resTable != null) pollContentCell.removeChild(resTable);
        
        resTable = document.createElement("TABLE");
        resTable.className = "pollResults";
                
        aRow = resTable.insertRow(0);
        aCell = aRow.insertCell(0);
        
        for (var i=0; i < pollResults.length; i++)
        {
            var pct = Math.round((pollResults[i][1] * (100 / totalVotes)));
            
            // answer label
            var answerLabelDiv = document.createElement("DIV");            
            var answerLabel = document.createElement("DIV");
            answerLabel.className = "answer";
            answerLabel.innerHTML = pollResults[i][0];
            
            answerLabelDiv.appendChild(answerLabel);
            aCell.appendChild(answerLabelDiv);
            
            var graphTab = document.createElement("TABLE");
            graphTab.className = "answerGraph";
            gRow = graphTab.insertRow(0);
            gCell = gRow.insertCell(0);
                        
            var graphImgBlock = document.createElement("DIV");
            if (leadingVotes == pollResults[i][1]) graphImgBlock.className = "pollResultBarLeading";
            else graphImgBlock.className = "pollResultBar";
            graphImgBlock.style.width = pct + "%";
            
            // insert cell where percent rate will go to            
            pctCell = gRow.insertCell(1);
            pctCell.className = "pctRate";
            pctCell.innerHTML = pct + "%";            
            
            gCell.appendChild(graphImgBlock);            
            aCell.appendChild(graphTab);
        }
        
        //var totalVotesDiv = document.createElement("DIV");
        //totalVotesDiv.className = "totalVotes";
        //totalVotesDiv.innerHTML = "Total votes: " + totalVotes;
        //aCell.appendChild(totalVotesDiv);
        
        // add results table to the poll content cell        
        pollContentCell.appendChild(resTable);
    }
}

function GetLeadingNbVotes()
{
    var leadNb = null;
    
    for (var i=0; i < pollResults.length; i++)
    {
        if (leadNb == null) leadNb = pollResults[i][1];
        else if (leadNb < pollResults[i][1])
        {
            leadNb = pollResults[i][1];
        }            
    }
    
    return leadNb;
}

function CountTotalVotes()
{
    var nbVotes = 0;
    
    for (var i=0; i < pollResults.length; i++)
    {
        nbVotes += pollResults[i][1];
    }
    
    return nbVotes;
}

function SelectOpinionChoice(id)
{
    if (!isNaN(id)) opinionChoice = id;
}

// showes/hides object found by ID
function SetItemVisibilityByID(objID, visible)
{
    var item = getRef(objID);
    SetItemVisibilityByRef(item, visible);
}

// showes/hides object found by ID
function SetItemVisibilityByRef(obj, visible)
{
    if (obj != null)
    {
        if (visible) obj.style.display = 'block';
        else obj.style.display = 'none';
    }
}

function Vote_CallBack(response)
{
    if(response.error == null)
    {
        if (response.value.length > 1)
        {
            ReBuildResultsArray(response.value);
            GeneratePollResults();
            
            // hide vote choices and show the results
            SetItemVisibilityByID('opinionChoices', false);
            SetItemVisibilityByRef(resTable, true);
        }
        else
        {
            switch (response.value)
            {
                default:
                case '-1':
                case '-2':
                    alert('Your vote hasn\'t been accepted placed. Processing error has been found.');
                break;
                case '-3':
                    alert('Your vote has been already placed. You can\' vote more than once.');
                break;
            }
        }
    }
    else
    {
        alert('There has been an error while processing your request.');
    }
}