 </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>
 

	<cfset whrtbls=" permit	">
	<cfset whrcls=" where 1=1">

<cfif len(IssuedByAgent) gt 0>
	<cfset whrtbls=whrtbls & ", agent_name IssuedByAgentName, permit_agent permit_agent_IBA ">
	<cfset whrcls=whrcls & " and permit.permit_id= permit_agent_IBA.permit_id and
			permit_agent_IBA.agent_role='issued by' and

</cfif>


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

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


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


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


<cfif isdefined("permit_num") and 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>
