﻿//var xmlHttp

function GetPlantFullName()
{
    var seg = null;
    /*
    var curr_family = $id("FamilyList").selectedIndex;
    curr_family = $id("FamilyList").options[curr_family].text;
    seg = curr_family.indexOf("----");
    $id("FamilyName").innerText = curr_family.substring(0,seg);
    $id("FamilyCName").innerText = curr_family.substring(seg+4);
    
    var curr_genus = $id("GenusList").selectedIndex;
    curr_genus = $id("GenusList").options[curr_genus].text;
    seg = curr_genus.indexOf("----");
    $id("GenusName").innerText = curr_genus.substring(0,seg);
    $id("GenusCName").innerText = curr_genus.substring(seg+4);
    */
    
    if (typeof $("#Photo_ID").text()!="string")
    {
        alert("没有指定照片！");
        return false;
    }
    /*
    if ((typeof $("#Photo_ID").text().length)!=36)
    {
        alert("还没有指定当前照片，请先指定照片！");
        return false;
    }
    */
    
    var FName = $("#Ident_FamilyName").val();
    var CFName = $("#Ident_CFamilyName").val();
    
    if (typeof FName!="string" && typeof CFName !="string")
    {
        alert("没有给出科信息，鉴定无效！");
        return false;
    }
    else
    {
        if (FName=='' && CFName=='')
        {
           alert("没有给出科信息，鉴定无效！");
           return false;
        }
    }

    CheckValue($id("Ident_FamilyName"));
    CheckValue($id("Ident_CFamilyName"));
    CheckValue($id("Ident_GenusName"));
    CheckValue($id("Ident_CGenusName"));
    
    CheckValue($id("SpLName"));
    CheckValue($id("SpCName"));
    CheckValue($id("SpEName"));
    CheckValue($id("SpAName"));
    CheckValue($id("IdentCause"));
    CheckValue($id("IdentMemo"));
    CheckValue($id("Signature"));
    
    if (typeof $("#SpId")=="string")
    {
        var curr_sp = $("#SpId").val();
        if (curr_sp>0)
        {
            curr_sp = $("#SpId").text();
            seg = curr_sp.indexOf("----");
            if (seg<1)
            {
                alert("选择物种名称时出错！名称格式不正确，无法解析！");
                return false;
            }
            $id("SpLName").value = curr_sp.substring(0,seg);
            $id("SpCName").value = curr_sp.substring(seg+4);
        }
    }
    else
    {
        alert("注意：没有从物种库当中选择种名！") 
        return;
        // 注意这种情况是允许的，也许用户提交了新命名
    }
   
    return true;
}

function CheckValue(obj)
{
    if (obj.value.substring(0,2)=="--")
    {
        obj.value = "";
    }
}
function AddIdent2PhotoList(cSpid,cPhotoList,cCause,cTrueName)
{
    //alert("物种ID："+cSpid+" 照片列表："+cPhotoList);
    // 
    $.post("/Ajaxserver/server.ashx?service=photo&method=batident",
    {spid:cSpid,name_latin:'',PhotoList:cPhotoList,Cause:cCause,TrueName:cTrueName},
    function(msg)
    {
       ShowBatIdentResult("批量鉴定已经完成！");
       //ShowBatIdentResult("批量鉴定完成，马上将刷新网页！");
       //setTimeout("location.reload(true)",200);
    }
    );

}
function AddFavorite(cPhotoList, mainTag, call)
{
    $.post("/Ajaxserver/FavoritePhoto.ashx",
    { m: "add", PhotoList: cPhotoList, maintag: mainTag },
    function (msg)
    {
        if (IsJSONMsg(msg))
        {
            ShowJSONMsg(msg);
            if (typeof call == "function")
            {
                call();
            }
            return;
        }
    }
    );
}
function showMyFavoriteTags(containerId)
{
    $.getJSON("/Ajaxserver/FavoritePhoto.ashx?m=gettags&r="+Math.random(),
    function (json)
    {
        if (json.length < 1)
            return;
        var htm = "";
        for (var i = 0; i < json.length; i++)
        {
            htm += "<span style='padding:0 10px; font-weight:bold; cursor:pointer;' onclick=\"setFavTag('" + json[i] + "');\">" + json[i] + "</span>";
        }
        $("#" + containerId).html(htm);
    }
    );
}
// 提交正式鉴定（是经过用户选择认定的）
function AddSimpleIdent()
{
    var SpSelect = $("input[name='SelectedSpItem']:checked");
    var si_spid = $(SpSelect).val();
    var si_lname = $(SpSelect).attr("lname");
    var si_cname = $(SpSelect).attr("cname");
    var si_level = $(SpSelect).attr("level");
    // alert(si_spid+" "+si_cname+" "+si_lname+" "+si_level);
    if (typeof si_spid != "string" || si_spid=="" || typeof si_lname!="string" || si_lname=="")
    {
        alert("没有从物种库中匹配到记录，请重新输入物种名称，再次提交！");
        return;
    }
    
    var IdentData = "";
    // 必须输入的参数
    IdentData = addPostParam(IdentData,"PhotoID",$id("Photo_ID").innerText);
    IdentData = addPostParam(IdentData,"SPID",si_spid);   
    IdentData = addPostParam(IdentData,"LName",si_lname);
    IdentData = addPostParam(IdentData,"CName",si_cname);
    IdentData = addPostParam(IdentData,"TaxonLevel",si_level);
    // 附加参数
    IdentData = addPostParam(IdentData,"AName",$("#txtQuickIdent_AName").val());
    IdentData = addPostParam(IdentData,"TrueName",$("#txtQuickIdent_SignName").val());
    IdentData = addPostParam(IdentData,"Cause",$("#txtQuickIdent_Cause").val());
    
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="/AddIdent.ashx";
    xmlHttp.onreadystatechange = AddIdentReady;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(IdentData);
}



function AddSimpleIdentFunc(photoid,spid,lname,cname,splevel,aname,identman,cause)
{
    if (!spid && !lname)
    {
        alert("没有从物种库中匹配到记录，请重新输入物种名称，再次提交！");
        return;
    }

    var IdentData = "";
    // 必须输入的参数
    IdentData = addPostParam(IdentData, "PhotoID", photoid);
    IdentData = addPostParam(IdentData, "SPID", spid);
    IdentData = addPostParam(IdentData, "LName", lname);
    IdentData = addPostParam(IdentData, "CName", cname);
    IdentData = addPostParam(IdentData, "TaxonLevel", splevel);
    // 附加参数
    IdentData = addPostParam(IdentData, "AName", aname);
    IdentData = addPostParam(IdentData, "TrueName", identman);
    IdentData = addPostParam(IdentData, "Cause", cause);

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }
    var url = "/AddIdent.ashx";
    xmlHttp.onreadystatechange = AddIdentReady;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(IdentData);
}

// 添加临时鉴定
function AddTempIdent()
{
    //alert("正在调试...，稍后发布");
    //return;
    
    //var si_spid = "0";
    var si_genus = $("#tmpGenus").val();
    var si_lname = $("#tmpLName").val();
    var si_cname = $("#tmpCName").val();
    var si_signName = $("#txtTmpIdent_SignName").val();
    var si_desc = $("#tmpIdentMemo").val();
    
    //var si_level = $(SpSelect).attr("level");

    var IdentData = "";
    // 必须输入的参数
    IdentData = addPostParam(IdentData, "PhotoID", $id("Photo_ID").innerText);
    IdentData = addPostParam(IdentData, "LName", si_lname);
    IdentData = addPostParam(IdentData, "CName", si_cname);
    IdentData = addPostParam(IdentData, "Genus", si_genus);
    
    //IdentData = addPostParam(IdentData, "TaxonLevel", si_level);
    // 附加参数

    IdentData = addPostParam(IdentData, "TrueName", si_signName);
    IdentData = addPostParam(IdentData, "Description", si_desc);

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null)
    {
        alert("Your browser does not support AJAX!");
        return;
    }
    var url = "/AddTmpIdent.ashx";
    xmlHttp.onreadystatechange = AddIdentReady;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(IdentData);
}

// 查询物种信息
// cLNameCtrl ： 包含拉丁名的编辑控件ID
// cANameCtrl ： 包含别名的编辑控件ID
// aTarget    ： 结果容器的ID
function QuerySpByCtrl(cLNameCtrl, cANameCtrl, aTarget) 
{
    var cLName = $("#" + cLNameCtrl).val().TrimUnnecessaryBlank();
    var cAName = $("#" + cANameCtrl).val().TrimUnnecessaryBlank(); 
    return QuerySp(cLName, cAName, aTarget);
}

// 查询物种
// cLName ： 植物学拉丁名
// cAName ： 植物学别名
// aTarget： 查询结果（一个Html列表）容器的ID
function QuerySp(cLName, cAName, aTarget) {
    
    $("#"+aTarget).html("");

    $.getJSON("/Ajaxserver/server.ashx?service=spdict&method=querybynameauto&format=json", { lname: cLName, aname: cAName },
        function(data) {
            if (IsJSONMsg(data)) {
                ShowJSONMsg(data);
                return;
            }
            else {
                // 返回的是 List<SpDictItem>

                var SpCount = data.length;
                if (SpCount < 1) {
                    alert("您键入的物种名称不在当前物种库中，如果您确认它是正确的，请先添加到物种库当中！");
                    return;
                }
                if (SpCount > 0) {
                    // 将多个物种记录列出，显示为选项供用户选择
                    var SpOptionListHtml = "";
                    for (var i = 0; i < SpCount; i++) {
                        SpOptionListHtml += "<label><input type=\"radio\" name=\"SelectedSpItem\" value=\"";
                        SpOptionListHtml += data[i].Key;
                        SpOptionListHtml += "\" id=\"Opt_SelectedSpItem_" + i + "\" ";
                        SpOptionListHtml += "lname=\"";
                        SpOptionListHtml += data[i].Name_Latin;
                        SpOptionListHtml += "\" ";
                        SpOptionListHtml += "cname=\"";
                        SpOptionListHtml += data[i].Name_Zh;
                        SpOptionListHtml += "\" ";
                        SpOptionListHtml += "level=\"";
                        SpOptionListHtml += data[i].LevelEName;
                        SpOptionListHtml += "\" ";
                        if (i == 0) {
                            SpOptionListHtml += "checked=\"checked\" ";
                        }
                        SpOptionListHtml += " />" + data[i].Label + "</label><br />";
                    }
                    $("#" + aTarget).html(SpOptionListHtml);
                }

            }
        }
    );

}

// 准备废弃的方法
function QuerySpByName()
{
    $("#QueryResultSpList").html("");
    $("#QueryResultSpList_Row").hide();
                    
    var cLName = $("#txtQuickIdent_LName").val().TrimUnnecessaryBlank();
    var cAName = $("#txtQuickIdent_AName").val().TrimUnnecessaryBlank();

    $.getJSON("/Ajaxserver/server.ashx?service=spdict&method=querybynameauto&format=json", { lname: cLName, aname: cAName },
        function (data)
        {
            if (IsJSONMsg(data))
            {
                ShowJSONMsg(data);
                return;
            }
            else
            {
                // 返回的是 List<SpDictItem>

                var SpCount = data.length;
                if (SpCount < 1)
                {
                    alert("您键入的物种名称不在当前物种库中，如果您确认它是正确的，请先添加到物种库当中！");
                    return;
                }
                
                // 将多个物种记录列出，显示为选项供用户选择
                $("#QueryResultSpList").html("");
                var SpOptionListHtml = "";
                for (var i = 0; i < SpCount; i++)
                {
                    SpOptionListHtml += "<label><input type=\"radio\" name=\"SelectedSpItem\" value=\"";
                    SpOptionListHtml += data[i].Key;
                    SpOptionListHtml += "\" id=\"Opt_SelectedSpItem_" + i + "\" ";
                    SpOptionListHtml += "lname=\"";
                    SpOptionListHtml += data[i].Name_Latin;
                    SpOptionListHtml += "\" ";
                    SpOptionListHtml += "cname=\"";
                    SpOptionListHtml += data[i].Name_Zh;
                    SpOptionListHtml += "\" ";
                    SpOptionListHtml += "level=\"";
                    SpOptionListHtml += data[i].LevelEName;
                    SpOptionListHtml += "\" ";
                    if (i == 0)
                    {
                        SpOptionListHtml += "checked=\"checked\" ";
                    }
                    SpOptionListHtml += " />" + data[i].Label + "</label><br />";
                }
                $("#QueryResultSpList").html(SpOptionListHtml);
                $("#QueryResultSpList_Row").show();
            }
        }
    );
}
/* 直接根据文本只能获取物种名称  */
function GetSpByTxt(cName, resultContainer)
{
    $("#" + resultContainer).html("");
    $.getJSON("/Ajaxserver/server.ashx?service=spdict&method=querybynameauto&format=json", { lname: cLName, aname: cAName },
        function (data)
        {
            if (IsJSONMsg(data))
            {
                ShowJSONMsg(data);
                return "";
            }
            else
            {
                // 返回的是 List<SpDictItem>
                var SpCount = data.length;
                if (SpCount < 1)
                {
                    alert("您键入的物种名称不在当前物种库中，如果您确认它是正确的，请先添加到物种库当中！");
                    return;
                }
                if (SpCount > 0)
                {
                    var SpListHtml = "<div class='resultTips'>共搜索到" + SpCount + "条物种记录：</div><br />";
                    for (var i = 0; i < SpCount; i++)
                    {
                        SpListHtml += "<table ";
                        if (i % 2 == 0)
                        {
                            SpListHtml += "class='nooddrow'>";
                        }
                        else
                        {
                            SpListHtml += "class='oddrow'>";
                        }
                        SpListHtml += "<tr><td valign='top' align='center' style='font-size:10px;'>";
                        SpListHtml += "<a href=\"" + _WebRoot + "/spdb/TaxonNodeTree.aspx?spid=";
                        SpListHtml += data[i].Key;
                        SpListHtml += "\" target=\"_blank\">";
                        SpListHtml += "<img src=\"/Data/";
                        SpListHtml += data[i].PhotoID;
                        SpListHtml += ".photoThumbnail\" style=\"border:solid 1px #bbb; padding:1px; width:100px;\" /></a>";
                        SpListHtml += "<br />照片多于 " + data[i].PhotoCount + " 张";
                        SpListHtml += "</td><td style=\"padding-left:20px;\" valign='top'>";
                        SpListHtml += "<a href=\"" + _WebRoot + "/";
                        SpListHtml += data[i].Key;
                        SpListHtml += ".sp\" target=\"_blank\">";
                        SpListHtml += "<span class='SpCName'>";
                        SpListHtml += data[i].Name_Zh;
                        SpListHtml += "</span><br />";
                        SpListHtml += "<span class=\"SpLatinName\">";
                        SpListHtml += data[i].Name_Latin;
                        SpListHtml += "</span>";
                        SpListHtml += "<span class='SpLatinNameSAuthor'> ";
                        SpListHtml += data[i].SAuthor;
                        SpListHtml += "</span>";
                        SpListHtml += "</a><br />";
                        if (!IsEmpty(data[i].FamilyGenus))
                        {
                            SpListHtml += data[i].FamilyGenus.FamilyName_Zh + "&nbsp;&nbsp;" + data[i].FamilyGenus.FamilyName_Latin + "&nbsp;&nbsp;" + data[i].FamilyGenus.GenusName_Zh + "<br />";
                        }

                        if (!IsEmpty(data[i].NormalNamesList))
                        {
                            SpListHtml += "别名：";
                            for (var j = 0; j < data[i].NormalNamesList.length; j++)
                            {
                                if (data[i].NormalNamesList[j].Name == cAName || data[i].NormalNamesList[j].Name == cLName)
                                    SpListHtml += "<span class='foundWord'>" + data[i].NormalNamesList[j].Name + "</span>&nbsp;&nbsp;";
                                else
                                    SpListHtml += data[i].NormalNamesList[j].Name + "&nbsp;&nbsp;";
                            }
                            SpListHtml += "<br />";
                        }

                        var linkKey = _WebRoot + "/" + data[i].Key;
                        SpListHtml += "<a href=\"" + linkKey + ".spage\" target=\"_blank\"><span class='hightlightlink'>物种详细信息页：" + linkKey + ".spage" + "</span></a>" + "<br />";
                        SpListHtml += "<a href=\"" + linkKey + ".sp\" target=\"_blank\"><span class='hightlightlink'>物种信息卡：" + linkKey + ".sp" + "</span></a>" + "<br />";
                        SpListHtml += "<a href=\"" + linkKey + ".spalbum\" target=\"_blank\"><span class='hightlightlink'>物种图库：" + linkKey + ".spalbum" + "</span></a>" + "<br />";
                        //http: //www.cfh.ac.cn/Wiki/page.aspx?action=read&bookid=CFHWiki&target=Sp&targetid=28140
                        SpListHtml += "<a href=\"/Wiki/page.aspx?action=read&bookid=CFHWiki&target=Sp&targetid=" + data[i].Key + "\" target=\"_blank\"><span class='hightlightlink'>物种Wiki词条：" + linkKey + ".spWiki</span></a><br />";


                        SpListHtml += "</td></table>";
                    }
                    $("#" + resultContainer).html(SpListHtml);
                }

            }
        }
    );
}
function GetSpByName(cLName,cAName,resultContainer)
{
    $("#"+resultContainer).html("");
    $.getJSON("/Ajaxserver/server.ashx?service=spdict&method=querybynameauto&format=json", { lname:cLName,aname:cAName }, 
        function(data)
        {
            if (IsJSONMsg(data))
            {
                ShowJSONMsg(data);
                return "";
            }
            else
            {
                // 返回的是 List<SpDictItem>
                var SpCount = data.length;
                if (SpCount<1)
                {
                    alert("您键入的物种名称不在当前物种库中，如果您确认它是正确的，请先添加到物种库当中！");
                    return;
                }
                if (SpCount>0)
                {
                    var SpListHtml = "<div class='resultTips'>共搜索到"+SpCount+"条物种记录：</div><br />";
                    for(var i=0; i<SpCount;i++)
                    {
                        SpListHtml += "<table ";
                        if (i%2 == 0)
                        {
                            SpListHtml += "class='nooddrow'>";
                        }
                        else
                        {
                            SpListHtml += "class='oddrow'>";
                        }
                        SpListHtml += "<tr><td valign='top' align='center' style='font-size:10px;'>";
                        SpListHtml += "<a href=\""+_WebRoot+"/spdb/TaxonNodeTree.aspx?spid=";
                        SpListHtml += data[i].Key;
                        SpListHtml += "\" target=\"_blank\">";
                        SpListHtml += "<img src=\"/Data/";
                        SpListHtml += data[i].PhotoID;
                        SpListHtml += ".photoThumbnail\" style=\"border:solid 1px #bbb; padding:1px; width:100px;\" /></a>";
                        SpListHtml += "<br />照片多于 "+data[i].PhotoCount + " 张";
                        SpListHtml += "</td><td style=\"padding-left:20px;\" valign='top'>";
                        SpListHtml += "<a href=\""+_WebRoot+"/";
                        SpListHtml += data[i].Key;
                        SpListHtml += ".sp\" target=\"_blank\">";
                        SpListHtml += "<span class='SpCName'>";
                        SpListHtml += data[i].Name_Zh;
                        SpListHtml += "</span><br />";
                        SpListHtml += "<span class=\"SpLatinName\">";
                        SpListHtml += data[i].Name_Latin;
                        SpListHtml += "</span>";
                        SpListHtml += "<span class='SpLatinNameSAuthor'> ";
                        SpListHtml += data[i].SAuthor;
                        SpListHtml += "</span>";
                        SpListHtml += "</a><br />";
                        if (!IsEmpty(data[i].FamilyGenus))
                        {
                            SpListHtml += data[i].FamilyGenus.FamilyName_Zh +"&nbsp;&nbsp;"+data[i].FamilyGenus.FamilyName_Latin +"&nbsp;&nbsp;"+data[i].FamilyGenus.GenusName_Zh + "<br />";
                        }
                        
                        if (!IsEmpty(data[i].NormalNamesList))
                        {
                        SpListHtml += "别名：";
                        for(var j=0; j<data[i].NormalNamesList.length; j++)
                        {
                            if (data[i].NormalNamesList[j].Name== cAName || data[i].NormalNamesList[j].Name==cLName)
                                SpListHtml += "<span class='foundWord'>"+data[i].NormalNamesList[j].Name+"</span>&nbsp;&nbsp;";
                            else
                                SpListHtml += data[i].NormalNamesList[j].Name+"&nbsp;&nbsp;";
                        }
                        SpListHtml += "<br />";
                        }
                        
                        var linkKey = _WebRoot+"/"+data[i].Key;
                        SpListHtml += "<a href=\"" + linkKey + ".spage\" target=\"_blank\"><span class='hightlightlink'>物种详细信息页：" + linkKey + ".spage" + "</span></a>" + "<br />";
                        SpListHtml += "<a href=\"" + linkKey + ".sp\" target=\"_blank\"><span class='hightlightlink'>物种信息卡：" + linkKey + ".sp" + "</span></a>" + "<br />";
                        SpListHtml += "<a href=\"" + linkKey + ".spalbum\" target=\"_blank\"><span class='hightlightlink'>物种图库：" + linkKey + ".spalbum" + "</span></a>" + "<br />";
                        //http: //www.cfh.ac.cn/Wiki/page.aspx?action=read&bookid=CFHWiki&target=Sp&targetid=28140
                        SpListHtml += "<a href=\"/Wiki/page.aspx?action=read&bookid=CFHWiki&target=Sp&targetid=" + data[i].Key + "\" target=\"_blank\"><span class='hightlightlink'>物种Wiki词条：" + linkKey + ".spWiki</span></a><br />";

                        
                        SpListHtml += "</td></table>";
                    }
                    $("#"+resultContainer).html(SpListHtml);
                }
                
            }
        } 
    );
    }

/* 清除前面输入的鉴定信息和历史选项 */
function clearIdentHistory()
{
    $("#QueryResultSpList").html("");
    $("#QueryResultSpList").attr("spcount", "0");
    $("#QueryResultSpList_Row").hide();
}
function clearIdentHistoryAll()
{
    $("#txtQuickIdent_LName").val("");
    $("#txtQuickIdent_AName").val("");
    $("#QueryResultSpList").html("");
    $("#QueryResultSpList").attr("spcount", "0");
    $("#QueryResultSpList_Row").hide();
}
/* 简化步骤的鉴定提交函数  */
function AddIdentSimaple()
{
    var spcount = Number($("#QueryResultSpList").attr("spcount"));
    if (spcount > 0)
    {
        AddSimpleIdent();
        return;
    }

    clearIdentHistory();

    var cLName = $("#txtQuickIdent_LName").val().TrimUnnecessaryBlank();
    var cAName = $("#txtQuickIdent_AName").val().TrimUnnecessaryBlank();

    $.getJSON("/Ajaxserver/server.ashx?service=spdict&method=querybynameauto&format=json", { lname: cLName, aname: cAName },
        function (data)
        {
            if (IsJSONMsg(data))
            {
                $("#QueryResultSpList").attr("spcount", "0");
                ShowJSONMsg(data);
                return;
            }
            else
            {
                // 返回的是 List<SpDictItem>

                var SpCount = data.length;
                if (SpCount < 1)
                {
                    alert("您键入的物种名称不在当前物种库中，如果您确认它是正确的，请先添加到物种库当中！");
                    return;
                }

                $("#QueryResultSpList").attr("spcount", SpCount);

                // 将多个物种记录列出，显示为选项供用户选择
                $("#QueryResultSpList").html("");
                var SpOptionListHtml = "";
                if (SpCount > 1)
                    SpOptionListHtml += "<span style='color:#f00;'>有多个名字匹配，请选择：</span><br />";
                for (var i = 0; i < SpCount; i++)
                {
                    SpOptionListHtml += "<label><input type=\"radio\" name=\"SelectedSpItem\" value=\"";
                    SpOptionListHtml += data[i].Key;
                    SpOptionListHtml += "\" id=\"Opt_SelectedSpItem_" + i + "\" ";
                    SpOptionListHtml += "lname=\"";
                    SpOptionListHtml += data[i].Name_Latin;
                    SpOptionListHtml += "\" ";
                    SpOptionListHtml += "cname=\"";
                    SpOptionListHtml += data[i].Name_Zh;
                    SpOptionListHtml += "\" ";
                    SpOptionListHtml += "level=\"";
                    SpOptionListHtml += data[i].LevelEName;
                    SpOptionListHtml += "\" ";
                    if (i == 0)
                    {
                        SpOptionListHtml += "checked=\"checked\" ";
                    }
                    SpOptionListHtml += " />" + data[i].Label + "</label><br />";
                }
                $("#QueryResultSpList").html(SpOptionListHtml);
                $("#QueryResultSpList_Row").show();


                if (SpCount == 1)
                {
                    var photoid = $("#Photo_ID").text();
                    var aname = $("#txtQuickIdent_AName").val();
                    var truename = $("#txtQuickIdent_SignName").val();
                    var cause = $("#txtQuickIdent_Cause").val();
                    /*alert(photoid+" spid="+data[0].Key+" LName="+data[0].Name_Latin+data[0].Name_Zh+data[0].LevelEName+aname+truename+cause);*/
                    AddSimpleIdentFunc(photoid, data[0].Key, data[0].Name_Latin, data[0].Name_Zh, data[0].LevelEName, aname, truename, cause);
                }
            }
        }
    );
}

// 添加新鉴定
function AddNewIdent()
{
    //  此处为校验物种名称正确性
    if (!GetPlantFullName())
    {
        return false;
    }
        
    var IdentData = "";
    IdentData = addPostParam(IdentData,"PhotoID",$id("Photo_ID").innerText);
    IdentData = addPostParam(IdentData,"FamilyID",$("#FamilyId").val());
    if (typeof $("#GenusId").val()=="string")
    {
        IdentData = addPostParam(IdentData,"GenusID",$("#GenusId").val());
    }
    else
    {
        IdentData = addPostParam(IdentData,"GenusID","");
    }
    if (typeof $("#SpId").val()=="string")
    {
        IdentData = addPostParam(IdentData,"SPID",$("#SpId").val());
    }
    else
    {
        IdentData = addPostParam(IdentData,"SPID","");
    }
    
    IdentData = addPostParam(IdentData,"FName",$id("Ident_FamilyName").value);
    IdentData = addPostParam(IdentData,"CFName",$id("Ident_CFamilyName").value);
    IdentData = addPostParam(IdentData,"GName",$id("Ident_GenusName").value);
    IdentData = addPostParam(IdentData,"CGName",$id("Ident_CGenusName").value);
    
    IdentData = addPostParam(IdentData,"LName",$id("SpLName").value);
    IdentData = addPostParam(IdentData,"CName",$id("SpCName").value);
    IdentData = addPostParam(IdentData,"EName",$id("SpEName").value);
    IdentData = addPostParam(IdentData,"AName",$id("SpAName").value);
    IdentData = addPostParam(IdentData,"TrueName",$id("Signature").value);
    IdentData = addPostParam(IdentData,"Cause",$id("IdentCause").value);
    IdentData = addPostParam(IdentData,"Description",$id("IdentMemo").value);
    
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="/AddIdent.ashx";
    xmlHttp.onreadystatechange = AddIdentReady;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(IdentData);
}
// 取消鉴定
function CancelIdent()
{
    $id("Ident_FamilyName").value = "";
    $id("Ident_CFamilyName").value = "";
    $id("Ident_GenusName").value = "";
    $id("Ident_CGenusName").value = "";
    
    $id("SpLName").value = "";
    $id("SpCName").value ="";
    $id("SpEName").value = "";
    $id("SpAName").value = "";
    $id("IdentCause").value = "";
    $id("IdentMemo").value = "";
    $id("Signature").value = "";
}
function AddIdentReady()
{
    if (xmlHttp.readyState==4)
    { 
        var msg = xmlHttp.responseText;
        //alert(msg);
        var Stat = msg.substring(0,msg.indexOf(":"));
        if (Stat=="Added_Adopted")
        {
            // 本应由Flash来刷新物种记录，但是似乎Flash执行有问题，所以采用 Ajax 多执行一遍；以后即使 Flash 的 Bug 修复了，也互不影响。防御性代码
            // 2010-6-17日发现鉴定有问题了。就是SetPhotoName出了问题，暂时关闭
            //SetPhotoName($id("SPLName").value,$id("SpCName").value); 
            alert("恭喜，"+msg.substring(msg.indexOf(":")+1)+" 照片信息已更改，现在重新加载照片。"); 
            RefreshFlashPhoto();
            return;
        }
        else
        {
            alert(msg.substring(msg.indexOf(":")+1));
        }
    }
}

/// 为照片设定采集单
function SetPhotoCollectionCard(photoid,collid)
{
    //alert(photoid+" 附上采集单 "+collid);
    $.get("/user/SetPhotoCollCard.ashx?photoid="+photoid+"&collid="+collid,
    function(data)
    {
        var result = GetJSONMsgObj(data);
                
        if (result==null)
        {
            alert("发生意外错误！");
        }
        else
        {
            if (result.status=="ok")
            {
                //$("#sel_CollectionCard").hide(); // 关闭采集单选择区域
                RefreshFlashPhoto();
                alert(result.message);
            }
            else
            {
                alert(result.message);
            }
        }
    }
    );
}

function SubmitPhotoInfo()
{
    var PhotoInfoData = "";
    PhotoInfoData = addPostParam(PhotoInfoData,"PhotoID",$id("Photo_ID").innerText);
    PhotoInfoData = addPostParam(PhotoInfoData,"Collector",$id("PhotoInfo_Collector").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"CollSn",$id("PhotoInfo_CollSN").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Country",$id("PhotoInfo_Country").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Province",$id("PhotoInfo_Province").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"City",$id("PhotoInfo_City").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Place",$id("PhotoInfo_Place").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Location",$id("PhotoInfo_Location").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"SpTime",$id("PhotoInfo_SpTime").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Hat",$id("PhotoInfo_Hat").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Zoology",$id("PhotoInfo_Zoology").value);
    PhotoInfoData = addPostParam(PhotoInfoData,"Soil",$id("PhotoInfo_Soil").value);            
    PhotoInfoData = addPostParam(PhotoInfoData,"Memo",$id("PhotoInfo_Memo").value);
    
    //alert(PhotoInfoData)
    
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="/User/UpdatePhotoInfo.ashx";
    xmlHttp.onreadystatechange = UpdatePhotoInfoReady;
    xmlHttp.open("POST",url,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttp.send(PhotoInfoData);
}

function UpdatePhotoInfoReady()
{
    if (xmlHttp.readyState==4)
    { 
        var msg = xmlHttp.responseText;
        var Stat = msg.substring(0,msg.indexOf(":"));
        if (Stat=="UpdatedOK")
        {
            RefreshFlashPhoto();
            alert("恭喜！"+msg.substring(msg.indexOf(":")+1)); 
            return;
        }
        else
        {
            alert(msg.substring(msg.indexOf(":")+1));
        }
    }
}

function RefreshFlashPhoto()
{
    $id("Photo").innerText = "";
    //alert($id("Album_ID").innerText+"===="+$id("Album_Page").innerText+"===="+$id("Photo_ID").innerText)
    //writeFlash($("#Album_ID").text(),$("#Photo_ID").text());
    writeFlashByOrder($("#Album_ID").text(),$("#Photo_ID").text(),$("#Album_Page").text(),currentPhotoOrder);
    ShowPhoto();
}

function BatMove(newFolder,resultMsgContainerId) {
    var photo_list = $("#txtSelectedPhotoList").text();
    $.post("/ajaxserver/AlbumService.ashx?method=move_photos",
            { target: newFolder, PhotoIDList: photo_list },
            function (msg) {
                $("#" + resultMsgContainerId).html(msg
                + "<br /><span style=\"padding:5px; color:#00f; font-weight:bold; cursor:pointer;\" onclick=\"javascript:location.reload(true);\">刷新本页</span>"
                + "&nbsp;&nbsp;&nbsp;&nbsp;<a href='/User/ShowAlbum.aspx?albumid=" + newFolder + "'>"
                + "<span style='padding:5px;color:#00f;font-weight:bold;'>浏览目标相册</span></a>");
            }
            );
        }

function BatDelete()
{
    var photo_list = $("#txtSelectedPhotoList").text();
    $.post("/AjaxServer/Server.ashx?service=photo&method=BatDelete",
            { PhotoIDList: photo_list },
            function(msg)
            {
                $("#BatDeleteResultMsg").html(msg+"<br />1秒钟后自动刷新页面！");
                setTimeout("location.reload(true)", 1000);
            }
            );
}
function BatAddTmpIdent()
        {
            var photo_list = $("#txtSelectedPhotoList").text();
            //var si_genus = $("#tmpGenus").val();
            var si_lname = $("#tmpLName").val();
            var si_cname = $("#tmpCName").val();
            var si_signName = $("#txtTmpIdent_SignName").val();
            var si_desc = $("#tmpIdentMemo").val();
            // alert(si_lname + " " + si_cname + " " + si_signName + " " + si_desc + " " + photo_list);
            $.post("/AddTmpIdent.ashx",
            { PhotoIDList: photo_list, LName: si_lname, CName: si_cname, TrueName: si_signName, Description: si_desc },
            function(msg)
            {
                ShowBatIdentResult("批量鉴定完成，马上将刷新网页！");
                setTimeout("location.reload(true)", 200);
            }
            );
        }
    function BatAddIdent()
    {
        var SpSelect = $("input[name='SelectedSpItem']:checked");
        var si_spid = $(SpSelect).val();

        
        var si_lname = $(SpSelect).attr("lname");
        var si_cname = $(SpSelect).attr("cname");
        var si_level = $(SpSelect).attr("level");
        

        var photo_list = $("#txtSelectedPhotoList").text();
        var cmemo = $("#txtQuickIdent_Memo").val();
        var csignName = $("#txtQuickIdent_SignName").val();

        /*
        alert(si_spid+" "+si_cname+" "+si_lname+" "+si_level);
        alert(cmemo+" "+csignName);
        alert("照片列表： "+photo_list);
        */

        if (typeof si_spid != "string" || si_spid == "")
        {
            alert("没有从物种库中匹配到记录，请重新输入物种名称，再次提交！");
            return;
        }
        //alert("抱歉，批量鉴定出现故障！正在修复当中！");
        // return;
        BatSetPhotoIdentNameInPage(si_lname, si_cname, photo_list);
        AddIdent2PhotoList(si_spid, photo_list, cmemo, csignName);
    }

function BatAddIdentFunc(photo_list,spid, lname, cname, cmemo, identman)
{
    BatSetPhotoIdentNameInPage(lname, cname, photo_list);
    AddIdent2PhotoList(spid, photo_list, cmemo, identman);
}

function BatIdentSimaple()
{
    var spcount = Number($("#QueryResultSpList").attr("spcount"));
    if (spcount > 0)
    {
        BatAddIdent();
        return;
    }

    clearIdentHistory();

    var cLName = $("#txtQuickIdent_LName").val().TrimUnnecessaryBlank();
    var cAName = $("#txtQuickIdent_AName").val().TrimUnnecessaryBlank();

    $.getJSON("/Ajaxserver/server.ashx?service=spdict&method=querybynameauto&format=json", { lname: cLName, aname: cAName },
        function (data)
        {
            if (IsJSONMsg(data))
            {
                $("#QueryResultSpList").attr("spcount", "0");
                ShowJSONMsg(data);
                return;
            }
            else
            {
                // 返回的是 List<SpDictItem>

                var SpCount = data.length;
                if (SpCount < 1)
                {
                    alert("您键入的物种名称不在当前物种库中，如果您确认它是正确的，请先添加到物种库当中！");
                    return;
                }

                $("#QueryResultSpList").attr("spcount", SpCount);

                // 将多个物种记录列出，显示为选项供用户选择
                $("#QueryResultSpList").html("");
                var SpOptionListHtml = "";
                if (SpCount > 1)
                    SpOptionListHtml += "<span style='color:#f00;'>有多个名字匹配，请选择：</span><br />";
                for (var i = 0; i < SpCount; i++)
                {
                    SpOptionListHtml += "<label><input type=\"radio\" name=\"SelectedSpItem\" value=\"";
                    SpOptionListHtml += data[i].Key;
                    SpOptionListHtml += "\" id=\"Opt_SelectedSpItem_" + i + "\" ";
                    SpOptionListHtml += "lname=\"";
                    SpOptionListHtml += data[i].Name_Latin;
                    SpOptionListHtml += "\" ";
                    SpOptionListHtml += "cname=\"";
                    SpOptionListHtml += data[i].Name_Zh;
                    SpOptionListHtml += "\" ";
                    SpOptionListHtml += "level=\"";
                    SpOptionListHtml += data[i].LevelEName;
                    SpOptionListHtml += "\" ";
                    if (i == 0)
                    {
                        SpOptionListHtml += "checked=\"checked\" ";
                    }
                    SpOptionListHtml += " />" + data[i].Label + "</label><br />";
                }
                $("#QueryResultSpList").html(SpOptionListHtml);
                $("#QueryResultSpList_Row").show();


                if (SpCount == 1)
                {
                    var photo_list = $("#txtSelectedPhotoList").text();
                    var cmemo = $("#txtQuickIdent_Memo").val();
                    var identman = $("#txtQuickIdent_SignName").val();

                    BatAddIdentFunc(photo_list, data[0].Key, data[0].Name_Latin, data[0].Name_Zh, cmemo, identman);
                }
            }
        }
    );
}


function BatSetPhotoIdentNameInPage(si_lname,si_cname,photolist)
{
	var PhotoIdArray = photolist.split(",");
	if (PhotoIdArray)
	{
	    $.each(PhotoIdArray, function(i, PId)
	    {
	        $("#spname_la_" + PId).text(si_lname);
	        $("#spname_zh_" + PId).text(si_cname);
	        $("#sel_" + PId).attr("checked", false);
	    });	
	}
}
function SetPhotosSignature(cAlbumId, photolist, type, name)
{
    // type = author | identifier | currentidentifier
    $.post("/AjaxServer/SetPhotosSignature.ashx",
    { albumid: cAlbumId, photos: photolist, signType: type, signName: name },
    function (data) { if (IsJSONMsg(data)) { ShowJSONMsg(data); } });
}


