function doAddTeam() {
	var req = new GAjax();
	req.inintLoading( 'wait' , true );
	req.send( 'game/action.php' , 'action=addteam', function(xhr){
		var ds = xhr.responseText.split('|');
		// tr
		tr = document.createElement('tr');
		// วันที่ เวลา แข่งขัน
		var d = ds[1].split('-');
		td = document.createElement('td');
		td.className = 'left';
		sel = document.createElement('select');
		sel.id = 'date_' + ds[0];
		for( i = 1 ; i <= 31 ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = i;
			sel.appendChild(option);
		};
		td.appendChild(sel);
		sel.selectedIndex = parseFloat(d[2]) - 1;
		span = document.createElement('span');
		span.innerHTML = '-';
		td.appendChild(span);
		sel = document.createElement('select');
		sel.id = 'month_' + ds[0];
		for( i = 1 ; i <= 12 ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = shortmonth[i - 1];
			sel.appendChild(option);
		};
		td.appendChild(sel);
		sel.selectedIndex = parseFloat(d[1]) - 1;
		span = document.createElement('span');
		span.innerHTML = '-';
		td.appendChild(span);
		sel = document.createElement('select');
		sel.id = 'year_' + ds[0];
		for( i = 2552 ; i < 2563 ; i++ ) {
			option = document.createElement('option');
			option.value = i - 543;
			option.innerHTML = i;
			sel.appendChild(option);
		};
		td.appendChild(sel);
		sel.selectedIndex = parseFloat(d[0]) - 2552 + 543;
		span = document.createElement('span');
		span.innerHTML = '&nbsp;';
		td.appendChild(span);
		sel = document.createElement('select');
		sel.id = 'hour_' + ds[0];
		for( i = 0 ; i < 24 ; i++ ) {
			option = document.createElement('option');
			a = formatstr(i, 2);
			option.value = a;
			option.innerHTML = a;
			sel.appendChild(option);
		};
		td.appendChild(sel);
		span = document.createElement('span');
		span.innerHTML = ':';
		td.appendChild(span);
		sel = document.createElement('select');
		sel.id = 'minute_' + ds[0];
		for( i = 0 ; i < 60 ; i++ ) {
			option = document.createElement('option');
			a = formatstr(i, 2);
			option.value = a;
			option.innerHTML = a;
			sel.appendChild(option);
		};
		td.appendChild(sel);
		tr.appendChild(td);
		// เจ้าบ้าน
		var td = document.createElement('td');
		input = document.createElement('input');
		input.id = 'hometeam_' + ds[0];
		input.type = 'text';
		input.size = 10;
		td.appendChild(input);
		tr.appendChild(td);
		// อัตราต่อรอง
		var td = document.createElement('td');
		sel = document.createElement('select');
		sel.id = 'bargain_' + ds[0];
		option = document.createElement('option');
		for( i = 0 ; i < games_team.length ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = games_team[i];
			sel.appendChild(option);
		};
		td.appendChild(sel);
		span = document.createElement('span');
		span.innerHTML = '&nbsp;ต่อ&nbsp;';
		td.appendChild(span);
		sel = document.createElement('select');
		sel.id = 'bargainrate_' + ds[0];
		td.appendChild(sel);
		for( i = 0 ; i < games_rate.length ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = games_rate[i];
			sel.appendChild(option);
		};
		td.appendChild(sel);
		tr.appendChild(td);
		// ทีมเยือน
		var td = document.createElement('td');
		input = document.createElement('input');
		input.id = 'visitteam_' + ds[0];
		input.type = 'text';
		input.size = 10;
		td.appendChild(input);
		tr.appendChild(td);
		// score
		var td = document.createElement('td');
		sel = document.createElement('select');
		sel.id = 'score_0_' + ds[0];
		for( i = 0 ; i < 20 ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = i;
			sel.appendChild(option);
		};
		td.appendChild(sel);
		span = document.createElement('span');
		span.innerHTML = '-';
		td.appendChild(span);
		sel = document.createElement('select');
		sel.id = 'score_1_' + ds[0];
		for( i = 0 ; i < 20 ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = i;
			sel.appendChild(option);
		};
		td.appendChild(sel);
		tr.appendChild(td);
		// ผลการแข่งขัน
		var td = document.createElement('td');
		sel = document.createElement('select');
		sel.id = 'result_' + ds[0];
		for( i = 0 ; i < games_score.length ; i++ ) {
			option = document.createElement('option');
			option.value = i;
			option.innerHTML = games_score[i];
			sel.appendChild(option);
		};
		td.appendChild(sel);
		tr.appendChild(td);
		// submit,delete
		var td = document.createElement('td');
		input = document.createElement('input');
		input.type = 'button';
		input.value = 'ลบ';
		input.onclick = function(){doDeleteItem(ds[0])};
		input.id = 'delete_' + ds[0];
		td.appendChild(input);
		span = document.createElement('span');
		span.innerHTML = '&nbsp;';
		td.appendChild(span);
		input = document.createElement('input');
		input.type = 'button';
		input.value = 'บันทึก';
		input.onclick = function(){doSaveItem(ds[0])};
		input.id = 'save_' + ds[0];
		td.className = 'right';
		td.appendChild(input);
		tr.appendChild(td);
		tr.id = 'row_' + ds[0];
		// ใส่ลงในตาราง
		$E('game_table').appendChild(tr);
	});
};

function formatstr( val, digit ) {
	var ret = '';
	if ( typeof val != 'undefined' ) {
		val = val.toString();
		for ( var b = 0 ; b < ( digit - val.length ) ; b++ ) {
			ret = ret + '0';
		};
	};
	return ret + val;
};

function doDeleteItem( id ) {
	if (confirm('คุณต้องการจะลบรายการนี้ ?')) {
		var req = new GAjax();
		req.inintLoading( 'wait' , true );
		req.send( 'game/action.php' , 'action=deleteteam&id=' + id , function(xhr){
			if (xhr.responseText != '') {
				alert(xhr.responseText);
			} else {
				$G('row_' + id).remove();
			};
		});
	};
};

function doSaveItem( id ) {
	var query = 'action=saveteam&id=' + id + '&playtime=' + $E('year_' + id).value + '-' + $E('month_' + id).value + '-' + $E('date_' + id).value + ' ' + $E('hour_' + id).value + ':' + $E('minute_' + id).value + ':00';
	query += '&home=' + encodeURIComponent($E('hometeam_' + id).value);
	query += '&visit=' + encodeURIComponent($E('visitteam_' + id).value);
	query += '&bargain=' + $E('bargain_' + id).value;
	query += '&bargainrate=' + $E('bargainrate_' + id).value;
	query += '&a=' + $E('score_0_' + id).value;
	query += '&b=' + $E('score_1_' + id).value;
	query += '&result=' + $E('result_' + id).value;
	var req = new GAjax();
	req.inintLoading( 'wait' , true );
	req.send( 'game/action.php' , query , function(xhr){
		if (xhr.responseText != '') {
			alert(xhr.responseText);
		};
	});
};

var modal;

function doPlayGame( id ) {
	var req = new GAjax();
	req.inintLoading( 'wait' , true );
	req.send( 'game/action.php' , 'action=selteam&id=' + id , function(xhr){
		if (xhr.responseText != '') {
			modal = new GModal();
			modal.show(xhr.responseText);
		};
	});
};

function doSelTeam( id, val) {
	var query = 'action=savesel&mp=' + $E('game_mp').value;
	query += '&id=' + id;
	query += '&val=' + val;

	var req = new GAjax();
	req.inintLoading( 'wait' , true );
	req.send( 'game/action.php' , query , function(xhr){
		if (xhr.responseText != '') {
			alert(xhr.responseText);
		};
		modal.hide();
	});
};

function doNewGame() {
	if (confirm("คุณต้องการที่จะอัปเดทผลคะแนนสมาชิก และ เริ่มเกมส์ใหม่ ?\n\nถ้าคุณดำเนินการนี้ต่อหลังเที่ยงคืน วันที่เล่นเกมส์และเกมส์จะถูกปรับเปลี่ยนเป็นวันใหม่อัตโนมัติ\nถ้าคุณยังเล่นเกมส์ในวันนี้ไม่จบให้ยกเลิกการทำงานนี้ไปก่อน และกลับมาเริ่มใหม่หลังจากที่เกมส์ในวันนี้สิ้นสุดแล้ว")) {
		var req = new GAjax();
		req.inintLoading( 'wait' , true );
		req.send( 'game/action.php' , 'action=newgame' , function(xhr){
			if (xhr.responseText != '') {
				alert(xhr.responseText);
			} else {
				window.location = 'index.php?module=game/setup';
			};
		});
	};
};

function doReCalc( gamedate ) {
	if (confirm("คุณต้องการที่จะคำนวณผลคะแนนของเดือนนี้ใหม่อีกครั้ง ?\nการคำนวณผลคะแนนใหม่จะไม่รวมเกมส์ในวันนี้\nคุณควรตรวจสอบผลคะแนนให้ถูกต้องก่อนการอัปเดท")) {
		var req = new GAjax();
		req.inintLoading( 'wait' , true );
		req.send( 'game/action.php' , 'action=recalc' , function(xhr){
			if (xhr.responseText != '') {
				alert(xhr.responseText);
			} else {
				window.location = 'index.php?module=game/setup&date=' + gamedate;
			};
		});
	};
};

function viewMP( id ) {
    window.location = 'index.php?module=game/score&id=' + id;
};

function doSelDivision( sel ) {
	window.location = 'index.php?module=game/order&div=' + sel.value;
};

function doGameSelDate( sel ) {
    window.location = 'index.php?module=game/setup&date=' + sel.value;
};