<?php

/*

Old updates, before 2011!

*/

if(empty($version)) {
	$MySql = "CREATE TABLE IF NOT EXISTS `BackNumber` (
		`BnTournament` int(10) unsigned NOT NULL,
		`BnFinal` tinyint(4) NOT NULL,
		`BnHeight` smallint(5) unsigned NOT NULL,
		`BnWidth` smallint(5) unsigned NOT NULL,
		`BnBackground` mediumblob NOT NULL,
		`BnBgX` smallint(6) NOT NULL,
		`BnBgY` smallint(6) NOT NULL,
		`BnBgW` smallint(6) NOT NULL,
		`BnBgH` smallint(6) NOT NULL,
		`BnTargetNo` tinyint(3) unsigned NOT NULL,
		`BnTnoColor` varchar(6) NOT NULL default '000000',
		`BnTnoSize` smallint(6) NOT NULL,
		`BnTnoX` smallint(6) NOT NULL,
		`BnTnoY` smallint(6) NOT NULL,
		`BnTnoW` smallint(6) NOT NULL,
		`BnTnoH` smallint(6) NOT NULL,
		`BnAthlete` tinyint(3) unsigned NOT NULL,
		`BnAthColor` varchar(6) NOT NULL default '000000',
		`BnAthSize` smallint(6) NOT NULL,
		`BnAthX` smallint(6) NOT NULL,
		`BnAthY` smallint(6) NOT NULL,
		`BnAthW` smallint(6) NOT NULL,
		`BnAthH` smallint(6) NOT NULL,
		`BnCountry` tinyint(3) unsigned NOT NULL,
		`BnCoColor` varchar(6) NOT NULL default '000000',
		`BnCoSize` smallint(6) NOT NULL,
		`BnCoX` smallint(6) NOT NULL,
		`BnCoY` smallint(6) NOT NULL,
		`BnCoW` smallint(6) NOT NULL,
		`BnCoH` smallint(6) NOT NULL,
		`BnOffsetX` smallint(6) NOT NULL,
		`BnOffsetY` smallint(6) NOT NULL,
		PRIMARY KEY  (`BnTournament`,`BnFinal`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	safe_w_sql($MySql);

	$MySql = "CREATE TABLE IF NOT EXISTS `Broadcast` (
		`BrTournament` int(10) unsigned NOT NULL,
		`BrEnabled` int(10) unsigned NOT NULL default '0',
		`BrSottoPancia0` tinytext NOT NULL,
		`BrSottoPancia1` tinytext NOT NULL,
		`BrSottoPancia2` tinytext NOT NULL,
		`BrSottoPancia3` tinytext NOT NULL,
		`BrEvent` varchar(4) NOT NULL,
		`BrMatchNo` tinyint(3) unsigned NOT NULL,
		`BrCommentL` tinytext NOT NULL,
		`BrCommentR` tinytext NOT NULL,
		`BrTimeStamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
		PRIMARY KEY  (`BrTournament`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	safe_w_sql($MySql);

	$MySql = "ALTER TABLE `Finals` ADD `FinSyncro` DATETIME NOT NULL AFTER `FinDateTime`";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "ALTER TABLE `Entries` ADD `EnDob` DATE NOT NULL AFTER `EnCtrlCode`";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "ALTER TABLE `TeamFinals` ADD `TfSyncro` DATETIME NOT NULL AFTER `TfDateTime`";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "ALTER TABLE `Teams` ADD `TeSubTeam` VARCHAR(1) NOT NULL DEFAULT '0' AFTER `TeCoId`";
	safe_w_sql($MySql, null, array(1060));
	$MySql = "ALTER TABLE `Teams` DROP PRIMARY KEY ,ADD PRIMARY KEY (`TeCoId`, `TeSubTeam`, `TeEvent`, `TeTournament`, `TeFinEvent`)";
	safe_w_sql($MySql, null, array(1060));
	$MySql = "ALTER TABLE `TeamComponent` ADD `TcSubTeam` VARCHAR(1) NOT NULL DEFAULT '0' AFTER `TcCoId`";
	safe_w_sql($MySql, null, array(1060));
	$MySql = "ALTER TABLE `TeamComponent` DROP PRIMARY KEY, ADD PRIMARY KEY (`TcCoId`, `TcSubTeam`, `TcTournament`, `TcEvent`, `TcId`, `TcFinEvent`)";
	safe_w_sql($MySql, null, array(1060));
	$MySql = "ALTER TABLE `TeamFinals` ADD `TfSubTeam` VARCHAR(1) NOT NULL DEFAULT '0' AFTER `TfTeam`";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "CREATE TABLE IF NOT EXISTS `Reviews` (
		`RevEvent` varchar(4) NOT NULL,
  		`RevMatchNo` tinyint(3) unsigned NOT NULL,
  		`RevTournament` int(10) unsigned NOT NULL,
  		`RevTeamEvent` tinyint(4) NOT NULL,
  		`RevLanguage1` text NOT NULL,
  		`RevLanguage2` text NOT NULL,
  		`RevDateTime` datetime NOT NULL default '0000-00-00 00:00:00',
  		`RevSyncro` datetime NOT NULL default '0000-00-00 00:00:00',
 		 PRIMARY KEY  (`RevEvent`,`RevMatchNo`,`RevTournament`,`RevTeamEvent`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	safe_w_sql($MySql);

	$MySql = "ALTER TABLE `Qualifications` CHANGE `QuClRank` `QuClRank` SMALLINT NOT NULL , CHANGE `QuRank` `QuRank` SMALLINT NOT NULL";
	safe_w_sql($MySql);

	$MySql = "DELETE FROM `TournamentDistances` WHERE `TdType` IN (6,7,8,9,10,11,12,13,14,15)";
	safe_w_sql($MySql);

	$MySql = "REPLACE INTO `TournamentDistances` (`TdClasses`, `TdType`, `Td1`, `Td2`, `Td3`, `Td4`, `Td5`, `Td6`, `Td7`, `Td8`) VALUES
		('AN__', 4, '60 m', '50 m', '40 m', '30 m', '', '', '', ''),
		('OLSM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('OLJM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('OLRM', 4, '50 m', '40 m', '30 m', '20 m', '', '', '', ''),
		('OLVM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('OLGM', 4, '30 m', '25 m', '20 m', '15 m', '', '', '', ''),
		('OLAM', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('OLGF', 4, '30 m', '25 m', '20 m', '15 m', '', '', '', ''),
		('OLVF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('OLRF', 4, '50 m', '40 m', '30 m', '20 m', '', '', '', ''),
		('OLJF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('OLSF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('OLAF', 4, '60 m', '50 m', '40 m', '30 m', '', '', '', ''),
		('COSM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('COJM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('CORM', 4, '50 m', '40 m', '30 m', '20 m', '', '', '', ''),
		('COVM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('COGM', 4, '30 m', '25 m', '20 m', '15 m', '', '', '', ''),
		('COAM', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('COGF', 4, '30 m', '25 m', '20 m', '15 m', '', '', '', ''),
		('COVF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('CORF', 4, '50 m', '40 m', '30 m', '20 m', '', '', '', ''),
		('COJF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('COSF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('COAF', 4, '60 m', '50 m', '40 m', '30 m', '', '', '', ''),
		('OLMM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('COMM', 4, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('OLMF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('COMF', 4, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('OLMM', 1, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('COMM', 1, '90 m', '70 m', '50 m', '30 m', '', '', '', ''),
		('OLMF', 1, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('COMF', 1, '70 m', '60 m', '50 m', '30 m', '', '', '', ''),
		('OLMM', 2, '90 m', '70 m', '50 m', '30 m', '90 m', '70 m', '50 m', '30 m'),
		('OLMF', 2, '70 m', '60 m', '50 m', '30 m', '70 m', '60 m', '50 m', '30 m'),
		('COMM', 2, '90 m', '70 m', '50 m', '30 m', '90 m', '70 m', '50 m', '30 m'),
		('COMF', 2, '70 m', '60 m', '50 m', '30 m', '70 m', '60 m', '50 m', '30 m'),
		('__M_', 3, '70m-1', '70m-2', '', '', '', '', '', ''),
		('%', 11, 'Perc.', '', '', '', '', '', '', ''),
		('%', 13, 'Perc.', 'Perc.', '', '', '', '', '', ''),
		('%', 14, 'Day 1', 'Day 2', 'Day 3', 'S.O.', '', '', '', ''),
		('__P_', 15, '10m-1', '10m-2', '', '', '', '', '', ''),
		('__G_', 15, '15m-1', '15m-2', '', '', '', '', '', ''),
		('__M_', 15, '20m-1', '20m-2', '', '', '', '', '', ''),
		('__F_', 15, '20m-1', '20m-2', '', '', '', '', '', ''),
		('%', 12, 'Hunter', 'Field', '', '', '', '', '', ''),
		('%', 10, 'Hunter', 'Field', '', '', '', '', '', ''),
		('%', 9, 'H&F', '', '', '', '', '', '', ''),
		('%', 8, '25m-1', '25m-2', '18m-1', '18m-2', '', '', '', ''),
		('%', 7, '25m-1', '25m-2', '', '', '', '', '', ''),
		('%', 6, '18m-1', '18m-2', '', '', '', '', '', ''),
		('__R_', 5, '40 m', '35 m', '30 m', '', '', '', '', ''),
		('__G_', 5, '25 m', '20 m', '15 m', '', '', '', '', ''),
		('__A_', 5, '60 m', '50 m', '40 m', '', '', '', '', ''),
		('__J_', 5, '60 m', '50 m', '40 m', '', '', '', '', ''),
		('__S_', 5, '60 m', '50 m', '40 m', '', '', '', '', ''),
		('__V_', 5, '60 m', '50 m', '40 m', '', '', '', '', ''),
		('__M_', 5, '60 m', '50 m', '40 m', '', '', '', '', '')";
	safe_w_sql($MySql);

	$MySql = "REPLACE INTO `TournamentType` (`TtId`, `TtName`, `TtNumDist`, `TtNumEnds`, `TtMaxDistScore`, `TtMaxFinIndScore`, `TtMaxFinTeamScore`, `TtElabTeam`, `TrElimination`, `TtGolds`, `TtXNine`) VALUES
		(9, 'Campagna 12+12', 1, 24, 432, 72, 144, 1, 1, '6+5', '6'),
		(12, 'Campagna 12+12', 2, 12, 216, 72, 144, 1, 1, '6+5', '6'),
		(10, 'Campagna 24+24', 2, 24, 432, 72, 144, 1, 1, '6+5', '6'),
		(11, '3D', 1, 20, 200, 40, 120, 2, 1, '10', 'X'),
		(13, '3D', 2, 20, 200, 40, 120, 2, 1, '10', 'X'),
		(14, 'La Vegas', 4, 10, 300, 0, 0, 0, 0, '10', 'X'),
		(15, 'G.d.G.', 2, 8, 240, 0, 0, 0, 0, '10', 'X')";
	safe_w_sql($MySql);

	$MySql = "REPLACE INTO `InvolvedType` (`ItId`, `ItDescription`, `ItJudge`, `ItDoS`) VALUES (1, 'Judge', 1, 0), (2, 'Dos', 0, 1), (3, 'OrgResponsible', 0, 0), (4, 'Jury', 0, 0);";
	safe_w_sql($MySql);

	$MySql = "ALTER TABLE `Events` ADD `EvPartialTeam` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0';";
	safe_w_sql($MySql, null, array(1060));

	$MySql="ALTER TABLE `Events` ADD `EvMultiTeam` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0';";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "ALTER TABLE `Tournament` ADD `ToBlock` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0'";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "ALTER TABLE `Eliminations` ADD `ElArrowString` VARCHAR( 36 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `ElXnine`";
	safe_w_sql($MySql, null, array(1060));

	$MySql="CREATE TABLE IF NOT EXISTS `HTTData` (
		`HtdEnId` int(10) unsigned NOT NULL,
		`HtdTargetNo` varchar(5) NOT NULL,
		`HtdDistance` tinyint(4) NOT NULL,
		`HtdArrowStart` tinyint(4) NOT NULL,
		`HtdArrowEnd` tinyint(4) NOT NULL,
		`HtdArrowString` varchar(6) NOT NULL,
		PRIMARY KEY  (`HtdTargetNo`,`HtdDistance`,`HtdArrowStart`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	safe_w_sql($MySql);

	$MySql="REPLACE INTO `Parameters` (`ParId`, `ParValue`) VALUES
		('HttMode', '0'),
		('HttFlg', 'NNNNNNNNNNNNNNNN'),
		('HttSeq', '0103011006'),
		('HttSes', '0'),
		('HttHost','192.168.1.1'),
		('HttPort','9001');";
	safe_w_sql($MySql);

	$MySql="ALTER TABLE `HTTData` ADD `HtdFinScheduling` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `HtdDistance` ,
ADD `HtdTeamEvent` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `HtdFinScheduling`;";
	safe_w_sql($MySql, null, array(1060));

	$MySql="ALTER TABLE `HTTData` DROP PRIMARY KEY ,
ADD PRIMARY KEY ( `HtdTargetNo` , `HtdDistance` , `HtdArrowStart` , `HtdFinScheduling` , `HtdTeamEvent` ) ;";
	safe_w_sql($MySql);

	$MySql="ALTER TABLE `HTTData` ADD `HtdEvent` VARCHAR( 4 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `HtdEnId`,
	 ADD `HtdMatchNo` TINYINT( 4 ) UNSIGNED NULL DEFAULT NULL AFTER `HtdEnId` ;";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "UPDATE `Parameters` SET `ParId` = 'LuePath',`ParValue` = 'http://www.fitarco-italia.org/gare/ianseo/Ianseo.php' WHERE  `ParId`='LuePath'";
	safe_w_sql($MySql);

	$MySql = "ALTER TABLE `TVOut` ADD `TVMaxPage` TINYINT UNSIGNED NOT NULL DEFAULT '0' AFTER `TVNumTeam`";
	safe_w_sql($MySql, null, array(1060));

	$MySql = "ALTER TABLE `TVOut` ADD `TVViewTeamComponents` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '1' AFTER `TVNameComplete` ";
	safe_w_sql($MySql, null, array(1060));

	$MySql="CREATE TABLE IF NOT EXISTS `TeamFinComponent` (
	  `TfcCoId` int(11) NOT NULL,
	  `TfcSubTeam` varchar(1) NOT NULL default '0',
	  `TfcTournament` int(11) NOT NULL,
	  `TfcEvent` varchar(4) NOT NULL,
	  `TfcId` int(10) unsigned NOT NULL,
	  `TfcOrder` tinyint(4) NOT NULL,
	  PRIMARY KEY  (`TfcCoId`,`TfcSubTeam`,`TfcTournament`,`TfcEvent`,`TfcId`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	safe_w_sql($MySql);

	$MySql="CREATE TABLE IF NOT EXISTS `AccColors` (
		`AcTournament` int(10) unsigned NOT NULL,
		`AcDivClass` varchar(4) NOT NULL,
		`AcColor` varchar(6) NOT NULL,
		`AcIsAthlete` tinyint(3) unsigned NOT NULL,
		PRIMARY KEY  (`AcTournament`,`AcDivClass`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	safe_w_sql($MySql);

	$MySql="CREATE TABLE IF NOT EXISTS `Photos` (
		`PhEnId` INT( 10 ) UNSIGNED NOT NULL ,
		`PhPhoto` BLOB NOT NULL ,
		PRIMARY KEY ( `PhEnId` )
		) ENGINE = MYISAM DEFAULT CHARSET=utf8;";
	safe_w_sql($MySql);

/*
 * Per aggiungere gli indici devo prima verificare che non esistano già.
 * Se sono indici la colonna Key ritornata da SHOW COLUMNS è diversa da ''
 */
	$MySql= "SHOW COLUMNS FROM Qualifications WHERE Field='QuSession' OR Field='QuTargetNo'";
	$rs=safe_w_sql($MySql);
	if (safe_num_rows($rs)==2)
	{
		while ($myRow=safe_fetch($rs))
		{
			if ($myRow->Key=='')	// non è indice quindi lo aggiungo
			{
				$MySql = "ALTER TABLE `Qualifications` ADD INDEX ( `" . $myRow->Field . "` ) ";
				safe_w_sql($MySql);
			}
		}
	}

	$MySql= "SHOW COLUMNS FROM Entries WHERE Field='EnDivision' OR Field='EnClass'";
	$rs=safe_r_sql($MySql);
	if (safe_num_rows($rs)==2)
	{
		while ($myRow=safe_fetch($rs))
		{
			if ($myRow->Key=='')	// non è indice quindi lo aggiungo
			{
				$MySql = "ALTER TABLE `Entries` ADD INDEX ( `" . $myRow->Field . "` ) ";
				safe_w_sql($MySql);
			}
		}
	}
	db_save_version('2009-05-04 00:00:00');
}

if($version<'2009-08-14 00:20:00') {
	safe_w_sql("ALTER TABLE Entries ADD INDEX CalcRank (`EnTournament`,`EnAthlete`,`EnStatus`)",false,array(1061));
	db_save_version('2009-08-14 00:20:00');
}
if($version<'2009-08-24 12:06:27') {
	safe_w_sql("update AccOperationType set AOTDescr=substr(AOTDescr,5)  WHERE LEFT(AOTDescr,1)='~'");
	safe_w_sql("update InvolvedType set ItDescription=substr(ItDescription,5)  WHERE LEFT(ItDescription,1)='~'");
	safe_w_sql("update Phases set PhDescr=substr(PhDescr,5)  WHERE LEFT(PhDescr,1)='~'");
	safe_w_sql("update Targets set TarDescr=substr(TarDescr,5) WHERE LEFT(TarDescr,1)='~'");
	db_save_version('2009-08-24 12:06:27');
}
if($version<'2009-09-01 12:42:57') {
	safe_w_sql("ALTER TABLE `Events` ADD `EvMixedTeam` TINYINT UNSIGNED NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `Events` ADD `EvRunning` TINYINT UNSIGNED NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `TournamentType` CHANGE `TrElimination` `TtElimination` TINYINT( 4 ) NOT NULL DEFAULT '0' COMMENT 'o: No Eliminations, 1: Elimination Allowed'", false, array(1054));
	safe_w_sql("ALTER TABLE `TournamentType` ADD `TtCategory` TINYINT( 4 ) NOT NULL DEFAULT '0' COMMENT '0: Other, 1: Outdoor, 2: Indoor, 4:Field, 8:3D' AFTER `TtMaxFinTeamScore`", false, array(1060));

	safe_w_sql("ALTER TABLE `Tournament` ADD `ToIntEvent` TINYINT UNSIGNED NOT NULL DEFAULT '0' AFTER `ToWhenTo`", false, array(1060));
	safe_w_sql("ALTER TABLE `Tournament` ADD `ToCurrency` VARCHAR( 8 ) NULL DEFAULT NULL AFTER `ToIntEvent`", false, array(1060));
	safe_w_sql("ALTER TABLE `Tournament` ADD `ToPrintLang` VARCHAR( 2 ) NULL DEFAULT NULL AFTER `ToCurrency`", false, array(1060));
	safe_w_sql("ALTER TABLE `Tournament` ADD `ToPrintChars` TINYINT UNSIGNED NOT NULL DEFAULT '0' AFTER `ToPrintLang`", false, array(1060));
	safe_w_sql("ALTER TABLE `Tournament` ADD `ToPrintPaper` TINYINT UNSIGNED NOT NULL DEFAULT '0' COMMENT '0: ansi A4, 1: Letter' AFTER `ToPrintChars`", false, array(1060));
	db_save_version('2009-09-01 12:42:57');
}
if($version<'2009-09-10 09:25:00') {
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcTitleReverse` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea0` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea1` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea2` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea3` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea4` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea5` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea6` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcArea7` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcAreaStar` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcTransport` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcAccomodation` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `AccColors` ADD `AcMeal` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	db_save_version('2009-09-10 09:25:00');
}
if($version<'2009-09-10 10:00:00') {
	safe_w_sql("DROP TABLE IF EXISTS `FinalReportA`");
	safe_w_sql("CREATE TABLE IF NOT EXISTS `FinalReportA` ("
		. "`FraQuestion` varchar(5) NOT NULL, "
		. "`FraTournament` int(10) unsigned NOT NULL, "
		. "`FraAnswer` text NOT NULL, "
		. "PRIMARY KEY  (`FraQuestion`,`FraTournament`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");

	safe_w_sql("DROP TABLE IF EXISTS `FinalReportQ`");
	safe_w_sql("CREATE TABLE IF NOT EXISTS `FinalReportQ` ( "
		. "`FrqId` varchar(5) NOT NULL, "
		. "`FrqStatus` tinyint(4) NOT NULL default '0', "
		. "`FrqQuestion` tinytext NOT NULL, "
		. "`FrqTip` text NOT NULL, "
		. "`FrqType` tinyint(4) NOT NULL, "
		. "`FrqOptions` varchar(200) NOT NULL, "
		. "PRIMARY KEY  (`FrqId`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");

	db_save_version('2009-09-10 10:00:00');
}

if($version<'2010-01-01 16:00:00') {
	safe_w_sql("ALTER TABLE `TournamentType` ADD `TtOrder` int(11) NOT NULL ", false, array(1060));
	safe_w_sql("ALTER TABLE `TournamentType` change `TtName` `TtName` varchar(25) NOT NULL ", false, array(1060));
	// aggiorna l'ordine cono step 10 così da poterci infilare in mezzo eventuali gare
	safe_w_sql("update `TournamentType` set TtName='Type_FITA',TtOrder=10 where TtId=1"); // FITA
	safe_w_sql("update `TournamentType` set TtName='Type_2xFITA',TtOrder=20 where TtId=2"); // Doppio FITA
	safe_w_sql("update `TournamentType` set TtName='Type_70m Round',TtOrder=40 where TtId=3"); // 70 mt Round
	safe_w_sql("update `TournamentType` set TtName='Type_FITA 72',TtOrder=30 where TtId=4"); // FITA 72
	safe_w_sql("update `TournamentType` set TtName='Type_900 Round',TtOrder=140 where TtId=5"); // 900 Round
	safe_w_sql("update `TournamentType` set TtName='Type_Indoor 18',TtOrder=50 where TtId=6"); // Indoor 18 m
	safe_w_sql("update `TournamentType` set TtName='Type_Indoor 25',TtOrder=60 where TtId=7"); // Indoor 25 m
	safe_w_sql("update `TournamentType` set TtName='Type_Indoor 25+18',TtOrder=70 where TtId=8"); // Indoor 25+18 m
	safe_w_sql("update `TournamentType` set TtName='Type_HF 12+12',TtOrder=90 where TtId=9"); // Campagna 12+12
	safe_w_sql("update `TournamentType` set TtName='Type_HF 24+24',TtOrder=110 where TtId=10"); // Campagna 24+24
	safe_w_sql("update `TournamentType` set TtName='Type_3D',TtOrder=120 where TtId=11"); // 3D
	safe_w_sql("update `TournamentType` set TtName='Type_HF 12+12',TtOrder=100 where TtId=12"); // Campagna 12+12 (2 distanze)
	safe_w_sql("update `TournamentType` set TtName='Type_3D',TtOrder=130 where TtId=13"); // 3D (2 distanze)
	safe_w_sql("update `TournamentType` set TtName='Type_Las Vegas',TtOrder=80 where TtId=14"); // La Vegas
	safe_w_sql("update `TournamentType` set TtName='Type_GiochiGioventu',TtOrder=150 where TtId=15"); // G.d.G.
	safe_w_sql("ALTER TABLE `Tournament` ADD `ToDbVersion` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER `ToTeamFinVxA`", false, array(1060));
	safe_w_sql("ALTER TABLE `Events` ADD `EvMatchMode` TINYINT NOT NULL DEFAULT '0'", false, array(1060));
	safe_w_sql("ALTER TABLE `Finals` ADD `FinSetScore` TINYINT NOT NULL DEFAULT '0' AFTER `FinScore`", false, array(1060));
	safe_w_sql("ALTER TABLE `Finals` ADD `FinSetPoints` VARCHAR(15) DEFAULT NULL AFTER `FinSetScore`", false, array(1060));

	safe_w_sql("CREATE TABLE IF NOT EXISTS `CasGrid` (
		`CGPhase` tinyint(3) unsigned NOT NULL COMMENT '1 o 2 a seconda della fase della gara',
		`CGRound` tinyint(3) unsigned NOT NULL,
		`CGMatchNo1` tinyint(3) unsigned NOT NULL,
		`CGMatchNo2` tinyint(3) unsigned NOT NULL,
		`CGGroup` tinyint(3) unsigned NOT NULL,
		PRIMARY KEY  (`CGPhase`,`CGRound`,`CGMatchNo1`,`CGMatchNo2`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;");

	safe_w_sql("REPLACE INTO `CasGrid` (`CGPhase`, `CGRound`, `CGMatchNo1`, `CGMatchNo2`, `CGGroup`) VALUES
		(1, 1, 1, 8, 1),
		(1, 1, 2, 7, 2),
		(1, 1, 3, 6, 3),
		(1, 1, 4, 5, 4),
		(1, 1, 9, 13, 4),
		(1, 1, 10, 14, 3),
		(1, 1, 11, 15, 2),
		(1, 1, 12, 16, 1),
		(1, 2, 1, 12, 1),
		(1, 2, 2, 11, 2),
		(1, 2, 3, 10, 3),
		(1, 2, 4, 9, 4),
		(1, 2, 5, 13, 4),
		(1, 2, 6, 14, 3),
		(1, 2, 7, 15, 2),
		(1, 2, 8, 16, 1),
		(1, 3, 1, 16, 1),
		(1, 3, 2, 15, 2),
		(1, 3, 3, 14, 3),
		(1, 3, 4, 13, 4),
		(1, 3, 5, 9, 4),
		(1, 3, 6, 10, 3),
		(1, 3, 7, 11, 2),
		(1, 3, 8, 12, 1),
		(2, 1, 1, 8, 1),
		(2, 1, 2, 7, 2),
		(2, 1, 3, 6, 3),
		(2, 1, 4, 5, 4),
		(2, 1, 9, 13, 4),
		(2, 1, 10, 14, 3),
		(2, 1, 11, 15, 2),
		(2, 1, 12, 16, 1),
		(2, 2, 1, 12, 1),
		(2, 2, 2, 11, 2),
		(2, 2, 3, 10, 3),
		(2, 2, 4, 9, 4),
		(2, 2, 5, 13, 4),
		(2, 2, 6, 14, 3),
		(2, 2, 7, 15, 2),
		(2, 2, 8, 16, 1),
		(2, 3, 1, 16, 1),
		(2, 3, 2, 15, 2),
		(2, 3, 3, 14, 3),
		(2, 3, 4, 13, 4),
		(2, 3, 5, 9, 4),
		(2, 3, 6, 10, 3),
		(2, 3, 7, 11, 2),
		(2, 3, 8, 12, 1);"
	);

	safe_w_sql("CREATE TABLE IF NOT EXISTS `CasGroupMatch` (
		  `CaGMGroup` tinyint(3) unsigned NOT NULL,
		  `CaGRank` tinyint(4) unsigned NOT NULL,
		  `CaGMMatchNo` tinyint(3) unsigned NOT NULL,
		  PRIMARY KEY  (`CaGMGroup`,`CaGMMatchNo`,`CaGRank`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);

	safe_w_sql("REPLACE INTO `CasGroupMatch` (`CaGMGroup`, `CaGRank`, `CaGMMatchNo`) VALUES
		(1, 1, 1),
		(1, 2, 2),
		(1, 3, 3),
		(1, 4, 4),
		(2, 4, 5),
		(2, 3, 6),
		(2, 1, 7),
		(2, 2, 8),
		(3, 4, 9),
		(3, 3, 10),
		(3, 2, 11),
		(3, 1, 12),
		(4, 4, 13),
		(4, 3, 14),
		(4, 1, 15),
		(4, 2, 16);"
	);

	safe_w_sql("CREATE TABLE IF NOT EXISTS `CasRankMatch` (
		  `CRMEventPhase` tinyint(4) NOT NULL,
		  `CRMRank` tinyint(4) NOT NULL,
		  `CRMMatchNo` tinyint(3) unsigned NOT NULL,
		  PRIMARY KEY  (`CRMEventPhase`,`CRMRank`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);

	safe_w_sql("REPLACE INTO `CasRankMatch` (`CRMEventPhase`, `CRMRank`, `CRMMatchNo`) VALUES
		(16, 1, 1),
		(16, 2, 2),
		(16, 3, 3),
		(16, 4, 4),
		(16, 5, 5),
		(16, 6, 6),
		(16, 7, 7),
		(16, 8, 8),
		(16, 9, 9),
		(16, 10, 10),
		(16, 11, 11),
		(16, 12, 12),
		(16, 13, 13),
		(16, 14, 14),
		(16, 15, 15),
		(16, 16, 16);"
	);

	safe_w_sql("CREATE TABLE IF NOT EXISTS `CasScore` (
		  `CaSTournament` int(10) unsigned NOT NULL default '0',
		  `CaSPhase` tinyint(3) unsigned NOT NULL default '0' COMMENT '1=fase 1;2=fase2',
		  `CaSRound` tinyint(3) unsigned NOT NULL,
		  `CaSMatchNo` tinyint(3) unsigned NOT NULL,
		  `CaSEventCode` varchar(4) NOT NULL,
		  `CaSTarget` varchar(2) NOT NULL,
		  `CaSScore` smallint(6) NOT NULL default '0',
		  `CaSTie` tinyint(1) NOT NULL default '0',
		  `CaSArrowString` varchar(24) NOT NULL,
		  `CaSArrowPosition` varchar(240) NOT NULL,
		  `CaSTiebreak` varchar(9) NOT NULL,
		  `CaSTiePosition` varchar(90) NOT NULL,
		  `CaSPoints` tinyint(4) NOT NULL default '0',
		  PRIMARY KEY  (`CaSTournament`,`CaSPhase`,`CaSMatchNo`,`CaSEventCode`,`CaSRound`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);

	safe_w_sql("CREATE TABLE IF NOT EXISTS `CasTeam` (
		  `CaTournament` int(10) unsigned NOT NULL default '0',
		  `CaPhase` tinyint(3) unsigned NOT NULL default '0' COMMENT '1=fase 1;2=fase2',
		  `CaMatchNo` tinyint(3) unsigned NOT NULL default '0',
		  `CaEventCode` varchar(4) NOT NULL,
		  `CaTeam` int(11) unsigned NOT NULL default '0',
		  `CaSubTeam` varchar(1) NOT NULL,
		  `CaRank` tinyint(4) unsigned NOT NULL,
		  `CaTiebreak` varchar(9) NOT NULL,
		  PRIMARY KEY  (`CaTournament`,`CaPhase`,`CaMatchNo`,`CaEventCode`,`CaTeam`,`CaSubTeam`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;"
	);
	safe_w_sql("ALTER TABLE `Tournament` change `ToPrintLang` `ToPrintLang` varchar (5) NOT NULL ", false, array(1060));
	safe_w_sql("ALTER TABLE `Events` ADD `EvMatchArrowsNo` TINYINT UNSIGNED NOT NULL DEFAULT '0'", false, array(1060));

	safe_w_sql("CREATE TABLE IF NOT EXISTS `ClubTeam` ( "
		. "`CTTournament` int(10) unsigned NOT NULL default '0', "
		. "`CTPhase` tinyint(3) unsigned NOT NULL default '0' COMMENT '1=fase 1;2=fase2', "
		. "`CTMatchNo` tinyint(3) unsigned NOT NULL default '0', "
		. "`CTEventCode` varchar(4) NOT NULL, "
		. "`CTPrimary` tinyint(3) unsigned NOT NULL, "
		. "`CTTeam` int(11) unsigned NOT NULL default '0', "
		. "`CTSubTeam` varchar(1) NOT NULL, "
		. "`CTBonus` tinyint(3) unsigned NOT NULL default '0', "
		. "`CTRank` tinyint(4) unsigned NOT NULL, "
		. "`CTTiebreak` varchar(9) NOT NULL default '', "
		. "PRIMARY KEY  (`CTTournament`,`CTPhase`,`CTMatchNo`,`CTEventCode`,`CTPrimary`,`CTTeam`,`CTSubTeam`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");

	safe_w_sql("CREATE TABLE IF NOT EXISTS `ClubTeamGrid` ( "
		. "`CTGPhase` tinyint(3) unsigned NOT NULL COMMENT '1 o 2 a seconda della fase della gara', "
		. "`CTGRound` tinyint(3) unsigned NOT NULL, "
		. "`CTGMatchNo1` tinyint(3) unsigned NOT NULL, "
		. "`CTGMatchNo2` tinyint(3) unsigned NOT NULL, "
		. "`CTGGroup` tinyint(3) unsigned NOT NULL, "
		. "PRIMARY KEY  (`CTGPhase`,`CTGRound`,`CTGMatchNo1`,`CTGMatchNo2`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");

	safe_w_sql("CREATE TABLE IF NOT EXISTS `ClubTeamGroupMatch` ( "
		. "`CTGMGroup` tinyint(3) unsigned NOT NULL, "
		. "`CTGMMatchNo` tinyint(3) unsigned NOT NULL, "
		. "PRIMARY KEY  (`CTGMGroup`,`CTGMMatchNo`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");

	safe_w_sql("CREATE TABLE IF NOT EXISTS `ClubTeamRankMatch` ( "
		. "`CTRMEventPhase` tinyint(4) NOT NULL, "
		. "`CTRMRank` tinyint(4) NOT NULL, "
		. "`CTRMMatchNo` tinyint(3) unsigned NOT NULL, "
		. "PRIMARY KEY  (`CTRMEventPhase`,`CTRMRank`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");


	safe_w_sql("CREATE TABLE IF NOT EXISTS `ClubTeamScore` ( "
		. "`CTSTournament` int(10) unsigned NOT NULL default '0', "
		. "`CTSPhase` tinyint(3) unsigned NOT NULL default '0' COMMENT '1=fase 1;2=fase2', "
		. "`CTSRound` tinyint(3) unsigned NOT NULL, "
		. "`CTSMatchNo` tinyint(3) unsigned NOT NULL, "
		. "`CTSEventCode` varchar(4) NOT NULL, "
		. "`CTSPrimary` tinyint(3) unsigned NOT NULL, "
		. "`CTSTarget` varchar(2) NOT NULL, "
		. "`CTSScore` smallint(6) NOT NULL default '0', "
		. "`CTSTie` tinyint(1) NOT NULL default '0', "
		. "`CTSArrowString` varchar(24) NOT NULL, "
		. "`CTSArrowPosition` varchar(240) NOT NULL, "
		. "`CTSTiebreak` varchar(9) NOT NULL, "
		. "`CTSTiePosition` varchar(90) NOT NULL, "
		. "`CTSPoints` tinyint(4) NOT NULL default '0', "
		. "PRIMARY KEY  (`CTSTournament`,`CTSPhase`,`CTSMatchNo`,`CTSEventCode`,`CTSRound`,`CTSPrimary`) "
		. ") ENGINE=MyISAM DEFAULT CHARSET=utf8");

	safe_w_sql("REPLACE INTO `ClubTeamGrid` (`CTGPhase`, `CTGRound`, `CTGMatchNo1`, `CTGMatchNo2`, `CTGGroup`) VALUES (1, 1, 1, 8, 1),(1, 1, 2, 7, 2),(1, 1, 3, 6, 3),(1, 1, 4, 5, 4),(1, 1, 9, 16, 1),(1, 1, 10, 15, 2),(1, 1, 11, 14, 3),(1, 1, 12, 13, 4),(1, 2, 1, 9, 1),(1, 2, 2, 10, 2),(1, 2, 3, 11, 3),(1, 2, 4, 12, 4),(1, 2, 5, 13, 4),(1, 2, 6, 14, 3),(1, 2, 7, 15, 2),(1, 2, 8, 16, 1),(1, 3, 1, 16, 1),(1, 3, 2, 15, 2),(1, 3, 3, 14, 3),(1, 3, 4, 13, 4),(1, 3, 5, 12, 4),(1, 3, 6, 11, 3),(1, 3, 7, 10, 2),(1, 3, 8, 9, 1),(2, 1, 1, 4, 1),(2, 1, 2, 3, 1),(2, 1, 5, 8, 2),(2, 1, 6, 7, 2),(2, 1, 9, 12, 3),(2, 1, 10, 11, 3),(2, 1, 13, 16, 4),(2, 1, 14, 15, 4),(3, 1, 1, 2, 1),(3, 1, 3, 4, 1),(3, 1, 5, 6, 2),(3, 1, 7, 8, 2),(3, 1, 9, 10, 3),(3, 1, 11, 12, 3),(3, 1, 13, 14, 4),(3, 1, 15, 16, 4)");
	safe_w_sql("REPLACE INTO `ClubTeamGroupMatch` (`CTGMGroup`, `CTGMMatchNo`) VALUES (1, 1),(1, 8),(1, 9),(1, 16),(2, 2),(2, 7),(2, 10),(2, 15),(3, 3),(3, 6),(3, 11),(3, 14),(4, 4),(4, 5),(4, 12),(4, 13)");
	safe_w_sql("REPLACE INTO `ClubTeamRankMatch` (`CTRMEventPhase`, `CTRMRank`, `CTRMMatchNo`) VALUES (8, 1, 1),(8, 2, 2),(8, 3, 7),(8, 4, 8),(8, 5, 9),(8, 6, 10),(8, 7, 15),(8, 8, 16),(16, 1, 1),(16, 2, 2),(16, 3, 3),(16, 4, 4),(16, 5, 5),(16, 6, 6),(16, 7, 7),(16, 8, 8),(16, 9, 9),(16, 10, 10),(16, 11, 11),(16, 12, 12),(16, 13, 13),(16, 14, 14),(16, 15, 15),(16, 16, 16)");

	db_save_version('2010-01-01 16:00:00');
}

if($version<'2010-01-17 15:00:00') {
	safe_w_sql("DROP TABLE IF EXISTS `TVContents`", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `TVContents` (
		`TVCId` int(11) NOT NULL,
		`TVCTournament` int(11) NOT NULL,
		`TVCName` varchar(50) NOT NULL,
		`TVCContent` mediumblob NOT NULL,
		`TVCMimeType` varchar(50) NOT NULL,
		`TVCTime` tinyint(4) NOT NULL,
		`TVCScroll` tinyint(4) NOT NULL,
		PRIMARY KEY (`TVCId`,`TVCTournament`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));
	safe_w_sql("INSERT INTO `TVContents` (`TVCId`, `TVCTournament`, `TVCName`, `TVCContent`, `TVCMimeType`, `TVCTime`, `TVCScroll`) VALUES
(1, -1, 'Logo Ianseo',  , 'image/gif', 5, 50);", false, array(1060));

	safe_w_sql("DROP TABLE IF EXISTS `TVRules`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `TVRules` (
		`TVRId` int(11) NOT NULL,
		`TVRTournament` int(11) NOT NULL,
		`TVRName` varchar(50) NOT NULL,
		`TV_TR_BGColor` varchar(7) NOT NULL,
		`TV_TRNext_BGColor` varchar(7) NOT NULL,
		`TV_TR_Color` varchar(7) NOT NULL,
		`TV_TRNext_Color` varchar(7) NOT NULL,
		`TV_Content_BGColor` varchar(7) NOT NULL,
		`TV_Page_BGColor` varchar(7) NOT NULL,
		`TV_TH_BGColor` varchar(7) NOT NULL,
		`TV_TH_Color` varchar(7) NOT NULL,
		`TV_THTitle_BGColor` varchar(7) NOT NULL,
		`TV_THTitle_Color` varchar(7) NOT NULL,
		`TV_Carattere` int(11) NOT NULL,
		PRIMARY KEY (`TVRId`,`TVRTournament`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;", false, array(1060));

	safe_w_sql("DROP TABLE IF EXISTS `TVParams`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `TVParams` (
		`TVPId` int(11) NOT NULL,
		`TVPTournament` int(11) NOT NULL,
		`TVPTimeStop` smallint(5) unsigned NOT NULL,
		`TVPTimeScroll` smallint(5) unsigned NOT NULL,
		`TVPNumRows` smallint(5) unsigned NOT NULL,
		`TVMaxPage` tinyint(3) unsigned NOT NULL,
		`TVPSession` tinyint(3) unsigned NOT NULL,
		`TVPViewNationName` tinyint(1) unsigned NOT NULL,
		`TVPNameComplete` tinyint(1) unsigned NOT NULL,
		`TVPViewTeamComponents` tinyint(1) unsigned NOT NULL,
		`TVPEventInd` varchar(255) NOT NULL,
		`TVPEventTeam` varchar(255) NOT NULL,
		`TVPPhasesInd` varchar(255) NOT NULL,
		`TVPPhasesTeam` varchar(255) NOT NULL,
		`TVPPage` varchar(10) NOT NULL,
		`TVPDefault` varchar(1) NOT NULL,
		`TVP_TR_BGColor` varchar(7) NOT NULL,
		`TVP_TRNext_BGColor` varchar(7) NOT NULL,
		`TVP_TR_Color` varchar(7) NOT NULL,
		`TVP_TRNext_Color` varchar(7) NOT NULL,
		`TVP_Content_BGColor` varchar(7) NOT NULL,
		`TVP_Page_BGColor` varchar(7) NOT NULL,
		`TVP_TH_BGColor` varchar(7) NOT NULL,
		`TVP_TH_Color` varchar(7) NOT NULL,
		`TVP_THTitle_BGColor` varchar(7) NOT NULL,
		`TVP_THTitle_Color` varchar(7) NOT NULL,
		`TVP_Carattere` smallint(5) unsigned NOT NULL,
		PRIMARY KEY (`TVPId`,`TVPTournament`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	safe_w_sql("DROP TABLE IF EXISTS `TVSequence`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `TVSequence` (
		`TVSId` int(11) NOT NULL,
		`TVSTournament` int(11) NOT NULL,
		`TVSRule` int(11) NOT NULL,
		`TVSContent` int(11) NOT NULL,
		`TVSCntSameTour` tinyint(4) NOT NULL,
		`TVSTime` tinyint(4) NOT NULL,
		`TVSScroll` tinyint(4) NOT NULL,
		`TVSTable` varchar(5) NOT NULL,
		`TVSOrder` tinyint(4) NOT NULL,
		PRIMARY KEY (`TVSId`,`TVSTournament`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	db_save_version('2010-01-17 15:00:00');
}

if($version<'2010-01-20 01:00:00') {
	//Agiungo i GDG Invernali
	safe_w_sql("REPLACE INTO `TournamentType` VALUES(16, 'Type_GiochiGioventuW', 2, 8, 240, 0, 0, 2, 0, 0, '10', '9', 160)");
	//Ri-Sistemo i Tipi per colpa del verbale arbitri
	safe_w_sql("UPDATE `TournamentType` SET `TtCategory`='1' WHERE `TtId` IN (1,2,3,4,5,15)");
	safe_w_sql("UPDATE `TournamentType` SET `TtCategory`='2' WHERE `TtId` IN (6,7,8,16)");
	safe_w_sql("UPDATE `TournamentType` SET `TtCategory`='4' WHERE `TtId` IN (9,10,12)");
	safe_w_sql("UPDATE `TournamentType` SET `TtCategory`='8' WHERE `TtId` IN (11,13)");
	safe_w_sql("UPDATE `TournamentType` SET `TtMaxFinTeamScore`='240' WHERE `TtCategory`='8'");
	//Rimando anche il verbale che male non fa
	safe_w_sql("REPLACE INTO `FinalReportQ` (`FrqId`, `FrqStatus`, `FrqQuestion`, `FrqTip`, `FrqType`, `FrqOptions`) VALUES "
		 . "('A', 15, 'Definizione Competizione', '', -1, ''), "
		 . "('a01', 15, 'Conforme al calendario', '', 2, ''), "
		 . "('a02', 15, 'Tutte le classi e divisioni ammesse', '', 2, ''), "
		 . "('a03', 15, 'in caso contrario, specificare', '', 1, '70|2'), "
		 . "('a04', 3, 'Direttore dei tiri', '', 3, 'non iscritto all''albo|iscritto all''albo|arbitro|arbitro e GdG'), "
		 . "('a05', 15, 'Giuria d''Appello', 'Pubblicata ad inizio gara; solo per gare nazionali ed internazionali', 2, ''), "
		 . "('a06', 15, 'Field Manager', 'Responsabile dell''organizzazione appositamente nominato per la gestione del campo', 2, ''), "
		 . "('a07', 15, 'Calendario', '', 3, 'Internazionale|Nazionale|Interregionale|Giovanile|Sperimentale'), "
		 . "('a08', 15, 'Squadre Nazionali straniere', 'Solo rappresentative ufficiali; non applicabile solo per eventi federali', 3, 'N.A.|No|Si'), "
		 . "('a09', 15, 'In caso affermativo, specificare', '', 1, '70|2'), "
		 . "('a10', 3, 'Segnalazione eventuali Record', '', 1, '70|10'), "
		 . "('a11', 15, 'Reclami', '', 1, '70|7'), "
		 . "('a12', 15, 'Programma della gara pervenuto alla Giuria di Gara', '', 3, 'No|Pubblicato|Si'), "
		 . "('a13', 15, 'Tassa ridotta per le classi giovanili', '50% o differente se stabilito da apposito regolamento (es: Campionati Italiani)', 3, 'Come da Regolamento|Non Applicabile|No|Gratuita'), "
		 . "('a14', 0, 'Numero Allegati', '', 0, '5'), "
		 . "('E', 15, 'Disposizione e attrezzatura del campo ', '', -1, ''), "
		 . "('e01', 15, 'Distanze in tolleranza', 'Distanze tra linea di tiro e bersagli', 2, ''), "
		 . "('e02', 3, 'Separazione interasse conforme al regolamento', '', 2, ''), "
		 . "('e03', 3, 'Linea di tiro', 'Temporanea = Nastro Fissa = autobloccanti o equivalente per indoor', 3, 'Assente|Temporanea|Fissa'), "
		 . "('e04', 3, 'Numeri sulla linea di Tiro ', 'tra 1 e 2 metri', 2, ''), "
		 . "('e05', 3, 'Linea dei tre metri', '', 3, 'Presente|Assente'), "
		 . "('e06', 3, 'Linea Stampa/Media', '', 2, ''), "
		 . "('e08', 3, 'Linea di attesa conforme al regolamento', '', 2, ''), "
		 . "('e09', 3, 'Corridoi ', 'Tra i paglioni ove previsto dal regolamento', 3, 'Presente|Assente|Non Applicabile'), "
		 . "('e10', 7, 'Battifreccia', '', 3, 'Usurati|Nuovi'), "
		 . "('e11', 7, 'Materiale Battifreccia', '', 3, 'Sintetico|Paglia'), "
		 . "('e12', 3, 'Supporti battifreccia', 'cavalletti', 3, 'Improvvisati|Triangolari|Rettangolari'), "
		 . "('e13', 7, 'Assicurazione battifreccia con tiranti', 'Per indoor = tirante tra paglione e cavalletto', 3, 'Non Applicabile|No|Si'), "
		 . "('e14', 7, 'Angolazione Battifreccia conforme al regolamento', 'per hf intendersi perpendicolarità del bersaglio rispetto alla posizione di tiro', 2, ''), "
		 . "('e15', 7, 'Disposizione visuali conforme', '', 2, ''), "
		 . "('e16', 1, 'Bandiere segnavento', '', 3, 'Assenti|Presenti non ben visibili|Presenti ben visibili'), "
		 . "('e17', 3, 'Numeratori dei battifreccia', 'numeri dei paglioni', 3, 'Assenti|Presenti non conformi|Presenti conformi'), "
		 . "('e18', 1, 'Maniche a vento', '', 3, 'Non Applicabile|Assente|Si 1|Si 2 o più'), "
		 . "('e19', 3, 'Impianto semaforico ', 'Per automatico si intende la temporizzazione e la sincronizzazione delle luci dopo la chiamata tranne che per un eventuale termine anticipato\r\nNon a norma di sicurezza=deferimento automatico', 3, 'Assenti|Non a norma di sicurezza|Manuale|Automatico'), "
		 . "('e20', 3, 'Sistemi visivi ausiliari', 'Bandierine ', 2, ''), "
		 . "('e21', 3, 'Indicatori di sequenza ', '(AB/CD) Non necessari per turno unico', 3, 'Non Necessari|Assenti|Manuale|Automatico'), "
		 . "('e22', 3, 'Orologi contasecondi', '', 2, ''), "
		 . "('e23', 3, 'Indicatori acustici', 'Automatico se sincronizzato automaticamente con l''impianto semforico', 3, 'Manuale|Automatico'), "
		 . "('e24', 3, 'Indicatori individuali di punteggio per la fase di qualifica', 'flip board o altri indicatori di punteggio', 2, ''), "
		 . "('e25', 15, 'Indicatori di punteggio per le fasi finali', 'Manuali ed automatici= utilizzati i manuali per le eliminatorie e elettronici solo per le finali a medaglia', 3, 'Non applicabile|Assenti|Manuali|Manuali ed Automatici|Automatici'), "
		 . "('e26', 3, 'Sedie e ombrelloni per gli arbitri', 'Nell''indoor solo sedie', 2, ''), "
		 . "('e27', 3, 'Postazione direttore dei tiri rialzata', '', 2, ''), "
		 . "('e28', 1, 'Blind per le finali', 'Protezione per gli arbitri in prossimità del bersaglio', 2, 'Non Applicabile|Si|No'), "
		 . "('e29', 1, 'Campo di tiri di allenamento', '', 3, 'Non necessario|Assente|Utilizzate parti del campo di gara|Campo separato con orientamento differente|Campo separato con stesso orientamento '), "
		 . "('e30', 15, 'Campo per la finale', 'Campo per le finali singole', 3, 'Non necessario|Utilizzata parte del campo di gara|Campo apposito'), "
		 . "('e31', 15, 'Spazio per il pubblico', 'Per HF e 3D da intendersi solo per le fasi finali', 3, 'No|Spazio libero|Sedie|Tribune della struttura|Tribune appositamente realizzate'), "
		 . "('e32', 12, 'Campo tiri di prova', '', 4, 'No|Si|Distante dal punto di raduno|In prossimità del punto di raduno|Sufficiente per tutti gli arcieri|Necessità di turnazione'), "
		 . "('e33', 12, 'Picchetto dello stop', '', 2, ''), "
		 . "('e34', 12, 'Percorso alternativo', 'per raggiungere le piazzole senza attraversare il percorso di gara', 2, ''), "
		 . "('e35', 12, 'Distanza campo base / piazzola più lontana', 'Tempo di percorrenza', 3, 'Oltre i 15 minuti|Entro i 15 minuti'), "
		 . "('e36', 12, 'Interferenze', '', 4, 'Al volo delle frecce|Alla visione del bersaglio|All''arco|No'), "
		 . "('e37', 4, 'Distanze conosciute', 'Rispetto delle distanze e del numero dei bersagli per tipo indicati nel regolamento', 2, ''), "
		 . "('e38', 12, 'Distanze sconosciute', '', 3, 'Inferiori alla media|nella media|Superiori alla media'), "
		 . "('e39', 12, 'Visibilità dei picchetti di tiro', 'dallo stop', 2, ''), "
		 . "('e40', 12, 'Pendenze medie', 'indicativo', 3, 'zero|Meno di 15°|Più di 15°'), "
		 . "('F', 15, 'Sicurezza', '', -1, ''), "
		 . "('f01', 15, 'Dietro la linea delle visuali', '', 3, 'Non Presidiata|Presidiata|Assoluta'), "
		 . "('f02', 15, 'Accesso al campo', 'libero=nessun controllo; regolato=con pass nel rispetto del regolamento', 3, 'Libero|Regolato'), "
		 . "('f03', 12, 'Problemi generali di sicurezza del percorso', '', 4, 'Tiri incrociati|Direzioni di tiro verso altre piazzole|Direzione di tiro verso il percorso di trasferimento|No'), "
		 . "('G', 15, 'Organizzazione', '', -1, ''), "
		 . "('g01', 15, 'Gestione della gara', '', 3, 'Non adeguata|Continui e necessari interventi gdg|Con sporadici interventi gdg|Autonoma'), "
		 . "('g02', 15, 'Assegnazione piazzole', '', 3, 'Assegnazione manuale|In ordine di rank|Sorteggio manuale|Sorteggio automatico'), "
		 . "('g03', 15, 'Abbigliamento  Personale organizzazione', '', 3, 'Non riconoscibile|Divisa di società|Pettorine|Divisa evento '), "
		 . "('g04', 7, 'Visuale di riserva', '', 3, 'Insufficienti|Appena Sufficienti|Abbondanti'), "
		 . "('g05', 15, 'Battifreccia o sagome di riserva', '', 2, ''), "
		 . "('g06', 3, 'Cavalletti di riserva', '', 2, ''), "
		 . "('g07', 15, 'Numeri di gara', 'Pettorali\r\nPersonalizzati (con nome dell''atleta)', 3, 'Assenti|Della Società|Dell''evento|Dell''evento personalizzati'), "
		 . "('g08', 15, 'Ristoro', 'Alimenti', 3, 'Assente|Presente a pagamento|Gratuito'), "
		 . "('g09', 15, 'Bevande sul campo di gara', '', 3, 'Assenti|Presenti a pagamento|Presenti gratuite'), "
		 . "('g10', 3, 'Assistenza ai disabili', '', 3, 'Non necessaria|No|Si'), "
		 . "('g11', 3, 'Periodicità di esposizione classifiche', '', 3, 'Mai|A fine gara|A fine distanza|Parziali di distanza|Tempo reale (ogni volèe)'), "
		 . "('g12', 15, 'Meccanismi di esposizione classifiche', '', 3, 'Assente|Cartaceo|Monitor|Maxischermo|Monitor e Maxischermo'), "
		 . "('g13', 3, 'Raccolta punteggi parziali', 'Strumento utilizzato', 3, 'Assente|Cartaceo|Tastierini elettronici'), "
		 . "('g14', 15, 'Speaker', '', 3, 'No|Durante la competizione|Durante le Finali|Durante qualifica e finali'), "
		 . "('g15', 15, 'Musica', '', 3, 'No|Durante le pause|Dal vivo'), "
		 . "('g16', 15, 'Impianto di amplificazione', 'Per musica, comunicazioni di servizio, speaker', 2, ''), "
		 . "('g17', 15, 'Comunicazione sul campo', '', 3, 'A voce|Telefoni dell''organizzazione|Radio dell''organizzazione'), "
		 . "('g18', 12, 'Raccolta punteggi parziali', '', 2, 'Assente|Via Radio'), "
		 . "('g19', 12, 'Periodicità esposizione classifiche', '', 3, 'Assente|Al termine della gara|Parziale una volta|Parziale più volte'), "
		 . "('g20', 15, 'Rinfresco finale', '', 2, ''), "
		 . "('g21', 15, 'Connessione ad internet', 'per pubblico o partecipanti', 3, 'No|Si Gratuita|Si a pagamento'), "
		 . "('H', 15, 'Luogo della gara', '', -1, ''), "
		 . "('h01', 3, 'Servizi igienici', '', 4, 'No|Comuni|Divisi per sesso|Lontani dal campo di gara|Facilmente Accessibili|Anche per disabili'), "
		 . "('h02', 3, 'Barriere architettoniche', '', 3, 'Presenti|Assenti'), "
		 . "('h03', 15, 'Indicazioni stradali', 'Non mappe o indicazioni online ma cartellonistica', 3, 'No|Insufficiente|Sufficiente'), "
		 . "('h04', 15, 'Recettivita'' alberghiera', '', 4, 'No|Gestita dall''organizzazione|Oltre i 10 minuti dal campo di gara|Entro i 10 minuti dal campo di gara'), "
		 . "('h05', 15, 'Trasporto da e per il campo di gara', '', 3, 'Non applicabile|No|Gestiti ma insufficienti|Gestiti e organizzati'), "
		 . "('h06', 3, 'Sedili', 'Insufficiente se < al 50% dei partecipanti', 3, 'Non applicabile|Insufficiente|Sufficiente per gli atleti|Sufficienti per atleti e accompagnatori'), "
		 . "('h07', 13, 'Ombrelloni e/o ombreggiatura', 'Insufficiente se < al 50% dei partecipanti', 3, 'Non applicabile|Insufficiente|Sufficiente per gli atleti|Sufficienti per atleti e accompagnatori'), "
		 . "('h08', 15, 'Sacchetti per la spazzatura', '', 3, 'Sufficiente|Insufficiente'), "
		 . "('h09', 1, 'Orientamento campo', '', 3, 'Non Applicabile|Sud|Est o Ovest|Entro 15 gradi|Nord'), "
		 . "('h10', 1, 'Fondo del campo', '', 3, 'Non Applicabile|Terriccio| Sintetico|Erboso sconnesso|Erboso raso'), "
		 . "('h11', 2, 'Temperatura sulla linea di tiro', '', 3, 'Meno di 18°|18° o più'), "
		 . "('h12', 2, 'Illuminazione artificiale ', '', 4, 'Diffusa|Diretta|Uniforme|Non uniforme|Sufficiente|Non Sufficiente'), "
		 . "('h13', 2, 'Illuminazione naturale ', '', 4, 'No|Filtrante|Omogenea'), "
		 . "('h14', 12, 'Servizi igienici', '', 3, 'No|Solo al punto di ritrovo|Servizi lungo il percorso'), "
		 . "('h15', 12, 'Indicazioni di percorso', '', 4, 'No|Scarsamente segnalato|Ben segnalato e visibile|Mappa del percorso distribuita'), "
		 . "('h16', 12, 'Pulizia del percorso', 'da intendersi del fondo del sentiero tra le piazzole', 2, ''), "
		 . "('I', 15, 'Assistenza Sanitaria', '', -1, ''), "
		 . "('i01', 15, 'Ospedale Entro i 10 Km', '', 2, ''), "
		 . "('i02', 15, 'Medico Presente', '', 2, ''), "
		 . "('i03', 15, 'Ambulanza', '', 2, ''), "
		 . "('i04', 15, 'Stanza Attrezzata per le Emergenze', '', 2, ''), "
		 . "('i05', 15, 'Stanza attrezzata per antidoping', '', 3, 'Non necessaria|No|Si comune|Si separata per sesso'), "
		 . "('J', 15, 'Premiazioni', '', -1, ''), "
		 . "('j01', 15, 'Podio', '', 3, 'Assente|Realizzato con paglioni o estemporaneamente|Struttura apposita'), "
		 . "('j02', 15, 'Bandiere', 'Nazionale, Fitarco, del comitato Regionale, della società', 3, 'No|Appoggiate a reti o siepi|Appese|Issate su pali'), "
		 . "('j03', 15, 'Inno Nazionale', '', 3, 'Non Applicabile|No|Si'), "
		 . "('j04', 15, 'Tipo di Premi (Scelta multipla)', '', 4, 'Medaglie/Coppe|Diplomi|Premi in Denaro|Premi in Natura'), "
		 . "('j05', 15, 'Premiazione conforme al Regolamento', 'Numero di premiati e tipologia dei premi', 2, ''), "
		 . "('j06', 15, 'Cerimonia di premiazione conforme al Regolamento', 'Chiamata nell''ordine previsto, con posizione, società, punteggio e nome', 2, ''), "
		 . "('j07', 15, 'Omaggio di partecipazione', 'meglio conosciuto come \"premio di partecipazione\"', 2, ''), "
		 . "('j08', 15, 'Premi a sorteggio', '', 2, ''), "
		 . "('K', 15, 'Promozione', '', -1, ''), "
		 . "('k01', 15, 'Conferenza Stampa', 'Nei giorni precedenti la competizione', 2, ''), "
		 . "('k02', 15, 'Presenza sulla carta stampata', 'Prima della competizione', 4, 'No|Stampa Locale|Stampa Nazionale|Stampa Estera'), "
		 . "('k03', 15, 'Testate', '', 1, '70|5'), "
		 . "('k04', 15, 'Presenza di televisioni', 'di emittenti televisive che trasmettono la competizione', 4, 'Assente|Locali|Web|Nazionali|Diretta'), "
		 . "('k05', 15, 'Emittenti', '', 1, '70|5'), "
		 . "('k06', 15, 'Pubblicazione risultati su internet', '', 3, 'Nessuna|Termine gara|Rilevamenti parziali|Tempo reale (ogni volèe)'), "
		 . "('k07', 15, 'Indirizzo internet', 'indicare indirizzo internet sul quale sono stati pubblicati i risultati, con esclusione di quello fitarco', 0, ''), "
		 . "('Z', 15, 'Note', '', -1, ''), "
		 . "('z01', 15, 'Valutazione della Gara ed eventuali annotazioni ', '', 1, '70|7'), "
		 . "('e41', 4, 'Supporti Battifreccia', '', 4, 'Pali|Cavalletti')");

	safe_w_sql("ALTER TABLE `ElabQualifications` DROP `EqArrowstring`", false, array(1091));
	safe_w_sql("ALTER TABLE `ElabQualifications` DROP `EqRank`", false, array(1091));
	safe_w_sql("ALTER TABLE `ElabQualifications` CHANGE `EqEndNo` `EqArrowNo` SMALLINT UNSIGNED NOT NULL", false, array(1054));
	safe_w_sql("ALTER TABLE `TeamFinals` ADD `TfSetScore` TINYINT NOT NULL DEFAULT '0' AFTER `TfScore`", false, array(1060));
	safe_w_sql("ALTER TABLE `TeamFinals` ADD `TfSetPoints` VARCHAR(15) DEFAULT NULL AFTER `TfSetScore`", false, array(1060));

	db_save_version('2010-01-20 02:00:00');
}

if($version<'2010-02-06 01:00:00') {
	safe_w_sql("DROP TABLE IF EXISTS `HhtData`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `HhtData` (
		`HdTournament` int(11) NOT NULL,
		`HdTargetNo` varchar(5) NOT NULL,
		`HdDistance` tinyint(4) NOT NULL,
		`HdFinScheduling` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
		`HdTeamEvent` tinyint(1) unsigned NOT NULL DEFAULT '0',
		`HdArrowStart` tinyint(4) NOT NULL,
		`HdArrowEnd` tinyint(4) NOT NULL,
		`HdArrowString` varchar(6) NOT NULL,
		`HdEnId` int(10) unsigned NOT NULL,
		`HdMatchNo` tinyint(4) unsigned DEFAULT NULL,
		`HdEvent` varchar(4) NOT NULL DEFAULT '',
		`HdHhtId` int(11) NOT NULL,
		PRIMARY KEY (`HdTournament`,`HdTargetNo`,`HdDistance`,`HdArrowStart`,`HdFinScheduling`,`HdTeamEvent`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	safe_w_sql("DROP TABLE IF EXISTS `HhtEvents`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `HhtEvents` (
		`HeTournament` int(11) NOT NULL,
		`HeEventCode` varchar(3) NOT NULL,
		`HeHhtId` int(11) NOT NULL,
		`HeSession` tinyint(4) NOT NULL,
		`HeFinSchedule` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
		`HeTeamEvent` tinyint(1) unsigned NOT NULL DEFAULT '0',
		PRIMARY KEY (`HeTournament`,`HeEventCode`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	safe_w_sql("DROP TABLE IF EXISTS `HhtSetup`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `HhtSetup` (
		`HsId` int(11) NOT NULL,
		`HsTournament` int(11) NOT NULL,
		`HsIpAddress` varchar(15) NOT NULL,
		`HsPort` varchar(6) NOT NULL,
		`HsName` varchar(16) NOT NULL,
		`HsMode` tinyint(4) NOT NULL,
		`HsFlags` varchar(16) NOT NULL,
		`HsPhase` varchar(20) NOT NULL,
		`HsSequence` varchar(12) NOT NULL,
		`HsDistance` tinyint(4) NOT NULL,
		PRIMARY KEY (`HsId`,`HsTournament`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	safe_w_sql("ALTER TABLE `Tournament` ADD `ToUseHHT` tinyint(4) NOT NULL DEFAULT '0' AFTER `ToBlock`", false, array(1060));

	db_save_version('2010-02-06 01:00:00');
}
if($version<'2010-02-26 01:00:00') {
	$MySql = "REPLACE INTO `TournamentType` (`TtId`, `TtName`, `TtNumDist`, `TtNumEnds`, `TtMaxDistScore`, `TtMaxFinIndScore`, `TtMaxFinTeamScore`, `TtCategory`, `TtElabTeam`, `TtElimination`, `TtGolds`, `TtXNine`, `TtOrder`) VALUES
		(11, '3D', 1, 20, 220, 44, 132, 8, 2, 1, '11', '10', 0),
		(13, '3D', 2, 20, 220, 44, 132, 8, 2, 1, '11', '10', 0)";
	safe_w_sql($MySql);

	$MySql = "REPLACE INTO `TournamentDistances` (`TdClasses`, `TdType`, `Td1`, `Td2`, `Td3`, `Td4`, `Td5`, `Td6`, `Td7`, `Td8`) VALUES
		('__P_', 16, '10m-1', '10m-2', '', '', '', '', '', ''),
		('__G_', 16, '15m-1', '15m-2', '', '', '', '', '', ''),
		('__M_', 16, '18m-1', '18m-2', '', '', '', '', '', ''),
		('__F_', 16, '18m-1', '18m-2', '', '', '', '', '', '')";
	safe_w_sql($MySql);
	safe_w_sql("REPLACE INTO Targets set TarDescr = 'Trg3DComplete', TarArray='Trg3DComplete' ");

	db_save_version('2010-02-26 01:00:00');
}

if($version<'2010-03-03 15:17:00') {
	$MySql = "ALTER TABLE `Classes` CHANGE `ClSex` `ClSex` TINYINT( 3 ) NOT NULL DEFAULT '0' ";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-03-03 15:17:00');
}

if($version<'2010-03-14 17:22:00') {
	$MySql = "ALTER TABLE `Divisions` add (`DivAthlete` varchar(1) NOT NULL DEFAULT '1') ";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-03-14 17:22:00');
}

if($version<'2010-03-14 17:23:00') {
	$MySql = "ALTER TABLE `Classes` add (`ClAthlete` varchar(1) NOT NULL DEFAULT '1') ";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-03-14 17:23:00');
}

if($version<'2010-03-16 21:23:00') {
	$MySql = "ALTER TABLE `TVParams` add (`TVPViewPartials` varchar(1) NOT NULL DEFAULT '1', `TVPViewDetails` varchar(1) NOT NULL DEFAULT '1') ";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-03-16 21:23:00');
}

if($version<'2010-03-17 18:04:00') {
	$MySql = "ALTER TABLE `TVParams` add (`TVPViewIdCard` varchar(1) NOT NULL DEFAULT '') ";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-03-17 18:04:00');
}

if($version<'2010-03-21 10:00:00') {
	$MySql = "REPLACE INTO `Parameters` (`ParId`,`ParValue`) VALUES('DEBUG','')";
	safe_w_sql($MySql);

	db_save_version('2010-03-21 10:00:00');
}

if($version<'2010-03-21 12:00:00') {
	safe_w_sql("DROP TABLE IF EXISTS `FinTraining`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `FinTraining` (
		`FtTournament` int(10) unsigned NOT NULL,
		`FtScheduledDate` date NOT NULL,
		`FtScheduledTime` time NOT NULL,
		`FtTargetFrom` varchar(3) NOT NULL,
		`FtTargetTo` varchar(3) NOT NULL,
		PRIMARY KEY (`FtTournament`,`FtScheduledDate`,`FtScheduledTime`, `FtTargetFrom`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	safe_w_sql("DROP TABLE IF EXISTS `FinTrainingEvent`;", false, array(1060));
	safe_w_sql("CREATE TABLE IF NOT EXISTS `FinTrainingEvent` (
		`FteTournament` int(10) unsigned NOT NULL,
		`FteScheduledDate` date NOT NULL,
		`FteScheduledTime` time NOT NULL,
		`FteTargetFrom` varchar(3) NOT NULL,
		`FteEvent` varchar(4) NOT NULL,
		`FteTeamEvent` tinyint(3) unsigned NOT NULL DEFAULT '0',
		PRIMARY KEY (`FteTournament`,`FteScheduledDate`,`FteScheduledTime`,`FteTargetFrom`,`FteEvent`,`FteTeamEvent`)
	) ENGINE=MyISAM DEFAULT CHARSET=utf8", false, array(1060));

	db_save_version('2010-03-21 12:00:00');
}

if($version<'2010-03-23 10:00:00') {
	$MySql = "REPLACE INTO `Parameters` (`ParId`,`ParValue`) VALUES('TourBusy','')";
	safe_w_sql($MySql);

	db_save_version('2010-03-23 10:00:00');
}

if($version<'2010-03-29 19:05:00') {

	$MySql = "ALTER TABLE `Phases` ADD `PhLevel` TINYINT NOT NULL DEFAULT '-1'";
	safe_w_sql($MySql,false, array(1060,1054));

	$MySql = "UPDATE `Phases` SET `PhLevel` = '0' WHERE `Phases`.`PhId` =1";
	safe_w_sql($MySql);

	$MySql = "REPLACE INTO `Phases` (`PhId`,`PhDescr`,`PhLevel`) VALUES ('24', '24Final', '32'), ('48', '48Final', '-1')";
	safe_w_sql($MySql);

	$MySql = "INSERT INTO Grids (GrMatchNo, GrPosition, GrPhase) VALUES
		(128, 1, 48),(129, 0, 48),(130, 0, 48),(131, 0, 48),(132, 33, 48),(133, 80, 48),(134, 81, 48),(135, 32, 48),
		(136, 17, 48),(137, 96, 48),(138, 65, 48),(139, 48, 48),(140, 49, 48),(141, 64, 48),(142, 97, 48),(143, 16, 48),
		(144, 9, 48),(145, 104, 48),(146, 57, 48),(147, 56, 48),(148, 41, 48),(149, 72, 48),(150, 89, 48),(151, 24, 48),
		(152, 25, 48),(153, 88, 48),(154, 73, 48),(155, 40, 48),(156, 0, 48),(157, 0, 48),(158, 0, 48),(159, 8, 48),
		(160, 5, 48),(161, 0, 48),(162, 0, 48),(163, 0, 48),(164, 37, 48),(165, 76, 48),(166, 85, 48),(167, 28, 48),
		(168, 21, 48),(169, 92, 48),(170, 69, 48),(171, 44, 48),(172, 53, 48),(173, 60, 48),(174, 101, 48),(175, 12, 48),
		(176, 13, 48),(177, 100, 48),(178, 61, 48),(179, 52, 48),(180, 45, 48),(181, 68, 48),(182, 93, 48),(183, 20, 48),
		(184, 29, 48),(185, 84, 48),(186, 77, 48),(187, 36, 48),(188, 0, 48),(189, 0, 48),(190, 0, 48),(191, 4, 48),
		(192, 3, 48),(193, 0, 48),(194, 0, 48),(195, 0, 48),(196, 35, 48),(197, 78, 48),(198, 83, 48),(199, 30, 48),
		(200, 19, 48),(201, 94, 48),(202, 67, 48),(203, 46, 48),(204, 51, 48),(205, 62, 48),(206, 99, 48),(207, 14, 48),
		(208, 11, 48),(209, 102, 48),(210, 59, 48),(211, 54, 48),(212, 43, 48),(213, 70, 48),(214, 91, 48),(215, 22, 48),
		(216, 27, 48),(217, 86, 48),(218, 75, 48),(219, 38, 48),(220, 0, 48),(221, 0, 48),(222, 0, 48),(223, 6, 48),
		(224, 7, 48),(225, 0, 48),(226, 0, 48),(227, 0, 48),(228, 39, 48),(229, 74, 48),(230, 87, 48),(231, 26, 48),
		(232, 23, 48),(233, 90, 48),(234, 71, 48),(235, 42, 48),(236, 55, 48),(237, 58, 48),(238, 103, 48),(239, 10, 48),
		(240, 15, 48),(241, 98, 48),(242, 63, 48),(243, 50, 48),(244, 47, 48),(245, 66, 48),(246, 95, 48),(247, 18, 48),
		(248, 31, 48),(249, 82, 48),(250, 79, 48),(251, 34, 48),(252, 0, 48),(253, 0, 48),(254, 0, 48),(255, 2, 48);";
	safe_w_sql($MySql,false, array(1062));


	db_save_version('2010-03-29 19:05:00');
}

if($version<'2010-04-06 15:10:00') {

	$MySql = "DELETE FROM `Phases` WHERE `PhId` = 64";
	safe_w_sql($MySql);

	$MySql = "ALTER TABLE `Finals` ADD `FinWinnerSet` TINYINT NOT NULL DEFAULT 0 AFTER `FinSetPoints`";
	safe_w_sql($MySql,false, array(1060,1054));

	$MySql = "ALTER TABLE `TeamFinals` ADD `TfWinnerSet` TINYINT NOT NULL DEFAULT 0 AFTER `TfSetPoints`";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-04-06 15:10:00');
}

if($version<'2010-04-13 14:00:00') {
	$MySql = "ALTER TABLE `Entries` ADD `EnTeamMixEvent` TINYINT(1) NOT NULL DEFAULT 1 AFTER `EnTeamFEvent`";
	safe_w_sql($MySql,false, array(1060,1054));


	$MySql = "ALTER TABLE `HhtEvents` CHANGE `HeEventCode` `HeEventCode` VARCHAR( 3 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL";
	safe_w_sql($MySql,false, array(1060,1054));

	$MySql = "ALTER TABLE `Entries` ADD `EnDoubleSpace` TINYINT(1) NOT NULL DEFAULT 0 AFTER `EnTeamMixEvent`";
	safe_w_sql($MySql,false, array(1060,1054));


	$MySql = "ALTER TABLE `CasScore` CHANGE `CaSArrowString` `CaSArrowString` VARCHAR( 36 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,CHANGE `CaSArrowPosition` `CaSArrowPosition` VARCHAR( 360 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ;";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql = "ALTER TABLE `CasScore` ADD `CaSSetPoints` VARCHAR( 23 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `CaSTarget` ,ADD `CaSSetScore` TINYINT( 4 ) NOT NULL DEFAULT '0' AFTER `CaSSetPoints`;";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql = "ALTER TABLE `CasScore` CHANGE `CaSTarget` `CaSTarget` VARCHAR( 3 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ;";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql ="CREATE TABLE IF NOT EXISTS `CasTeamFinal` (
		`CTFEvent` VARCHAR( 4 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
		`CTFMatchNo` TINYINT( 4 ) UNSIGNED NOT NULL DEFAULT '0',
		`CTFTournament` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0',
		`CTFSetPoints` VARCHAR( 23 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
		`CTFSetScore` TINYINT( 4 ) NOT NULL DEFAULT '0',
		`CTFScore` SMALLINT( 6 ) NOT NULL DEFAULT '0',
		`CTFTie` TINYINT( 1 ) NOT NULL DEFAULT '0',
		`CTFArrowString` VARCHAR( 36 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
		`CTFTieBreak` VARCHAR( 3 ) NOT NULL ,
		`CTFTiePoins` VARCHAR( 5 ) NOT NULL ,
		`CTFTieScore` SMALLINT( 6 ) NOT NULL DEFAULT '0',
		PRIMARY KEY ( `CTFEvent` , `CTFMatchNo` , `CTFTournament` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql="ALTER TABLE `CasTeamFinal` ADD `CTFScore2` SMALLINT( 6 ) NOT NULL DEFAULT '0'";
	safe_w_sql($MySql,false, array(1060,1054));

	db_save_version('2010-04-13 14:00:00');
}

if($version<'2010-04-14 10:17:00') {

	$MySql="CREATE TABLE IF NOT EXISTS `CasTeamTarget` (
		`CTTTournament` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0',
		`CTTEvent` VARCHAR( 4 ) NOT NULL DEFAULT '',
		`CTTMatchNo` TINYINT( 4 ) UNSIGNED NOT NULL DEFAULT '0',
		`CTTTarget` VARCHAR( 3 ) NOT NULL ,
		`CTTSchedule` DATETIME NOT NULL,
		PRIMARY KEY ( `CTTTournament` , `CTTEvent` , `CTTMatchNo` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	db_save_version('2010-04-14 10:17:00');
}

if($version<'2010-04-17 13:34:01') {

	$MySql="ALTER TABLE `TournamentDistances` add (`TdTournament` int(10) not null default 0 )";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	db_save_version('2010-04-17 13:34:01');
}

if($version<'2010-04-17 15:34:02') {

	$MySql="ALTER TABLE `TournamentDistances` drop `TdTournament`";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));
	$MySql="ALTER TABLE `TournamentDistances` add `TdTournament` int(10) not null after `TdType` ";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));
	$MySql="ALTER TABLE `TournamentDistances` DROP PRIMARY KEY , ADD PRIMARY KEY ( `TdClasses` , `TdType` , `TdTournament` )";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$q=safe_r_sql("select ToId, ToType from Tournament");
	while($r=safe_fetch($q)) {
		$t=safe_r_sql("SELECT distinct td.* FROM TournamentDistances td inner join Entries e on CONCAT(TRIM(e.EnDivision),TRIM(e.EnClass)) LIKE TdClasses where TdType=$r->ToType and e.EnTournament=$r->ToId and TdTournament=0");
		while($u=safe_fetch($t)) {
			$u->TdTournament=$r->ToId;
			$qubits=array();
			foreach($u as $key=>$val) {
				$qubits[]="$key = " . StrSafe_DB($val);
			}
			safe_w_sql("insert ignore into TournamentDistances set " . implode(',', $qubits));
		}
		safe_w_sql("update Tournament set ToDbVersion = '2010-04-17 15:34:01' where ToDbVersion < '2010-04-17 15:34:01'");
	}

	db_save_version('2010-04-17 15:34:02');
}

if($version<'2010-04-25 11:34:00') {
	$MySql = "ALTER TABLE `Entries` CHANGE `EnPhoto` `EnBadgePrinted` DATETIME NULL DEFAULT NULL";
	safe_w_sql($MySql,false, array(1060,1054));
	safe_w_sql("update `Entries` set EnBadgePrinted=NULL");
	db_save_version('2010-04-25 11:34:00');
}

if($version<'2010-04-25 19:10:00') {
	$MySql = "ALTER TABLE `Photos` ADD `PhPhotoEntered` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, ADD INDEX ( `PhPhotoEntered` ) ";
	safe_w_sql($MySql,false, array(1060,1054));
	safe_w_sql("update `Photos` set PhPhotoEntered='".date('Y-m-d H:i:s')."'");
	db_save_version('2010-04-25 19:10:00');
}

if($version<'2010-05-10 16:45:00') {
	$MySql = "REPLACE INTO `Parameters` (`ParId`,`ParValue`) VALUES('SpkTimer','30|ffffff;60|ffffff;90|ffffff;120|#')";
	safe_w_sql($MySql);

	db_save_version('2010-05-10 16:45:00');
}

if($version<'2010-06-20 19:00:00') {
	$MySql = "TRUNCATE TABLE `FinalReportQ`";
	safe_w_sql($MySql);
	$MySql = "INSERT INTO `FinalReportQ` (`FrqId`, `FrqStatus`, `FrqQuestion`, `FrqTip`, `FrqType`, `FrqOptions`) VALUES
		('A', 15, 'Organizzazione', '', -1, ''),
		('a01', 15, 'Conforme al calendario', '', 2, ''),
		('a05', 15, 'Tutte le classi e divisioni ammesse', '', 2, ''),
		('a06', 15, 'in caso contrario, specificare', '', 1, '70|2'),
		('a08', 3, 'Direttore dei tiri', '', 3, 'non iscritto all''albo|iscritto all''albo|arbitro|Arbitro e Direttore dei Tiri'),
		('a10', 15, 'Commissione di garanzia', 'Pubblicata ad inizio gara; solo per gare nazionali ed internazionali', 2, ''),
		('a11', 15, 'Field Manager (responsabile di campo)', 'Responsabile dell''organizzazione appositamente nominato per la gestione del campo', 2, ''),
		('a02', 15, 'Calendario', '', 3, 'Evento Federale|Internazionale|Nazionale|Interregionale|Giovanile|Sperimentale'),
		('a12', 15, 'Squadre Nazionali straniere', 'Solo rappresentative ufficiali; non applicabile solo per eventi federali', 3, 'N.A.|No|Si'),
		('a13', 15, 'In caso affermativo, specificare', '', 1, '70|2'),
		('a14', 3, 'Segnalazione eventuali Record', '', 1, '70|10'),
		('a15', 15, 'Reclami', '', 1, '70|7'),
		('a03', 15, 'Programma della gara pervenuto alla Giuria di Gara', '', 3, 'No|Pubblicato|Si'),
		('a07', 15, 'Tassa ridotta per le classi giovanili', '50% o differente se stabilito da apposito regolamento (es: Campionati Italiani)', 3, 'Come da Regolamento|Non Applicabile|No|Gratuita'),
		('B', 15, 'Luogo di gara', '', -1, ''),
		('b09', 15, 'Distanze in tolleranza', 'Distanze tra linea di tiro e bersagli', 3, 'Si|Solo dopo intervento GdG|No'),
		('b10', 3, 'Separazione interasse conforme al regolamento', '160 cm due atleti per turno; 240 cm con turno unico con 3 atleti', 2, ''),
		('b11', 3, 'Linea di tiro', 'Stabile=ben bloccata a terra\r\nPosizione atleti interasse=Art. 7.1.1.7 - 8.1.1.5', 4, 'Stabile|Ben definita|Confusa|Con posizione atleti|con interasse|Coperta'),
		('b12', 3, 'Numeri sulla linea di Tiro ', 'tra 1 e 2 metri', 3, 'Assenti|Sulla linea di tiro|Davanti alla linea di tiro tra 1 e 2 metri'),
		('b15', 3, 'Linea dei tre metri', '', 3, 'Presente|Assente'),
		('b16', 3, 'Linea Stampa/Media', '', 2, ''),
		('b17', 3, 'Linea di attesa conforme al regolamento', '', 2, ''),
		('b18', 3, 'Corridoi ', 'Tra i paglioni ove previsto dal regolamento', 3, 'Presente|Assente|Non Applicabile'),
		('b19', 7, 'Battifreccia', '', 3, 'Usurati|Seminuovi|Nuovi'),
		('b20', 7, 'Materiale Battifreccia', '', 3, 'Sintetico|Paglia'),
		('b21', 3, 'Supporti battifreccia', 'cavalletti', 3, 'Improvvisati|Triangolari|Rettangolari'),
		('c02', 7, 'Assicurazione battifreccia con tiranti', 'Per indoor = tirante tra paglione e cavalletto', 3, 'No|Si|Si ma con intervento GdG'),
		('b23', 7, 'Angolazione Battifreccia conforme al regolamento', 'per hf intendersi perpendicolarità del bersaglio rispetto alla posizione di tiro', 2, ''),
		('b24', 7, 'Disposizione visuali conforme', '', 2, ''),
		('b25', 1, 'Bandiere segnavento', '', 3, 'Assenti|Presenti non ben visibili|Presenti ben visibili'),
		('b27', 3, 'Numeratori dei battifreccia', 'numeri dei paglioni', 3, 'Assenti|Presenti non conformi|Presenti conformi'),
		('b26', 1, 'Maniche a vento', '', 3, 'Non Applicabile|Assente|Si 1|Si 2 o più'),
		('b28', 3, 'Impianto semaforico ', 'Per automatico si intende la temporizzazione e la sincronizzazione delle luci dopo la chiamata tranne che per un eventuale termine anticipato\r\nNon a norma di sicurezza=deferimento automatico', 3, 'Assenti|Non a norma di sicurezza|Manuale|Automatico'),
		('b29', 3, 'Sistemi visivi ausiliari', 'Bandierine ', 2, ''),
		('b30', 3, 'Indicatori di sequenza ', '(AB/CD) Non necessari per turno unico', 3, 'Turno unico|Assenti|Manuale|Automatico'),
		('b31', 3, 'Orologi contasecondi', '', 2, ''),
		('b32', 3, 'Indicatori acustici', 'Automatico se sincronizzato automaticamente con l''impianto semforico', 3, 'Manuale|Automatico'),
		('b33', 3, 'Indicatori individuali di punteggio per la fase di qualifica', 'flip board o altri indicatori di punteggio', 2, ''),
		('b34', 15, 'Indicatori di punteggio per le fasi finali', 'Manuali ed automatici= utilizzati i manuali per le eliminatorie e elettronici solo per le finali a medaglia', 3, 'Non applicabile|Assenti|Manuali|Manuali ed Automatici|Automatici'),
		('b36', 1, 'Sedie e ombrelloni per gli arbitri', '', 2, ''),
		('b38', 3, 'Postazione direttore dei tiri rialzata', '', 3, 'No|Si|Si ma non correttamente posizionata'),
		('b39', 1, 'Blind per le finali', 'Protezione per gli arbitri in prossimità del bersaglio', 3, 'Non Applicabile|Si|No'),
		('b40', 1, 'Campo di tiri di allenamento', '', 3, 'Non necessario|Assente|Utilizzate parti del campo di gara|Campo separato con orientamento differente|Campo separato con stesso orientamento '),
		('b41', 15, 'Campo per la finale', 'Campo per le finali singole', 3, 'Non necessario|Utilizzata parte del campo di gara|Campo apposito'),
		('b43', 15, 'Spazio per il pubblico', 'Per HF e 3D da intendersi solo per le fasi finali', 3, 'No|Spazio libero|Sedie|Tribune della struttura|Tribune appositamente realizzate'),
		('b42', 12, 'Campo tiri di prova', '', 4, 'No|Si|Distante dal punto di raduno|In prossimità del punto di raduno|Sufficiente per tutti gli arcieri|Necessità di turnazione'),
		('b46', 12, 'Picchetto dello stop', '', 3, 'Si|Si ma con intervento GdG'),
		('b49', 12, 'Percorso alternativo', 'per raggiungere le piazzole senza attraversare il percorso di gara', 2, ''),
		('b45', 12, 'Distanza campo base / piazzola più lontana', 'Tempo di percorrenza', 3, 'Oltre i 15 minuti|Entro i 15 minuti'),
		('b52', 12, 'Interferenze', '', 4, 'Al volo delle frecce|Alla visione del bersaglio|All''arco|No'),
		('b53', 4, 'Distanze conosciute', 'Rispetto delle distanze e del numero dei bersagli per tipo indicati nel regolamento', 2, ''),
		('b54', 12, 'Distanze sconosciute', '', 3, 'Inferiori alla media|nella media|Superiori alla media'),
		('b48', 12, 'Visibilità dei picchetti di tiro', 'dallo stop', 3, 'Si|Si ma con intervento GdG'),
		('b55', 12, 'Pendenze medie', 'indicativo', 3, 'zero|Meno di 15°|Più di 15°'),
		('C', 15, 'Sicurezza e assistenza sanitaria', '', -1, ''),
		('c03', 15, 'Dietro la linea delle visuali', '', 3, 'Non Presidiata|Presidiata|Assoluta'),
		('c01', 15, 'Accesso al campo', 'libero=nessun controllo; regolato=con pass nel rispetto del regolamento', 3, 'Libero|Regolato'),
		('c06', 12, 'Problemi generali di sicurezza del percorso', '', 4, 'Tiri incrociati|Direzioni di tiro verso altre piazzole|Direzione di tiro verso il percorso di trasferimento|No'),
		('a17', 15, 'Gestione della gara', 'Escluso il cambio delle targhe', 3, 'Non adeguata|Continui e necessari interventi gdg|Con sporadici interventi gdg|Autonoma'),
		('a21', 15, 'Assegnazione piazzole', '', 3, 'Assegnazione manuale|In ordine di rank|Sorteggio manuale|Sorteggio automatico'),
		('a22', 15, 'Abbigliamento  Personale organizzazione', '', 3, 'Non riconoscibile|Divisa di società|Pettorine|Divisa evento '),
		('a23', 7, 'Visuale di riserva', '', 3, 'Insufficienti|Appena Sufficienti|Abbondanti'),
		('a24', 15, 'Battifreccia o sagome di riserva', '', 2, ''),
		('a25', 3, 'Cavalletti di riserva', '', 2, ''),
		('a26', 15, 'Numeri di gara', 'Pettorali\r\nPersonalizzati (con nome dell''atleta)', 3, 'Assenti|Della Società|Dell''evento|Dell''evento personalizzati'),
		('a27', 15, 'Ristoro', 'Alimenti', 3, 'Assente|Presente a pagamento|Gratuito'),
		('a28', 15, 'Bevande sul campo di gara', '', 3, 'Assenti|Presenti a pagamento|Presenti gratuite'),
		('a30', 3, 'Assistenza ai disabili', '', 3, 'No|Si'),
		('a33', 3, 'Periodicità di esposizione classifiche', '', 3, 'Mai|A fine gara|A fine distanza|Parziali di distanza|Tempo reale (ogni volèe)'),
		('a34', 15, 'Meccanismi di esposizione classifiche', '', 3, 'Assente|Cartaceo|Monitor|Maxischermo|Monitor e Maxischermo'),
		('a31', 3, 'Raccolta punteggi parziali', 'Strumento utilizzato', 3, 'Assente|Cartaceo|Tastierini elettronici'),
		('a36', 15, 'Speaker', '', 3, 'No|Durante la competizione|Durante le Finali|Durante qualifica e finali'),
		('a37', 15, 'Musica', '', 3, 'No|Durante le pause|Dal vivo'),
		('b05', 15, 'Impianto di amplificazione', 'Per musica, comunicazioni di servizio, speaker', 2, ''),
		('b06', 15, 'Comunicazione sul campo', '', 3, 'A voce|Telefoni dell''organizzazione|Radio dell''organizzazione'),
		('a32', 12, 'Raccolta punteggi parziali', '', 2, ''),
		('a35', 12, 'Periodicità esposizione classifiche', '', 3, 'Mai|A fine gara|A fine distanza|Parziale una volta|Parziale più volte'),
		('a29', 15, 'Rinfresco finale', '', 2, ''),
		('a38', 15, 'Connessione ad internet', 'per pubblico o partecipanti', 3, 'No|Si Gratuita|Si a pagamento'),
		('b01', 15, 'Durata della competizione', '', 1, '70|1'),
		('a39', 3, 'Servizi igienici', '', 4, 'No|Comuni|Divisi per sesso|Lontani dal campo di gara|Facilmente Accessibili|Anche per disabili'),
		('b07', 3, 'Barriere architettoniche', '', 3, 'Presenti|Assenti'),
		('b08', 15, 'Indicazioni stradali', 'Non mappe o indicazioni online ma cartellonistica', 3, 'No|Insufficiente|Sufficiente'),
		('a40', 15, 'Recettivita'' alberghiera', '', 4, 'No|Gestita dall''organizzazione|Oltre i 10 minuti dal campo di gara|Entro i 10 minuti dal campo di gara'),
		('a41', 15, 'Trasporto da e per il campo di gara', '', 3, 'Non applicabile|No|Gestiti ma insufficienti|Gestiti e organizzati'),
		('b44', 3, 'Sedili', 'Insufficiente se < al 50% dei partecipanti', 3, 'Insufficiente|Sufficiente per gli atleti|Sufficienti per atleti e accompagnatori'),
		('b57', 1, 'Ombrelloni e/o ombreggiatura', 'Insufficiente se < al 50% dei partecipanti', 3, 'Non applicabile|Insufficiente|Sufficiente per gli atleti|Sufficienti per atleti e accompagnatori'),
		('b58', 15, 'Sacchetti per la spazzatura', '', 3, 'Sufficiente|Insufficiente'),
		('b59', 1, 'Orientamento campo', '', 3, 'Sud|Est o Ovest|Entro 15 gradi|Nord'),
		('b60', 1, 'Fondo del campo', '', 3, 'Terriccio|Sintetico|Erboso sconnesso|Erboso raso'),
		('b61', 2, 'Temperatura sulla linea di tiro', '', 3, 'Meno di 18°|18° o più'),
		('b62', 2, 'Illuminazione artificiale ', '', 4, 'Diffusa|Diretta|Uniforme|Illuminati singolarmente|Non uniforme|Sufficiente|Non Sufficiente'),
		('b63', 2, 'Illuminazione naturale ', '', 4, 'Schermata|Filtrante|Omogenea|Difforme'),
		('b64', 12, 'Servizi igienici', '', 3, 'No|Solo al punto di ritrovo|Servizi lungo il percorso'),
		('b50', 12, 'Indicazioni di percorso', '', 4, 'Con intervento GdG|Scarsamente segnalato|Ben segnalato e visibile|Mappa del percorso distribuita'),
		('b51', 12, 'Pulizia del percorso', 'da intendersi del fondo del sentiero tra le piazzole', 2, ''),
		('c08', 15, 'Ospedale Entro i 10 Km', '', 2, ''),
		('c09', 15, 'Medico Presente', '', 2, ''),
		('c10', 15, 'Ambulanza', '', 2, ''),
		('c11', 15, 'Stanza Attrezzata per le Emergenze', '', 2, ''),
		('c12', 15, 'Stanza attrezzata per antidoping', '', 3, 'Non necessaria|No|Si comune|Si separata per sesso'),
		('D', 15, 'Premiazioni e Pubblicità', '', -1, ''),
		('d05', 15, 'Podio', '', 3, 'Assente|Realizzato con paglioni o estemporaneamente|Struttura apposita'),
		('d03', 15, 'Bandiere', 'Nazionale, Fitarco, del comitato Regionale, della società', 3, 'No|Appoggiate a reti o siepi|Appese|Issate su pali'),
		('d04', 15, 'Inno Nazionale', '', 4, 'Non Applicabile|No|Si|Inizio gara|Fine gara'),
		('d06', 15, 'Tipo di Premi (Scelta multipla)', '', 4, 'Medaglie/Coppe|Diplomi|Premi in Denaro|Premi in Natura'),
		('d07', 15, 'Premiazione conforme al Regolamento', 'Numero di premiati e tipologia dei premi', 2, ''),
		('d08', 15, 'Cerimonia di premiazione conforme al Regolamento', 'Chiamata nell''ordine previsto, con posizione, società, punteggio e nome', 2, ''),
		('a42', 15, 'Omaggio di partecipazione', 'meglio conosciuto come \"premio di partecipazione\"', 2, ''),
		('a43', 15, 'Premi a sorteggio', '', 2, ''),
		('d09', 15, 'Conferenza Stampa', 'Nei giorni precedenti la competizione', 2, ''),
		('d10', 15, 'Presenza sulla carta stampata', 'Prima della competizione', 4, 'No|Stampa Locale|Stampa Nazionale|Stampa Estera'),
		('d11', 15, 'Testate', '', 1, '70|5'),
		('d12', 15, 'Presenza di televisioni', 'di emittenti televisive che trasmettono la competizione', 4, 'Assente|Locali|Web|Nazionali|Diretta'),
		('d13', 15, 'Emittenti', '', 1, '70|5'),
		('d14', 15, 'Pubblicazione risultati su internet', '', 3, 'Nessuna|Termine gara|Rilevamenti parziali|Tempo reale (ogni volèe)'),
		('d15', 15, 'Indirizzo internet', 'indicare indirizzo internet sul quale sono stati pubblicati i risultati, con esclusione di quello fitarco', 0, ''),
		('Z', 15, 'Note', '', -1, ''),
		('z01', 15, 'Valutazione della Gara ed eventuali annotazioni ', '', 1, '70|7'),
		('b56', 4, 'Supporti Battifreccia', '', 4, 'Pali|Cavalletti'),
		('a09', 3, 'Direttore dei tiri', '', 4, 'Preparato|Non preparato|Attento|Disattento'),
		('a16', 15, 'Sintetizzare reclamo ed inviare l''originale alla Segreteria Ufficio Tecnico', '', 1, '70|2'),
		('a18', 15, 'Commissari di campo ', 'per il cambio dei bersagli', 4, 'sufficienti|insufficienti|coordinati|professionali|disattenti'),
		('a19', 15, 'Tutte le richieste del Gdg soddisfatte', '', 2, ''),
		('a20', 15, 'Se non soddisfatte indicare quali', '', 1, '70|2'),
		('b03', 2, 'Tipologia di impianto', '', 3, 'Capannone|Palestra|Palestra scolastica|Palazzetto|Impianto dedicato'),
		('b02', 1, 'Tipologia di impianto', '', 3, 'Campo incolto|Prato|Stadio|Impianto dedicato'),
		('b04', 12, 'Tipologia di percorso', '', 3, 'Campagna pianeggiante|Campagna sconnessa|Bosco pianeggiante|Bosco collinare|Bosco montano'),
		('b031', 2, 'Capienza piazzole', '', 3, 'fino a 10|da 11 a 15|da 16 a 20|Più di 20'),
		('b021', 1, 'Capienza piazzole di tiro', '', 3, 'fino a 15|da 16 a 20|da 21 a 25|da 25 a 30|Più di 30 '),
		('b13', 3, 'Linea degli archi', '', 2, ''),
		('b14', 3, 'Line del metro per match round', 'Solo per gare con scontri diretti', 3, 'Non applicabile|con box per tecnici|con box GdG'),
		('b35', 15, 'Cartelli nomi partecipanti agli scontri', '', 2, ''),
		('b37', 2, 'Sedie per gli arbitri', '', 2, ''),
		('b47', 8, 'Picchetto per l''immagine della sagoma', '', 3, 'Si|Si ma con intervento GdG'),
		('c04', 15, 'Accessi incustoditi', '', 2, ''),
		('c05', 15, 'Commissari/Protezione Civile od altro addetti al controllo spettatori', '', 2, ''),
		('c07', 15, ' attrezzatura di primo soccorso ', '(bende garze ecc.)', 2, ''),
		('d01', 15, 'Tempo tra fine gara e inizio premiazione', '', 1, '70|1'),
		('d02', 15, 'Classifiche finali esposte prima della premiazione', '', 2, ''),
		('d16', 15, 'Pubblicazione su sito federale prima dell''allontanamento dell''arbitro', '', 2, ''),
		('M', 15, 'Atleti', '', -1, ''),
		('m01', 15, 'Fair Play', 'comportamento corretto dei partecipanti', 2, ''),
		('m02', 15, 'Se la risposta precedente è NO, descrivere nel dettaglio', '', 1, '70|2'),
		('m03', 15, 'Ammonizioni', '', 2, ''),
		('m04', 15, 'Comportamento antisportivo: descrizione', '', 1, '70|2'),
		('m05', 15, 'Abbigliamento non conforme: descrizione', '', 1, '70|2'),
		('m06', 15, 'Altri ammonimenti: descrizione', '', 1, '70|2'),
		('m07', 15, 'Sanzioni', '', 2, ''),
		('m08', 15, 'Infrazioni registrazione punti: descrizione', '', 1, '70|2'),
		('m09', 15, 'infrazione antidoping/alcool/stupefacenti: descrizione', '', 1, '70|2'),
		('m10', 15, 'Infrazione esecuzione tiro: descrizione', '', 1, '70|2'),
		('m11', 15, 'Non idonei alla competizione: descrizione', '', 1, '70|2'),
		('m12', 15, 'Tutti gli arcieri idonei al tiro', '', 4, 'No|Si|Visita medica scaduta|Non Tesserato|Tesseramento scaduto|Sotto squalifica|Altro'),
		('m13', 15, 'Se la risposta alla domanda precedente è NO indicare i soggetti', '', 1, '70|2'),
		('a04', 15, 'E'' stato rispettato il programma di gara', '', 2, ''),
		('b22', 7, 'Visuali omologate', '', 2, '')
	";
	safe_w_sql($MySql);

	db_save_version('2010-06-20 19:00:00');
}

if($version<'2010-06-25 11:11:00') {

	$MySql="CREATE TABLE IF NOT EXISTS `Flags` (
		`FlTournament` INT( 10 ) NOT NULL default '0',
		`FlCode` varchar(5) NOT NULL,
		`FlSVG` BLOB NOT NULL,
		`FlPNG` BLOB NOT NULL,
		`FlEntered` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
		PRIMARY KEY ( `FlCode` , `FlTournament` ),
		INDEX ( `FlEntered` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	db_save_version('2010-06-25 11:11:00');
}

if($version<'2010-06-25 23:11:00') {

	$MySql="Alter table `Flags` change `FlSVG` `FlSVG` mediumBLOB not null default ''";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	db_save_version('2010-06-25 23:11:00');
}

if($version<'2010-06-26 18:30:00') {

	$q=safe_r_sql("select * from `Flags` where FlSVG>''");
	while($r=safe_fetch($q)) {
		safe_w_sql("update Flags set FlSVG='".addslashes(gzdeflate(base64_decode($r->FlSVG), 9))."' where FlCode='$r->FlCode' and FlTournament=$r->FlTournament");
	}
	db_save_version('2010-06-26 18:30:00');
}

if($version<'2010-06-27 12:11:00') {

	$MySql="Alter table `Flags` add( `FlChecked` varchar(1) not null default '')";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	db_save_version('2010-06-27 12:11:00');
}

if($version<'2010-07-06 08:55:00') {

	$MySql="ALTER TABLE `Flags` CHANGE `FlPNG` `FlPNG` MEDIUMBLOB NOT NULL";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	db_save_version('2010-07-06 08:55:00');
}

if($version<'2010-09-01 10:15:00') {

	$MySql="ALTER TABLE `TournamentType` ADD `TtDouble` TINYINT NOT NULL DEFAULT '0' AFTER `TtXNine`";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

// qui metto a 1 il double per il doppio fita e il 25+18
	$MySql="UPDATE TournamentType SET TtDouble=1 WHERE TtId IN(2,8)";
	safe_w_sql($MySql);

	$MySql="ALTER TABLE `Tournament` ADD `ToLocRule` VARCHAR( 16 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
		ADD `ToUpNamesUrl` VARCHAR( 256 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
		ADD `ToUpPhotosUrl` VARCHAR( 256 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
		ADD `ToUpFlagsUrl` VARCHAR( 256 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT ''";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql="ALTER TABLE `Tournament`
		ADD `ToTypeName` VARCHAR( 25 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `ToUpFlagsUrl`,
		ADD `ToNumDist` TINYINT(3) UNSIGNED NOT NULL,
		ADD `ToNumEnds` TINYINT(3) UNSIGNED NOT NULL,
		ADD `ToMaxDistScore` MEDIUMINT(8) UNSIGNED NOT NULL,
		ADD `ToMaxFinIndScore` MEDIUMINT(8) UNSIGNED NOT NULL,
		ADD `ToMaxFinTeamScore` MEDIUMINT(8) UNSIGNED NOT NULL,
		ADD `ToCategory` TINYINT(4) NOT NULL DEFAULT '0',
		ADD `ToElabTeam` TINYINT(4) NOT NULL DEFAULT '0',
		ADD `ToElimination` TINYINT(4) NOT NULL DEFAULT '0',
		ADD `ToGolds` VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
		ADD `ToXNine` VARCHAR(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
		ADD `ToDouble` TINYINT NOT NULL DEFAULT '0'";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql="UPDATE Tournament INNER JOIN TournamentType ON ToType=TtId SET
		ToTypeName=TtName,
		ToNumDist=TtNumDist,
		ToNumEnds=TtNumEnds,
		ToMaxDistScore=TtMaxDistScore,
		ToMaxFinIndScore=TtMaxFinIndScore,
		ToMaxFinTeamScore=TtMaxFinTeamScore,
		ToCategory=TtCategory,
		ToElabTeam =TtElabTeam,
		ToElimination=TtElimination,
		ToGolds=TtGolds,
		ToXNine =TtXNine,
		ToDouble=TtDouble";
	safe_w_sql($MySql);

	db_save_version('2010-09-01 10:15:00');
}

if($version<'2010-09-03 17:45:00') {

/*** converto le arrowstring e le tiearrowstring delle finali individuali ***/

// questa mi fa BCDE=>A IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(REPLACE(FinArrowstring,'B','A'),'C','A'),'D','A'),'E','A'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(REPLACE(FinTiebreak,'B','A'),'C','A'),'D','A'),'E','A')
		WHERE
			TarArray IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa bcde=>a IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(REPLACE(FinArrowstring,'b','a'),'c','a'),'d','a'),'e','a'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(REPLACE(FinTiebreak,'b','a'),'c','a'),'d','a'),'e','a')
		WHERE
			TarArray IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa F=>B IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'F','B'),
			FinTiebreak=REPLACE(FinTiebreak,'F','B')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa f=>b IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'f','b'),
			FinTiebreak=REPLACE(FinTiebreak,'f','b')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa G=>C IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'G','C'),
			FinTiebreak=REPLACE(FinTiebreak,'G','C')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa g=>c IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'g','c'),
			FinTiebreak=REPLACE(FinTiebreak,'g','c')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa H=>D IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'H','D'),
			FinTiebreak=REPLACE(FinTiebreak,'H','D')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa h=>d IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'h','d'),
			FinTiebreak=REPLACE(FinTiebreak,'h','d')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa I=>E IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'I','E'),
			FinTiebreak=REPLACE(FinTiebreak,'I','E')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa i=>e IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'i','e'),
			FinTiebreak=REPLACE(FinTiebreak,'i','e')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa J=>F IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'J','F'),
			FinTiebreak=REPLACE(FinTiebreak,'J','F')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa j=>f IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'j','f'),
			FinTiebreak=REPLACE(FinTiebreak,'j','f')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa KL=>G IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(FinArrowstring,'K','G'),'L','G'),
			FinTiebreak=REPLACE(REPLACE(FinTiebreak,'K','G'),'L','G')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa kl=>g IN ('TrgField')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(FinArrowstring,'k','g'),'l','g'),
			FinTiebreak=REPLACE(REPLACE(FinTiebreak,'k','g'),'l','g')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);


// questa mi fa F=>A IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'F','A'),
			FinTiebreak=REPLACE(FinTiebreak,'F','A')
		WHERE
			TarArray IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa f=>a IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'f','a'),
			FinTiebreak=REPLACE(FinTiebreak,'f','a')

		WHERE
			TarArray IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa L=>J IN ('TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'L','J'),
			FinTiebreak=REPLACE(FinTiebreak,'L','J')

		WHERE
			TarArray IN ('TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa l=>j IN ('TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'l','j'),
			FinTiebreak=REPLACE(FinTiebreak,'l','j')

		WHERE
			TarArray IN ('TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa K=>L IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'K','L'),
			FinTiebreak=REPLACE(FinTiebreak,'K','L')

		WHERE
			TarArray IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa k=>l IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'k','l'),
			FinTiebreak=REPLACE(FinTiebreak,'k','l')
		WHERE
			TarArray IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa GHI=>A IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(FinArrowstring,'G','A'),'H','A'),'I','A'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(FinTiebreak,'G','A'),'H','A'),'I','A')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa ghi=>a IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(FinArrowstring,'g','a'),'h','a'),'i','a'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(FinTiebreak,'g','a'),'h','a'),'i','a')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa JKL=>B IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(FinArrowstring,'J','B'),'K','B'),'L','B'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(FinTiebreak,'J','B'),'K','B'),'L','B')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa jkl=>b IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(FinArrowstring,'j','b'),'k','b'),'l','b'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(FinTiebreak,'j','b'),'k','b'),'l','b')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa GHJ=>A IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(FinArrowstring,'G','A'),'H','A'),'J','A'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(FinTiebreak,'G','A'),'H','A'),'J','A')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa ghj=>a IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(REPLACE(REPLACE(FinArrowstring,'g','a'),'h','a'),'j','a'),
			FinTiebreak=REPLACE(REPLACE(REPLACE(FinTiebreak,'g','a'),'h','a'),'j','a')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa K=>M IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'K','M'),
			FinTiebreak=REPLACE(FinTiebreak,'K','M')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa k=>m IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Finals INNER JOIN Events ON FinTournament=EvTournament AND FinEvent=EvCode AND EvTeamEvent=0 INNER JOIN Targets	ON EvFinalTargetType=TarId
		SET
			FinArrowstring=REPLACE(FinArrowstring,'k','m'),
			FinTiebreak=REPLACE(FinTiebreak,'k','m')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

/*** converto le arrowstring e le tiearrowstring delle finali a squadre ***/

// questa mi fa BCDE=>A IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(REPLACE(TfArrowstring,'B','A'),'C','A'),'D','A'),'E','A'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(REPLACE(TfTiebreak,'B','A'),'C','A'),'D','A'),'E','A')
		WHERE
			TarArray IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa bcde=>a IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(REPLACE(TfArrowstring,'b','a'),'c','a'),'d','a'),'e','a'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(REPLACE(TfTiebreak,'b','a'),'c','a'),'d','a'),'e','a')
		WHERE
			TarArray IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa F=>B IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'F','B'),
			TfTiebreak=REPLACE(TfTiebreak,'F','B')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa f=>b IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'f','b'),
			TfTiebreak=REPLACE(TfTiebreak,'f','b')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa G=>C IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'G','C'),
			TfTiebreak=REPLACE(TfTiebreak,'G','C')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa g=>c IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'g','c'),
			TfTiebreak=REPLACE(TfTiebreak,'g','c')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa H=>D IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'H','D'),
			TfTiebreak=REPLACE(TfTiebreak,'H','D')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa h=>d IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'h','d'),
			TfTiebreak=REPLACE(TfTiebreak,'h','d')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa I=>E IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'I','E'),
			TfTiebreak=REPLACE(TfTiebreak,'I','E')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa i=>e IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'i','e'),
			TfTiebreak=REPLACE(TfTiebreak,'i','e')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa J=>F IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'J','F'),
			TfTiebreak=REPLACE(TfTiebreak,'J','F')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa j=>f IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'j','f'),
			TfTiebreak=REPLACE(TfTiebreak,'j','f')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa KL=>G IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(TfArrowstring,'K','G'),'L','G'),
			TfTiebreak=REPLACE(REPLACE(TfTiebreak,'K','G'),'L','G')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa kl=>g IN ('TrgField')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(TfArrowstring,'k','g'),'l','g'),
			TfTiebreak=REPLACE(REPLACE(TfTiebreak,'k','g'),'l','g')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa F=>A IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'F','A'),
			TfTiebreak=REPLACE(TfTiebreak,'F','A')

		WHERE
			TarArray IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa f=>a IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'f','a'),
			TfTiebreak=REPLACE(TfTiebreak,'f','a')

		WHERE
			TarArray IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa L=>J IN ('TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'L','J'),
			TfTiebreak=REPLACE(TfTiebreak,'L','J')

		WHERE
			TarArray IN ('TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa l=>j IN ('TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'l','j'),
			TfTiebreak=REPLACE(TfTiebreak,'l','j')

		WHERE
			TarArray IN ('TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa K=>L IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'K','L'),
			TfTiebreak=REPLACE(TfTiebreak,'K','L')

		WHERE
			TarArray IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa k=>l IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'k','l'),
			TfTiebreak=REPLACE(TfTiebreak,'k','l')
		WHERE
			TarArray IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa GHI=>A IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(TfArrowstring,'G','A'),'H','A'),'I','A'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(TfTiebreak,'G','A'),'H','A'),'I','A')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa ghi=>a IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(TfArrowstring,'g','a'),'h','a'),'i','a'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(TfTiebreak,'g','a'),'h','a'),'i','a')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);


// questa mi fa JKL=>B IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(TfArrowstring,'J','B'),'K','B'),'L','B'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(TfTiebreak,'J','B'),'K','B'),'L','B')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa jkl=>b IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(TfArrowstring,'j','b'),'k','b'),'l','b'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(TfTiebreak,'j','b'),'k','b'),'l','b')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa GHJ=>A IN ('Trg3DComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(TfArrowstring,'G','A'),'H','A'),'J','A'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(TfTiebreak,'G','A'),'H','A'),'J','A')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa ghj=>a IN ('Trg3DComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(REPLACE(REPLACE(TfArrowstring,'g','a'),'h','a'),'j','a'),
			TfTiebreak=REPLACE(REPLACE(REPLACE(TfTiebreak,'g','a'),'h','a'),'j','a'
			)
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa K=>M IN ('Trg3DComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'K','M'),
			TfTiebreak=REPLACE(TfTiebreak,'K','M')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa k=>m IN ('Trg3DComplete')
	$MySql="
		UPDATE
			TeamFinals INNER JOIN Events ON TfTournament=EvTournament AND TfEvent=EvCode AND EvTeamEvent=1 INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			TfArrowstring=REPLACE(TfArrowstring,'k','m'),
			TfTiebreak=REPLACE(TfTiebreak,'k','m')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

/*
 * converto le arrowstring delle eliminatorie.
 * Queste query sono diverse perchè dalla tabella Eliminations devo fare una join su Entries per risalire
 * all'evento dal quale ricavo il bersaglio.
 * Eliminations non ha il riferimento diretto al torneo quindi devo per forza far così visto che gli eventi devono essere
 * vincolati al torneo considerato
 */

// questa mi fa BCDE=>A IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(REPLACE(ElArrowString,'B','A'),'C','A'),'D','A'),'E','A')
		WHERE
			TarArray IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa bcde=>a IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(REPLACE(ElArrowString,'b','a'),'c','a'),'d','a'),'e','a')
		WHERE
			TarArray IN ('TrgField','TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete','Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa F=>B IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'F','B')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa f=>b IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'f','b')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa G=>C IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'G','C')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa g=>c IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'g','c')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa H=>D IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'H','D')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa h=>d IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'h','d')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa I=>E IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'I','E')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa i=>e IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'i','e')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa J=>F IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'J','F')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa j=>f IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'j','f')
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa KL=>G IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE
			(
				REPLACE
				(
					ElArrowString,
					'K','G'
				),
				'L','G'
			)
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa kl=>g IN ('TrgField')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE
			(
				REPLACE
				(
					ElArrowString,
					'k','g'
				),
				'l','g'
			)
		WHERE
			TarArray IN ('TrgField')
	";
	safe_w_sql($MySql);

// questa mi fa F=>A IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'F','A')

		WHERE
			TarArray IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa f=>a IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'f','a')

		WHERE
			TarArray IN ('TrgIndSmall','TrgCOIndSmall','TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa L=>J IN ('TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'L','J')

		WHERE
			TarArray IN ('TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa l=>j IN ('TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'l','j')

		WHERE
			TarArray IN ('TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa K=>L IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'K','L')

		WHERE
			TarArray IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa k=>l IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'k','l')
		WHERE
			TarArray IN ('TrgIndComplete','TrgIndSmall','TrgCOIndComplete','TrgCOIndSmall')
	";
	safe_w_sql($MySql);

// questa mi fa GHI=>A IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(ElArrowString,'G','A'),'H','A'),'I','A')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa ghi=>a IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(ElArrowString,'g','a'),'h','a'),'i','a')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa JKL=>B IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(ElArrowString,'J','B'),'K','B'),'L','B')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa jkl=>b IN ('TrgHMOutComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(ElArrowString,'j','b'),'k','b'),'l','b')
		WHERE
			TarArray IN ('TrgHMOutComplete')
	";
	safe_w_sql($MySql);

// questa mi fa GHJ=>A IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(ElArrowString,'G','A'),'H','A'),'J','A')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa ghj=>a IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(REPLACE(REPLACE(ElArrowString,'g','a'),'h','a'),'j','a')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa K=>M IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'K','M')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

// questa mi fa k=>m IN ('Trg3DComplete')
	$MySql="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId INNER JOIN Events ON ElEventCode=EvCode AND EvTeamEvent=0 AND EvTournament=EnTournament INNER JOIN Targets ON EvFinalTargetType=TarId
		SET
			ElArrowString=REPLACE(ElArrowString,'k','m')
		WHERE
			TarArray IN ('Trg3DComplete')
	";
	safe_w_sql($MySql);

	db_save_version('2010-09-03 17:45:00');
}

if($version<'2010-09-08 15:40:00') {
	$MySql="ALTER TABLE `InvolvedType` ADD `ItJury` TINYINT( 3 ) UNSIGNED NOT NULL ,ADD `ItOC` TINYINT UNSIGNED NOT NULL ";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091));

	$MySql="UPDATE `InvolvedType` SET `ItJury` = '2' WHERE `InvolvedType`.`ItId` =4";
	safe_w_sql($MySql);

	$MySql="UPDATE `InvolvedType` SET `ItOC` = '1' WHERE `InvolvedType`.`ItId` =3";
	safe_w_sql($MySql);

	$MySql="UPDATE `InvolvedType` SET `ItJudge` = '2' WHERE `InvolvedType`.`ItId` =1";
	safe_w_sql($MySql);

	$MySql="
		INSERT INTO `InvolvedType` (`ItId` ,`ItDescription` ,`ItJudge` ,`ItDoS` ,`ItJury` ,`ItOC`) VALUES
			('5', 'ChairmanJudge', '1', '0', '0', '0'), ('6', 'DosAssistant', '0', '2', '0', '0'),
			('7', 'ChairmanJury', '0', '0', '1', '0'),('8', 'AlternateJury', '0', '0', '3', '0'),
			('9', 'FieldResp', '0', '0', '0', '2'), ('10', 'ResultResp', '0', '0', '0', '5'),
			('11', 'LogisticResp', '0', '0', '0', '4'), ('12', 'MediaResp', '0', '0', '0', '3')
	";
	safe_w_sql($MySql);

	db_save_version('2010-09-08 15:40:00');
}

if($version<'2010-09-09 11:34:00') {
	$MySql="CREATE TABLE `Individuals` (
		`IndId` INT UNSIGNED NOT NULL ,
		`IndEvent` VARCHAR( 4 ) NOT NULL ,
		`IndTournament` INT NOT NULL ,
		`IndD1Rank` SMALLINT NOT NULL ,
		`IndD2Rank` SMALLINT NOT NULL ,
		`IndD3Rank` SMALLINT NOT NULL ,
		`IndD4Rank` SMALLINT NOT NULL ,
		`IndD5Rank` SMALLINT NOT NULL ,
		`IndD6Rank` SMALLINT NOT NULL ,
		`IndD7Rank` SMALLINT NOT NULL ,
		`IndD8Rank` SMALLINT NOT NULL ,
		`IndRank` SMALLINT NOT NULL ,
		`IndRankFinal` SMALLINT NOT NULL ,
		`IndTieBreak` VARCHAR( 8 ) NOT NULL ,
		`IndTimestamp` TIMESTAMP NULL DEFAULT NULL ,
		PRIMARY KEY ( `IndId` , `IndEvent` , `IndTournament` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	safe_w_sql($MySql,false, array(1060,1054,1062,1091,1050));

	db_save_version('2010-09-09 11:34:00');
}

if($version<'2010-09-15 09:35:00') {
// aggiungo i campi con i parametri per i numeri di frecce
	$MySql="
		ALTER TABLE `Events` ADD `EvElimEnds` TINYINT UNSIGNED NOT NULL DEFAULT '0',
			ADD `EvElimArrows` TINYINT UNSIGNED NOT NULL DEFAULT '0',
			ADD `EvElimSO` TINYINT UNSIGNED NOT NULL DEFAULT '0',
			ADD `EvFinEnds` TINYINT UNSIGNED NOT NULL DEFAULT '0',
			ADD `EvFinArrows` TINYINT UNSIGNED NOT NULL DEFAULT '0',
			ADD `EvFinSO` TINYINT UNSIGNED NOT NULL DEFAULT '0';
	";
	safe_w_sql($MySql);

/* butto dentro i valori di default per le gare che sono già nel db */

// individuali cumulativi
	$MySql="
		UPDATE
			Events
		SET
			EvElimEnds=2,EvElimArrows=6,EvElimSO=3,EvFinEnds=4,EvFinArrows=3,EvFinSO=3
		WHERE
			EvTeamEvent=0 AND EvMatchMode=0
	";
	safe_w_sql($MySql);

// individuali set
	$MySql="
		UPDATE
			Events
		SET
			EvElimEnds=3,EvElimArrows=6,EvElimSO=1,EvFinEnds=5,EvFinArrows=3,EvFinSO=1
		WHERE
			EvTeamEvent=0 AND EvMatchMode=1
	";
	safe_w_sql($MySql);

// team cumulativi no mixed
	$MySql="
		UPDATE
			Events
		SET
			EvElimEnds=4,EvElimArrows=6,EvElimSO=9,EvFinEnds=4,EvFinArrows=6,EvFinSO=9
		WHERE
			EvTeamEvent=1 AND EvMatchMode=0 AND EvMixedTeam=0
	";
	safe_w_sql($MySql);

// team cumulativi  mixed
	$MySql="
		UPDATE
			Events
		SET
			EvElimEnds=4,EvElimArrows=4,EvElimSO=6,	EvFinEnds=4,EvFinArrows=4,EvFinSO=6
		WHERE
			EvTeamEvent=1 AND EvMatchMode=0 AND EvMixedTeam=1
	";
	safe_w_sql($MySql);

	db_save_version('2010-09-15 09:35:00');
}

if($version<'2010-09-19 17:04:00') {
	$MySql="ALTER TABLE `TeamFinals`"
		. " CHANGE `TfSetPoints` `TfSetPoints` VARCHAR( 36 ) NOT NULL , "
		. " CHANGE `TfArrowstring` `TfArrowstring` VARCHAR( 72 ) NOT NULL , "
		. " CHANGE `TfTiebreak` `TfTiebreak` VARCHAR( 18 ) NOT NULL ,"
		. " CHANGE `TfArrowPosition` `TfArrowPosition` VARCHAR( 576 ) NOT NULL ,"
		. " CHANGE `TfTiePosition` `TfTiePosition` VARCHAR( 144 ) NOT NULL ";
	safe_w_sql($MySql);
	$MySql="ALTER TABLE `Finals` CHANGE `FinSetPoints` `FinSetPoints` VARCHAR( 36 ) NOT NULL ,"
		. " CHANGE `FinArrowstring` `FinArrowstring` VARCHAR( 24 ) NOT NULL ,"
		. " CHANGE `FinTiebreak` `FinTiebreak` VARCHAR( 9 ) NOT NULL ,"
		. " CHANGE `FinArrowPosition` `FinArrowPosition` VARCHAR( 240 ) NOT NULL ,"
		. " CHANGE `FinTiePosition` `FinTiePosition` VARCHAR( 72 ) NOT NULL";
	safe_w_sql($MySql);

	db_save_version('2010-09-19 17:04:00');
}

if($version<'2010-09-24 12:30:00') {
	$MySql="ALTER TABLE `HhtData` DROP PRIMARY KEY , ADD PRIMARY KEY ( `HdTournament` , `HdTargetNo` , `HdDistance` , `HdArrowStart` , `HdFinScheduling` , `HdTeamEvent` )";
	safe_w_sql($MySql);
	$MySql="ALTER TABLE `FinSchedule` ADD UNIQUE `FSTournament` ( `FSTournament` , `FSTeamEvent` , `FSEvent` , `FSMatchNo` , `FSScheduledDate` , `FSScheduledTime` )";
	safe_w_sql($MySql);

	db_save_version('2010-09-24 12:30:00');
}

if($version<'2010-09-24 15:00:00') {
	$MySql="ALTER TABLE `HhtData` ADD INDEX `HdTournament` ( `HdTournament` , `HdTeamEvent` , `HdHhtId` , `HdFinScheduling` , `HdTargetNo` )";
	safe_w_sql($MySql);
	$MySql="ALTER TABLE `FinSchedule` ADD `FSLetter` VARCHAR( 5 ) NOT NULL";
	safe_w_sql($MySql);
	$MySql="update FinSchedule f "
		. " inner join FinSchedule f2 on f.FSTournament=f2.FSTournament "
			. " and f.FSTeamEvent=f2.FSTeamEvent"
			. " and f.FSEvent=f2.FSEvent"
			. " and f.FSMatchNo=f2.FSMatchNo-1 "
			. " and f.FSTarget=f2.FSTarget 	"
			. " and f.FSGroup=f2.FSGroup 	"
			. " and f.FSScheduledDate=f2.FSScheduledDate 	"
			. " and f.FSScheduledTime=f2.FSScheduledTime "
		. " set f.FSLetter=concat(f.FSTarget,'A'), f2.FSLetter=concat(f2.FSTarget,'B') "
		. "WHERE f.FSTarget>'' AND mod(f.FSMatchNo,2)=0 "
			;
	safe_w_sql($MySql);
	$MySql="update FinSchedule set FSLetter=concat(FSTarget,'A') WHERE FSTarget>'' AND FSLetter='' ";
	safe_w_sql($MySql);

	db_save_version('2010-09-24 15:00:00');
}

if($version<'2010-10-08 23:18:00') {
	$MySql="ALTER TABLE `BackNumber` ADD `BnCapitalFirstName` varchar(1) not null";
	safe_w_sql($MySql);

	db_save_version('2010-10-08 23:18:00');
}

if($version<'2010-10-10 17:31:00') {
	$MySql="ALTER TABLE `Targets` ADD"
		. " (A_size int(3) not null,"
		. " A_color varchar(6) not null,"
		. " B_size int(3) not null,"
		. " B_color varchar(6) not null,"
		. " C_size int(3) not null,"
		. " C_color varchar(6) not null,"
		. " D_size int(3) not null,"
		. " D_color varchar(6) not null,"
		. " E_size int(3) not null,"
		. " E_color varchar(6) not null,"
		. " F_size int(3) not null,"
		. " F_color varchar(6) not null,"
		. " G_size int(3) not null,"
		. " G_color varchar(6) not null,"
		. " H_size int(3) not null,"
		. " H_color varchar(6) not null,"
		. " I_size int(3) not null,"
		. " I_color varchar(6) not null,"
		. " J_size int(3) not null,"
		. " J_color varchar(6) not null,"
		. " K_size int(3) not null,"
		. " K_color varchar(6) not null,"
		. " L_size int(3) not null,"
		. " L_color varchar(6) not null,"
		. " M_size int(3) not null,"
		. " M_color varchar(6) not null)"
		;
	safe_w_sql($MySql);

	// update ALL indoor targets (valid for 1 and 5)
	$MySql="update `Targets` set"
		. " B_size=100,"
		. " B_color='FFFFFF',"
		. " C_size=90,"
		. " C_color='FFFFFF',"
		. " D_size=80,"
		. " D_color='000000',"
		. " E_size=70,"
		. " E_color='000000',"
		. " F_size=60,"
		. " F_color='00A3D1',"
		. " G_size=50,"
		. " G_color='00A3D1',"
		. " H_size=40,"
		. " H_color='ED2939',"
		. " I_size=30,"
		. " I_color='ED2939',"
		. " J_size=20,"
		. " J_color='F9E11E',"
		. " K_size=0,"
		. " K_color='F9E11E',"
		. " L_size=10,"
		. " L_color='F9E11E',"
		. " M_size=0,"
		. " M_color='F9E11E' "
		;
	safe_w_sql($MySql);

	// update Indoor Small targets (target 2)
	$MySql="update `Targets` set"
		. " B_size=0,"
		. " C_size=0,"
		. " D_size=0,"
		. " E_size=0,"
		. " F_size=0 "
		. "WHERE TarId=2"
		;
	safe_w_sql($MySql);

	// update CO Indoor Complete targets (target 3)
	$MySql="update `Targets` set"
		. " L_size=5 "
		. "WHERE TarId=3"
		;
	safe_w_sql($MySql);

	// update CO Indoor Small targets (target 4)
	$MySql="update `Targets` set"
		. " B_size=0,"
		. " C_size=0,"
		. " D_size=0,"
		. " E_size=0,"
		. " F_size=0,"
		. " L_size=5 "
		. "WHERE TarId=4"
		;
	safe_w_sql($MySql);

	// update CO Indoor Complete targets (target 5)
	$MySql="update `Targets` set"
		. " K_size=5 "
		. "WHERE TarId=5"
		;
	safe_w_sql($MySql);

	// update Field targets (target 6)
	$MySql="update `Targets` set"
		. " B_size=50,"
		. " B_color='000000',"
		. " C_size=40,"
		. " C_color='000000',"
		. " D_size=30,"
		. " D_color='000000',"
		. " E_size=20,"
		. " E_color='000000',"
		. " F_size=10,"
		. " F_color='F9E11E',"
		. " G_size=5,"
		. " G_color='F9E11E',"
		. " H_size=0,"
		. " I_size=0,"
		. " J_size=0,"
		. " L_size=0 "
		. "WHERE TarId=6"
		;
	safe_w_sql($MySql);

	// update HM targets (target 7)
	$MySql="update `Targets` set"
		. " A_size=16,"
		. " A_color='ED2939',"
		. " B_size=4,"
		. " B_color='F9E11E',"
		. " C_size=0,"
		. " D_size=0,"
		. " E_size=0,"
		. " F_size=0,"
		. " G_size=0,"
		. " H_size=0,"
		. " I_size=0,"
		. " J_size=0,"
		. " K_size=0,"
		. " L_size=0 "
		. "WHERE TarId=7"
		;
	safe_w_sql($MySql);

	$MySql="update `Targets` set"
		. " B_size=0,"
		. " C_size=0,"
		. " D_size=0,"
		. " E_size=0,"
		. " F_size=60,"
		. " I_color='000000'," // black
		. " G_size=0,"
		. " H_size=0,"
		. " I_size=30,"
		. " I_color='00A3D1'," // cyan
		. " J_size=0,"
		. " L_size=15,"
		. " L_color='ED2939'," // red
		. " M_size=5 "
		. "WHERE TarId=8"
		;
	safe_w_sql($MySql);

	// Inserts the Compound 50m Target
	$MySql="insert into `Targets` set"
		. " TarDescr='TrgCOOutdoor',"
		. " TarArray='TrgCOOutdoor',"
		. " F_size=60,"
		. " F_color='00A3D1',"
		. " G_size=50,"
		. " G_color='00A3D1',"
		. " H_size=40,"
		. " H_color='ED2939',"
		. " I_size=30,"
		. " I_color='ED2939',"
		. " J_size=20,"
		. " J_color='F9E11E',"
		. " K_size=5,"
		. " K_color='F9E11E',"
		. " L_size=10,"
		. " L_color='F9E11E' "
		;
	safe_w_sql($MySql);

	db_save_version('2010-10-10 17:31:00');
}

if($version<'2010-10-12 15:44:00') {
/* cambio delle domande del verbale */
	$MySql="
		UPDATE `FinalReportQ` SET `FrqOptions` = 'Non necessaria|No|Si' WHERE `FinalReportQ`.`FrqId` = 'a30';
	";
	safe_w_sql($MySql);

	$MySql="
		DELETE FROM `FinalReportQ` WHERE `FinalReportQ`.`FrqId` = 'm09';
	";
	safe_w_sql($MySql);

/* nuove tabelle */
	$MySql="
		CREATE TABLE IF NOT EXISTS `Session` (
		`SesTournament` INT( 10 ) UNSIGNED NOT NULL ,
		`SesOrder` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '0',
		`SesType` VARCHAR( 1 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'Q' COMMENT 'Q: qual E: elim F: final T: teamfinal',
		`SesName` VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
		`SesTar4Session` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '0',
		`SesAth4Target` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '0',
		`SesFirstTarget` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '0',
		`SesFollow` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0',
		PRIMARY KEY ( `SesTournament` , `SesOrder` , `SesType` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;
	";
	safe_w_sql($MySql);

	$MySql="
		CREATE TABLE IF NOT EXISTS `Scheduler` (
		`SchTournament` INT( 10 ) UNSIGNED NOT NULL ,
		`SchOrder` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '1',
		`SchDateStart` DATETIME NOT NULL DEFAULT '0000-00-00',
		`SchDateEnd` DATETIME NOT NULL DEFAULT '0000-00-00',
		`SchSesOrder` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '0',
		`SchSesType` VARCHAR( 1 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT 'Z' COMMENT 'Q: qual E: elim F: final T: teamfinal Z: freetext',
		`SchDescr` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '',
		PRIMARY KEY ( `SchTournament` , `SchOrder` , `SchDateStart` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;
	";
	safe_w_sql($MySql);

/* popolo Session pigliando i dati dalle colonne di sessione di Tournament per le gare presenti */
// gare presenti
	$q="
		SELECT
			ToId,
			ToNumSession,
			ToTar4Session1,ToTar4Session2,ToTar4Session3,
			ToTar4Session4,ToTar4Session5,ToTar4Session6,
			ToTar4Session7,ToTar4Session8,ToTar4Session9,
			ToAth4Target1,ToAth4Target2,ToAth4Target3,
			ToAth4Target4,ToAth4Target5,ToAth4Target6,
			ToAth4Target7,ToAth4Target8,ToAth4Target9
		FROM
			Tournament
		ORDER BY
			ToId ASC
	";
	$rs=safe_r_sql($q);

	if ($rs && safe_num_rows($rs)>0)
	{
		while ($myRow=safe_fetch($rs))
		{
			for ($i=1;$i<=$myRow->ToNumSession;++$i)
			{
				$ToTar4Session=$myRow->{'ToTar4Session' . $i};
				$ToAth4Target=$myRow->{'ToAth4Target' . $i};

				$q="
					REPLACE INTO Session (SesTournament,SesOrder,SesType,SesName,SesTar4Session,SesAth4Target,SesFirstTarget,SesFollow)
					VALUES({$myRow->ToId},{$i},'Q','',{$ToTar4Session},{$ToAth4Target},1,0)
				";
				$r=safe_w_sql($q);
			}
		}
	}

/*
 * ATTENZIONE!!!
 * Qui andrebbe la cancellazione dei vecchi campi
 *
 * ToTar4Session1,ToTar4Session2,ToTar4Session3,ToTar4Session4,ToTar4Session5,ToTar4Session6,ToTar4Session7,ToTar4Session8,ToTar4Session9,ToAth4Target1,ToAth4Target2,ToAth4Target3,ToAth4Target4,ToAth4Target5,ToAth4Target6,	ToAth4Target7,ToAth4Target8,ToAth4Target9
 *
 * da Tournament
 */


	db_save_version('2010-10-12 15:44:00');
}

if($version<'2010-10-18 15:32:00') {
	safe_w_sql("drop table if exists TargetFaces");

	$MySql="CREATE TABLE IF NOT EXISTS `TargetFaces` (
		`TfId` INT NOT NULL ,
		`TfTournament` INT NOT NULL ,
		`TfClasses` VARCHAR( 4 ) NOT NULL ,
		`TfT1` INT NOT NULL ,
		`TfW1` INT NOT NULL ,
		`TfT2` INT NOT NULL ,
		`TfW2` INT NOT NULL ,
		`TfT3` INT NOT NULL ,
		`TfW3` INT NOT NULL ,
		`TfT4` INT NOT NULL ,
		`TfW4` INT NOT NULL ,
		`TfT5` INT NOT NULL ,
		`TfW5` INT NOT NULL ,
		`TfT6` INT NOT NULL ,
		`TfW6` INT NOT NULL ,
		`TfT7` INT NOT NULL ,
		`TfW7` INT NOT NULL ,
		`TfT8` INT NOT NULL ,
		`TfW8` INT NOT NULL ,
		`TfDefault` varchar(1) NOT NULL default '',
		PRIMARY KEY ( `TfTournament` , `TfId` )
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT = 'Defines the faces to shoot on';";
	safe_w_sql($MySql);

	// update of the DB!
	$MySql = "select distinct ToId, ToType, ToNumDist, td.Td1, td.Td2, td.Td3, td.Td4, td.Td5, td.Td6, td.Td7, td.Td8 "
		. "from"
		. " Tournament"
		. " INNER JOIN Divisions on DivTournament=ToId and DivAthlete='1' "
		. " INNER JOIN Classes on ClTournament=ToId and ClAthlete='1'  "
		. " LEFT JOIN TournamentDistances td on td.TdTournament in (0,ToId) and TdType=ToType and concat(trim(DivId), trim(ClId)) like TdClasses "
		. "WHERE td.Td1>'' order by ToId"
		;

	$q=safe_r_sql($MySql);
	$oldTour='';
	$Query="REPLACE into TargetFaces set TfTournament=%s, TfClasses='%s', TfId=%s, TfDefault='1'";
	while($r=safe_fetch($q)) {
		// insert the known data
		if($oldTour==$r->ToId) continue;
		$oldTour=$r->ToId;
		switch($r->ToType) {
			case 2: //	Type_2xFITA
				$Straight1=($r->Td1>=$r->Td2); // FITA 90-70-50-30
				$Straight2=($r->Td5>=$r->Td6); // FITA 90-70-50-30
				$Sql = $Query . ', TfT1=5, TfW1='.($Straight1?122:80).', TfT2=5, TfW2='.($Straigh1t?122:80).', TfT3=5, TfW3='.($Straight1?80:122).', TfT4=5, TfW4='.($Straight1?80:122).', TfT5=5, TfW5='.($Straight2?122:80).', TfT6=5, TfW6='.($Straight2?122:80).', TfT7=5, TfW7='.($Straight2?80:122).', TfT8=5, TfW8='.($Straight2?80:122).'';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				$Sql = $Query . ', TfT1=5, TfW1='.($Straight1?60:122).', TfT2=5, TfW2=80, TfT3=5, TfW3=80, TfT4=5, TfW4='.($Straight1?122:60).', TfT5=5, TfW5='.($Straight2?60:122).', TfT6=5, TfW6=80, TfT7=5, TfW7=80, TfT8=5, TfW8='.($Straight2?122:60).'';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'VI%', 2));
				break;
			case 1: // Type_FITA
			case 4: //	Type_FITA 72
				$Straight=($r->Td1>=$r->Td2); // FITA 90-70-50-30
				$Sql = $Query . ', TfT1=5, TfW1='.($Straight?122:80).', TfT2=5, TfW2='.($Straight?122:80).', TfT3=5, TfW3='.($Straight?80:122).', TfT4=5, TfW4='.($Straight?80:122).'';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				$Sql = $Query . ', TfT1=5, TfW1='.($Straight?60:122).', TfT2=5, TfW2=80, TfT3=5, TfW3=80, TfT4=5, TfW4='.($Straight?122:60).'';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'VI%', 2));
				break;
			case 5: //	Type_900 Round
				$Sql = $Query . ', TfT1=5, TfW1=122, TfT2=5, TfW2=122, TfT3=5, TfW3=122';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
			case 3: //	Type_70m Round
				$Sql = $Query . ', TfT1=5, TfW1=122, TfT2=5, TfW2=122';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				$Sql = $Query . ', TfT1=5, TfW1=80, TfT2=5, TfW2=80';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'VI%', 2));
				break;
			case 8: //	Type_Indoor 25+18
				// Catch All
				$Sql = $Query . ', TfT1=1, TfW1=60, TfT2=1, TfW2=60, TfT3=1, TfW3=40, TfT4=1, TfW4=40';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				// Compound
				$Sql = $Query . ', TfT1=4, TfW1=60, TfT2=4, TfW2=60, TfT3=4, TfW3=40, TfT4=4, TfW4=40';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'C%', 2));
				// Giovanissimi others
				$Sql = $Query . ', TfT1=1, TfW1=80, TfT2=1, TfW2=80, TfT3=1, TfW3=60, TfT4=1, TfW4=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, '__G_', 3));
				// Giovanissimi CO
				$Sql = $Query . ', TfT1=4, TfW1=80, TfT2=4, TfW2=80, TfT3=4, TfW3=60, TfT4=4, TfW4=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'COG_', 4));
				// VI
				$Sql = $Query . ', TfT1=1, TfW1=80, TfT2=1, TfW2=80, TfT3=1, TfW3=60, TfT4=1, TfW4=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'VI%', 5));
				break;
			case 7: //	Type_Indoor 25
				// Catch All
				$Sql = $Query . ', TfT1=1, TfW1=60, TfT2=1, TfW2=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				// Compound
				$Sql = $Query . ', TfT1=4, TfW1=60, TfT2=4, TfW2=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'C%', 2));
				// Giovanissimi others
				$Sql = $Query . ', TfT1=1, TfW1=80, TfT2=1, TfW2=80';
				safe_w_SQL(sprintf($Sql, $r->ToId, '__G_', 3));
				// Giovanissimi CO
				$Sql = $Query . ', TfT1=4, TfW1=80, TfT2=4, TfW2=80';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'COG_', 4));
				// VI
				$Sql = $Query . ', TfT1=1, TfW1=80, TfT2=1, TfW2=80';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'VI%', 5));
				break;
			case 6: //	Type_Indoor 18
				// Catch All
				$Sql = $Query . ', TfT1=1, TfW1=40, TfT2=1, TfW2=40';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				// Compound
				$Sql = $Query . ', TfT1=4, TfW1=40, TfT2=4, TfW2=40';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'C%', 2));
				// Giovanissimi others
				$Sql = $Query . ', TfT1=1, TfW1=60, TfT2=1, TfW2=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, '__G_', 3));
				// Giovanissimi CO
				$Sql = $Query . ', TfT1=4, TfW1=60, TfT2=4, TfW2=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'COG_', 4));
				// VI
				$Sql = $Query . ', TfT1=1, TfW1=60, TfT2=1, TfW2=60';
				safe_w_SQL(sprintf($Sql, $r->ToId, 'VI%', 5));
				break;
			case 10: //	Type_HF 24+24
			case 12: //	Type_HF 12+12
				$Sql = $Query . ', TfT1=6, TfW1=0, TfT2=6, TfW2=0';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
			case 9: //	Type_HF 12+12
				$Sql = $Query . ', TfT1=6, TfW1=0';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
			case 13: //	3D
				$Sql = $Query . ', TfT1=8, TfW1=0, TfT2=8, TfW2=0';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
			case 11: //	3D
				$Sql = $Query . ', TfT1=8, TfW1=0';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
			case 14: //	Type_Las Vegas: EVERYBODY shoots on large 10 ring, CO shoot on small target
				$Sql = $Query . ', TfT1=1, TfW1=40, TfT2=1, TfW2=40, TfT3=1, TfW3=40, TfT4=1, TfW4=40';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
			case 15: //	Type_GiochiGioventu
			case 16: //	Type_GiochiGioventuWinter
				$Sql = $Query . ', TfT1=5, TfW1=80, TfT2=5, TfW2=80';
				safe_w_SQL(sprintf($Sql, $r->ToId, '%', 1));
				break;
		}
	}

	$MySql="ALTER TABLE `Entries` ADD `EnTargetFace` INT NOT NULL";
	safe_w_sql($MySql);

	$q=safe_r_SQL("select distinct EnId, TfId from
		Entries INNER join
		TargetFaces
		on EnTournament=TfTournament and concat(trim(EnDivision),trim(EnClass)) like TfClasses
		order by
		EnId,
		concat(trim(EnDivision),trim(EnClass)) = TfClasses desc,
		left(TfClasses,1)!='\_' and left(TfClasses,1)!='\%' desc,
		left(TfClasses,1)='\_' desc,
		TfClasses='\%'");
	while($r=safe_fetch($q)) {
		$MySql="Update `Entries` set EnTargetFace=$r->TfId where EnId=$r->EnId and EnTargetFace=0";
		safe_w_sql($MySql);
	}


	// Alter the Target table to insert a "dummy" line
	$MySql="Alter table `Targets` Add TarDummyLine int NOT NULL"		;
	safe_w_sql($MySql);

	$MySql="update Targets set TarDummyLine=10 where TarId in (3,4)";
	safe_w_sql($MySql);

	// Inserts the Compound 30m Target
	$MySql="REPLACE into `Targets` set"
		. " TarDescr='TrgCOOutdoorSmall',"
		. " TarArray='TrgCOOutdoorSmall',"
		. " G_size=50,"
		. " G_color='00A3D1',"
		. " H_size=40,"
		. " H_color='ED2939',"
		. " I_size=30,"
		. " I_color='ED2939',"
		. " J_size=20,"
		. " J_color='F9E11E',"
		. " K_size=5,"
		. " K_color='F9E11E',"
		. " L_size=10,"
		. " L_color='F9E11E' "
		;
	safe_w_sql($MySql);

	db_save_version('2010-10-18 15:32:00');
}

if($version<'2010-10-19 20:00:00') {
	safe_w_sql("Alter table Targets add TarOrder int not null after TarArray");
	safe_w_sql("update Targets set TarOrder = TarId+3");
	safe_w_sql("update Targets set TarOrder = 1 where TarId=5");
	safe_w_sql("update Targets set TarOrder = 2 where TarId=9");
	safe_w_sql("update Targets set TarOrder = 3 where TarId=10");
	safe_w_sql("update Targets set TarOrder = 8 where TarId=6");
	safe_w_sql("update Targets set TarOrder = 9 where TarId=8");
	safe_w_sql("update Targets set TarOrder = 10 where TarId=7");

	db_save_version('2010-10-19 20:00:00');
}

if($version<'2010-10-20 08:00:00') {
	$MySql = "select ToId from Tournament where ToType=6";
	$q=safe_r_sql($MySql);
	$Query="update TargetFaces set %s where TfTournament=%s and TfClasses='%s' and TfId=%s";
	while($r=safe_fetch($q)) {
		// Catch All
		safe_w_SQL(sprintf($Query, 'TfW1=40, TfW2=40', $r->ToId, '%', 1));
		// Compound
		safe_w_SQL(sprintf($Query, 'TfW1=40, TfW2=40', $r->ToId, 'C%', 2));
		// Giovanissimi others
		safe_w_SQL(sprintf($Query, 'TfW1=60, TfW2=60', $r->ToId, '__G_', 3));
		// Giovanissimi CO
		safe_w_SQL(sprintf($Query, 'TfW1=60, TfW2=60', $r->ToId, 'COG_', 4));
		// VI
		safe_w_SQL(sprintf($Query, 'TfW1=60, TfW2=60', $r->ToId, 'VI%', 5));
	}

	$MySql = "alter table TargetFaces add TfName varchar(15) not null after TfId";
	safe_w_SQL($MySql);

	safe_w_sql("update TargetFaces set TfName='~Default' where TfClasses='%' and TfDefault='1'");
	safe_w_sql("update TargetFaces set TfName='~DefaultCO' where TfClasses like 'C%' and TfDefault='1'");
	safe_w_sql("update TargetFaces set TfName='~DefaultVI' where TfClasses = 'VI%' and TfDefault='1'");
	safe_w_sql("update TargetFaces set TfName='Default G' where TfClasses = '__G_' and TfDefault='1'");
	safe_w_sql("update TargetFaces set TfName='Default COG' where TfClasses = 'COG_' and TfDefault='1'");


	db_save_version('2010-10-20 08:00:00');
}

if($version<'2010-10-26 11:30:00') {
// tolgo i campi ToAth4Target[i] e ToTar4Session[i] 1<=i<=9
	$MySql="
		ALTER TABLE `Tournament`
		  DROP `ToTar4Session1`,
		  DROP `ToTar4Session2`,
		  DROP `ToTar4Session3`,
		  DROP `ToTar4Session4`,
		  DROP `ToTar4Session5`,
		  DROP `ToTar4Session6`,
		  DROP `ToTar4Session7`,
		  DROP `ToTar4Session8`,
		  DROP `ToTar4Session9`,
		  DROP `ToAth4Target1`,
		  DROP `ToAth4Target2`,
		  DROP `ToAth4Target3`,
		  DROP `ToAth4Target4`,
		  DROP `ToAth4Target5`,
		  DROP `ToAth4Target6`,
		  DROP `ToAth4Target7`,
		  DROP `ToAth4Target8`,
		  DROP `ToAth4Target9`;
	";
	safe_w_SQL($MySql,null,array(1060));

	db_save_version('2010-10-26 11:30:00');
}

if($version<'2010-11-08 11:48:00') {
/*
 *  Prima aggiungo i campi nuovi alla tabella Eliminations senza però alterare la chiave!
 *  Non posso impostarla ora perchè non ho ancora le nuove colonne valorizzate
 */
	$MySql="
		ALTER TABLE `Eliminations` ADD `ElTournament` INT UNSIGNED NOT NULL AFTER `ElEventCode` ,
			ADD `ElQualRank` SMALLINT( 6 ) NOT NULL AFTER `ElTournament`;
	";
	safe_w_SQL($MySql,null,array(1060));

/*
 * Popolo le nuove colonne
 */

/*
 * 1) Imposto il torneo
 */
	$q="
		UPDATE
			Eliminations INNER JOIN Entries ON ElId=EnId
		SET
			ElTournament=EnTournament
	";
	$r=safe_w_sql($q);

/*
 *  2) eventi con le eliminatorie:
 *  sicuramente hanno EvElim2<>0 perchè in caso di nu girone solo è quello che viene popolato
 */
	$q="
		SELECT
			EvCode,EvTeamEvent,EvTournament,EvElim1,EvElim2
		FROM
			Events
		WHERE
			EvTeamEvent=0 AND EvElim2<>0
		ORDER BY
			EvTournament ASC,EvCode ASC
	";
	$r=safe_r_sql($q);

// 3) per ogni evento nella lista devo fare gli aggiornamenti delle righe di Eliminations
	if ($r && safe_num_rows($r)>0)
	{
		while ($row=safe_fetch($r))
		{
		/*
		 * Una sola fase quindi in Eliminations per questo evento avrò solo
		 * le ElElimPhase=1 e piglio la rank da Qualifications
		 */
			if ($row->EvElim1==0)
			{
				$q="
					UPDATE
						Eliminations
						INNER JOIN
							Qualifications
						ON ElId=QuId AND ElElimPhase=1

						INNER JOIN
							Entries
						ON QuId=EnId AND EnTournament={$row->EvTournament}

					SET
						ElQualRank=QuRank

					WHERE
						ElEventCode='{$row->EvCode}' AND ElElimPhase=1 AND EnTournament={$row->EvTournament}
				";
				$rs=safe_w_sql($q);
			}
		/*
		 * Due fasi quindi:
		 * 1) Le righe con ElElimPhase=0 pigliano da Qualifications mentre
		 * 2) quelle con ElElimPhase=1 lo fanno da Eliminations con ElElimPhase=0
		 */
			else
			{

			// prima la fase 2 (in realtà l'ordine non è importante)
				$q="
					UPDATE
						Eliminations AS e1
							INNER JOIN Eliminations AS e2
						ON e1.ElId = e2.ElId AND e1.ElElimPhase =0 AND e2.ElElimPhase =1 AND e1.ElEventCode = e2.ElEventCode

						INNER JOIN
							Entries
						ON e1.ElId = EnId AND EnTournament ={$row->EvTournament}
					SET
						e2.ElQualRank=e1.ElRank
					WHERE
						e1.ElEventCode='{$row->EvCode}'
				";
				$rs=safe_w_sql($q);

			// ora aggiorno la prima fase con QuRank
				$q="
					UPDATE
						Eliminations
						INNER JOIN
							Qualifications
						ON ElId=QuId AND ElElimPhase=0

						INNER JOIN
							Entries
						ON QuId=EnId AND EnTournament={$row->EvTournament}

					SET
						ElQualRank=QuRank

					WHERE
						ElEventCode='{$row->EvCode}' AND ElElimPhase=0 AND EnTournament={$row->EvTournament}
				";
				$rs=safe_w_sql($q);
			}
		}
	}
/*
 * Ora posso alterare la chiave
 */

// cancello la precedente
	$q="
		ALTER TABLE `Eliminations` DROP PRIMARY KEY
	";
	$r=safe_w_sql($q,null,array(1060,1091));

	// check if we don't have duplicate rows...
	$SQL="select count(*) as quanti, ElElimPhase, ElEventCode, ElTournament, ElQualRank from Eliminations group by ElElimPhase, ElEventCode, ElTournament, ElQualRank having quanti>1 or ElQualRank=0";
	$q=safe_r_sql($SQL);
	while(safe_num_rows($q)) {
		while($r=safe_fetch($q)) {
			safe_w_sql("update Eliminations set ElQualRank=".($r->ElQualRank+1)." where ElElimPhase=$r->ElElimPhase and ElEventCode='$r->ElEventCode' and ElTournament=$r->ElTournament and ElQualRank=$r->ElQualRank limit 1");
		}
		$SQL="select count(*) as quanti, ElElimPhase, ElEventCode, ElTournament, ElQualRank from Eliminations group by ElElimPhase, ElEventCode, ElTournament, ElQualRank having quanti>1 or ElQualRank=0";
		$q=safe_r_sql($SQL);
	}

	// create new key
	$q="ALTER TABLE `Eliminations` ADD PRIMARY KEY ( `ElElimPhase` , `ElEventCode` , `ElTournament` , `ElQualRank` )";
	$r=safe_w_sql($q,null,array(1060));

	db_save_version('2010-11-08 11:48:00');
}

if($version<'2010-11-18 14:53:00') {
// aggiungo la colonna IndSO che vale 1 se c'è lo spareggio (giallo o rosso)
	$MySql="
		ALTER TABLE `Individuals` ADD `IndSO` TINYINT UNSIGNED NOT NULL DEFAULT '0' AFTER `IndRankFinal`
	";

	safe_w_SQL($MySql,null,array(1060));
	db_save_version('2010-11-18 14:53:00');
}

if($version<'2010-11-22 18:36:00') {
// aggiungo due indici per la cleazione "Rapida" della tabella Individuals
	$MySql="ALTER TABLE `EventClass` ADD INDEX `MakeIndividuals` (`EcTeamEvent`, `EcTournament`, `EcClass`, `EcDivision`)";
	safe_w_SQL($MySql,null,array(1060,1061));
	$MySql="ALTER TABLE `Entries` ADD INDEX `EnTournament` (`EnTournament`)";
	safe_w_SQL($MySql,null,array(1060,1061));
	db_save_version('2010-11-22 18:36:00');
}

if($version<'2010-11-24 09:40:00') {
// aggiungo due indici per la cleazione "Rapida" della tabella Individuals
	$MySql="ALTER TABLE `Individuals` ADD `IndTimestampFinal` TIMESTAMP NULL DEFAULT NULL ";
	safe_w_SQL($MySql,null,array(1060));

	db_save_version('2010-11-24 09:40:00');
}

if($version<'2010-11-28 12:26:00') {
	$MySql="ALTER TABLE `BackNumber` ADD `BnGivenNameInitial` varchar(1) NOT NULL ";
	safe_w_SQL($MySql,null,array(1060));
	$MySql="ALTER TABLE `BackNumber` ADD `BnCountryCodeOnly` varchar(1) NOT NULL ";
	safe_w_SQL($MySql,null,array(1060));

	db_save_version('2010-11-28 12:26:00');
}

if($version<'2010-11-28 16:00:00') {
	// aggiungo due indici per la cleazione "Rapida" della tabella Individuals
	$MySql="ALTER TABLE `Qualifications` CHANGE `QuTimestamp` `QuTimestamp` DATETIME NULL DEFAULT NULL";
	safe_w_SQL($MySql);
	$MySql="ALTER TABLE `Individuals`
		CHANGE `IndTimestamp` `IndTimestamp` DATETIME NULL DEFAULT NULL ,
		CHANGE `IndTimestampFinal` `IndTimestampFinal` DATETIME NULL DEFAULT NULL";
	safe_w_SQL($MySql);
	$MySql="ALTER TABLE `Entries` ADD `EnTimestamp` DATETIME NULL DEFAULT NULL";
	safe_w_SQL($MySql,false,array(1060));
	db_save_version('2010-11-26 15:50:00');
	$MySql="Update Entries INNER JOIN Qualifications On EnId=QuId Set EnTimestamp=QuTimestamp";
	safe_w_SQL($MySql);
}

if($version<'2010-11-29 11:03:00') {
// aggiungo la colonna x salvare se quella rank ha dei pari oppure no.
	$MySql="ALTER TABLE `Eliminations` ADD `ElSO` TINYINT( 3 ) UNSIGNED NOT NULL DEFAULT '0' AFTER `ElRank`; ";
	safe_w_SQL($MySql,false,array(1060));
	db_save_version('2010-11-29 11:03:00');
}

if($version<'2010-12-03 16:40:00') {
// aggiungo la colonna x le frecce di spareggio delle eliminatorie
	$MySql="ALTER TABLE `Eliminations` ADD `ElTiebreak` VARCHAR( 8 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `ElArrowString` ; ";
	safe_w_SQL($MySql,false,array(1060));
	db_save_version('2010-12-03 16:40:00');
}

if($version<'2010-12-11 20:30:00') {
	$MySql="ALTER TABLE `Individuals` CHANGE `IndSO` `IndSO` SMALLINT NOT NULL DEFAULT '0'";
	safe_w_SQL($MySql);

	$MySql="ALTER TABLE `Eliminations` CHANGE `ElSO` `ElSO` SMALLINT NOT NULL DEFAULT '0'";
	safe_w_SQL($MySql);

	$MySql = "ALTER TABLE `HhtData` ADD `HdRealTargetNo` VARCHAR( 3 ) NOT NULL ,
		ADD `HdLetter` VARCHAR( 1 ) NOT NULL";
	safe_w_SQL($MySql,false,array(1060));


	$MySql="SELECT ToId From Tournament";
	$rs=safe_r_SQL($MySql);
	while($myRow=safe_fetch($rs))
		recalculateIndividuals_20101211($myRow->ToId);

	//Riporto i QuRank e le freccie di Shootoff dalla tabella qualifications
	$MySql="UPDATE Entries
		INNER JOIN Qualifications On QuId=EnId
		INNER JOIN EventClass ON EcTeamEvent=0 AND EcTournament=EnTournament AND EcClass=EnClass AND EcDivision=EnDivision
		INNER JOIN Individuals ON IndId=EnId AND IndEvent=EcCode AND IndTournament=EnTournament
		SET IndRank=QuRank, IndTieBreak=QuTieBreak
		WHERE QuRank!=0 AND (QuRank!=IndRank OR QutieBreak!=IndTieBreak)";
	safe_w_SQL($MySql);

	//riporto la posizione dalla tabella finali... mi serve solo per partecipazioni ad eventi multi pli sennò bastava query sopra
	$MySql = "UPDATE Individuals
		INNER JOIN Finals ON IndId=FinAthlete AND IndEvent=FinEvent AND IndTournament=FinTournament
		INNER JOIN Events ON EvCode=FinEvent AND EvTeamEvent=0 AND EvTournament=FinTournament
		INNER JOIN Grids ON GrMatchNo=FinMatchNo AND GrPhase=IF(EvFinalFirstPhase=24,32,EvFinalFirstPhase)
		SET IndRank=GrPosition
		WHERE FinAthlete!=0 AND (EvElim1=0 AND EvElim2=0)";
	safe_w_SQL($MySql);
	db_save_version('2010-12-11 20:30:00');
}

?>