/**************************************************************生成数组对象***********************************************************/

var AskVolumeTopicArray = new Array(); ///题目数组对象
var TKS_AskVolumeTitleArray = new Array(); ///标题数组对象
var TKS_AskVolumeTopicOptionArray = new Array(); ///选项数组对象
var SelectedAnswerObjectArray = new Array(); //所选答题对象数组
/**************************************************************对象属性***********************************************************/
///题目对象
///id,题目名,题目顺序,题目描述,题型,是否必填（0：开启，1关闭）,FID（所属层次）
function AskVolumeTopic(AskVolumeTopic_ID, AskVolumeTopic_Name, SortIndex, AskVolumeTopic_Dis, AskVolumeTopic_Type, AskVolumeTopic_IsNull, AskVolumeTopic_Fid) {

    if (AskVolumeTopic_ID != null && AskVolumeTopic_ID != "" && AskVolumeTopic_ID != "null") {
        this.AskVolumeTopic_ID = AskVolumeTopic_ID;
    } else {
        this.AskVolumeTopic_ID = randomString(50);
    }
    this.AskVolumeTopic_Name = AskVolumeTopic_Name;
    this.SortIndex = SortIndex;
    this.AskVolumeTopic_Dis = AskVolumeTopic_Dis;
    this.AskVolumeTopic_Type = AskVolumeTopic_Type;
    this.AskVolumeTopic_IsNull = AskVolumeTopic_IsNull;
    this.AskVolumeTopic_Fid = AskVolumeTopic_Fid;
}
//所选答题对象
function SelectedAnswerObject(TopicOptionId, TopicOptionDis) {
    this.TopicOptionId = TopicOptionId;
    this.TopicOptionDis = TopicOptionDis;
}
///试卷对象
///问卷名,创建时间,开始时间,结束时间,创建人,创建id,问卷是否开启密码（0：开启，1关闭）,问卷密码,问卷描述,提交说明
function TKS_AskVolume(AskVolume_ID, AskVolume_Name, CreateTime, AskVolume_Start, AskVolume_End, AskVolume_UserName, AskVolume_UserID, AskVolume_OpenPwd, AskVolume_Pwd, AskVolume_Dis, AskVolume_BtnDis) {
    if (AskVolume_ID != null && AskVolume_ID != "" && AskVolume_ID != "null") {
        this.AskVolume_ID = AskVolume_ID;
    } else {
        this.AskVolume_ID = randomString(50);
    }
    this.AskVolume_Name = AskVolume_Name;
    this.CreateTime = CreateTime;
    this.AskVolume_Start = AskVolume_Start;
    this.AskVolume_End = AskVolume_End;
    this.AskVolume_UserName = AskVolume_UserName;
    this.AskVolume_UserID = AskVolume_UserID;
    this.AskVolume_OpenPwd = AskVolume_OpenPwd;
    this.AskVolume_Pwd = AskVolume_Pwd;
    this.AskVolume_Dis = AskVolume_Dis;
    this.AskVolume_BtnDis = AskVolume_BtnDis;
    //    this.AskVolumeTopicArray = AskVolumeTopicArray;
    //    this.TKS_AskVolumeTitleArray = TKS_AskVolumeTitleArray;
    //    this.TKS_AskVolumeTopicOptionArray = TKS_AskVolumeTopicOptionArray;
}
///标题对象
///id,标题名,标题父级,标题顺序
function TKS_AskVolumeTitle(AskVolumeTitle_ID, AskVolumeTitle_Name, AskVolumeTitle_Fid, SortIndex) {
    if (AskVolumeTitle_ID != null && AskVolumeTitle_ID != "" && AskVolumeTitle_ID != "null") {
        this.AskVolumeTitle_ID = AskVolumeTitle_ID;
    } else {
        this.AskVolumeTitle_ID = randomString(50);
    }

    this.AskVolumeTitle_Name = AskVolumeTitle_Name;
    this.AskVolumeTitle_Fid = AskVolumeTitle_Fid;
    this.SortIndex = SortIndex;
}

///题目选项对象
/// id,题目id,选项名,选项顺序,选项统计数,是否开启跳转(0：开启，1关闭),跳转题目id,选项理由
function TKS_AskVolumeTopicOption(TopicOption_ID, VolumeTopic, TopicOption_Name, SortIndex, TopicOption_Count, TopicOption_Jump, TopicOption_JumpTopic_ID, TopicOption_Dis) {
    if (TopicOption_ID != null && TopicOption_ID != "" && TopicOption_ID != "null") {
        this.TopicOption_ID = TopicOption_ID;
    } else {
        this.TopicOption_ID = randomString(50);
    }
    this.VolumeTopic = VolumeTopic;
    this.TopicOption_Name = TopicOption_Name;
    this.SortIndex = SortIndex;
    this.TopicOption_Count = TopicOption_Count;
    this.TopicOption_Jump = TopicOption_Jump;
    this.TopicOption_JumpTopic_ID = TopicOption_JumpTopic_ID;
    this.TopicOption_Dis = TopicOption_Dis;
}
//生成id
function randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoprstuvwxyz0123456789';
    var maxPos = $chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}
/**************************************************************保存题目对象***********************************************************/
//保存问答题 Type1
function btnJsonType1(AskVolumeTitle_ID, AskVolumeTopic_Name, AskVolumeTopic_Dis) {
    var Topic = new AskVolumeTopic(AskVolumeTitle_ID, AskVolumeTopic_Name, AskVolumeTopic_Dis, SortIndex);
}
///获取所有题目数组对象
function GetAllAskVolumeTopicArray() {
    return AskVolumeTopicArray;
}
/**************************************************************新增数组对象***********************************************************/
///新增题目数组
function AddAskVolumeTopicArray(obj) {
    //alert($(AskVolumeTopicArray).push(obj));
    return AskVolumeTopicArray.push(obj); //返回数组当前count
}
///新增标题数组
function AddTKS_AskVolumeTitleArray(obj) {
    return TKS_AskVolumeTitleArray.push(obj);
}
//新增题目选项数组
function AddTKS_AskVolumeTopicOptionArray(obj) {
    return TKS_AskVolumeTopicOptionArray.push(obj); //返回数组当前count
}
/**************************************************************查询数组对象***********************************************************/
///根据题目id数组 查询题目对象
function GetAskVolumeTopicArrayByID(id) {
    var json = null;
    $.each(AskVolumeTopicArray, function (i, obj) {
        if (obj.AskVolumeTopic_ID == id) {
            return json = obj;
        }
    });
    return json;
}
///根据标题数组 查询标题对象
function GetTKS_AskVolumeTitleArrayByID(id) {
    var json = null;
    $.each(TKS_AskVolumeTitleArray, function (i, obj) {
        // alert(JSON.parse(obj).AskVolumeTitle_ID);
        if (obj.AskVolumeTitle_ID == id) {
            return json = obj;
        }
    });
    return json;
}
///根据题目id 查询 选项数组 获取对象数组
function GetTKS_AskVolumeTopicOptionArrayByTopic_ID(id) {
    var json = null;
    var TopicOptionArray = new Array();
    $.each(TKS_AskVolumeTopicOptionArray, function (i, obj) {
        // alert(JSON.parse(obj).AskVolumeTitle_ID);
        if (obj.VolumeTopic == id) {
            TopicOptionArray.push(obj);
        }
    });
    return TopicOptionArray;
}

///根据id 查询选项数量
function GetTKS_AskVolumeTopicOptionArrayCountByTopic_ID(id) {
    var j= 0;
    var TopicOptionArray = new Array();
    $.each(TKS_AskVolumeTopicOptionArray, function (i, obj) {
        if (obj.VolumeTopic == id) {
            j++;
        }
    });
    return j;
}
//根据选项id 获取选项对象
function GetTKS_AskVolumeTopicOptionArrayByTopicOption_ID(id) {
    var json = null;
    $(TKS_AskVolumeTopicOptionArray).each(function (i, obj) {
        if (obj.TopicOption_ID == id) {
            return json = obj;
        }
    });
    return json;
}
/**************************************************************删除数组对象***********************************************************/
///根据题目id 删除 选项数组 获取对象数组
function DelTKS_AskVolumeTopicOptionArrayByTopic_ID(id) {
    //声明下标数组
    var indexarray = new Array();
    $.each(TKS_AskVolumeTopicOptionArray, function (i, obj) {
        if (obj.VolumeTopic == id) {
            indexarray.push(obj);
        }
    });
    $.each(indexarray, function (i, obj) {
        TKS_AskVolumeTopicOptionArray.splice($.inArray(obj, TKS_AskVolumeTopicOptionArray), 1);
    });
}
//删除题目和选项
function DelTKS_AskVolumeTopicArrayByTopic_ID(id) {
    //声明下标数组
    var indexarray = new Array();
    $.each(AskVolumeTopicArray, function (i, obj) {
        if (obj.AskVolumeTopic_ID == id) {
            indexarray.push(obj);
        }
    });
    $.each(indexarray, function (i, obj) {
        AskVolumeTopicArray.splice($.inArray(obj, AskVolumeTopicArray), 1);
    });
    DelTKS_AskVolumeTopicOptionArrayByTopic_ID(id);
}
//删除标题
function DelTitle(id) {
    //声明下标数组
    var indexarray = new Array();
    $.each(TKS_AskVolumeTitleArray, function (i, obj) {
        if (obj.AskVolumeTitle_ID == id) {
            indexarray.push(obj);
        }
    });
    $.each(indexarray, function (i, obj) {
        TKS_AskVolumeTitleArray.splice($.inArray(obj, TKS_AskVolumeTitleArray), 1);
    });
}
/**************************************************************修改题目数组对象***********************************************************/
///根据题目id 修改或添加现有题目数组的对象 
function EditAskVolumeTopicArrayById(VolumeTopic) {
    var isok = false;
    var index = 0;
    $(AskVolumeTopicArray).each(function (i, obj) {
        if (obj.AskVolumeTopic_ID == VolumeTopic.AskVolumeTopic_ID) {
            index = i;
            isok = true;
        }
    });
    if (!isok) {
        AskVolumeTopicArray.push(VolumeTopic);
    } else {
        AskVolumeTopicArray.splice(index, 1);
        AskVolumeTopicArray.push(VolumeTopic);
    }
    return isok;
}

/**************************************************************修改题目选项数组对象***********************************************************/
function EditTKS_AskVolumeTopicOptionArray(option) {
    var isok = false;
    var index = 0;
    $(TKS_AskVolumeTopicOptionArray).each(function (i, obj) {
        if (obj.TopicOption_ID == option.TopicOption_ID) {
            index = i;
            isok = true;
        }
    });
    if (!isok) {
        TKS_AskVolumeTopicOptionArray.push(option);
    } else {
        TKS_AskVolumeTopicOptionArray.splice(index, 1);
        TKS_AskVolumeTopicOptionArray.push(option);
    }
    return isok;
}
/**************************************************************修改标题选项数组对象***********************************************************/
//如果存在 修改 如果不存在 新增
function EditTKS_AskVolumeTitleArray(Title) {
    var isok = false;
    var index = 0;
    $(TKS_AskVolumeTitleArray).each(function (i, obj) {
        if (obj.AskVolumeTitle_ID == Title.AskVolumeTitle_ID) {
            index = i;
            isok = true;
        }
    });
    if (!isok) {
        TKS_AskVolumeTitleArray.push(Title);
    } else {
        TKS_AskVolumeTitleArray.splice(index, 1);
        TKS_AskVolumeTitleArray.push(Title);
    }
    return isok;
}

/**************************************************************修改答题选项数组对象***********************************************************/
//如果存在 修改 如果不存在 新增
function EditSelectedAnswerObjectArray(Title) {
    var isok = false;
    var index = 0;
    $(SelectedAnswerObjectArray).each(function (i, obj) {
        if (obj.TopicOptionId == Title.TopicOptionId) {
            index = i;
            isok = true;
        }
    });
    if (!isok) {
        SelectedAnswerObjectArray.push(Title);
    } else {
        SelectedAnswerObjectArray.splice(index, 1);
        SelectedAnswerObjectArray.push(Title);
    }
    return isok;
}
//时间转换
function ChangeDateFormat(cellval) {
    try {
        var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    } catch (e) {
        return "";
    }
}
//判断时间
function strDateTime(str) {
    var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (r == null) return false;
    var d = new Date(r[1], r[3] - 1, r[4]);
    return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}