if not exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'CSEasyScriptSupportedPages' and COLUMN_NAME = 'DefaultRow')
begin
	alter table [dbo].[CSEasyScriptSupportedPages]
		alter column [DefaultColumn] [nvarchar](255) NULL
end
go

if not exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'CSEasyScriptSupportedPages' and COLUMN_NAME = 'DefaultRow')
begin
	alter table [dbo].[CSEasyScriptSupportedPages]
		add [DefaultRow] [nvarchar](255) NULL
end
go


if not exists(select * from [dbo].[CSEasyScriptSupportedPages] where ASPXPage = N'CRMOpportunityCard.aspx')
begin
	insert into [dbo].[CSEasyScriptSupportedPages](ID, Caption, CaptionID, ASPXPage, DefaultColumn, IsMaintScreen)
	values(newId(), N'Opportunity card', 34972, N'CRMOpportunityCard.aspx', N'cc1', 0)
end
go

if not exists(select * from [dbo].[CSEasyScriptSupportedPages] where ASPXPage = N'CRMOpportunityMaint.aspx')
begin
	insert into [dbo].[CSEasyScriptSupportedPages](ID, Caption, CaptionID, ASPXPage, DefaultRow, IsMaintScreen)
	values(newId(), N'Opportunity card', 34972, N'CRMOpportunityMaint.aspx', N'crMain', 1)
end
go

if not exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'CSEasyScriptSections' and COLUMN_NAME = 'Criteria')
begin
	alter table [dbo].[CSEasyScriptSections]
		add [Criteria] [nvarchar](255) NULL
end
go

if not exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'CSEasyScriptSupportedPages' and COLUMN_NAME = 'TableName')
begin
	alter table [dbo].[CSEasyScriptSupportedPages]
		add [TableName] [nvarchar](255) NULL,
			[KeyColumn] [nvarchar](255) NULL,
			[KeyInputField] [nvarchar](255) NULL
end
go

if exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'CSEasyScriptSupportedPages' and COLUMN_NAME = 'TableName') and exists(select * from CSEasyScriptSupportedPages where TableName is null)
begin
	update CSEasyScriptSupportedPages set TableName = 'cicmpy', KeyColumn = 'cmp_wwn', KeyInputField = 'ID' where ASPXPage in ('CRMAccountMaint.aspx', 'CRMPersonAccountMaint.aspx', 'CRMAccountCard.aspx', 'CRMPersonAccountCard.aspx')
	update CSEasyScriptSupportedPages set TableName = 'cicntp', KeyColumn = 'cnt_id', KeyInputField = 'ContactID' where ASPXPage in ('CRMContactCard.aspx', 'CRMContactMaint.aspx')
	update CSEasyScriptSupportedPages set TableName = 'Items', KeyColumn = 'ItemCode', KeyInputField = 'ItemCode' where ASPXPage in ('LogItem.aspx')
	update CSEasyScriptSupportedPages set TableName = 'Items', KeyColumn = 'ItemCode', KeyInputField = 'Item' where ASPXPage in ('LogItemCard.aspx')
	update CSEasyScriptSupportedPages set TableName = 'PrProject', KeyColumn = 'ProjectNr', KeyInputField = 'Project' where ASPXPage in ('ProCard.aspx')
	update CSEasyScriptSupportedPages set TableName = 'PrProject', KeyColumn = 'ProjectNr', KeyInputField = 'ProjectNr' where ASPXPage in ('ProEdit.aspx')
	update CSEasyScriptSupportedPages set TableName = 'humres', KeyColumn = 'res_id', KeyInputField = 'ID' where ASPXPage in ('HRMResource.aspx', 'HRMResourceCard.aspx')
	update CSEasyScriptSupportedPages set TableName = 'Opportunities', KeyColumn = 'ID', KeyInputField = 'ID' where ASPXPage in ('CRMOpportunityMaint.aspx', 'CRMOpportunityCard.aspx')
	update CSEasyScriptSupportedPages set TableName = 'ItemNumbers', KeyColumn = 'Number', KeyInputField = 'Number' where ASPXPage in ('LogItemNumber.aspx')
end
go

if exists(select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'CSEasyScriptSupportedPages' and COLUMN_NAME = 'TableName')
begin
	alter table [dbo].[CSEasyScriptSupportedPages]
		alter column [TableName] [nvarchar](255) NOT NULL

	alter table [dbo].[CSEasyScriptSupportedPages]
		alter column [KeyColumn] [nvarchar](255) NOT NULL

	alter table [dbo].[CSEasyScriptSupportedPages]
		alter column [KeyInputField] [nvarchar](255) NOT NULL
end
go