var txtPicID;
function InsertOption(optType) {
    $('#imgMore').show();
    $('#imgLess').show();
    $('#QusTitle').html('备选项：');
    $('#QusOption').css('margin', '');
    $('#' + hfOptionTypeID).attr('value',optType);
    var strHTML = '';
    var strJson = $('#' + hfOptionsID).attr('value');
    if (strJson != '' && strJson != null) {
        var arrayObj = JSON.parse(strJson);
        var rightOption=$('#' + hfRightID).attr('value');
        for (var i = 0; i < arrayObj.length; i++) {
            if (optType == 'radio') {
                strHTML += '<li style="list-style-type: upper-alpha">\
                        <input id=' + arrayObj[i].ID + ' onfocus=javascript:ShowHideSet(this.id); name=Answer value="' + arrayObj[i].OptText + '">\
                        <span style="display: none" id=ActionSpan' + arrayObj[i].ID + '>';
                        
                if (arrayObj[i].ID==rightOption){
                    strHTML += '<input type=radio name=OptionSelect id=r' + arrayObj[i].ID + ' checked />设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;';
                }else{
                    strHTML += '<input type=radio name=OptionSelect id=r' + arrayObj[i].ID + ' />设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                strHTML += '<span style="cursor:pointer" onclick="SetPic(' + arrayObj[i].ID + ');" alt="插入网上图片，支持格式为：gif、jpg、bmp">选择图片</span></span>';
                if(arrayObj[i].OptPicture!=null && arrayObj[i].OptPicture!=''){
                    strHTML += '<br><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" id=pic' + arrayObj[i].ID + ' name=picbox src="' + arrayObj[i].OptPicture + '"></li>';
                }else{
                    strHTML += '<br><img style="border-bottom: 0px; border-left: 0px; display: none; border-top: 0px; border-right: 0px" id=pic' + arrayObj[i].ID + ' name=picbox></li>';
                }
            } else {
                var rOption = '';
                strHTML += '<li style="list-style-type: decimal">\
                        <input id=' + arrayObj[i].ID + ' onfocus=javascript:ShowHideSet(this.id); name=Answer value="' + arrayObj[i].OptText + '">\
                        <span style="display: none" id=ActionSpan' + arrayObj[i].ID + '>';
                var aryRight=rightOption.split(',');
                for (var r = 0; r < aryRight.length; r++) {
                    if (aryRight[r] == arrayObj[i].ID) {
                        rOption = 'checked';
                        break;
                    }
                }
                strHTML += '<input type="checkbox" name=OptionSelect id=c' + arrayObj[i].ID + ' ' + rOption + ' />设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;\
                            <span style="cursor:pointer" onclick="SetPic(' + arrayObj[i].ID + ');" alt="插入网上图片，支持格式为：gif、jpg、bmp">选择图片</span></span>';
                if(arrayObj[i].OptPicture!=null && arrayObj[i].OptPicture!=''){
                    strHTML += '<br><img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" id=pic' + arrayObj[i].ID + ' name=picbox src="' + arrayObj[i].OptPicture + '"></li>';
                }else{
                    strHTML += '<br><img style="border-bottom: 0px; border-left: 0px; display: none; border-top: 0px; border-right: 0px" id=pic' + arrayObj[i].ID + ' name=picbox></li>';
                }
            }
        }
    } else {
        for (var i = 1; i <= 4; i++) {
            if (optType == 'radio') {
                strHTML += '<li style="list-style-type: upper-alpha">\
                        <input id=' + i + ' onfocus=javascript:ShowHideSet(this.id); name=Answer>\
                        <span style="display: none" id=ActionSpan' + i + '>\
                        <input type=radio name=OptionSelect id=r' + i + '>设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;\
                        <span style="cursor:pointer" onclick="SetPic(' + i + ');" alt="插入网上图片，支持格式为：gif、jpg、bmp">选择图片</span></span>\
                        <br><img style="border-bottom: 0px; border-left: 0px; display: none; border-top: 0px; border-right: 0px" id=pic' + i + ' name=picbox></li>';
            } else {
                strHTML += '<li style="list-style-type: decimal">\
                        <input id=' + i + ' onfocus=javascript:ShowHideSet(this.id); name=Answer>\
                        <span style="display: none" id=ActionSpan' + i + '>\
                        <input type="checkbox" name=OptionSelect id=c' + i + '>设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;\
                        <span style="cursor:pointer" onclick="SetPic(' + i + ');" alt="插入网上图片，支持格式为：gif、jpg、bmp">选择图片</span></span>\
                        <br><img style="border-bottom: 0px; border-left: 0px; display: none; border-top: 0px; border-right: 0px" id=pic' + i + ' name=picbox></li>';
            }
        }
    }
    
    
    $('#QusOption').html(strHTML);
}
function LessOption() {
    var chdCount = $('#QusOption').children('li').length;
    if (chdCount == 1) { alert("最后一个选项不能删除"); return; }
    $('#QusOption').children('li').last().remove();
}
function MoreOption() {
    var optType = $('#' + hfOptionTypeID).attr('value');
    var strHTML = '';
    var NewID = $('#QusOption>li').length + 1;
    if (optType == 'radio') {
        strHTML += '<li style="list-style-type: upper-alpha">\
                        <input id=' + NewID + ' onfocus=javascript:ShowHideSet(this.id); name=Answer>\
                        <span style="display: none" id=ActionSpan' + NewID + '>\
                        <input type=radio name=OptionSelect id=r' + NewID + '>设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;\
                        <span style="cursor:pointer" onclick="SetPic(' + NewID + ');" alt="插入网上图片，支持格式为：gif、jpg、bmp">选择图片</span></span>\
                        <br><img style="border-bottom: 0px; border-left: 0px; display: none; border-top: 0px; border-right: 0px" id=pic' + NewID + ' name=picbox></li>';
    } else {
        strHTML += '<li style="list-style-type: decimal">\
                        <input id=' + NewID + ' onfocus=javascript:ShowHideSet(this.id); name=Answer>\
                        <span style="display: none" id=ActionSpan' + NewID + '>\
                        <input type="checkbox" name=OptionSelect id=c' + NewID + '>设为正确答案&nbsp;&nbsp;&nbsp;&nbsp;\
                        <span style="cursor:pointer" onclick="SetPic(' + NewID + ');" alt="插入网上图片，支持格式为：gif、jpg、bmp">选择图片</span></span>\
                        <br><img style="border-bottom: 0px; border-left: 0px; display: none; border-top: 0px; border-right: 0px" id=pic' + NewID + ' name=picbox></li>';
    }
    $('#QusOption').append(strHTML);
}

function SetPic(PicID) {
    $.dialog({
        lock: true,
        title: '选择文件',
        content: 'url:/Manager/FileExplorer.aspx?Path=' + SitePath,
        width: '800px',
        height: '500px',
        max: false,
        min: false
    });
    txtPicID = PicID;
}
function callBack(FilePath) {
    if (!isNaN(txtPicID)) {
        $('#pic' + txtPicID).show();
        $('#pic' + txtPicID).attr('src', FilePath);
    } else {
        $('#' + QusPicID).attr('value', FilePath);
    }
}
function ShowHideSet(AnswerID) {
    $('span').each(function() {
        if (this.id.indexOf('ActionSpan') >= 0) {
            $(this).hide();
        }
    });
    $('#ActionSpan' + AnswerID).show();
}
function SaveOption() {
    var arrayObj = new Array();
    var Title='';
    var Pic = '';
    var isRight = false;
    var objInput;
    var ID;
    $('#QusOption>li').each(function() {
        ID = $('input[name="Answer"]', this).attr('id');
        Title = $('input[name="Answer"]', this).attr('value');
        Pic = $('img[name="picbox"]', this).attr('src');
        objInput = $('input[name="OptionSelect"]', this)[0];
        isRight = objInput.checked;
        var newJson = { 'ID': ID, 'Title': Title, 'Pic': Pic, 'IsRight': isRight };
        arrayObj.push(newJson);
    });
    $('#' + hfOptionsID).attr('value', JSON.stringify(arrayObj));
}

function IsDigit() {
    return ((event.keyCode >= 48) && (event.keyCode <= 57));
}