﻿//获得全部数据
function GetAllCdKeyInfo()
{    
    var url = "ashx/BuyCdKey.ashx";
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "get",
                                        onComplete: ShowInfo
                                    }
                                );
}

function gameTypeChange()
{
    var gameType = $F('CdKeyGamelist');

    var url = "ashx/GetCdKeyInfoByGameType.ashx?type=" + gameType;
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "get",
                                        onComplete: ShowInfo
                                    }
                                );
}

function GetAllGameList()
{
    var url = "ashx/cdKeyGetAllGameType.ashx";
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "get",
                                        onComplete: ShowGameList
                                    }
                                );
}

function getBeautify()
{
    return getMoneyType();
}

//获得全部游戏列表
function ShowGameList(xmlhttp)
{
    if(xmlhttp.responseText != "<NewDataSet />")
    {
        var xml = xmlhttp.responseXML;
        var pricelist = xml.getElementsByTagName("Table");
        for(var i = 0; i < pricelist.length; i++)
        {
            option = document.createElement("option");
            option.text = pricelist[i].getElementsByTagName("gamename")[0].childNodes[0].nodeValue;
            option.value = pricelist[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
            $("CdKeyGamelist").options.add(option);
            
        }
    }
}

//获得全部数据
function ShowInfo(xmlhttp)
{
    var xml = xmlhttp.responseXML;

    var div = $('DataList');
     var content = '';
    
    if(xmlhttp.responseText != "<NewDataSet />")
    {
    
        var beautify = getBeautify();
        
        var pricelist = xml.getElementsByTagName("Table");
        for(var i = 0; i < pricelist.length; i++)
        {
            var Tid = pricelist[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
            var Ttitle = pricelist[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
            var TimagePath = pricelist[i].getElementsByTagName("imagePath")[0].childNodes[0].nodeValue;
            var Tbewrite = pricelist[i].getElementsByTagName("bewrite")[0].childNodes[0].nodeValue;
            var Tprice = pricelist[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
            var Tabbreviate = pricelist[i].getElementsByTagName("abbreviate")[0].childNodes[0].nodeValue;
            var Tgamename = pricelist[i].getElementsByTagName("gamename")[0].childNodes[0].nodeValue;      
            
            
            content += "<table bgcolor=\"#b36d23\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"ml3 mt5\"";
            content += "width=\"747\">";
            content += "<tr>";
            content += "<td width=\"5\">";
            content += "<img height=\"6\" src=\"images/cdkey_03.jpg\" width=\"5\" /></td>";
            content += "<td width=\"736\">";
            content += "</td>";
            content += "<td valign=\"top\">";
            content += "<img height=\"5\" src=\"images/cdkey_05.jpg\" width=\"6\" /></td>";
            content += "</tr>";
            content += "<tr>";
            content += "<td>";
            content += "</td>";
            content += "<td>";
            content += "<table bgcolor=\"#734929\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"736\">";
            content += "<tr>";
            content += "<td rowspan=\"5\" width=\"127\">";
            content += "<input type=\"hidden\" id=\"oprice" + i + "\" value=\""  + Tprice + "\" />";
            content += "<input type=\"hidden\" id=\"gamename" + i + "\" value=\""  + Tgamename + "\" />";
            content += "<input type=\"hidden\" id=\"id" + i + "\" value=\""  + Tid + "\" />";
            content += "<input type=\"hidden\" id=\"abbreviate" + i + "\" value=\"" + Tabbreviate + "\" />"
            content += "<img id=\"imgPath" + i + "\" alt=\"" + TimagePath + "\" height=\"121\" src=\"" + TimagePath.substring(2,TimagePath.length) + "\" width=\"127\" /></td>";
            content += "<td width=\"10\">";
            content += "<img height=\"8\" src=\"images/cdkey_10.jpg\" width=\"10\" /></td>";
            content += "<td valign=\"top\" width=\"437\">";
            content += "<img height=\"1\" src=\"images/cdkey_11.jpg\" width=\"437\" /></td>";
            content += "<td width=\"10\">";
            content += "<img height=\"8\" src=\"images/cdkey_13.jpg\" width=\"10\" /></td>";
            content += "<td rowspan=\"5\" width=\"1\">";
            content += "<img height=\"121\" src=\"images/cdkey_10-06.jpg\" width=\"1\" /></td>";
            content += "<td width=\"10\">";
            content += "<img height=\"8\" src=\"images/cdkey_10.jpg\" width=\"10\" /></td>";
            content += "<td width=\"130\">";
            content += "</td>";
            content += "<td width=\"10\">";
            content += "<img height=\"8\" src=\"images/cdkey_13.jpg\" width=\"10\" /></td>";
            content += "<td rowspan=\"5\" width=\"1\">";
            content += "<img height=\"121\" src=\"images/cdkey_10-06.jpg\" width=\"1\" /></td>";
            content += "</tr>";
            content += "<tr>";
            content += "<td height=\"35\">";
            content += "&nbsp;</td>";
            content += "<td class=\"ffv fwb cdkc1 fs14 \" id=\"title" + i + "\">";
            content += Ttitle + "</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "<td class=\"ffv fwb cdkc1 fs14 \">";
            content += "Price:" + beautify + "<span id=\"price" + i + "\" class=\"ffv fwb cdkc1 fs14\">" + changeRate(Tprice) + "</span></td>";
            content += "<td>";
            content += "</td>";
            content += "</tr>";
            content += "<tr>";
            content += "<td height=\"35\">";
            content += "&nbsp;</td>";
            content += "<td class=\"fontc_white ffv fs12\" rowspan=\"2\" id=\"bewrite" + i + "\" valign=\"top\">";
            content += Tbewrite + "</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "<td>";
            content += "<div class=\"fl\">";
            content += "<div class=\"fontc_white fwb fl fs14 ffa\">";
            content += "Amount</div>";
            content += "<div class=\"fl pl5 pt3\">";
            content += "<img onclick=\"allayValue('input" + i +"');\" height=\"12\" src=\"images/cdkey_23-11.jpg\" width=\"12\" /></div>";
            content += "<div class=\"fl pl5\">";
            content += "<input class=\"cdkinput\" name=\"textfield4\" style=\"text-align:center;\" id=\"input" + i +"\" type=\"text\" readonly=\"readOnly\" value=\"1\" />";
            content += "</div>";
            content += "<div class=\"fl pl5 pt3\">";
            content += "<img height=\"13\" onclick=\"addValue('input" + i +"');\" src=\"images/cdkey_23.jpg\" width=\"12\" />";
            content += "</div>";
            content += "</div>";
            content += "</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "</tr>";
            content += "<tr>";
            content += "<td height=\"35\">";
            content += "&nbsp;</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "<td>";
            content += "&nbsp;</td>";
            content += "<td>";
           /// content += "<img height=\"20\" onclick=\"saveShopInfo('id" + i + "','title" + i + "','price" + i + "','abbreviate" + i + "','input" + i +"','bewrite" + i + "','gamename" + i + "','oprice" + i + "');\" src=\"images/cdkey_31.jpg\" width=\"88\" /></td>";
               content += "<input class='qobutton hand' onclick=\"saveShopInfo('id" + i + "','title" + i + "','price" + i + "','abbreviate" + i + "','input" + i +"','bewrite" + i + "','gamename" + i + "','oprice" + i + "');\"  type='button'  value='Check Out' /></td>";
               
            content += "<td>";
            content += "&nbsp;</td>";
            content += "</tr>";
            content += "<tr>";
            content += "<td height=\"8\">";
            content += "<img height=\"8\" src=\"images/cdkey_34.jpg\" width=\"10\" /></td>";
            content += "<td>";
            content += "</td>";
            content += "<td>";
            content += "<img height=\"8\" src=\"images/cdkey_15.jpg\" width=\"10\" /></td>";
            content += "<td>";
            content += "<img height=\"8\" src=\"images/cdkey_34.jpg\" width=\"10\" /></td>";
            content += "<td>";
            content += "</td>";
            content += "<td>";
            content += "<img height=\"8\" src=\"images/cdkey_15.jpg\" width=\"10\"  /></td>";
            content += "</tr>";
            content += "</table>";
            content += "</td>";
            content += "<td>";
            content += "</td>";
            content += "</tr>";
            content += "<tr>";
            content += "<td>";
            content += "<img height=\"6\" src=\"images/cdkey_38.jpg\" width=\"5\" /></td>";
            content += "<td>";
            content += "</td>";
            content += "<td align=\"right\">";
            content += "<img height=\"6\" src=\"images/cdkey_31-18.jpg\" width=\"5\" /></td>";
            content += "</tr>";
            content += "</table>";
        }
    }
    div.innerHTML = content;
}
//减少选购数量
function allayValue(inputID)
{
    var text = $(inputID);
    var Tvalue = Number(text.value);
    if(Tvalue > 1)
        Tvalue = Tvalue - 1;
    text.value = Tvalue;        
}
//增加选够数量
function addValue(inputID)
{
    var text = $(inputID);
    var Tvalue = Number(text.value);
    if(Tvalue < 10)
        Tvalue = Tvalue + 1;
    text.value = Tvalue; 
}

var id;
var title;
var price;
var newPrice;
var abbreviate;
var num = 1;
var bewrite;
var gamename;
var amount;
var oldprice;

//将选中的CdKey信息显示到列表
function saveShopInfo(Vid,Vtitle,Vprice,Vabbreviate,Vnumber,Vbewrite,Vgamename,Voprice)
{    
    id = $(Vid).value;
    title = $(Vtitle).innerHTML;
    price = Number($(Voprice).value);
    newPrice = Number($(Vprice).innerHTML);
    abbreviate = $(Vabbreviate).value;
    num = Number($(Vnumber).value);
    bewrite = $(Vbewrite).innerText;
    gamename = $(Vgamename).value;
    $('tt1').style.display = 'none';
    $('selectTr1').style.display = 'none';
    $('selectTr2').style.display = 'none';
    $('DataList').style.display = 'none';
    $('tb2').style.display = '';
    
    
    amount = ws2(eval(newPrice * num));
    oldprice= ws2(eval(price * num));;
    
    $("tr1").title = bewrite;
    $("TItem").innerHTML = abbreviate;
    $("TPrice").innerHTML = getBeautify() + newPrice;
    $("TQuantity").innerHTML = num;    
    $("TAmount").innerHTML = getBeautify() + amount;
    
    $("row31").innerHTML = getBeautify() + newPrice;
    $("row32").innerHTML = num;
    $("row33").innerHTML = getBeautify() +  amount;
}
//删除被选中项
function deleteitem()
{
    id = '';
    title = '';
    price = 0;
    abbreviate = '';
    num = 1;
    bewrite = '';
    gamename = '';
    amount = 0;
    $('tt1').style.display = '';
    $('selectTr1').style.display = '';
    $('selectTr2').style.display = '';
    $('DataList').style.display = '';
    $('tb2').style.display = 'none';
    $('table3').style.display = '';
    $('table4').style.display = 'none';
    
    $("coupontr").style.display = "none";
    $("coupontr").value = '';
    $("couponrow1").style.display = "";
    $("couponrow2").style.display = "none";
    
    ischeck = 0;
}

var payment;
var kefu;
function newFillbuy()
{
    var res = /^[0-9a-zA-Z_\-\.]+@[0-9a-zA-Z_\-]+(\.[0-9a-zA-Z_\-]+)*$/;  
    var re = new RegExp(res);
    if(!re.test($F("emailtb").strip()))
    {
        alert("Please check Email");
        return;
    } 
    
    fullname = $F("fullnametb").strip();
    email = $F("emailtb").strip();
    
    us1 = $F("usphone1").strip();
    us2 = $F("usphone2").strip();
    us3 = $F("usphone3").strip();
    
    nousphone = $F("nonus").strip();
    
  
    
    if($("rad1").checked == true)
    {
        kefu = "";
    }    
    else if($("rad2").checked == true)
    {
        if($("rad2").selectedIndex == 0)
        {
            alert("Please select customer rep!");
            return;
        }
        else
        {
            kefu = $F("repddl");
        }
    }
    
    kefu += $("reptb").value;
    
    if(kefu.length == 0)
    {
        kefu=" ";
    }
    
    if(fullname == "" || email == "")
    {
        alert("Please fill all need!");
        return;
    }
    
    if(us1 + us2 + us3 == "" && nousphone == "")
    {
        alert("Please fill telephone!");
        return;
    }
    
    var us = us1 + us2 + us3;
    
    if(us.length > 0 && us.length <10)
    {
        alert("Please check usphone!");
        return;
    }
    
    usphone = us1 + "-" + us2 + "-" + us3;
    
    newcheckcouponp();  //check coupon
    
    coupon = $F("coupontb").strip();
    
    $("powertd2").innerHTML = $("powertd1").innerHTML;
    getpp();
    
    $("c1").innerHTML = fullname;
    $("c2").innerHTML = email;
    if(us == "")
    {
        $("c3").innerHTML = nousphone;
    }
    else
    {
        $("c3").innerHTML = usphone;
    }
    
    $("c4").innerHTML = kefu;
    
    if($("pay_type_1").checked)
    {
        payment = "paypal";
    }
    else if($("pay_type_2").checked)
    {
        payment = "card";
    }
    else if($("pay_type_3").checked)
    {
        payment = "moneybookers";
    }
    else if($("pay_type_4").checked)
    {
        payment = "westernunion";
    }
    
    $("pay").innerHTML=payment;
    $("coupontb2").innerHTML=coupon;
    $('selectTr1').style.display = 'none';
    $('selectTr2').style.display = 'none';
    $('DataList').style.display = 'none';
    $('table3').style.display = 'none';
    $('table4').style.display = '';
    
}
//生成订单编号
function newGetserial()
{
    var md = new Date();
    var year = md.getYear();
    year = year.toString();
    year = year.substring(2,4);
    
    if(year.length == 1)
    {
        year = "0" + year;
    }
    
    var month = md.getMonth() + 1;
    if(month < 10)
    {
        month = "0" + month;
    }
    
    var day = md.getDate();
    if(day < 10)
    {
        day = "0" + day;
    }
    
    var s = "0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
    var arr = s.split(" ");
     var sn4 = "";
    while(sn4.length <=3)
    {
        sn4 += arr[Math.round(Math.random()*100)%36];
    }       
    return  year + month + day + sn4 + "-CDK";
}


var sn;
//将订单保存到数据库
function newPowertopaypal()
{
    needgold = 0;
    sn = newGetserial();
    if(usphone == "--")
    {
        usphone = "";
    }
    
    var xmlStr = "<userinputs>";    
    xmlStr += "<gamename>" + escape(gamename) + "</gamename>";
    xmlStr += "<servername> </servername>";
    xmlStr += "<charactername> </charactername>";
    xmlStr += "<fullname>" + fullname + "</fullname>";
    xmlStr += "<usphone>" + escape(usphone) + "</usphone>";
    xmlStr += "<mobile>" + escape(nousphone) + "</mobile>";
    xmlStr += "<money>" + escape(amount) + "</money>";
    xmlStr += "<moneytype>" + escape(getmoneytype2()) + "</moneytype>";
    xmlStr += "<oldmoney>" + escape(ws2(price * num)) + "</oldmoney>";
    xmlStr += "<coupon>" + escape(coupon) + "</coupon>";
    xmlStr += "<email>" + escape(email) + "</email>";
    xmlStr += "<staffname>" + escape(kefu) + "</staffname>";
    xmlStr += "<uid>" + escape(getuid()) + "</uid>"; 
    xmlStr += "<password> </password>";
    xmlStr += "<account> </account>";
    xmlStr += "<sn>" + escape(sn) + "</sn>";
    xmlStr += "<itemame>" + escape(abbreviate + ' * ' + num) + "</itemame>";
    xmlStr += "<input>" + escape($F("userinputtb")) + "</input>";
    xmlStr += "<needgold>" + escape(needgold) + "</needgold>";
    xmlStr += "<charclass> </charclass>";
    xmlStr += "<usetime> </usetime>";
    xmlStr += "<number>" + num + "</number>";
    xmlStr += "<payment>" + payment + "</payment>";
    xmlStr += "</userinputs>";
    
    var url="ashx/CdKeyByInput.ashx";
    
    $("imgSubmit").style.display="none"; 
    $("loading").style.display=""; 
    try
    {
        var myAjax = new Ajax.Request(   
                                        url,   
                                        {
                                            method: "post", 
                                            postBody: xmlStr, 
                                            onComplete: newPowertopaypaldo
                                        }   
                                    ); 
   }
   catch(err)
   {
        alert(err.description);
   }
}
//获得插入数据库结果
function newPowertopaypaldo(xmlhttp)
{
    var result = xmlhttp.responseText;
    
    if(result=="ok")
    {
        if(payment == "westernunion")
        {
            submitwest();
            return;
        }
        
        if(payment == "moneybookers")
        {
            
            submitmoneybooker();
            return;
        }
        createFormAndSubmit(sn, amount, getmoneytype2());
    }
    else
    {
        alert('Result:'  + result);
    }
}



//westen union
function submitwest()
{
    var submitForm = getNewSubmitForm("WestenUnion");
    createNewFormElement(submitForm, "ordercode", sn);
    createNewFormElement(submitForm, "gamename", gamename);
    createNewFormElement(submitForm, "servername", "");
    createNewFormElement(submitForm, "ccharactername", "");
    createNewFormElement(submitForm, "goldnum",abbreviate + ' * ' + num);
    createNewFormElement(submitForm, "price",price);
    createNewFormElement(submitForm, "moneytype",getmoneytype2());
    submitForm.action= "paymoney3.aspx";
    submitForm.submit();
}
//moneybookers
function submitmoneybooker()
{
     var submitForm = getNewSubmitForm("MoneyBookers");
    createNewFormElement(submitForm, "pay_to_email", ppemail);
    createNewFormElement(submitForm, "amount", amount);
    createNewFormElement(submitForm, "currency",getmoneytype2());
    createNewFormElement(submitForm, "detail1_description",sn);
    createNewFormElement(submitForm, "detail1_text","CDK");
    submitForm.action= "paymoney2.aspx";
    submitForm.submit();
}

//判断是否重复优惠
var ischeck =0;
//查询优惠券
function newcheckcouponp()
{
    if(ischeck == 0)
    {
        coupon = $F("coupontb").strip().toUpperCase();  
        
        if(coupon.length == 0)
        {
             ischeck =0;
            return;
        }
        if ( coupon.length > 12 || coupon.length < 3 ) 
        {
            alert("Coupon is not useful!Please check coupon!");
            $("coupontb").value = "";
            ischeck =0;
            return;
        }
        
        var url = "ashx/coupon.ashx?igncoupon=" + coupon +  "&game=" + gamename + "&gametype=CD&email=WWESED";
        
        var myAjax = new Ajax.Request(
                                        url,
                                        {
                                            method: "get",
                                            onComplete: newcouponpdo
                                        }
                                    );
   
    }
}
//获得优惠券信息
function newcouponpdo(xmlhttp)
{
   if(ischeck == 0)
    {
        var result = xmlhttp.responseText;
        
        if(result == "-1")
        {
            ischeck =0;
            alert("Coupon is not useful,please check it!");
            coupon = "";
            $("coupontb").value = "";
            return;
        }
        if(result == "-2")
        {
            ischeck =0;
            alert("This coupon code has expired, plz contact with your customer rep!");
            coupon = "";
            $("coupontb").value = "";
            return;
        }
        var carr = result.split("|");
        
        if(carr[0] != "0")
        {
            ischeck =0;
            alert("Coupon is only use for Gold!");
            return;
        }
        if(parseFloat(price) < parseFloat(carr[1]))
        {
             ischeck =0;
            alert("The price is lower!");
            $("coupontb").value = "";
            coupon = "";
            return;
        }
        
        if(carr.length != 6)
        {
            ischeck =0;
            alert("Coupon is error!");
            $("coupontb").value = "";
            coupon = "";
            return;
        }
        
        if(carr[2] == "3" || carr[2] == "4")
        {
            ischeck =0;
            alert("Coupon is only use for Gold!");
            $("coupontb").value = "";
            coupon = "";
            return;
        }
        

        if(carr[2] == "1")
        {
            ischeck = 1;
            $("row21").innerHTML = "- " + getBeautify() + ' ' + changeRate2(parseFloat(carr[3]));
            $("row23").innerHTML = "- " + getBeautify() + ' ' + changeRate2(parseFloat(carr[3]));
          
             amount = ws2(changeRate(oldprice) - changeRate2(parseFloat(carr[3])));
            $("row31").innerHTML = getBeautify() + amount;
            $("row33").innerHTML = getBeautify() + amount;
            $("row32").innerHTML = Number($('TQuantity').innerHTML) +1;
        }
        
        if(carr[2] == "2")
        {
            ischeck = 1;
            $("row21").innerHTML = "- " + carr[3] + '%';
            $("row23").innerHTML = "- " + getBeautify() + ws2(changeRate(parseFloat(price)*parseFloat(carr[3])/100.0) * num);
            
            amount = ws2(changeRate(parseFloat(price)  - parseFloat(price)*parseFloat(carr[3])/100.0) * num);
           
            $("row31").innerHTML = getBeautify() + amount;
            $("row33").innerHTML = getBeautify() + amount;
            $("row32").innerHTML = Number($('TQuantity').innerHTML) +1;
        }
            
        $("coupontr").style.display = "";
        
        $("couponrow1").style.display = "none";
        $("couponrow2").style.display = "";
        
        
        alert("coupon is ok.");
   }
}