<!--#include file="./includes/sql.asp"-->
<%
' Get form inputs for KB article
Dim linkurl, shortdescription, keywords, appid
linkurl = Trim(Request.Form("linkurl"))
shortdescription = Trim(Request.Form("shortdescription"))
keywords = Trim(Request.Form("keywords"))
appid = Trim(Request.Form("appid"))

' Get form inputs for new topic
Dim newappname, newappdescription, newsupportteamid
Dim newapplicationnotes, newinstallpath, newdocumentationpath, newisactive
newappname = Trim(Request.Form("newappname"))
newappdescription = Trim(Request.Form("newappdescription"))
newsupportteamid = Trim(Request.Form("newsupportteamid"))
newapplicationnotes = Trim(Request.Form("newapplicationnotes"))
newinstallpath = Trim(Request.Form("newinstallpath"))
newdocumentationpath = Trim(Request.Form("newdocumentationpath"))
newisactive = Request.Form("newisactive")

' Get form inputs for new support team
Dim newsupportteamname, newsupportteamurl, newappownerid
newsupportteamname = Trim(Request.Form("newsupportteamname"))
newsupportteamurl = Trim(Request.Form("newsupportteamurl"))
newappownerid = Trim(Request.Form("newappownerid"))

' Get form inputs for new app owner
Dim newappownername, newappownersso
newappownername = Trim(Request.Form("newappownername"))
newappownersso = Trim(Request.Form("newappownersso"))

' Basic validation for KB article
If Len(linkurl) = 0 Or Len(shortdescription) = 0 Or Len(appid) = 0 Then
	Response.Write("Required fields missing")
	objConn.Close
	Response.End
End If

If Len(linkurl) > 2000 Or Len(shortdescription) > 500 Or Len(keywords) > 500 Then
	Response.Write("Field length exceeded")
	objConn.Close
	Response.End
End If

' Handle new topic creation
If appid = "new" Then
	If Len(newappname) = 0 Then
		Response.Write("New topic name is required")
		objConn.Close
		Response.End
	End If

	If Len(newsupportteamid) = 0 Then
		Response.Write("Support team is required for new topic")
		objConn.Close
		Response.End
	End If

	' Validate field lengths for new topic
	If Len(newappname) > 50 Or Len(newappdescription) > 255 Or Len(newapplicationnotes) > 512 Or Len(newinstallpath) > 255 Or Len(newdocumentationpath) > 512 Then
		Response.Write("New topic field length exceeded")
		objConn.Close
		Response.End
	End If

	' Handle new support team creation (nested)
	If newsupportteamid = "new" Then
		If Len(newsupportteamname) = 0 Then
			Response.Write("New support team name is required")
			objConn.Close
			Response.End
		End If

		If Len(newappownerid) = 0 Then
			Response.Write("App owner is required for new support team")
			objConn.Close
			Response.End
		End If

		If Len(newsupportteamname) > 50 Or Len(newsupportteamurl) > 512 Then
			Response.Write("New support team field length exceeded")
			objConn.Close
			Response.End
		End If

		' Handle new app owner creation (doubly nested)
		If newappownerid = "new" Then
			If Len(newappownername) = 0 Or Len(newappownersso) = 0 Then
				Response.Write("App owner name and SSO are required")
				objConn.Close
				Response.End
			End If

			If Len(newappownername) > 50 Or Len(newappownersso) > 255 Then
				Response.Write("App owner field length exceeded")
				objConn.Close
				Response.End
			End If

			' Escape single quotes for new app owner
			Dim escapedOwnerName, escapedOwnerSSO
			escapedOwnerName = Replace(newappownername, "'", "''")
			escapedOwnerSSO = Replace(newappownersso, "'", "''")

			' Insert new app owner
			Dim sqlNewOwner
			sqlNewOwner = "INSERT INTO appowners (appowner, sso, isactive) " & _
			              "VALUES ('" & escapedOwnerName & "', '" & escapedOwnerSSO & "', 1)"

			On Error Resume Next
			objConn.Execute sqlNewOwner

			If Err.Number <> 0 Then
				Response.Write("Error creating new app owner: " & Err.Description)
				objConn.Close
				Response.End
			End If

			' Get the newly created app owner ID
			Dim rsNewOwner
			Set rsNewOwner = objConn.Execute("SELECT LAST_INSERT_ID() AS newid")
			newappownerid = rsNewOwner("newid")
			rsNewOwner.Close
			Set rsNewOwner = Nothing
			On Error Goto 0
		End If

		' Escape single quotes for new support team
		Dim escapedTeamName, escapedTeamURL
		escapedTeamName = Replace(newsupportteamname, "'", "''")
		escapedTeamURL = Replace(newsupportteamurl, "'", "''")

		' Insert new support team with selected or newly created app owner
		Dim sqlNewTeam
		sqlNewTeam = "INSERT INTO supportteams (teamname, teamurl, appownerid, isactive) " & _
		             "VALUES ('" & escapedTeamName & "', '" & escapedTeamURL & "', " & newappownerid & ", 1)"

		On Error Resume Next
		objConn.Execute sqlNewTeam

		If Err.Number <> 0 Then
			Response.Write("Error creating new support team: " & Err.Description)
			objConn.Close
			Response.End
		End If

		' Get the newly created support team ID
		Dim rsNewTeam
		Set rsNewTeam = objConn.Execute("SELECT LAST_INSERT_ID() AS newid")
		newsupportteamid = rsNewTeam("newid")
		rsNewTeam.Close
		Set rsNewTeam = Nothing
		On Error Goto 0
	End If

	' Escape single quotes for new topic
	Dim escapedAppName, escapedAppDesc, escapedAppNotes, escapedInstallPath, escapedDocPath
	escapedAppName = Replace(newappname, "'", "''")
	escapedAppDesc = Replace(newappdescription, "'", "''")
	escapedAppNotes = Replace(newapplicationnotes, "'", "''")
	escapedInstallPath = Replace(newinstallpath, "'", "''")
	escapedDocPath = Replace(newdocumentationpath, "'", "''")

	' Convert isactive checkbox
	Dim isActiveValue
	If newisactive = "1" Then
		isActiveValue = 1
	Else
		isActiveValue = 0
	End If

	' Insert new application/topic
	Dim sqlNewApp
	sqlNewApp = "INSERT INTO applications (appname, appdescription, supportteamid, applicationnotes, installpath, documentationpath, isactive, isinstallable, ishidden, isprinter, islicenced) " & _
	            "VALUES ('" & escapedAppName & "', '" & escapedAppDesc & "', " & newsupportteamid & ", '" & escapedAppNotes & "', '" & escapedInstallPath & "', '" & escapedDocPath & "', " & isActiveValue & ", 0, 0, 0, 0)"

	On Error Resume Next
	objConn.Execute sqlNewApp

	If Err.Number <> 0 Then
		Response.Write("Error creating new topic: " & Err.Description)
		objConn.Close
		Response.End
	End If

	' Get the newly created topic ID
	Dim rsNewApp
	Set rsNewApp = objConn.Execute("SELECT LAST_INSERT_ID() AS newid")
	appid = rsNewApp("newid")
	rsNewApp.Close
	Set rsNewApp = Nothing
	On Error Goto 0
End If

' Escape single quotes for KB article
linkurl = Replace(linkurl, "'", "''")
shortdescription = Replace(shortdescription, "'", "''")
keywords = Replace(keywords, "'", "''")

' Build INSERT statement for KB article
Dim strSQL
strSQL = "INSERT INTO knowledgebase (linkurl, shortdescription, keywords, appid, isactive, clicks) " & _
         "VALUES ('" & linkurl & "', '" & shortdescription & "', '" & keywords & "', " & appid & ", 1, 0)"

On Error Resume Next
objConn.Execute strSQL

If Err.Number = 0 Then
	objConn.Close
	Response.Redirect("displayknowledgebase.asp?status=added")
Else
	objConn.Close
	Response.Redirect("displayknowledgebase.asp?status=error&msg=" & Server.URLEncode("Error: " & Err.Description))
End If
%>
