﻿// JScript 文件
var game = 1;
var hMode = 1;     //1--自动挂单，0--手动挂单
var isPFL = 1;     //1--分首字母显示，0--不分首字母
var ServerType = 'A';

function init()
{
    ajaxMethod.gid(function(result){
        game = result.value;
        gameState();
        rightInit();
        document.getElementById(ddlGL).value = game;
    });
}
window.onload = init;
//返回游戏状态
function gameState()
{
    var spGameState = document.getElementById("spGameState");
    ajaxMethod.ReadState(game, function(result){
        spGameState.innerHTML = result.value;
    });
    serverList(game, ServerType, "");
}
//搜索服务器
function searchSever()
{
    var kw = document.getElementById("txtKeyWord");
    var kwv = kw.value.replace("'", "%27");
    ServerType = 'all';
    if(kw == null) return;
    else
    {
        serverList(game, "all", kwv);
    }
}
//设置游戏状态，游戏选择变化触发事件
function setGameState()
{
    game = parseInt(document.getElementById(ddlGL).value);
    var isInitDispAll = 1;
    
    ajaxMethod.GetHangMode(game, function(result){
        var ar = result.value;
        if (ar != null)
        {
            hMode = ar[0];
            isPFL = ar[1];
            isInitDispAll = ar[2];
        }
        
        if(isInitDispAll == 1)
        ServerType = 'all';
        else    
            ServerType = 'A';
            
        gameState();
    });
}
//设置服务器状态，服务区首字母选择变化触发事件
function setServerType(type)
{
    ServerType = type;
    gameState();
}
//设置服务器首字母的着色
function setColor()
{
    var ac = document.getElementById("divType").getElementsByTagName("a");
    for(i = 0; i < ac.length; i++)
    {
        ac[i].style.color = '#000';
    }
    document.getElementById("a_" + ServerType).style.color = '#FF0000';
}
//把所有服务器首字母设置为黑色
function setNoColor()
{
    var ac = document.getElementById("divType").getElementsByTagName("a");
    for(i = 0; i < ac.length; i++)
    {
        ac[i].style.color = '#000';
    }
}
//返回服务器列表
var neednum = 0;
function serverList(gameId, type, keyWord)
{
    var spGameState = document.getElementById("divServerList");
    spGameState.innerHTML = "数据加载中...";
    var list = "";
    keyWord = keyWord.replace("%27", "'");
    var showOnline = document.getElementById("hidShow").value;
    ajaxMethod.ReadServerNeedList(gameId, type, showOnline, keyWord, function(result){
        var tb = result.value;
        if (tb != null)
        {
            if(tb.Rows.length == 0)
            {
                spGameState.innerHTML = "";
            }
            else
            {
                if (gameId == 1 || gameId == 15)
                    list = list1(gameId, tb);   //区分国家、阵营的显示样式
                else
                    list = list2(gameId, tb);   //不区分国家、阵营的显示样式
            }
        }    
        if (hMode == 1 && isPFL == 1)
        {
            spGameState.innerHTML = addServerType(keyWord);  //头字母列表、服务区搜索显示样式
            if(keyWord != '')
                setNoColor();                                //有输入服务区搜索关键字的，把所有服务器首字母设置为黑色
            else    
                setColor();
        }
        else
            spGameState.innerHTML = "";    
        spGameState.innerHTML += list;
    });
}
function list1(gameId, tb)
{
    var r = 0;
    var list = "<table cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:12px; width:100%; border: #DED6C8 1px solid;\">";
    list += "<colgroup>";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "</colgroup>";
    
    list += "<tr style='background-color:#BCBEC0;height:25px;'>";
    list += "<td style=\"font-weight:bold;\">国家</td>";
    list += "<td style=\"font-weight:bold;\">游戏服务器</td>";
    list += "<td style=\"font-weight:bold;\">阵营</td>";
    list += "<td style=\"font-weight:bold;\">价格</td>";
    list += "<td style=\"font-weight:bold;\">需求量</td>";
    list += "<td style=\"font-weight:bold;\">下单</td>";
    list += "</tr>";
    
    for(var i = 0; i < parseInt(tb.Rows.length); i++)
    {
        neednum = tb.Rows[i].NeedNum;
        var state = returnState(gameId, tb.Rows[i].ServerID, tb.Rows[i].AccountTypes, tb.Rows[i].MinNum);
        r = (i + 1) % 2;
        if(r != 0)
        {
            list += "<tr style='height:25px;'>";
            list += "<td>" + tb.Rows[i].ServerCountryCn + "</td>";
            list += "<td>" + tb.Rows[i].ServerName + "</td>";
            list += "<td>" + tb.Rows[i].GameTypeName + "</td>";
            list += "<td>" + tb.Rows[i].UnitPrice.toFixed(4) + "元/" + tb.Rows[i].GameUnit + "</td>";
            list += "<td>" + neednum + "</td>";
            list += "<td>" + state + "</td>";
            list += "</tr>";
        }
        else
        {
            list += "<tr style='background-color:#E6E9EB;height:25px;'>";
            list += "<td>" + tb.Rows[i].ServerCountryCn + "</td>";
            list += "<td>" + tb.Rows[i].ServerName + "</td>";
            list += "<td>" + tb.Rows[i].GameTypeName + "</td>";
            list += "<td>" + tb.Rows[i].UnitPrice.toFixed(4) + "元/"+tb.Rows[i].GameUnit + "</td>";
            list += "<td>" + neednum + "</td>";
            list += "<td>" + state + "</td>";
            list += "</tr>";
        }
    }
    list += "</table>";
    return list;
}
function list2(gameId, tb)
{
    var r = 0;
    var list = "<table cellpadding=\"0\" cellspacing=\"0\" style=\"font-size:12px; width:100%; border: #DED6C8 1px solid;\">";
    list += "<colgroup>";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "<col align=\"center\" />";
    list += "</colgroup>";
    
    list += "<tr style='background-color:#BCBEC0;height:25px;'>";
    list += "<td style=\"font-weight:bold;\">游戏服务器</td>";
    list += "<td style=\"font-weight:bold;\">价格</td>";
    list += "<td style=\"font-weight:bold;\">需求量</td>";
    list += "<td style=\"font-weight:bold;\">下单</td>";
    list += "</tr>";
    
    for(var i = 0; i < parseInt(tb.Rows.length); i++)
    {
        neednum = tb.Rows[i].NeedNum;
        var state = returnState(gameId, tb.Rows[i].ServerID, tb.Rows[i].AccountTypes, tb.Rows[i].MinNum);
        r = (i + 1) % 2;
        if(r != 0)
        {
            list += "<tr style='height:25px;'>";
            list += "<td>" + tb.Rows[i].ServerName + "</td>";
            list += "<td>" + tb.Rows[i].UnitPrice.toFixed(4) + "元/" + tb.Rows[i].GameUnit + "</td>";
            list += "<td>" + neednum + "</td>";
            list += "<td>" + state + "</td>";
            list += "</tr>";
        }
        else
        {
            list += "<tr style='background-color:#E6E9EB;height:25px;'>";
            list += "<td>" + tb.Rows[i].ServerName + "</td>";
            list += "<td>" + tb.Rows[i].UnitPrice.toFixed(4) + "元/" + tb.Rows[i].GameUnit + "</td>";
            list += "<td>" + neednum + "</td>";
            list += "<td>" + state + "</td>";
            list += "</tr>";
        }    
    }
    list += "</table>";
    return list;
}
//添加服务
function addServerType(keyWord)
{
    var list = "<div id='divType' style=\"text-align:left; border-bottom: #DED6C8 1px solid; line-height:22px; padding:3px;\">";
    list += "<a id='a_A' href=\"javascript:setServerType('A');\">A</a> | <a id='a_B' href=\"javascript:setServerType('B');\">";
    list += "B</a> | <a id='a_C' href=\"javascript:setServerType('C');\">C</a> | <a id='a_D' href=\"javascript:setServerType('D');\">";
    list += "D</a> | <a id='a_E' href=\"javascript:setServerType('E');\">E</a> | <a id='a_F' href=\"javascript:setServerType('F');\">";
    list += "F</a> | <a id='a_G' href=\"javascript:setServerType('G');\">G</a> | <a id='a_H' href=\"javascript:setServerType('H');\">";
    list += "H</a> | <a id='a_I' href=\"javascript:setServerType('I');\">I</a> | <a id='a_J' href=\"javascript:setServerType('J');\">";
    list += "J</a> | <a id='a_K' href=\"javascript:setServerType('K');\">K</a>";
    list += "| <a id='a_L' href=\"javascript:setServerType('L');\">L</a> | <a id='a_M' href=\"javascript:setServerType('M');\">";
    list += "M</a> | <a id='a_N' href=\"javascript:setServerType('N');\">N</a> | <a id='a_O' href=\"javascript:setServerType('O');\">";
    list += "O</a> | <a id='a_P' href=\"javascript:setServerType('P');\">P</a> | <a id='a_Q' href=\"javascript:setServerType('Q');\">";
    list += "Q</a> | <a id='a_R' href=\"javascript:setServerType('R');\">R</a> | <a id='a_S' href=\"javascript:setServerType('S');\">";
    list += "S</a> | <a id='a_T' href=\"javascript:setServerType('T');\">T</a> | <a id='a_U' href=\"javascript:setServerType('U');\">";
    list += "U</a> | <a id='a_V' href=\"javascript:setServerType('V');\">V</a>";
    list += "| <a id='a_W' href=\"javascript:setServerType('W');\">W</a> | <a id='a_X' href=\"javascript:setServerType('X');\">";
    list += "X</a> | <a id='a_Y' href=\"javascript:setServerType('Y');\">Y</a> | <a id='a_Z' href=\"javascript:setServerType('Z');\">";
    list += "Z</a> | <a id='a_all' href=\"javascript:setServerType('all');\">全部服务器</a>";
    list += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    list += "<input id='txtKeyWord' type='text' style='width:100px;' maxlength='30' value=\""+keyWord.replace("%27","'")+"\" onkeydown=\"SubmitKeyClick(event,'btnSearch');\" />&nbsp;<img id='btnSearch' src='skin/go.gif' alt='搜索' style='cursor:pointer;' onclick='searchSever();' />";
    list += "</div>";
    
    return list;
}
//返回下单状态
function returnState(gameid,ServerId,AccountTypes,minNum)
{
    //if(neednum>20000) neednum=20000;
    if (hMode == 1)  //自动挂单模式
    {
        if (AccountTypes == 0)
        {
            return "<a href='javascript:linkOrder(" + ServerId + "," + AccountTypes + ",0);'><span style='color:#008000;cursor:pointer;'>UC35在线</span></a>";
        }
        else if (AccountTypes == 1)
        {
            return "<a href='javascript:linkOrder(" + ServerId + "," + AccountTypes + ",0);'><span style='color:#FF0000;cursor:pointer;'>老外在线</span></a>";
        }
        else
        { onclick = 'linkOrder("+ServerId+","+AccountTypes+",0);'
            return "<a href='javascript:linkOrder(" + ServerId + "," + AccountTypes + ",0);'><span style='color:#0000FF;cursor:pointer;'>高价订单</span></a>";
        }
    }
    else
    {
        if(neednum >= minNum)
        {
            if(neednum > 0)
            {
                if (AccountTypes == 2)
                    return "<a href='javascript:linkOrder(" + ServerId + "," + AccountTypes + ",1);'><span style='color:#0000FF;cursor:pointer;'>高价订单</span></a>";
                else
                    return "<a href='javascript:linkOrder(" + ServerId + "," + AccountTypes + ",1);'><span style='color:#FF0000;cursor:pointer;'>下单</span></a>";
            }
            else
                return "<span style='color:#FF0000;'>满仓</span>";
        }
        else
            return "<span style='color:#FF0000;'>满仓</span>";
    }
}
function linkOrder(ServerId, AccountTypes, flg)
{
    ajaxMethod.checkRole(function(result){
        if(result.value == "noLogin")
            alert('您还未登录请先登录！');
        else if(result.value=="false")
            alert('您还未开通权限，请联系客服！');
        else if(result.value == "true")
        {
            if(flg == 0)
                window.location.href = 'ForeignOrderList2.aspx?ServerId=' + ServerId + '&AccountTypes=' + AccountTypes;
            else
                window.location.href = 'UserOrder.aspx?ServerID=' + ServerId;
        }
        else
            alert('您的操作有误！');        
    })
}

