<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
require(root."/".adminFolder."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "pages";
$pageName = "games";
$pageTitle = "Games";
$thisUrl = "games";
$ajax_page = "ajax_games.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("games_main", $ses_kduser)) {
		unAuthorizedAccess("games_main");
	} else {
		$fil_nama = trim(cekpost(cleanQuery($_REQUEST["fil_nama"]), ""));
		$fil_ket = trim(cekpost(cleanQuery($_REQUEST["fil_ket"]), ""));
		$fil_isactive = trim(cekpost(cleanQuery($_REQUEST["fil_isactive"]), ""));
		$fil_kdgame_cat = trim(cekpost(cleanQuery($_REQUEST["fil_kdgame_cat"]), ""));
		$swhere = '';
		if ($fil_nama!="") { $swhere .= " AND a.nama LIKE '%$fil_nama%' "; }
		if ($fil_ket!="") { $swhere .= " AND a.ket LIKE '%$fil_ket%' "; }
		if ($fil_isactive!="") { $swhere .= " AND a.isactive = '$fil_isactive' "; }
		if ($fil_kdgame_cat!="") { $swhere .= " AND a.kdgame_cat = '$fil_kdgame_cat' "; }
		
		$rs=array(); $q="SELECT a.*, b.nama AS nama_cat, b.url AS url_cat, c.flag AS negara_flag, namanegara FROM games a 
			LEFT JOIN game_cats b ON b.kdgame_cat=a.kdgame_cat 
			LEFT JOIN negara c ON c.kdnegara=a.kdnegara 
			WHERE 1=1 $swhere";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 20;
			$cp = cekpost(cleanQuery($_GET["cp"]), "1");
			$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
			$sr = ($cp-1) * $ld;
			$pagination = '';
			if ($tp!=1) {
				$pagination .= ('<ul class="pagination pagination-sm">');
					$disabled = '';
					$purl = '#';
					$onclick = '';
					if ($cp<=1) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
					for ($c=1;$c<=$tp;$c++) {
						$active = '';
						$purl = '#';
						$disabled = '';
						$onclick = '';
						if ($c==$cp) {
							$active = 'active';
						} else {
							$purl = '#';
							$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
						}
						$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
					}
					$disabled = '';
					$onclick = '';
					if ($cp>=$tp) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
				$pagination .= ('</ul>');
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<table class="table table-striped table-bordered">');
				cout('<thead>');
					cout('<tr>');
						cout('<th class="text-center">#</th>');
						if (authorizedModule("games_element_name", $ses_kduser)) {
							cout('<th class="text-center">Name</th>');
						}
						cout('<th class="text-center">Game Category</th>');
						cout('<th class="text-center">Only in Country</th>');
						if (authorizedModule("games_element_links", $ses_kduser)) {
							cout('<th class="text-center hidden-xs hidden-sm">Links</th>');
						}
						if (authorizedModule("games_element_active", $ses_kduser)) {
							cout('<th class="text-center">Active?</th>');
						}
						cout('<th class=""></th>');
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$cnt = 0;
				for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$cnt++;
					$kdgame = $rs[$i]["kdgame"];
					if (intval($rs[$i]["isactive"])==0) {
						$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$kdgame.', '.$rs[$i]["isactive"].');return false;">Non-Active</a>';
					} else {
						$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$kdgame.', '.$rs[$i]["isactive"].');return false;">Active</a>';
					}
					$isi = str_replace("\n", "<br/>", $rs[$i]["isi"]);
					
					$links = $rs[$i]["links"];
					$arr_links = explode(",", $links);
					$tmp_link = '';
					$cnt_links = 0;
					for ($j=0;$j<count($arr_links);$j++) {
						$tmp_link = trim($arr_links[$j]);
						if ($tmp_link!="") { $cnt_links += 1; }
					}
					cout('<tr>');
						cout('<td class="mid">'.($i+1).'</td>');
						if (authorizedModule("games_element_name", $ses_kduser)) {
							cout('<td class="mid"><a href="'.$pageUrl.'/view/'.$rs[$i]["kdgame"].'/">'.$rs[$i]["nama"].'</a></td>');
						}
						cout('<td class="mid"><a href="/'.adminFolder.'/game-categories/view/'.$rs[$i]["kdgame_cat"].'/" target="_blank">'.$rs[$i]["nama_cat"].'</a></td>');
						cout('<td class="mid"><span class="flag flag-'.$rs[$i]["negara_flag"].'"></span> '.$rs[$i]["namanegara"].'</td>');
						if (authorizedModule("games_element_links", $ses_kduser)) {
							cout('<td class="mid hidden-xs hidden-sm">'.$cnt_links.' Link'.($cnt_links>1?'s':'').'</td>');
						}
						if (authorizedModule("games_element_active", $ses_kduser)) {
							cout('<td class="mid isactive'.$kdgame.'">'.$isactive.'</td>');
						}
						cout('<td class="mid">');
							if (authorizedModule("games_edit", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/edit/'.$kdgame.'/"><span class="glyphicon glyphicon-pencil"></span><span class="hidden-xs hidden-sm"> Edit</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("games_image_logo", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/logo/'.$kdgame.'/"><span class="glyphicon glyphicon-apple"></span><span class="hidden-xs hidden-sm"> Logo</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("games_image_screenshot", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/image/'.$kdgame.'/"><span class="glyphicon glyphicon-picture"></span><span class="hidden-xs hidden-sm"> SS</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("games_image_screenshot", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/girl/'.$kdgame.'/"><span class="glyphicon glyphicon-picture"></span><span class="hidden-xs hidden-sm"> Girl</span></a>');
								cout(BSTextSeparator());
							}
							if (authorizedModule("games_delete", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/delete/'.$kdgame.'/"><span class="glyphicon glyphicon-trash"></span><span class="hidden-xs hidden-sm"> Delete</span></a>');
							}
						cout('</td>');
					cout('</tr>');
				}
				cout('</tbody>');
			cout('</table>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			$act_ket = "Games: Main List";
			if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
			updateUserActivities($act_ket);
		} else {
			cout('<div class="alert alert-info" role="alert">');
				cout('<strong>No data available.</strong> Please add new data for display.');
			cout('</div>');
		}
	}
}
if ($a=="chactive") {
	$kd = cekpost(cleanQuery($_REQUEST["kd"]), "");
	$ca = cekpost(cleanQuery($_REQUEST["ca"]), "");
	$kd = (trim($kd)=="undefined")?'':$kd;
	$ca = (trim($ca)=="undefined")?'':$ca;
	if ($kd!="" && $kd!=0 && $ca!="") {
		$na = $ca==0?1:0;
		$modby = $_SESSION["user"]["inisial"];
		$modtime = time();
		$qu = "UPDATE games SET isactive='$na', modby='$modby', modtime='$modtime' WHERE kdgame='$kd'";
		execRS($qu);
		$act_ket = "Games: Ch. Active [kd: $kd][from Act: $ca][to Act: $na]";
		updateUserActivities($act_ket);
		if (intval($na)==0) {
			$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$kd.', '.$na.');return false;">Non-Active</a>';
		} else {
			$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$kd.', '.$na.');return false;">Active</a>';
		}
		echo $isactive;
	}
}

if ($a=="section_view") {
	if (!authorizedModule("games_view", $ses_kduser)) {
		unAuthorizedAccess("games_view");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT a.*, b.nama AS nama_cat, b.url AS url_cat, c.flag AS negara_flag, namanegara FROM games a 
			LEFT JOIN game_cats b ON b.kdgame_cat=a.kdgame_cat 
			LEFT JOIN negara c ON c.kdnegara=a.kdnegara 
			WHERE kdgame='$id'";
		callRS($rs, $q);
		if (intval($rs[0]["isactive"])==0) {
			$isactive = '<a href="#" title="Activate" onclick="chActive(\''.$pageName.'\', '.$rs[0]["kdgame"].', '.$rs[0]["isactive"].');return false;">Non-Active</a>';
		} else {
			$isactive = '<a href="#" title="Deactivate" onclick="chActive(\''.$pageName.'\', '.$rs[0]["kdgame"].', '.$rs[0]["isactive"].');return false;">Active</a>';
		}
		$creby = (trim($rs[0]["creby"])!=""?'By: <strong>'.$rs[0]["creby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["cretime"])?' ('.date('d M Y, H:i:s A', $rs[0]["cretime"]).')':' &lt;<em>no data</em>&gt;');
		$modby = (trim($rs[0]["modby"])!=""?'By: <strong>'.$rs[0]["modby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["modtime"])?' ('.date('d M Y, H:i:s A', $rs[0]["modtime"]).')':' &lt;<em>no data</em>&gt;');
		cout('<dl class="dl-horizontal dldata">');
			if (authorizedModule("games_element_name", $ses_kduser)) {
				cout('<dt>Name</dt>');
				cout('<dd>'.$rs[0]["nama"].'</dd>');
				cout('<dt>URL</dt>');
				cout('<dd>'.absDom.'/'.$rs[0]["url_cat"].'/'.$rs[0]["url"].'&nbsp;&nbsp;<a href="'.absDom.'/'.$rs[0]["url_cat"].'/'.$rs[0]["url"].'" target="_blank"><span class="glyphicon glyphicon-share"></span></a></dd>');
			}
			cout('<dt>Game Category</dt>');
			cout('<dd><a href="/'.adminFolder.'/game-categories/view/'.$rs[0]["kdgame_cat"].'/" target="_blank">'.$rs[0]["nama_cat"].'</a></dd>');
			if (authorizedModule("games_element_links", $ses_kduser)) {
				$links = $rs[0]["links"];
				$arr_links = explode(",", $links);
				$tmp_link = '';
				$str_links = '';
				for ($j=0;$j<count($arr_links);$j++) {
					$tmp_link = trim($arr_links[$j]);
					if ($str_links!="") { $str_links .= ', '; }
					if ($tmp_link!="") {
						$str_links .= ''.$tmp_link.'&nbsp;<a href="//'.$tmp_link.'" target="_blank"><span class="glyphicon glyphicon-share"></span></a>';
					}
				}
				cout('<dt>Links</dt>');
				cout('<dd>'.$str_links.'</dd>');
			}
			cout('<dt>Only in Country</dt>');
			cout('<dd>'.(isset($rs[0]["namanegara"]) && $rs[0]["namanegara"]!=""?'<span class="flag flag-'.$rs[0]["negara_flag"].'"></span> '.$rs[0]["namanegara"]:'All').'</dd>');
			$sket = json_decode($rs[0]["short_ket"], 1);
			$ket = json_decode($rs[0]["ket"], 1);
			#printR($sket);
			#printR($ket);
			$str_w = '';
			foreach($ket AS $kdlang => $isi_ket) {
				$str_w .= ($str_w==""?" AND ( ":" OR ")."kdlang=$kdlang ";
			}
			$str_w .= $str_w!=""?")":"";
			$rsn=array();$qn="SELECT * FROM langs WHERE 1=1 $str_w";
			callRS($rsn, $qn);
			if (authorizedModule("games_element_active", $ses_kduser)) {
				cout('<dt>Active Status</dt>');
				cout('<dd class="isactive'.$rs[0]["kdgame"].'">'.$isactive.'</dd>');
			}
			if (authorizedModule("games_element_description", $ses_kduser)) {
				cout('<dt>Short Description</dt>');
				cout('<dd>');
					cout('<div class="container-fluid">');
						cout('<ul class="nav nav-pills">');
							for ($i=0;$i<count($rsn);$i++) {
								cout('<li class="'.($i==0?'active':'').'"><a  href="#slang'.$rsn[$i]["kdlang"].'" data-toggle="tab">'.$rsn[$i]["kodelang"].'</a></li>');
							}
						cout('</ul><!--/.nav nav-pills-->');
						cout('<div class="tab-content clearfix">');
							for ($i=0;$i<count($rsn);$i++) {
								$t_isi_sket = $sket[$rsn[$i]["kdlang"]];
								$t_isi_sket = str_replace('(loik/enter)', "<br/>", $t_isi_sket);
								$t_isi_sket = str_replace('(loik/slash)', "'", $t_isi_sket);
								$t_isi_sket = str_replace('(loik/doubleslash)', '"', $t_isi_sket);
								cout('<div class="tab-pane '.($i==0?'active':'').'" id="slang'.$rsn[$i]["kdlang"].'">');
									cout($t_isi_sket);
								cout('</div><!--/.tab-pane '.($i==0?'active':'').'-->');
							}
						cout('</div><!--/.tab-content clearfix-->');
					cout('</div><!--/.container-fluid-->');
				cout('</dd>');
				
				cout('<dt>Description</dt>');
				cout('<dd>');
					cout('<div class="container-fluid">');
						cout('<ul class="nav nav-pills">');
							for ($i=0;$i<count($rsn);$i++) {
								cout('<li class="'.($i==0?'active':'').'"><a  href="#lang'.$rsn[$i]["kdlang"].'" data-toggle="tab">'.$rsn[$i]["kodelang"].'</a></li>');
							}
						cout('</ul><!--/.nav nav-pills-->');
						cout('<div class="tab-content clearfix">');
							for ($i=0;$i<count($rsn);$i++) {
								$t_isi_ket = $ket[$rsn[$i]["kdlang"]];
								$t_isi_ket = str_replace('(loik/enter)', "<br/>", $t_isi_ket);
								$t_isi_ket = str_replace('(loik/slash)', "'", $t_isi_ket);
								$t_isi_ket = str_replace('(loik/doubleslash)', '"', $t_isi_ket);
								cout('<div class="tab-pane '.($i==0?'active':'').'" id="lang'.$rsn[$i]["kdlang"].'">');
									cout($t_isi_ket);
								cout('</div><!--/.tab-pane '.($i==0?'active':'').'-->');
							}
						cout('</div><!--/.tab-content clearfix-->');
					cout('</div><!--/.container-fluid-->');
				cout('</dd>');
			}
			if (authorizedModule("games_element_creation", $ses_kduser)) {
				cout('<dt>Created</dt>');
				cout('<dd>'.$creby.'</dd>');
			}
			if (authorizedModule("games_element_modification", $ses_kduser)) {
				cout('<dt>Modified</dt>');
				cout('<dd>'.$modby.'</dd>');
			}
			
		cout('</dl>');
		
		$act_ket = "Games: View: [kd: ".$rs[0]["kdgame"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
	exit;
}
if ($a=="do_delete") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	if ($kirim=="1") {
		$delby = $_SESSION["user"]["inisial"];
		$deltime = time();
		$kdgame = cekpost(cleanQuery($_POST["kdgame"]), "0");
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$kdgame'";
		callRS($rs, $q);
		
		$q = "DELETE FROM games WHERE kdgame='$kdgame'";
		execRS($q);
		$act_ket = "Games: Delete: Deleted [kd: $kdgame][query: ".cleanQuery($q)."]";
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/";</script>');
		exit;
	}
}
if ($a=="section_delete") {
	if (!authorizedModule("games_delete", $ses_kduser)) {
		unAuthorizedAccess("games_delete");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$id'";
		callRS($rs, $q);
		
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_delete" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kdgame" id="kdgame" value="'.$rs[0]["kdgame"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("games_element_name", $ses_kduser)) {
					cout('<dt>Name</dt>');
					cout('<dd>'.$rs[0]["nama"].'</dd>');
				}
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Delete</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Games: Deleting... [kdgame: $id][Data: ".json_encode($rs)."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="section_logo") {
	if (!authorizedModule("games_image_logo", $ses_kduser)) {
		unAuthorizedAccess("games_image_logo");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$id'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame_logo='$id'";
		callRS($rsi, $qi);
		$pageName = 'game_logo';
		if (count($rsi)!=0) {
			cout('<script type="text/javascript">parent.loadUploadedImage(\''.$pageName.'\', \''.$id.'\');</script>');
		} else {
			cout('<script type="text/javascript">parent.loadFormUploadImage(\''.$pageName.'\', \''.$id.'\', 0, 0);</script>');
		}
		$act_ket = "Games: Upload Logo... [kd: ".$rs[0]["kdgame"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="section_girl") {
	if (!authorizedModule("games_image_screenshot", $ses_kduser)) {
		unAuthorizedAccess("games_image_screenshot");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$id'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame_girl='$id'";
		callRS($rsi, $qi);
		$pageName = 'game_girl';
		if (count($rsi)!=0) {
			cout('<script type="text/javascript">parent.loadUploadedImage(\''.$pageName.'\', \''.$id.'\');</script>');
		} else {
			cout('<script type="text/javascript">parent.loadFormUploadImage(\''.$pageName.'\', \''.$id.'\', 0, 0);</script>');
		}
		$act_ket = "Games: Upload Girl Image... [kd: ".$rs[0]["kdgame"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="section_image") {
	if (!authorizedModule("games_image_screenshot", $ses_kduser)) {
		unAuthorizedAccess("games_image_screenshot");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$id'";
		callRS($rs, $q);
		$rsi=array(); $qi="SELECT * FROM u_files WHERE kdgame='$id'";
		callRS($rsi, $qi);
		if (count($rsi)!=0) {
			cout('<script type="text/javascript">parent.loadUploadedImage(\''.$pageName.'\', \''.$id.'\');</script>');
		} else {
			cout('<script type="text/javascript">parent.loadFormUploadImage(\''.$pageName.'\', \''.$id.'\', 0, 0);</script>');
		}
		$act_ket = "Games: Upload Image... [kd: ".$rs[0]["kdgame"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_edit") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kdgame = cekpost(cleanQuery($_REQUEST["kdgame"]), "0");
		$links = cekpost(cleanQuery($_REQUEST["links"]), "");
		
		$nama = cekpost(cleanQuery($_REQUEST["nama"]), "");
		$kdgame_cat = cekpost(cleanQuery($_REQUEST["kdgame_cat"]), 0);
		$kdnegara = cekpost(cleanQuery($_REQUEST["kdnegara"]), 0);
		
		$rsgc=array();$qgc="SELECT a.* FROM langs a LEFT JOIN negara b ON b.kdlang=a.kdlang WHERE b.isactive=1";
		callRS($rsgc, $qgc);
		for ($i=0;$i<count($rsgc);$i++) {
			#cout('<option value="'.$rsgc[$i]["kdlang"].'">'.$rsgc[$i]["namanegara"].' ('.$rsgc[$i]["kodenegara"].')</option>');
			$tsket = cekpost(cleanQuery($_REQUEST["sket-lang".$rsgc[$i]["kdlang"].""]), "");
			$tket = cekpost(cleanQuery($_REQUEST["ket-lang".$rsgc[$i]["kdlang"].""]), "");
			$tsket = str_replace('\'', "(loik/slash)", $tsket);
			$tsket = str_replace('\"', "(loik/doubleslash)", $tsket);
			$tsket = str_replace('\r\n', "(loik/enter)", $tsket);
			$tsket = str_replace('\\', "", $tsket);
			
			$tket = str_replace('\'', "(loik/slash)", $tket);
			$tket = str_replace('\"', "(loik/doubleslash)", $tket);
			$tket = str_replace('\r\n', "(loik/enter)", $tket);
			$tket = str_replace('\\', "", $tket);
			$sket[$rsgc[$i]["kdlang"]] = $tsket;
			$ket[$rsgc[$i]["kdlang"]] = $tket;
		}
		$sket_compiled = json_encode($sket);
		$ket_compiled = json_encode($ket);
		#echo'<xmp style="width:500px;"><pre style="width:500px;">';
		#echo $ket_compiled;
		#echo'</pre></xmp>';
		#exit;
		$isactive = cekpost(cleanQuery($_REQUEST["isactive"]), 0);
		if ($nama=="") { $err.= BSErrorMessage('Name must be filled'); }
		if ($kdgame_cat=="") { $err.= BSErrorMessage('Game Category Name must be chosen'); }
		if ($kdnegara=="") { $err.= BSErrorMessage('Available Country must be chosen'); }
		if ($err=="") {
			$modby = $_SESSION["user"]["inisial"];
			$modtime = time();
			$flag = 0;
			$url = cleanURL($nama);
			do {
				$rscek = array(); $qcek = "SELECT kdgame FROM games WHERE url = '$url' AND kdgame!='$kdgame'";
				callRS($rscek, $qcek);
				if (count($rscek)!=0) {
					$flag = 0;
					$url = "$kdgame-$url";
				} else {
					$flag = 1;
				}
			} while($flag==0);
			$q = "UPDATE games SET kdgame_cat='$kdgame_cat', kdnegara='$kdnegara', nama='$nama', url='$url', links='$links', short_ket='$sket_compiled', ket='$ket_compiled', isactive='$isactive', modby='$modby', modtime='$modtime' 
				WHERE kdgame='$kdgame'";
			#echo $q;
			execRS($q);
			#exit;
			$act_ket = "Games: Edit: Updated [kd: $kdgame][query: ".cleanQuery($q)."]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kdgame.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_edit") {
	if (!authorizedModule("games_edit", $ses_kduser)) {
		unAuthorizedAccess("games_edit");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM games WHERE kdgame='$id'";
		callRS($rs, $q);
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_edit" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kdgame" id="kdgame" value="'.$rs[0]["kdgame"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("games_element_name", $ses_kduser)) {
					cout('<dt><label for="nama">Name</label></dt>');
					cout('<dd><input class="form-control" type="text" name="nama" id="nama" value="'.$rs[0]["nama"].'" /></dd>');
				}
				if (authorizedModule("games_element_links", $ses_kduser)) {
					cout('<dt><label for="links">Links</label></dt>');
					cout('<dd><input class="form-control" type="text" name="links" id="links" value="'.$rs[0]["links"].'" /></dd>');
				}
				
				cout('<dt><label for="kdgame_cat">Game Categories</label></dt>');
				cout('<dd>');
					cout('<select name="kdgame_cat" id="kdgame_cat" class="form-control">');
						cout('<option value="">-</option>');
						$rsgc=array();$qgc="SELECT * FROM game_cats";
						callRS($rsgc, $qgc);
						for ($i=0;$i<count($rsgc);$i++) {
							cout('<option value="'.$rsgc[$i]["kdgame_cat"].'" '.(($rsgc[$i]["kdgame_cat"]==$rs[0]["kdgame_cat"])?' selected="selected"':'').'>'.$rsgc[$i]["nama"].'</option>');
						}
					cout('</select>');
				cout('</dd>');
				
				cout('<dt><label for="kdnegara">Only in Country</label></dt>');
				cout('<dd>');
					cout('<select name="kdnegara" id="kdnegara" class="form-control">');
						cout('<option value="0" '.((0==$rs[0]["kdnegara"])?' selected="selected"':'').'>All</option>');
						$rsgc=array();$qgc="SELECT * FROM negara WHERE isactive=1";
						callRS($rsgc, $qgc);
						for ($i=0;$i<count($rsgc);$i++) {
							cout('<option value="'.$rsgc[$i]["kdnegara"].'" '.(($rsgc[$i]["kdnegara"]==$rs[0]["kdnegara"])?' selected="selected"':'').'>'.$rsgc[$i]["namanegara"].' ('.$rsgc[$i]["kodenegara"].')</option>');
						}
					cout('</select>');
				cout('</dd>');
				
				if (authorizedModule("games_element_active", $ses_kduser)) {
					$isactive0 = intval($rs[0]["isactive"])==0?'selected="selected"':'';
					$isactive1 = intval($rs[0]["isactive"])==1?'selected="selected"':'';
					cout('<dt><label for="isactive">Active Status</label></dt>');
					cout('<dd>
						<select name="isactive" id="isactive" class="form-control">
							<option value="0" '.$isactive0.'>Non-Active</option>
							<option value="1" '.$isactive1.'>Active</option>
						</select>
					</dd>');
				}
				
				
				if (authorizedModule("games_element_description", $ses_kduser)) {
					$rsgc=array();$qgc="SELECT a.* FROM langs a LEFT JOIN negara b ON b.kdlang=a.kdlang WHERE b.isactive=1";
					callRS($rsgc, $qgc);
					$sket = json_decode($rs[0]["short_ket"], 1);
					cout('<dt><label for="sket-lang'.$rsgc[0]["kdlang"].'">Short Description</label></dt>');
					cout('<dd>');
						cout('<div class="container-fluid">');
							cout('<ul class="nav nav-pills">');
								for ($i=0;$i<count($rsgc);$i++) {
									cout('<li class="'.($i==0?'active':'').'"><a  href="#slang'.$rsgc[$i]["kdlang"].'" data-toggle="tab">'.$rsgc[$i]["kodelang"].'</a></li>');
								}
							cout('</ul><!--/.nav nav-pills-->');
							cout('<div class="tab-content clearfix">');
								for ($i=0;$i<count($rsgc);$i++) {
									$isi_sket = $sket[$rsgc[$i]["kdlang"]];
									$isi_sket = str_replace('(loik/enter)', "\r\n", $isi_sket);
									$isi_sket = str_replace('(loik/slash)', "\'", $isi_sket);
									$isi_sket = str_replace('(loik/doubleslash)', "\"", $isi_sket);
									cout('<div class="tab-pane '.($i==0?'active':'').'" id="slang'.$rsgc[$i]["kdlang"].'">');
										cout('<textarea name="sket-lang'.$rsgc[$i]["kdlang"].'" id="sket-lang'.$rsgc[$i]["kdlang"].'" rows="10" cols="80" class="form-control">'.$isi_sket.'</textarea>');
									cout('</div><!--/.tab-pane '.($i==0?'active':'').'-->');
								}
							cout('</div><!--/.tab-content clearfix-->');
						cout('</div><!--/.container-fluid-->');
					cout('</dd>');
					
					$ket = json_decode($rs[0]["ket"], 1);
					cout('<dt><label for="ket-lang'.$rsgc[0]["kdlang"].'">Description</label></dt>');
					cout('<dd>');
						cout('<div class="container-fluid">');
							cout('<ul class="nav nav-pills">');
								for ($i=0;$i<count($rsgc);$i++) {
									cout('<li class="'.($i==0?'active':'').'"><a  href="#lang'.$rsgc[$i]["kdlang"].'" data-toggle="tab">'.$rsgc[$i]["kodelang"].'</a></li>');
								}
							cout('</ul><!--/.nav nav-pills-->');
							cout('<div class="tab-content clearfix">');
								for ($i=0;$i<count($rsgc);$i++) {
									$isi_ket = $ket[$rsgc[$i]["kdlang"]];
									$isi_ket = str_replace('(loik/enter)', "\r\n", $isi_ket);
									$isi_ket = str_replace('(loik/slash)', "\'", $isi_ket);
									$isi_ket = str_replace('(loik/doubleslash)', "\"", $isi_ket);
									cout('<div class="tab-pane '.($i==0?'active':'').'" id="lang'.$rsgc[$i]["kdlang"].'">');
										cout('<textarea name="ket-lang'.$rsgc[$i]["kdlang"].'" id="ket-lang'.$rsgc[$i]["kdlang"].'" rows="10" cols="80" class="form-control">'.$isi_ket.'</textarea>');
									cout('</div><!--/.tab-pane '.($i==0?'active':'').'-->');
								}
							cout('</div><!--/.tab-content clearfix-->');
						cout('</div><!--/.container-fluid-->');
					cout('</dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Update</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="100%" height="200" style="background:#fff;overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Games: Editing... [kdgame: $id][From: ".json_encode($rs)."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_add") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$nama = cekpost(cleanQuery($_REQUEST["nama"]), "");
		$kdgame_cat = cekpost(cleanQuery($_REQUEST["kdgame_cat"]), 0);
		$kdnegara = cekpost(cleanQuery($_REQUEST["kdnegara"]), 0);
		$isactive = cekpost(cleanQuery($_REQUEST["isactive"]), 0);
		
		$rsgc=array();$qgc="SELECT a.* FROM langs a LEFT JOIN negara b ON b.kdlang=a.kdlang WHERE b.isactive=1";
		callRS($rsgc, $qgc);
		for ($i=0;$i<count($rsgc);$i++) {
			#cout('<option value="'.$rsgc[$i]["kdlang"].'">'.$rsgc[$i]["namanegara"].' ('.$rsgc[$i]["kodenegara"].')</option>');
			#$ket[$rsgc[$i]["kdlang"]] = cekpost(cleanQuery($_REQUEST["ket-lang".$rsgc[$i]["kdlang"].""]), "");
			#$sket[$rsgc[$i]["kdlang"]] = cekpost(cleanQuery($_REQUEST["sket-lang".$rsgc[$i]["kdlang"].""]), "");
			$tsket = cekpost(cleanQuery($_REQUEST["sket-lang".$rsgc[$i]["kdlang"].""]), "");
			$tket = cekpost(cleanQuery($_REQUEST["ket-lang".$rsgc[$i]["kdlang"].""]), "");
			$tsket = str_replace('\'', "(loik/slash)", $tsket);
			$tsket = str_replace('\"', "(loik/doubleslash)", $tsket);
			$tsket = str_replace('\r\n', "(loik/enter)", $tsket);
			$tsket = str_replace('\\', "", $tsket);
			
			$tket = str_replace('\'', "(loik/slash)", $tket);
			$tket = str_replace('\"', "(loik/doubleslash)", $tket);
			$tket = str_replace('\r\n', "(loik/enter)", $tket);
			$tket = str_replace('\\', "", $tket);
			$sket[$rsgc[$i]["kdlang"]] = $tsket;
			$ket[$rsgc[$i]["kdlang"]] = $tket;
		}
		$sket_compiled = json_encode($sket);
		$ket_compiled = json_encode($ket);
		#if ($tanggal!="") { $tanggal = strtotime($tanggal." ".date('H:i:s', time())); }
		if ($nama=="") { $err.= BSErrorMessage('Name must be filled'); }
		if ($kdgame_cat=="") { $err.= BSErrorMessage('Game Category Name must be chosen'); }
		#if ($kdnegara=="") { $err.= BSErrorMessage('Available Country must be chosen'); }
		#if ($ket=="") { $err.= BSErrorMessage('Description must be filled'); }
		if ($err=="") {
			$creby = $_SESSION["user"]["inisial"];
			$cretime = time();
			$kdgame = getID("games", "kdgame");
			$flag = 0;
			$url = cleanURL($nama);
			do {
				$rscek = array(); $qcek = "SELECT kdgame FROM games WHERE url = '$url'";
				callRS($rscek, $qcek);
				if (count($rscek)!=0) {
					$flag = 0;
					$url = "$kdgame-$url";
				} else {
					$flag = 1;
				}
			} while($flag==0);
			
			$q = "INSERT INTO games (kdgame, kdgame_cat, kdnegara, nama, url, short_ket, ket, isactive, creby, cretime) 
				VALUES ('$kdgame', '$kdgame_cat', '$kdnegara', '$nama', '$url', '$sket_compiled', '$ket_compiled', '$isactive', '$creby', '$cretime')";
			execRS($q);
			$act_ket = "Games: Add [kd: $kdgame][query: ".cleanQuery($q)."]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kdgame.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_add") {
	if (!authorizedModule("games_add", $ses_kduser)) {
		unAuthorizedAccess("games_add");
	} else {
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_add" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<dl class="dl-horizontal dldata">');
				cout('<dt><label for="nama">Game Name</label></dt>');
				cout('<dd><input class="form-control" type="text" name="nama" id="nama" /></dd>');
				
				cout('<dt><label for="kdgame_cat">Game Categories</label></dt>');
				cout('<dd>');
					cout('<select name="kdgame_cat" id="kdgame_cat" class="form-control">');
						cout('<option value="">-</option>');
						$rsgc=array();$qgc="SELECT * FROM game_cats";
						callRS($rsgc, $qgc);
						for ($i=0;$i<count($rsgc);$i++) {
							cout('<option value="'.$rsgc[$i]["kdgame_cat"].'">'.$rsgc[$i]["nama"].'</option>');
						}
					cout('</select>');
				cout('</dd>');
				
				cout('<dt><label for="kdnegara">Only in Country</label></dt>');
				cout('<dd>');
					cout('<select name="kdnegara" id="kdnegara" class="form-control">');
						cout('<option value="">All</option>');
						$rsgc=array();$qgc="SELECT * FROM negara WHERE isactive=1";
						callRS($rsgc, $qgc);
						for ($i=0;$i<count($rsgc);$i++) {
							cout('<option value="'.$rsgc[$i]["kdnegara"].'">'.$rsgc[$i]["namanegara"].' ('.$rsgc[$i]["kodenegara"].')</option>');
						}
					cout('</select>');
				cout('</dd>');
				
				cout('<dt><label for="isactive">Active Status</label></dt>');
				cout('<dd>
					<select name="isactive" id="isactive" class="form-control">
						<option value="0">Non-Active</option>
						<option value="1">Active</option>
					</select>
				</dd>');
				$rsgc=array();$qgc="SELECT a.* FROM langs a LEFT JOIN negara b ON b.kdlang=a.kdlang WHERE b.isactive=1";
				callRS($rsgc, $qgc);
				cout('<dt><label for="sket-lang'.$rsgc[0]["kdlang"].'">Short Description</label></dt>');
				cout('<dd>');
					cout('<div class="container-fluid">');
						cout('<ul class="nav nav-pills">');
							for ($i=0;$i<count($rsgc);$i++) {
								cout('<li class="'.($i==0?'active':'').'"><a  href="#slang'.$rsgc[$i]["kdlang"].'" data-toggle="tab">'.$rsgc[$i]["kodelang"].'</a></li>');
							}
						cout('</ul><!--/.nav nav-pills-->');
						cout('<div class="tab-content clearfix">');
							for ($i=0;$i<count($rsgc);$i++) {
								cout('<div class="tab-pane '.($i==0?'active':'').'" id="slang'.$rsgc[$i]["kdlang"].'">');
									cout('<textarea name="sket-lang'.$rsgc[$i]["kdlang"].'" id="sket-lang'.$rsgc[$i]["kdlang"].'" rows="5" cols="80" class="form-control"></textarea>');
								cout('</div><!--/.tab-pane '.($i==0?'active':'').'-->');
							}
						cout('</div><!--/.tab-content clearfix-->');
					cout('</div><!--/.container-fluid-->');
				cout('</dd>');
				
				
				cout('<dt><label for="ket-lang'.$rsgc[0]["kdlang"].'">Description</label></dt>');
				cout('<dd>');
					cout('<div class="container-fluid">');
						cout('<ul class="nav nav-pills">');
							for ($i=0;$i<count($rsgc);$i++) {
								cout('<li class="'.($i==0?'active':'').'"><a  href="#lang'.$rsgc[$i]["kdlang"].'" data-toggle="tab">'.$rsgc[$i]["kodelang"].'</a></li>');
							}
						cout('</ul><!--/.nav nav-pills-->');
						cout('<div class="tab-content clearfix">');
							for ($i=0;$i<count($rsgc);$i++) {
								cout('<div class="tab-pane '.($i==0?'active':'').'" id="lang'.$rsgc[$i]["kdlang"].'">');
									cout('<textarea name="ket-lang'.$rsgc[$i]["kdlang"].'" id="ket-lang'.$rsgc[$i]["kdlang"].'" rows="10" cols="80" class="form-control"></textarea>');
								cout('</div><!--/.tab-pane '.($i==0?'active':'').'-->');
							}
						cout('</div><!--/.tab-content clearfix-->');
					cout('</div><!--/.container-fluid-->');
				cout('</dd>');
				
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Add</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="100%" height="200" style="background:#fff;overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Games: Adding...";
		updateUserActivities($act_ket);
		exit;
	}
}
?>