﻿var paytype = "paypal";

var price;
var moneytype;
var accountid;
var gamename;
var servername;
var iniprice;
var itemname;
var coupon = "";
var orderserial;
var realm;

var ppemail;
var ipn;
var ppimg;
var rate;

//get pp
function getemail()
{
    var url = "ashx/getppinfo.ashx";
        
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "get",
                                        onComplete: getppdo
                                    }
                                );
}

function getppdo(xmlhttp)
{
    var result = xmlhttp.responseText;
    
    var arr = result.split("|");
    
    if(arr.length == 3)
    {
        ppemail = arr[0];
        ipn = arr[1];
        ppimg = arr[2];
    }
    else
    {
        window.location = "index.html";
    }
}
        
function showaccountinfo()
{
    getemail();
    
    var cookievalue = getCookie("accountinfo");
    
    if(cookievalue == null)
    {
        window.location = "index.html";
    }
    cookievalue = cookievalue.replace("||","–");
    
    var arr = cookievalue.split("@|@");
    
    if(arr.length != 7)
    {
        window.location = "index.html";
    }
    
    price = ws2(arr[0]);
    moneytype = arr[1];
    accountid = arr[2];
    gamename = arr[3];
    servername = arr[4];
    iniprice = arr[5];
    realm = arr[6];
    
    itemname = getCookie("Remark");
    itemname = itemname.replace("||","–");
    
    changemoney(moneytype);
    if(servername != null)
        servername = servername.replace("@","\'");
    
    $("accinfocell1").innerHTML = gamename + " - " + servername;
    $("accinfocell2").innerHTML = itemname;
    $("accinfocell3").innerHTML = price + " " + moneytype;
}

//set paytype
function getpaytype()
{
    if($("paypal").checked)
    {
        paytype = "paypal";
    }
    if($("card").checked)
    {
        paytype = "card";
    }
    if($("moneyBookers").checked) paytype = "moneybookers";
    
    if($("westernunion").checked) paytype = "westernunion";
}

//goto paypal
function gopaypal()
{
    getpaytype();
    var fullname = $F("iname").strip();
    var email = $F("iemail").strip();
    var telephone = $F("itelephone").strip();
    var remark = $F("iremark").strip();
    var kefu = $F("ikefu").strip();
    
    if(fullname == "" || email == "" || telephone == "")
    {
        alert("Please fill all *");
        return;
    }
    
    if(!checkemail(email))
    {
        alert("Please check email!");
        return;
    }
    
    orderserial = getserialaccount();
    var ogame = gamename;
    if(gamename == "WOW") ogame = "World Of Warcraft US";
    if(gamename == "WOWEUR") ogame = "World Of Warcraft EU";

    if(gamename.indexOf("World Of Warcraft EU") != -1 || gamename.indexOf("WOWEUR") != -1) gamename = "EUWOW";
    else gamename = "WOW";

    var xmlStr = "<userinputs>";
    
    xmlStr += "<accountid>" + accountid + "</accountid>";
    xmlStr += "<orderserial>" + orderserial + "</orderserial>";
    xmlStr += "<gamename>" + gamename + "</gamename>";
    xmlStr += "<game>" + ogame + "</game>";
    xmlStr += "<servername>" + servername + "</servername>";
    xmlStr += "<price>" + price + "</price>";
    xmlStr += "<moneytype>" + getmoneytype(moneytype) + "</moneytype>";
    xmlStr += "<fullname>" + fullname + "</fullname>";
    xmlStr += "<email>" + email + "</email>";
    xmlStr += "<telephone>" + telephone + "</telephone>";
    xmlStr += "<coupon>" + coupon + "</coupon>";
    xmlStr += "<kefu>" + kefu + "</kefu>";
    xmlStr += "<paytype>" + paytype + "</paytype>";
    xmlStr += "<uid>" + getuid() + "</uid>";
    xmlStr += "<iniprice>" + iniprice + "</iniprice>";
    xmlStr += "<remark>" + remark + "</remark>";
    xmlStr += "<itemname>" + itemname + "</itemname>";
    xmlStr += "<realm>" + realm + "</realm>";
    xmlStr += "</userinputs>";
    
    var url="ashx/accountbuyinput.ashx";
    
    var myAjax = new Ajax.Request(   
                                    url,   
                                    {
                                        method: "post", 
                                        postBody: xmlStr, 
                                        onComplete: gopaypalgo
                                    }   
                                );
}

function gopaypalgo(xmlhttp)
{
    var result = xmlhttp.responseText;
//    alert(result);
    if(result == "ok")
    {
        if(paytype == "westernunion")
        {
            ignsubmitwest();
            return;
        }
        
        if(paytype == "moneybookers")
        {
            ignsubmitmoneybooker();
            return;
        }
        
        createFormAndSubmit(orderserial, price, getmoneytype(moneytype));
    }
    else
    {
        alert("data error!");
    }
}

function ignsubmitwest()
{   
    var ah = realm;
    if(realm == "A") ah = "-Alliance";
    else ah = "-Horde";
    
    var submitForm = getNewSubmitForm("WestenUnion");
    createNewFormElement(submitForm, "ordercode", orderserial);
    createNewFormElement(submitForm, "gamename", gamename);
    createNewFormElement(submitForm, "ppemail", ppemail);
    createNewFormElement(submitForm, "servername",servername + ah);
    createNewFormElement(submitForm, "ccharactername", " ");
    createNewFormElement(submitForm, "goldnum","ACCOUNT" + ' * ' + 1);
    createNewFormElement(submitForm, "price",price);
    createNewFormElement(submitForm, "moneytype",getmoneytype(moneytype));
    submitForm.action= "paymoney3.aspx";
    submitForm.submit();
}

//moneybookers
function ignsubmitmoneybooker()
{                               
    var submitForm = getNewSubmitForm("MoneyBookers");
    createNewFormElement(submitForm, "pay_to_email", ppemail);
    createNewFormElement(submitForm, "amount", price);;
    createNewFormElement(submitForm, "currency",getmoneytype(moneytype));
    createNewFormElement(submitForm, "detail1_description",orderserial);
    createNewFormElement(submitForm, "detail1_text","ACCOUNT");
    submitForm.action= "paymoney2.aspx";
    submitForm.submit();
}


//check coupon 
function checkcoupon()
{
    coupon = $F("icoupon").strip();
    
    if(coupon == "")
    {
        alert("Please fill coupon!");
        return;
    }
    var game = gamename;
    if(gamename == "WOW") game = "World Of Warcraft US";
    if(gamename == "WOWEUR") game = "World Of Warcraft EU";
    
    var url = "ashx/coupon.ashx?igncoupon=" + coupon + "&game=" + game + "&gametype=ZH"; 
    
    var myAjax = new Ajax.Request(
                                    url,
                                    {
                                        method: "get",
                                        onComplete: couponpdo
                                    }
                                ); 
}

function couponpdo(xmlhttp)
{
    var result = xmlhttp.responseText;
    
    if(result == "-1")
    {
        alert("Coupon is not useful,please check it!");
        coupon = "";
        $("icoupon").value = "";
        return;
    }

    var carr = result.split("|");
    
    var limitmoney = carr[0];
    var givetype = carr[1];
    var givenum = ws2(carr[2]);
    
    if(parseFloat(iniprice) < parseFloat(limitmoney))
    {
        alert("The price is lower!");
        $("icoupon").value = "";
        coupon = "";
        return;
    }
    
    if(carr.length != 4)
    {
        alert("Coupon is error!");
        $("coupontb").value = "";
        coupon = "";
        return;
    }
    
    var cutmoney;
    //cut off money num
    if(givetype == "1")
    {
        price = ws2((iniprice - givenum) * rate);
        cutmoney = ws2(givenum * rate);
        $("cutmoneydiv").innerHTML = "- " + cutmoney + " " + moneytype;
    }
    //discount
    if(givetype == "2")
    {   
        price = ws2((iniprice * (100 - givenum)/100.0) * rate);
        cutmoney = ws2(givenum * iniprice * rate/100.0);
        $("cutmoneydiv").innerHTML = "- " + cutmoney + " " + moneytype;
    }
    
    $("accinfocell3").innerHTML = price + " " + moneytype;
    $("icoupon").readOnly = "true";
}

//change money type
function changemoney(fuhao)
{
    var imoneytype;
        
    imoneytype = getmoneytype(fuhao);

    var url = "ashx/moneytype.ashx?moneytype=" + imoneytype;

    var myAjax = new Ajax.Request
                (   
                    url,   
                    {
                        method: "get",
                        onComplete: getrateDo
                    }   
                );  
}

//get money rate 
function getrateDo(xmlhttp)
{
    rate = Number(xmlhttp.responseText);
    
    if(isNaN(rate))
    {
        return;
    }
     
   return Number(rate);
}

