<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/validation.asp"-->
<!--#include file="./includes/encoding.asp"-->
<!--#include file="./includes/error_handler.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
<%
	' Initialize error handling
	Call InitializeErrorHandling("addlink.asp")

	' Get form inputs
	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"))

	' Validate required fields
	If Len(linkurl) = 0 Or Len(shortdescription) = 0 Or Len(appid) = 0 Then
		Call HandleValidationError("search.asp", "REQUIRED_FIELD")
	End If

	' Validate URL format
	If Not ValidateURL(linkurl) Then
		Call HandleValidationError("search.asp", "INVALID_INPUT")
	End If

	' Validate field lengths
	If Len(linkurl) > 2000 Then
		Call HandleValidationError("search.asp", "INVALID_INPUT")
	End If

	If Len(shortdescription) > 500 Then
		Call HandleValidationError("search.asp", "INVALID_INPUT")
	End If

	If Len(keywords) > 500 Then
		Call HandleValidationError("search.asp", "INVALID_INPUT")
	End If

	' Validate appid is numeric
	If Not ValidateID(appid) Then
		Call HandleValidationError("search.asp", "INVALID_ID")
	End If

	' Verify the application exists
	If Not RecordExists(objConn, "applications", "appid", appid) Then
		Call HandleValidationError("search.asp", "NOT_FOUND")
	End If

	' Insert using parameterized query
	strSQL = "INSERT INTO knowledgebase (linkurl, shortdescription, keywords, appid, isactive, clicks) VALUES (?, ?, ?, ?, 1, 0)"
	Dim recordsAffected
	recordsAffected = ExecuteParameterizedInsert(objConn, strSQL, Array(linkurl, shortdescription, keywords, appid))

	' Cleanup and redirect
	Call CleanupResources()

	If recordsAffected > 0 Then
		Response.Redirect("displayknowledgebase.asp?status=added")
	Else
		Response.Redirect("displayknowledgebase.asp?status=error&msg=Could+not+add+article")
	End If
%>