/* 
 * Autor    : Dhidhik
 * Date     : 15 Januari 2016
 * Desc     : Mendeklarasikan variable untuk nilai dari code ascii
 * 
 */
var keys_tab    = 9 ;
var keys_alt    = 18;
var keys_left   = 37;
var keys_top    = 38;
var keys_right  = 39;
var keys_bottom = 40;
var keys_a =65; 
var keys_b =66; 
var keys_c =67; 
var keys_d =68; 
var keys_e =69; 
var keys_f =70; 
var keys_g =71; 
var keys_h =72; 
var keys_i =73; 
var keys_j =74; 
var keys_k =75; 
var keys_l =76; 
var keys_m =77; 
var keys_n =78; 
var keys_o =79; 
var keys_p =80; 
var keys_q =81; 
var keys_r =82; 
var keys_s =83; 
var keys_t =84; 
var keys_u =85; 
var keys_v =86; 
var keys_w =87; 
var keys_x =88; 
var keys_y =89; 
var keys_z =90; 

var keys_keys_tab       = keys_tab;//9
var keys_button_first   = keys_alt;//18
var keys_open_edit_grid = keys_i;//73
var keys_button_new     = keys_n; //78   name button class = keyBtnNew
var keys_button_edit    = keys_i; //73   name button class = keyBtnEdit
var keys_button_delete  = keys_l; //76   name button class = keyBtnDelete
var keys_button_save    = keys_s; //83   name button class = keyBtnSave
var keys_button_search  = keys_s; //83   name button class = keyBtnSearch
var keys_button_reset   = keys_t; //84   name button class = keyBtnReset
var keys_button_refresh = keys_r; //82   name button class = keyBtnRefresh
var keys_button_ok      = keys_k; //75   name button class = keyBtnOk
var keys_button_cancel  = keys_a; //65   name button class = keyBtnCancel
var keys_button_close   = keys_c; //67   name button class = keyBtnClose
var keys_button_suspend = keys_b; //66   name button class = keyBtnSuspend
var keys_button_preview = keys_v; //86   name button class = keyBtnPreview
var keys_button_print   = keys_p; //80   name button class = keyBtnPrint
var keys_button_journal = keys_j; //74   name button class = keyBtnJournal
var keys_button_help    = keys_h; //72   name button class = keyBtnHelp
var keys_button_addItem = keys_t; //84   name button class = keyBtnAddItem
var keys_button_editItem= keys_u; //85   name button class = keyBtnEditItem
//
var isKeys              = [];   //variable Global
var isArrayTab          = [];   //variable Global
var isArrayTabIndex     = -1;   //variable Global
var isIdNameDatagrid    = null; //variable Global
var isFirstFieldName    = null; //variable Global
var isFormIdDatagrid    = "";   //variable Global
var isStatusShortKey    = true; //variable Global

var urlGetKeys      = base_url_shortkey+"custom/shortkey/get_value_isKeys";
var urlSetKeys      = base_url_shortkey+"custom/shortkey/set_value_isKeys";
var urlResetKeys    = base_url_shortkey+"custom/shortkey/reset_value_isKeys";
//var url         = base_url_shortkey + 'report/index_cash_bank/ajax_reloadDataPayment';
//console.log("location="+urlGetKeys);

$(document).ready(function(){
    var tabsTopTitle = "";
    $.post(urlGetKeys).done( function(result){isKeys = JSON.parse(result);setArrayTrue(isKeys);});
    $('#tt').tabs({
        onSelect:function(tabTopTitle,tabTopIndex){
            //alert(' is selected');
            //isKeys = [];
            $.post(urlGetKeys).done( function(result){
                setTimeout(isKeys = JSON.parse(result),0);
                setArrayTrue(isKeys);
                tabsTopTitle = tabTopTitle;
                var tipePage = self;
                $( "#tt" ).length?tipePage=self:tipePage=parent;
                var tabTop           = tipePage.$('#tt'); 
                var tabsTop          = tabTop.tabs('tabs');
                //var tabsTopTitle     = tabsTop[tabTopIndex].panel('options').title;
                if(tabsTop[tabTopIndex].find('#ifmWindow').length){
                    var iframe = tabsTop[tabTopIndex].find('#ifmWindow');
                    iframe[0].focus();                    
                }
            });   
            
        }
    });
});

$(document).keydown(function(e) {    
    $.post(urlGetKeys).done( function(result){
        isKeys = JSON.parse(result);
        setArrayTrue(isKeys);
        if(isStatusShortKey){
            if(keys_button_first==e.keyCode){
                $.post(urlSetKeys+"/"+e.keyCode+"/"+true).done( function(result){
                    isKeys = JSON.parse(result); 
                    setArrayTrue(isKeys); 
                    exeKeyShort(e);
                });
            }else{
                setArrayTrue(isKeys);
                exeKeyShort(e);
            }
        }
    });
});
$(document).keyup(function(e) {   
    if(keys_button_first==e.keyCode){
        $.post(urlSetKeys+"/"+e.keyCode+"/"+false).done( function(result){
            isKeys = JSON.parse(result);
            setArrayTrue(isKeys);
        });
    }else{
        setArrayTrue(isKeys);
    }
});
    
//>>>>>>>>>>>>>>>>>>>>>>>>> Event Function Key Button <<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    function keyTab(e){
        var nArray      = isArrayTab.length;
        var indexFocus  = 0; 
        if(nArray>0){            
            if(keys_keys_tab == e.keyCode){ 
                //alert($focusedElement.attr("id"));
                var $focusedElement         = $("*:focus");
                var $focusedElementName     = $focusedElement.attr("id");
                var $focusedElementIndex    = jQuery.inArray( $focusedElementName, isArrayTab);
                
                if((isArrayTabIndex<0) || (isArrayTabIndex>0)){
                    isArrayTabIndex = 0;
                    indexFocus      = 0;             
                    if($.isArray(isArrayTab[indexFocus])){  
                        if(isArrayTab[indexFocus][0]['type']=="combobox"){ 
                            //console.log(" tekan kene 0 combobox");                       
                            var idName  = "#"+isArrayTab[indexFocus][0]['idname'];
                            if($(idName)){setTimeout(function(){ $(idName).next().find("input").focus(); });} 
                        }
                        else if(isArrayTab[indexFocus][0]['type']=="date"){                  
                            var idName  = "#"+isArrayTab[indexFocus][0]['idname'];
                            if($(idName)){setTimeout(function(){ $(idName).next().find("input").focus(); });} 
                        }
                    }else{                        
                        var idName      = "#"+isArrayTab[indexFocus]+"";
                        if($(idName)){setTimeout(function(){ $(idName).focus(); });}
                    }   
//                    var idName      = "#"+isArrayTab[indexFocus]+"";
//                    if($(idName)){setTimeout(function(){ $(idName).focus(); console.log("kudune wis di tab di 0");});}
                }else{
                    if($focusedElementIndex==1){                        
                        isArrayTabIndex = 1;
                        indexFocus      = 1;                
                        if($.isArray(isArrayTab[indexFocus])){  
                            if(isArrayTab[indexFocus][0]['type']=="combobox"){                        
                                var idName  = "#"+isArrayTab[indexFocus][0]['idname'];
                                if($(idName)){setTimeout(function(){ $(idName).next().find("input").focus(); });} 
                            }
                            else if(isArrayTab[indexFocus][0]['type']=="date"){                  
                                var idName  = "#"+isArrayTab[indexFocus][0]['idname'];
                                if($(idName)){setTimeout(function(){ $(idName).next().find("input").focus();});} 
                            }
                        }else{                        
                            var idName      = "#"+isArrayTab[indexFocus]+"";
                            if($(idName)){setTimeout(function(){ $(idName).focus(); });}
                        }         
                    }
                }
            }             
        }
    }
    
    function keyUpDownGrid(e){  
        if((isIdNameDatagrid!=null && isIdNameDatagrid!="") && (e.keyCode==38 || e.keyCode==40)){
            var idName = "#"+isIdNameDatagrid; 
            var qrow = qtyRowDatagrid(isIdNameDatagrid);
//            var thisGrid = $(idName).datagrid('getPanel').panel('panel').attr('tabindex',1);
//            thisGrid.focus();
            var indexgrid = indexSelectedRowDatagrid(isIdNameDatagrid);
            if(checkValidDatagrid(isFormIdDatagrid)){
                if(qrow>0){
                    $(idName).datagrid('endEdit', indexgrid);
                    if(e.keyCode==38){// up
                        indexgrid = indexSelectedRowDatagrid(isIdNameDatagrid);
                        if ((indexgrid-1)<0){
                            indexgrid=0;
                            $(idName).datagrid('selectRow', indexgrid);
                        } else {
                            indexgrid = indexgrid - 1;
                            $(idName).datagrid('selectRow', indexgrid);
                        }
                    }else if(e.keyCode==40){// down
                        indexgrid = indexSelectedRowDatagrid(isIdNameDatagrid);
                        if ((indexgrid+1)<0){
                            indexgrid=0;
                            $(idName).datagrid('selectRow', indexgrid);
                        } else if ((indexgrid+1)>=qrow){
                            indexgrid=(qrow-1);
                            $(idName).datagrid('selectRow', indexgrid);
                        } else {
                            indexgrid=indexgrid+1;
                            $(idName).datagrid('selectRow', indexgrid);
                        }
                    }
                }
            }
                
        }
    }
    
    function next_tab(xpage,e){//>>>>>>>>>>>>>>> NEXT TAB <<<<<<<<<<<<<<<<<<<<
        var tipePage = self;
        xpage=="parent"?tipePage=parent:tipePage=self;
        if((isKeys[keys_button_first]) && keys_right==e.keyCode){ 
            var tab = tipePage.$('#tt').tabs('getSelected');
            var index = tipePage.$('#tt').tabs('getTabIndex',tab);  
            var countTab = tipePage.$('#tt ul.tabs li').size();
            var nextIndex = index + 1;
            if(nextIndex>=countTab){nextIndex=0;}
            var t = tipePage.$('#tt');
            var tabs = t.tabs('tabs');
            var titleTab = tabs[nextIndex].panel('options').title;
            t.tabs('select', titleTab);   
        }        
    }//>>>>>>>>>>>>>>>END NEXT TAB <<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyOpenEditGrid(e){ //>>>>>>>>>>>>>>> OPEN EDIT GRID  <<<<<<<<<<<<<<<<<<<<<<<<         
        if(isKeys[keys_button_first] && keys_open_edit_grid==e.keyCode){    
            if((isIdNameDatagrid!=null && isIdNameDatagrid!="")){
                var idName = "#"+isIdNameDatagrid; 
                var qrow = qtyRowDatagrid(isIdNameDatagrid);
                var thisGrid = $(idName).datagrid('getPanel').panel('panel').attr('tabindex',1);
                var rstValid = checkValidDatagrid(isFormIdDatagrid);
                if(isFormIdDatagrid!="" && rstValid){
                    if(thisGrid.focus()){
                        if(isFirstFieldName!=null && isFirstFieldName!=""){
                            var indexgrid = indexSelectedRowDatagrid(isIdNameDatagrid);
                            if(indexgrid>=0){
                                $(idName).datagrid('beginEdit', indexgrid); 
                                focusCellDatagrid(indexgrid,isIdNameDatagrid,isFirstFieldName);
                            }
                        }                        
                    }
                }else{
                    if((typeof tempIndexEditRow!='undefined') ){
                        if((tempIndexEditRow!=-1) && (tempIndexEditRow!=undefined) && (tempIndexEditRow!="")){
                            $("#"+isIdNameDatagrid).datagrid('selectRow', tempIndexEditRow);
                            console.log("key tempIndexEditRow= "+tempIndexEditRow);
                        }  
                    }
                }    
            }            
        }
    }//>>>>>>>>>>>>>>>>END- OPEN EDIT GRID <<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyBtnNew(pageIndex,e){ //>>>>>>>>>>>>>>> N E W  <<<<<<<<<<<<<<<<<<<<<<<<         
        if(isKeys[keys_button_first] && keys_button_new==e.keyCode){    
            var nameBtnNew = '.keyBtnNew'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>>>>END- N E W <<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyBtnEdit(pageIndex,e){//>>>>>>>>>>>>>>> E D I T <<<<<<<<<<<<<<<<<<<<<<<<  
        if(isKeys[keys_button_first] && keys_button_edit==e.keyCode){  
            var nameBtnNew = '.keyBtnEdit'+pageIndex;
            exeClick(nameBtnNew);
        }        
    }//>>>>>>>>>>>>>>>END- E D I T <<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyBtnDelete(pageIndex,e){//>>>>>>>>>>>>>>> D E L E T E <<<<<<<<<<<<<<<<<<<<<<<<
        if(isKeys[keys_button_first] && keys_button_delete==e.keyCode){  
            var nameBtnNew = '.keyBtnDelete'+pageIndex;
            exeClick(nameBtnNew);  
        }
    }//>>>>>>>>>>>>>END- D E L E T E <<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyBtnSave(pageIndex,e){//>>>>>>>>>>>>>>> S A V E <<<<<<<<<<<<<<<<<<<<
        if(isKeys[keys_button_first] && keys_button_save==e.keyCode){   
            var nameBtnNew = '.keyBtnSave'+pageIndex;
            exeClick(nameBtnNew);  
        }
    }//>>>>>>>>>>>>>>>END- S A V E <<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyBtnSearch(pageIndex,e){ //>>>>>>>>>>>>>>> S E A R C H <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_search==e.keyCode){       
            var nameBtnNew = '.keyBtnSearch'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- S E A R C H <<<<<<<<<<<<<<<<<<<<<<<<  
    
    function keyBtnReset(pageIndex,e){ //>>>>>>>>>>>>>>> R E S E T <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_reset==e.keyCode){       
            var nameBtnNew = '.keyBtnReset'+pageIndex;
            $.post(urlResetKeys).done( function(result){
                exeClick(nameBtnNew);
                isKeys = JSON.parse(result);
            });
        }
    }//>>>>>>>>>>>>>END- R E S E T <<<<<<<<<<<<<<<<<<<<<<<<    
        
    function keyBtnRefresh(pageIndex,e){ //>>>>>>>>>>>>>>> R E F R E S H <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_refresh==e.keyCode){       
            var nameBtnNew = '.keyBtnRefresh'+pageIndex;
            $.post(urlResetKeys).done( function(result){
                exeClick(nameBtnNew);
                isKeys = JSON.parse(result);
            });
        }
    }//>>>>>>>>>>>>>END- R E F R E S H <<<<<<<<<<<<<<<<<<<<<<<<   
        
    function keyBtnOk(pageIndex,e){ //>>>>>>>>>>>>>>> O K <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_ok==e.keyCode){       
            var nameBtnNew = '.keyBtnOk'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- O K <<<<<<<<<<<<<<<<<<<<<<<<  
    
    function keyBtnCancel(pageIndex,e){ //>>>>>>>>>>>>>>> C A N C E L <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_cancel==e.keyCode){       
            var nameBtnNew = '.keyBtnCancel'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- C A N C E L <<<<<<<<<<<<<<<<<<<<<<<< 
    
    function keyBtnClose(pageIndex,e){ //>>>>>>>>>>>>>>> C L O S E <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_close==e.keyCode){       
            var nameBtnNew = '.keyBtnClose'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- C L O S E <<<<<<<<<<<<<<<<<<<<<<<< 
    
    function keyBtnPreview(pageIndex,e){ //>>>>>>>>>>>>>>> P R I N T   P R E V I E W <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_preview==e.keyCode){       
            var nameBtnNew = '.keyBtnPreview'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- P R I N T    P R E V I E W <<<<<<<<<<<<<<<<<<<<<<<<  
    
    function keyBtnPrint(pageIndex,e){ //>>>>>>>>>>>>>>> P R I N T <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_print==e.keyCode){       
            var nameBtnNew = '.keyBtnPrint'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- P R I N T <<<<<<<<<<<<<<<<<<<<<<<<   
    
    function keyBtnJournal(pageIndex,e){ //>>>>>>>>>>>>>>> J O U R N A L <<<<<<<<<<<<<<<<<<<< 
        if(isKeys[keys_button_first] && keys_button_journal==e.keyCode){       
            var nameBtnNew = '.keyBtnJournal'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>>END- J O U R N A L <<<<<<<<<<<<<<<<<<<<<<<<  
                
    function keyBtnAddItem(pageIndex,e){    //>>>>>>>>>>>>>>> ADD ITEM <<<<<<<<<<<<<<<<<<<<
        if(isKeys[keys_button_first] && keys_button_addItem==e.keyCode){       
            var nameBtnNew = '.keyBtnAddItem'+pageIndex;
            exeClick(nameBtnNew);
        }
    }//>>>>>>>>>>>>END- ADD ITEM <<<<<<<<<<<<<<<<<<<<<<<<
        
    function keyBtnEditItem(pageIndex,e){    //>>>>>>>>>>>>>>> EDIT ITEM <<<<<<<<<<<<<<<<<<<<
        if(isKeys[keys_button_first] && keys_button_editItem==e.keyCode){       
            var nameBtnNew = '.keyBtnEditItem'+pageIndex;
            exeClick(nameBtnNew); 
        }
    }//>>>>>>>>>>>END- EDIT ITEM <<<<<<<<<<<<<<<<<<<<<<<<  
    
    function exeClick(nameBtnNew){ //>>>>>>>>>>>>>>> ACTION EVENT CLICK BUTTON <<<<<<<<<<<<<<<<<<<<<<<<<       
        if($(nameBtnNew)){
            if(!$(nameBtnNew).attr('disabled')){
                $(nameBtnNew).click();
            }
        }            
    } //>>>>>>>>>>>>>>>END ACTION EVENT CLICK BUTTON <<<<<<<<<<<
    
    function exeClickReset(nameBtnNew){ //>>>>>>>>>>>>>>> ACTION EVENT CLICK BUTTON RESET / REFRESH <<<<<<<<<<<<<<<<<<<<<<<<<       
        if($(nameBtnNew)){
            if(!$(nameBtnNew).attr('disabled')){
                $(nameBtnNew).click();
            }
        }            
    } //>>>>>>>>>>>>>>>END ACTION EVENT CLICK BUTTON  RESET / REFRESH <<<<<<<<<<<
    
    function exeAllKeys(tipePage,e){        
        keyBtnNew("",e);
        keyBtnEdit("",e);
        keyBtnDelete("",e);
        keyBtnSave("",e);
        keyBtnSearch("",e);
        keyBtnReset("",e);
        keyBtnRefresh("",e);
        keyBtnOk("",e);
        keyBtnCancel("",e);
        keyBtnClose("",e);
        keyBtnPreview("",e);
        keyBtnPrint("",e);
        keyBtnJournal("",e);
    }
    
    function keyMultiTab(xpage,e){ //>>>>>>>>>>>>>>>>>>> MULTI TAB ON PAGE <<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
        var tipePage = self;
        var index= null;
        if(xpage=="parent"){ 
        }else{
            tipePage=self;
            var tab = tipePage.$('#wrapptabs').tabs('getSelected');
            index = tipePage.$('#wrapptabs').tabs('getTabIndex',tab);
            keyBtnNew(index,e);
            keyBtnEdit(index,e);
            keyBtnDelete(index,e);
            keyBtnCancel(index,e);
            keyBtnAddItem(index,e);
            keyBtnEditItem(index,e);
        }
    } //>>>>>>>>>>>>>>>>END MULTI TAB ON PAGE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    function keyMultiTabOkCancel(tipePage,e,idTableGrid){ //>>>>>>>>>>>>>>>>>>> MULTI TAB ON PAGE MODAL ITEM <<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
        var index= null;
        if(tipePage=="parent"){ 
        }else{
            tipePage=self;
            var tab = tipePage.$(idTableGrid).tabs('getSelected');
            index = tipePage.$(idTableGrid).tabs('getTabIndex',tab);
            keyBtnOk(index,e);
            keyBtnCancel(index,e);
        }
    } //>>>>>>>>>>>>>>>>END MULTI TAB ON PAGE MODAL ITEM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    
//>>>>>>>>>>>>>>>>>>>>>>>>>End Event Function Key Button <<<<<<<<<<<<<<<<<<<<<<<<<<<<//
    function exeKeyShort(e){
        var tipePage    = "";
        var tipePageStr = "self";
        if($("#tt").length){
            tipePage=self;    
            tipePageStr = "self";
        }else{
            tipePage=parent; 
            tipePageStr = "parent";
        } 

        var tabTop           = tipePage.$('#tt');
        var tabTopSelected   = tabTop.tabs('getSelected');
        var tabTopIndex      = tabTop.tabs('getTabIndex',tabTopSelected); 
        var tabsTop          = tabTop.tabs('tabs');
        var tabsTopTitle     = tabsTop[tabTopIndex].panel('options').title;

        if((tabsTop[tabTopIndex].find('#ifmWindow').length) && (!$('#dlg_periode_end:visible').length)){
            var iframe = tabsTop[tabTopIndex].find('#ifmWindow');
            iframe[0].focus(); 
        }else if((tabsTop[tabTopIndex].find('#ifmWindow').length) && ($('#dlg_periode_end:visible').length)){
            $("#dlg_periode_end").focus();
        }else{
            //noting
        }
        
        keyTab(e);
        keyUpDownGrid(e);
        keyOpenEditGrid(e);
        next_tab(tipePageStr,e);
        if(tabsTopTitle=="Preferences"){   
            menuPreference(e);
        }
//        else if((tabsTopTitle=="Purchase Requisitons") || (tabsTopTitle=="New Purchase Requisition") || (tabsTopTitle=="Edit Purchase Requisition")){
//            menuPurchaseRequisitions(tipePage,e);
//        }
        else{
            exeAllKeys(tipePage,e);
            menuPurchaseRequisitions(tipePage,e);
        }
    }
    
    function menuPreference(e){
        if($('#wrapptabs').length){
            keyMultiTab("self",e);
        }
    }
    
    function menuPurchaseRequisitions(tipePage,e){
        //exeAllKeys(tipePage,e);
        if($('#tab_purchase_req').length){
            keyMultiTabOkCancel(tipePage,e,'#tab_purchase_req');
        }        
    }
    
