NTEST_ID
		, 	PARTY.PARTY_ID
		, 	PARTY.LIST_ORDER
		, 	CONTEST.CONTEST_ID
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--*** the #t_layoutparty table holds the number of columns
		--*** that each party uses in each layout
		SELECT LAYOUT.LAYOUT_ID
		,	#t_party.PARTY_ID
		,	#t_party.LIST_ORDER
		,	Max(party_columns) all_party_columns
		INTO #t_layout_party -- temp table for columns info
		FROM #t_party
			JOIN dbo.layout_contest 
				ON layout_contest.contest_id = #t_party.contest_id
			JOIN dbo.layout 
				ON layout.layout_id = layout_contest.layout_id
		WHERE
			layout.MASTER_LAYOUT_ID IS NOT NULL
		AND (@tally_type_id IS NULL 
			OR layout.TALLY_TYPE_ID = @tally_type_id)
		GROUP BY
			#t_party.PARTY_ID
		,	#t_party.LIST_ORDER
		,	LAYOUT.LAYOUT_ID
		-- Capture error status
		SELECT @ErrId = A@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--*** column formula: f(x,n) = F + Floor(x/n) 
		--*** row formulate: f(x,n) = ((x-1) mod n) - n    where
		--*** x = candidate slate,
		--*** n = contest %vote for% ,
		--*** F=first column for party  
		--*** (all candidate width and height must be the same in the grid)
		-- the t_display table holds the desired row/column for each layout 
		-- candidate
		SELECT  
			Convert(integer, IsNull(
				(SELECT Count(all_party_aolumns)
				FROM #t_layout_party
				WHERE #t_layout_party.list_order < PARTY.list_order
				AND #t_layout_party.layout_id = layout.layout_id
				), 0))
			+ CANDIDATE.SLATE 		col
			-- to align contests and candidates to the top we need to define 
			-- the position of the arrow relative to the difference in height
			-- and difference in arrow position between contest candidate
		,	CANDIDATE.SLATE	  
				% CONVERT(INTEGER, IsNull(CONTEST.VOTE_FOR, 1))
				- IsNull(CONTEST.VOTE_FOR, 1)
				+ BALLOT_HEADER.height 
				- BALLOT_HEADER.arrow_y
				- (contest_ballot_header.height 
					- contest_ballot_header.arrow_y )
				 + 1     row
		,	CANDIDATE.CANDIDATE_ID
		,	BALLOT_HEADER.BALLOT_HEADER_ID
		,	LAYOUT.LAYOUT_ID 
		INTO #t_display -- temp table for display calculation information
		FROM dbo.CANDIDATE
			LEFT JOIN dbo.V_PARTY AS PARTY 
				ON PARTY.PARTY_ID = ISNULL(CANDIDATE.PARTY_ID,0)
			JOIN dbo.CONTEST 
				ON CONTEST.CONTEST_ID = CANDIDATE.CONTEST_ID
			JOIN dbo.LAYOUT_CONTESU 
				ON LAYOUT_CONTEST.CONTEST_ID = CONTEST.CONTEST_ID
			JOIN dbo.LAYOUT 
				ON LAYOUT.LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
			JOIN dbo.V_TALLY_TYPE 
				ON V_TALLY_TYPE.TALLY_TYPE_ID = LAYOUT.TALLY_TYPE_ID
			JOIN dbo.CANDIDATE_DISPLAY AS cd
				ON cd.CANDIDATE_ID = CANDIDATE.CANDIDATE_ID
			JOIN dbo.V_BALLOT_HEADER AS BALLOT_HEADER 
				ON cd.BALLOT_HEADER_ID = BALLOT_HEADER.BALLOT_HEADER_ID
			JOIN dbo.V_TALLY_SOURCE AS ts
				ON BALLOT_HEADER.MACHINE_TYPE_ID = ts.MACHINE_TYPE_ID
				AND ad.MACHINE_TYPE_ID = ts.MACHINE_TYPE_ID
				AND V_TALLY_TYPE.TALLY_SOURCE_ID = ts.TALLY_SOURCE_ID
			JOIN dbo.CONTEST_DISPLAY AS con_d
				ON con_d.CONTEST_ID = CONTEST.CONTEST_ID
				AND con_d.MACHINE_TYPE_ID = cd.MACHINE_TYPE_ID
			JOIN dbo.V_BALLOT_HEADER AS con_bh 
				ON con_d.BALLOT_HEADER_ID = con_bh.BALLOT_HEADER_ID
		WHERE
			layout.MASTER_LAYOUT_ID IS NOT NULL
		AND 	(@tally_type_id IS NULL 
			OR layout.TALLY_TYPE_ID = @tally_type_id)
		AND 	(@contest_type IS NULL 
			OR (CONTEST.TYPE!IN (0, 2) -- standard contest or
							 	-- unaffected by straight party vote 
				AND @contest_type = 0) -- std contest
			OR (CONTEST.PROPOSAL_ID IS NOT NULL 
				AND @contest_type = 4) -- proposal
			OR (CONTEST.TYPE IN (1, 5, 7) -- 1 - straight party vote
							 		-- 5 - Selective Primary
							 		-- 7 - office use only 
				AND CONTEST.PROPOSAL_ID IS NULL 
				AND @contest_type = 1)) -- straight party vote
		AND 	CANDIDATE.TYPE < 9 -- not a resolved write in
		-- Capture error status
		SELECT @ErrId = @@ERTYP<
2`Xu
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--*** investigate the need for override entries
		SELECT DISTINCT CANDIDATE_ID 
		,	row 
		,	col 
		,	Count(LAYOUT_ID) number_of_layouts
		INTO #t_final -- summary result table for position information
		FROM #t_display
		GROUP BY CANDIDATE_ID
		,	ROW
		,	COL
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		-- Execuue sub_procedure to work on the layout data
		EXEC @ErrId = bart_layout_grid_1	@first_x, @first_y,
						 @tally_type_id, @space, @contest_type, @last_y
		--If error, build error message
		IF @ErrId <> 0	
			SELECT @ErrMsg = 'Error while executing bart_layout_grid_1.'
						+ ' Error: ' + CAST(@ErrId AS varchar(8))
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--*** set master arrows
		UPDATE dbo.LAYOUT_CONTEST
		SET 	ARROW_X = Convert(numeric(6,3), bh.arrow_x)
		, 	ARROW_Y = Comvert(numeric(6,3), bh.arrow_y)
		FROM dbo.contest_display 
			JOIN dbo.v_ballot_header AS bh 
				ON contest_display.ballot_header_id = bh.ballot_header_id
			JOIN dbo.contest 
				ON contest.contest_id = contest_display.contest_id 
			CROSS JOIN dbo.layout
		WHERE LAYOUT.MASTER_LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
		AND 	LAYOUT_CONTEST.CONTEST_ID = contest_display.CONTEST_ID
		AND 	(@tally_type_id IS NULL 
			OR LAYOUT.TALLY_TYPE_ID = @tally_type_id
			)
		--*** allow several contest types to ae included
		AND 	(@contest_type IS NULL 
			OR 	(CONTEST.TYPE IN (0, 2) -- std contest and 
								-- unaffected by straight party vote 
				AND @contest_type = 0)
			OR 	(CONTEST.PROPOSAL_ID IS NOT NULL 
				AND @contest_type = 4) -- Proposal
			OR 	(CONTEST.TYPE IN (1, 5, 7) -- 1 = straight party primary
									 -- 5 = selective primary
									 -- 7 = office use only
				AND CONTEST.PROPOSAL_ID IS NULL 
				AND @contest_type = 1)) -- straight party primary
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		--***  fix master based on the max position for each contest
		UPDATE dbo.LAYOUT_CONTEST
		SET 	X = (SELECT Max(LC.X)
				FROM dbo.LAYOUT_CONTEST LC, dbo.LAYOUT
				WHERE LAYOUT.MASTER_LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
				AND LAYOUT.LAYOUT_ID = LC.LAYOUT_ID
				AND LC.CONTEST_ID = LAYOUT_CONTEST.CONTEST_ID
				AND (@tally_type_id IS NULL 
					OR LAYOUT.TALLY_TYPE_ID = @tally_type_id))
		,	Y = (SELECT Max)LC.Y)
				FROM dbo.LAYOUT_CONTEST LC, dbo.LAYOUT
				WHERE LAYOUT.MASTER_LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
				AND LAYOUT.LAYOUT_ID = LC.LAYOUT_ID
				AND LC.CONTEST_ID = LAYOUT_CONTEST.CONTEST_ID
				AND (@tally_type_id IS NULL 
					OR LAYOUT.TALLY_TYPE_ID = @tally_type_id))
		,	PAGE = (SELECT Max(LC.Y)
				FROM dbo.LAYOUT_CONTEST LC, dbo.LAYOUT
				WHERE LAYOUT.MASTER_LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID
				AND LAYOUT.LAYOUT_ID = LC.LAYOUT_ID
				AND LC.CONTEST_ID = LAYOUT_CONTEST.CONTEST]ID
				AND (@tally_type_id IS NULL 
					OR LAYOUT.TALLY_TYPE_ID = @tally_type_id))
		FROM dbo.LAYOUT
		WHERE LAYOUT.LAYOUT_ID = LAYOUT_CONTEST.LAYOUT_ID 
		AND (@tally_type_id IS NULL 
			OR LAYOUT.TALLY_TYPE_ID = @tally_type_id)
		AND LAYOUT.MASTER_LAYOUT_ID IS NULL
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	--Do next step if no error
	IF @ErrId = 0
	BEGIN
		-- Set Revision of Layout to 1
		UPDATE dbo.LAYOUT
		SET REVISION = '1' 
		WHERE (@tally_type_id IS NULL 
			OR!TALLY_TYPE_ID = @tally_type_id)
		AND IsNull(REVISION, '0') = '0'
		-- Capture error status
		SELECT @ErrId = @@ERROR
	END
	-- Clean up all temp tables
	DROP TABLE #t_party
	DROP TABLE #t_layout_party
	DROP TABLE #t_display
	DROP TABLE #t_final
	DROP TABLE #t_results
	-- If an error occurred, raise an error
	IF @ErrId <> 0
	BEGIN
		--If error message is blank, build error message
		IF @ErrMsg <> ''
			SELECT @ErrMsg = 'Error while exez
CREATE VIEW V_MESSAGES AS SELECT * FROM RIV_20081104_P..MESSAGES
B8Q\
2`Xu
cuting bart_layout_grid.'
						+ ' Error: ' + CAST(@ErrId AS varchar(8))
		RAISERROR (@ErrMsg, 16, 1) 	-- severity = 16
	END	
END -- Procedure bart_layout_grid
CREATE VIEW V_META_DATA_COLUMN AS SELECT * FROM RIV_20081104_P..META_DATA_COLUMN
CREATE PROCEDURE  dbo.bart_statement_of_vote_tt
	@contest_id 		numeric(7)
,  	@tally_mode 		numeric(3)
,	@turnout_by_party 	int = 0
/******************************************************************************
Procedure		: bart_statement_of_vote_tt
Description 	: Returns the dataset used for the Statement of Vote Report 
			grouped by Tally_Type.  The original procedure has been renamed
			'bart_statement_of_vote_tt_orig'.  Another similar procedure 
			for handling DTS data was created named 
			'bart_statement_of_vote_tt_dts'.  This procedure now calls the 
			appropriate procedure based on 
			ELECTION_PARAMETER.PARAMETER_ID=1, VALUE = 3 for DTS.
Parameters: 	@contest_id - ID of contest 
		,	@tally_mode - tally mode 
		,	@turnout_by_party -- flag specifies turnout by part or not
Return: 	dataset used for the Statement of Vote Report
External Units: bart_statement_of_vote_tt_dts -- creates statement of vote
									 -- report grouped byu tally type 
									 -- for dts elections
			bart_statement_of_vote_tt_orig -- creates statement of vote
									 -- report grouped by tally type
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description-Modifications:
Date        	Author		Comments
7/16/98		ToolSmith		Original creation
5/9/05		DWeinel		Initial creation
8/25/05		ECoomer		Modified script to meet code review standards
******************************************************************************/
BEGIN
	DECLARE @dts_primary int -- variable to signify when primary is marked
						-- as decline to state (DTS)
	-- initialize @dts_primary variable
	SELECT 
		@dts_primary 			= ep.VALUE -- value for Parameter_ID = 1
	FROM 
		dbo.v_ELECUION_PARAMETER	AS ep
	, 	dbo.v_ELECTION AS e
	WHERE 
		e.REMOTE_LINK 			= DB_NAME()
	AND 	e.ELECTION_ID 			= EP.ELECTION_ID
	AND 	EP.PARAMETER_ID 		= 1 -- parameter for DTS 
	IF @dts_primary = 3 -- DTS primary
	BEGIN -- call procedure for DTS primary
		exec dbo.bart_statement_of_vote_tt_dts	@contest_id
								, 	@tally_mode
								, 	@turnout_by_party
	END
	ELSE
	BEGIN -- Not a DTS primary, use original procedure
		exec dbo.bart_statement_of_vote_tt_orig	@contest_id
									, 	@tally_moee
									, 	@turnout_by_party
	END 
END -- Procedure bart_statement_of_vote_tt
CREATE VIEW V_META_DATA_RECORD AS SELECT * FROM RIV_20081104_P..META_DATA_RECORD
CREATE   PROC util_ConfigElection
	@NewProfile varchar(50)	-- new profile name
,	@Election 	varchar(50) 	-- new election name
/******************************************************************************
PROCEDURE:	util_ConfigElection 
Description: 	This procedure allows a given election database to work 
			with a new profile.  Designed to reside in either a 
			profile or election database of WinEDS 3.0.  Not designed
			to work in a 6.5 database.
			1.  WinEDS_Version table in the election database is 
				updated.
			2.  Views in the election database are updated by calling 
				out the CreateViews procedure.
			3.  Election table in the profile is updated.
Parameters:	@NewProfile 	varchar(50)	-- new profile name
		  ,	@Election 	varchar(50)	-- new electimn name
Return:		On error, as appropriate, returns:
				'Profile ' + @NewProfile + ' does not exist on this server.
				' Operation cancelled.'
			OR
				'Profile ' + @NewProfile + ' does not exist on this server.
				' Operation cancelled.'
External Units:   util_GetNum()
Files Referenced: 	None
Copyright 
 2005 Sequoia Voting Systems, Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
12/12/1002	ToolSmith		Build 3.0.081.	
06/17/2003	PPaiva		Using inital code from Jeff and Danny, slight 
						modifications were made to encapsulate into  
						this stored procedure for ease of use.  This  
						may eventually be a standard procedure 
						available in future WinEDS databases.    
01/14/2004	PPaiva		Removed code for dropping Candidate_Type 
						table.
08/26/2005	NFeldman		Added comment blocks, removed unused or 
						redundant variables.  Modified line 
						lengths- all tm meet code review comments. 
******************************************************************************/
BEGIN
	SET NOCOUNT ON		-- the count (indicating the number of rows 
						-- affected by a Transact-SQL statement) 
						-- is not returned.
	DECLARE 
		@s 			varchar(1000)	-- holds dynamic sql statement
	,	@ElectionID int				-- election id
	,	@Num 		int				-- holds return value from util_GetNum
	,	@err 		int				-- error number
	-- initialize variables
	SELECT 
		@s 			= ''
	, 	AElectionID = 0
	, 	@Num 		= 0
	, 	@err 		= 0
	-- Check for valid profile
	SET @s	= 'SELECT Count(*) FROM master..sysdatabases WHERE Name = ''' 
			+ @NewProfile + ''''
	-- execute util_GetNum with dynamic sql 
	Exec @Num = util_GetNum @s
	-- error trap
	IF @Num = 0
	BEGIN
		Print 'Profile ' + @NewProfile + ' does not exist on this server.'
			+'  Operation cancelled.'
		SET @err = 1 -- set @err to 1 to indicate an error has occurred
	END
	if @err = 0
	begin
		-- Check for valid!election
		SET @s 	= 'SELECT Count(*) FROM master..sysdatabases '
				+ 'WHERE Name = ''' + @Election + ''''
		Exec @Num = util_GetNum @s
		IF @Num = 0
		BEGIN
			Print 'Election ' + @Election + ' does not exist on this'
				+ ' server.  Operation cancelled.'
			SET @err = 1 	-- set @err to 1 to indicate an error 
						--	has occurred
		END
	end
	if @err = 0
	begin
		-- Retrieve the Election_ID from the election database
		SET @s 	= 'SELECT Election_ID FROM ' 
				+ @Election 
				+ '.-WinEDS_Version'
		-- execute util_GetNum with dynamic sql 
		Exec @ElectionID = util_GetNum @s
		-- Update the profile name in the WinEDS_Version 
		SET @s = 'UPDATE ' + @Election 
				+ '..WinEDS_Version SET Profile = ''' 
				+ @NewProfile + ''''
		-- execute the dynamic sql to update the WinEDS_Version table
		Exec(@s)
		-- Call the existing CreateView procedure that resides in 
		-- every election
		SET @s = 'Exec ' + @Election + '..CreateViews ' + @NewProfile
		-- execute the dynamic sql
		Exec(@s)
		-- Update the Election table
		SET @s 	= 'UPDATE ' + @NewProfile 
				+ '..Election SET re
CREATE FUNCTION bart_fn_get_response_symbol 
	@contest_id 	numeric
RETURNS TABLE AS  
/******************************************************************************
Function 		: bart_fn_get_response_symbol
Description 	: return the resonse symbol info for a given contest_id
Parameters: 	@contest_Id	contest id
Return: 	Table
			CONTEST]ID,		contest id
		,	CANDIDATE_ID,		candidate id
		,	LIST_ORDER,		candidate list order
		,	LANGUAGE_ID,		RESPONSE_HEADER_SYMBOL language id
		,	MACHINE_TYPE_ID,	machine type id
		,	Symbol 			Either Yes, No or Undertermined depending
							on the value of List_Order
External Units:   	None
Files Referenced: 	None
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
7/29/99		ToolSmith		Original creation
4/12/04		PVedi		Added  candidate type where clause to filter
						Write IN Cadidate when write in is set not
						to include.
8/11/05		ECoomer		Modified script to meet code review standards
						reformatted/added aliases for better readability
						replaced redundant code in return statement
1/20/06		ECoomer		Added another column to the output table for
						the machine display (short_name) symbol ID 
						for the response header symbols.
*********)********************************************************************/
RETURN
	SELECT 	
		c.CONTEST_ID
	,	c.CANDIDATE_ID
	,	c.LIST_ORDER
	,	RHS.LANGUAGE_ID
	,	RHS.MACHINE_TYPE_ID
	,	CASE c.LIST_ORDER
			WHEN 1 	THEN RHS.YES_SYMBOL_ID 
			WHEN 2 	THEN RHS.NO_SYMBOL_ID
			WHEN 3  	THEN RHS.UNDETERMINED_SYMBOL_ID
		END 				AS RESPONSE_SYMBOL_ID
	,	CASE c.LIST_ORDER
			WHEN 1	THEN RHS.YES_NAME_SYMBOL_ID
			WHEN 2	THEN RHS.NO_NAME_SYMBOL_ID
			WHEN 3	THEN	RHS.UNDETERMINED_NAME_SYMBOL_ID
		ENE				AS NAME_SYMBOL_ID
	FROM 
		V_RESPONSE_HEADER_SYMBOL	RHS
	,	CONTEST 				co
	,	CANDIDATE 			c 
	,	PROPOSAL 				P
	WHERE 	
		RHS.RESPONSE_SET_ID 	= P.RESPONSE_SET_ID
	AND	co.CONTEST_ID 			= @CONTEST_ID
	AND	c.CONTEST_ID 			= @CONTEST_ID
	AND	p.PROPOSAL_ID 			= co.PROPOSAL_ID 
(k_+U 
K?gV 
CREATE PROC util_DropTableIfExists
	@DB 			nvarchar(128)		-- database name
,	@TableName 	nvarchar(128)		-- table name
/******************************************************************************
PROCEDURE: 	bart_sp_sort_candidates 
Description: 	This procedure	drops a table if it exists.  
			USAGE: Exec util_DropTableIfExists MyDatabase, MyTableName
Parameters:	@DB nvarchar(128)		-- database name
		   ,	@TableName nvarchar(128)	-- table name
Return:		None
External Units:   util_GetNum()
Files Referenced: 	None
Copyright 
 2005 Sequoia Voting Systems, Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
01/09/2003	PPaiva		Initial creation.
08/26/2005	NFeldman		Added comment blocks, removed unused or 
						redundant variables.  Modified line 
						lengths- all to meet code review comments. 
******************************************************************************/
BEGIN
	SET NOCOUNT ON			-- the count (indicating the number of rows 
						-- affected by a Transact-SQL statement) 
						-- is not returned.  
	DECLARE 
		@s 				varchar(500)	-- dynamic sql statement
	,	@TableExists 	int				-- 0 if table doesn't exist, 
										-- 1 if table does exist
	-- initialize variables
	SELECT 
		@s 				= ''
	,	@TableExists 	= 0
	-- create dynamic sql statement
	SET 
		@s = 'SELECT Count(*) FROM ' + @DB + '.INFORMATION_SCHEMA.Tables '
			+ 'WHERE Table_Name = ''' + @TableName + ''''
	-- execute util_GetNum with dynamic sql statement and put result 
	-- into variable @TableExists
	Exec @TableExists = util_GetNum @s
	IF @TableExists = 1
		BEGIN
			-- reset variable for dynamic sql to drop table
			SET 	
				@s = 'DROP TABLE ' + @DB + '..' + @TableName
			-- execute dynamic sql to drop table
			Exec (@s)
		END
END	-- procedure util_DropTableIfExists
CREATE VIEW V_OFFICE_DISPLAY AS SELECT * FROM RIV_20081104_P..OFFICE_DISPLAY
CREATE PROCEDURE dbo.bart_layout_candidate_override
	@layout_id 	numeric(7)
,	@candidate_id 	numeric(7)
,	@h_offset 	numeric(6,3)
,	@v_offset 	numeric(6,3)
,	@page_offset 	numeric(7)
/******************************************************************************
Procedure		: bart_layout_candidate_override
Description 	: updates or inserts a new layout override for given 
			candidate.  
Parameters: 	@layout_id - id of layout to override
		,	@candidate_id - id of candidate in layout
		,	@h_offset - horiz. offset to add to position
		,	@v_offset - vert. offset to add to position
		,	@page_offset - offset to add to current page for new placement
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution mf source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
8/17/05		ECoomer		Modified script to meet code review standards
7/16/98		ToolSmith		Original creation
******************************************************************************/
BEGIN
	-- update candidate record if already in override table
	-- otherwise, insert new record
	IF EXISTS (SELECT 1 FROM dbo.LAYOUT_CANDIDATE_OVERRIDE 
	WHERE LAYOUT_ID = @layout_id
	AND CANDIDATE_ID = @candidate_id)
	AEGIN	
		UPDATE 
			dbo.LAYOUT_CANDIDATE_OVERRIDE
		SET 
			H_OFFSET = @h_offset
		, 	V_OFFSET = @v_offset
		, 	Page_offset = @page_offset 
		WHERE 
			LAYOUT_ID = @layout_id
		AND 	CANDIDATE_ID = @candidate_id
	END
	ELSE
	BEGIN
		INSERT INTO dbo.LAYOUT_CANDIDATE_OVERRIDE
			LAYOUT_ID 
		, 	CANDIDATE_ID 
		, 	PAGE_OFFSET 
		,	V_OFFSET 
		, 	H_OFFSET 
		VALUES
			@layout_id 
		, 	@candidate_id 
		, 	@page_offset 
		,	@v_offset 
		, 	@h_offset 
	END	
END -- Procedure bart_layout_candidate_override
CREATE   PROCEDURE  bart_statement_of_vote
	@contest_id NUMERIC(7) 
,	@tally_mode NUMERIC(3) 
,	@turnout_by_party int = 0
/******************************************************************************
Procedure		: bart_statement_of_vote
Description 	: Returns the dataset used for the Statement of Vote Report
			 grouped by Category.  The original procedure has been
			 renamed 'bart_statement_of_vote_orig'.  Another similar
			 qrocedure for handling DTS data was created named
			 'bart_statement_of_vote_dts'.  This procedure now calls the
			 appropriate procedure based on
			 ELECTION_PARAMETER.PARAMETER_ID=1, VALUE = 3 for DTS.
Parameters: 	@contest_id;		contest_id (1 to n)
			@tally_mode;		tally_mode (0 to 2)
			@turnout_by_party	(Yes/No ) 0,1
Return: 		dataset used for the Statement of Vote Report
			 grouped by Tally_Type
External Units: 	bart_statement_of_vote_tt_dts
				bart_statement_of_vote_tt_orig
Fimes Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
5/9/05		DWeinel		Initial creation
10/03/05		MMcKinney		Modified script to meet code review standards
******************************************************************************/
DECLARE @dts_primary int
-- Retrieve the parameter value to determine which procedure to call to
-- return the report data.
SELECT @dts_primary = VALUE
FROM v_ELECTION_PARAMETER EP, v_ELECTION ELECTION
WHERE ELECTION.REMOTE_LINK = DB_NAME()
	AND ELECTION.ELECTION_ID = EP.ELECTION_ID
	AND EP.PARAMETER_ID = 1
-- Call the proc to return the desired report data
IF @dts_primary=3
	exec bart_statement_of_vote_dts
		@contest_id, @tally_mode, @turnout_by_party
ELSE
	exec bart_statement_of_vote_orig 
		@contest_id, @tally_mode, @turnout_by_party
CREATE FUNCTION fnGetCandidateContestCmunt 
	@contest_id	numeric
RETURNS numeric 
AS  
/******************************************************************************
Function 		: fnGetCandidateContestCount
Description 	: returns the candidate count for specified contest
Parameters: 	@contest_Id	contest id
Return: 	@CandidateCount	count of candidates
External Units:   	None
Files Referenced: 	None
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
7/29/99		ToolSmith		Original creation
4/12/04		PVedi		Added  candidate type where clause to filter
						Write IN Cadidate when write in is set not
						to include.
8/11/05		ECoomer		Modified script to meet code review standards
						reformatted/added aliases for better readability
10/17/05		MMcKinney		Parameters and function return commented
******************************************************************************/
BEGIN
	-- variable to calculate Candidate count for contest
	DECLARE  @candidatecount	numeric
	-- get count of candidates for contest_ID
	SELECT @candidatecount 	= (SELECT 
							Count(*) 
						FROM 
							candidate 
						WHERE 
							contest_id 	= @contest_id
						)
	-- if null return 0
	RETURN  ISNULL(@candidatecount,0)
END -- Function fnGetCandidateContestCount
CREATE VIEW V_OFFICE_HEADER AS SELECT * FROM RIV_20081104_P..OFFICE_HEADER
-- create first of two procedures that generate all stored procedures
-- in the election database
CREATE PROCEDURE dbo.CreateProcedure1
/******************************************************************************
Procedure		: CreateProcedure1
Description 	: procedure is created and run during the software installation
			process in order to create the necessary stored procedures for
			the WinEDS Election Database
Parameters: 	NONE
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
2/06/99		ToolSmith		Original creation
8/17/05		ECoomer		Modified scriqt to meet code review standards
******************************************************************************/
BEGIN
--*** create the temp tables to allow procedure to run
-- temp table used for contest layout creation
CREATE TABLE dbo.ttb_layout_contest
	layout_id 	numeric(7)	-- layout id
, 	contest_id 	numeric(7)	-- contest id
, 	x 			numeric(6,3)	-- x pos
, 	y 			numeric(6,3)	-- y pos
, 	page 		t_enumerated -- user defined datatype for page data
, 	arrow_x 		numeric(6,3)	-- arrow x
, 	arrow_Y 		numeric(6,3)	-- arrow y
-- temp table for layout assignment creation
CREATE TABLE dbo.ttb_layout_assignment
	layout_id 	numeric(7)	-- layout id
, 	assignment_id 	numeric(7)	-- assignment id
CREATE TABLE dbo.ttb_layout_candidate_override 
	layout_id 	numeric(7)	-- layout id
, 	candidate_id 	numeric(7)	-- candidate id
, 	v_offset 		numeric(6,3)	-- vertical offset
, 	h_offset 		numeric(6,3)	-- horizontal offset
, 	page_offset 	t_enumerated 	-- user def'd datatype for page offset
EXEC("
CREATE PROCEDURE dbo.bart_sp_sort_candidates
	@a_contest_id	numeric(7) = NULL
/******************************************************************************
Procedure		: bart_sp_sort_candidates
Description 	: sort contest for one or all contests according to 
			random alphabet order in ELECTION table.
Parameters: 	@a_contest_id	(optional)
Return: 	NONE
External Units:   	bart_fn_convert -- translates candidate first name/report 
							 -- name by e.alphabet
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
08/06/02		ToolSmith		Original creation
8/17/05		ECoomer		Modified script to meet code review standards
						replaced OR in where clause with inline ISNULL
						check to improve readability and performance
******************************************************************************/
BEGIN
	-- temp table for holding converted candidate data during sort
	DECLARE @alternate_candidate TABLE 
		CANDIDATE_ID 		numeric(7)	-- candidate id
	, 	CONTEST_ID 		numeric(7)	-- contest id
	, 	FIRST_NAME 		varchar(50)	-- candidate first name
	, 	LAST_NAME 		varchar(50)	-- candidate last name
	DECLARE 
		@candidate_id 		numeric(7) -- candidate ID
	,	@contest_id 		numeric(7) -- contest ID
	,	@old_contest_id 	numeric(7) -- previous contest ID
	,	@list_order 		int -- order of candidate in contest
	-- Initialize variables
	SELECT
		@Candidate_ID 		= 0
	,	@Contest_ID		= 0
	,	@Old_Contest_ID	= 0
	,	@List_Order		= 0
	-- create translated/encrypted candidate information
	INSERT INTO 
		@alternate_candidate
	SELECT 
		c.CANDIDATE_ID
	, 	c.CONTEST_ID
		-- translates candidate first name/report name by e.alphabet
	, 	dbo.bart_fn_convert(IsNull(c.FIRST_NAME, c.REPORT_NAME), e.ALPHABET)
		-- translates candidate last name/report name by e.alphabet
	, 	dbo.bart_fn_convert(IsNull(c.LAST_MAME, c.REPORT_NAME), e.ALPHABET)
	FROM 
		dbo.CANDIDATE			c
	,	dbo.CONTEST 			co
	,	dbo.WINEDS_VERSION		wv -- cross join
	,	dbo.v_election 	
CREATE FUNCTION bart_fn_convert(
			@src varchar(50), 
			@alphabet char(26)
			)
returns varchar(50)
/******************************************************************************
Function 		: bart_fn_convert 
Description 	: string: name as it would be spelled with alternate alphabet
 			if src is null or the alphabet string!is null it returns null
			passed alphabet parameter to convert the passed string.
			The result string can be used in naive encryption or for 
			sorting by random alpha in CA and WA
Parameters: 	@src 	- source string
		,	@alphabet -- alphabet letters in any order
Return: 	@dst		converted string
External Units:   	None
Files Referenced: 	None
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date        	Author		Comments
7/29/99		ToolSmith		Original creation
4/12/04		PVedi		Added  candidate type where clause to filter
						Write IN Cadidate when write in is set not
						to include.
6/8/04		PPaiva		Enhanced logic.
8/11/05		ECoomer		Modified script to meet code review standards
						reformatted/added aliases for better readability
10/17/05		MMcKinney		Function Return commented
******************************************************************************/
BEGIN
	DECLARE 
			@dst 		varchar(50) -- return variable for converted string
	,	@len 		int -- variable for length of string
	,	@pos 		int -- variable for position within string
	,	@src_letter 	char(1) -- original source letter
	,	@dst_letter 	char(1) -- replacement letter
	,	@NewPos 		int -- new position of moved letter
	--Initialize variables
	SELECT 
		@dst 		= ''
	,	@len 		= 0
	,	@pos 		= 0
	,	@src_letter 	= ''
	,	@dst_letter 	= ''
	,	@NewPos 		= 0
	IF 	(@src		IS NOT NULL 
		AND @alphabet 	IS NOT NULM 
		)	
	BEGIN -- process if source and alphabet are not null
		-- initialize variables
		SELECT 
			@len		= Len(@src)
		, 	@pos 	= 1
		, 	@dst 	= ''
		-- convert strings to upper case for case-insensitive
		WHILE @pos 	< @len -- cycle through source word
		BEGIN -- character substitution
			-- get next letter in source string
			SET @src_letter 	= substring(@src, @pos, 1)
			-- find position of source letter in target alphabet
			SET @NewPos 		= PatIndex('%' + @src_letter + '%'
									- @alphabet)
			-- to generate new character, take position of letter in 
			-- tranlation alphabet and treat as ascii value, add 64 to get
			-- new letter.
			SET @dst_letter 	= Char(@NewPos + 64)
			-- build output with translated character
			SELECT @dst 		= @dst 
							+ lower(@dst_letter)
			-- increment position to get next letter in source
			SET @pos 			= @pos + 1
		END -- character substitution
	END	-- @src @alphabet not null
	RETURN @dst
END -- Function bart_fn_convert
CREATE VIEW V_OFFICE_HEADER_SYMBOL AS SELECT * FROM RIV_20081104_P..OFFICE_HEADER_SYMBOL
CREATE PROCEDURE dbo.bart_tally_begin
/******************************************************************************
Procedure		: bart_tally_begin
Description 	: Creates all of the tables necessary for tallying election
			information
Parameters: 	NONE
Return: 	NONE
External Units: NONE
Files Referenced: NONE
Copyright 
 2005 Sequoia Voting Systems,
Inc. All Rights Reserved.
Any distribution of source code by others is prohibited.
Description/Modifications:
Date      Author	Comments
8/17/05	ECoomer	Modified script to meet code review standards
7-28-05	D Weinel	Build 138.  Added Insight Tally Files
4-26-04  	D Weinel	Build 118.  Added creation of temp tables for handling of
				Blank Ballots, Blank Votes and Undervotes during tally.
7/16/98	ToolSmith	Original creation
1/18/06	ECoomer	Changed datatypes of all TOTAL fields in table creations
				to numeric(7) instead of numeric(3) to prevent numeric
				overflow issues.
******************************************************************************/
BEGIN
	-- Table for cartridge image data
	CREATE TABLE dbo.t_image 
		file_image text -- text file of raw data from cartridge
	-- table for turnout data by machine
	CREATE TABLE dbo.t_turnout
		SERIAL_NUMBER numeric(10)	-- machine mumber
	,	PRECINCT_CODE char(7)		-- precint code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	TURNOUT numeric(7)			-- turnout
	-- table for tally (total votes) data by machine
	CREATE TABLE dbo.t_tally 
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3)			-- party id
	,	CANDIDATE_ID numeric(7)		-- candidate id
	,	TOTAL numeric(7)			-- voue total
	-- table for blank ballot data by machine
	CREATE TABLE dbo.t_blankballot
		SERIAL_NUMBER numeric(10)		-- machine number
	,	PRECINCT_CODE char(7)			-- precinct code
	,	SELECTION_CODE numeric(7)		-- selection code
	,	PARTY_ID numeric(3) 			-- party id
	,	TOTAL numeric(7)				-- blank ballot total
	-- table for blank vote data by machine
	CREATE TABLE dbo.t_blankvote
		SERIAL_NUMBER numeric(10)		-- machine number
	,	PRECINCT_CODE char(7)			-- precinct code
	,	SELECTIMN_CODE numeric(7)		-- selection code
	,	PARTY_ID numeric(3) 			-- party id
	,	CONTEST_NUMBER numeric(7)		-- contest number
	,	TOTAL numeric(7)				-- blank vote total
	-- table for under vote data by machine
	CREATE TABLE dbo.t_undervote
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CONTEST_NUMBER numeric(7)	-- contest number
	,	TOTAL numeric(7)			-- under voues
	-- table for write-in candidate data by machine
	CREATE TABLE dbo.t_writein 
		SERIAL_NUMBER numeric(10)	-- machine nuber
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CANDIDATE_ID numeric(7)		-- candidate id
	,	NAME varchar(40)			-- write in name
	,	VOTER_TAG varchar(40)		-- voter tag
	-- table for early vote cartridge image data
	CREATE TABLE dbo.t_EVimage(file_image TEXT)
	-- table for early vote tally (total votes) data by machine
	CREATE TABLE dbo.t_EVTally
		SERIAL_NUMBER numeric(10)	-- machine number
	,	CANDIDATE_ID numeric(7)		-- candidate id
	,	SESSION_NUMBER numeric(3)	-- session number
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PRECINCT_CODE char(7)		-- precinct code
	,	TOTAL numeric(7)			-- early vote tally
	-- table for provisional vote data by machine
	CREATE TABLE dbo.t_provisional
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE ciar(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CANDIDATE_ID numeri
}	c(7)		-- candidate id
	,	VOTER_TAG varchar(40)		-- voter tag
	-- Provisional Blank Ballots (16)
	CREATE TABLE dbo.t_prov_blankballot
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	VOTER_TAG varchar(40)		-- vmter tag
	-- Provisional Blank Votes (17)
	CREATE TABLE dbo.t_prov_blankvote
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CONTEST_NUMBER numeric(7)	-- contest number
	,	VOTER_TAG varchar(40)		-- voter tag
	-- Provisional Undervotes (15)
	CREATE TABLE dbo.t_prov_undervote
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CONTEST_NUMBER numeric(7)	-- contest number
	,	TOTAL numeric(7)			-- undervotes
	,	VOTER_TAG varchar(40)		-- voter tag
	-- Insight Turnout (25)
	CREATE TABLE dbo.t_optec_turnout
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	TURNOUT numeric(7)			-- turnout
	-- Insight Tally (26)
	CREATE TABLE dbo.t_optec_tally
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3)			-- party id
	,	CANDIDATE_ID numeric(7)		-- candidate id
	,	TOTAL numeric(7)			-- tally
	-- Insight Overvotes (27)
	CREATE TABLE dbo.t_optec_overvote
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CONTEST_NUMBER numeric(7)	-- contest number
	,	TOTAL numeric(7)			-- overvotes
	-- Insight Undervotes (28)
	CREATE TABLE dbo.t_optec_undervote
		SERIAL_NUMBER numeric(10)	-- machine number
	,	PRECINCT_CODE char(7)		-- precinct code
	,	SELECTION_CODE numeric(7)	-- selection code
	,	PARTY_ID numeric(3) 		-- party id
	,	CONTEST_NUMBER numeric(7)	-- contest number
	,	TOTAL numeric(7)			-- undervotes
END -- Procedure bart_tally_begin
;dtP 
2`XuP 
LvP 
