 </cfif>
 
 
-<cfif len(permit_type) gt 0>
+<cfif isdefined("permit_type") and len(permit_type) gt 0>
 	<cfset whrcls=whrcls & " AND permit.permit_id in (select permit_id from permit_type where permit_type = '#permit_type#')">
 </cfif>
-<cfif len(permit_regulation) gt 0>
+<cfif isdefined("permit_regulation") and len(permit_regulation) gt 0>
 	<cfset whrcls=whrcls & " AND permit.permit_id in (select permit_id from permit_type where permit_regulation = '#permit_regulation#')">
 </cfif>
 
 
-<cfif len(permit_remarks) gt 0>
+<cfif isdefined("permit_remarks") and len(permit_remarks) gt 0>
 	<cfset whrcls=whrcls & " AND upper(permit_remarks) like '%#ucase(permit_remarks)#%'">
 </cfif>
 

</cfif>


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

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


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


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


<cfif len(permit_num) gt 0>
	<cfif left(permit_num,1) is "=">
		<cfset whrcls=whrcls & " AND permit_num = '#ucase(mid(permit_num,2,len(permit_num)-1))#'">
	<cfelse>

			upper(IssuedByAgentName.agent_name) like '%#ucase(IssuedByAgent)#%' ">
</cfif>

<cfif isdefined("IssuedToAgent") and len(IssuedToAgent) gt 0>
	<cfset whrtbls=whrtbls & ", agent_name IssuedToAgentName, permit_agent permit_agent_ITA ">
	<cfset whrcls=whrcls & " and permit.permit_id= permit_agent_ITA.permit_id and
			permit_agent_ITA.agent_role='issued to' and
			permit_agent_ITA.agent_id=IssuedToAgentName.agent_id and
			upper(IssuedToAgentName.agent_name) like '%#ucase(IssuedToAgent)#%' ">
</cfif>
<cfif isdefined("ContactAgent") and len(ContactAgent) gt 0>
	<cfset whrtbls=whrtbls & ", agent_name ContactAgentName, permit_agent permit_agent_CA ">
	<cfset whrcls=whrcls & " and permit.permit_id= permit_agent_CA.permit_id and
			permit_agent_CA.agent_role='issued to' and
