﻿// JScript 文件
var game = 1;
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);
    if(game == 15)
        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.ReadEUWow(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 (gameId == 1 || gameId == 15)
        {
            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 (gameid == 1 || gameid == 15)
    {
        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('您的操作有误！');        
    })
}
