 		</cfloop>
 	</select>
 
+	<label for="permit_num">Permit Identifier</label>
+	<input type="text" name="permit_num">
+
 	<label for="permit_remarks">Remarks</label>
 	<input type="text" name="permit_remarks">
 
-	<label for="permit_remarks">Permit Number</label>
-	<input type="text" name="permit_num">
 
 	<input type="submit" value="Search" class="schBtn">
 

</p>
<form name="findPermit" action="Permit.cfm" method="post">
	<input type="hidden" name="Action" value="search">
	<label for="IssuedByAgent">Issued By</label>
	<input type="text" name="IssuedByAgent">


<!--- set dateformat --->
<cfif not isdefined("sql") or len(#sql#) is 0>
	<!--- regular old search ---->
<cfset sql = "select
	permit.permit_id,
	getPreferredAgentName(permit_agent.agent_id) permit_agent,
	permit_agent.agent_role,
	permit.issued_Date,
	permitexp_Date,
	permit.permit_Num,
	permit.permit_Type,
	permit.permit_remarks
	permit_type.permit_type,
	permit_type.permit_regulation
from
	permit,
	permit_agent,
	permit_type
where
	permit.permit_id = permit_agent.permit_id (+) and
	permit.permit_id = permit_type.permit_id (+) ">



<cfif len(IssuedByAgent) gt 0>
	<cfset sql = "#sql# AND permit_agent.agent_role='issued by' and permit_agent.agent_id in (select agent_id from agent_name where upper(agent_name) like '%#ucase(IssuedByAgent)#%')">
</cfif>

<cfif len(IssuedToAgent) gt 0>
	<cfset sql = "#sql# AND permit_agent.agent_role='issued to' and permit_agent.agent_id in (select agent_id from agent_name where upper(agent_name) like '%#ucase(IssuedToAgent)#%')">
</cfif>


<cfif len(ContactAgent) gt 0>
	<cfset sql = "#sql# AND permit_agent.agent_role='contact' and permit_agent.agent_id in (select agent_id from agent_name where upper(agent_name) like '%#ucase(ContactAgent)#%')">
</cfif>

<cfif len(IssuedAfter) gt 0>
	<cfset sql = "#sql# AND issued_date >= '#issued_date#'">
</cfif>

<cfif len(IssuedBefore) gt 0>
	<cfset sql = "#sql# AND issued_date <= '#IssuedBefore#'">
</cfif>


<cfif len(ExpiresAfter) gt 0>
	<cfset sql = "#sql# AND exp_date >= '#ExpiresAfter#'">
</cfif>


<cfif len(ExpiresBefore) gt 0>
	<cfset sql = "#sql# AND exp_date <= '#ExpiresBefore#'">
</cfif>


<cfif len(permit_num) gt 0>
	<cfset sql = "#sql# AND upper(permit_Num) like '%#ucase(permit_Num)#%'">
</cfif>


<cfif len(permit_type) gt 0>
	<cfset sql = "#sql# AND permit.permit_id in (select permit_id from permit_type where permit_type = '#permit_type#')">
</cfif>


<cfif len(permit_remarks) gt 0>
	<cfset sql = "#sql# AND upper(permit_remarks) like '%#ucase(permit_remarks)#%'">
</cfif>

<cfif len(#permit_id#) gt 0>
	<cfset sql = "#sql# AND permit.permit_id = #permit_id#">
</cfif>


<cfquery name="matchPermit" datasource="user_login" username="#session.dbuser#" password="#decrypt(session.epw,session.sessionKey)#">
	#preservesinglequotes(sql)#
</cfquery>

<cfdump var=#matchPermit#>


<table border>
	<tr>
