function AdmMDSave()
 {
     $tid = JRequest::getVar('tid', 0, '', 'int');
     $sid = JRequest::getVar('sid', 0, '', 'int');
     $mid = JRequest::getVar('mid', 0, '', 'int');
     $user = JFactory::getUser();
     $t_single = JRequest::getVar('t_single', 0, 'post', 'int');
     $t_knock = JRequest::getVar('t_knock', 0, 'post', 'int');
     ///
     $s_id = JRequest::getVar('sid', 0, 'post', 'int');
     $this->season_id = $s_id;
     $post = JRequest::get('post');
     $post['s_id'] = $s_id;
     $post['k_format'] = JRequest::getVar('format_post', 0, 'post', 'int');
     //format_fe
     $post['m_descr'] = JRequest::getVar('m_descr', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $post['t_type'] = JRequest::getVar('t_knock', 0, 'post', 'int');
     if ($this->acl == 2 || $this->acl == 3) {
         unset($post['m_descr']);
         unset($post['m_name']);
         $jsmr_mark_played = $this->getJS_Config("jsmr_mark_played");
     }
     if ($this->acl == 3) {
         unset($post['s_id']);
     }
     $row = new JTableMday($this->db);
     if (!$row->bind($post)) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->check()) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     $row->checkin();
     $row->load($row->id);
     $mj = 0;
     if ($row->s_id == -1) {
         $t_type = 0;
     } else {
         //$query = "SELECT tr.t_type FROM #__bl_seasons as s,#__bl_tournament as tr WHERE s.published=1 AND tr.id=s.t_id AND s.s_id=".$row->s_id;
         //$this->db->setQuery($query);
         $t_type = $post['t_type'];
     }
     if ($this->acl == 3) {
         $query = "SELECT id FROM #__bl_players WHERE usr_id=" . $this->_user->id;
         $this->db->setQuery($query);
         $playerid = $this->db->LoadResult();
     }
     $prevarr = array();
     if ($this->acl == 1 && $t_knock) {
         if (isset($_POST['teams_kn']) && count($_POST['teams_kn'])) {
             foreach ($_POST['teams_kn'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['match_id'][$mj]) ? $_POST['match_id'][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 //update
                 $match->team2_id = intval($_POST['teams_kn_aw'][$mj]);
                 if ($_POST['res_kn_1'][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_1'][$mj]);
                 }
                 if ($_POST['res_kn_1_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_1_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = 1;
                 if (isset($_POST['kn_match_played_' . $mj])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj]);
                 } else {
                     $match->m_played = 0;
                 }
                 if (!isset($_POST['res_kn_1'][$mj]) || !isset($_POST['res_kn_1_aw'][$mj]) || $_POST['res_kn_1'][$mj] == '' || $_POST['res_kn_1_aw'][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $prevarr[] = $match->id;
                 $mj++;
             }
         }
         $levcount = 2;
         //$lev = isset($_POST['teams_kn1'])?$_POST['teams_kn1']:0;
         while (isset($_POST['teams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['teams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 2) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } else {
                     if ($_POST['final']) {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj * 2]) ? $_POST['lmatches_' . ($levcount - 1)][$mj * 2] : 0);
                     } else {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2 + 1]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2 + 1] : 0);
                     }
                 }
                 //if(($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['matches_' . $levcount][$mj]) ? $_POST['matches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['teams_kn_aw_' . $levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 if ($_POST['res_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['kn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['res_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['res_kn_' . $levcount][$mj] && !$_POST['res_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 //}
             }
             $levcount++;
         }
         ////////////////////////////////////////
         $levcount = 1;
         while (isset($_POST['lteams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['lteams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 1) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } elseif ($levcount % 2 == 0) {
                     $match_1 = new JTableMatch($this->db);
                     $num = intval($levcount / 4) ? ceil($levcount / 4) : floor($levcount / 4);
                     if ($levcount == 8 || $levcount == 10) {
                         $num += 1;
                     }
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['matches_' . ($levcount - $num)][$mj]) ? $_POST['matches_' . ($levcount - $num)][$mj] : 0);
                 } else {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj + 1]) ? $_POST['lmatches_' . ($levcount - 1)][$mj + 1] : 0);
                 }
                 //////////////&&&&
                 //if(($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['lmatches_' . $levcount][$mj]) ? $_POST['lmatches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['lteams_kn_aw_' . $levcount][$mj]);
                 $match->k_type = intval($_POST['lk_type_' . $levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 //var_dump($match);die();
                 if ($_POST['lres_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['lres_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['lres_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['lkn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['lres_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['lres_kn_' . $levcount][$mj] && !$_POST['lres_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 //////////////////
                 // if($match_1->m_played == 0 || $match_2->m_played == 0){
                 //$match->m_played = 0;
                 //}
                 //else{
                 // $match->m_played = 1;
                 //}
                 if (($match_1->team1_id == -1 || $match_1->team2_id == -1 || $match_2->team1_id == -1 or $match_2->team2_id == -1) && intval($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 //}
             }
             $levcount++;
         }
         ////////////////////////////////////////
         $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . implode(',', $prevarr) . ")");
         $this->db->query();
         $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . implode(',', $prevarr) . ")";
         $this->db->setQuery($query);
         $mcids = $this->db->loadColumn();
         if (count($mcids)) {
             $cids = implode(',', $mcids);
             $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
             $this->db->query();
         }
     } else {
         $arr_match = array();
         if (isset($_POST['home_team']) && count($_POST['home_team'])) {
             foreach ($_POST['home_team'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load($_POST['match_id'][$mj]);
                 if (!$this->t_type) {
                     /////!!!!!!!!!!!!!!!!!!!!!!!!!!!WARNING
                     $match->m_id = $row->id;
                     $match->team1_id = intval($home_team);
                     $match->team2_id = intval($_POST['away_team'][$mj]);
                     if ($this->acl != 1 && $jsmr_mark_played == 0 && $match->m_played == 1) {
                     } else {
                         $match->score1 = intval($_POST['home_score'][$mj]);
                         $match->score2 = intval($_POST['away_score'][$mj]);
                     }
                     $match->is_extra = isset($_POST['extra_time'][$mj]) ? intval($_POST['extra_time'][$mj]) : 0;
                     $match->published = 1;
                 }
                 if ($this->acl != 1 && $jsmr_mark_played == 0 && $match->m_played == 1) {
                 } else {
                     $match->m_played = intval($_POST['match_played'][$mj]);
                 }
                 $match->m_date = strval($_POST['match_data'][$mj]);
                 $match->venue_id = intval($_POST['venue_id'][$mj]);
                 $match->m_time = strval($_POST['match_time'][$mj]);
                 if ($this->acl == 3 && $row->s_id == -1) {
                     $match->m_single = 1;
                 }
                 if ($this->acl == 3) {
                     $query = "SELECT COUNT(*) FROM #__bl_players WHERE usr_id=" . $this->_user->id . " AND (id=" . $match->team1_id . " OR id=" . $match->team2_id . ")";
                     $this->db->setQuery($query);
                     if (!$this->db->loadResult()) {
                         JError::raiseError(500, $match->getError());
                     }
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . intval($home_team);
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if ($this->acl == 2) {
                     $query = "SELECT COUNT(*) FROM #__bl_teams as t, #__bl_moders as m WHERE m.tid=t.id AND m.uid=" . $user->id . " AND (t.id=" . $match->team1_id . " OR t.id=" . $match->team2_id . ")";
                     $this->db->setQuery($query);
                     if (!$this->db->loadResult()) {
                         JError::raiseError(500, $match->getError());
                     }
                 }
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $arr_match[] = $match->id;
                 $mj++;
             }
             if ($this->acl == 1) {
                 $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND m_id = " . $row->id);
                 $this->db->query();
                 $query = "SELECT id FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND m_id = " . $row->id;
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 if (count($mcids)) {
                     $cids = implode(',', $mcids);
                     $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                     $this->db->query();
                 }
             } else {
                 if ($this->acl == 2) {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->query();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if ($this->acl == 2) {
                     $this->db->setQuery("SELECT id FROM  #__bl_match WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("SELECT id FROM #__bl_match  WHERE id NOT IN (" . implode(',', $arr_match) . ") AND (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if (count($mcids)) {
                     $cids = implode(',', $mcids);
                     $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                     $efcid = $this->db->loadColumn();
                     if (count($efcid)) {
                         $efcids = implode(',', $efcid);
                         $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN (" . $efcids . ") AND uid IN (" . $cids . ")");
                         $this->db->query();
                     }
                 }
             }
         } else {
             if ($this->acl != 1 && !$this->t_type) {
                 ///WARNING!!!!!!!!!!!!!!!!!!!!!
                 if ($this->acl == 2) {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("DELETE FROM #__bl_match WHERE (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->query();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if ($this->acl == 2) {
                     $this->db->setQuery("SELECT id FROM #__bl_match WHERE (team1_id = " . $tid . " OR team2_id=" . $tid . ") AND m_id = " . $row->id);
                 } else {
                     $this->db->setQuery("SELECT id FROM #__bl_match WHERE (team1_id = " . $playerid . " OR team2_id=" . $playerid . ") AND m_id = " . $row->id);
                 }
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 $error = $this->db->getErrorMsg();
                 if ($error) {
                     return JError::raiseError(500, $error);
                 }
                 if (count($mcids)) {
                     $cids = implode(',', $mcids);
                     $query = "SELECT id FROM #__bl_match WHERE m_id IN (" . $cids . ")";
                     $this->db->setQuery($query);
                     $mcids = $this->db->loadColumn();
                     $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                     $this->db->query();
                     $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                     $efcid = $this->db->loadColumn();
                     if (count($efcid)) {
                         $efcids = implode(',', $efcid);
                         $mcids = implode(',', $mcids);
                         $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN (" . $efcids . ") AND uid IN (" . $cids . ")");
                         $this->db->query();
                     }
                 }
             } elseif ($this->acl == 1) {
                 $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id;
                 $this->db->setQuery($query);
                 $mcids = $this->db->loadColumn();
                 $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id = " . $row->id);
                 $this->db->query();
                 $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                 $efcid = $this->db->loadColumn();
                 if (count($efcid) && count($mcids)) {
                     $efcids = implode(',', $efcid);
                     $mcids = implode(',', $mcids);
                     $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN (" . $efcids . ") AND uid IN (" . $mcids . ")");
                     $this->db->query();
                 }
             }
         }
     }
     $this->id = $row->id;
     $this->tid = $tid;
     $this->mid = $mid;
     $this->season_id = $sid;
 }
 public function saveMday()
 {
     $t_single = JRequest::getVar('t_single', 0, 'post', 'int');
     $t_knock = JRequest::getVar('t_knock', 0, 'post', 'int');
     $post = JRequest::get('post');
     $post['k_format'] = JRequest::getVar('format_post', 0, 'post', 'int');
     $post['t_type'] = JRequest::getVar('t_knock', 0, 'post', 'int');
     $post['m_descr'] = JRequest::getVar('m_descr', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $row = new JTableMday($this->db);
     if (!$row->bind($post)) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->check()) {
         JError::raiseError(500, $row->getError());
     }
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     $row->checkin();
     // save match
     $mj = 0;
     $prevarr = array();
     if ($t_knock) {
         if (isset($_POST['teams_kn']) && count($_POST['teams_kn'])) {
             foreach ($_POST['teams_kn'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['match_id'][$mj]) ? $_POST['match_id'][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 //update
                 $match->team2_id = intval($_POST['teams_kn_aw'][$mj]);
                 if ($_POST['res_kn_1'][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_1'][$mj]);
                 }
                 if ($_POST['res_kn_1_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_1_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = 1;
                 if (isset($_POST['kn_match_played_' . $mj])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj]);
                 } else {
                     $match->m_played = 0;
                 }
                 if (!isset($_POST['res_kn_1'][$mj]) || !isset($_POST['res_kn_1_aw'][$mj]) || $_POST['res_kn_1'][$mj] == '' || $_POST['res_kn_1_aw'][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $prevarr[] = $match->id;
                 $mj++;
             }
         }
         $levcount = 2;
         while (isset($_POST['teams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['teams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 2) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } else {
                     if ($_POST['final']) {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj * 2]) ? $_POST['lmatches_' . ($levcount - 1)][$mj * 2] : 0);
                     } else {
                         $match_1 = new JTableMatch($this->db);
                         $match_1->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2] : 0);
                         $match_2 = new JTableMatch($this->db);
                         $match_2->load(isset($_POST['matches_' . ($levcount - 1)][$mj * 2 + 1]) ? $_POST['matches_' . ($levcount - 1)][$mj * 2 + 1] : 0);
                     }
                 }
                 //print_R($match_2);die;
                 //if((isset($match_1->id) && isset($match_2->id)) || ($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['matches_' . $levcount][$mj]) ? $_POST['matches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['teams_kn_aw_' . $levcount][$mj]);
                 //$match->k_type = intval($_POST['lk_type'.$levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 //var_dump($match);die();
                 if ($_POST['res_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['res_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['res_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['kn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['kn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['res_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['res_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['res_kn_' . $levcount][$mj] && !$_POST['res_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 // }
             }
             $levcount++;
         }
         //////////////////
         $levcount = 1;
         while (isset($_POST['lteams_kn_' . $levcount])) {
             $mj = 0;
             foreach ($_POST['lteams_kn_' . $levcount] as $home_team) {
                 if ($levcount == 1) {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['match_id'][$mj * 2]) ? $_POST['match_id'][$mj * 2] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['match_id'][$mj * 2 + 1]) ? $_POST['match_id'][$mj * 2 + 1] : 0);
                 } elseif ($levcount % 2 == 0) {
                     $match_1 = new JTableMatch($this->db);
                     $num = intval($levcount / 4) ? ceil($levcount / 4) : floor($levcount / 4);
                     if ($levcount == 8 || $levcount == 10) {
                         $num += 1;
                     }
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['matches_' . ($levcount - $num)][$mj]) ? $_POST['matches_' . ($levcount - $num)][$mj] : 0);
                 } else {
                     $match_1 = new JTableMatch($this->db);
                     $match_1->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj]) ? $_POST['lmatches_' . ($levcount - 1)][$mj] : 0);
                     $match_2 = new JTableMatch($this->db);
                     $match_2->load(isset($_POST['lmatches_' . ($levcount - 1)][$mj + 1]) ? $_POST['lmatches_' . ($levcount - 1)][$mj + 1] : 0);
                 }
                 //////////////&&&&
                 //if(($match_1->id && $match_2->id) || ($match_1->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_2->m_played && $match_1->team1_id !=0 && $match_1->team2_id !=0) || ($match_1->team1_id == -1 && $match_1->team2_id != 0) || ($match_1->team2_id == -1 && $match_1->team1_id != 0) || ($match_2->team1_id == -1 && $match_2->team2_id != 0) || ($match_2->team2_id == -1 && $match_2->team1_id != 0)){
                 $match = new JTableMatch($this->db);
                 $match->load(isset($_POST['lmatches_' . $levcount][$mj]) ? $_POST['lmatches_' . $levcount][$mj] : 0);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 if (!$match->team1_id && ($match_1->m_played && $match_1->team1_id != 0 && $match_1->team2_id != 0 || $match_1->team1_id == -1 || $match_1->team2_id == -1)) {
                     if ($match_1->team1_id == -1) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->team2_id == -1) {
                             $match->team1_id = $match_1->team1_id;
                         }
                     }
                     if ($match_1->score1 > $match_1->score2) {
                         $match->team1_id = $match_1->team1_id;
                     } elseif ($match_1->score1 < $match_1->score2) {
                         $match->team1_id = $match_1->team2_id;
                     } else {
                         if ($match_1->aet1 > $match_1->aet2) {
                             $match->team1_id = $match_1->team1_id;
                         } elseif ($match_1->aet1 < $match_1->aet2) {
                             $match->team1_id = $match_1->team2_id;
                         } else {
                             if ($match_1->p_winner == $match_1->team1_id) {
                                 $match->team1_id = $match_1->team1_id;
                             } elseif ($match_1->p_winner == $match_1->team2_id) {
                                 $match->team1_id = $match_1->team2_id;
                             }
                         }
                     }
                 }
                 $match->team2_id = intval($_POST['lteams_kn_aw_' . $levcount][$mj]);
                 $match->k_type = intval($_POST['lk_type_' . $levcount][$mj]);
                 if (!$match->team2_id && ($match_2->m_played && $match_2->team1_id != 0 && $match_2->team2_id != 0 || $match_2->team1_id == -1 || $match_2->team2_id == -1)) {
                     if ($match_2->team1_id == -1) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->team1_id == -1) {
                             $match->team2_id = $match_2->team1_id;
                         }
                     }
                     if ($match_2->score1 > $match_2->score2) {
                         $match->team2_id = $match_2->team1_id;
                     } elseif ($match_2->score1 < $match_2->score2) {
                         $match->team2_id = $match_2->team2_id;
                     } else {
                         if ($match_2->aet1 > $match_2->aet2) {
                             $match->team2_id = $match_2->team1_id;
                         } elseif ($match_2->aet1 < $match_2->aet2) {
                             $match->team2_id = $match_2->team2_id;
                         } else {
                             if ($match_2->p_winner == $match_2->team1_id) {
                                 $match->team2_id = $match_2->team1_id;
                             } elseif ($match_2->p_winner == $match_2->team2_id) {
                                 $match->team2_id = $match_2->team2_id;
                             }
                         }
                     }
                 }
                 //var_dump($match);die();
                 if ($_POST['lres_kn_' . $levcount][$mj] != '') {
                     $match->score1 = intval($_POST['lres_kn_' . $levcount][$mj]);
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] != '') {
                     $match->score2 = intval($_POST['lres_kn_' . $levcount . '_aw'][$mj]);
                 }
                 $match->k_ordering = $mj;
                 $match->k_stage = $levcount;
                 if (isset($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = intval($_POST['lkn_match_played_' . $mj . '_' . $levcount]);
                 } else {
                     $match->m_played = 0;
                 }
                 if ($_POST['lres_kn_' . $levcount . '_aw'][$mj] == '' || $_POST['lres_kn_' . $levcount][$mj] == '') {
                     $match->m_played = 0;
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$_POST['lres_kn_' . $levcount][$mj] && !$_POST['lres_kn_' . $levcount . '_aw'][$mj]) {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 } else {
                     $match->m_played = isset($match->m_played) ? $match->m_played : 1;
                 }
                 /////////////////////////////////////////////////////////////////////////////
                 //if($match_1->m_played == 0 || $match_2->m_played == 0){          WARNING!!!!
                 //$match->m_played = 0;
                 // }////////////////////////////////////////////////////////////////////////////
                 //else{
                 // $match->m_played = 1;
                 //}
                 if (($match_1->team1_id == -1 || $match_1->team2_id == -1 || $match_2->team1_id == -1 or $match_2->team2_id == -1) && intval($_POST['lkn_match_played_' . $mj . '_' . $levcount])) {
                     $match->m_played = 1;
                 }
                 $match->published = 1;
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 $match->checkin();
                 $mj++;
                 $prevarr[] = $match->id;
                 // }
             }
             $levcount++;
         }
         /////////////////
         $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . (count($prevarr) ? implode(',', $prevarr) : '-1') . ")";
         $this->db->setQuery($query);
         $mcids = $this->db->loadColumn();
         $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id . " AND id NOT IN (" . (count($prevarr) ? implode(',', $prevarr) : '-1') . ")");
         $this->db->query();
         if (count($mcids)) {
             $cids = implode(',', $mcids);
             $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
             $this->db->query();
             $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
             $efcid = $this->db->loadColumn();
             if (count($efcid)) {
                 $efcids = implode(',', $efcid);
                 $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN ('" . $efcids . "') AND uid IN ('" . $cids . "')");
                 $this->db->query();
             }
         }
     } else {
         $arr_match = array();
         if (isset($_POST['home_team']) && count($_POST['home_team'])) {
             foreach ($_POST['home_team'] as $home_team) {
                 $match = new JTableMatch($this->db);
                 $match->load($_POST['match_id'][$mj]);
                 $match->m_id = $row->id;
                 $match->team1_id = intval($home_team);
                 $match->team2_id = intval($_POST['away_team'][$mj]);
                 if ($_POST['home_score'][$mj] != '') {
                     $match->score1 = intval($_POST['home_score'][$mj]);
                 }
                 if ($_POST['away_score'][$mj] != '') {
                     $match->score2 = intval($_POST['away_score'][$mj]);
                 }
                 $match->is_extra = isset($_POST['extra_time'][$mj]) ? intval($_POST['extra_time'][$mj]) : 0;
                 $match->published = 1;
                 $match->m_played = intval($_POST['match_played'][$mj]);
                 $match->m_date = strval($_POST['match_data'][$mj]);
                 $match->venue_id = intval($_POST['venue_id'][$mj]);
                 $match->m_time = strval($_POST['match_time'][$mj]);
                 $match->m_single = strval($_POST['matchtype'][$mj]);
                 //betting
                 $match->betavailable = intval($_POST['bet_available'][$mj]);
                 if ($match->betavailable) {
                     $betfinishdate = JRequest::getVar('betfinishdate', array(), 'post', 'array');
                     $betfinishtime = JRequest::getVar('betfinishtime', array(), 'post', 'array');
                     $match->betfinishdate = $betfinishdate[$mj];
                     $match->betfinishtime = $betfinishtime[$mj];
                 }
                 if (!$match->id) {
                     $query = "SELECT venue_id FROM #__bl_teams WHERE id=" . $match->team1_id;
                     $this->db->setQuery($query);
                     $venue = $this->db->loadResult();
                     if ($venue) {
                         $match->venue_id = $venue;
                     }
                 }
                 if (!$match->check()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if (!$match->store()) {
                     JError::raiseError(500, $match->getError());
                 }
                 if ($match->betavailable) {
                     $bet_coeff_old1 = JRequest::getVar('bet_coeff_old1', array(), 'post', 'array');
                     $bet_coeff_old2 = JRequest::getVar('bet_coeff_old2', array(), 'post', 'array');
                     if ($bet_coeff_old1[$match->id]) {
                         foreach ($bet_coeff_old1[$match->id] as $idevent => $coeff1) {
                             $coeff1 = floatval($coeff1);
                             $coeff2 = floatval($bet_coeff_old2[$match->id][$idevent]);
                             $bet = new JTableBettingCoeffs($this->db);
                             $bet->idmatch = $match->id;
                             $bet->idevent = $idevent;
                             $bet->load(array('idmatch' => $bet->idmatch, 'idevent' => $bet->idevent));
                             if (!$bet->id) {
                                 $bet->idmatch = $match->id;
                                 $bet->idevent = $idevent;
                             }
                             $bet->coeff1 = $coeff1;
                             $bet->coeff2 = $coeff2;
                             $bet->betfinishdate = $betfinishdate[$mj];
                             $bet->betfinishtime = $betfinishtime[$mj];
                             $bet->store();
                         }
                     }
                 }
                 $match->checkin();
                 $arr_match[] = $match->id;
                 $mj++;
             }
             $query = "SELECT id FROM #__bl_match WHERE id NOT IN (" . (count($arr_match) ? implode(',', $arr_match) : '-1') . ") AND m_id = " . $row->id;
             $this->db->setQuery($query);
             $mcids1 = $this->db->loadColumn();
             if (count($mcids1)) {
                 $mcids = implode(',', $mcids1);
                 $this->db->setQuery("DELETE p,ap FROM #__bl_photos as p, #__bl_assign_photos as ap WHERE p.id = ap.photo_id AND ap.cat_type = 3 AND ap.cat_id IN (" . $mcids . ")");
                 $this->db->query();
             }
             // print_R($mcids);die;
             $this->db->setQuery("DELETE FROM #__bl_match WHERE id NOT IN (" . (count($arr_match) ? implode(',', $arr_match) : '-1') . ") AND m_id = " . $row->id);
             $this->db->query();
             if (count($mcids1)) {
                 $cids = implode(',', $mcids1);
                 $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id IN (" . $cids . ")");
                 $this->db->query();
                 $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
                 $efcid = $this->db->loadColumn();
                 if (count($efcid)) {
                     $efcids = implode(',', $efcid);
                     $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN ('" . $efcids . "') AND uid IN ('" . $cids . "')");
                     $this->db->query();
                 }
             }
         } else {
             $query = "SELECT id FROM #__bl_match WHERE m_id = " . $row->id;
             $this->db->setQuery($query);
             $mcids = $this->db->loadColumn();
             $this->db->setQuery("DELETE FROM #__bl_match WHERE m_id = " . $row->id);
             $this->db->query();
             //
             if (count($mcids)) {
                 $mcids = implode(',', $mcids);
                 $this->db->setQuery("DELETE p,ap FROM #__bl_photos as p, #__bl_assign_photos as ap WHERE p.id = ap.photo_id AND ap.cat_type = 3 AND ap.cat_id IN (" . $mcids . ")");
                 $this->db->query();
             }
             //print_r($mcids);die;
             $this->db->setQuery("DELETE FROM #__bl_squard WHERE match_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_match_events WHERE match_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_subsin WHERE match_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("DELETE FROM #__bl_mapscore WHERE m_id = " . $row->id);
             $this->db->query();
             $this->db->setQuery("SELECT id FROM #__bl_extra_filds WHERE type='2'");
             $efcid = $this->db->loadColumn();
             if (count($efcid) && count($mcids)) {
                 $efcids = implode(',', $efcid);
                 $mcids = implode(',', $mcids);
                 $this->db->setQuery("DELETE FROM #__bl_extra_values WHERE f_id IN ('" . $efcids . "') AND uid IN ('" . $mcids . "')");
                 $this->db->query();
             }
         }
     }
     $this->_id = $row->id;
 }