<%
'
'    Filename: fac_persoongroeprecord.asp
'    Generated with CodeCharge 2.0.5
'    ASP 2.0 & Templates.ccp build 11/30/2001
'

'-------------------------------
' 
' fac_persoongroeprecord CustomIncludes Event begin
%>
<!-- #INCLUDE FILE="Common.asp" -->
<%
' fac_persoongroeprecord CustomIncludes Event end


'===============================
' Save Page and File Name available into variables
'-------------------------------
sFileName = "fac_persoongroeprecord.asp"
sTemplateFileName = "fac_persoongroeprecord.html"
'===============================


'===============================
' fac_persoongroeprecord CustomSecurity Event begin
checkAutorisation("WEB_PRSMSU")
' fac_persoongroeprecord CustomSecurity Event end
'===============================

'===============================
' fac_persoongroeprecord Open Event begin
' fac_persoongroeprecord Open Event end
'===============================

'===============================
' fac_persoongroeprecord OpenAnyPage Event begin
' fac_persoongroeprecord OpenAnyPage Event end
'===============================

'===============================
'Save the name of the form and type of action into the variables
'-------------------------------
sAction = GetParam("FormAction")
sForm = GetParam("FormName")
'===============================

' fac_persoongroeprecord Show begin
'===============================
' Perform the form's action
'-------------------------------
' Initialize error variables
'-------------------------------
sPersoonRecordErr = ""

'-------------------------------
' Select the FormAction
'-------------------------------
Select Case sForm
  Case "PersoonRecord"
    PersoonRecordAction(sAction)
end select
'===============================

'===============================
' Display page
'-------------------------------
' Load HTML template for this page
'-------------------------------
LoadTemplate sAppPath & sTemplateFileName, "main"
'-------------------------------
' Load HTML template of Header and Footer
'-------------------------------
'-------------------------------
SetVar "FileName", sFileName


'-------------------------------
' Step through each form
'-------------------------------
PersoonRecord_Show
GroepGrid_Show
'-------------------------------
' Process page templates
'-------------------------------
Parse "main", False
'-------------------------------
' Output the page to the browser
'-------------------------------
Response.write PrintVar("main")

' fac_persoongroeprecord Show end

'-------------------------------
' Destroy all object variables
'-------------------------------

' fac_persoongroeprecord Close Event begin
' fac_persoongroeprecord Close Event end

cn.Close
Set cn = Nothing
UnloadTemplate
'===============================

'===============================
' Display Grid Form
'-------------------------------
Sub GroepGrid_Show()
'-------------------------------
' Initialize variables  
'-------------------------------
  Dim rs
  Dim sWhere : sWhere = ""
  Dim sOrder : sOrder = ""
  Dim sSQL : sSQL = ""
  Dim sFormTitle: sFormTitle = "Autorisatiegroepen"
  Dim HasParam : HasParam = false
  Dim bReq : bReq = true
  Dim iSort : iSort = ""
  Dim iSorted : iSorted = ""
  Dim sDirection : sDirection = ""
  Dim sSortParams : sSortParams = ""
  Dim iRecordsPerPage : iRecordsPerPage = 25
  Dim iCounter : iCounter = 0
  Dim iPage : iPage = 0
  Dim bEof : bEof = False
  Dim iTmpI : iTmpI = 0
  Dim iTmpJ : iTmpJ = 0
  Dim sCountSQL : sCountSQL = ""
  Dim sActionFileName : sActionFileName = "fac_persoongroeprecord1.asp"

  SetVar "TransitParams", "PRS_PERSLID_KEY=" & ToURL(GetParam("PRS_PERSLID_KEY")) & "&fFAC_GROEP_KEY=" & ToURL(GetParam("fFAC_GROEP_KEY")) & "&fPRS_AFDELING_KEY=" & ToURL(GetParam("fPRS_AFDELING_KEY")) & "&fPRS_PERSLID_NAAM=" & ToURL(GetParam("fPRS_PERSLID_NAAM")) & "&"
  SetVar "FormParams", "PRS_PERSLID_KEY=" & ToURL(GetParam("PRS_PERSLID_KEY")) & "&fFAC_GROEP_KEY=" & ToURL(GetParam("fFAC_GROEP_KEY")) & "&fPRS_AFDELING_KEY=" & ToURL(GetParam("fPRS_AFDELING_KEY")) & "&fPRS_PERSLID_NAAM=" & ToURL(GetParam("fPRS_PERSLID_NAAM")) & "&"

'-------------------------------
' Build WHERE statement
'-------------------------------
  pPRS_PERSLID_KEY = GetParam("PRS_PERSLID_KEY")
  if IsNumeric(pPRS_PERSLID_KEY) and not isEmpty(pPRS_PERSLID_KEY) then pPRS_PERSLID_KEY = ToSQL(pPRS_PERSLID_KEY, "Number") else pPRS_PERSLID_KEY = Empty
  if not isEmpty(pPRS_PERSLID_KEY) then
    HasParam = true
    sWhere = sWhere & "F.PRS_PERSLID_KEY=" & pPRS_PERSLID_KEY
  else
    bReq = false
  end if


  if HasParam then
    sWhere = " AND (" & sWhere & ")"
  end if
  
'-------------------------------
' Build ORDER BY statement
'-------------------------------
  iSort = GetParam("FormGroepGrid_Sorting")
  iSorted = GetParam("FormGroepGrid_Sorted")
  sDirection = ""
  if IsEmpty(iSort) then
    SetVar "Form_Sorting", ""
  else
    if iSort = iSorted then 
      SetVar "Form_Sorting", ""
      sDirection = " DESC"
      sSortParams = "FormGroepGrid_Sorting=" & iSort & "&FormGroepGrid_Sorted=" & iSort & "&"
    else
      SetVar "Form_Sorting", iSort
      sDirection = " ASC"
      sSortParams = "FormGroepGrid_Sorting=" & iSort & "&FormGroepGrid_Sorted=" & "&"
    end if
    if iSort = 1 then sOrder = " order by F.FAC_GEBRUIKERSGROEP_KEY" & sDirection
    if iSort = 2 then sOrder = " order by F1.FAC_GROEP_OMSCHRIJVING" & sDirection
  end if

'-------------------------------
' Build base SQL statement
'-------------------------------
  sSQL = "select F.FAC_GEBRUIKERSGROEP_KEY as F_FAC_GEBRUIKERSGROEP_KEY, " & _
    "F.FAC_GROEP_KEY as F_FAC_GROEP_KEY, " & _
    "F.PRS_PERSLID_KEY as F_PRS_PERSLID_KEY, " & _
    "F1.FAC_GROEP_KEY as F1_FAC_GROEP_KEY, " & _
    "F1.FAC_GROEP_OMSCHRIJVING as F1_FAC_GROEP_OMSCHRIJVING " & _
    " from FAC_GEBRUIKERSGROEP F, FAC_GROEP F1" & _
    " where F1.FAC_GROEP_KEY=F.FAC_GROEP_KEY  "
'-------------------------------

'-------------------------------
' GroepGrid Open Event begin
' GroepGrid Open Event end
'-------------------------------

'-------------------------------
' Assemble full SQL statement
'-------------------------------
  sSQL = sSQL & sWhere & sOrder
  if sCountSQL = "" then
    iTmpI = instr(lcase(sSQL), "select")
    iTmpJ = instr(lcase(sSQL), "from") - 1
    sCountSQL = replace(sSQL, mid(sSQL, iTmpI + 6, iTmpJ - 6), " count(*) ")
    iTmpI = instr(lcase(sCountSQl), "order by")
    if iTmpI > 1 then sCountSQL = left(sCountSQL, iTmpI - 1)
  end if
'-------------------------------

SetVar "FormTitle", sFormTitle

'-------------------------------
' Process the link to the record page
'-------------------------------
  SetVar "FormAction", sActionFileName
'-------------------------------

'-------------------------------
' Process the parameters for sorting
'-------------------------------
  SetVar "SortParams", sSortParams
'-------------------------------

'-------------------------------
' Process if form has all required parameter
'-------------------------------
  if not bReq then
    SetVar "DListGroepGrid", ""
    Parse "GroepGridNoRecords", False
    SetVar "GroepGridNavigator", ""
    Parse "FormGroepGrid", False
    exit sub
  end if
'-------------------------------

'-------------------------------
' Open the recordset
'-------------------------------
  openrs rs, sSQL
'-------------------------------

'-------------------------------
' Process empty recordset
'-------------------------------
  if rs.eof then
    set rs = nothing
    SetVar "DListGroepGrid", ""
    Parse "GroepGridNoRecords", False
    SetVar "GroepGridNavigator", ""
    Parse "FormGroepGrid", False
    exit sub
  end if
'-------------------------------

'-------------------------------
' Initialize page counter and records per page
'-------------------------------
  iRecordsPerPage = 25
  iCounter = 0
'-------------------------------

'-------------------------------
' Process page scroller
'-------------------------------
  iPage = GetParam("FormGroepGrid_Page")
  openrs rsCount, sCountSQL
  dResult = CLng(rsCount.fields(0).value) / iRecordsPerPage
  iPageCount = int(dResult)
  if iPageCount < dResult then iPageCount = iPageCount + 1
  SetVar "GroepGridPageCount", iPageCount
  set rsCount = nothing
  if IsEmpty(iPage) then
    iPage = 1
  else
    if iPage = "last" then iPage = iPageCount
  end if
  while not rs.eof and iCounter < (iPage-1)*iRecordsPerPage
    rs.movenext
    iCounter = iCounter + 1
  wend
  iCounter = 0
'-------------------------------

'-------------------------------
' Display grid based on recordset
'-------------------------------
  while not rs.EOF  and iCounter < iRecordsPerPage
'-------------------------------
' Create field variables based on database fields
'-------------------------------
    fldFAC_GEBRUIKERSGROEP_KEY = GetValue(rs, "F_FAC_GEBRUIKERSGROEP_KEY")
    fldFAC_GROEP_KEY_URLLink = "fac_persoongroeprecord2.asp"
    fldFAC_GROEP_KEY_FAC_GEBRUIKERSGROEP_KEY = GetValue(rs, "F_FAC_GEBRUIKERSGROEP_KEY")
    fldFAC_GROEP_KEY = GetValue(rs, "F1_FAC_GROEP_OMSCHRIJVING")
    fldPRS_PERSLID_KEY = GetValue(rs, "F_PRS_PERSLID_KEY")
'-------------------------------
' GroepGrid Show begin
'-------------------------------

'-------------------------------
' GroepGrid Show Event begin
' GroepGrid Show Event end
'-------------------------------

'-------------------------------
' Replace Template fields with database values
'-------------------------------
    
      SetVar "FAC_GEBRUIKERSGROEP_KEY", ToHTML(fldFAC_GEBRUIKERSGROEP_KEY)
      SetVar "PRS_PERSLID_KEY", ToHTML(fldPRS_PERSLID_KEY)
      SetVar "FAC_GROEP_KEY", ToHTML(fldFAC_GROEP_KEY)
      SetVar "FAC_GROEP_KEY_URLLink", fldFAC_GROEP_KEY_URLLink
      SetVar "PrmFAC_GROEP_KEY_FAC_GEBRUIKERSGROEP_KEY", ToURL(fldFAC_GROEP_KEY_FAC_GEBRUIKERSGROEP_KEY)
    Parse "DListGroepGrid", True

'-------------------------------
' GroepGrid Show end
'-------------------------------

'-------------------------------
' Move to the next record and increase record counter
'-------------------------------
    rs.MoveNext
    iCounter = iCounter + 1
  wend
'-------------------------------


'-------------------------------
' GroepGrid Navigation begin
'-------------------------------
  bEof = rs.eof
  if rs.eof and iPage = 1 then
	SetVar "GroepGridNavigator", ""
  else
    iCounter = 1
    iHasPages = iPage
    iDisplayPages = 0
    iNumberOfPages = 10
    iHasPages = iPageCount
    if (iHasPages - iPage) < int(iNumberOfPages/2) then
      iStartPage = iHasPages - iNumberOfPages
    else
      iStartPage = iPage - iNumberOfPages + int(iNumberOfPages/2)
    end if
    if iStartPage < 0 then iStartPage = 0
    for iPageCount = iStartPage + 1 to iPage - 1
      SetVar "NavigatorPageNumber", iPageCount
      SetVar "NavigatorPageNumberView", iPageCount
      Parse "GroepGridNavigatorPages", true
      iDisplayPages = iDisplayPages + 1
    next
    SetVar "NavigatorPageSwitch", "_"
    SetVar "NavigatorPageNumber", iPage
    SetVar "NavigatorPageNumberView", iPage
    Parse "GroepGridNavigatorPages", true
    iDisplayPages = iDisplayPages + 1
    SetVar "NavigatorPageSwitch", ""
    iPageCount = iPage + 1
    while iDisplayPages < iNumberOfPages and iStartPage + iDisplayPages < iHasPages
      SetVar "NavigatorPageNumber", iPageCount
      SetVar "NavigatorPageNumberView", iPageCount
      Parse "GroepGridNavigatorPages", true
      iDisplayPages = iDisplayPages + 1
      iPageCount = iPageCount + 1
    wend
    if bEof then
      SetVar "GroepGridNavigatorLastPage", "_"
    else
      SetVar "NextPage", (iPage + 1)
    end if
    if iPage = 1 then
      SetVar "GroepGridNavigatorFirstPage", "_"
    else
      SetVar "PrevPage", (iPage - 1)
    end if
    SetVar "GroepGridCurrentPage", iPage
    Parse "GroepGridNavigator", False
  end if
'-------------------------------
' GroepGrid Navigation end
'-------------------------------

'-------------------------------
' Finish form processing
'-------------------------------
  set rs = nothing
  SetVar "GroepGridNoRecords", ""
  Parse "FormGroepGrid", False

'-------------------------------
' GroepGrid Close Event begin
' GroepGrid Close Event end
'-------------------------------
End Sub
'===============================

'===============================
' Action of the Record Form
'-------------------------------
Sub PersoonRecordAction(sAction)
'-------------------------------
' Initialize variables  
'-------------------------------
  Dim bExecSQL: bExecSQL = true
  Dim sActionFileName : sActionFileName = ""
  Dim sParams : sParams = "?"
  Dim sWhere : sWhere = "" 
  Dim bErr : bErr = False
  Dim pPKPRS_PERSLID_KEY : pPKPRS_PERSLID_KEY = ""
'-------------------------------

'-------------------------------
' PersoonRecord Action begin
'-------------------------------
  sActionFileName = "fac_persoongroepgrid.asp"
  sParams = sParams & "fPRS_AFDELING_KEY=" & ToURL(GetParam("Trn_fPRS_AFDELING_KEY")) & "&"
  sParams = sParams & "fFAC_GROEP_KEY=" & ToURL(GetParam("Trn_fFAC_GROEP_KEY")) & "&"
  sParams = sParams & "fPRS_PERSLID_NAAM=" & ToURL(GetParam("Trn_fPRS_PERSLID_NAAM"))

'-------------------------------
' CANCEL action
'-------------------------------
  if sAction = "cancel" then

'-------------------------------
' PersoonRecord BeforeCancel Event begin
' PersoonRecord BeforeCancel Event end
'-------------------------------
    cn.Close
    Set cn = Nothing
    response.redirect sActionFileName & sParams
  end if
'-------------------------------

'-------------------------------
' Load all form fields into variables
'-------------------------------

'-------------------------------
' Validate fields
'-------------------------------
  if sAction = "insert" or sAction = "update" then
'-------------------------------
' PersoonRecord Check Event begin
' PersoonRecord Check Event end
'-------------------------------
    If len(sPersoonRecordErr) > 0 then
      exit sub
    end if
  end if
'-------------------------------

'-------------------------------
' PersoonRecord BeforeExecute Event begin
' PersoonRecord BeforeExecute Event end
'-------------------------------

'-------------------------------
' Execute SQL statement
'-------------------------------
if len(sPersoonRecordErr) > 0 then Exit Sub
  on error resume next
  if bExecSQL then 
    cn.execute sSQL
  end if
  sPersoonRecordErr = ProcessError
  on error goto 0
  if len(sPersoonRecordErr) > 0 then Exit Sub
  cn.Close
  Set cn = Nothing
  response.redirect sActionFileName & sParams
'-------------------------------
' PersoonRecord Action end
'-------------------------------
end sub
'===============================

'===============================
' Display Record Form
'-------------------------------
Sub PersoonRecord_Show()
'-------------------------------
' PersoonRecord Show begin
'-------------------------------
  Dim sWhere : sWhere = ""
  Dim sFormTitle: sFormTitle = "Naam"
  Dim bPK : bPK = True

'-------------------------------
' Load primary key and form parameters
'-------------------------------
  if sPersoonRecordErr = "" then
    SetVar "Trn_fPRS_AFDELING_KEY", GetParam("fPRS_AFDELING_KEY")
    SetVar "Trn_fFAC_GROEP_KEY", GetParam("fFAC_GROEP_KEY")
    SetVar "Trn_fPRS_PERSLID_NAAM", GetParam("fPRS_PERSLID_NAAM")
    pPRS_PERSLID_KEY = GetParam("PRS_PERSLID_KEY")
    SetVar "PersoonRecordError", ""
  else
    SetVar "Trn_fPRS_AFDELING_KEY", GetParam("Trn_fPRS_AFDELING_KEY")
    SetVar "Trn_fFAC_GROEP_KEY", GetParam("Trn_fFAC_GROEP_KEY")
    SetVar "Trn_fPRS_PERSLID_NAAM", GetParam("Trn_fPRS_PERSLID_NAAM")
    pPRS_PERSLID_KEY = GetParam("PK_PRS_PERSLID_KEY")
    SetVar "sPersoonRecordErr", sPersoonRecordErr
    SetVar "FormTitle", sFormTitle
    Parse "PersoonRecordError", False
  end if
'-------------------------------

'-------------------------------
' Load all form fields

'-------------------------------

'-------------------------------
' Build WHERE statement

  if IsEmpty(pPRS_PERSLID_KEY) then bPK = False
  
  sWhere = sWhere & "PRS_PERSLID_KEY=" & ToSQL(pPRS_PERSLID_KEY, "Number")
  SetVar "PK_PRS_PERSLID_KEY", pPRS_PERSLID_KEY
'-------------------------------
'-------------------------------
' PersoonRecord Open Event begin
' PersoonRecord Open Event end
'-------------------------------

  SetVar "FormTitle", sFormTitle

'-------------------------------
' Build SQL statement and open recordset
'-------------------------------
  sSQL = "select * from FAC_V_WEBUSER where " & sWhere
  openrs rs, sSQL
  bIsUpdateMode = (bPK and not(sAction = "insert" and sForm = "PersoonRecord") and not rs.eof)
'-------------------------------

'-------------------------------
' Load all fields into variables from recordset or input parameters
'-------------------------------
  if bIsUpdateMode then
    fldPRS_AFDELING_KEY = GetValue(rs, "PRS_AFDELING_KEY")
    fldPRS_PERSLID_KEY = GetValue(rs, "PRS_PERSLID_KEY")
    fldPRS_PERSLID_NAAM = GetValue(rs, "PRS_PERSLID_NAAM")
    SetVar "PersoonRecordDelete", ""
    SetVar "PersoonRecordUpdate", ""
    SetVar "PersoonRecordInsert", ""
'-------------------------------
' PersoonRecord ShowEdit Event begin
' PersoonRecord ShowEdit Event end
'-------------------------------
  else
    SetVar "PersoonRecordEdit", ""
    SetVar "PersoonRecordInsert", ""
'-------------------------------
' PersoonRecord ShowInsert Event begin
' PersoonRecord ShowInsert Event end
'-------------------------------
  end if
  Parse "PersoonRecordCancel", false

'-------------------------------
' Set lookup fields
'-------------------------------
  fldPRS_AFDELING_KEY = DLookUp("PRS_V_AANWEZIGAFDELING", "PRS_AFDELING_OMSCHRIJVING", "PRS_AFDELING_KEY=" & ToSQL(fldPRS_AFDELING_KEY, "Number"))
  if sPersoonRecordErr = "" then
'-------------------------------
' PersoonRecord Show Event begin
fldPRS_PERSLID_NAAM = buildName(fldPRS_PERSLID_KEY)
' PersoonRecord Show Event end
'-------------------------------
  end if

'-------------------------------
' Show form field
'-------------------------------
      SetVar "PRS_PERSLID_KEY", ToHTML(fldPRS_PERSLID_KEY)
      SetVar "PRS_PERSLID_NAAM", ToHTML(fldPRS_PERSLID_NAAM)
      SetVar "PRS_AFDELING_KEY", ToHTML(fldPRS_AFDELING_KEY)
  Parse "FormPersoonRecord", False

'-------------------------------
' PersoonRecord Close Event begin
' PersoonRecord Close Event end
'-------------------------------

Set rs = Nothing

'-------------------------------
' PersoonRecord Show end
'-------------------------------
End Sub
'===============================
%>