<?php

if($version<'2013-01-03 15:27:00') {
	safe_w_sql("update TVContents set
			TVCContent=".strsafe_DB(file_get_contents(dirname(__FILE__).'/Images/ianseo.png')).",
			TVCMimeType='image/png'
			where TVCId=1 and TVCTournament=-1");

	db_save_version('2013-01-03 15:27:00');
}

if($version<'2013-01-20 17:45:00') {
	safe_w_sql("drop table if exists Vegas ");
	safe_w_sql("CREATE TABLE IF NOT EXISTS Vegas (
		VeId int(10) unsigned NOT NULL,
		VeArrowstring varchar(90) NOT NULL,
		VeScore smallint(6) NOT NULL,
		VeX smallint(6) NOT NULL,
		VeRank smallint(6) NOT NULL,
		VeSubClass varchar(2) NOT NULL,
		VeTimestamp datetime DEFAULT NULL,
		PRIMARY KEY (VeId),
		index VeScore (VeScore,VeX)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8");
	db_save_version('2013-01-20 17:45:00');
}

if($version<'2013-01-21 18:10:00') {
	$q="ALTER TABLE `Tournament` ADD `ToTimeZone` varchar(6) NOT NULL DEFAULT '' AFTER `ToIocCode` ";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-01-21 18:10:00');
}

if($version<'2013-01-21 21:10:00') {
	$q="ALTER TABLE `Tournament` change `ToTimeZone` `ToTimeZone` varchar(50) NOT NULL DEFAULT '' ";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-01-21 21:10:00');
}

if($version<'2013-01-22 13:10:00') {
	$q="ALTER TABLE `Tournament` change `ToTimeZone` `ToTimeZone` varchar(50) NOT NULL DEFAULT '' ";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-01-22 13:10:00');
}

if($version<'2013-01-25 15:10:00') {
	safe_w_sql("drop table if exists VegasAwards ");
	$q="CREATE TABLE IF NOT EXISTS VegasAwards (
		VaTournament int(11) NOT NULL,
		VaDivision varchar(2) NOT NULL,
		VaClass varchar(2) NOT NULL,
		VaSubClass varchar(2) NOT NULL,
		VaRank int(10) NOT NULL,
		VaAward float(15,2) not null DEFAULT 0,
		VaToDelete TINYINT not null default 0,
		PRIMARY KEY (VaTournament, VaDivision, VaClass, VaSubClass, VaRank)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	$r=safe_w_sql($q,false,array(1060));

	// not every country has "strong" money!
	$q="alter TABLE AccPrice change APPrice APPrice float(15,2) not null DEFAULT 0";
	$r=safe_w_sql($q,false,array(1060));
	db_save_version('2013-01-25 15:10:00');
}

if($version<'2013-02-01 12:37:00') {
	safe_w_sql("update InvolvedType set ItOc=ItOc+1 where ItOc>0");
	safe_w_sql("update InvolvedType set ItOc=ItOc+1 where ItOc>4");
	$q="insert into InvolvedType value (13, 'TecDelegate', 0, 0, 0, 1), (14, 'SportPres', 0, 0, 0, 5)";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-02-01 12:37:00');
}

if($version<'2013-02-06 20:00:00') {
	$q="ALTER TABLE `Qualifications` ADD `QuBacknoPrinted` DATETIME NOT NULL AFTER `QuTargetNo` ";
	$r=safe_w_sql($q,false,array(1060));
	db_save_version('2013-02-06 20:00:00');
}

if($version<'2013-02-21 21:30:00') {
	$q="ALTER TABLE  `LookUpEntries` CHANGE  `LueCtrlCode`  `LueCtrlCode` DATE NULL DEFAULT NULL ";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-02-21 21:30:00');
}

if($version<'2013-03-14 10:09:00') {
	// Adds the subrule definition
	$q="replace into LookUpPaths SET LupPath = 'http://www.asta-sbv.ch/var/ianseo/IanseoData.php', LupIocCode = 'SUI'";
	$r=safe_w_sql($q,false,array(1061,1091));

	db_save_version('2013-03-14 10:09:00');
}

if($version<'2013-04-01 16:21:00') {
	safe_w_sql("insert into TourTypes set TtId=23, TtType='Type_Bel_25m_Out', TtDistance=2, TtOrderBy=23 on duplicate key update TtType='Type_Bel_25m_Out', TtDistance=2, TtOrderBy=23");
	safe_w_sql("insert into TourTypes set TtId=24, TtType='Type_Bel_50-30_Out', TtDistance=2, TtOrderBy=24 on duplicate key update TtType='Type_Bel_50-30_Out', TtDistance=2, TtOrderBy=24");
	safe_w_sql("insert into TourTypes set TtId=25, TtType='Type_Bel_50_Out', TtDistance=2, TtOrderBy=25 on duplicate key update TtType='Type_Bel_50_Out', TtDistance=2, TtOrderBy=25");
	safe_w_sql("insert into TourTypes set TtId=26, TtType='Type_Bel_B10_Out', TtDistance=2, TtOrderBy=26 on duplicate key update TtType='Type_Bel_B10_Out', TtDistance=2, TtOrderBy=26");
	safe_w_sql("insert into TourTypes set TtId=27, TtType='Type_Bel_B15_Out', TtDistance=2, TtOrderBy=27 on duplicate key update TtType='Type_Bel_B15_Out', TtDistance=2, TtOrderBy=27");
	safe_w_sql("insert into TourTypes set TtId=28, TtType='Type_Bel_B25_Out', TtDistance=2, TtOrderBy=28 on duplicate key update TtType='Type_Bel_B25_Out', TtDistance=2, TtOrderBy=28");
	safe_w_sql("insert into TourTypes set TtId=29, TtType='Type_Bel_B50-30_Out', TtDistance=2, TtOrderBy=29 on duplicate key update TtType='Type_Bel_B50-30_Out', TtDistance=2, TtOrderBy=29");
	safe_w_sql("insert into TourTypes set TtId=30, TtType='Type_Bel_BFITA_Out', TtDistance=4, TtOrderBy=30 on duplicate key update TtType='Type_Bel_BFITA_Out', TtDistance=4, TtOrderBy=30");

	db_save_version('2013-04-01 16:21:00');
}

if($version<'2013-04-03 12:00:00') {
	$q="ALTER TABLE `Finals` ADD `FinConfirmed` int(4) not null";
	$r=safe_w_sql($q,false,array(1060));
	$q="ALTER TABLE `TeamFinals` ADD `TfConfirmed` int(4) not null";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-04-03 12:00:00');
}

if($version<'2013-04-05 18:56:00') {
	$q="drop view EventCategories";
	$r=safe_w_sql($q,false,array(1060));
	$q="create view EventCategories as select * from `EventClass` inner join `Events` on EvCode = EcCode and if(EvTeamEvent=0, EcTeamEvent=0, EcTeamEvent>0) and EvTournament = EcTournament";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-04-05 18:56:00');
}

if($version<'2013-04-19 12:15:00') {
	$q="ALTER TABLE  `Awards` CHANGE  `AwAwarders`  `AwAwarders` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-04-19 12:15:00');
}

if($version<'2013-04-24 14:15:00') {
	$q="REPLACE INTO `TourTypes` (`TtId`, `TtType`, `TtDistance`, `TtOrderBy`) VALUES (31, 'Type_ITA_Sperimental', 2, 31)";
	$r=safe_w_sql($q,false,array(1060));

	$q="REPLACE INTO `LookUpPaths` (`LupIocCode`, `LupFors`, `LupPath`, `LupPhotoPath`, `LupFlagsPath`, `LupLastUpdate`) VALUES
		('ITA_e', '', 'http://www.fitarco-italia.org/gare/ianseo/IanseoDataEsordienti.php', 'http://www.fitarco-italia.org/gare/ianseo/IanseoPhoto.php', 'http://www.fitarco-italia.org/gare/ianseo/IanseoFlags.php', '0000-00-00 00:00:00')";
	$r=safe_w_sql($q,false,array(1060));

	$q="REPLACE INTO `LookUpPaths` (`LupIocCode`, `LupFors`, `LupPath`, `LupPhotoPath`, `LupFlagsPath`, `LupLastUpdate`) VALUES
		('ITA_p', '', 'http://www.fitarco-italia.org/gare/ianseo/IanseoDataPinocchio.php', 'http://www.fitarco-italia.org/gare/ianseo/IanseoPhoto.php', 'http://www.fitarco-italia.org/gare/ianseo/IanseoFlags.php', '0000-00-00 00:00:00')";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-04-24 14:15:00');

}

if($version<'2013-05-02 08:11:01') {
	$q="ALTER TABLE `Tournament` ADD `ToOnlineId` int(10) NOT NULL DEFAULT '0' AFTER `ToId` ";
	$r=safe_w_sql($q,false,array(1060));
	$q="ALTER TABLE `Entries` ADD `EnOnlineId` int(10) NOT NULL DEFAULT '0' AFTER `EnId` ";
	$r=safe_w_sql($q,false,array(1060));
	$q="ALTER TABLE `Countries` ADD `CoOnlineId` int(10) NOT NULL DEFAULT '0' AFTER `CoId` ";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-05-02 08:11:01');
}

if($version<'2013-05-02 12:11:00') {
	$r=safe_w_sql("ALTER TABLE  CasGrid ENGINE = MYISAM ",false,array(1060));
	$r=safe_w_sql("ALTER TABLE  CasScore ENGINE = MYISAM ",false,array(1060));
	$r=safe_w_sql("ALTER TABLE  CasTeam ENGINE = MYISAM ",false,array(1060));
	$r=safe_w_sql("ALTER TABLE  CasTeamFinal ENGINE = MYISAM ",false,array(1060));
	$r=safe_w_sql("ALTER TABLE  CasTeamTarget ENGINE = MYISAM ",false,array(1060));

	db_save_version('2013-05-02 12:11:00');
}

if($version<'2013-06-13 11:00:00') {
	$q="update `Flags`
set FlSVG=0x7d90cb6e83400c45f7fd0acbdd82c7f30ad32a93acc2aebbf6035020804420821154fdfa0eb4ddf4b5b174afceb125ef8fafd70ee66a9cdaa1f7288911aafe3c946d5f7b7c79ce5387c7c3dd7e9a6b88643f796c42b83d0ab12c0b2d9a86b1168a9945241096b60c4d5c130b84a66aeb2678b431c41563750e7069bbcee3bdd20f27b3fb8717df840df895d699fd419f3266a9ff10a4fa106e4568be843ccf114a8f4fd6185289624baeb08674b20e6090090327ce9129768a6cb28ead96c0e95a5f53a9c9c1aa77ce904c55463ab50a324976de90ee33bd6de7d7871dde01
,  
, FlEntered='2013-06-13 08:17:14'
where FlTournament=-1
and FlCode='LBA'";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-06-13 11:00:00');
}

if($version<'2013-07-08 15:00:00') {
	$q="insert into Flags (select FlTournament, FlIocCode, 'TTO', FlSVG, FlJPG, FlEntered, FlChecked, FlContAssoc from Flags where FlIocCode='FITA' and FlCode='TRI')";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-07-08 15:00:00');
}
if($version<'2013-07-21 18:05:00') {
	$q="REPLACE INTO `LookUpPaths` (`LupIocCode`, `LupFors`, `LupPath`, `LupPhotoPath`, `LupFlagsPath`, `LupLastUpdate`) VALUES
		('NOR', '', 'http://nor.service.ianseo.net/IanseoData.php', '', '', ''),
		('NOR_s', '', 'http://nor.service.ianseo.net/IanseoData.php?ScoreClass=1', '', '', '')";
	$r=safe_w_sql($q,false,array());
	db_save_version('2013-07-21 18:05:00');
}

if($version<'2013-12-19 12:30:00') {
	$q="drop table if exists `DistanceInformation`";
	$r=safe_w_sql($q,false,array(1060));
	$q="CREATE TABLE if not exists `DistanceInformation` (
			`DiTournament` int(10) NOT NULL,
			`DiSession` tinyint(3) NOT NULL,
			`DiDistance` tinyint(3) NOT NULL,
			`DiEnds` tinyint(3) NOT NULL,
			`DiArrows` tinyint(3) NOT NULL,
			`DiMaxpoints` int(11) NOT NULL,
			`DiStart` datetime NOT NULL,
			`DiEnd` datetime NOT NULL,
			`DiWarmup` datetime NOT NULL,
			`DiOptions` text NOT NULL,
			PRIMARY KEY (`DiTournament`,`DiSession`,`DiDistance`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8";
	$r=safe_w_sql($q,false,array(1060));

	db_save_version('2013-12-19 12:30:00');
}
