<?php
include("config.php");
function con(){
global $globalConfig;
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
return $db;
}

function show_error($error){
global $globalConfig;
	
	switch((int)$error){
	
		case 1:
			$message =$globalConfig['errorNoCookies'];
		break;
		
		case 2:
			$message =$globalConfig['errorDelay'];
		break;
		
		case 3:
			$message =$globalConfig['errorNoAdmin'];
		break;
		
		case 4:
			$message =$globalConfig['errorNoGroup'];
		break;
		
		case 5:
			$message =$globalConfig['errorNoLogin'];
		break;		
		
		case 6:
			$message =$globalConfig['errorInvalid'];
		break;
		
	
	}
return '<div dir="rtl" style="color:red; background-color:#FFC4C4; width:380px; border-bottom: 1px solid #F30; border-top: 1px solid #F30; font-size:11px ; font-weight:bold; text-align:right;"><img src="images/notice-alert.png"  align="absmiddle"> '.$message.'</div>';
}
//===========================================================


	function get_team_list ($name, $league_id = 0, $selected) {
		$db = con();
		$text = '';
		if($league_id == 0) {
			$text .= '<select name="'.$name.'">';
            $sql = $db->query("select * from team order by team_name");
			while($res = $db->fetchNextArray($sql)){
				$selected_text = '';
				if ($res['team_id'] == $selected) $selected_text = 'selected="selected"';
				$text .= '<option value="'.$res['team_id'].'" '.$selected_text.'>'.$res['team_name'].'</option>'."\n\t\t\t";
			}
            $text .= '</select>';
	return $text;
		}else{
			$text .= '<select name="'.$name.'">';
            $sql = $db->query("SELECT t.team_id, t.team_name FROM team_to_league t2l, team t where t2l.team_id = t.team_id and t2l.league_id = '".(int)$league_id."'");
			while($res = $db->fetchNextArray($sql)){
				$selected_text = '';
				if ($res['team_id'] == $selected) $selected_text = 'selected="selected"';
				$text .= '<option value="'.$res['team_id'].'" '.$selected_text.'>'.$res['team_name'].'</option>'."\n\t\t\t";
			}
            $text .= '</select>';
	return $text;
		}
	}


//==============================================

	function get_years ($selected, $name = 'year') {
		$from = 1389;
		$to = 1387;
		$text = '<select name="'.$name.'">';
		for ($i=$from;$i>=$to; $i--) {
		$selected_text = '';
			if ($i == $selected) $selected_text = 'selected="selected"';
			if (($i == 1388) && ($selected == '')) $selected = 'selected="selected"';
			$text .= '<option value="'.$i.'" '.$selected_text.'>'.$i.'</option>';
		}
		$text .= '</select>';
	return $text;
	}



//==============================================

	function get_monthes ($selected, $name = 'month') {
		$from = 1;
		$to = 12;
		$text = '<select name="'.$name.'">';
		for ($i=$from;$i<=$to; $i++) {
		$selected_text = '';
			if ($i == $selected) $selected_text = 'selected="selected"';
			$text .= '<option value="'.$i.'" '.$selected_text.'>'.$i.'</option>';
		}
		$text .= '</select>';
	return $text;
	}



//==============================================

	function get_days ($selected, $name = 'day') {
		$from = 1;
		$to = 31;
		$text = '<select name="'.$name.'">';
		for ($i=$from;$i<=$to; $i++) {
		$selected_text = '';
			if ($i == $selected) $selected_text = 'selected="selected"';
			$text .= '<option value="'.$i.'" '.$selected_text.'>'.$i.'</option>';
		}
		$text .= '</select>';
	return $text;
	}


//==============================================

	function get_hours ($selected, $name = 'hour') {
		$from = 1;
		$to = 24;
		$text = '<select name="'.$name.'">';
		for ($i=$from;$i<=$to; $i++) {
		$selected_text = '';
			if ($i == $selected) $selected_text = 'selected="selected"';
			$text .= '<option value="'.$i.'" '.$selected_text.'>'.$i.'</option>';
		}
		$text .= '</select>';
	return $text;
	}


//==============================================

	function get_minutes ($selected, $name = 'minute') {
		$from = 0;
		$to = 59;
		$text = '<select name="'.$name.'">';
		for ($i=$from;$i<=$to; $i++) {
		$selected_text = '';
			if ($i == $selected) $selected_text = 'selected="selected"';
			$text .= '<option value="'.$i.'" '.$selected_text.'>'.$i.'</option>';
		}
		$text .= '</select>';
	return $text;
	}


//======================================================

	function get_team_name ($team_id) {
		$db = con();
		$sql = $db->query("SELECT team_name FROM team WHERE team_id = '".(int)$team_id."'");
		$res = $db->fetchNextArray($sql);
	return $res['team_name'];
	}

//==================================================

	function get_match_status ($status) {
		$db = con();
		switch ($status) {
			case 1:
				$output = 'تحت ويرايش';
			break;
			case 2:
				$output = 'عملياتی';
			break;
			case 3:
				$output = 'اتمام مهلت ثبت نام';
			break;
			case 4:
				$output = 'لغو شده';
			break;
		}
	return $output;
	}

//==============================================

	function get_match_state_list ($selected, $name = 'match_status') {
		$from = 1;
		$to = 4;
		$text = '<select name="'.$name.'">';
		for ($i=$from;$i<=$to; $i++) {
		$selected_text = '';
			if ($i == $selected) $selected_text = 'selected="selected"';
			$text .= '<option value="'.$i.'" '.$selected_text.'>'.get_match_status ($i).'</option>';
		}
		$text .= '</select>';
	return $text;
	}

//============================================

	function get_game_name ($game_id) {
		$db = con();
		$sql1 = $db->query("SELECT team1_id, team2_id FROM game WHERE game_id = '".(int)$game_id."'");
		$res1 = $db->fetchNextArray($sql1);
		$sql2 = $db->query("SELECT team_name FROM team WHERE team_id = '".(int)$res1['team1_id']."'");
		$res2 = $db->fetchNextArray($sql2);
		$sql3 = $db->query("SELECT team_name FROM team WHERE team_id = '".(int)$res1['team2_id']."'");
		$res3 = $db->fetchNextArray($sql3);
		
	return $res2['team_name'] . ' - ' . $res3['team_name'];
	}

//========================================

	function tep_get_leagues_list ($selected = '') {
		$db = con();
		$sql = $db->query("SELECT league_id, league_name FROM league ORDER BY league_id ASC");
		$output = '';
		while($res = $db->fetchNextArray($sql)) {
			$selected_text = '';
			if ($res['league_id'] == $selected) $selected_text = ' selected="selected"';
			$output .= '<option value="'.(int)$res['league_id'].'"'.$selected_text.'>'.$res['league_name'].'</option>'."\n";
		}
	return $output;
	}
	
//===========================================

	function get_games_list($name, $league_id, $selected = '') {
		$db = con();
		$sql = $db->query("SELECT game_id FROM game WHERE league_id = '".(int)$league_id."' ORDER BY game_id ASC");
		$output = '<select name="'.$name.'">'."\n";
		$output .= '<option value=""> انتخاب کنید </option>'."\n";
		while($res = $db->fetchNextArray($sql)) {
			$selected_text = '';
			if ($res['league_id'] == $selected) $selected_text = ' selected="selected"';
			$output .= '<option value="'.(int)$res['game_id'].'"'.$selected_text.'>'.get_game_name((int)$res['game_id']).'</option>'."\n";
		}
		$output .= '</select>';
	return $output;
	}


//========================================

	function get_game_result ($game_id, $disabled = 2) {
		$db = con();
		$sql = $db->query("SELECT team1_goals,team2_goals FROM game WHERE game_id = '".(int)$game_id."'");
		$res = $db->fetchNextArray($sql);
		if((int)$disabled == 1){
			$disabled_text = ' disabled="disabled"';
		}else{
			$disabled_text = '';
		}
		if((int)$res['game_status'] == 0){
			$output = '<input type="checkbox" name="team1"'.$disabled_text.' /> | <input type="checkbox" name="equal"'.$disabled_text.' /> | <input type="checkbox" name="team1"'.$disabled_text.' />';
		}elseif($res['team1_goals'] > $res['team2_goals']){
			$output = '<input type="checkbox" name="team1" checked="checked"'.$disabled_text.' /> | <input type="checkbox" name="equal"'.$disabled_text.' /> | <input type="checkbox" name="team1"'.$disabled_text.' />';
		}elseif($res['team2_goals'] > $res['team1_goals']){
			$output = '<input type="checkbox" name="team1"'.$disabled_text.' /> | <input type="checkbox" name="equal"'.$disabled_text.' /> | <input type="checkbox" name="team1" checked="checked"'.$disabled_text.' />';
		}elseif($res['team1_goals'] == $res['team2_goals']){
			$output = '<input type="checkbox" name="team1"'.$disabled_text.' /> | <input type="checkbox" name="equal" checked="checked"'.$disabled_text.' /> | <input type="checkbox" name="team1"'.$disabled_text.' />';
		}
	return $output;
	}
	
	
//===========================================================================================================================================new function

	function get_game_date ($game_id) {
		$db = con();
		$sql = $db->query("SELECT game_date FROM game WHERE game_id = '".(int)$game_id."'");
		$res = $db->fetchNextArray($sql);
	return tep_hejri3($res['game_date']);
	}
	
	
//============================================

	function get_league_name ($league_id) {
		$db = con();
		$sql = $db->query("SELECT league_name FROM league WHERE league_id = '".(int)$league_id."'");
		$res = $db->fetchNextArray($sql);
		
	return $res['league_name'];
	}

	
//====================================================	
	function get_credit_amount($user_id){
		$db = con();
		$sql = $db->query("select amount from credit where user_id='".(int)$user_id."'");
		$res = $db->fetchNextArray($sql);
		
	return $res['amount'];
	}

	
//=========================================================

	function decrease_from_credit_amount($user_id, $amount) {
		$db = con();
		$sql = $db->execute("update credit SET amount = amount - ".(int)$amount." where user_id='".(int)$user_id."'");
	return true;
	}
	


//=========================================================

	function increase_credit_amount($user_id, $amount) {
		$db = con();
		$sql = $db->execute("update credit SET amount = amount + ".(int)$amount." where user_id='".(int)$user_id."'");
	return true;
	}
	


//============================================

	function get_match_name ($match_id) {
		$db = con();
		$sql = $db->query("SELECT match_name FROM matches WHERE match_id = '".(int)$match_id."'");
		$res = $db->fetchNextArray($sql);
		
	return $res['match_name'];
	}

	
	
//==============================================

	function get_game_status ($status) {
		switch($status) {
			case 0:
				$output = ' شروع نشده ';
			break;
			
			
			case 1:
				$output = ' در حال انجام ';
			break;
			
			
			case 2:
				$output = ' نتيجه نهايی ';
			break;
			
			
			case 3:
				$output = ' به تعويق افتاد ';
			break;
			
			
		}
	return $output;
	}
	

//==================================


	function draw_game_status_list ($name, $selected = '') {
		
		$output = '<select name="'.$name.'">'."\n";
		for($i=0;$i<=3;$i++) {
			$selected_text = '';
			if ($i == $selected) {$selected_text = ' selected="selected"';}
			$output .= '<option value="'.$i.'"'.$selected_text.'>'.get_game_status($i).'</option>'."\n";
		}
		$output .= '</select>';
		
	return $output;
	}
	
	
	
//==============================

	function get_goal_num ($number) {
		if($number == ''){
			return '?';
		}else{
			return (int)$number;
		}
	}
	
	

?>