﻿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 changeGame(f) {
    if(f.server)getServer(f);
    if (f.product) getProduct(f);

}
//改变货币
function changeCurrency(f) {
    if(f.price)getPrice(f.server, f.product, f.currency);
}
//改变服务器
function changeServer(f) {
    if(f.price)getPrice(f.server, f.product, f.currency);
}
//改变产品
function changeProduct(f) {
    if(f.price)getPrice(f.server, f.product, f.currency);
}

//获取产品
function getProduct(f) {
    f.product.options.length = 0;f.price.value = ''; //清空价格
    if (f.game.value != '') {
        f.product.options.add(new Option('Loading....'));
    } else {
        f.product.options.add(new Option('Choose'));
        return;
    }
    xmlhttpPost('/common/ajax.html?productlist', function(q) {
        if (q == "") { return; }
        var s = q.split('@@');
        f.product.options.length = 0;
        for (var i = 0; i < s.length; i++) {
            var ss = s[i].split('@');
            // if (ss.length != 2) continue;
            var pro = new Option(ss[1], ss[0]);
            f.product.options.add(pro);
        }
        f.product.remove(s.length - 1);
       if(f.price) getPrice(f.server, f.product, f.currency);

    }, 'game=' + f.game.value);
}





//获取服务器
function getServer(f) {
    f.server.options.length = 0;
    if (f.game.value != '') {
        f.server.options.add(new Option('Loading....'));
    } else {
        f.server.options.add(new Option('Choose'));
        return;
    }
    xmlhttpPost('/common/ajax.html?serverlist', function(q) {
        if (q == "") {
            return;
        } else {
            f.server.options.length = 0;
            var s = q.split('@@');
            for (var i = 0; i < s.length; i++) {
                var ss = s[i].split('@');
                if (ss.length != 2) continue;
                var ser = new Option(ss[1], ss[0]);
                f.server.options.add(ser);
            }

        }
        if(f.product)getProduct(f);
    }, 'game=' + escape(f.game.value));

}

//获取价格
function getPrice(fserver, fproduct, fcurrency) {
    if (fserver.value == '' && fproduct.value == '' && fcurrency.value == '') {
        return;
    }
    xmlhttpPost('/common/ajax.html?price', function(q) {
        fserver.form.price.value = q;
    }, 'server=' + escape(fserver.value) + '&product=' + escape(fproduct.value) + '&currency=' + escape(fcurrency.value));
}


//获取游戏列表
function getGame(f) {
    f.game.length = 0;
    if (f.game.value == '') {
        f.game.options.add(new Option('Choose', ''));
    }
    xmlhttpPost('/common/ajax.html?gamelist', function(q) {
        if (q == '') { return; }
        var s = q.split('@@');
        for (var i = 0; i < s.length; i++) {
            var ss = s[i].split('@');
            if (ss.length != 2) continue;
            var pro = new Option(ss[1], ss[0]);
            f.game.options.add(pro);
        }

    }, null);
    getCurrency(f); //同时加载货币

}


//获取货币
function getCurrency(f) {
    xmlhttpPost('/common/ajax.html?currencylist', function(q) {
        if (q == '') { return; }
        f.currency.remove(0);
        var s = q.split('@');
        for (var i = 0; i < s.length; i++) {
            var pro = new Option(s[i], s[i]);
            f.currency.options.add(pro);
         
        }
        f.currency.remove(s.length - 1);
        f.currency.value = "USD";
    }, null);

}

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));
}


//end


function goldFormCheck(f, pop) {
	fimsgShow(f.fullname, null);fimsgShow(f.rolename, 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 == ''||f.server.value=='Loading....')) { 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.price && f.price.value == '') { alert('Please try again later.'); 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.rolename && f.rolename.value.trim() == '') { if (pop) fimsgShow(f.rolename, 'Please input your character name .'); else alert('Please input your character name.'); f.rolename.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;} }
	//if (!(f.phone || f.email || f.character || f.fullname)) { f.game.disabled = f.price.disabled = true; }
	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 != f.password.value) { fimsgShow(f.password1, 'Your password is error.'); f.password1.focus(); return false; }
	if (f.rolename && f.rolename.value.trim() == '') { fimsgShow(f.rolename, 'Please input your character name.'); f.rolename.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() == '') { fimsgShow(f.gameserver, 'Please input your 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.rolename && f.rolename.value.trim() == '') { fimsgShow(f.rolename, 'Please input your character name.'); f.rolename.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; }
	
	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, 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);
}
	,'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) { 
    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 siblingPrevious(ele) {
	if (ele == null || typeof ele.previousSibling == 'undefined') return null;
	while (ele.previousSibling != null && ele.previousSibling.nodeType != 1) ele = ele.previousSibling;
	return ele.previousSibling;
}


function itemsbuyFormCheck(f) {
	fimsgShow(f.fullname, null);fimsgShow(f.character,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.character && f.character.value.trim() == '') { fimsgShow(f.character, 'Please input your character name.'); f.character.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;
}

// for payment.ascx  page
function p_check(id) {
    document.getElementById(id).checked = true;

}
