var deph = 0;
var askclassName;
var editorId;
var url;
var t;
var param;

function askadd() {
    var title = document.getElementById("txtTitle").value;
    var content = document.getElementById(Editor).contentWindow.document.body.innerHTML;
    var classId = document.getElementById("txtClassId").value;
    var reward = document.getElementById("DropReward").value;

    if (title.length == 0) {
        $.dialog.alert('请输入问题标题', function() {
            document.getElementById("txtTitle").focus();
        });
        return false;
    }

    if (classId == "0") {
        $.dialog.alert('请选择问题分类', function() {
            document.getElementById("dropClass").focus();
        });
        return false;
    }

    D.BLL.Ask.B_Question.addQuestion(title, content, classId, reward, callBackQuestion);
}

function callBackQuestion(res) {
    t = res.value.split("☂")[0];
    var v = res.value.split("☂")[1];
    url = res.value.split("☂")[2];
    param = res.value.split("☂")[3];
    $.dialog({
        lock: true,
        drag: false,
        icon: 'success.gif',
        content: '' + v + '',
        title: "提示",
        time: setInterval(fn, 1500)
    });
}

function fn() {
    switch (t) {
        case "100":
            window.location.href = url;
            break;
        case "101":
            window.location.href = "" + param + "/user/login.aspx?ReturnUrl=" + escape(window.location) + "";
            break;
    }
}

function askansweradd(param) {
    var content = document.getElementById("Answer_HtmlEdit").contentWindow.document.body.innerHTML;
    var ItemId = document.getElementById("txtItemId").value;
    
    D.BLL.Ask.B_Answer.addAnswer(content, ItemId, callBackQuestion);
}

function setEditorId(id) {
    editorId = id;
}

function askclass(v, d) {
    deph = d;
    removeSelect();
    addClassId(v);
    if (v != "0") {
        askclassName = "askclass_" + d;
        D.BLL.Ask.B_Class.GetAjaxList(v, callBackClass);
    }
}

function callBackClass(res) {
    if (res.value.Rows.length > 0) {
        createSelect();
        var objName = document.getElementById(askclassName)
        objName.add(new Option("请选择", "0"));
        for (var i = 0, p = 1; i < res.value.Rows.length; i++, p++) {
            objName.add(new Option(res.value.Rows[i]["CnName"], res.value.Rows[i]["ClassId"]));
        }
    }
}

function createSelect() {
    var _d = Number(deph) + 1;
    if (document.getElementById(askclassName) == null) {
        var mySelect = document.createElement("select");
        mySelect.id = askclassName;
        mySelect.setAttribute("onchange", "askclass(this.value," + _d + ")");
        document.getElementById("askclass").appendChild(mySelect);
    }
    else {
        var obj = document.getElementById(askclassName);
        obj.options.length = 0;
    }
}

function removeSelect() {
    var _deph = deph;
    var _askclassName;
    for (var i = 0; i < 1000; i++) {
        _askclassName = "askclass_" + _deph;
        var mySelect = document.getElementById(_askclassName);
        if (mySelect != null) {
            mySelect.parentNode.removeChild(mySelect);
            _deph = Number(_deph) + 1;
        }
        else {
            break;
        }
    }
}

function addClassId(v) {
    document.getElementById("txtClassId").value = v;
}

function KeySearchAskPress() {
    if (event.keyCode == 13) {
        SearchAsk();
    }
}

function SearchAsk() {
    var keyword = document.getElementById("txtkeyword").value;
    if (keyword.length == 0) {
        $.dialog.alert('请输入查询关键字', function() {
        document.getElementById("txtkeyword").focus();
        });
        return false;
    }
    window.location.href = "/ask/search.aspx?keyword=" + encodeURIComponent(keyword) + "";
}