 <%@language = "javascript" %>
<% /*
     $Revision$
     $Id$

     load outlet or wallplate kenmerken
*/ %>
<!-- #include file="../Shared/common.inc" -->

<%
re1 = '[' + (L("lcl_date_separator") == '-'
      ? L("lcl_date_separator")
      : '-' + L("lcl_date_separator")) + ']';
empty = "__" + re1 + "__" + re1 + "____";
%>

<html>
  <head>
    <LINK href="<%=custpath%>/cust.css" rel="stylesheet" type="text/css">
<script>
  var dateInputs = new Object();

  function owcheckDate(value)
  {
    if (/^<%=empty%>$/.test(value)) return true;
    var valid = true;
    var arr = /^(\d{1,2})<%=re1%>(\d{1,2})<%=re1%>(\d{1,4})$/.exec(value);
    if (arr)
    {
      var dt = new Date( arr[3], arr[2] -1, arr[1] );
      if ((arr[1] != dt.getDate()) ||
          (arr[2] != dt.getMonth() + 1) ||
          (arr[3] != dt.getYear() && arr[3] != dt.getFullYear())) {
        valid = false;
      }
    }
    else valid = false;
    return(valid);
  }

  function owkenmerkDateChange()
  {
    if (event.propertyName != 'value') return;

    var el = event.srcElement || event.target;
    var newT = el.value;
    var nm = el.name;
    var oldT = dateInputs[nm];
    if (oldT == null)
      oldT = '';
    var valid = false;

    if (newT == '')
      valid = true;
    else
      if (/^\d{2}$/.test(newT) || /^\d+[/-]\d{2}$/.test(newT))
      {
        el.value += '<%=L("lcl_date_separator")%>';
        valid = true;
      }
      else if (/^\d{1,2}(|<%=re1%>(|\d{1,2}(|<%=re1%>(|\d{1,4}))))$/.test(newT))
      {
        valid = true;
      }
    if (valid)
    {
      dateInputs[nm] = el.value;
    }
    else if (el.value != oldT)
      el.value = oldT;
  }

  function owkenmerkDateKey()
  {
    // Alleen cijfers, datumseperator of - toegestaan !
    var kc = event.keyCode;
    if (kc >= 48 && kc <= 57) return;
    if (String.fromCharCode(kc) == '<%=L("lcl_date_separator")%>' ||
        String.fromCharCode(kc) == '-') return;
    event.returnValue = false;
  }

</script>
  </head>
<%
  var sort = getQParamInt("sort");
  var loc = getQParamInt("loc", -1);
  var obj_key = getQParamInt("obj_key", -1);
  var reado = getQParamInt("reado", 0) == 1;
  var parent = getQParamInt("parent", -1);
  tablename = getQParam("tablename");
  start_row = getQParamInt("start_row");
  var serienr = getQParamInt("serienr");

  var val_seperator = '';
%>

<body>
<form name=u2>
  <input type='hidden' name='temp' value='0'>
<script><%
  var autfunction = "WEB_INSMAN";
  var alg_level_read;
  var alg_level_write;
  sql = " SELECT fd.fac_gebruiker_alg_level_read"
      + ", fd.fac_gebruiker_alg_level_write"
      + " FROM fac_v_my_disciplines fd"
      + " WHERE fd.ins_discipline_key = ("
      + "SELECT ins_discipline_key"
      + " FROM ins_tab_discipline"
      + " WHERE UPPER(ins_discipline_omschrijving) = UPPER ('LANbeheer'))"
      + " AND fd.prs_perslid_key = " + user_key
      + " AND fd.fac_functie_code = " + safe.quoted_sql(autfunction);
  oRs = Oracle.Execute( sql );
  if( !oRs.eof ) {
    alg_level_read = oRs(0).Value;
    alg_level_write = oRs(1).Value;
  }

  if  (!isNaN(obj_key) && !isNaN(alg_level_write) && alg_level_write>=0 && alg_level_read<alg_level_write) {
    sql = " SELECT id.ins_deel_key"
        + ", id.ins_deel_omschrijving"
        + ", id.ins_deel_parent_key"
        + " FROM ins_deel id"
        + " WHERE id.ins_srtdeel_key = " + sort
        + " AND id.ins_deel_parent_key IS NOT NULL"
        + " AND id.ins_deel_verwijder IS NULL"
        + " AND id.ins_deel_key = " + obj_key
        + " AND id.ins_alg_ruimte_key IN (SELECT alg_ruimte_key FROM fac_v_my_rooms"
     	  + " WHERE prs_perslid_key=" + user_key + " AND niveau=" + alg_level_write+")";
    oRs = Oracle.Execute( sql );
    if( oRs.eof ) {
      reado=true;
    }
  }

  sql = "SELECT ins_kenmerk_key"
      + " FROM ins_ol_kenmerk ok"
      + ", ins_srtkenmerk sk"
      + ", ins_kenmerk k"
      + " WHERE ok.ins_ol_kenmerk_key = sk.fac_kenmerkdomein_key"
      + " AND sk.ins_srtkenmerk_key = k.ins_srtkenmerk_key"
      + " AND ins_ol_kenmerk_naam = 'REF_POORT'";
  oRs = Oracle.Execute( sql );
  if( !oRs.eof ) {
    var kenmerk_key_port = oRs(0).Value;
  }
  else {
    Response.Write("<p>" + L("lcl_internal_error") + "</p>");
    Response.End();
  }

  if (loc > 0 && obj_key > 0) {
    sql = "SELECT " +
        + "ins.get_description('"+(isNaN(parent)? "LPN_INS_DEEL" : "LPN_INS_ONDERDEEL") + "',"
        + (parent > 0? parent : loc)  // scope for uniqueness could be location (unused) or parent
        + ", ins_srtdeel_code_upper)"
        + " FROM ins_srtdeel WHERE ins_srtdeel_key = " + sort;
    oRs = Oracle.Execute( sql );
    if( !oRs.eof ) { %>
if( parent )
if( parent.u2 )
if( parent.u2.desc )
//if( ! parent.u2.desc.value )
parent.u2.desc.value = '<%= safe.jsstring(oRs(0).Value) %>';
<%  }
    oRs.close();
  }
%>
var tab = parent.document.getElementById("<%=tablename%>");
var kdd=/^k\d+$/;
var tr,td=null;

for( i=tab.rows.length-1; i>=0; i-- ) {
  tr = tab.rows[i];
  if( kdd.test( tr.id ) ) {
    tr.parentNode.removeChild(tr);
  }
}

var txt = parent.document.getElementById("theText");
var rowIndex = <%=(start_row-2)%>; // not -1 because there is already 1 or more select/input/text-boxen
if( txt ) rowIndex = txt.rowIndex;

<%
    i=0; x=0;
    sql = "select k.INS_KENMERK_KEY,"                // 0

        + " CASE t.INS_SRTKENMERK_KENMERKTYPE"
        + " WHEN 'Q' THEN ''"
        + " ELSE t.ins_srtkenmerk_omschrijving END"    // 1

        +      ", t.INS_SRTKENMERK_KENMERKTYPE"     // 2
        +      ", r.FAC_KENMERKDOMEIN_OBJECTNAAM"   // 3
        +      ", r.FAC_KENMERKDOMEIN_KOLOMNAAM"    // 4
        +      ", r.FAC_KENMERKDOMEIN_KOLOMTXT"     // 5
        +      ", r.FAC_USRTAB_KEY"                 // 6

        +      ", t.INS_SRTKENMERK_LENGTE"          // 7
        +      ", t.INS_SRTKENMERK_DEC"             // 8
        +      ", t.INS_SRTKENMERK_NMIN"            // 9
        +      ", t.INS_SRTKENMERK_NMAX"            // 10
        +      ", k.INS_KENMERK_VERPLICHT"          // 11
        +      ", k.INS_KENMERK_UNIEK"              // 12

        + ','
        + ( !isNaN(obj_key) ? "(select v.INS_KENMERKDEEL_WAARDE"
           + " from INS_KENMERKDEEL v where v.INS_DEEL_KEY="+obj_key
           + " and v.INS_KENMERK_KEY=k.INS_KENMERK_KEY"
           + " and INS_KENMERKDEEL_VERWIJDER is null)"
        : "k.INS_KENMERK_DEFAULT" )                 // 13
        + ", k.INS_KENMERK_DEFAULT"                 // 14
        + ", t.INS_SRTKENMERK_DIMENSIE"             // 15
        + ", k.INS_KENMERK_GROEP"                   // 16

        +  " from INS_SRTKENMERK t"
        +       ",INS_KENMERK k"
        +       ",INS_SRTDEEL s"
        +       ",INS_SRTGROEP g"
        +       ",FAC_KENMERKDOMEIN r"
        + " where s.INS_SRTDEEL_KEY="+sort
        +   " and ((k.INS_SRTINSTALLATIE_KEY=s.INS_SRTDEEL_KEY"
        +     " and k.INS_KENMERK_NIVEAU='S')"
        +      " or(k.INS_SRTINSTALLATIE_KEY=s.INS_SRTGROEP_KEY"
        +     " and k.INS_KENMERK_NIVEAU='G')"
        +      " or(k.INS_SRTINSTALLATIE_KEY=g.INS_DISCIPLINE_KEY"
        +     " and k.INS_KENMERK_NIVEAU='D'))"
        +   " and k.INS_SRTKENMERK_KEY=t.INS_SRTKENMERK_KEY"
        +   " and g.INS_SRTGROEP_KEY=s.INS_SRTGROEP_KEY"
        +   " and k.INS_KENMERK_VERWIJDER IS NULL"
        +   " and t.INS_SRTKENMERK_VERWIJDER IS NULL"
        +   " and t.FAC_KENMERKDOMEIN_KEY = r.FAC_KENMERKDOMEIN_KEY(+)"
        +   " ORDER BY k.INS_kenmerk_volgnummer, t.INS_srtkenmerk_omschrijving"
        ;

  oRs = Oracle.Execute( sql );
  while( !oRs.eof ) {
    i++;
    var s0=-1;
    var s1=-1;

    val_seperator = ':&nbsp;';
    var val = null;
    if( !this.kenmerk_search )
      val = oRs(13).Value;

    if( val || !reado ) {

      var ktype = oRs(2).Value;
      var klen = oRs(7).Value; if( ! klen ) klen = 255;
      var kdec = oRs(8).Value;
      var kmin = oRs(9).Value;
      var kmax = oRs(10).Value;

      var kreq = oRs(11).Value;  // Mandatory
      var kuniq = oRs(12).Value; // Unique
      var def_val = oRs(13).value;
      if (def_val==null) def_val = "&nbsp;";
      var kdim    = oRs(15).Value;

      var val1 = safe.html(val);
      var kv = reado ? (ktype == 'X' ? (val? '<a href=\\"'+ S("image_path_virtual") +val1+'\\">'+val1+'</a>' :'') : ("<input type=text style='width:200px' value='"+ safe.htmlattr(val)+"' readonly "+"maxlength="+(ktype=='N'&&kdec?klen+1:klen)+">"))
             : ("<input type='"+(ktype == 'X' &&!val ? 'file' : 'text')+"' onChange=frames.hidFrameKenmerk" + serienr + ".checkK" + i + serienr + "(false) onBlur=frames.hidFrameKenmerk" + serienr + ".checkK" + i + serienr + "(true) name='k" + i + "val" + serienr + "'" + (val? " value='" + safe.htmlattr(val) + "'" : '')
             + " maxlength="+(ktype=='N'&&kdec?klen+1:klen)
             + (ktype=='D'?' onKeyPress=owkenmerkDateKey() onPropertyChange=owkenmerkDateChange()':'')
             + " style='width:" + (klen > 28? (klen > 40? "360" : "" + (klen * 100 / 14 + 10)) : "200") + "px'>")
             + ((val && ktype == 'X') ? "<input type=button class=mbutton style='width:20px;height:20px;background-image:url(../Pictures/fileopen.gif)' onClick='openDoc(\\\""+ safe.jsstring(S("image_path_virtual")) + val1 + "\\\");'>" : "");

      if( ktype == 'R' ) {

        var FAC_USRTAB_KEY = oRs(6).Value;
        var where = '';
        if( FAC_USRTAB_KEY && oRs(3).Value=='FAC_USRDATA') where = "FAC_USRTAB_KEY="+FAC_USRTAB_KEY;
        if( reado && oRs(3).Value!="INS_V_INS_POORTEN") where = (where?where+' and ':'')+oRs(4).Value+"="+val;
        sql = (oRs(3).Value!="INS_V_INS_POORTEN"?"select "+oRs(4).Value+", " + oRs(5).Value:"select distinct ins_deel_hub_key, ins_deel_hub")
            + " from "+oRs(3).Value
            + ( where ? (" where " + where
            + " and fac_usrdata_verwijder IS NULL")
            : "" )
            //+ (oRs(3).Value=="PRS_V_AANWEZIGAFDELING"?(!S("prs_dep_all")?" WHERE niveau="+S("prs_max_dep_level"):""):"")
            //+ (oRs(3).Value=="INS_V_INS_POORTEN"?" group by ins_deel_hub_key, ins_deel_hub":"")
            + ((oRs(3).Value == "FAC_USRDATA") ? " order by fac_usrdata_volgnr, 2":" order by 2")
            ;
        oRs2 = Oracle.Execute( sql );
        kv = '';

        var builder = new Array();
        c0=oRs2(0); // for performance this is better, it seems (-JGL).
        c1=oRs2(1);

        if (!isNaN(val) && oRs(3).Value=="INS_V_INS_POORTEN") {
          sql = "SELECT ins_deel_hub_key"
              + ", ins_deel_poort"
              + " FROM ins_v_ins_poorten"
              + " WHERE ins_deel_key = " + val;
          oRs3 = Oracle.Execute( sql );
          if (!oRs3.eof) {
            s0=oRs3(0).Value;
            s1=oRs3(1).Value;
          }
        }

        while( !oRs2.eof ) {
          if( reado )
            kv = "<input type=text readonly style='width:200px' value='"+ safe.htmlattr( c1.Value ) + "'>";
          else
          {
              appender = '<option value="'+c0.Value+'"'
                            + ((c0.Value==val || (!isNaN(val) && oRs(3).Value=="INS_V_INS_POORTEN" && s0==c0.Value))?' selected':'')+'>'+ safe.html( c1.Value ) + '</option>';
              builder[builder.length]=appender; // push at end of array
          }
          oRs2.MoveNext();
        }
        if (!reado) kv = builder.join(' ');
        oRs2.close();

        if( !reado )
          kv = "<select name='k"+i+"val" + serienr + (oRs(3).Value=="INS_V_INS_POORTEN"?"part":"") + "' style='width:" + (oRs(3).Value=="INS_V_INS_POORTEN"?"120px'":"200px'") + " onFocus=this.s_str='' onKeyPress='return false;' onKeyUp='autoComplete(event,this);'" + (oRs(3).Value=="INS_V_INS_POORTEN"?" onChange=updatePort('k"+i+"val" + serienr + "');":"") + "><option value=''></option>"+kv+"</select>"
             + (oRs(3).Value=="INS_V_INS_POORTEN"?"<select name='k"+i+"val" + serienr + "' style='width:80px' onFocus=this.s_str='' onKeyPress='return false;' onKeyUp='autoComplete(this);'" + (oRs(3).Value=="INS_V_INS_POORTEN"?" onChange=updateCon('k"+i+"val" + serienr + "');":"") + "><option value='' ex_value='-1'></option></select><input type='hidden' name='k"+i+"val" + serienr + "con' value='0'> ":"")
      }
      if( klen > 50 && ktype == 'C' ) {
          kv = "<textarea rows=3 cols=55 "
               + (reado ? " readonly ":(" onChange=frames.hidFrameKenmerk"+serienr+".checkK"+i+serienr+"(false) onBlur=frames.hidFrameKenmerk"+serienr+".checkK"+i+serienr+"(true) name='k"+i+"val" + serienr + "'"))
             + ">"
             + safe.textarea(val)+"</textarea>";
      }
      if( ktype == 'L' ) {
        kv = "<p align='bottom' class='disabled'><b id=k" + i + "l>" + def_val + "</b></p>";
      }

      if( ktype == 'Q' ) {
        kv = "";
        val_seperator = '';
      }

      // kv = "FLEXPROP VALUE";

/*if( ++x & 1 )*/ { %>
tr = tab.insertRow(++rowIndex);
tr.id = 'k<%=i%>';
<% } %>
td = tr.insertCell(-1);


<%
if (kenmerk_key_port == oRs(0).value) { %>
  if( parent )
  if( parent.u2 )
  if( parent.u2.portid )
  parent.u2.portid.value = 'k<%=i%>val<%=serienr%>';

<%
}

if( (klen > 50 && ktype == 'C') || ( ktype == 'L') || ( ktype == 'Q')) {
%>
    td.align='left';
    td.innerHTML = "<nobr><b id=k<%=i%>l<%=serienr%>><%=((ktype == 'Q')?"":oRs(1).Value)+(kdim!=null?' ['+kdim+']':'') + val_seperator%></b></nobr><input type=hidden name='k<%=i%>key<%=serienr%>' value='<%=oRs(0)%>'><%= kreq ? "<input type=hidden name=k"+i+"req" + serienr + " value=1>" :'' %>"
                   + "<br /><div  align=<%=ktype == 'L'?'left':'right'%>><%=safe.jsstring(kv)%></div>";
    td.colSpan = 2;
<%
} else {
%>
    td.align='right';
    td.innerHTML = "<nobr><b id=k<%=i%>l<%=serienr%>><%=oRs(1).value+(kdim!=null?' ['+kdim+']':'') + val_seperator%></b></nobr><input type=hidden name='k<%=i%>key<%=serienr%>' value='<%=oRs(0)%>'><%= kreq ? "<input type=hidden name=k"+i+"req" + serienr + " value=1>" :'' %>";
    td = tr.insertCell(-1);
    td.innerHTML = "<%=safe.jsstring(kv)%>";
<%}%>
<% if( !reado ) { %>
function checkK<%=i%><%=serienr%>(show){
  var valid = true;
  var field = parent.u2.k<%=i%>val<%=serienr%>;
  var value = field.value;
  var err = "<%=L("lcl_mld_invalid_kenmerk_format")%>";
  if( value != '' ) {

<% if( ktype == 'D' ) { %>
    valid = parent.owcheckDate( value );
    if( ! valid ) err = "<%=L("lcl_mld_kenmerk_out_range")%>";
<% } else if( (ktype == 'C') || (ktype == 'L') || (ktype == 'Q')) { %>
    if( value.length > <%=klen%> ) valid = false;
<% } else if( ktype == 'N' ) {
     if( kdec ) {
       l1 = klen-kdec; l2 = kdec;
       s = '/^(\\d{1,'+l1+'}[.,]?|\\d{0,'+l1+'}[.,]\\d{1,'+l2+'})$/';
     } else {
       s = '/^\\d{1,'+klen+'}[.,]?$/';
     } %>
    valid = value.match(<%=s%>) && valid;
<%
    if( kmin != null ) { %> if( value < <%=kmin%> ) { valid = false; err = "<%=L("lcl_mld_kenmerk_out_range")%>"; } <% }
    if( kmax != null ) { %> if( value > <%=kmax%> ) { valid = false; err = "<%=L("lcl_mld_kenmerk_out_range")%>"; } <% }
  }
%>
  }
  if( show && ! valid ) {
    alert(err);
    field.focus();
  }
  parent.u2.action = valid ? 'outlet.asp' : 'javascript:0';
  return valid;
}
<% } // ?? if( !reado )
    } // if( val || !reado )
    if (!isNaN(val) && oRs(3).Value=="INS_V_INS_POORTEN") {
      %>parent.updatePort('k<%=i%>val<%=serienr%>', '<%=s1%>')<%
    }
    oRs.MoveNext();
  } // while( !oRs.eof )
  oRs.close();
%>

window.parent.changeButtons();

</script>
</form>
<%

Response.Write("load_kenmerk.asp done" + obj_key);
%>
</body>
</html>
