/**
 * Neuron Frame Work
 * for Neuron Soft application
 * www.neuron.com.pl
 */


'use strict';


//****************************************************************************
//  DIALOGI
//****************************************************************************

///tools
var kit_d_id_count=1;


function _d_get_uid(){
    kit_d_id_count+=1 ;
    return 'kdialog'+kit_d_id_count+'_';
}

function _create_overlay(oid,zid){
    var nid='koverlay';
    if (oid!=undefined){nid=nid+oid }
    var overlay = jQuery('<div class="koverlay ribbed-steel" id='+nid+'> </div>');
    overlay.appendTo(document.body);
    overlay.css( { 'z-index':zid } );
    return overlay;
}

function _create_overlay2(oid,zid,ado){
    var nid='koverlay';
    if (oid!=undefined){nid=nid+oid }
    var overlay = jQuery('<div class="koverlay ribbed-steel" id='+nid+'> </div>');
    overlay.appendTo(ado);
    overlay.css( { 'z-index':zid } );
    return overlay;
}

function _showAnim(obj,a){
    switch(a){
        case 0:   obj.show();     break;
        case 1:   obj.show('slide',{},100);   break;
        case 2:   obj.show('bounce',{},500);  break;
        case 3:   obj.show('fade',{},350);    break;
    }
}

function Kit_dialog_constructor(w, h, btn_mode, animation, modal, top) {

    /*
     btn_mode 0- bez przyciskow; 1-ok(close); 2- tak/nie 3 - ok/anuluj, 4- cancel
     animation 0- bez animacji; 1 - slide left 2- explode/close

     */
    this.drag=false;

    this.btn_ok=lang[2];
    this.btn_no=lang[1];
    this.btn_yes=lang[0];
    this.btn_cancel=lang[3];
    this.header_ico='mdi-crop-free';

    this.btn_call_close='';
    this.btn_call_ok='';
    this.btn_close_after=true;
    this.load_indicator_use=false;

    var that = this;
    this.uid=_d_get_uid();
    this.msg_box='';  // kontener
    this.div_content='';
    this.div_content_id='';
    //
    this.width=w;
    this.height=h;
    this.hheight=0;
    this._th='_'+nApp.cfgi.theme_style;
    this._sb='';

    this.sall=function(hname,context,scroll){
        this.draw();
        this.addHeader(hname);
        this.content(context,scroll  );
        this.showDialog(0);
    };

    this.sall_call=function(hname,context,scroll,callok){
        this.btn_close_after=false;
        this.btn_call_ok = callok;
        this.btn_call_close=function(){that.aFree()};
        this.draw();
        this.addHeader(hname);
        this.content(context,scroll  );
        this.showDialog(0);
    };




    this.content=function(ct,ascroll){
        if(ascroll) {
            var sb = ct;
            var ch =h-(70+this.hheight);
            sb = ' <div id="' + nohash(this.div_content_id) + '_sb" style="height: ' + ch + 'px"  >' + sb + '</div>';
            this.div_content.html(sb);
            this._sb = MakeScrollBox(this.div_content_id + '_sb');
            this._usesb=true;
        }else{  this.div_content.html(ct)  }
    };
    this.showDialog= function(){
        _showAnim(this.msg_box, animation );
    };

    this.addHeader= function(aCaption){
        var hid=this.uid+'header';
        var hidb=this.uid+'header_x';
        var ico = htmlts.ico('',this.header_ico)+' ';
        var tt='';
        if (nApp.cfgi.theme_style==2){ tt='rotating'   }
        var hdx='<div id= "'+hidb+'"  class=" mdi i24 mdi-window-close pointer kit_status_bar_btn '+tt+'" style="display: inline; float: right" ></div>';
        this.msg_box.prepend('<div id= "'+hid+'" >'+ ico + aCaption+hdx+'</div>');

        this.div_header= $('#'+hid);
        this.div_header.addClass('kdialog_header').addClass('kdialog_header'+this._th);
        BindClick('#'+hidb,function(){  that._clbutton()} );
        this.hheight=30;
    };

    this.draw=function(){
        if(h=='max'){  h=window.innerHeight-50  }

        if ( kit_d_id_count >5) { kit_note_ex('zbyt wiele okien','okien na stercie:'+kit_d_id_count,5,1 )    }
        if (  this.width=='max' ){this.width = nApp.cfgi.dialog_max_width }

        this.msg_box = kf_add_before( 'body', this.uid+'mpanel','','');
        this.msg_box.css( { 'z-index':20000+kit_d_id_count, width:this.width, height:h }).addClass('kdialog_body'+this._th).hide();
        if (this.drag){ this.msg_box.draggable( ) }
        if (top){  this.msg_box.centerTop(33)  } else { this.msg_box.center()  }
        if (modal){    this.overlay=_create_overlay(kit_d_id_count, 20000+kit_d_id_count -1);       }

        var s='';
        var id=this.uid;
        s+='<div id="'+id+'box" class="p10 "  >';
        s+='<div id="'+id+'content"  style="position: relative" >?????</div>';
        s+='<div id="'+id+'buttonpane"  class="kdialog_buttonp" ></div> ';
        s+='</div>';
        this.msg_box.append(s);
        this.div_content= $('#'+id+'content');
        this.div_content_id='#'+id+'content';
        this.div_bpane= $('#'+id+'buttonpane');

        //przyciski

        s='';
        if ( btn_mode==1){
            s= '<button  id="'+ this.uid +'bcl"  class="btn btn-dlg btn-green"    >'+this.btn_ok +'</button>';
        }else
        if ( btn_mode==2){
            s= '<button  id="'+ this.uid +'bok"  class="btn btn-dlg btn-green"    >'+this.btn_yes+'</button>  <button  id="'+ this.uid +'bcl"  class="btn btn-dlg btn-red"    >'+this.btn_no+'</button> ';
        }else
        if ( btn_mode==3) {
            s= '<button  id="'+ this.uid +'bok"  class="btn btn-dlg btn-green"    >'+this.btn_ok+'</button>  <button  id="'+ this.uid +'bcl"  class="btn btn-dlg btn-red"    >'+this.btn_cancel+'</button> ';
        }
        else
        if ( btn_mode==4) {
            s= '<button  id="'+ this.uid +'bcl"  class="btn btn-dlg btn-red"    >'+this.btn_cancel+'</button>  ';
        }
        if (this.load_indicator_use){ s+='<div id="'+ this.uid +'loadind"  style="display: inline" >LOAD ...</div>' }

        this.div_bpane.html(s);
        BindClick('#'+this.uid+'bcl',function(){  that._clbutton()} );
        BindClick('#'+this.uid+'bok',function(){  that._okbutton()} );

    };

    this.addButtonP=function( Caption,cthClass,mrg,  aCall){

        var sp= '<button  id="'+ this.uid +'bpn"  class="btn btn-dlg '+cthClass+'"  style="margin-left: '+mrg+'px"   >'+Caption+'</button>';
        this.div_bpane.append(sp) ;
        BindClick('#'+this.uid+'bpn',aCall);
    };

    this.load_indicator=function(state){ $('#'+this.uid+'loadind').toggle(state)  };
    this._clbutton=function(){  if(that.btn_close_after){    that.aFree() }  is_callback( that.btn_call_close )  };
    this._okbutton=function(){  if(that.btn_close_after){    that.aFree() }  is_callback( that.btn_call_ok )   };
    this.content_height=function(){ return h-(70+this.hheight)   };
    this.aFree=function(){
        kit_d_id_count-=1 ;
        function _killme(){
            that.msg_box.detach();
            if (modal ==true){  that.overlay.detach()  }
            $("body").unbind("keyup.myDialog"+that.uid);

        }

        if (animation==1) {
            this.msg_box.hide('slide', {}, 100, function () {
                _killme();
            });

        } else {  _killme()  }
    };
}


function Kit_dialog_form_constructor(animation,modal,caption) {
    var that = this;
    this.uid=_d_get_uid();
    this.msg_box='';  // kontener
    this.div_content='';
    this.div_content_id='';
    this._th='_'+nApp.cfgi.theme_style;

    this.header_ico='mdi-crop-free';
    this.btn_call_close='';
    this.btn_call_ok='';
    this.btn_close_after=true;
    this.overlay='';
    this._sb='';

    this.draw=function(){
        if ( kit_d_id_count >5) { kit_note_ex('zbyt wiele okien','okien na stercie:'+kit_d_id_count,5,1 )    }
        if (  this.width==0 ){this.width = nApp.cfgi.dialog_max_width }

        this.msg_box = kf_add_before( '#kit_view_content', this.uid+'mpanel','','');
        this.msg_box.css( { 'z-index':20000+kit_d_id_count, width: nApp.cfgi.view_width , height:400 }).addClass('kdialog_body'+this._th).hide();
        if (modal){    this.overlay=_create_overlay(kit_d_id_count, 20000+kit_d_id_count -1);       }
        // if (top){  this.msg_box.centerTop(50)  } else { this.msg_box.center()  }
        this.msg_box.css("top", Math.max(0, 0 + $(window).scrollTop()) + "px");
        this.msg_box.css("height", + $(window).height()-60 + "px");
        var s='';
        var id=this.uid;
        s+='<div id="'+id+'box" class="p10"  >';
        s+='<div id="'+id+'content"  style="position: relative" >?????</div>';

        s+='</div>';
        this.msg_box.append(s);
        //this.div_content= $('#'+id+'content');
        //this.div_content_id='#'+id+'content';
        //this.div_bpane= $('#'+id+'buttonpane');

        this.div_content= $('#'+id+'content');
        this.div_content_id='#'+id+'content';
        var hid=this.uid+'header';
        var hidb=this.uid+'header_x';
        var hidr=this.uid+'header_r';
        var hidh=this.uid+'header_h';
        var ico = htmlts.ico('',this.header_ico)+' ';
        var tt='';
        if (nApp.cfgi.theme_style==2){ tt='rotating'   }
        var hdx='<div id= "'+hidb+'"  class=" mdi i24 mdi-window-close pointer kit_status_bar_btn '+tt+'" style="display: inline; float: right" ></div>';
        var rbtn='<button id="'+hidr+'" class="btn btn-red" style="height: 32px; width:50px; margin-right: 10px; padding:0; padding-left: 15px; margin-left-10px " ><i class="mdi i32 mdi-arrow-left-bold-hexagon-outline "></i></button>';
        this.msg_box.prepend('<div id= "'+hid+'" >'+ rbtn + '<div id="'+hidh+'"  style="display: inline;vertical-align: bottom">'+caption+'</div>'+hdx+'</div>') ;

        this.div_header= $('#'+hid);
        this.div_header.addClass('kdialog_header').addClass('kdialog_header'+this._th).css('width', nApp.cfgi.view_width );
        BindClick('#'+hidb,function(){  that._clbutton()} );
        BindClick('#'+hidr,function(){  that._clbutton()} );
        this.hheight=30;

    };
    this.head_caption=function(cap){ $('#'+this.uid+'header_h').text(cap)  };
    this.sbr=function(){this._sb.refresh() };
    this.content=function(ct,ascroll){

        if(ascroll) {
            var sb = ct;
            var ch = nApp.cfgi.content_height -60  ;
            sb = ' <div id="' + nohash(this.div_content_id) + '_sb" style="height: ' + ch + 'px"  >' + sb + '</div>';
            this.div_content.html(sb);
            this._sb = MakeScrollBox(this.div_content_id + '_sb');
            this._usesb=true;
        }else{  this.div_content.html(ct)  }

    };
    this.showDialog= function(){
        _showAnim(this.msg_box, animation );

    };
    this._clbutton=function(){  if(that.btn_close_after){    that.aFree() }  is_callback( that.btn_call_close )  };

    this.aFree=function(){
        kit_d_id_count-=1 ;
        function _killme(){
            that.msg_box.detach();
            if (modal ==true){  that.overlay.detach()  }
            $("body").unbind("keyup.myDialog"+that.uid);


        }

        if (animation==1) {
            this.msg_box.hide('slide', {}, 100, function () {
                _killme();

            });

        } else {  _killme();  }
    };
}


function kit_dialog_calc(mode,start,aTitle, callback) {
    /// mode 0 - kalkulator integer
    /// mode 1 - kalkulator float
    function __dclose() {    dm.aFree();   is_callback(callback(start, false))  }
    function __dok() {
        var ov = $("#clc_input").val();
        if (IsNumeric(ov)){
            is_callback(callback(ov, true));
            dm.aFree();
        }
    }

    var temp = ""; var store = []; var answer = "";
    var dm = new  Kit_dialog_constructor(270, 480,3,0,true,false) ;
    dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.btn_close_after=false;
    dm.drag=true;
    dm.header_ico='mdi-calculator';
    dm.draw();
    dm.addHeader(aTitle);

    var sx = '';
    sx+='<div class="calculator"  style="width: 270px"  >';
    sx+='<input class="calc_screen" id="clc_input" value="" readonly  >';
    sx += '<button class="btn btn-calc btn-red"   >C</button>';
    sx += '<button class="btn btn-calc btn-teal"  >B</button>';
    sx += '<button class="btn btn-calc btn-orange"  >±</button>';
    sx += '<button class="btn btn-calc btn-orange"  >÷</button>';

    sx += '<button class="btn btn-calc btn-blue"  >7</button>';
    sx += '<button class="btn btn-calc btn-blue"  >8</button>';
    sx += '<button class="btn btn-calc btn-blue"  >9</button>';
    sx += '<button class="btn btn-calc btn-orange"  >*</button>';

    sx += '<button class="btn btn-calc btn-blue"  >4</button>';
    sx += '<button class="btn btn-calc btn-blue"  >5</button>';
    sx += '<button class="btn btn-calc btn-blue"  >6</button>';
    sx += '<button class="btn btn-calc btn-orange"  >-</button>';


    sx += '<button class="btn btn-calc btn-blue"  >1</button>';
    sx += '<button class="btn btn-calc btn-blue"  >2</button>';
    sx += '<button class="btn btn-calc btn-blue"  >3</button>';
    sx += '<button class="btn btn-calc btn-orange"  >+</button>';

    sx += '<button class="btn btn-calc btn-blue"  >0</button>';
    if (mode == 1) sx += '<button class="btn btn-calc btn-blue"  >.</button>';
    sx+='<button class="btn btn-calc btn-green" style="width: 122px" >=</button>';

    sx+='</div>';

    dm.content(sx,false );
    dm.showDialog();

    if ( start!=0 ) {
        temp = start;
        answer = start;
        $("#clc_input").val(start);
    }

    $('.btn-calc').on('click touchstart', function(e){
        e.stopPropagation(); e.preventDefault();

        function _disp(vx){  $("#clc_input").val(vx); }

        var val = $(this).text();

        if (!isNaN(val) || val === ".") {  temp += val;   }
        if (val === "C") { temp = "";  store = [];        }
        if (val === "B") {  temp = temp.slice(0 , -1);    }
        if (val === "±") {  temp = -temp;    }
        _disp(temp);
        if (val) {
            // Select operators and push them to array
            switch (val) {
                case "÷":
                    store.push(temp);
                    store.push("/");
                    _disp(val);
                    temp = "";
                    break;
                case "*":
                    store.push(temp);
                    store.push("*");
                    _disp(val);
                    temp = "";
                    break;
                case "-":
                    store.push(temp);
                    store.push("-");
                    _disp(val);
                    temp = "";
                    break;
                case "+":
                    store.push(temp);
                    store.push("+");
                    _disp(val);
                    temp = "";
                    break;
            }
        }

        if (val === "=") {
            if (IsNumeric(temp)){
                store.push(temp);
                store = store.join("");
                answer = eval(store);
                var fx=0; if (mode==1){fx=2}
                _disp(answer.toFixed(fx));
                temp = "";
                store = [];
                store.push(answer);
                answer = "";
            }
        }
    })
}



function kit_dialog_pin(aTitle, callback) {

    function __dclose() { dm.div_content .parents().removeClass ('kdialog_body_l');   dm.aFree();
        is_callback(callback(-1, false))

    }

    function __dok() {
        var ov = $("#clc_input").val();
        if (IsNumeric(ov)){
            is_callback(callback(ov, true));
            dm.div_content .parents().removeClass ('kdialog_body_l');
            dm.aFree();
        }
    }

    var temp = ""; var store = []; var answer = "";
    var dm = new  Kit_dialog_constructor(270, 420,3,0,true,false) ;
    dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.btn_close_after=false;
    dm.drag=true;
    dm.header_ico='mdi-calculator';
    dm.draw();
    dm.addHeader(aTitle);


    var sx = '';
    sx+='<div class="calculator"  style="width: 250px;"  >';
    sx+='<div class="mdi i48 mdi-lock-open-outline" style="display: inline"></div> <input class="calc_screen" id="clc_input" style="width: 188px" value="" readonly><br>';


    sx += '<button class="btn btn-pin btn-black"  >7</button>';
    sx += '<button class="btn btn-pin btn-black"  >8</button>';
    sx += '<button class="btn btn-pin btn-black"  >9</button>';


    sx += '<button class="btn btn-pin btn-black"  >4</button>';
    sx += '<button class="btn btn-pin btn-black"  >5</button>';
    sx += '<button class="btn btn-pin btn-black"  >6</button>';



    sx += '<button class="btn btn-pin btn-black"  >1</button>';
    sx += '<button class="btn btn-pin btn-black"  >2</button>';
    sx += '<button class="btn btn-pin btn-black"  >3</button>';
    sx += '<button class="btn btn-pin btn-black" style="width: 143px"   >0</button>';
    sx += '<button class="btn btn-pin btn-red"  style="margin-left: 20px"  >B</button>';


    sx+='</div>';

    dm.content(sx);

    dm.showDialog();
    dm.div_content .parents().addClass('kdialog_body_l');

    $('.btn-pin').on('click touchstart', function(e){
        e.stopPropagation(); e.preventDefault();

        function _disp(vx){  $("#clc_input").val(vx); }

        var val = $(this).text();

        if (!isNaN(val) || val === ".") {  temp += val;   }
        if (val === "C") {
            temp = "";
            store = [];
        }
        if (val === "B") {  temp = temp.slice(0 , -1);    }
        if (val === "±") {  temp = -temp;    }
        _disp(temp);
    })
}


function kit_dialog_password(aTitle,callback){

    function __dclose() {  dm.aFree()   }
    function __dok(){
        var ps=$( "#kdpass1234").val();
        if(ps!=''){  is_callback( callback( ps )  ) }
        dm.aFree();
    }

    var dm = new Kit_dialog_constructor(250,150,3,0,true,true);
    dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.btn_close_after=false;
    dm.draw();
    dm.addHeader(aTitle );
    var sx='<input type="password"  id="kdpass1234" style="width: 220px" > ';
    dm.content(sx,false);

    dm.showDialog();


    $( "#kdpass1234").addClass('inputtxt').attr('autocomplete', 'off') .focus().on( 'keyup' ,     function(e){
        if(e.keyCode === 13){
            e.preventDefault();
            __dok();
        } } );

}


function kit_dialog_passwordEx2(pin,aTitle,callback){
    function ab(a,b){ if (b) {
        if (a==0){  kit_dialog_password(lang[19],callback) } else {  callback(a) }
    } }

    if (pin){
        kit_dialog_pin(lang[17] , function (a,b  ){ ab(a,b)   }    );
    }else{
        kit_dialog_password(aTitle,callback)
    }
}

function kit_dialog_alarm(aWidth,aHeight,aTitle, aBody , aCall, bName){
    function __dclose() {
        dm.aFree();
        is_callback(aCall);
    }

    if (bName==undefined ){ bName="OK"  }
    var dm = new Kit_dialog_constructor(aWidth,aHeight,1,0,true);
    dm.c_ok=bName;
    //dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.header_ico='mdi-bell-ring';
    dm.draw();
    dm.addHeader( aTitle);

    dm.content(aBody,false);

    dm.showDialog();
    dm.div_header .addClass('blink_mef')
}


function kit_dialog_yes_no(title,body,cally,calln){
    function __dclose() {
        dm.aFree();
        is_callback(calln);
    }
    function __dok() {
        dm.aFree();
        is_callback(cally);
    }
    var dm = new Kit_dialog_constructor( 400, 200  ,2,0,true,false);
    dm.draw();
    dm.addHeader(title);
    dm.btn_close_after=false;
    dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.content(body,false  );
    dm.showDialog(0);

}

function kit_dialog_yes_noPH(title,body,cally,calln){
    function __dclose() {
        dm.aFree();
        is_callback(calln);
    }
    function __dok() {
        dm.aFree();
        is_callback(cally);
    }
    var dm = new Kit_dialog_constructor( 330, 300  ,2,0,true,true);
    dm.draw();
    dm.addHeader(title);
    dm.btn_close_after=false;
    dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.content(body,false  );
    dm.showDialog(0);

}


function kit_dialog_note(title,body){

    var dm = new Kit_dialog_constructor( 330, 350  ,1,0,true,true);
    dm.draw();
    dm.addHeader(title);
    dm.content(body,true  );
    dm.showDialog(0);

}
function kit_dialog_noteb(title,body){

    var dm = new Kit_dialog_constructor( 500, 500  ,1,0,true,true);
    dm.draw();
    dm.addHeader(title);
    dm.content(body,true  );
    dm.showDialog(0);

}
function kit_dialog_enote(title,body){

    var dm = new Kit_dialog_constructor( 330, 350  ,0,0,true,true);
    dm.btn_close_after=false;
    dm.draw();
    dm.addHeader(title);
    dm.content(body,false  );
    dm.showDialog(0);

}

function kit_dialog_get_txt(aTitle,callback){

    function __dclose() {  dm.aFree()   }
    function __dok(){
        var ps=$( "#kdgtxt1234").val();
        dm.aFree();
        if(ps!=''){  is_callback( callback( ps )  ) }

    }

    var dm = new Kit_dialog_constructor(450,150,3,0,true,true);
    dm.btn_call_ok =__dok;
    dm.btn_call_close=__dclose;
    dm.btn_close_after=false;
    dm.draw();
    dm.addHeader(aTitle );
    var sx='<input type=""  id="kdgtxt1234" style="width: 420px" > ';
    dm.content(sx,false);

    dm.showDialog();


    $( "#kdgtxt1234").addClass('inputtxt').attr('autocomplete', 'off') .focus().on( 'keyup' ,     function(e){
        if(e.keyCode === 13){
            e.preventDefault();
            __dok();
        } } );

}


//****************************************************************************
//  form constructor
//****************************************************************************

function Kit_spin_box_constructor(base_d,label,start){
    var that=this;
    this.spin=[];
    this.width=50;
    this.min=1;
    this.max=5000;
    this.label_width=0;
    this.make=function(){
        var s='<table><tr >';
        s+='<td class="kit_form_label" style="min-width: '+that.label_width +'px">'+label +'</td><td style="width:10px"></td>';
        s+='<td>'+htmlts.spinner( 'spx'   )+'</td>';
        s+='<td><button class="btn btn-blue " style="display: inline-table;" id="spincalc"   >CALC</button></td>'
        s+='</tr></table>'
      $(base_d).html(s);

        this.spin=htmlcmp.spinner( '#spx', start , this.min , this.max).width(this.width )

        $('#spincalc').on('click touchstart',function(e){
            kit_dialog_calc(0,0,'', function( v ){
                that.spin.val(v)

            })
        });
    };
    this.val=function(){ return this.spin.val() }

}


function Kit_form_constructor(base_id){
    var that=this;
    this.label_width=150;   // szerokość kolumny label
    this.c_mode=[];         // 0-notuse; 1-spin; 2-check; 3-text 4-combo 5-text ronly6-memo
    this.c_id=[];           // ID obiektu:  base_id+numer
    this.c_label=[];        // nazwa
    this.c_field=[];        // rezerwa
    this.c_value_i=[];      // wartość integer dla obiektów spin, combo  na starcie wartosc domyslna potem wynik
    this.c_spmin=[];        // spin min
    this.c_spmax=[];        // spin max
    this.c_value_b=[];      // wartość boolean dla check
    this.ob_spinner=[];     // obiekt spin
    this.c_value_txt=[];    // wartość pola text
    this.c_value_txt_l=[];  // długość pola txt
    this.c_memo_c=[];
    this.c_memo_r=[];
    //this.c_combo=[];
    this.c_options_array=[];// tablica options dla combobox
    this.json='';

    this.addSpin=function(nr,caption,value,min,max){
        this.c_mode[nr]=1;
        this.c_label[nr]=caption;
        this.c_value_i[nr]=value;
        this.c_spmin[nr]=min;
        this.c_spmax[nr]=max;
    };
    this.addCheck=function(nr,caption,state){
        this.c_mode[nr]=2;
        this.c_label[nr]=caption;
        this.c_value_b[nr]=state;
    };

    this.addText=function(nr,caption,l){

        this.c_mode[nr]=3;
        this.c_label[nr]=caption;
        this.c_value_txt_l[nr]=l;

    };
    this.addCombo=function(nr,caption,options,value){
        this.c_mode[nr]=4;
        this.c_label[nr]=caption;
        this.c_options_array[nr]=options;
        this.c_value_i[nr]=value;
    };
    this.addTextRO=function(nr,caption,txt){
        this.c_mode[nr]=5;
        this.c_label[nr]=caption;
        this.c_value_txt[nr]=txt;
    };
    this.addMemo=function(nr,caption,txt,c,r){

        this.c_mode[nr]=6;
        this.c_label[nr]=caption;
        this.c_value_txt[nr]=txt;
        this.c_memo_c[nr]=c;
        this.c_memo_r[nr]=r;
    };

    this.setCheck=function (nr,state) {
        that.c_value_b[nr] = state;
        htmlcmp.setCheck('#' + this.c_id[nr], state)
    };

    this.htmlString=function(){
        function tdl(nr){  return '<tr ><td class="kit_form_label" style="max-width: '+that.label_width +'px">'+that.c_label[nr] +'</td><td style="width:10px"></td>'     }

        var s='<table style="border-spacing:0 6px;" ><tbody>';
        for (var i = 0; i < 16; i++) {

            var sbtn=htmlts.btn_inline(11,30,30,'...','btn btn-blue');
            sbtn='';
            if (this.c_mode[i] == 1 ){  s+= tdl(i)+'<td>'+htmlts.spinner( this.c_id[i]   )+sbtn+ '</td></tr>' }

            if (this.c_mode[i] == 2 ){  s+= tdl(i)+'<td>'+htmlts.check(this.c_id[i],this.c_value_b[i]  ) +'</td></tr>' ;   }

            if (this.c_mode[i] == 3 ){  s+= tdl(i)+'<td>'+htmlts.input(this.c_id[i],this.c_value_txt_l[i],'kit_form_input') +'</td></tr>' }

            if (this.c_mode[i] == 4 ){  s+= tdl(i)+'<td>'+htmlts.select( this.c_id[i] ,  htmlts.options_l(  this.c_options_array[i]    )   )+'</td></tr>' }

            if (this.c_mode[i] == 5 ){  s+= tdl(i)+'<td class="kit_form_label" style="text-align: left; min-height: 28px">'+this.c_value_txt[i]+'</td></tr>' }

            if (this.c_mode[i] == 6 ){  s+= tdl(i)+'<td><textarea id="'+this.c_id[i] +'" cols="'+this.c_memo_c[i] +'" rows="'+this.c_memo_r[i] +'">'+this.c_value_txt[i]+'</textarea>  </td></tr>' }

        } //eloop

        return s+'</tbody></table>';
    };
    this.activate=function(){
        for (var i = 0; i < 16; i++) {
            if (this.c_mode[i] == 1 ){  this.ob_spinner[i]=  htmlcmp.spinner('#'+this.c_id[i], this.c_value_i[i]  , this.c_spmin[i] , this.c_spmax [i]  )}  //end spin
            if (this.c_mode[i] == 3 ){  $('#'+this.c_id[i]).val( this.c_value_txt[i]  )   }
            if (this.c_mode[i] == 4 ){  htmlcmp.setCombo( '#'+this.c_id[i], this.c_value_i[i]  )    }  //end spin


        } //eloop

    };
    this.getval=function(){
        for (var i = 0; i < 16; i++) {
            if (this.c_mode[i] == 1 ){  this.c_value_i[i]=   this.ob_spinner[i].spinner( "value" ) ; }  //end spin
            if (this.c_mode[i] == 2 ){  this.c_value_b[i]=   htmlcmp.getCheck('#'+this.c_id[i]  )          } //check
            if (this.c_mode[i] == 3 ){  this.c_value_txt[i]=  $('#'+this.c_id[i]).val()           } //text
            if (this.c_mode[i] == 4 ){  this.c_value_i[i]=   htmlcmp.getCombo('#'+this.c_id[i]  )          } //combo
            if (this.c_mode[i] == 6 ){  this.c_value_txt[i]=  $('#'+this.c_id[i]).val()           } //memo
        } //eloop
    };

    this.btnStr=function(id,bclass){  return '<button class="btn '+bclass+'" id="'+id+'" style="width:98%; margin-left: 3px" >btn</button>'  };

    this.draw=function (){

        for (var i = 0; i < 16; i++) {
            this.c_mode.push(0) ;
            this.c_id.push( base_id+i ) ;
            this.c_label.push('label'+i);
            this.c_field.push('');
            this.c_value_i.push(0);
            this.c_spmin.push(0);
            this.c_spmax.push(9999999);
            this.c_value_b.push(false);
            this.ob_spinner.push('');
            this.c_value_txt.push('');
            this.c_value_txt_l.push(150);
            this.c_memo_c.push(20);
            this.c_memo_r.push(3);
        }

    };
    this.draw();
}


//****************************************************************************
//  popup menu
//****************************************************************************
function Kit_popup_menu_constructor(  ){

    this.btn_height=50;
    this._s='';
    this.markclass='bme12nu';
    this.wwidth=340;
    this.ww=70;
    this.lbt=false;
    this.top=true;

    this.addBtn=function(long, label,btnclass,btnico,code,enabled  ){

        var mr=0;
        if (long){  this.lbt=false; this.ww+= this.btn_height+10 } else {
            if (!this.lbt){  this.lbt=true;  mr=18 ;this.ww+= this.btn_height+10 }
        }

        var w=this.wwidth -20 ;
        if (!long){ w=(w/2)-10  }
        var bi='';
        if (btnico!=''){ bi='<i class="mdi i20 '+btnico+'" style="width: 40px"  ></i>'  }

        this._s+= '<button style="width:'+w+'px; height:'+this.btn_height+'px; margin-bottom:10px ; margin-right:'+mr+'px"' ;

        if (!enabled){ this._s+=' class="btndisabled">'+bi+label+'</button>'
        } else {
            this._s+=' class="btn '+btnclass+' '+this.markclass +'" data-button="'+code+'">'+bi+label+'</button>'
        }
    };

    this.addSep=function(slabel){ this._s+=slabel+'<div style="height:2px; background-color: #0a0a0a ; margin-bottom:10px" ></div> ';
        this.ww+= 15;
        if (slabel!=''){ this.ww+= 15 }
    };

    this.make=function(callback){
        function __dclose() {  dm.aFree()   }

        var dm = new Kit_dialog_constructor(this.wwidth,this.ww,4 ,0,true, this.top );
        dm.draw();

        dm.content( '<div class="mes_auth_marker">'+ this._s,false );
        dm.showDialog();

        $('.'+this.markclass).on('click touchstart',function(e){
            e.stopPropagation(); e.preventDefault();
            var data = $.parseJSON($(this).attr('data-button'));
            __dclose();
            is_callback(callback(data) );

        });
    }
}


//****************************************************************************
//  my dataset table
//****************************************************************************


function Kit_table_constructor(){
    var that = this;
    this.sx='';
    this.btnw=70;
    this.btnh=40;

    this.setbtn=function(w,h){
      this.btnw=w;
      this.btnh=h;
    }
    this.tbegin=function(tclass){
        this.sx+='<table  class="'+tclass+'">';
    };
    this.td=function(body){
        this.sx+='<td>'+body+'</td>'
    };

    this.tdw=function(w,body){
        this.sx+='<td  style="min-width: '+w+'px;" >'+body+'</td>'
   };
    this.tds=function(style,body){
        this.sx+='<td  style="'+style+'" >'+body+'</td>'
    };
    this.tdwc=function(w,cl,body){
        this.sx+='<td  style="min-width: '+w+'px; background-color: '+cl+'" >'+body+'</td>'
    };

    this.tend=function(){
        this.sx+='</table>';
        return this.sx;
    }

    this.trb=function(){ this.sx+='<tr>'    }
    this.tre=function(){ this.sx+='</tr>'    }

    this.tdbtn=function(id,data,caption,bclass){
        this.sx+='<td  style="min-width: '+ (this.btnw+10) +'px;" >'
        this.sx+=htmlts.btn_inline_wdata( id,data ,this.btnw,this.btnh,caption,bclass)
        this.sx+='</td>'
    }
    this.tdbtnW=function(w,id,data,caption,bclass){
        this.sx+='<td  style="min-width: '+ (this.btnw+10) +'px;" >'
        if(w){ this.sx+=htmlts.btn_inline_wdata( id,data ,this.btnw,this.btnh,caption,bclass)}
        this.sx+='</td>'
    }
    this.addhead=function(hl){
        this.sx+='<thead><tr>'+hl+' </tr> </thead>';
    }
};


function Dataset_table_constructor(parent, styl){
    var that = this;
    this.id=nohash(parent)+'_tab';
    this.idc=nohash(parent)+'_ct';
    this.idd=nohash(parent)+'_d';
    this.idb=nohash(parent)+'_b';
    this.idf=nohash(parent)+'_f';
    this.colwidth=[];
    this.colname=[];
    this.height=100;
    this.no_find_box=false;
    this.min_cell_height=0;
    this._sb='';
    this._clw='';
    this.rc=0;
    this.grc=0;

    this.td=function(s){
        var mhc='';
        if (this.min_cell_height!=0){  mhc='style="height:'+this.min_cell_height+'px"'   }
        return '<td '+mhc+'>'+s+'</td>';
    };
    this.tdc=function(s,color){
        var mhc='style="background-color:'+color+'"'   ;
        return '<td '+mhc+'>'+s+'</td>';
    };

    this.tdid=function(s,id){
        var mhc='';
        if (this.min_cell_height!=0){  mhc='style="height:'+this.min_cell_height+'px"'   }
        return '<td id="'+id+'"   '+mhc+'>'+s+'</td>';
    };

    this.tdidc=function(s,id,color){
        var mhc='style="color:black; background-color:'+color+'"'   ;;
        return '<td id="'+id+'"   '+mhc+'>'+s+'</td>';
    };


    this.make=function(data){
        var scw='';
        var scn='';
        var s='';
        var shtml='';

        for (var i = 0; i < this.colname.length; ++i)
        {
            scn=scn+'<th >'+this.colname[i]+'</th>';
            scw=scw+'<col style="width:'+this.colwidth[i]+'px ">'

        }
        this._clw = scw;
        shtml+='<table  class="dataset_'+styl+'" >';
        shtml+='<colgroup>'+scw+'</colgroup>';
        shtml+='<thead></thead><tr>'+scn+'</tr></thead>';
        shtml+='</table>';
        shtml+='<div id="'+this.idc+'" style="height:'+this.height+'px"> no data ...</div>';

        if (!this.no_find_box){
        shtml+='<div style="margin-left: 4px;  margin-top:4px;"  ><input style="max-height: 27px;width:150px" id="'+this.idf+'" value="" placeholder="szukaj">';
        shtml+='<button id="'+this.idb+'"  style="height: 27px;width:27px;margin:0;padding: 0;vertical-align:bottom" class="btn btn-red" >X</button>';
        shtml+='<div id="'+this.idd+'" style="display:inline; vertical-align: bottom;padding-left: 30px">----</div> </div>';
        }
        $(parent).html(shtml);


    };

    this.load=function(data){
        var shtml='';
        shtml+='<table id="'+this.id+'" class="dataset_'+styl+'"  >';
        shtml+='<colgroup>'+this._clw+'</colgroup>';
        shtml+=data;
        shtml+='</table>';

        $('#'+this.idc).off();
        $('#'+this.idc).html('');
        $('#'+this.idc).html(shtml);
        this._sb= MakeScrollBox('#'+this.idc);
        this._sb.refresh();
        that.count();
        this.grc=this.rc;
        that.count();

        $('#'+this.idb).click( function(){
            $("#"+that.idf).val('');
            $('#'+that.id+' tr').filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf('') > -1)
                that._sb.refresh();
                that.count();
            });
        });


        $("#"+this.idf).on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $('#'+that.id+' tr').filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
                that._sb.refresh();
                that.count();
            });
        });
    };

    this.count=function(){
        this.rc= $('#'+this.id+' tr:visible').length;
        if(this.rc==this.grc){   $('#'+this.idd).html('rec:'+this.rc ) }else{ $('#'+this.idd).html('rec:'+this.rc+'/'+this.grc ) }
    };

    this.setheight=function(h){ $('#'+this.idc).css('height',h  )  }

}


//****************************************************************************
//  notification
//****************************************************************************


function kit_note(txt){       $.notific8( ' '+ txt,{life: 3000,theme: 'lemon'}); }
function kit_note_ex (aTitle, aTxt, lifeTime,   aStyle ){
    var st=false;
    var thxx='';
    if (lifeTime==0){ st=true; } else { st=false; }

    switch (aStyle)
    {
        case 1:  thxx='lemon';  break;
        case 4:  thxx='lime';  break;
        case 5:  thxx='red';  break;
        case 2:  thxx='tangerine';  break;
        case 3:  thxx='ebony';  break;
        case 6:  thxx='amethyst';  break;
        case 7:  thxx='teal';  break;
        case 8:  thxx='smokie';  break;
    }
    if (thxx==''){thxx=aStyle}
    $.notific8( ' '+aTxt,{  heading:aTitle, height:200, life:lifeTime*1000,  sticky:st,   theme:thxx   });
}