﻿// JScript File

function SetStates(specialId)
{
    if(document.getElementById(specialId))
    {
        document.getElementById(specialId).disabled='disabled';
    }
}

function ChangeStates(obj)
{
    for(var i=0;i<obj.parentNode.getElementsByTagName("INPUT").length; i++)
    {
        obj.parentNode.getElementsByTagName("INPUT")[i].disabled='';
    }
    obj.disabled='disabled';
}

function MakeScore_Send(obj, objectId, specialId, meter, annotation)
{    
    var strpost = "objectId="+objectId+"&specialId="+specialId+"&meter="+meter+"&annotation="+annotation;
    //parentElement works ok in IE6.0,but dont in Firefox
    //alert(obj.parentNode);
    var ajaxMakeScore = new Ajax(null, null,"/WebService/WsVote.asmx/MakeScore",0,strpost,function(rexm){MakeScore_Get(obj, objectId, specialId, rexm)});
    ajaxMakeScore.post();
}

function MakeScore_Get(obj, objectId, specialId, rexm){
    ChangeStates(obj);
    GetTotalScore_Set(obj, objectId, specialId);
}

function GetTotalScore_Set(obj, objectId, specialId){
    var strpost = "objectId="+objectId+"&specialId="+specialId;
    //alert(obj.parentElement.nextSibling.innerHTML);
    var ajaxGetTotalScore = new Ajax(null, null,"/WebService/WsVote.asmx/GetTotalScore",0,strpost,function(rexm){GetTotalScore_Get(obj,rexm)});
    ajaxGetTotalScore.get();
}

function GetTotalScore_Get(obj, rexm){

        xmlDoc=rexm;
        xmlObj=xmlDoc.documentElement;

        //because of the stupid firefox,
        try{
        countPoor = parseInt(xmlDoc.getElementsByTagName("Poor")[0].firstChild.nodeValue);
        }
        catch(e){
        countPoor = parseInt(xmlObj.childNodes[3].text);
        
        }
        
        try{
        countAverage = parseInt(xmlDoc.getElementsByTagName("Average")[0].firstChild.nodeValue);        
        }
        catch(e){
        countAverage = parseInt(xmlObj.childNodes[4].text);
        }
        
        try{
        countGood = parseInt(xmlDoc.getElementsByTagName("Good")[0].firstChild.nodeValue);
        }
        catch(e){
        countGood = parseInt(xmlObj.childNodes[5].text);
        }
        
        countTotal = countGood + countAverage + countPoor;
        //alert(countTotal);

        if(countTotal>0)
        { 
        var s = document.createElement("span");
        var tbl = document.createElement("table");
        var tr1 = document.createElement("tr");
        var tr2 = document.createElement("tr");
        tbl.className  = "s";
        tr1.className = "s";
        tr2.className = "s";
  
        if(countGood>0)
        {
        var td3a = document.createElement("td");
        var td3b = document.createElement("td");
        td3a.style.width  = ((countGood/countTotal)*100)+"%";
        newText = document.createTextNode(parseInt((countGood/countTotal)*100)+"%");
        td3a.appendChild(newText);
        tr1.appendChild(td3a);
        td3b.style.background="#FE0000";
        tr2.appendChild(td3b);
        }
        
        if(countAverage>0)
        {
        var td2a = document.createElement("td");
        var td2b = document.createElement("td");
        td2a.style.width  = ((countAverage/countTotal)*100)+"%";
        newText = document.createTextNode(parseInt((countAverage/countTotal)*100)+"%");
        td2a.appendChild(newText);
        tr1.appendChild(td2a);
        td2b.style.background="#00A74F";
        tr2.appendChild(td2b);
        }

        if(countPoor>0)
        {
        var td1a = document.createElement("td");
        var td1b = document.createElement("td");
        td1a.style.width  = ((countPoor/countTotal)*100)+"%";
        newText = document.createTextNode(parseInt((countPoor/countTotal)*100)+"%");
        td1a.appendChild(newText);
        tr1.appendChild(td1a);
        td1b.style.background="#000000";
        tr2.appendChild(td1b);
        }
        //alert(tr1);alert(tr2);
        
        tbl.appendChild(tr1);
        tbl.appendChild(tr2);
        s.appendChild(tbl);
        //alert(s.innerHTML);
        
        
        obj.parentNode.nextSibling.innerHTML = s.innerHTML;
        }

}
