﻿
var _WebRoot = "http://www.nature-museum.net";
/*
    判断对象类型是否为文档类型（与COWiki编辑器当中的 IsDocValid 函数相同）
*/
function IsDocObject(jDoc)
{
    if (typeof jDoc != "object")
        return false;
    
    if (jDoc==null)
    {
        //showDebuginfo("IsDocObject","结果是null");
        return false;
    }
    if ((typeof jDoc.Id=="string") && (typeof jDoc.Title=="string") && (typeof jDoc.Author=="string") && (typeof jDoc.Content=="string"))
    {
        return true;
    }
    else
    {
        //showDebuginfo("IsDocObject", "格式有问题，不是有效的文档对象");
        return false;
    }
}

function ValidDoc(jDoc)
{
    if (!IsDocObject(jDoc))
        return false;
        
    if (!jDoc.AppName)
    {
        showDebuginfo("ValidDoc", "Doc对象的 AppName 属性未设置！");
        return false;
    }   
    if (!jDoc.AppEntryId)
    {
        showDebuginfo("ValidDoc", "Doc对象的 AppEntryId 属性未设置！");
        return false;
    } 
    if (!jDoc.Category)
    {
        showDebuginfo("ValidDoc", "Doc对象的 Category 属性未设置！");
        return false;
    } 
    if (!jDoc.Title)
    {
        showDebuginfo("ValidDoc", "Doc对象的 Title 属性未设置！");
        return false;
    }
    if (!jDoc.ContentFormat)
    {
        showDebuginfo("ValidDoc", "Doc对象的 ContentFormat 属性未设置！");
        return false;
    } 
    if (!jDoc.Content)
    {
        showDebuginfo("ValidDoc", "Doc对象的 Content 属性未设置！");
        return false;
    } 
    return true;
}


/*
	全局的显示信息的函数
*/
function MsgBox(cMsg)
{
	alert(cMsg);
}

function GetLangName(nLangNumber)
{
    /*
    Unknow = 0,
    En = 1,
    Zh = 2,
    Latin = 3
    */
    
    if (typeof nLangNumber !="number")
        return null;
    if (nLangNumber==1)
        return "En";
    if (nLangNumber==2)
        return "Zh";
    if (nLangNumber==3)
        return "La";  /*  TODO：此处需要与.NET 组件内的枚举校对   */
        
}

/* 
    添加访问记录
*/
function AddAccessLog(cResName,cResId,cAction,nScores)
{
    showDebuginfo("CFH.js--AddAccessLog","添加资源访问统计信息");
    $.post("/AjaxServer/Server.ashx?service=statis&method=add",{ResName:cResName,ResId:cResId,Action:cAction,Scores:nScores });
}

/*
    设置用户的动作Cookie
*/
function SetUserCookie(cAppEntry,cResId,cAction,nDayPara)
{
    var nDays = 1;
    if (arguments.length>3)
        nDays = 4;
    $.cookie(cAppEntry+"_"+cResId+"_"+cAction, 'Yes', { expires: nDays });  // 设置当前文档已经被顶踩过了；如果用户意见想改变，1天后才能修改
}

/*
    取得用户的动作Cookie
*/

function GetUserCookie(cAppEntry,cResId,cAction)
{
    return  $.cookie(cAppEntry+"_"+cResId+"_"+cAction);
}

/*
    取得当前文档的指定动作的Cookie
*/
function GetCurrentCookie(cAction)
{
    return GetUserCookie(WebConfig.AppEntryId,WebConfig.CurrentDoc.ResId,cAction);
}

/*
    设置当前 Cookie 的使用状态
*/
function SetCurrentCookie(cAction)
{
    SetUserCookie(WebConfig.AppEntryId,WebConfig.CurrentDoc.ResId,cAction);
}

/*
    在评分系统不能服务时提示信息
*/
function ShowRatingDisableMsg()
{
    MsgBox("您对每篇文档只能评分一次！");
}

/*
    是否允许评分
*/
function EnableRating()
{
    if (GetCurrentCookie("RatingUsed")=="Yes")
        return false;
    return true;
}

/* 从服务器取得一个可用的GUID  */
function getGUIDFromServer(callback,errorCall)
{
    if (typeof callback!="undefined" && typeof errorCall!="undefined")
    {
        $.ajax(
          {
            type: "POST",
            url: "/AjaxServer/Server.ashx?service=comm&method=GetGUID",
            success: function(msg){ 
                if (IsJSONMsg(msg)) 
                {
                    ShowJSONMsg(msg);
                    callback();
                }
                else
                {
                   callback(msg);
                }},
            error:function()
                { 
                    errorCall(); 
                }
          });
    }
    else
    {
        $.get("/AjaxServer/Server.ashx?service=comm&method=GetGUID",function(data){ return data;});
       
    }
}

/*
    注意：此处的 msg 既可以为对象，也可以是对象的字符串表示形式
*/
function ShowJSONMsg(msg)
{
    if (typeof msg=="string")
    {
        try
        {
            msg = eval("("+msg+")");
        }
        catch(e)
            {
            MsgBox("ShowJSONMsg 信息格式不正确！"+msg);
            return;
        }
    }
    if (typeof msg=="object")
    {
        MsgBox(msg.message);
    }
}
/*
    取得 JSONMsg 当中的字段信息
    注意：此处的 msg 既可以为对象，也可以是对象的字符串表示形式
*/
function GetJSONMsg(msg, section)
{
    if (typeof msg=="string")
    {
        try
        {
            msg = eval("("+msg+")");
        }
        catch(e)
        {
            return "";
        }
    }
    if (typeof msg=="object")
    {
        return msg[section];
    }
}

/*
    判断是否为 JSONMsg 类型
*/
function IsJSONMsg(data)
{
    if (typeof data=="string")
    {
        try
        {
            data = eval("("+data+")");
        }
        catch(e)
        {
            //showDebuginfo("IsJSONMsg","字符串转换为对象失败！");
            return false;
        }
    }
    
    if (typeof data=="object")
    {
        if (data==null)
        {
            //showDebuginfo("IsJSONMsg","结果是null");
            return false;
        }
        if (typeof data.type == "string")
        {
            if (data.type=="jsonmsg")
                return true;
        }
        //showDebuginfo("IsJSONMsg","结果不是JSONMSg类型");
        return false;
    }

    //showDebuginfo("IsJSONMsg","结果不是对象类型");
    return false;
}

function GetJSONMsgObj(data)
{
    if (typeof data=="string")
    {
        try
        {
            data = eval("("+data+")");
        }
        catch(e)
        {
            //showDebuginfo("IsJSONMsg","字符串转换为对象失败！");
            return null;
        }
    }
    
    if (typeof data=="object")
    {
        if (data==null)
        {
            //showDebuginfo("IsJSONMsg","结果是null");
            return null;
        }
        if (typeof data.type == "string")
        {
            if (data.type=="jsonmsg")
                return data;
        }
        //showDebuginfo("IsJSONMsg","结果不是JSONMSg类型");
        return null;
    }   
}

/* 格式化拉丁学名 */ 
function FormatAsLatinName(nameContainer)
{
	$(nameContainer).each(
	function(index,Container)
	{
	    
	        
	        
		var cString = $(Container).text();
		var cAuthor = '';
		var cName = '';
		// 作者与名字的分界		
		var nAuthorLoc = GetAuthorNameLoc(cString);
		if (nAuthorLoc>0)
		{
			cAuthor = cString.substring(nAuthorLoc);
			cName = cString.substring(0,nAuthorLoc);
		}
		else
		{
			cName = cString;
		}
		cName = cName.replace(/\svar\./mg, "<span style='font-style:normal;'> var.</span>");
		cName = cName.replace(/ f\./mg, "<span class='LatinNameMark'> f.</span>");
		cName = cName.replace(/\scv\./mg, "<span class='LatinNameMark'> cv.</span>");
		cName = cName.replace(/\sSect\./mg, "<span class='LatinNameMark'> Sect.</span>");
		cName = cName.replace(/\sSer\./mg, "<span class='LatinNameMark'> Ser.</span>");
		cName = cName.replace(/\sssp\./mg, "<span class='LatinNameMark'> ssp.</span>");
		cName = cName.replace(/\ssubf\./mg, "<span class='LatinNameMark'> subf.</span>");
		cName = cName.replace(/\sSubFam\./mg, "<span class='LatinNameMark'> SubFam.</span>");
		cName = cName.replace(/\sSubgen\./mg, "<span class='LatinNameMark'> Subgen.</span>");
		cName = cName.replace(/\s×\s/mg, "<span class='LatinNameMark'> × </span>");
		cName = cName.replace(/\sX\s/mg, "<span class='LatinNameMark'> × </span>");
		cName = cName.replace(/\sSubsect\./mg, "<span class='LatinNameMark'> Subsect.</span>");
		cName = cName.replace(/\sSubser\./mg, "<span class='LatinNameMark'> Subser.</span>");
		cName = cName.replace(/\ssubsp\./mg, "<span class='LatinNameMark'> subsp.</span>");	
		cName = cName.replace(/\sSubtrib\./mg, "<span class='LatinNameMark'> Subtrib.</span>");
		cName = cName.replace(/\ssubvar\./mg, "<span class='LatinNameMark'> subvar.</span>");
		cName = cName.replace(/\sTrib\./mg, "<span class='LatinNameMark'> Trib.</span>");
		cName = cName.replace(/\svar\.\s×\s/mg, "<span class='LatinNameMark'> var. × </span>");
        
		// 第二步：识别名字当中需要变正体的标识符
		var cLevel = $(Container).attr("level");
	    if (cLevel)
	    {
	        if (cLevel=="Kingdom" || cLevel=='Phylum' || cLevel=='SubPhylum' || cLevel=='Class' || cLevel=='SubClass' || cLevel=='Order' || cLevel=='SubOrder' || cLevel=='Family' || cLevel=='SubFamily' || cLevel=='Tribe')
	        {
	            $(Container).html("<span>"+cName+"</span><span class='LatinAuthor'>"+cAuthor+"</span>");
	            return;
	        }
	    }
	    $(Container).html("<span class='LatinName'>"+cName+"</span><span class='LatinAuthor'>"+cAuthor+"</span>");
	}
	);
}
// 从拉丁学名当中取得作者名字
function GetAuthorNameLoc(lname)
{
	var sgnStart = lname.indexOf("(");
	if (sgnStart>0)
	{
		return sgnStart;
	}
	else
	{
		for(var i=1;i<lname.length;i++)
		{
			if (lname.charCodeAt(i)>64 && lname.charCodeAt(i)<91)
			{
				return i;
			}
		}
	}
	return 0;
}
