﻿
function getClientWidth ()
{
	var width = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		width = window.innerWidth;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		width = document.body.clientWidth;
	}
	
	return width;
}

function getClientHeight()
{
	var height = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		height = document.body.clientHeight;
	}
	
	return height;
}

function trim(str)
{ 
	//删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function ltrim(str)
{ 
	//删除左边的空格
	return str.replace(/(^\s*)/g,"");
}

function rtrim(str)
{ 
	//删除右边的空格
	return str.replace(/(\s*$)/g,"");
}

function reload()
{
    if (window.ActiveXObject)
    {
        window.location.reload();
    }
    else
    {
        window.location.href = "";
    }
}

$(document).ready(function()
{       
     $(".init_text_input").each(function(){
        setInputControlInitText($(this));
     });
});

function setInputControlInitText(obj)
{
    var initText = obj.attr('title');
          
    if (obj.val() == '' || obj.val() == initText)
        obj.css('color', '#999999').val(initText);
    
    obj.focus(function(){
      if (obj.val() == initText)
      {
          obj.val('').css('color', '');
      }
    });

    obj.blur(function(){
      if ('' + obj.val() == '')
      {
          obj.val(initText).css('color', '#999999');
      }
    });
} 

function syncAjaxRequest(mothed, url, params) 
{
    var result = null;
    
    $.ajax({
        async:  false,
        cache:  false,
        type:   mothed,
        url:    url,
        data:   params,
        success: function(data) 
        {
            result = data;
        }
    });
    
    if (result == null)
        throw "网络通信错误或者服务器错误。";
    
    return result;
}

function ajax_getDialogUI(params)
{
    var result = "";
    try 
    {
        result = syncAjaxRequest("get", "/ajax_dialog_ui.aspx", params);	            
    }
    catch(ex) {
        result = ex;
    }
    
    return result;
}

function ajax_isLogined()
{
    var result = "";
    try 
    {
        result = syncAjaxRequest("get", "/ajax_login.aspx", "action=check");	            
    }
    catch(ex) {
        result = ex;
    }
    
    return result;
}

function ajax_login(username, password)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_login.aspx", 
            { "action": "login", "username": username, "password": password});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_logout()
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_login.aspx", 
            { "action": "logout"});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initMessageDialog()
{
    if ($("#message_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"message_dialog"}));
        $("#message_dialog").dialog({
		    autoOpen: false,
		    width: 340,
		    height: 200,
            resizable: false,
            bgiframe: true,		
            modal: true
        });   
    }
}

function showMessageDialog(msg, completeFunction, param)
{
    initMessageDialog();
    
    $("#message_dialog").dialog("option", "open", function(){
         $("#message_dialog_message").text(msg);
    });
    
    $("#message_dialog").dialog("option", "buttons", {
		 " 确定 ": function() {
			$("#message_dialog").dialog("close");
			if (completeFunction)
			    completeFunction(param);
		 }
    });
    
    $("#message_dialog").dialog("open");
}

function initConfirmDialog()
{
    if ($("#confirm_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"confirm_dialog"}));
        $("#confirm_dialog").dialog({
		    autoOpen: false,
		    width: 340,
		    height: 200,
            resizable: false,	
            bgiframe: true,		
            modal: true
        });
    }
}

function showConfirmDialog(msg, okFunction, okParam, cancelFunction, cancelParam)
{
    initConfirmDialog();
    
    $("#confirm_dialog").dialog("option", "buttons", {
		 " 否 ": function() {
		    $("#confirm_dialog").dialog("close");
		    if (cancelFunction)
		        cancelFunction(cancelParam);
		 },
		 " 是 ": function() {
			$("#confirm_dialog").dialog("close");
			if (okFunction)
			    okFunction(okParam);
		 }
    });
    
    $("#confirm_dialog").dialog("option", "open", function(){
         $("#confirm_dialog_message").text(msg);
    });
    
    $("#confirm_dialog").dialog("open");
}

function initLoginDialog()
{
    if ($("#login_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"login_dialog"}));
        
        $("#login_dialog").dialog({
		    autoOpen: false,
		    width: 400,
		    height: 280,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openLoginDialog(completeFunction, param)
{
    initLoginDialog();
    
    $("#login_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#login_dialog").dialog("close");
		    },
	    " 确定 ": function() {
	        var result = ajax_login($("#login_user_name").val(), $("#login_password").val());
	        if (result == "1")
            {
                $("#login_dialog").dialog("close");
                
                rememberLoginInfo($("#login_user_name").val(), $("#login_password").val(), $("#login_remember_me").attr("checked") ? "1" : "0")
                
                if (completeFunction)
                    completeFunction(param);
            }
            else
            {
                $("#login_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#login_dialog").dialog("option", "open", function(){
     	$("#login_dialog_validate_tips").text("请输入用户名和密码登录").css("color", "");
	    
	    if ($("#login_user_name").val() == "")
	        $("#login_user_name").val("" + $.cookie("login_user_name"));
		    
	    $("#login_password").val("");
	    
	    if ($("#login_user_name").val() == "")
	        $("#login_user_name").focus();
	    else
	        $("#login_password").focus();
    });
    
    $("#login_dialog").dialog("open");
}

function setLoginName(userNameInput)
{
    var username = "" + $.cookie("login_user_name");    
    if (userNameInput != null && username != "")
    {
        userNameInput.val(username);
    }

    return "0";
}

function formLogin()
{
    if ("" + $("#login_user_name").val() == "")
    {
        showMessageDialog("请输入用户名。");
        $("#login_user_name").focus();
        return;    
    }
    
    if ("" + $("#login_password").val() == "")
    {
        showMessageDialog("请输入密码。");
        $("#login_password").focus();
        return;    
    }
    
    if (ajax_login($("#login_user_name").val(), $("#login_password").val()) == "1")
    {
        rememberLoginInfo($("#login_user_name").val(), 
            $("#login_password").val(), 
            $("#login_remember_me").attr("checked") ? "1" : "0");
        
        reload();
    }
    else
        window.location.href = "/login";
}

function specialLogin()
{
    if ("" + $("#login_user_name").val() == "")
    {
        showMessageDialog("请输入用户名。");
        $("#login_user_name").focus();
        return;    
    }
    
    if ("" + $("#login_password").val() == "")
    {
        showMessageDialog("请输入密码。");
        $("#login_password").focus();
        return;    
    }
    
    var result = ajax_login($("#login_user_name").val(), $("#login_password").val());
    if (result == "1")
    {
        rememberLoginInfo($("#login_user_name").val(), 
            $("#login_password").val(), 
            $("#login_remember_me").attr("checked") ? "1" : "0");
        
        window.location.href = $("#redirection_url").val();
    }
    else
    {    
        showMessageDialog(result);
    }
}

function rememberLoginInfo(username, password, autoLogin)
{
    var domain = null;
    if (document.domain.toLowerCase().indexOf("yaoliwang.com") > 0)
        domain = "yaoliwang.com";
    else if (document.domain.toLowerCase().indexOf("yaoleetest.com") > 0)
        domain = "yaoleetest.com";
        
    $.cookie("login_user_name", username, {"path":"/", "expires": 365, "domain": domain});
    
    if (autoLogin == "1")
        $.cookie("login_password", password, {"path":"/", "expires": 30, "domain": domain});
    else
        $.cookie("login_password", password, {"path":"/", "expires": 0.5, "domain": domain});
}

function clearLoginInfo()
{
    var domain = null;
    if (document.domain.toLowerCase().indexOf("yaoliwang.com") > 0)
        domain = "yaoliwang.com";
    else if (document.domain.toLowerCase().indexOf("yaoleetest.com") > 0)
        domain = "yaoleetest.com";
        
    $.cookie("login_password", "", {"path":"/", "expires": 365, "domain": domain});
}

function ajax_post_question(title, content, score, email, expertId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_question_post.aspx", 
            { "action": "new",
            "question_title" : title,
            "question_content" : content,
            "question_score" : score,
            "question_email" : email,
            "question_expert_id": expertId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function submitQuestion()
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(submitQuestion_step2);
            break;
        case "1":
            submitQuestion_step2();
            break;
        default:
            showMessageDialog(result);
    }
    
    return false;
}

function submitQuestion_step2()
{
    var title = $('#question_title').val();
    var content = $('#question_content').val();
    var score = $('#question_score').val();
    var email = $('#question_email').val();
    var expertId = $('#question_expert_id').val();
    
    var result = '' + ajax_post_question(title, content, score, email, expertId);
    var arr = result.split('|');
    
    if (arr.length == 3 && arr[0] == "1")
    {
        showMessageDialog("您的问题己经提交，请等待来自药历网专家和网友的回答，我们也会及时给您发送通知邮件。", submitQuestion_step3, [arr[1],arr[2]]);
    }
    else if (arr.length == 3 && arr[0] == "2")
    {
        showMessageDialog("您的问题己经提交，因为内容中包含敏感词，需要通过审核才能被其他用户看到。",submitQuestion_step3, [arr[1],arr[2]]);
    }
    else
    {
        showMessageDialog(result);
    }
}

function submitQuestion_step3(Ar)
{
    window.location.href = Ar[1] + "-" + Ar[0] + ".html";
}


function ajax_isFriend(friendId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_friend.aspx", 
            { "action": "check", "friend": friendId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_newFriend(friendId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_friend.aspx", 
            { "action": "new", "friend": friendId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function newFriend(friendId, friendName)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(newFriendStep2, [friendId, friendName]);
            break;
        case "1":
            newFriendStep2([friendId, friendName]);
            break;
        default:
            showMessageDialog(result);
    }
}

function newFriendStep2(s)
{
    var result = ajax_isFriend(s[0]);
    switch(result) {
        case "0":
            showConfirmDialog("您要加“" + s[1] + "”为好友吗？", newFriendStep3, s[0], null, null);
            break;
        case "1":
            showMessageDialog("“" + s[1] + "”己经是你的好友了。", reload, null);
            break;
        case "2":
            showMessageDialog("对不起，您不能加自己为好友啊。", reload, null);
            break;
        default:
            showMessageDialog(result);
    }
}

function newFriendStep3(friendId)
{
    var result = ajax_newFriend(friendId);
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

//写邮件
function ajax_sendEmail(receiver, title, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
        "/ajax_user_email.aspx", 
        { "action": "send", "title": title,"content":content,"receiver":receiver});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function sendEmail()
{
    if ("" + $("#receiver").val() == "")
    {
        showMessageDialog("请填写收件人。");
        return;    
    }
    
    if ("" + $("#title").val() == "")
    {
        showMessageDialog("请输入主题。");
        return;    
    }
    
    str = "" + $("#content").val();
    if (str.length < 1 || str.length > 500)
    {
        showMessageDialog("正文字数要求在500个字符以内。");
        return;    
    }
    
    var result = ajax_sendEmail($("#receiver").val(), $("#title").val(),$("#content").val());
    if (result == "1")
    {
        showMessageDialog("邮件发送成功。", finishSendEmail, null);
    }
    else
    {
        showMessageDialog(result);
    }
}

function finishSendEmail()
{
    window.location.href = "/usercentral/shoujianxiang";
}

// 发送邮件弹出对话框
function initWriteEmailDialog()
{
    if ($("#write_email_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"write_email_dialog"}));
        
        $("#write_email_dialog").dialog({
		    autoOpen: false,
		    width: 550,
		    height: 400,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openWriteEmailDialog(receiver, completeFunction, param)
{
    initWriteEmailDialog();
    
    $("#write_email_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#write_email_dialog").dialog("close");
		    },
	    " 发送 ": function() {
	        
	        if ("" + $("#receiver").val() == "")
            {
                $("#write_email_dialog_validate_tips").text("请填写收件人。").css("color", "red");
                return;    
            }
            
            if ("" + $("#title").val() == "")
            {
                 $("#write_email_dialog_validate_tips").text("请输入主题。").css("color", "red");
                return;    
            }
            
            str = "" + $("#content").val();
            if (str.length > 500)
            {
                $("#write_email_dialog_validate_tips").text("正文字数要求在500个字符以内。").css("color", "red");
                return;    
            }
            
            var result = ajax_sendEmail($("#receiver").val(), $("#title").val(),$("#content").val());
	        if (result == "1")
            {
                $("#write_email_dialog").dialog("close");
                if (completeFunction != null)
                    completeFunction(param);
            }
            else
            {
                $("#write_email_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#write_email_dialog").dialog("option", "open", function(){
         $("#write_email_dialog_validate_tips").text("撰写新信息").css("color", "");
         $("#receiver").val(receiver);
         $("#title").focus();
    });
    
    $("#write_email_dialog").dialog("open");
}

function writeEmail(receiver)
{
    if (ajax_isLogined() == "1")
    {
        writeEmail_step2(receiver);
    }
    else
    {
        openLoginDialog(writeEmail_step2, receiver);
    }
}

function writeEmail_step2(receiver)
{
    openWriteEmailDialog(receiver, writeEmail_step3, null);
}

function writeEmail_step3()
{
    showMessageDialog("信息发送成功。", reload, null);
}

function ajax_collectioQuestion(questionId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
        "/ajax_question_post.aspx", 
        { "action": "collection", "question_id": questionId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function collectQuestion(questionId)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(collectQuestion_step2, questionId);
            break;
        case "1":
            collectQuestion_step2(questionId);
            break;
        default:
            showMessageDialog(result);
    }
}

function collectQuestion_step2(questionId)
{
    var result = ajax_collectioQuestion(questionId);
    if (result == "1")
        showMessageDialog("问题已经被您成功收藏。", reload);
    else
        showMessageDialog(result, reload);
}

function ajax_scoreQuestion(questionId, score)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
        "/ajax_question_post.aspx", 
        { "action": "addscore", "question_id": questionId, "question_score" : score});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initScoreQuestionDialog()
{
    if ($("#score_question_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"score_question_dialog"}));
        
        $("#score_question_dialog").dialog({
		    autoOpen: false,
		    width: 300,
		    height: 200,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openScoreQuestionDialog(questionId, completeFunction, param)
{
    initScoreQuestionDialog();
    
    $("#score_question_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#score_question_dialog").dialog("close");
		    },
	    " 确定 ": function() {  
            var result = ajax_scoreQuestion(questionId, $('#question_score').val());
	        if (result == "1")
            {
                $("#score_question_dialog").dialog("close");
                if (completeFunction != null)
                    completeFunction(param);
            }
            else
            {
                $("#score_question_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#score_question_dialog").dialog("option", "open", function(){
         $("#score_question_dialog_validate_tips").text("请选择您的悬赏分").css("color", "");
    });
    
    $("#score_question_dialog").dialog("open");
}

function scoreQuestion(questionId)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(scoreQuestion_step2, questionId);
            break;
        case "1":
            scoreQuestion_step2(questionId);
            break;
        default:
            showMessageDialog(result);
    }
}

function scoreQuestion_step2(questionId)
{
    openScoreQuestionDialog(questionId, reload);
}


function ajax_getQuestionNote(questionId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
        "/ajax_question_post.aspx", 
        { "action": "getaddcontent", "question_id": questionId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_modifyQuestion(questionId, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
        "/ajax_question_post.aspx", 
        { "action": "addcontent", "question_id": questionId, "question_addition_content" : content});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initModifyQuestionDialog()
{
    if ($("#modify_question_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"modify_question_dialog"}));
        
        $("#modify_question_dialog").dialog({
		    autoOpen: false,
		    width: 500,
		    height: 380,
            resizable: false,	
            bgiframe: true,		
            modal: true
	    });
	}
}

function openModifyQuestionDialog(questionId, completeFunction, param)
{
    initModifyQuestionDialog();
    
    $("#modify_question_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#modify_question_dialog").dialog("close");
		    },
	    " 确定 ": function() {  
            var result = ajax_modifyQuestion(questionId, $('#question_note').val());
	        if (result == "1")
            {
                $("#modify_question_dialog").dialog("close");
                if (completeFunction != null)
                    completeFunction(param);
            }
            else
            {
                $("#modify_question_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#modify_question_dialog").dialog("option", "open", function(){
         $("#modify_question_dialog_validate_tips").text("请输入要补充的内容（最多800字）").css("color", "");
         $('#question_note').val(ajax_getQuestionNote(questionId));
    });
    
    $("#modify_question_dialog").dialog("open");
}

function modifyQuestion(questionId)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(modifyQuestion_step2, questionId);
            break;
        case "1":
            modifyQuestion_step2(questionId);
            break;
        default:
            showMessageDialog(result);
    }
}

function modifyQuestion_step2(questionId)
{
    openModifyQuestionDialog(questionId, reload);
}

function ajax_closeQuestion(questionId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
        "/ajax_question_post.aspx", 
        { "action": "close", "question_id": questionId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function closeQuestion(questionId)
{
    showConfirmDialog("您确定要关闭该问题吗，问题关闭后将不能再修改和回答。", closeQuestion_step2, questionId);
}

function closeQuestion_step2(questionId)
{
    var result = ajax_closeQuestion(questionId);
    if (result != "1")
        showMessageDialog(result, reload);
    else
        reload();
}

function ajax_evaluteAnswer(answerId, value)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
        "/ajax_question_post.aspx", 
        { "action": "evalute_answer", "answer_id": answerId, "evalute_value" : value});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function evaluteAnswer(answerId, value)
{
    var result = ajax_evaluteAnswer(answerId, value);
    if (result == "1")
    {
        showMessageDialog("您的评价已经成功提交。", reload);
    }
    else
    {
        showMessageDialog(result);
    }
}

function ajax_newAnswer(questionId, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
        "/ajax_question_post.aspx", 
        { "action": "new_anwser", "question_id": questionId, "answer_content" : content});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function initNewAnswerDialog()
{
    if ($("#new_answer_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"new_answer_dialog"}));
        
        $("#new_answer_dialog").dialog({
		    autoOpen: false,
		    width: 500,
		    height: 380,
            resizable: false,	
            bgiframe: true,		
            modal: false
	    });
	}
}

function openNewAnswerDialog(questionId, completeFunction, param)
{
    initNewAnswerDialog();
    
    $("#new_answer_dialog").dialog("option", "buttons", {
		 " 取消 ": function() {
				$("#new_answer_dialog").dialog("close");
		    },
	    " 回答 ": function() {  
            var result = ajax_newAnswer(questionId, $('#answer_content').val());
	        if (result == "1")
            {
                $("#new_answer_dialog").dialog("close");
                if (completeFunction != null)
                    completeFunction(param);
            }
            else if (result == "2")
            {
                 $("#new_answer_dialog").dialog("close");
                 showMessageDialog("对不起，由于您的回答中包含敏感词汇，请等待我们的审核。", reload);
            }
            else
            {
                $("#new_answer_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#new_answer_dialog").dialog("option", "open", function(){
         $("#new_answer_dialog_validate_tips").text("请输入回答的内容（最多4000字）").css("color", "");
    });
    
    $("#new_answer_dialog").dialog("open");
}

function answerQuestion(questionId)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(answerQuestion_step2, questionId);
            break;
        case "1":
            answerQuestion_step2(questionId);
            break;
        default:
            showMessageDialog(result);
    }
}

function answerQuestion_step2(questionId)
{
    openNewAnswerDialog(questionId, reload);
}

function answerQuestion_isLogined()
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog();
            break;
        case "1":
            break;
        default:
            showMessageDialog(result);
    }
}

function answerQuestion_submit(questionId)
{
    if(ajax_isLogined() == "1")
    {
        var initText = '请输入回答的内容（最多4000字）。';
        if ("" + $('#answer_content').val() == "" || "" + $('#answer_content').val() == initText)
        {
            $('#answer_content').focus();
            return false;
        }
        else if($('#answer_content').val().length > 4000)
        {
            showMessageDialog('请输入不多于4000个字符。');
            return false;
        }
        
        var result = ajax_newAnswer(questionId, $('#answer_content').val());
        if (result == "1")
        {
            showMessageDialog("您回答的内容已经成功提交。", answerQuestion_step3,questionId);
        }
        else if (result == "2")
        {
             showMessageDialog("对不起，由于您的回答中包含敏感词汇，请等待我们的审核。", reload);
        }
        else
        {
            showMessageDialog(result);
        }
    }
    
    return false;
}

function answerQuestion_step3(id)
{
    window.location.href = "/commend-" + id + ".html";
}

function ajax_setExcellenceAnswer(answerId, questionId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
        "/ajax_question_post.aspx", 
        { "action": "excellence_answer", "question_id": questionId, "answer_id" : answerId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function setExcellenceAnswer(answerId, questionId)
{
     var result = ajax_setExcellenceAnswer(answerId, questionId);
    if (result == "1")
    {
        showMessageDialog("最佳答案已经被成功设置。", reload);
    }
    else
    {
        showMessageDialog(result);
    }
}

function ajax_addUserHope(surveyName,surveyNote)
{
    var result = "";
    try
    {
            result = syncAjaxRequest("post", 
            "/ajax_ask_userhope.aspx", 
            { "action": "adduserhope", "name":surveyName,"note":surveyNote });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

//问药活动页面用户调查
function addUserSurvey()
{
    var str="";
    $("[name='survey_radiobutton']").each(function(){
      if($(this).attr("checked")==true)
      {
         str=$(this).val();  
      }
    });
    if(str == "")
    {
        showMessageDialog("您还没有选择感兴趣的健康话题。")
        return;
    }
    
    var result = ajax_addUserHope("yaolee2010_ask_survey",str);
    if (result == "1")
    {
        showMessageDialog("您感兴趣的健康话题己经提交到我们的数据库，我们将会认真考虑您的意见。谢谢您的参与。")
        $("[name='survey_radiobutton']").removeAttr("checked");
        return;
    }
    else
    {
        showMessageDialog(result);
    }
}

//提交关键词
function checkSubmitKeyword()
{
    var str = $("#keyword_txt").val();
    var initText = '请在这里输入您关心的问题。';
    
    if (str == "" || "" + str == initText)
    {
        $('#keyword_txt').focus();
        return false;
    }
    else if($('#keyword_txt').val().length >80)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function submitKeyword(type)
{
    var str = $("#keyword_txt").val();
    if(type == 'search')
    {
        if(checkSubmitKeyword())
        {
            window.open('http://www.yaoliwang.com/search.html?type=question&keyword='+encodeURI(str));
        }
    }
    else
    {
        var initText = '请在这里输入您关心的问题。';
        if (str == "" || "" + str == initText)
        {
            window.open('/ask.html');
            return ;
        }
        else if(str.length >80)
        {
            window.open('/ask.html');
            return ;
        }
        else
        {
            window.open('/ask.html?title='+encodeURI(str));
        }
    }  
}

//添加关注的关键词
var attentionKeywordText = '订阅感兴趣的问题关键词';

function initAttentionKeyword() 
{  
  $('#attentionkeywordtext').val(attentionKeywordText);

  $('#attentionkeywordtext').focus(function() {
      if ('' + $('#attentionkeywordtext').val() == attentionKeywordText)
        $('#attentionkeywordtext').val('');
  });

  $('#attentionkeywordtext').blur(function() {
      if ('' + $('#attentionkeywordtext').val() == '')
        $('#attentionkeywordtext').val(attentionKeywordText);
  });
}

function ajax_addAttentionKeyword(keyWord)
{
    var result = "";
    try
    {
            result = syncAjaxRequest("post", 
            "/ajax_attention_keyword.aspx", 
            { "action": "add", "keyword": keyWord });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}
function addAttentionKeyword_step2(keyword)
{
    var result = "0";
    if(keyword.length > 0)
    {
        var result = ajax_addAttentionKeyword(keyword);
    }
    else
    {
        if ('' + $('#attentionkeywordtext').val() == attentionKeywordText || '' + $('#attentionkeywordtext').val() == '')
            return;
        var result = ajax_addAttentionKeyword($("#attentionkeywordtext").val());
    }
    
    if (result == "1")
    {
       addAttentionKeyword_step3();
    }
    else
    {
        showMessageDialog(result);
    }
    
}

function addAttentionKeyword_step3()
{
    showMessageDialog("关键词添加成功。",reload);
}

function addAttentionKeyword(keyword)
{
     result = ajax_isLogined();
     switch(result) {
        case "0":
            openLoginDialog(addAttentionKeyword_step2,keyword);
            break;
        case "1":
            addAttentionKeyword_step2(keyword)
            break;
        default:
            showMessageDialog(result);
    }
}

//删除关注的关键词
function ajax_deleteAttentionKeyword(keyword)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_attention_keyword.aspx", 
            { "action" : "delete", "keyword" : keyword});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function deleteAttentionKeyword_step2(keyword)
{
    showConfirmDialog("您确定要删除订阅关键词“"  + keyword + "”吗？", deleteAttentionKeyword_step3, keyword , null, null);
}

function deleteAttentionKeyword(keyword)
{
     result = ajax_isLogined();
     switch(result) {
        case "0":
            openLoginDialog(deleteAttentionKeyword_step2,keyword);
            break;
        case "1":
            deleteAttentionKeyword_step2(keyword)
            break;
        default:
            showMessageDialog(result);
    }
}

function deleteAttentionKeyword_step3(keyword)
{
    result = ajax_deleteAttentionKeyword(keyword);
    if (result == "1")
    {
       reload();
    }
    else
    {
        showMessageDialog(result);
    }
    
}
