<?php
    if (preg_match("/libdhtmlx.php/i", $_SERVER['PHP_SELF'])){header("location: index.php");die;}
    class MY_DHTMLX extends MY_HTML
    {
        var $my_form;
        public function FN_DHTMLXNEWCOMBO($myForm,$comboname,$url)
        {
            if(!$url){$url="''";}
            echo "var $comboname=$myForm.getCombo(\"$comboname\");
                      url=$url;
                      //alert(url);
                      $comboname.enableFilteringMode(true);
                      if(url)
                      {
                          $comboname.load(url);
                      }\n";    
        }
        
        public function FN_DHTMLXNEWCOMBOIMAGE($myForm,$comboname,$url,$setdefaultImagePath)
        {
            if(!$url){$url="''";}
            echo "var $comboname=$myForm.getCombo(\"$comboname\");
                      $comboname.setImagePath(\"".$setdefaultImagePath."\");
                      url=$url;
                      //alert(url);
                      $comboname.enableFilteringMode(true);
                      if(url)
                      {
                          $comboname.load(url);
                      }\n";    
        }        
        
        public function FN_DHTMLXNEWCOMBOWITHARRAY($myForm,$comboname,$arr)
        {
            $valarr="''";
            if(is_array($arr)){$valarr="'".json_encode($arr)."'";}
            echo "var $comboname=$myForm.getCombo(\"$comboname\");
                      url=$valarr;
                      //alert(url);
                      $comboname.enableFilteringMode(true);
                      if(url)
                      {
                          $comboname.load(url);
                      }\n";    
        }
        
        public function FN_DHTMLXNEWCOMBOGRID($gridname,$getColumnCombo,$comboname,$url)
        {
            echo "var $comboname=$gridname.getColumnCombo($getColumnCombo);
                      $comboname.enableFilteringMode(false);
                      $comboname.readonly(true);
                      url=$url;
                      //if($getColumnCombo==14){alert(url);}
                      $comboname.load(url);";
//            var cbousergroup2=dguser.getColumnCombo(7);
//              cbousergroup2.enableFilteringMode(false);
//              cbousergroup2.readonly(true);
//              url=fn_dhtmlxloadobject('".$this->enc("obj_user")."','obj=".$this->enc("-")."&task=".$this->enc("addcombo")."&tableobj=tbl_usergroup|groupuser,groupuser');
//              cbousergroup2.load(url);
        }
        
        public function FN_COMBOVALIDATIONVALUE($comboname,$tablename,$otherevent=null)
        {
            echo "$comboname.attachEvent(\"onChange\",function(val){
                        if(val!=='')
                        {
                            var result=ajaxuridhtmlx('".$this->enc("obj_controller")."','&task=".$this->enc("getvalidcombo")."&tablename=$tablename&id='+val+'&validationvalue='+$comboname.getComboText());
                            var spresult=result.split('|');
                            if(trim(spresult[0])!='ok'){fn_dhtmlxmessage('error','Error Found : '+spresult[1]);$comboname.setComboText('');$comboname.setComboValue('');return false;}
                            $otherevent
                        }
                   });";
        }
        
        public function FN_COMBODHMTLXFORM_GETJSON($myquery,$withoutindex0=null,$withimg=null)
        {
            $img="";
//            if($withimg){$img="img:\"\"";}
            $rsqry=$this->selectquery($myquery);
            if($rsqry)
            {    
                $rtn="{options:[";
                if(!$withoutindex0)
                {
                    //$rtn.="options:[{value:\"\",text:\"\"}";    
                    $rtn.="{value:\"\",text:\"\"},";    
                }
                $rtnval="";
                if($this->REC_ROWS_COUNT($rsqry)>0)
                {
                    while($rw=$this->data_row_array($rsqry))
                    {                                  
                        if($withimg){$img=",img:\"".$rw[2]."\"";}
                        $rtnval.="{value:\"".$rw[0]."\",text:\"".$rw[1]."\"$img},";
                    }
                    //$rtn.=substr($rtnval,0,strlen($rtnval)-1);                    
                    $rtnval=substr($rtnval,0,strlen($rtnval)-1);                    
                    $rtn.=$rtnval;
                }
                $rtn.="]}";
            }
            $this->clearrecordset($rsqry);
            return $rtn;
        }
        
        public function FN_COMBOVALIDATIONVALUE2($myForm,$comboname,$tablename,$url,$otherevent=null,$resulttovariable=null)
        {
            $rtn="var $comboname=$myForm.getCombo(\"".$comboname."\");
                      $comboname.enableFilteringMode(true);";
                      //if($url){$rtn.="$comboname.load($url);alert($url);";}
                      if($url){$rtn.="$comboname.load($url);";}
            $rtn.="    $comboname.attachEvent(\"onChange\",function(val){
                            if($comboname.getComboText())
                            {
                                if(val!=='')
                                {";
                                    if($tablename)
                                    {
                                        $rtn.="var result=ajaxuridhtmlx('".$this->enc("obj_controller")."','&task=".$this->enc("getvalidcombo")."&tablename=$tablename&id='+val+'&validationvalue='+$comboname.getComboText());
                                              var spresult=result.split('|');
                                              if(trim(spresult[0])!='ok'){fn_dhtmlxmessage('error','Error Found : '+spresult[1]);$comboname.setComboText('');$comboname.setComboValue('');return false;}";                                    
                                    }
            $rtn.="                  $otherevent
                                }
                                if(val==null){fn_dhtmlxmessage('error','Error Found : Mohon Maaf, Pilihan <b>'+$comboname.getComboText()+' tidak terdaftar</b>');$comboname.setComboText('');$comboname.setComboValue('');return false;}
                                else if(val==''){fn_dhtmlxmessage('error','Error Found : Silahkan Pilih pilihan anda!');$comboname.setComboText('');$comboname.setComboValue('');return false;}
                            }
                       });";
            if($resulttovariable){return $rtn;}else{echo $rtn;}
        }
        
        public function FN_COMBOVALIDATIONVALUEARRAY($myForm,$comboname,$arrvalue,$otherevent=null)
        {
            echo "var $comboname=$myForm.getCombo(\"".$comboname."\");
                      $comboname.enableFilteringMode(true);";
                      //if($url){echo "$comboname.load($url);";}
            echo "    $comboname.addOption(".json_encode($arrvalue).");";
            echo "    $comboname.attachEvent(\"onChange\",function(val){
                            if(val==null){fn_dhtmlxmessage('error','Error Found : Mohon Maaf Pilihan <b>'+$comboname.getComboText()+' tidak terdaftar</b>, Silahkan pilih sesuai yang ada pada Daftar Combo<br/>TERIMA KASIH.');$comboname.setComboText('');$comboname.setComboValue('');return false;}
                            if(val!=='')
                            {";
            echo "              $otherevent
                            }
                       });";
        }
        
        public function FN_DHTMLXGRIDVIEW($gridinit=array(),$gridcolumnproperties=array(),$gridtypes,$contexmenu=null,$functioninit=null,$resulttovariable=null)
        {
            $dgname=$gridinit['gridname'];
            $divtarget=$gridinit['divtarget'];
            $divpaging=$gridinit['divpaging'];
            $paginglimit=$gridinit['paginglimit'];
            $setcolumnhidden=$gridinit['setcolumnhidden'];
            $footer=$gridinit['footer'];
            $footerspan=$gridinit['footerspan'];
            $footeralign=$gridinit['footeralign'];
            $showloader=$gridinit['showloader'];
            $resulttovariable=$gridinit['resulttovariable'];
            $split=$gridinit['split'];
            $skin=$gridinit['skin'];if(!$skin){$skin="dhx_skyblue";}
            $columnformat=$gridinit['columnformat'];
            if(!$footeralign){$footeralign="left";}
            if(!$paginglimit){$paginglimit=PAGE_LIMIT;}
            if($contexmenu){$contexmenu="$dgname.enableContextMenu($contexmenu);";}
            $fieldname="";$headeralign="";$coltypes="";$widths="";$contentalign="";$colsort="";
            $enabletooltips="";
            foreach($gridcolumnproperties as $arr=>$arrval)
            {
                $fieldname.=$arr.",";
                $spprop=explode(",",$arrval);
                $headeralign.="\"text-align:".$spprop[0]."\",";$coltypes.=$spprop[1].",";
                $widths.=$spprop[2].",";$contentalign.=$spprop[3].",";$colsort.=$spprop[4].",";
                $enabletooltips.=$spprop[5].",";
                
            }
            $fieldname=substr($fieldname,0,strlen($fieldname)-1);
            $headeralign=substr($headeralign,0,strlen($headeralign)-1);
            $coltypes=substr($coltypes,0,strlen($coltypes)-1);
            $widths=substr($widths,0,strlen($widths)-1);
            $contentalign=substr($contentalign,0,strlen($contentalign)-1);
            $colsort=substr($colsort,0,strlen($colsort)-1);
            $enabletooltips=substr($enabletooltips,0,strlen($enabletooltips)-1);
            //echo $fieldname.' '.$headeralign.' '.$coltypes.' '.$contentalign.' '.$colsort;return false;
            $rtn="// === Data Grid Akun Header
                   $dgname=new dhtmlXGridObject(\"$divtarget\");
                   $dgname.setSkin(\"$skin\");
                   $dgname.setImagePath(\"".PATH_THEME."/codebase/imgs/\");
                   $dgname.setHeader(\"".$fieldname."\",null,[".$headeralign."]);
                   $dgname.setColTypes(\"".$coltypes."\"); // ro => Read Only, ed => Edit
                   $dgname.setInitWidths(\"".$widths."\");
                   $dgname.setColAlign(\"".$contentalign."\");
                   $dgname.setColSorting(\"".$colsort."\");
                   $dgname.enablePaging(true,".$paginglimit.",5,\"$divpaging\",true,\"recInfoArea\");
                   $dgname.setPagingSkin(\"".$gridtypes."\");".$contexmenu."                   
                   $dgname.enableTooltips(\"".$enabletooltips."\");";
                   if($setcolumnhidden){$rtn.="$dgname.setColumnsVisibility('".$setcolumnhidden."');";}
                   
                   if($columnformat)
                   {
                       foreach($columnformat as $arrf1=>$arrvalf1)
                       {
                           foreach($arrvalf1 as $arrf=>$arrfvalue)
                           {
                               //$rtn.="//".$arrf."<";
                               if($arrfvalue=='numberformat')
                               {
                                   $columnnumber=explode(",",$arrvalf1["columnnumber"]);
                                   for($i=0;$i<count($columnnumber);$i++)
                                   {
                                      $rtn.="$dgname.setNumberFormat(\"".$arrvalf1["format"]."\",".$columnnumber[$i].",\".\",\",\");";      
                                   }
                                   
                               }     
                           }
                       }
                   }
                    //<div id='div$dgnamegrandtotal' style='color:blue'>Total</div>,#cspan,#cspan,#cspan,<div id='divdgtranominal' style='text-align:right;'>{#stat_total}</div>,<div id='divdgtrapotongan' style='text-align:right;'>{#stat_total}</div>,<div id='divdgtratotalnominal' style='text-align:right;'>{#stat_total}</div>
                    if($footer)
                    {
                        if($footerspan)
                        {
                            $footerspan=str_repeat("#cspan,", $footerspan);$footerspan=substr($footerspan, 0, strlen($footerspan)-1);
                        } 
                        $rtn.="$dgname.attachFooter(\"".$footer.",".$footerspan."\",[\"text-align:".$footeralign.";\"]);";
                    }
            $rtn.=" $dgname.init();";
            if($functioninit){$rtn.="$functioninit;";}
            if($split){$rtn.="$dgname.splitAt($split);";}
            if($showloader)
            {
                $rtn.="$dgname.attachEvent(\"onXLS\",function(){document.getElementById('loadergrid').style.display='block';});";
                $rtn.="$dgname.attachEvent(\"onXLE\",function(){document.getElementById('loadergrid').style.display='none';});";
            }
            if($resulttovariable){return $rtn;}else{echo $rtn;}
        }
        
        public function FN_DHTMLXGRIDVIEWTOFORM($gridname,$formname,$tablename,$idparameterindex,$fieldsarray,$fielddatetime=null,$otherevent=null,$otherbuttonevent=null,$resulttovariable=null,$expcolumnindexclick=null)
        {
            $rtn="$gridname.attachEvent(\"onRowSelect\",function(rowid,ind){";
            if($expcolumnindexclick){$rtn.="if(ind==$expcolumnindexclick){return false;}";}
            $rtn.="id=$gridname.cells(rowid,$idparameterindex).getValue();";
            $i=0;$fieldsid="";$getresult="";$fieldfocus="";
            foreach($fieldsarray as $arr=>$arrvalue)
            {   
                if($i==1){$fieldfocus=$arr;}
                if($i>0)
                {
                    $tfield=$arrvalue;
                    if(!$arrvalue)
                    {
                        $tfield=$arr;
                        $spf=explode("_",$tfield);
                        $tfield=$spf[1];
                    }
                
                    $fieldname.=$tfield.",";
                    //$rtn.="   var ".$fieldname."=spres[".$i."];";
                    $getresult.="$formname.setItemValue('".$arr."',trim(spres[".$i."]));";
                }
                else
                {$getresult.="$formname.setItemValue('".$arr."',id);";}
                $i+=1;
            }
            $fieldname=substr($fieldname,0,strlen($fieldname)-1);
            if($otherevent){$getresult.=$otherevent;}
            $rtn.="   var result=ajaxuridhtmlx('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("getinfotable")."&tablename=$tablename&fields=$fieldname&fielddatetime=$fielddatetime&id='+id);";
            //$rtn.="   alert(result);";
            $rtn.="   var spres=result.split('|');";
            $rtn.=$getresult;
            if($otherbuttonevent)
            {
                $rtn.=$otherbuttonevent;
            }
            else
            {
                $rtn.="   $formname.disableItem(\"btnsave\");";
                $rtn.="   $formname.enableItem(\"btnupdate\");";
                $rtn.="   $formname.enableItem(\"btndelete\");";
                $rtn.="   $formname.setItemFocus(\"$fieldfocus\");";
            }
            $rtn.="});";
            if($resulttovariable){return $rtn;}else{echo $rtn;}
            //$rtn.="   var customeridentity=spres[1];";
//            $rtn.="   var customercode=spres[2];";
//            $rtn.="   var customername=spres[3];";
//            $rtn.="   var address=spres[4];";
//            $rtn.="   var region=spres[5];";
//            $rtn.="   var town=spres[6];";
//            $rtn.="   var phone=spres[7];";
//            $rtn.="   var contactperson=spres[8];";
//            $rtn.="   var contactpersonnumber=spres[9];";
//            $rtn.="   var notes=spres[10];";

//            $rtn.="   myForm.setItemValue('txtidcustomer',id);";
//            $rtn.="   myForm.setItemValue('txt_customeridentity',customeridentity);";
//            $rtn.="   myForm.setItemValue('txt_customercode',customercode);";
//            $rtn.="   myForm.setItemValue('txt_customername',customername);";
//            $rtn.="   myForm.setItemValue('txt_address',address);";
//            $rtn.="   myForm.setItemValue('txt_region',region);";
//            $rtn.="   myForm.setItemValue('txt_town',town);";
//            $rtn.="   myForm.setItemValue('txt_phone',phone);";
//            $rtn.="   myForm.setItemValue('txt_contactperson',contactperson);";
//            $rtn.="   myForm.setItemValue('txt_contactpersonnumber',contactpersonnumber);";
//            $rtn.="   myForm.setItemValue('txt_notes',notes);";

//            $rtn.="   myForm.disableItem(\"btnsave\");";
//            $rtn.="   myForm.enableItem(\"btnupdate\");";
//            $rtn.="   myForm.enableItem(\"btndelete\");";
                      //myForm.enableItem(\"btncancel\");";
//            $rtn.="   myForm.setItemFocus(\"txt_customeridentity\");";

        }
        
        
        
        public function FN_DHTMLXGRIDVIEWINCONTENT($gridinit=array(),$gridcolumnproperties=array(),$gridtypes,$contexmenu=null,$functioninit=null)
        {
            $rtn="";
            $dgname=$gridinit['gridname'];
            $contenttarget=$gridinit['contenttarget'];
            $divpaging=$gridinit['divpaging'];
            $paginglimit=$gridinit['paginglimit'];
            $setcolumnhidden=$gridinit['setcolumnhidden'];
            $footer=$gridinit['footer'];
            if(!$paginglimit){$paginglimit=PAGE_LIMIT;}
            if($contexmenu){$contexmenu="$dgname.enableContextMenu($contexmenu)";}
            $fieldname="";$headeralign="";$coltypes="";$widths="";$contentalign="";$colsort="";
            $enabletooltips="";
            foreach($gridcolumnproperties as $arr=>$arrval)
            {
                $fieldname.=$arr.",";
                $spprop=explode(",",$arrval);
                $headeralign.="\"text-align:".$spprop[0]."\",";$coltypes.=$spprop[1].",";
                $widths.=$spprop[2].",";$contentalign.=$spprop[3].",";$colsort.=$spprop[4].",";
                $enabletooltips.=$spprop[5].",";
                
            }
            $fieldname=substr($fieldname,0,strlen($fieldname)-1);
            $headeralign=substr($headeralign,0,strlen($headeralign)-1);
            $coltypes=substr($coltypes,0,strlen($coltypes)-1);
            $widths=substr($widths,0,strlen($widths)-1);
            $contentalign=substr($contentalign,0,strlen($contentalign)-1);
            $colsort=substr($colsort,0,strlen($colsort)-1);
            $enabletooltips=substr($enabletooltips,0,strlen($enabletooltips)-1);
            //echo $fieldname.' '.$headeralign.' '.$coltypes.' '.$contentalign.' '.$colsort;return false;
            $rtn="// === Data Grid 
                   $dgname=new dhtmlXGridObject($contenttarget);
                   $dgname.setSkin(\"dhx_skyblue\");
                   $dgname.setImagePath(\"".PATH_THEME."/codebase/imgs/\");
                   $dgname.setHeader(\"".$fieldname."\",null,[".$headeralign."]);
                   $dgname.setColTypes(\"".$coltypes."\"); // ro => Read Only, ed => Edit
                   $dgname.setInitWidths(\"".$widths."\");
                   $dgname.setColAlign(\"".$contentalign."\");
                   $dgname.setColSorting(\"".$colsort."\");
                   $dgname.enablePaging(true,".$paginglimit.",5,\"$divpaging\",true,\"recInfoArea\");
                   $dgname.setPagingSkin(\"".$gridtypes."\");".$contexmenu."                   
                   $dgname.enableTooltips(\"".$enabletooltips."\");";
                   if($setcolumnhidden){$rtn.="$dgname.setColumnsVisibility('".$setcolumnhidden."');";}
                   
                   //<div id='div$dgnamegrandtotal' style='color:blue'>Total</div>,#cspan,#cspan,#cspan,<div id='divdgtranominal' style='text-align:right;'>{#stat_total}</div>,<div id='divdgtrapotongan' style='text-align:right;'>{#stat_total}</div>,<div id='divdgtratotalnominal' style='text-align:right;'>{#stat_total}</div>
                   if($footer){$rtn.="$dgname.attachFooter(\"".$footer."\",[\"text-align:right;\"]);";}
            $rtn.=" $dgname.init();";
            if($functioninit){$rtn.="$functioninit;";}
            return $rtn;
        }
        
        public function FN_DHTMLXWINDOWS($windowsname,$windowproperties=array(),$w,$h,$x,$y,$attachtodiv=null,$myform=null,$formdata=null,$myformjquery=null)
        {
            //echo json_encode($formdata);return false;
            $windowtitle="";$skin="dhx_web";$denyresize="";$modalwin="";$closedby="";$eventwhenclosed="";
            $position="";$skinform="dhx_skyblue";
            foreach($windowproperties as $arr=>$arrval)
            {
                if($arr=='title'){$windowtitle=$arrval;}
                if($arr=='skin'){$skin=$arrval;}
                if($arr=='skinform'){$skinform=$arrval;}
                if($arr=='denyresize'){$denyresize=$arrval;}
                if($arr=='modalwin'){$modalwin=$arrval;}
                if($arr=='closedby'){$closedby=$arrval;}
                if($arr=='eventwhenclosed'){$eventwhenclosed=$arrval;}
                if($arr=='position'){$position=trim($arrval);}
            }
            echo "var dhxwin=new dhtmlXWindows();";
            echo "dhxwin.setSkin('$skin');";
            
            if($attachtodiv){echo "dhxwin.attachViewportTo('$attachtodiv');";}
            echo "$windowsname=dhxwin.createWindow(\"$windowsname\",$x,$y,$w,$h);";            
            echo "dhxwin.window('$windowsname').progressOn();";
            echo "$windowsname.setText('$windowtitle');";
            if($denyresize){echo "dhxwin.window('$windowsname').denyResize();";}
            if($modalwin){echo "dhxwin.window('$windowsname').setModal($modalwin);";}
            if(!$closedby){echo "dhxwin.window('$windowsname').button(\"close\").disable();";}
            echo "dhxwin.window('$windowsname').bringToTop();";
            if($position){echo "dhxwin.window('$windowsname').".$position."();";}
            if($myform)
            {
                $formdata=json_encode($formdata);
                echo "$myform=$windowsname.attachForm($formdata, true);";
                echo "$myform.setFocusOnFirstActive();";
                echo "$myform.setSkin('$skinform');";
                echo "$myform.attachEvent(\"onKeyDown\",function(inp,ev,id,value){";
                          if($closedby)
                          {
                              echo "if(ev.keyCode==27){dhxwin.unload();}"; 
                          }
                echo "});";
                
                if($myformjquery){echo $myformjquery;}
            }
            echo "dhxwin.window('$windowsname').progressOff();";
            if($eventwhenclosed){echo "dhxwin.attachEvent('onClose',function(win){".$eventwhenclosed.";return true;})";}
            
            
        }
        
        public function FN_DHTMLXWINDOWSHOWPROGESS($event)
        {
            $rtn="var dhxwin=new dhtmlXWindows();";
            $rtn.="dhxwin.setSkin('dhx_terrace');";
            
            $rtn.="winprogress=dhxwin.createWindow(\"winprogress\",0,0,190,40);";            
            $rtn.="winprogress.setText('Progress');";
            $rtn.="dhxwin.window('winprogress').hideHeader();";
            $rtn.="dhxwin.window('winprogress').denyResize();";
            $rtn.="dhxwin.window('winprogress').setModal(true);";
            $rtn.="dhxwin.window('winprogress').button(\"close\").disable();";
            $rtn.="dhxwin.window('winprogress').bringToTop();";
            $rtn.="dhxwin.window('winprogress').center();";
            $winprogressvalue="<table border=\"0\" style=\"font-family:tahoma;font-size:9pt;margin-top:-5px\" width=\"100%\">";
            $winprogressvalue.="    <tr>";
            $winprogressvalue.="         <td align=\"center\">Please Wait..<td>";
            $winprogressvalue.="    </tr>";
            $winprogressvalue.="    <tr>";
            $winprogressvalue.="         <td align=\"center\">".$this->CREATE_IMAGE("loaderring.gif","32","32")."<td>";
            $winprogressvalue.="    </tr>";
            $winprogressvalue.="</table>";
            $rtn.="winprogress.attachHTMLString('$winprogressvalue');";
            $rtn.=$event;
            return $rtn;
        }
        
        public function FN_DHTMLXWINDOWSHIDEPROGRESS()
        {
            return "dhxwin.unload();";    
        }
        
        public function FN_DHTMLXFORMCLEAR($myformname,$ArrayControls=array(),$setFocusControl=null)
        {
            $statement="";
            foreach($ArrayControls as $arr=>$arrval)
            {
                $statement="$myformname.setItemValue('$arr','$arrval');";
                //if(substr($arr,0,3)!='cbo'){$statement="$myformname.setItemValue('$arr','$arrval');";}
                if(substr($arr,0,3)=='chk'){$statement="$myformname.checkItem('$arr',$arrval);";}
                if(substr($arr,0,3)=='cbo'){$statement.="$myformname.getCombo('$arr').setComboValue('');$myformname.getCombo('$arr').setComboText('$arrval');";}
                $dhtmlxformclear.=$statement;
            }
            $rtn="$dhtmlxformclear";
            if($setFocusControl){$rtn.="$myformname.setItemFocus('$setFocusControl');";}            
            return $rtn;
        }
        
        public function FN_DHTMLXFORM($myformname,$elementarget,$formdata,$notsetFocusOnFirstActive=null)
        {
            $formdata=json_encode($formdata);
            
            echo "$myformname=new dhtmlXForm(\"".$elementarget."\", ".$formdata.");";
            echo "$myformname.enableLiveValidation(true);";
            if(is_null($notsetFocusOnFirstActive)){echo "$myformname.setFocusOnFirstActive();";}
            //echo "$myformname.setFocusOnFirstActive();";
        }
        
        public function FN_DHTMLXFORMDOCUMENT($myformname,$documentbody,$formdata)
        {
            $formdata=json_encode($formdata);
            
            echo "$myformname=new dhtmlXForm(".$documentbody.", ".$formdata.");";
            echo "$myformname.enableLiveValidation(true);";
            echo "$myformname.setFocusOnFirstActive();";
        }
        
        public function FN_DHTMLXFORMEVENT($myformname,$arrevent,$resulttovariable=null)
        {
            $rtn="";
            foreach($arrevent as $arr=>$arrval1)
            {
                $eventname="";$eventparam="";$withcase="";$arrsyntax=array();$statement="";
                //echo json_encode($arrval1);return false;
                foreach($arrval1 as $arrt1=>$arrvalt1)
                {
                    if($arrt1=='eventname'){$eventname=$arrvalt1;}
                    if($arrt1=='eventparam'){$eventparam=$arrvalt1;}
                    if($arrt1=='switch'){$withcase=$arrvalt1;$arrsyntax=$arrvalt1;}
                    if($arrt1=='statement'){$statement=$arrvalt1;}
                }
                $rtn="$myformname.attachEvent('$eventname',function($eventparam){";
                          if($withcase)
                          {
                              $rtn.="switch(id)
                                    {";
                                        foreach($arrsyntax as $arrcase=>$arrstatement)
                                        {
                                            $rtn.="case '$arrcase':$arrstatement;break;";
                                        }
                              $rtn.="}";
                          }
                          else{$rtn.=$statement;}    
                $rtn.="});";
                if($resulttovariable){return $rtn;}else{echo $rtn;}

            }
        }
                                                  
        public function FN_DHTMLXCREATECONTEXTMENUGRID($gridname,$contextmenuname,$arrmenucontext)
        {
            $menuproperties="";
            echo "var $contextmenuname=new dhtmlXMenuObject();
                      $contextmenuname.renderAsContextMenu();
                      $contextmenuname.setIconsPath(\"".PATH_THEME."/icons/\");
                   
                      $contextmenuname.attachEvent(\"onClick\",function(id){
                           switch(id)
                           {";
                               foreach($arrmenucontext as $arr=>$arrvalue)
                               {
                                   $event=$arrvalue["event"];
                                   $gifname=$arrvalue["gifname"];
                                   $menutext=$arrvalue["menutext"];
                                   $menuproperties.=$arr."|".$gifname."|".$menutext.",";
                                   echo "case '$arr':";
                                   if($arr=='contextmenu_deleted')
                                   {
                                        $contextconfirm="Data <b>'+getVal+'</b> ingin <font color=\"red\">dihapus</font>?";
                                        $contextevent="$gridname.deleteRow(data[0]);$event";

                                       //if(!$event)
                                       //{
//                                           echo "var data=$gridname.contextID.split(\"_\");
//                                                 var getValuserText=$gridname.cellById(data[0],1).getValue();
//                                                        dhtmlx.confirm({
//                                                            title:'Confirm',
//                                                            type:'confirm',
//                                                            text:'',
//                                                            callback:function(result){
//                                                                if(result){
//                                                                    $gridname.deleteRow(data[0]);$event;
//                                                                }
//                                                            }
//                                                        });";
                                       //}
                                       //else
                                       //{echo $event;}
                                   }
                                   if($arr=='contextmenu_print')
                                   {
                                       $contextconfirm="Data <b>'+getVal+'</b> ingin <font color=\"blue\">di Print</font>?";
                                       $contextevent=$event;
                                   }
                                   echo "var data=$gridname.contextID.split(\"_\");
                                         var getVal=$gridname.cellById(data[0],1).getValue();
                                                dhtmlx.confirm({
                                                    title:'Confirm',
                                                    type:'confirm',
                                                    text:'$contextconfirm',
                                                    callback:function(result){
                                                        if(result){
                                                            $contextevent;
                                                        }
                                                    }
                                                });";
                                   echo "break;";
                                   $menuproperties=substr($menuproperties,0,strlen($menuproperties)-1);
                               } 
            echo "         }";
            echo "    });
                   url=fn_dhtmlxloadobject('".$this->enc("obj_controller")."','obj=".$this->enc("-")."&task=".$this->enc("contxtmenustruc")."&mnuprop=$menuproperties');
                   //alert(url);
                   $contextmenuname.loadStruct(url);";    
        }
        
        public function FN_DHTMLXCONTEXTMENUSTRUCTURE()
        {
            $mnuprop=$_REQUEST['mnuprop'];
            $spmnuprop=explode(",",$mnuprop);
            
            header("Content-type: text/xml");
            echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
            echo("<menu id=\"0\">");
                for($i=0;$i<count($spmnuprop);$i++)
                {
                    $sp=explode("|",$spmnuprop[$i]);
                    $mnuid=$sp[0];$mnugif=$sp[1];$mnutext=$sp[2];
                    echo("<item text=\"$mnutext\" img=\"$mnugif\"  id=\"$mnuid\" />");
                }
            
            echo("</menu>");
        }
        
        public function FN_DHTMLXADDCOMBO($DBNAME=null)
        {
            if(!$DBNAME){$DBNAME=DB_APORTIL;}
            // === param request tablename|col1,col2
            $tableobj=$_REQUEST['tableobj'];
            //echo $tableobj;
            $sp=explode("|",$tableobj);
            
            $table=$sp[0];
            //echo $table;
            $spcol=explode(",",$sp[1]);
            $col1=$spcol[0];$col2=$spcol[1];
            
            $swhere="";
            if(isset($_REQUEST['exp']))//&&$_SESSION['nik']!=='9999'
            {
                //$spexp=explode("|",$_REQUEST['exp']);
                //$exp="and ".$spexp[0]."<>'".$spexp[1]."'";
                $spexp=explode(",",$_REQUEST['exp']);
                if(count($spexp)>0)
                {
                    for($i=0;$i<count($spexp);$i++)
                    {
                        $spcriteria=explode("|",$spexp[$i]);    
                        $swhere.="and ".$spcriteria[0]."<>'".$spcriteria[1]."' ";
                    }
                }
                else
                {
                    $spcriteria=explode("|",$spexp[$i]);
                    $swhere="and ".$spcriteria[0]."<>'".$spcriteria[1]."'";
                }
                
            }
            
            if(isset($_REQUEST['and']))//&&$_SESSION['nik']!=='9999'
            {
                //$spexp=explode("|",$_REQUEST['exp']);
                //$exp="and ".$spexp[0]."<>'".$spexp[1]."'";
                $spexp=explode(",",$_REQUEST['and']);
                if(count($spexp)>0)
                {
                    for($i=0;$i<count($spexp);$i++)
                    {
                        $spcriteria=explode("|",$spexp[$i]);    
                        $swhere.="and ".$spcriteria[0]."='".$spcriteria[1]."' ";
                    }
                }
                else
                {
                    $spcriteria=explode("|",$spexp[$i]);
                    $swhere="and ".$spcriteria[0]."='".$spcriteria[1]."'";
                }
                
            }
            if(isset($_REQUEST['in']))//&&$_SESSION['nik']!=='9999'
            {
                //$spexp=explode("|",$_REQUEST['exp']);
                //$exp="and ".$spexp[0]."<>'".$spexp[1]."'";
                $spexp=explode("|",$_REQUEST['in']);
                $swhere="and ".$spexp[0]." in (".$spexp[1].")";
                
            }
            
            $captionnulltext="";
            if(isset($_REQUEST['withallcaption'])){$captionnulltext="ALL";}
            header("Content-type: text/xml");
            echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
            echo("<complete>"); 
                     echo("<option value=\"\">$captionnulltext</option>");
                     $qry="select $col1,$col2 from ".$DBNAME.".$table where isdeleted=0 $swhere order by id asc";
                     //echo $qry;
                     if($this->fn_row_isexists($qry))
                     {
                         $rs=$this->selectquery($qry);
                         if($rs)
                         {
                             while($rw=$this->data_row_array($rs))
                             {
                                 //echo("<option value=\"".$rw->img."\" img_src=\"".$rw->imgname."\">".$rw->imgname."</option>"); 
                                 echo("<option value=\"".str_replace("&","&amp;",$rw[0])."\">".str_replace("&","&amp;",$rw[1])."</option>"); 
                             }
                         }
                         $this->clearrecordset($rs);                         
                     }
            echo("</complete>"); 
        }
        
        public function FN_DHTMLXLOADOBJECT($withvariable=null,$objroot,$objname,$task,$params=null)
        {
            $singlequote="'";
            if($params){$params="&$params";if(substr($params,strlen($params)-1,1)!="'"){$singlequote="";};}
            $url="fn_dhtmlxloadobject('".$this->enc($objroot)."','obj=".$this->enc($objname)."&task=".$task."".$params."$singlequote);";
            if($withvariable){$url=$withvariable."=".$url;}
            return $url;
        }
        
        public function FN_DHTMLXLOADOBJECTURL($option,$task){return "?ajax=1&sec=1&option=".$option."&".$task;}
        
    }
?>
