﻿String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
}
function winOpen(url, w, h, n) {
    return window.open(url, (typeof n == 'undefined' ? '' : n), 'height=' + h + ',width=' + w + ',left=' + (window.screen.width - w) / 2 + ',top=' + (window.screen.height - h) / 2 + ',resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,status=no');
}
function xmlhttpCreate() {
    if (window.XMLHttpRequest) return new XMLHttpRequest();
    try { return new ActiveXObject('MSXML2.XMLHTTP.4.0'); } catch (e) { try { return new ActiveXObject('MSXML2.XMLHTTP.3.0'); } catch (e) { try { return new ActiveXObject('MSXML2.XMLHTTP.2.6'); } catch (e) { try { return new ActiveXObject('MSXML2.XMLHTTP'); } catch (e) { try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) { return null; } } } } }
}

function xmlhttpPost(url, callback, content) {
    var xhp = xmlhttpCreate();
    if (xhp == null) return;
    xhp.onreadystatechange = function(e) { if (xhp.readyState != 4 || xhp.status != 200) return; if (typeof callback == 'function') callback(xhp.responseText); xhp.abort(); }
    xhp.open("POST", url, true);
    xhp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    if (content) xhp.send(content);
    else xhp.send('');
}
//***********************
var fgoldgamegandserver;
function serverGet(sltgame, sltserver, sltcode, callback) {
    if (!fgoldgamegandserver) fgoldgamegandserver = sltgame.form
    while (sltserver.options.length > 0) sltserver.remove(0);

    if (sltgame.value != '') sltserver.options.add(new Option('Loading...'));
    else { sltserver.options.add(new Option('Choose')); stockGet(sltserver); return; }
    xmlhttpPost('/common/ajax.html?sys.game.serverlist', function(r) {
        sltserver.remove(0);
        if (r == '') return; var ss = r.split('\r\n');
        for (var i = 0; i < ss.length; i++) {
            var s = ss[i].split('\t'); if (s.length != 2) continue;
            var o = new Option(s[1], s[0]); if (typeof sltcode != 'undefined' && sltcode == s[0]) o.selected = true;
            sltserver.options.add(o);
        }
        var f = sltgame.form;
        if (callback != null && typeof callback == 'function') callback(f);
        if (f.price) priceGet(f.server, f.product, f.currency);
        stockGet(f.server);
    }, 'game=' + escape(sltgame.value));
}

function productGet(sltgame, sltproduct) {
   var g_href = window.location.href;
    var g_amout = g_href.substring(g_href.indexOf("amount") + 7, g_href.length)
    if (!fgoldgamegandserver) fgoldgamegandserver = sltgame.form;
    while (sltproduct.options.length > 0) sltproduct.remove(0);
    if (sltgame.value != '') sltproduct.options.add(new Option('Loading...'));
    else { sltproduct.options.add(new Option('Choose')); return; }
    xmlhttpPost('/common/ajax.html?gold.productlist', function(r) {
        sltproduct.remove(0);
        if (r == '') return; var ss = r.split('\r\n');
        for (var i = 0; i < ss.length; i++) {
            var s = ss[i].split('\t'); if (s.length != 2) continue;
            sltproduct.options.add(new Option(s[1], s[0]));
        }
        for (var j = 0; j < sltproduct.options.length; j++) {
            //alert(sltproduct.options[j].text)
            if (parseFloat(sltproduct.options[j].text) == g_amout) {
                sltproduct.options[j].selected = true;
            }
        }

        var f = sltgame.form;
        priceGet(f.server, f.product, f.currency);
    }, 'game=' + escape(sltgame.value));


}
function priceGet(sltserver, sltproduct, sltcurrency) {
    if (!fgoldgamegandserver) fgoldgamegandserver = sltserver.form;
    sltserver.form.price.value = '';
    if (sltserver.value == '' || sltproduct.value == '' || sltcurrency.value == '') return;
    xmlhttpPost('/common/ajax.html?gold.product.price', function(r) {
        if (r == '') return;
        sltserver.form.price.value = r;
    }, 'server=' + escape(sltserver.value) + '&product=' + sltproduct.value + '&currency=' + escape(sltcurrency.value));
    if (typeof productShow == 'function') productShow(sltserver.value, sltcurrency.value);
}
function productSelect(id) {
    if (!fgoldgamegandserver || !fgoldgamegandserver.product) return;
    if (typeof fgoldgamegandserver.product.options == 'undefined') return;
    for (var i = 0; i < fgoldgamegandserver.product.options.length; i++) if (fgoldgamegandserver.product.options[i].value == id) {
        fgoldgamegandserver.product.selectedIndex = i;
        priceGet(fgoldgamegandserver.server, fgoldgamegandserver.product, fgoldgamegandserver.currency);
        return;
    }
}
function stockGet(sltserver) {
    var dvstock = document.getElementById('dvStock'); if (!dvstock) return;
    var dtr = null;
    try { if (dvstock.parentNode.parentNode.tagName == 'TR') dtr = dvstock.parentNode.parentNode; }
    catch (e) { }
    dvstock.style.display = 'none'; if (dtr != null) dtr.style.display = 'none';
    if (sltserver.value == '') return;
    xmlhttpPost('/common/ajax.html?gold.stock', function(r) {
        if (r == '') return;
        var fr = r.split('\t'); if (fr.length != 2) return;
        dvstock.innerHTML = 'Yesterday we have sent <b style="color:blue;">' + fr[0] + '</b> Gold/Million out on this server.<br/>Currently we have <b style="color:red;">' + fr[1] + '</b> Gold/Million on this server you choose.'
        dvstock.style.display = '';
        if (dtr != null) dtr.style.display = '';
    }, 'server=' + escape(sltserver.value));
}


function goldQuickFormCheck(f) {
    if (!f.game.value) { alert("Please Select Game"); document.getElementById("divGame").style.color = 'red'; return false; }
    if (!f.server.value) { alert("Please Select Server"); return false; }
    if (!f.product.value) { alert("Please Select Product"); return false; }
    return true;
}


function goldFormCheck(f, pop) {
    fimsgShow(f.fullname, null); fimsgShow(f.character, null); fimsgShow(f.email, null); fimsgShow(f.phone, null);
    if (f.game && f.game.value == '') { alert('Please select game.'); f.game.focus(); return false; }
    if (f.server && f.server.value == '') { alert('Please select server.'); f.server.focus(); return false; }
    if (f.product && f.product.value == '') { alert('Please select product.'); f.product.focus(); return false; }
    if (f.fullname && f.fullname.value.trim() == '') { if (pop) fimsgShow(f.fullname, 'Please input your full name.'); else alert('Please input your full name.'); f.fullname.focus(); return false; }
    if (f.character && f.character.value.trim() == '') { if (pop) fimsgShow(f.character, 'Please input your character name .'); else alert('Please input your character name.'); f.character.focus(); return false; }
    if (f.email && f.email.value.trim() == '') { if (pop) fimsgShow(f.email, 'Please input your email.'); else alert('Please input your email.'); f.email.focus(); return false; }
    else if (f.email && f.email.value.trim() != '' && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(f.email.value)) { if (pop) fimsgShow(f.email, 'Your email is error.'); else alert('Your email is error.'); f.email.value = ''; f.email.focus(); return false; }
    if (f.phone && f.phone.value.trim() == '') { if (pop) fimsgShow(f.phone, 'Please input your phone number.'); else alert('Please input your phone number.'); f.phone.focus(); return false; }
    if (f.server && f.server.selectedIndex == 0) { if (!confirm("Are you sure your server is   " + f.server.options[0].text + ', please?')) { f.server.focus(); return false; } }
    return true;
}
function plFormCheck(f) {
    fimsgShow(f.fullname, null); fimsgShow(f.email, null); fimsgShow(f.phone, null); fimsgShow(f.account, null); fimsgShow(f.password, null); fimsgShow(f.password1, null); fimsgShow(f.character, null); fimsgShow(f.classname, null); fimsgShow(f.gameserver, null);
    if (f.fullname && f.fullname.value.trim() == '') { fimsgShow(f.fullname, 'Please input your full name.'); f.fullname.focus(); return false; }
    if (f.email && f.email.value.trim() == '') { fimsgShow(f.email, 'Please input your email.'); f.email.focus(); return false; }
    else if (f.email.value.trim() != '' && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(f.email.value)) { fimsgShow(f.email, 'Your email is error.'); f.email.value = ''; f.email.focus(); return false; }
    if (f.phone && f.phone.value.trim() == '') { fimsgShow(f.phone, 'Please input your phone number.'); f.phone.focus(); return false; }
    if (f.account && f.account.value.trim() == '') { fimsgShow(f.account, 'Please input your game account.'); f.account.focus(); return false; }
    if (f.password && f.password.value.trim() == '') { fimsgShow(f.password, 'Please input your game password.'); f.password.focus(); return false; }
    if (f.password1 && f.password1.value.trim() == '') { fimsgShow(f.password1, 'Please input your again game password.'); f.password1.focus(); return false; }
    if (f.password1 && f.password1.value != f.password.value) { fimsgShow(f.password1, 'Your password is error.'); f.password1.focus(); return false; }
    if (f.character && f.character.value.trim() == '') { fimsgShow(f.character, 'Please input your character name.'); f.character.focus(); return false; }
    if (f.classname && f.classname.value.trim() == '') { fimsgShow(f.classname, 'Please input your character class.'); f.classname.focus(); return false; }
    if (f.gameserver && f.gameserver.value.trim() == '') { alert('Please select game server.'); f.gameserver.focus(); return false; }
    return true;
}
function accountbuyFormCheck(f) {
    fimsgShow(f.fullname, null); fimsgShow(f.email, null); fimsgShow(f.phone, null);
    if (f.fullname && f.fullname.value.trim() == '') { fimsgShow(f.fullname, 'Please input your full name.'); f.fullname.focus(); return false; }
    if (f.email && f.email.value.trim() == '') { fimsgShow(f.email, 'Please input your email.'); f.email.focus(); return false; }
    else if (f.email.value.trim() != '' && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(f.email.value)) { fimsgShow(f.email, 'Your email is error.'); f.email.value = ''; f.email.focus(); return false; }
    if (f.phone && f.phone.value.trim() == '') { fimsgShow(f.phone, 'Please input your phone number.'); f.phone.focus(); return false; }
    return true;
}
function stuffbuyFormCheck(f) {
    fimsgShow(f.fullname, null); fimsgShow(f.email, null); fimsgShow(f.phone, null);
    if (f.fullname && f.fullname.value.trim() == '') { fimsgShow(f.fullname, 'Please input your full name.'); f.fullname.focus(); return false; }
    if (f.email && f.email.value.trim() == '') { fimsgShow(f.email, 'Please input your email.'); f.email.focus(); return false; }
    else if (f.email.value.trim() != '' && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(f.email.value)) { fimsgShow(f.email, 'Your email is error.'); f.email.value = ''; f.email.focus(); return false; }
    if (f.phone && f.phone.value.trim() == '') { fimsgShow(f.phone, 'Please input your phone number.'); f.phone.focus(); return false; }
    if (f.game && f.game.value == '') { alert('Please select game.'); f.game.focus(); return false; }
    if (f.server && f.server.value == '') { alert('Please select server.'); f.server.focus(); return false; }
    if (f.character && f.character.value.trim() == '') { fimsgShow(f.character, 'Please input your character name.'); f.character.focus(); return false; }
    return true;
}
function accountsellFormCheck(f) {
    fimsgShow(f.fullname, null); fimsgShow(f.email, null); fimsgShow(f.phone, null); fimsgShow(f.server, null); fimsgShow(f.level, null); fimsgShow(f.gclass, null); fimsgShow(f.faction, null); fimsgShow(f.gender, null); fimsgShow(f.race, null); fimsgShow(f.link, null);
    if (f.fullname && f.fullname.value.trim() == '') { fimsgShow(f.fullname, 'Please input your full name.'); f.fullname.focus(); return false; }
    if (f.email && f.email.value.trim() == '') { fimsgShow(f.email, 'Please input your email.'); f.email.focus(); return false; }
    else if (f.email.value.trim() != '' && !/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(f.email.value)) { fimsgShow(f.email, 'Your email is error.'); f.email.value = ''; f.email.focus(); return false; }
    if (f.phone && f.phone.value.trim() == '') { fimsgShow(f.phone, 'Please input your phone number.'); f.phone.focus(); return false; }
    if (f.server && f.server.value.trim() == '') { fimsgShow(f.server, 'Please select your server.'); f.server.focus(); return false; }
    if (f.level && f.level.value.trim() == '') { fimsgShow(f.level, 'Please input your game level.'); f.level.focus(); return false; }
    else if (f.level.value.trim() != '' && !/^[1-9]\d*$/g.test(f.level.value)) { fimsgShow(f.level, 'Your enter error.'); f.level.value = ''; f.level.focus(); return false; }
    if (f.gclass && f.gclass.value.trim() == '') { fimsgShow(f.gclass, 'Please select your game class.'); f.gclass.focus(); return false; }
    if (f.faction && f.faction.value.trim() == '') { fimsgShow(f.faction, 'Please select your game faction.'); f.faction.focus(); return false; }
    if (f.gender && f.gender.value.trim() == '') { fimsgShow(f.gender, 'Please select your game gender.'); f.gender.focus(); return false; }
    if (f.race && f.race.value.trim() == '') { fimsgShow(f.race, 'Please select your game race.'); f.race.focus(); return false; }
    if (f.link && f.link.value.trim() == '') { fimsgShow(f.link, 'Please enter your CTmods or armory link .'); f.link.focus(); return false; }
    return true;
}

//function couponCheck(ipt) {
//    fimsgShow(ipt, null);
//    if (ipt == null || ipt.value == '') return;

//    xmlhttpPost('/common/ajax.html?gold.coupon.check', function(r) {
//        if (r == '' || r == 'err') fimsgShow(ipt, "This discount code is invalid!");
//        else fimsgShow1(ipt, r);
//    }
//	, 'g=' + escape(ipt.form.gameservercode.value) + '&c=' + escape(ipt.value));
//}


function couponCheck(ipt, gc) {
    fimsgShow(ipt, null);
    if (ipt.value == '') return;
    if (gc == null) {
        if (ipt.form.game && ipt.form.game.value == '') { alert('Please select game before.'); ipt.form.game.focus(); return; }
        else gc = ipt.form.game.value;
    }
    xmlhttpPost('/common/ajax.html?gold.coupon.check', function(r) {
        if (r == '' || r == 'err') fimsgShow(ipt, "This discount code is invalid!");
        else fimsgShow1(ipt, r);
    }
	, 'g=' + escape(gc) + '&c=' + escape(ipt.value));
}


//function couponCheck(ipt,sltgame) {
//     fimsgShow(ipt, null);
//    if (ipt == null || ipt.value == '') return;
//    xmlhttpPost('/common/ajax.html?gold.coupon.check', function(r) {
//        if (r == '' || r == 'err') fimsgShow(ipt, "This discount code is invalid!");
//        else fimsgShow1(ipt, r);
//    }
//	, 'gamecode=' + sltgame + '&coupcode=' + escape(ipt.value));
//}
function plcouponCheck(ipt, gc) {
    fimsgShow(ipt, null);
    if (ipt.value == '') return;
    if (gc == null) {
        if (ipt.form.game && ipt.form.game.value == '') { alert('Please select game before.'); ipt.form.game.focus(); return; }
        else { gc = ipt.form.game.value; }
    }
    xmlhttpPost('/common/ajax.html?pl.coupon.check', function(r) {
        if (r == '' || r == 'err') fimsgShow(ipt, "This discount code is invalid!");
        else fimsgShow1(ipt, r);
    }
	, 'gc=' + escape(gc) + '&c=' + escape(ipt.value));
}



function fimsgShow(ele, msg) {
    if (!ele) return;
    var inputid = ele.getAttribute('name') == '' ? ele.getAttribute('id') : ele.getAttribute('name');
    var divMsg = document.getElementById(inputid + '__$msg$');
    if (divMsg) divMsg.parentNode.removeChild(divMsg);
    if (msg == null) return;
    divMsg = document.createElement('SPAN');
    divMsg.setAttribute('id', inputid + '__$msg$');
    divMsg.setAttribute('style', 'display:inline-block;');
    divMsg.innerHTML = '<span style="background-color:#FFFF00;margin-left:4px; border:1px solid #FF2200; padding:1px 2px 1px 2px; color:#FF0000; z-index:10000;">' + msg + '</span>';
    ele.parentNode.insertBefore(divMsg, ele.nextSibling);
}
function fimsgShow1(ele, msg) {
    if (!ele) return;
    var inputid = ele.getAttribute('name') == '' ? ele.getAttribute('id') : ele.getAttribute('name');
    var divMsg = document.getElementById(inputid + '__$msg$');
    if (divMsg) divMsg.parentNode.removeChild(divMsg);
    if (msg == null) return;
    divMsg = document.createElement('SPAN');
    divMsg.setAttribute('id', inputid + '__$msg$');
    divMsg.setAttribute('style', 'display:inline-block;');
    divMsg.innerHTML = '<span style="margin-left:4px; padding:1px 2px 1px 2px; color:green; z-index:10000;">' + msg + '</span>';
    ele.parentNode.insertBefore(divMsg, ele.nextSibling);
}

function priceChange(fid, c, price) {
    currencycolorchange(c);
    document.getElementById("currency").value=c;
    var f = document.getElementById(fid);
    if (f && f.currency) { f.currency.value = c; }
    xmlhttpPost('/common/ajax.html?cart.currency', function(r) {
        if (r != null) { document.getElementById("p").value = r; }
    }, 'currency=' + escape(c) + '&price=' + escape(price) + '&formid=' + escape(fid));
}



function checkLength(tname) {
    var len = tname.value;
    if (len.length > 150) {
        alert("please input less 150");
        return false;
    }
    return true;
}

function currencycolorchange(c) {
    var cur = document.getElementsByName("curname");
    for (var i = 0; i < cur.length; i++) {
        if (cur[i].id == "cur" + c) {
            cur[i].style.color = "red";
            cur[i].style.fontWeight = "700";

        } else {
            cur[i].style.color = "";
            cur[i].style.fontWeight = "100";
        }
    }
}


function clearText(field) {
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

function p_check(id) {
    document.getElementById(id).checked = true;

}




