﻿var ratinglistGlobalHoldFunction;

function ratinglistSetStates(levelId) {
    var element = document.getElementById(levelId);
    if (element) {
        //alert("禁用用户当前投票的按钮。");
        var onclick = "" + element.onclick;
        var offset = onclick.indexOf("ratinglistScore_Post");
        // hold function.
        ratinglistGlobalHoldFunction = element.onclick;
        element.onclick = onclick.substring(0, offset) + "return;" + onclick.substring(offset, onclick.length);
    }
}

function ratinglistChangeStates(obj) {
    //alert("修改用户当前投票的按钮为禁用。");
    var length = obj.parentNode.getElementsByTagName("a").length;
    for (var i = 0; i < length; i++) {
        var onclick = "" + obj.parentNode.getElementsByTagName("a")[i].onclick;
        var offset = onclick.indexOf("return;ratinglistScore_Post");
        if (offset >= 0) {
            if (onclick.substring(0, offset) + onclick.substring(offset + 7, onclick.length) == "" + ratinglistGlobalHoldFunction) {
                obj.parentNode.getElementsByTagName("a")[i].onclick = ratinglistGlobalHoldFunction;
            }
            else {
                throw new Error("hold function not matching.");
            }
            var elementId = obj.parentNode.getElementsByTagName("a")[i].id;
            if (elementId.indexOf("btnPic") == -1) {
                document.getElementById("ltl" + elementId.substring(3, elementId.length) + "Set").innerHTML = "";
            }
            else {
                document.getElementById("ltl" + elementId.substring(6, elementId.length) + "Set").innerHTML = "";
            }
        }
    }
    ratinglistSetStates(obj.id);
    if (obj.id.indexOf("btnPic") == -1) {
        ratinglistSetStates("btnPic" + obj.id.substring(3, obj.id.length));
        document.getElementById("ltl" + obj.id.substring(3, obj.id.length) + "Set").innerHTML = "--您给出的评价--";
    }
    else {
        ratinglistSetStates("btn" + obj.id.substring(6, obj.id.length));
        document.getElementById("ltl" + obj.id.substring(6, obj.id.length) + "Set").innerHTML = "--您给出的评价--";
    }
}

function ratinglistScore_Post(obj, focusId, typeOfFocus, specialId, meter) {
    var strPost = "focusId=" + focusId + "&typeOfFocus=" + typeOfFocus + "&specialId=" + specialId + "&meter=" + meter;
    var ajaxScoreService = new Ajax(null, null, "/WebServices/Protected/ScoreService.asmx/SetScore", 0, strPost, function() { ratinglistScore_Get(obj, focusId, typeOfFocus, specialId); });
    ajaxScoreService.post();
}

function ratinglistScore_Get(obj, focusId, typeOfFocus, specialId) {
    ratinglistChangeStates(obj);
    var strPost = "focusId=" + focusId + "&typeOfFocus=" + typeOfFocus + "&specialId=" + specialId;
    var ajaxScoreService = new Ajax(null, null, "/WebServices/Protected/ScoreService.asmx/GetSpecialScore", 0, strPost, function(rexm) { ratinglistDisplayScore(obj, rexm); });
    ajaxScoreService.post();
}

function ratinglistDisplayScore(obj, rexm) {
    //alert("更新投票统计结果。");
    var xmlDoc = rexm;
    var xmlObj = xmlDoc.documentElement;
    //because of the stupid firefox,
    try {
        countVeryPoor = parseInt(xmlDoc.getElementsByTagName("VeryPoor")[0].firstChild.nodeValue);
    }
    catch (e) {
        countVeryPoor = parseInt(xmlObj.childNodes[3].text);
    }
    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);
    }
    try {
        countGreat = parseInt(xmlDoc.getElementsByTagName("Great")[0].firstChild.nodeValue);
    }
    catch (e) {
        countGreat = parseInt(xmlObj.childNodes[5].text);
    }

    document.getElementById("ltlVeryPoorCount").innerHTML = countVeryPoor;
    document.getElementById("ltlPoorCount").innerHTML = countPoor;
    document.getElementById("ltlAverageCount").innerHTML = countAverage;
    document.getElementById("ltlGoodCount").innerHTML = countGood;
    document.getElementById("ltlGreatCount").innerHTML = countGreat;
}
