﻿var $j = jQuery.noConflict();
$j(document).ready(function() {
    loadCheckJs();
    document.getElementById ('zxCheckBox').checked = false;
    if ($j('#googleConfig').val() != "0") {
	    $j('#div_couponCode').hide();
    }
    // load gold
    //initGoldQuickOrder();
    getCustomerManager($j('#email').val());
});

function initGoldQuickOrder() {
//    $j('#ddlGame').initGoldQuickOrder({
//        domServer: document.getElementById('ddlServer'),
//        domServerKey: document.getElementById('ddlServerKey'),
//        domProduct: document.getElementById('ddlProduct'),
//        domPrice: document.getElementById('txtPrice'),
//        domWait: document.getElementById('goldImgWait')
//    });
    
    if($j('#ddlGame').val()=="019" || $j('#ddlGame').val()=="087")
	{
		$j('#a_EUR')=getMoneyType('EUR');
	}
	else
	{
		//$j('#a_USD').getMoneyType('USD');
	}

    $j('#ddlGame')=GameServerChanger({
        url: '/Ajax.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'operate=GetServer&gameCode=' + $j('#ddlGame').val()
    });

    $j('#ddlGame').change(function() {
        if($j(this).val()=="010")
        {
            window.location.href='/Runescape2/';
        }
        else
        {

			if($j('#ddlGame').val()=="019" || $j('#ddlGame').val()=="087")
			{
				$j('#a_EUR').getMoneyType('EUR');
			}
			else
			{
				$j('#a_USD').getMoneyType('USD');
			}
            changeDescription($j(this).val());
            $j(this)=GameServerChanger({
                url: '/Ajax.aspx?rd=' + Math.random(),
                type: 'get',
                data: 'operate=GetServer&gameCode=' + $j(this).val()
            });
        }
    });

    $j('#ddlServerKey').change(function() {
        $j(this).changeServerKey();
    });

    $j('#ddlServer').change(function() {
        var ddlServer = document.getElementById('ddlServer');
        var serverName = ddlServer.options[ddlServer.selectedIndex].text;
        $j(this).getProduct({
            url: '/Default.aspx?rd=' + Math.random(),
            type: 'get',
            data: 'operate=GetProduct&serverCode=' + $j(this).val() + '&serverName=' + serverName
        });
    });

    $j('#ddlProduct').change(function() {
        $j(this).getPrice();
    })
}

function DIVShow(i) {
    if (document.getElementById('zxCheckBox').checked) {
        document.getElementById(i).style.display = 'block';
    } else {
        document.getElementById(i).style.display = 'none';
    }
}
function DIVzyShow(i)
{
    if(document .getElementById ('ckbxZY').checked)
        {document .getElementById (i).style.display='block';}
    else 
        {document .getElementById (i).style.display='none';}
}


var lastAmount = 0;
var lastMoneyType = 'USD';

function GetCustomerGoldPrice(amount) {

    var gameCode = document.getElementById('ddlGame').value;
    if (!document.getElementById('zxCheckBox').checked) {
        return;
    }
    try {
        if (amount == '') {
            return;
        }
        if (gameCode == "013" || gameCode == "019") {
            if (parseFloat(amount) < 500) {
                document.getElementById('txtAmount').value = '500';
                amount = 500;
            }
        }
        if (gameCode == "083" || gameCode == "087") {
            if (parseFloat(amount) < 0.1) {
                document.getElementById('txtAmount').value = '0.1';
                amount = 0.1;
            }
        }
    } catch(e) {
        return;
    }
    var gameServerCode = document.getElementById('ddlServer').value;
    var moneyType = document.getElementById('sp_moneyType2').innerHTML;
    if (gameServerCode == '-1') {
        alert('Please choose your server');
        return;
    }
    if (amount == lastAmount && moneyType == lastMoneyType) {
        return;
    }
    lastAmount = amount;
    lastMoneyType = moneyType;

    $j('#div_wait_gold2').show();
    document.getElementById('txtPrice2').value = '0';
    $j.ajax({
        url: '/Ajax.aspx?rd=' + Math.random(),
        type: 'get',
        data: 'operate=GetCustomerGoldPrice&amount=' + amount + '&gameServerCode=' + gameServerCode + '&moneyType=' + moneyType,
        dataType: 'html',
        success: function(data) {
            $j('#div_wait_gold2').hide();
            document.getElementById('txtPrice2').value = data;
        }
    });
}

var isIE = navigator.userAgent.indexOf("MSIE") > 0;
function checkCustomerEnter(e) {
    var keyNum = 0;
    if (isIE) {
        keyNum = e.keyCode;
    } else {
        keyNum = e.which;
    }
    var amount = document.getElementById('txtAmount').value;
    for (var i = 0; i < amount.length; i++) {
        var keyCode = amount.charCodeAt(i);
        if (keyCode < 48 || keyCode > 57) {
            var gameCode = document.getElementById('ddlGame').value;
            if (gameCode != '013' && gameCode != '019') {
                if (amount.charAt(i) != '.') {
                    amount = amount.replace(amount.charAt(i), '');
                }
            } else {
                amount = amount.replace(amount.charAt(i), '');
            }
        }
    }
    document.getElementById('txtAmount').value = amount;
      if(document.getElementById('txtAmount').value=="")
    {
        document.getElementById('txtPrice2').value='0';
    }
}

function canSubmitForm() {
    if (!document.getElementById('zxCheckBox').checked) {
        document.getElementById('txtAmount').value = '0';
    }
    if ($j('#ddlGame').val() == '-1') {
        alert('Please choose your game');
        return false;
    }
    if ($j('#ddlServer').val() == '-1') {
        alert('Please choose your server');
        return false;
    }
    if (document.getElementById('zxCheckBox').checked) {
        if (isNaN($j('#txtAmount').val())) {
            $j('#txtAmount').val('0');
        }
        if (parseFloat($j('#txtAmount').val()) <= 0) {
            alert('Please choose your product');
            return false;
        }
    }
    if (!document.getElementById('zxCheckBox').checked && $j('#ddlProduct').val() == '-1') {
        alert('Please choose your product');
        return false;
    }
    return checkForm();
}

var lastEmail = '';
function getCustomerManager(email) {
    if (email == lastEmail) {
        return;
    }
    lastEmail = email;
    var regEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if (isEmpty(email) && regEmail.test(email) && email.length <= 50) {
        $j('.khjl').fadeIn(300);
        $j('#img_loadCustomer').show();
        $j.ajax({
            url: '/gold/GoldProduct.aspx?rd=' + Math.random(),
            type: 'get',
            data: 'operate=GetCustomerManager&email=' + email,
            dataType: 'json',
            success: function(data) {
                $j('#img_loadCustomer').hide();
                if (data == null) {
                    return;
                }
               // $j('.khjl > h4').html(data.name);

                var html = "";
                html += "<tr>";
                if (data.msn != "") {
                    html += "<td align=\"center\"><div type='Msn' class=\"pl51\" style=\"display: none;  margin-left:26px; margin-top:20px;\" >" + data.msn + "</div><a href=\"http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=9f0b233d411b655f@apps.messenger.live.com&mkt=en-US\"><img src=\"/images/msn.gif\" /></a></td>";
                }
                if (data.yahoo != "") {
                    html += "<td align=\"center\"><div type='Yahoo' class=\"pl52\" style=\"display: none;  margin-left:26px; margin-top:20px;\">" + data.yahoo + "</div><a href=\"javascript:\" onclick=\"javascript:window.open(\'ymsgr:sendIM?" + data.yahoo + "')\"><img src=\"/images/yahoo.gif\" /></a></td>";
                }
                if (data.gmail != "") {
                    html += "<td align=\"center\"><div type='GTalk' class=\"pl53\" style=\"display: none;  margin-left:26px; margin-top:20px;\">" + data.gmail + "</div><a href=\"http://www.google.com/talk/service/badge/Start?tk=z01q6amlqj73tfq9ui89f2va1i5p9rgud64qh86e5oa6dabr6i3djvk7easv428h6ap5tsraf1uvteb16u7gdhgc0nt890t2a78be8uhoonib4o7ko7d8ded51ap7c6409vfr3bme1vr3d41933gg72s192q0perg23k5bjt0d533lrangvrchukg10hc2trkd8\" target=_blank><img src=\"/images/Gmail.gif\" /></a></td>";
                }
                if (data.aim != "") {
                    html += "<td align=\"center\"><div type='Aim' class=\"pl54\" style=\"display: none; margin-left:26px; margin-top:20px;\">" + data.aim + "</div><a href=\"javascript:\" onclick=\"javascript:window.open('mailto:" + data.aim + "')\"><img src=\"/images/aim.gif\" /></a></td>";
                }
                html += "</tr><tr>";
                if (data.msn != "") {
//                    html += "<td align=\"center\"><a href=\"javascript:\" onclick=\"javascript:window.open('msnim:chat?contact=" + data.msn + "')\">Msn</a></td>";
                    html+="<td align=\"center\"><a target=\"_blank\" href=\"http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=9f0b233d411b655f@apps.messenger.live.com&mkt=en-US\">Msn</a></td>";
                    }
                if (data.yahoo != "") {
                   html += "<td align=\"center\"><a href=\"javascript:\" onclick=\"javascript:window.open(\'ymsgr:sendIM?" + data.yahoo + "')\">Yahoo</a></td>";

                    //html += "<td align=\"center\"><a target=\"_blank\" href=\"http://edit.yahoo.com/config/send_webmesg?.target=" + data.yahoo + "&.src=pg\">Yahoo</a></td>";
                }
                if (data.gmail != "") {
                   html += "<td align=\"center\"><a href=\"http://www.google.com/talk/service/badge/Start?tk=z01q6amlqj73tfq9ui89f2va1i5p9rgud64qh86e5oa6dabr6i3djvk7easv428h6ap5tsraf1uvteb16u7gdhgc0nt890t2a78be8uhoonib4o7ko7d8ded51ap7c6409vfr3bme1vr3d41933gg72s192q0perg23k5bjt0d533lrangvrchukg10hc2trkd8\" target=_blank>gtalk</a></td>";
                   //html += "<td align=\"center\"><a href=\"javascript:\" onclick=\"javascript:window.open('gtalk:chat?jid=" + data.gmail + "')\">GTalk</a></td>";
                }
                if (data.aim != "") {
                    html += "<td align=\"center\"><a href=\"javascript:\" onclick=\"javascript:window.open('mailto:" + data.aim + "')\">Aim</a></td>";
                }
                html += "</tr>";
                $j('#tbl_customer').html(html);
                $j('#tbl_customer>tbody>tr>td').mouseover(function() {
                    if ($j(this).find('div').html() != null) {
                        $j(this).find('div').show();
                    } else {
                        var type = $j(this).find('a').html();
                        $j('div[type=' + type + ']').show();
                    }
                });
                $j('#tbl_customer>tbody>tr>td').mouseout(function() {
                    if ($j(this).find('div').html() != null) {
                        $j(this).find('div').hide();
                    } else {
                        var type = $j(this).find('a').html();
                        $j('div[type=' + type + ']').hide();
                    }
                });
            }
        });
    } else {
        $j('.khjl').fadeOut(150);
    }
}
