<cfcomponent displayname="Application"  hint="Handle the application" output="true">

<!--- Set up the application. --->
    <cfset THIS.Name = "AppCFC" />
    <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 2, 0, 0 ) />
    <cfset THIS.SessionManagement = true />
    <cfset THIS.SetClientCookies = true />
    <cfset THIS.clientmanagement="yes">
  
    <!--- Define the page request properties. --->
    <cfsetting  requesttimeout="2000"  showdebugoutput="true"  enablecfoutputonly="false"/>
   <cffunction  name="OnApplicationStart"   access="public" returntype="boolean"  output="false"  hint="Fires when the application is first created.">
         <!--- Return out. --->
		     <cfset Application.Version = "7.0.0">
			<cfset Application.powerpath_version = "11.20.2009">
            <cfset Application.DataSrc = "hhapowerpath" />
            <cfset Application.dbPatientFiles = "hhapatientfiles" />
            <cfset Application.NoEmptyDates = 0>
			<cfset application.debug=0>
            <cfset Application.datatype = ".dat">
            <cfset Application.enckey ="54kczhzgHTj4a8n+SRZPyg==" />
            <cfif CGI.SERVER_NAME EQ "54.235.249.158" || CGI.SERVER_NAME EQ "dev.myhomecarebiz.com">
				<cfset Application.DEV = true />
                <cfset application.downloadpath="http://dev.myhomecarebiz.com/">
                <cfset application.webdatapath="#application.downloadpath#/datafiles/">
                <cfset application.datapath="/var/www/datafiles/">
                <cfset application.telephonypath="/var/www/exportdata/">
                <cfset application.secure="http://dev.myhomecarebiz.com/">
                <cfset application.siteURL="http://dev.myhomecarebiz.com/">
                <cfset Application.ServerName = "Dev Amazon 54.235.249.158">
            <!---Amazon Live Server--->
            <cfelse>
                <cfset Application.DEV = false />
                <cfset application.downloadpath="https://secure.myhomecarebiz.com">
                <cfset application.webdatapath="#application.downloadpath#/datafiles/">
                <cfset application.datapath="/var/www/datafiles/">
                <cfset application.telephonypath="/var/www/exportdata/">
                <cfset application.secure="https://secure.myhomecarebiz.com/">
                <cfset application.siteURL="https://secure.myhomecarebiz.com/">
                <cfset Application.ServerName = "#cgi.server_NAME#">
                <cfset Application.GoogleMapsAPIKey = "AIzaSyAXWgATrPBtb7em-iICftXhJT-e6a4BroQ"/>
                   <!---<cfinclude template="/Application_error_settings.cfm" />--->
            </cfif>
            <cfswitch expression="#CGI.SERVER_NAME#">
	            <!---Amazon Dev Server--->
                <cfcase value="54.235.249.158">
                    <cfset Application.DataSrc = "hhapowerpath">
                </cfcase>
                <cfcase value="secure.myhomecarebiz.com">
                    <cfset Application.DataSrc = "hhapowerpath">
                </cfcase>
                <cfcase value="mhcblive.ctmzbiqfwjd0.us-east-1.rds.amazonaws.com">
                    <cfset Application.DataSrc = "hhapowerpath">
                </cfcase>
                <cfdefaultcase>
                    <cfset Application.DataSrc = "hhapowerpath">
                </cfdefaultcase>
            </cfswitch>
        <cfreturn true />
    </cffunction>


    <cffunction name="OnSessionStart"  access="public" returntype="void"  output="false"   hint="Fires when the session is first created.">
         <!--- Return out. ---> 
        <cfreturn />
    </cffunction>


    <cffunction  name="OnRequestStart"   access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
       <!--- Define arguments. --->
      <cfargument  name="TargetPage" type="string" required="true"  />
		<cfset request.send_errors_to = "velmurugan@myhomecarebiz.com,velmurugan_g@outlook.com"> 
     <!---   <cfset request.send_errors_to = "velmurugan_g@outlook.com,velmurugan@myhomecarebiz.com"> --->
		  <cfset request.from = 'error@myhomecarebiz.com' />
          <cfset Request.prefix_db_lookup = "hhapowerpath" />
		<cfset Request.prefix_db_agency = "prefix_db_agency_invalid" />
        
        <cfif isDefined("URL.cron") and URL.cron eq 1>
            <cfset Request.Agency_ID = true />
        </cfif>
        
            <cfif isDefined("FORM.prefix_db_agency_id")>
                <cfif isNumeric(FORM.prefix_db_agency_id)>
                    <cfset Request.prefix_db_agency = "agency_"&FORM.prefix_db_agency_id />
                    <cfset Request.Agency_ID = FORM.prefix_db_agency_id />
                </cfif>
            
                <!--- delete agency_id to resolve conflict with cfinsert/cfupdate --->
                <cfset rc = StructDelete(FORM, "prefix_db_agency_id", "True")>
            <cfelseif isDefined("URL.prefix_db_agency_id") or isDefined("URL.passthrough_data")>
                            <cfif isDefined("URL.passthrough_data") and URL.passthrough_data neq "">
                                <cfset ptd = listToArray(#URL.passthrough_data#, "||", false, true) />
                    
                            <cfloop from="1" to="#arrayLen(ptd)#" index="i">
                                <cfset key = gettoken(ptd[i],1,"|") />
                                <cfset value = gettoken(ptd[i],2,"|") />
                    
                                <cfif key eq "prefix_db_agency_id">
                                    <cfset Request.prefix_db_agency = "agency_"&value />
                                    <cfset Request.Agency_ID = value />
                                    <cfset Request.agency_found = true />
                                </cfif>
                            </cfloop>
                        <cfelse>
                            <cfif isNumeric(URL.prefix_db_agency_id)>
                                <cfquery name="get_Agency" datasource='#Application.DataSrc#'>
                                    SELECT agency_id FROM Agency WHERE agency_id = #URL.prefix_db_agency_id# or agency_login = #URL.prefix_db_agency_id# limit 1
                                </cfquery>
                    
                                <cfif get_Agency.RecordCount gt 0>
                                    <cfset Request.agency_found = true />
                    
                                    <cfquery name="check_db" datasource='#Application.DataSrc#'>
                                        SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'agency_#get_Agency.agency_id#'
                                    </cfquery>
                    
                                    <cfif check_db.RecordCount gt 0>
                                        <cfset Request.prefix_db_agency = "agency_"&get_Agency.agency_id />
                                        <cfset Request.Agency_ID = get_Agency.agency_id />
                                    </cfif>
                                </cfif>
                            </cfif>
                        </cfif>
			<cfelseif isDefined("Session.Super_AgencyId") and isDefined("FORM.Super_AgencyId")>
                <cfset Session.Super_AgencyId = FORM.Super_AgencyId />
            
                <cfset Request.prefix_db_agency = "agency_"&FORM.Super_AgencyId />
                <cfset Request.Agency_ID = FORM.Super_AgencyId />
            <cfelseif isDefined("session.Super_AgencyId")>
                <cfset Request.prefix_db_agency = "agency_"&session.Super_AgencyId />
                <cfset Request.Agency_ID = session.Super_AgencyId />
            <cfelseif isDefined("session.AgencyId")>
                <cfset Request.prefix_db_agency = "agency_"&session.AgencyId />
                <cfset Request.Agency_ID = session.AgencyId />
            </cfif>
            
            <cfif Request.prefix_db_agency eq 'agency_455'>
                <cfset table_files = Request.prefix_db_lookup&'.files_old' />
            <cfelse>
                <cfset table_files = Request.prefix_db_agency&'.files' /> 
            </cfif>
          <cfif isDefined("session.AgencyId")>
            <cfquery  name='getTimeZone' datasource='#Application.DataSrc#'>
                SELECT *,'' AS coding_wizard FROM #Request.prefix_db_lookup#.Agency WHERE Agency_Id = '#session.AgencyId#'
            </cfquery>
             <cfquery  name='getuseragency' datasource='#Application.DataSrc#'>
                SELECT Agency.*  FROM #Request.prefix_db_lookup#.pEmployee
                JOIN #Request.prefix_db_lookup#.Agency  ON Agency.Agency_ID = pEmployee.Loc_ID
                WHERE pEmployee.Username = '#session.username#'
                AND pEmployee.status = 0 AND Agency.status = 'active'
            </cfquery>
                <cfset myTime = #now()#>
				 <cfif getTimeZone.TimeZone EQ 'CST'>
                    <cfset myTime = #TimeFormat(DateAdd('h', -1, myTime), 'HH:mm:ss')#>
                <cfelseif getTimeZone.TimeZone EQ 'MST'>
                    <cfset myTime = #TimeFormat(DateAdd('h', -2, myTime), 'HH:mm:ss')#>
                <cfelseif  getTimeZone.TimeZone EQ 'PST'>
                    <cfset myTime = #TimeFormat(DateAdd('h', -3, myTime), 'HH:mm:ss')#>
                <cfelseif  getTimeZone.TimeZone EQ 'AKST'>
                    <cfset myTime = #TimeFormat(DateAdd('h', -4, myTime), 'HH:mm:ss')#>
                <cfelseif  getTimeZone.TimeZone EQ 'HST'>
                    <cfset myTime = #TimeFormat(DateAdd('h', -7, myTime), 'HH:mm:ss')#>
                <cfelse>
                    <cfset myTime = #TimeFormat(myTime, 'HH:mm:ss')#>
                </cfif>
               <cfset myDateTime = DateFormat(Now(), 'yyyy-mm-dd') & " " & myTime />
         </cfif>
        <!--- Return out. --->
        <cfreturn true />
    </cffunction>


    <cffunction name="OnRequest"  access="public"  returntype="void" output="true" hint="Fires after pre page processing is complete.">
        <!--- Define arguments. --->
        <cfargument name="TargetPage"  type="string"  required="true"  />
        <!--- Include the requested page. --->
        <cfinclude template="#ARGUMENTS.TargetPage#" />
        <cfinclude template="Application_common.cfm" />
            <!--- Return out. --->
        <cfreturn />
    </cffunction>


    <cffunction   name="OnRequestEnd"  access="public"  returntype="void"  output="true"   hint="Fires after the page processing is complete.">
           <!--- Return out. --->
        <cfreturn />
    </cffunction>


    <cffunction name="OnSessionEnd"  access="public"  returntype="void"  output="false" hint="Fires when the session is terminated.">

        <!--- Define arguments. --->
        <cfargument   name="SessionScope" type="struct"  required="true" />
        <cfargument name="ApplicationScope"  type="struct" required="false"   default="#StructNew()#" />
       <!--- Return out. --->
        <cfreturn />
    </cffunction>


    <cffunction  name="OnApplicationEnd" access="public" returntype="void"output="false"hint="Fires when the application is terminated.">

        <!--- Define arguments. --->
        <cfargument  name="ApplicationScope" type="struct" required="false"  default="#StructNew()#"/>

        <!--- Return out. --->
        <cfreturn />
    </cffunction>


    <cffunction   name="OnError"  access="public"    returntype="void"  output="true"  hint="Fires when an exception occures that is not caught by a try/catch.">

        <!--- Define arguments. --->
        <cfargument  name="Exception" type="any" required="true" />
        <cfargument name="EventName"   type="string"  required="false"   default=""  />
       <!--- <cfinclude template="Application_error_settings.cfm" />--->
        <!--- Log all errors. ---> 
		<cflog file="#This.Name#" type="error"  text="Event Name: #Arguments.Eventname#" > 
		<cflog file="#This.Name#" type="error"  text="Message: #Arguments.Exception.message#"> 
		<!---<cflog file="#This.Name#" type="error"  text="Root Cause Message: #Arguments.Exception.rootcause.message#"> --->
		<!--- Display an error message if there is a page context. ---> 
       <!--- <cfdump var="#Arguments.Exception#"><cfabort>--->
		<cfif NOT (Arguments.EventName IS "onSessionEnd") OR  (Arguments.EventName IS "onApplicationEnd")> 
                 <div class="row">
                   
								
							<cfdump var="#Arguments.EventName#">
                                              <cfdump var ="#Arguments.Exception#">
                                                  <td nowrap valign=top colspan=2>
                                                    <b>Form Variables:</b><br />
                                                    <cfdump var="#FORM#"><br>

                                                    <b>URL Variables:</b><br />
                                                    <cfdump var="#URL#"><br>

                                                    <b>Request Variables:</b><br />
                                                    <cfdump var="#Request#"><br>

                                                    <b>Session Variables:</b><br />
                                                    <cfdump var="#Session#"><br>
                     
                    </div>
                
		</cfif> 
        <!--- Return out. --->
        <cfreturn />
    </cffunction>
    
</cfcomponent>