﻿
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()
{
    $(".groupdtlist ul li").hover(
        function(){
            $(this).removeClass("bgcolor_ffffff");
            $(this).addClass("bgcolor_ffffee");
        },
        function(){
             $(this).removeClass("bgcolor_ffffee");
            $(this).addClass("bgcolor_ffffff");
        });
        
     $(".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 ajax_isQuanMember(quan)
{
    var result = "";
    try 
    {
        result = syncAjaxRequest("get", "/ajax_quan_join.aspx", "action=check&quan=" + quan);        
    }
    catch(ex) 
    {
        result = ex;
    }
    
    return result;
}

function ajax_quanJoin(quan)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_quan_join.aspx", 
            { "action": "join", "quan": quan});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_quanQuit(quan)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_quan_join.aspx", 
            { "action": "quit", "quan": quan});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_newIllness(quan, illnessPathogeny, illnessTherapy, illnessEffect, illnessSymptom, illnessDescription, illnessSecurityLevel)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_illness_post.aspx", 
            {   "action": "new", 
                "quan": quan, 
                "illness_pathogeny": illnessPathogeny, 
                "illness_therapy": illnessTherapy, 
                "illness_effect": illnessEffect,
                "illness_symptom": illnessSymptom,
                "illness_description": illnessDescription,
                "illness_security_level": illnessSecurityLevel
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_checkIllness(quan)
{
 var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_illness_post.aspx", 
            { "action": "check", "quan": quan});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_dealIllness(op, id)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_illness_post.aspx", 
            { "action": op, "id": id});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_getInitIllnessData(quan)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_illness_post.aspx", 
            { "action": "init", "quan": quan});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_getEditIllnessData(illnessId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_illness_post.aspx", 
            { "action": "init_edit", "id": illnessId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_editIllness(id, illnessPathogeny, illnessTherapy, illnessEffect, illnessSymptom, illnessDescription, illnessSecurityLevel)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_illness_post.aspx", 
            {   "action": "edit", 
                "id": id, 
                "illness_pathogeny": illnessPathogeny, 
                "illness_therapy": illnessTherapy, 
                "illness_effect": illnessEffect,
                "illness_symptom": illnessSymptom,
                "illness_description": illnessDescription,
                "illness_security_level": illnessSecurityLevel
             });
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_newTopic(quan, titleType, title, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_topic_post.aspx", 
            { "action": "new", "quan": quan, "topic_title_type": titleType, 
            "topic_title": title,  "topic_content": content});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_checkTopic(quan)
{
 var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_topic_post.aspx", 
            { "action": "check", "quan": quan});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_deleteTopic(id)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_topic_post.aspx", 
            { "action": "delete", "id": id});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_newReply(type, relateId, content)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_reply_post.aspx", 
            { "action": "new", "type": type, "relate_id": relateId, "reply_content": content});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_checkReply(type, relateId)
{
        var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_reply_post.aspx", 
            { "action": "check", "type": type, "relate_id": relateId});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function ajax_deleteReply(id)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
            "/ajax_reply_post.aspx", 
            { "action": "delete", "id": id});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function deleteReply(id)
{
    var result = ajax_deleteReply(id)
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

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 ajax_deleteFriend(friendId)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("get", 
            "/ajax_friend.aspx", 
            { "action": "delete", "friend": friendId});
    }
    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").html(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 initQuanJoinDialog(quan)
{
    if ($("#quan_join_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"quan_join_dialog", "quan":quan}));
        $("#quan_join_dialog").dialog({
		    autoOpen: false,
		    width: 400,
		    height: 250,
            resizable: false,
            bgiframe: true,			
            modal: true
	    });
	}
}

function openQuanJoinDialog(quan, completeFunction, param)
{
    initQuanJoinDialog(quan);
    
    $("#quan_join_dialog").dialog("option", "buttons", {
		 " 否 ": function() {
				 $("#quan_join_dialog").dialog("close");
		    },
	    " 是 ": function() {
	        var result = ajax_quanJoin(quan);                    
            if (result == "1")
            {
                $("#quan_join_dialog").dialog("close");
                if (completeFunction != null)
                    completeFunction(param);
            }
            else
            {
                $("#quan_join_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#quan_join_dialog").dialog("option", "open", function(){
     	$("#quan_join_dialog_validate_tips").text("您还没有加入该圈子，需要加入吗？").css("color", "");
    });
    
    $("#quan_join_dialog").dialog("open");
}

function joinQuan(quanName)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(joinQuanStep2, quanName);
            break;
        case "1":
            joinQuanStep2(quanName);
            break;
        default:
            showMessageDialog(result);
    }
}

function joinQuanStep2(quanName)
{
    var result = ajax_isQuanMember(quanName);
    switch(result) {
        case "0":
            openQuanJoinDialog(quanName, reload, null);
            break;
        case "1":
            showMessageDialog("您己经是该圈子成员，不需要再加入了。", reload, null);
            break;
        default:
            showMessageDialog(result);
    }
}

function quitQuan(quanName, quanCName)
{
    showConfirmDialog("您确定要退出“" + quanCName + "”吗？", quitQuanProc, quanName);
}

function quitQuanProc(quanName)
{
    var result = ajax_quanQuit(quanName)
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

function initNewSymptomDialog()
{
    if ($("#new_symptom_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"new_symptom_dialog"}));
        $("#new_symptom_dialog").dialog({
		    autoOpen: false,
		    height: 210,
		    width: 290,
            resizable: false,
            bgiframe : true,			
            modal: true
	    });
	 }
}

function newSymptom()
{
    initNewSymptomDialog();
    
    $("#new_symptom_dialog").dialog("option", "buttons", {
		" 取消 ": function() {
				 $("#new_symptom_dialog").dialog("close");
		    },
	    " 确定 ": function() {
	        var symptomName = "" + $("#symptom_name").val();
	        var symptomStatus = "" + $("#symptom_status").val();
	        
	        if (symptomName != "")
	        {
	            buildIllnessDialogSymptomTableRow(symptomName, symptomStatus);
	            $("#new_symptom_dialog").dialog("close");
	        }
	        else
	        {
	            $("#new_symptom_dialog_validate_tips").text("请输入症状名称").css("color", "red");
	        }
        }
    });
    
    $("#new_symptom_dialog").dialog("option", "open", function(){
        $("#symptom_name").val("");
        $("#symptom_status").val("无");
     	$("#new_symptom_dialog_validate_tips").text("请输入症状属性").css("color", "");
    });
    
    $("#new_symptom_dialog").dialog("open");
}

function deleteSymptom(a)
{    
    $("tr", $("#illness_symptom")).each(function(){
        if ($(".illness_dialog_symptom_delete", $(this)).get(0) == a)
        {
            $(this).remove();
        }
    });
}

function initNewIllnessDialog(quan)
{
    if ($("#new_illness_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"new_illness_dialog", "quan":quan}));
        $("#new_illness_dialog").dialog({
		    autoOpen: false,
		    height: 520,
		    width: 450,
            resizable: false,	
            bgiframe: true,			
            modal: true
	    });
	 }
}

function buildIllnessDialogSymptomTableRow(s1, s2)
{
    var options = ["无", "较轻", "较重"];
    var select = "<select>";
    for (var i = 0; i < options.length; i++)
    {
        if (options[i] == s2)
            select += "<option value=\"" + options[i] + "\" selected>" + options[i] + "</option>";
        else
            select += "<option value=\"" + options[i] + "\">" + options[i] + "</option>";
    }
    
    select += "</select>";
    
    var str = "<tr><td class=\"illness_dialog_symptom_td\" style=\"width:150px;\" nowrap>" + s1 + 
        "</td><td>" + select + 
        "</td><td style=\"width:22px;text-align:right;\"><a class=\"illness_dialog_symptom_delete\" href=\"javascript:void(0)\" onclick=\"deleteSymptom(this)\" title=\"删除\"><img src=\"/images/delete_small.gif\" alt=\"删除\" /></a></td></tr>";
        
    $("#illness_symptom").append(str);
}

function buildIllnessDialogSymptomTable(s)
{
    var arr = s.split("|");
    for (var i = 0; i < arr.length; i+=2)
    {
        buildIllnessDialogSymptomTableRow(arr[i], arr[i+1]);
    }
}

function getIllnessDialogSymptomString()
{
    var result = "";
    
    $("tr", $("#illness_symptom")).each(function(){
        if (result != "")
            result += "|";
            
        result += $(".illness_dialog_symptom_td", $(this)).text() + "|" + $("select", $(this)).val();
    });
    
    return result;
}

function openNewIllnessDialog(quan, completeFunction, param)
{
    initNewIllnessDialog(quan);
    
    var initData = ajax_getInitIllnessData(quan);
    var arrInitData = initData.split("\n");
    if (arrInitData.length == 6)
    {
        $("#illness_pathogeny").val(arrInitData[0]);
        $("#illness_therapy").val(arrInitData[1]);
        $("#illness_effect").val(arrInitData[2]);
        $("#illness_symptom").html("");
        buildIllnessDialogSymptomTable(arrInitData[3]);
        $("#illness_description").val(arrInitData[4]);
        $("#illness_security_level").val(arrInitData[5]);
    }
    else if (initData != "")
    {
        showMessageDialog(initData);
        return;
    }
    
    $("#new_illness_dialog").dialog("option", "buttons", {
		 " 撤消 ": function() {
				$("#new_illness_dialog").dialog("close");
		    },
	    " 好了，发布 ": function() {
	    
	        var illnessPathogeny = "" + $("#illness_pathogeny").val();
	        var illnessTherapy = "" + $("#illness_therapy").val();
	        var illnessEffect = "" + $("#illness_effect").val();
	        var illnessSathogeny = getIllnessDialogSymptomString();
	        var illnessDescription = "" + $("#illness_description").val();
	        var illnessSecurityLevel = "" + $("#illness_security_level").val();
	        
	        if (illnessPathogeny == "")
            {
                $("#new_illness_dialog_validate_tips").text("您忘了输入病因。").css("color", "red");
                $("#illness_pathogeny").focus();
                return;
            }
            
            if (illnessTherapy == "")
            {
                $("#new_illness_dialog_validate_tips").text("您忘了输入治疗方法。").css("color", "red");
                $("#illness_therapy").focus();
                return;
            }
            
            if (illnessDescription.length > 2000)
            {
                $("#new_illness_dialog_validate_tips").text("请您将描述控制在2000个字以内。").css("color", "red");
                $("#illness_description").focus();
                return;
            }
	    
	        var result = ajax_newIllness(quan, illnessPathogeny, illnessTherapy, illnessEffect, illnessSathogeny, illnessDescription, illnessSecurityLevel);
	        if (result > 0)
            {
                $("#new_illness_dialog").dialog("close");
                if (completeFunction != null)
                    completeFunction(result);
            }
            else
            {
                $("#new_illness_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#new_illness_dialog").dialog("option", "open", function(){
        $("#new_illness_dialog_validate_tips").text("发布您的病情，分享您的经验。").css("color", "");
    });
    
    $("#new_illness_dialog").dialog("open");
}

function newIllness(quanName) {
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(newIllnessStep2, quanName);
            break;
        case "1":
            newIllnessStep2(quanName);
            break;
        default:
            showMessageDialog(result);
    }
}

function newIllnessStep2(quanName) {
    var result = ajax_isQuanMember(quanName);
    switch(result) {
        case "0":
            openQuanJoinDialog(quanName, newIllnessStep3, quanName);
            break;
        case "1":
            newIllnessStep3(quanName);
            break;
        default:
            showMessageDialog(result);
    }
}

function newIllnessStep3(quanName) 
{    
    var result = ajax_checkIllness(quanName);
    if ("" + result != "1")
    {
        showMessageDialog(result);
        return;
    }
    
    openNewIllnessDialog(quanName, newIllnessStep4, null);
}

function newIllnessStep4(result) {
    showMessageDialog('病情发布成功。', finishNewIllness, result);
}

function finishNewIllness(result) {
    window.open("/illness/"+result+".html");
} 

function openEditIllnessDialog(id, completeFunction, param)
{
    initNewIllnessDialog("");
    
    var initData = ajax_getEditIllnessData(id);
    var arrInitData = initData.split("\n");
    if (arrInitData.length == 6)
    {
        $("#illness_pathogeny").val(arrInitData[0]);
        $("#illness_therapy").val(arrInitData[1]);
        $("#illness_effect").val(arrInitData[2]);
        
        $("#illness_symptom").html("");
        buildIllnessDialogSymptomTable(arrInitData[3]);
        
        $("#illness_description").val(arrInitData[4]);
        $("#illness_security_level").val(arrInitData[5]);
    }
    else
    {
        showMessageDialog(initData);
        return;
    }
    
    $("#new_illness_dialog").dialog("option", "buttons", {
		 " 撤消 ": function() {
				$("#new_illness_dialog").dialog("close");
		    },
	    " 好了，发布 ": function() {
	    
	        var illnessPathogeny = "" + $("#illness_pathogeny").val();
	        var illnessTherapy = "" + $("#illness_therapy").val();
	        var illnessEffect = "" + $("#illness_effect").val();
	        var illnessSathogeny = getIllnessDialogSymptomString();
	        var illnessDescription = "" + $("#illness_description").val();
	        var illnessSecurityLevel = "" + $("#illness_security_level").val();
	        
	        if (illnessPathogeny == "")
            {
                $("#new_illness_dialog_validate_tips").text("您忘了输入病因。").css("color", "red");
                $("#illness_pathogeny").focus();
                return;
            }
            
            if (illnessTherapy == "")
            {
                $("#new_illness_dialog_validate_tips").text("您忘了输入治疗方法。").css("color", "red");
                $("#illness_therapy").focus();
                return;
            }
            
            if (illnessDescription.length > 2000)
            {
                $("#new_illness_dialog_validate_tips").text("请您将描述控制在2000个字以内。").css("color", "red");
                $("#illness_description").focus();
                return;
            }
	    
	        var result = ajax_editIllness(id, illnessPathogeny, illnessTherapy, illnessEffect, illnessSathogeny, illnessDescription, illnessSecurityLevel);
	        if (result == "1")
            {
                if (completeFunction != null)
                    completeFunction(param);
            }
            else
            {
                $("#new_illness_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#new_illness_dialog").dialog("option", "open", function(){
        $("#new_illness_dialog_validate_tips").text("更新您的病情卡，您将获得更全面的健康服务。").css("color", "");
    });
    
    $("#new_illness_dialog").dialog("open");
}

function editIllness(id) {
    openEditIllnessDialog(id, reload, null);
} 

function deleteIllness(id)
{
    showConfirmDialog("您确认要删除该病情吗？", deleteIllnessProc, id, null, null);
}

function deleteIllnessProc(id)
{
    var result = ajax_dealIllness("delete", id);
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

function dealIllness(op, id)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(dealIllnessStep2, [op, id]);
            break;
        case "1":
            dealIllnessStep2([op, id]);
            break;
        default:
            showMessageDialog(result);
    }
}

function dealIllnessStep2(s)
{
    var result = ajax_dealIllness(s[0], s[1]);
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

function initNewTopicDialog(quan)
{
    if ($("#new_topic_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"new_topic_dialog", "quan":quan}));
        $("#new_topic_dialog").dialog({
		    autoOpen: false,
		    height: 380,
		    width: 490,
            resizable: false,	
            bgiframe: true,			
            modal: true
	    });
	}
}

function openNewTopicDialog(quan, completeFunction, param)
{
    initNewTopicDialog(quan);
    
    $("#new_topic_dialog").dialog("option", "buttons", {
		 " 撤消 ": function() {
				$("#new_topic_dialog").dialog("close");
		    },
	    " 好了，发言 ": function() {
	        
	        var title = trim("" + $("#topic_title").val());
	        var content = "" + $("#topic_content").val();
	        if (title == "")
            {
                $("#new_topic_dialog_validate_tips").text("您忘了输入标题。").css("color", "red");
                $("#topic_title").focus();
                return;
            }
            
            if (content.length < 2)
            {            
                $("#new_topic_dialog_validate_tips").text("您忘了输入内容，内容至少为2个字符。").css("color", "red");
                $("#topic_content").focus();
                return;
            }
            
            if (content.length > 5000)
            {
                $("#new_topic_dialog_validate_tips").text("请您将内容控制在5000个字以内。").css("color", "red");
                $("#topic_content").focus();
                return;
            }
            
            var result = ajax_newTopic(quan, $("#topic_title_type").val(), title, content);
            
           if (result > 0 )
            {
                $("#topic_title").val("");
                $("#topic_content").val("");
                $("#new_topic_dialog").dialog("close");
                
                if (completeFunction != null)
                    completeFunction(result);
            }
            else
            {
                $("#new_topic_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#new_topic_dialog").dialog("option", "open", function(){
        $("#new_topic_dialog_validate_tips").text("欢迎与网友交流您的情况。").css("color", "");
		$("#topic_title").focus();
    });
    
    $("#new_topic_dialog").dialog("open");
}

function initNewReplyDialog()
{
    if ($("#new_reply_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"new_reply_dialog"}));
        $("#new_reply_dialog").dialog({
		    autoOpen: false,
		    height: 320,
		    width: 440,
            resizable: false,
            bgiframe: true,				
            modal: true
        });
    }
}

function openNewReplyDialog(type, relateId, completeFunction, param)
{
    initNewReplyDialog();
    
    $("#new_reply_dialog").dialog("option", "buttons", {
		 " 撤消 ": function() {
				$("#new_reply_dialog").dialog("close");
		    },
	    " 好了，回复 ": function() {
	        var content = "" + $("#reply_content").val();
	       
            if (content == "")
            {
                $("#new_reply_dialog_validate_tips").text("您忘了输入回复内容。").css("color", "red");
                return;
            }

            if (content.length > 5000)
            {
                $("#new_reply_dialog_validate_tips").text("请您将内容控制在5000个字以内。").css("color", "red");
                return;
            }

            var result = ajax_newReply(type, relateId, content);
                
            if (result == "1")
            {
                $("#reply_content").val("");
                $("#new_reply_dialog").dialog("close");
                
                if (completeFunction != null)
                    completeFunction(param);
            }
            else
            {
                $("#new_reply_dialog_validate_tips").text(result).css("color", "red");
            }
        }
    });
    
    $("#new_topic_dialog").dialog("option", "open", function(){
         $("#new_reply_dialog_validate_tips").text("有什么高见，请您发言吧。").css("color", "");
    });
    
    $("#new_reply_dialog").dialog("open");
}

function newTopic(quanName) {
    var result = ajax_isLogined();
   
    switch(result) {
        case "0":
            openLoginDialog(newTopicStep2, quanName);
            break;
        case "1":
            newTopicStep2(quanName);
            break;
        default:
            showMessageDialog(result);
    }
}



function newTopicStep2(quanName) {
    if(quanName == "choose")
    {
        openChooseGroupDialog(quanName, null, quanName);
        return;
    }
    else
    {
        var result = ajax_isQuanMember(quanName);
        switch(result) {
            case "0":
                openQuanJoinDialog(quanName, newTopicStep3, quanName);
                break;
            case "1":
                newTopicStep3(quanName);
                break;
            default:
                showMessageDialog(result);
                
        }
    }
}

function newTopicStep3(quanName) {
    var result = ajax_checkTopic(quanName);
    if ("" + result != "1")
    {
        showMessageDialog(result);
        return;
    }
    
    openNewTopicDialog(quanName, newTopicStep4, null);
}  

function newTopicStep4(result) {
    showMessageDialog('发表新话题成功，恭喜您，您的积分 <span style="color:red; font-size:24px; font-weight:bold;">+5</span>', finishNewTopic, result);
} 

function finishNewTopic(result) {
    window.open("/topic/"+result+".html");
} 

function deleteTopic(id)
{
    showConfirmDialog("您确认要删除该话题吗？", deleteTopicProc, id, null, null);
}

//选择圈子
function initChooseGroupDialog(quan)
{
    if ($("#choose_group_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"choose_group_dialog", "quan":quan}));
        $("#choose_group_dialog").dialog({
		    autoOpen: false,
		    height: 400,
		    width: 580,
            resizable: false,	
            bgiframe: true,			
            modal: true
	    });
	}
}

function openChooseGroupDialog(quan, completeFunction, param)
{
    initChooseGroupDialog(quan);
    
    $("#choose_group_dialog").dialog("option", "buttons", {
		 " 撤消 ": function() {
				$("#choose_group_dialog").dialog("close");
		    },
	    " 选择 ": function() {
	        
	        var count = 0;
	        var quan_name = "";
            $("input[name='group_title_type']").each(function(){
                  if($(this).attr("checked"))
                  {
                     quan_name = $(this).val();
                     count ++;
                  }
            });
            
            if(quan_name == "")
            {
                showMessageDialog("您还没有选择要发言的圈子。")
                return;
            }
            if(count != 1)
            {
                showMessageDialog("请选择一个您要发言的圈子。")
                return;
            }
            
	        if(quan_name.length > 0)
	        {
                newTopicStep2(quan_name);
                
                $("#choose_group_dialog").dialog("close");
                
                if (completeFunction != null)
                        completeFunction(param);
            }
            else
            {
                $("#choose_group_dialog_validate_tips").text("请选择一个您要发言的圈子。").css("color", "red");
            }
            
        }
    });
    
    $("#choose_group_dialog").dialog("option", "open", function(){
        $("#choose_group_dialog_validate_tips").text("请选择一个您要发言的圈子。").css("color", "");
    });
    
    $("#choose_group_dialog").dialog("open");
}

function selectLeaveWordGroup()
{
    var count = 0;
    $("input[name='group_title_type']").each(function(){
          if($(this).attr("checked"))
          {   
             count ++;
          }
    });
    
     $("input[name='group_title_type']").each(function(){
          if(count >= 1)
          {
            if (!$(this).attr("checked"))
                $(this).attr("disabled", true);
          }
          else
          {
            $(this).attr("disabled", false);
          }
    });
      
}

function deleteTopicProc(id)
{
    var result = ajax_deleteTopic(id);
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

function newReply(type, relateId,quanName)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(newReplyStep2, [type,relateId,quanName]);
            break;
        case "1":
            newReplyStep2([type,relateId,quanName]);
            break;
        default:
            showMessageDialog(result);
    }
}

function newReplyStep3(s)
{
    var result = ajax_checkReply(s[0], s[1]);
    if ("" + result != "1")
    {
        showMessageDialog(result);
        return;
    }
    
    if(s[0] == 'topic')
    {
        openNewReplyDialog(s[0], s[1], newReplyStep4, null);
    }
    else
    {
        openNewReplyDialog(s[0], s[1], reload, null);
    }
}


function newReplyStep4() 
{
    showMessageDialog('回复话题成功，恭喜您，您的积分 <span style="color:red; font-size:24px; font-weight:bold;">+1</span>', reload, null);
} 

function newReplyStep2(s) {
    var result = ajax_isQuanMember(s[2]);
    switch(result) {
        case "0":
            openQuanJoinDialog(quanName, newReplyStep3, [[s[0], s[1]]]);
            break;
        case "1":
            newReplyStep3([[s[0], s[1]]]);
            break;
        default:
            showMessageDialog(result);
    }
}

function deleteTopicReply(id)
{
    showConfirmDialog("您确认要删除该回复吗？", deleteTopicReplyProc, id, null, null);
}

function deleteTopicReplyProc(id)
{
    var result = ajax_deleteReply(id);
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

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 deleteFriend(friendId, friendName)
{
    showConfirmDialog("您要解除与“" + friendName + "”的好友关系吗？", deleteFriendProc, friendId, null, null);
}

function deleteFriendProc(friendId)
{
    var result = ajax_deleteFriend(friendId);
    if (result != "1")
    {
        showMessageDialog(result);
    }
    else
    {
        reload();
    }
}

function initMRWelcomeDialog()
{
    $("#mr_welcome_dialog").dialog({
		autoOpen: false,
		height: 200,
		width: 400,
        resizable: false,			
        modal: true,
        buttons: {
		    " 稍后填写 ": function() {
				$("#mr_welcome_dialog").dialog("close");
		    },
		    " 简化模式 ": function() {
		        $("#mr_welcome_dialog").dialog("close");
		        openMRDetailDialog();
            },
            " 开始填写 ": function() {
            }
		}
	});
}

function openMRWelcomeDialog()
{
    $("#mr_welcome_dialog").dialog("open");
}

function initMRDetailDialog()
{    
    $("#mr_detail_dialog").dialog({
		autoOpen: false,
		height: 450,
		width: 500,
        resizable: false,			
        modal: true,
        buttons: {
		    " 取消 ": function() {
				$("#mr_detail_dialog").dialog("close");
		    },
		    " 提交 ": function() {
            },
            " 向导模式 ": function() {
            }
		}
	});
}

function openMRDetailDialog()
{
    $("#mr_detail_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";
    else if (document.domain.toLowerCase().indexOf("124.205.145.3") > 0)
        domain = "yaoliwang.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";
    else if (document.domain.toLowerCase().indexOf("124.205.145.3") > 0)
        domain = "yaoliwang.com";
        
    $.cookie("login_password", "", {"path":"/", "expires": 365, "domain": domain});
}

//写邮件
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 replyIsLogined(quanName)
{
    var result = ajax_isLogined();
    switch(result) {
        case "0":
            openLoginDialog(replyIsLogined_step2,quanName);
            break;
        case "1":
            replyIsLogined_step2(quanName);
            break;
        default:
            showMessageDialog(result);
    }
}

function replyIsLogined_step2(quanName) {

    var result = ajax_isQuanMember(quanName);
    switch(result) {
        case "0":
            openQuanJoinDialog(quanName);
            break;
        case "1":
            break;
        default:
            showMessageDialog(result);
    }
}


function formNewReply(type, relateId)
{
    if(ajax_isLogined() == "1")
    {
        var initText = '请在这里输入您的回复内容（最多5000个字）。';
        if ("" + $('#form_reply_content').val() == "" || "" + $('#form_reply_content').val() == initText)
        {
            $('#form_reply_content').focus();
            return false;
        }
        else if($('#form_reply_content').val().length > 5000)
        {
            showMessageDialog('请输入不多于5000个字符。');
            return false;
        }
        
        var result = ajax_checkReply(type, relateId);
        if ("" + result != "1")
        {
            showMessageDialog(result);
            return false;
        }
        
        var result = ajax_newReply("" + type, relateId, $('#form_reply_content').val());
        if (result == "1")
        {
            if ("" + type == "topic")
                showMessageDialog('回复话题成功，恭喜您，您的积分 <span style="color:red; font-size:24px; font-weight:bold;">+1</span>', reload, null);
            else
                showMessageDialog("您的回复内容已经成功提交。", reload);
        }
        else
        {
            showMessageDialog(result);
        }
    }
    
    return false;
}
