function getPageSizeWithScroll(){
    if (window.innerHeight && window.scrollMaxY) {
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
        scrollHeight = window.pageYOffset;
        scrollWidth = window.pageXOffset;
    } else {
        yWithScroll = document.documentElement.scrollHeight;
        xWithScroll = document.documentElement.scrollWidth;
        scrollHeight = document.documentElement.scrollTop;
        scrollWidth = document.documentElement.scrollLeft;
    }
    arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll,scrollHeight,scrollWidth);
    return arrayPageSizeWithScroll;
}
/*
else if (document.body.scrollHeight > document.body.offsetHeight){
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    } 
*/
function display_back(){
    if ($('background') != null) {
        return;
    }
    var background = document.createElement('iframe');
    background.id = "background";
    var page_size = getPageSizeWithScroll();
    var wholewidth = page_size[0];
    var wholeheight = page_size[1];
    var screenwidth = screen.width;
    var screenheight = screen.height;
    if (wholewidth < screenwidth) {
        wholewidth = screenwidth;
    }
    if (wholeheight < screenheight) {
        wholeheight = screenheight;
    }
    background.style.width=wholewidth+"px";
    background.style.height=wholeheight+"px";
    background.style.filter="alpha(opacity=40)";
    background.style.opacity = "0.40";
    background.style.position= "absolute";
    background.style.top="0px";
    background.style.background="#777";
    background.style.zIndex="10000";
    background.style.left="0px";
    background.style.display="block";
    document.body.appendChild(background);
    return false;
}
function display_float(html_code, height, width, needback, needfix) {
    if (needback == null || needback == true) {
	   display_back();
	}
	if (needfix == null || needfix == true) {
	   needfix = true;
	}
	else {
	   needfix = false;
	}
    display_layer(html_code, height, width, needfix);
}

function display_float2(html_code, height, width, needback, needfix) {
    if (needback == null || needback == true) {
       display_back();
    }
    if (needfix == null || needfix == true) {
       needfix = true;
    }
    else {
       needfix = false;
    }
    display_layer2(html_code, height, width, needfix);
}

function isIE6() {
    var pattern = /(MSIE 6)+/g;
    if (pattern.test(navigator.appVersion)) {
        return true;
    }
    else {
        return false;
    }
}
function display_layer(html_code, height, width, needfix) {
    if ($('layer') != null) {
        return;
    }
    var layer = document.createElement("div");
    layer.id = "layer";
    var scrollXY = getPageSizeWithScroll();
    var pattern = /(MSIE 6)+/g;
    if (pattern.test(navigator.appVersion)) {
       position = '+position=absolute;';
       var scrolltop = 220 + scrollXY[2];
    }
    else {
       position = '+position=fixed;';
       var scrolltop = 220; 
    }
    if (needfix) {
	    layer.style.cssText = 'margin: 200px auto 0px 40%;' +
	                        '_margin: ' + scrolltop + 'px auto 0px 40%;'+
	                        "background:0 0 no-repeat;"+
	                        "position: fixed;"+
	                         position +
	                        "z-index: 100000;"+
	                        "display: block;";
	}
	else {
        layer.style.cssText = 'margin: ' + scrolltop + 'px auto 0px 40%;' +
                            '_margin: ' + scrolltop + 'px auto 0px 40%;'+
                            "background:0 0 no-repeat;"+
                            "position: absolute;"+
                            "z-index: 100000;"+
                            "display: block;";
	}
    layer.style.width = width + 'px';
    layer.style.height = height + 'px';
    layer.innerHTML = html_code;
    document.body.insertBefore(layer, document.body.childNodes[0]);
}
function display_layer2(html_code, height, width, needfix) {
    if ($('layer2') != null) {
        return;
    }
    var layer = document.createElement("div");
    layer.id = "layer2";
    var scrollXY = getPageSizeWithScroll();
    var pattern = /(MSIE 6)+/g;
    if (pattern.test(navigator.appVersion)) {
       position = '+position=absolute;';
       var scrolltop = 220 + scrollXY[2];
    }
    else {
       position = '+position=fixed;';
       var scrolltop = 220; 
    }
    if (needfix) {
        layer.style.cssText = 'margin: 200px auto 0px 40%;' +
                            '_margin: ' + scrolltop + 'px auto 0px 40%;'+
                            "background:0 0 no-repeat;"+
                            "position: fixed;"+
                             position +
                            "z-index: 200000;"+
                            "display: block;";
    }
    else {
        layer.style.cssText = 'margin: ' + scrolltop + 'px auto 0px 40%;' +
                            '_margin: ' + scrolltop + 'px auto 0px 40%;'+
                            "background:0 0 no-repeat;"+
                            "position: absolute;"+
                            "z-index: 200000;"+
                            "display: block;";
    }
    layer.style.width = width + 'px';
    layer.style.height = height + 'px';
    layer.innerHTML = html_code;
    document.body.insertBefore(layer, document.body.childNodes[0]);
    
    if (isIE6()) {
        fnLoadPngs();
        correctPNG();
    }
}
function clear_background() {
    document.body.removeChild($('background'));
}
function clear_layer() {
    document.body.removeChild($('layer'));
    if ($('layer2'))
    document.body.removeChild($('layer2'));
}
function clear_float() {
    clear_background();
    clear_layer();
}

var current_tangyuan = 0;
function show_message(number) {
    exchangeratetangyuan = $('exchangeratetangyuan' + number).value;
    exchangerategameicon = $('exchangerategameicon' + number).value;
    game_name = $('game_name' + number).value;
    server_id = $('server' + number).value;
    game_id = $('gameid' + number).value;
    game_icon_name = $('game_icon_name' + number).value;
    current_tangyuan = tang_yuan = $('tangyuan' + number).value;
    $('tangyuan' + number).blur();
    pattern = /^[0-9]*$/;
    if (!pattern.test(tang_yuan)) {
        myalert('对不起，您必须输入整数值的汤元数');
        $('tangyuan' + number).focus();
        return false;
    }
    if (tang_yuan <= 0 ) {
        myalert('对不起，您必须输入大于零的汤元数');
        $('tangyuan' + number).focus();
        return false;
    }
    if (tang_yuan > 5000) {
        myalert('对不起，单笔交易额不能高于5000汤元');
        $('tangyuan' + number).focus();
        return false;
    }
    game_icon = tang_yuan * exchangerategameicon / exchangeratetangyuan;
    pay_url = './service/exchange_service.php?serverid=' + server_id + '&gameid=' + game_id + '&tangyuan=' + tang_yuan;
    verify_account_exist(game_id, server_id);
}
function show_pay_check(game_name, game_icon, pay_url, game_icon_name) {
    var html_code = '<div style=" background:url(images/pop.png) no-repeat; width:300px; height:150px;margin:0px auto;"></div>';
    display_float(html_code, '400', '300');
    var html_code = '<div style="width:300px; height:150px;margin:0px auto;">' +
                   '<P style=" text-align:right; padding-top:10px;padding-right:10px;"><img src="images/close.gif" style="cursor: pointer" onclick="clear_float();" width="27" height="23" /></P>' +
                   '<p style="padding:10px;font-size:14px;text-align:center; line-height:20px;">您将为'+ game_name +'<br />' +
                   '兑换<span style="color:#f30">'+ game_icon +'</span>' + game_icon_name + '</p>' +
                   '<P><span class=" button4_1" style=" float:right;margin-right:20px;cursor: pointer" onclick="window.open(\''+ pay_url +'\');clear_float();">确认兑换</span></P></dd>' +
                   '</div>';
    display_float2(html_code, '400', '300');
}
function myalert(message) {
    if (isIE6()) {
        margin = 'margin-right:55px;';
    }
    else {
        margin = 'margin-right:115px;';
    }
    var html_code = '<div style=" background:url(images/pop.png) no-repeat; width:300px; height:150px;margin:0px auto;"></div>';
    display_float(html_code, '400', '300');
    var html_code = '<div style="width:300px; height:150px;margin:0px auto;">' +
                   '<P style=" text-align:right; padding-top:10px;padding-right:10px;"><img src="images/close.gif" style="cursor: pointer" onclick="clear_float();" width="27" height="23" /></P>' +
                   '<p style="padding:10px;font-size:14px;text-align:center; line-height:20px;">'+ message +'</p>' +
                   '<P style="text-align:center; margin-top:16px;"><span class=" button4" style="float:right;' + margin + 'cursor:pointer" onclick="clear_float();">确认</span></P></dd>' +
                   '</div>';
    display_float2(html_code, '400', '300');
}
function confirm(message, confirmmethod) {
    if (isIE6()) {
        margin = 'margin-right:30px;';
    }
    else {
        margin = 'margin-right:65px;';
    }
    var html_code = '<div style=" background:url(images/pop.png) no-repeat; width:300px; height:150px;margin:0px auto;"></div>';
    display_float(html_code, '400', '300');
    var html_code = '<div style="width:300px; height:150px;margin:0px auto;">' +
                   '<P style=" text-align:right; padding-top:10px;padding-right:10px;"><img src="images/close.gif" style="cursor: pointer" onclick="clear_float();" width="27" height="23" /></P>' +
                   '<p style="padding:10px;font-size:14px;text-align:center; line-height:20px;">'+ message +'</p>' +
                   '<P style="text-align:center; margin-top:16px;"><span class="button4" style=" float:right;' + margin + 'cursor:pointer" onclick="clear_float();">取消</span><span class=" button4" style=" float:right;margin-right:20px;cursor: pointer" onclick="'+confirmmethod+';clear_float();">确认</span></P></dd>' +
                   '</div>';
    display_float2(html_code, '400', '300');
}
function verify_tangyuan() {
    var parameters = "operation=checktangyuan";
    var verify_tangyuan_back = function (responseText) {
        tangyuanye = parseInt(responseText);
        if ((tangyuanye <= 0) || (tangyuanye < current_tangyuan)) {
            pay_tangyuan_url = "http://tangyuan.tom.com/redeem/index.php?back_url=" + window.location.href;
            confirm('您的汤元余额已不足，您需要充值吗?','window.open(\''+ pay_tangyuan_url +'\')');
        }
        else {
            show_pay_check(game_name, game_icon, pay_url, game_icon_name);
        }
    }
    var post_data = new PostData(url, parameters, verify_tangyuan_back);
    post_data.send_data();
}
function verify_account_exist(gameid, server_id) {
    var parameters = "operation=useraccountexit&gameid="+game_id+"&serverid="+server_id;
    var verify_account_exist_back = function (responseText) {
        result = parseInt(responseText);
        if (result == 1) {
            verify_tangyuan();
        }
        else if (result == 0) {
            myalert('请在游戏中创建好角色之后再进行充值');
        }
        else if (result == 2) {
            myalert('请先登陆');
        }
        else {
            myalert('由于某种原因，您不能充值');
        }
    }
    var post_data = new PostData(url, parameters, verify_account_exist_back);
    post_data.send_data();
}

function select_server_float(game_id) {
    var current_servers = game_servers[game_id];
    var servers_html_code = '';
    for (i in current_servers) {
        servers_html_code += '<p class="p15"><input type="radio" name="radio" id="radio" value="2000" onclick="destroy_select_server_float(event, \'' + current_servers[i].SERVER_NUM + '\',' + game_id + ');"/>' + current_servers[i].SERVER_NAME + '</P>';
    }
    var html_code = '<div style=" background:url(' +server_host+ 'images/pop4.png) no-repeat; width:300px; height:300px;margin:0px auto;"></div>';
    display_float(html_code, 620, 500, true, true);
    var html_code = '<div style="width:300px; height:300px;margin:0px auto;">' +
                    '<P style=" text-align:right; padding-top:10px;padding-right:10px; border:none;"><img src="'+server_host+'images/close.gif" width="27" height="23" style="cursor: pointer;" onclick="clear_background();clear_layer();"/></P>' +
                    '<DIV style="PADDING-RIGHT:10px;overflow:auto;PADDING-LEFT:10px;SCROLLBAR- FACE-COLOR:#ffffff;PADDING-BOTTOM:0px;SCROLLBAR- HIGHLIGHT-COLOR:#ffffff;OVERFLOW:auto;WIDTH:270px;SCROLLBAR-SHADOW- COLOR:#919192;SCROLLBAR-3DLIGHT-COLOR:#ffffff;LINE- HEIGHT:100%;SCROLLBAR-ARROW-COLOR:#919192;PADDING-TOP:0px;SCROLLBAR- TRACK-COLOR:#ffffff;SCROLLBAR-DARKSHADOW- COLOR:#ffffff;LETTER-SPACING:1pt;HEIGHT:240px;TEXT-ALIGN:left; font-size:14px;">' + 
                    '<p class="p15"><span style="color:#f30; font-weight:bold;padding-left:20px">请选择服务器</span></P>' +
                    servers_html_code +
                    '</DIV>' +
                    '</div>';
    display_float2(html_code, 620, 500, true, true);
}
function destroy_select_server_float(event, server_number, game_id) {
    clear_background();
    clear_layer();
    game_access(game_id, server_number);
}