function doRegisterSubmit(pForm) {
	var temp = false;
	$E('register_submit').disbled = true;
	var req = new GForm(pForm);
	req.inintLoading('wait', true);
	req.submit(function(xhr){
		$E('register_submit').disbled = false;
		var datas = req.toJSON( xhr.responseText );
		for (var prop in datas[0] ) {
			val = datas[0][prop];
			if (prop == 'alert') {
				alert(val);
			} else if (prop == 'location') {
				window.location = val;
            } else if (prop == 'usericon') {
                $E( prop ).src = val;
			} else {
				temp = !temp && val != '' ? prop : temp;
				$E( prop ).innerHTML = val;
			};
		};
		if (temp) {
			window.scrollTo(0, $G(temp).getTop());
		};
	});
	return true;
};

function doAvatarSel(img) {
	var id = img.alt;
	$E('register_iconid').value = id;
	var imgs = $E('avatar').getElementsByTagName('img');
	for( var i = 0 ; i < imgs.length ; i++ ) {
		imgs[i].className = (imgs[i].alt == id) ? 'selicon' : '';
	};
};

function doLogin(pForm) {
	var req = new GAjax();
	req.inintLoading('wait', true);
	req.send( web_url + 'member/chklogin.php' , req.getRequestBody( pForm ) , function(xhr){
		var datas = req.toJSON(xhr.responseText);
		if (datas[0].error != '')
		{
			alert(datas[0].error);
			document.getElementById(datas[0].input).focus();
			new GHighlight(datas[0].input).play();
		}
		else
		{
			var el = $G('login').elem;
			el.innerHTML = decodeURIComponent(datas[0].form);
			loader.inint(el);
		};
	});
	return false;
};

function doLogout() {
	var req = new GAjax();
	req.inintLoading('wait', true);
	req.send( web_url + 'member/chklogout.php' , 'action=logout' , function(xhr){
		var el = $G('login').elem;
		el.innerHTML = decodeURIComponent(xhr.responseText);
		loader.inint(el);
	});
};

function doForgot(pForm) {
	var req = new GAjax();
	req.inintLoading('wait', true);
	req.send( web_url + 'member/action.php' , req.getRequestBody( pForm ) , function(xhr){
		var datas = req.toJSON(xhr.responseText);
		if (datas[0].error != '')
		{
			alert(datas[0].error);
			if (datas[0].input && document.getElementById(datas[0].input)) {
				document.getElementById(datas[0].input).focus();
				new GHighlight(datas[0].input).play();
			};
		};
	});
};

function doMemberAction(obj) {
	var patt = /([a-z]+)-([0-9]+)/;
	var ids = patt.exec(obj.id);
	var query = 'action=' + ids[1] + '&id=' + ids[2];
	if (ids[1] == 'delete' && !confirm('คุณต้องการลบสมาชิกหมายเลข ' + ids[2])) {
		return;
	} else if (ids[1] == 'activate' && !confirm('คุณต้องการยอมรับสมาชิกหมายเลข ' + ids[2])) {
		return;
	} else if (ids[1] == 'ban' && !confirm('คุณต้องการระงับหรือยกเลิกการระงับการใช้งานสมาชิก ' + ids[2] + ' ชัวคราว ?')) {
		return;
	} else if (ids[1] == 'status') {
		query += '&value=' + obj.value;
	};
	var req = new GAjax();
	req.inintLoading('wait', true);
	req.send( web_url + 'member/action.php' ,query , function(xhr){
		var datas = req.toJSON(xhr.responseText);
		if (datas[0].error) {
			alert(datas[0].error);
		} else if (ids[1] == 'delete' && datas[0].id) {
			$G('ML_' + datas[0].id).remove();
		} else if (ids[1] == 'ban' && datas[0].id) {
			var td = document.getElementById('ML_' + datas[0].id).getElementsByTagName('td')[0];
			td.className = datas[0].ban == 1 ? 'ban' : '';
        } else if (ids[1] == 'activate' && datas[0].id) {
			var tds = $E('ML_' + datas[0].id).getElementsByTagName('td');
			for (var i = 0; i < tds.length ; i++) {
				if (tds[i].className == 'create') {
					tds[i].innerHTML = datas[0].create;
				};
			};
		};
	});
};

function start_counter() {
	new GAjax({'method':'get'}).autoupdate( web_url + 'counter/online.php' , counter_refresh_time , null , function(xhr){
		var data = xhr.responseText;
		if ( data != '' ) {
			var ds = data.split('|');
			$E('guest-online').innerHTML = ds[0];
			$E('user-online').innerHTML = ds[1];
		};
	});
};

var loader;
new GElement(window).addEvent('load' , function(){
	loader = new GLoader(geturl,function(xhr){
		var data = xhr.responseText;
		var content = $E( 'content' );
		content.innerHTML = data;
		loader.inint(content);
		Object.evalScript(data);
		window.scrollTo(0,$G(content).getTop());
	});
	loader.inintLoading( 'wait' , true );
	loader.inint(document);
	start_counter();
	$G(document).addEvent('contextmenu', function(event){
		GEvent.stop(event);
		return false;
	});
});

var geturl = function(url) {
	urls = url.split( '?' );
	return web_url + 'xhr.php?' + urls[1];
};
