﻿
$(function() {
    //提问
    $("#selectDepartment").FillOptions("/Resources/Services/GetQuestionCategories.ashx?typeid=A&cspell=0", { datatype: "json", textfield: "name", valuefiled: "spell" });
    $("#selectDiesease").AddOption("-请选择小栏目-", "-1", true, 0);
    $("#selectDepartment").CascadingSelect(
    							$("#selectDiesease"),
    						"/Resources/Services/GetQuestionCategories.ashx?typeid=A",
    						{ datatype: "json", textfield: "name", valuefiled: "spell", parameter: "cspell" },
   							function() {
   							    if ($("#selectDepartment").get(0).selectedIndex == 0) $("#selectDiesease").get(0).options.length = 0;
   							    $("#selectDiesease").AddOption("-请选择小栏目-", "-1", true, 0);
   							}
    		);
});

$(document).ready(
        function() {
            //问题提交
            $("#btnPostQuestion").click(function() { PostUserRegQuestion(); });

            //验证登录状态
            loadUserState();

            //选中验证图片刷新验证码
            loadKeyCode();

        })
        
        
//验证登录状态
function loadUserState() {
    var QuestionLoginState = $.ajax({ url: "/Resources/Services/QuestionRegLogin.ashx?usrhtml=state", async: false }).responseText;
    var QuestionUserName = $.ajax({ url: "/Resources/Services/QuestionRegLogin.ashx?usrhtml=questioname", async: false }).responseText;
    var html = "用户名：<input type=\"text\" id=\"textUserRegName\" size=\"8\" /> &nbsp;密码：<input type=\"password\" id=\"textUserRegPassword\" size=\"8\" /><br /><span style=\"color:#ff9966\">输入用户名和密码即'快速完成注册'，以方便查找您提交的咨询内容！</span>";

    if (QuestionLoginState == "True") {
        html = "<div>用户名：" + QuestionUserName + " <a onclick=\"PostQuestionSigOut()\" style=\"cursor:pointer\">注销</a></div>";
    }

    $(html).appendTo("#UserState");
}

//注销登录
function PostQuestionSigOut() {
    if (confirm('您确认要退出吗？') == true) {
        $.post("/Resources/PutData/QuestionSigOut.aspx", { Action: "post" },
     function(data, textStatus) {
         if (data.result == "success") {
             alert("注销成功,谢谢您的使用!"); location.href = "/question/";
         }
         else {
             alert("操作失败,请速与管理员联系,谢谢合作!");
         }
     }, "json")
    }
}

//选中验证图片刷新验证码
function loadKeyCode() {
    $("#textKeyCode").focus(function() {
        $("#spRandImg").empty();
        var htmlImg = "<img id=\"imgRandCode\" src=\"" + GetRandCodeUrl() + "\" onclick=\"GetRandCodeNum()\" style=\"cursor: pointer\" alt=\"看不清楚,换一张\" />";
        $(htmlImg).appendTo("#spRandImg");
    });
}


//***********************获取验证码***********************
//获取验证码
function GetRandCodeNum() {
    $("#imgRandCode").click(function() {
        $(this).attr("src", GetRandCodeUrl());
        $("#textKeyCode").focus();
    });

}

//随机获取验证码Url
function GetRandCodeUrl() {
    var timestamp = (new Date()).valueOf();
    var url = "/Resources/Services/RandImageCode.ashx?data=get&RandNum=" + timestamp;
    return url;
}
//***********************获取验证码*********************** 


//***********************提交问题***********************
//提问信息提交
function PostUserRegQuestion() {
    if (CheckPostUserRegQuestion()) {
        PostUserRegQuestionGo();
    }
}

//验证输入情况
function CheckPostUserRegQuestion() {
    if ($("#textTitle").val() == "") {
        alert("请输入您提问的标题.");
        $("#textTitle").focus();
        return false;
    }
    if ($("#selectDepartment").val() == "0") {
        alert("请选择科室.");
        return false;
    }
    if ($("#selectDiesease").val() == "-1") {
        alert("请选择疾病.");
        return false;
    }
    if ($("#textContent").val() == "") {
        alert("请输入您提问的详细内容.");
        $("#textContent").focus();
        return false;
    }
    if ($("#textTitle").val() == "") {
        alert("请输入您提问的标题.");
        $("#textTitle").focus();
        return false;
    }
    if ($("#textKeyCode").val() == "") {
        alert("请输入验证码.");
        $("#textKeyCode").focus();
        return false;
    }
    var QuestionLoginState = $.ajax({ url: "/Resources/Services/QuestionRegLogin.ashx?usrhtml=state", async: false }).responseText;
    if (QuestionLoginState != "True") {
        if ($("#textUserRegName").val() == "") {
            alert("请输入您的帐号(*如果没有注册咨询帐号,请输入您需要注册的帐号以及密码,系统将为您快速注册此帐号).");
            $("#textUserRegName").focus();
            return false;
        }
        if ($("#textUserRegPassword").val() == "") {
            alert("请输入您的帐号密码(*如果没有注册咨询帐号,请输入您需要注册的帐号以及密码,系统将为您快速注册此帐号).");
            $("#textUserRegPassword").focus();
            return false;
        }
        return true;
    }
    return true;
}

//提交提问信息
function PostUserRegQuestionGo() {
    $.post("/Resources/PutData/PutQuestion.aspx", { Action: "post",
        SweetBox2U_textTitle: $("#textTitle").val(),
        SweetBox2U_selectDepartment: $("#selectDepartment").val(),
        SweetBox2U_selectDiesease: $("#selectDiesease").val(),
        SweetBox2U_textContent: $("#textContent").val(),
        SweetBox2U_textKeyCode: $("#textKeyCode").val(),
        SweetBox2U_textUserRegName: $("#textUserRegName").val(),
        SweetBox2U_textUserRegPassword: $("#textUserRegPassword").val()
    },
     function(data, textStatus) {
         if (data.result == "success") {

             $("#textTitle").attr("value","");
             $("#textContent").attr("value", "");
             $("#textKeyCode").attr("value", "");
             alert("问题提交成功,我们将第一时间为您解答."); location.href = '/question/';
         }
         else if (data.result == "fail") {
             alert("问题提交失败,请速与管理员联系,谢谢.");
         }
         else if (data.result == "rcoderr") {
             $("#textKeyCode").focus();
             alert("验证码输入错误");
         }
         else {
             alert(data.result);
         }
     }, "json")
}

//***********************提交问题***********************

