﻿/*
[   Component] 投票启动脚本
[      Author] Fancy
[Created Date] Jan 25th, 2010
[   Copyright] lab206.cnu.edu.cn
*/

/*  弹出窗口的参数  */
var g_strOpenWindowArgs = "scrollbars=yes,resizable=no,width=600,height=400,left=200,top=200,screenX=200,screenY=200";

/*  投票执行和显示页  */
var g_strVoteUrl = "/Vote/ViewVote.aspx";

var g_arrOptionList = new Array();

function findOptionById(iOptionID) {
    var iIndex = -1;
    for (var i = 0; i < g_arrOptionList.length; i++)
        if (g_arrOptionList[i] == iOptionID) {
        iIndex = i;
        break;
    }
    return iIndex;
}

function default_on_click(iType) {
    switch (iType) {
        case 1:
        case 2:
        default:
            alert("您还没有选择，请先选择！");
            break;
        case 3:
            alert("您还没有填写内容，请先填写！");
            break;
    }
    return false;
}

/*  
doVote函数的主要功能是为参数a_tag引用的超链接设置onclick事件，
在onclick事件中将会弹出新窗口，其URL同样通过本函数计算得到
    
参数：
a_tag       锚记标签<a>的对象
vote_type   投票类型：1为单选题，2为多选题，3为问答题
item        控件对象，单选题对应<input type="radio">，多选题对应<input type="checkbox">，问答题对应<textarea>
container   item的容器，对于单选和多选题，container为<table>，对于问答题，container为<textarea>
count       仅对单选题和多选题有效，表示选项的个数
*/
function doVote(a_tag, vote_type, item, container, count) {

    var strVoteUrl = g_strVoteUrl + "?Type=" + vote_type;
    var oValue = undefined;
    var bHasValue = undefined;

    switch (vote_type) {
        case 1:
            oValue = item.parentNode._value;
            g_arrOptionList.push(0);
            if (item.checked)
                g_arrOptionList[0] = oValue;
            strVoteUrl += "&OptionID=" + escape(oValue);
            bHasValue = (g_arrOptionList.length > 0);
            break;
        case 2:
            oValue = item.parentNode._value;
            if (item.checked && findOptionById(oValue) < 0) {
                g_arrOptionList.push(oValue);
            }
            else if (!item.checked && findOptionById(oValue) >= 0) {
                g_arrOptionList.splice(findOptionById(oValue), 1);
            }
            strVoteUrl += "&OptionIDList=" + escape(g_arrOptionList.join());
            bHasValue = (g_arrOptionList.length > 0);
            break;
        case 3:
            oValue = item.value;
            strVoteUrl += "&ResultName=" + escape(oValue);
            bHasValue = (oValue != "");
            break;
    }
    if (bHasValue) {
        a_tag.onclick = function() {
            window.open(strVoteUrl, "", g_strOpenWindowArgs);
            clearValue(vote_type, container, count);
            a_tag.onclick = function() {
                return default_on_click(vote_type);
            }
        };
    }
    else a_tag.onclick = function() {
        return default_on_click(vote_type);
    }
}

function clearValue(type, container, count) {
    switch (type) {
        case 1:
        case 2:
            for (var i = 0; i < count; i++) {
                var item = document.getElementById(container.id + "_" + i);
                item.checked = false;
            }
            break;
        case 3:
            container.value = "";
            break;
    }
    type = null;
    container = null;
    count = null;
    g_arrOptionList = new Array();
}

/*  查看结果  */
function viewVote() {
    window.open(g_strVoteUrl, "", g_strOpenWindowArgs);
}
