﻿/*---------------------------------------Default EVENT handlers---------------------------------------*/

dGrid.prototype.xDefOnEditCell = function(stage, rId, cInd, newVal, oldVal) {
    switch (stage) {
        // cell edit initiated (can be cancelled by return false)
        case 0:
            return this.xDefOnBeforeEdit(rId,cInd,oldVal);
            break;
        // cell edit activated and ready for input
        case 1:
            switch (this.getColType(cInd)) {
                case "ra":
                case "ch":
                    if (this.xDefOnAfterEdit(rId,cInd,oldVal,newVal)==false) {
                        return false;
                    } else {
                        this.xSetRowIsChanged(rId);
//                        var curFlag = this.xGetInsFlag(rId)
//                        if (curFlag == 'Q') { this.xSetInsFlag(rId,'U') };
//                        if (curFlag == '') { this.xSetInsFlag(rId,'I') };
//                        gDocModified = true;
                    }
                    break;
                default:
                    break;
            }
            break;
        // cell edit finished
        case 2:
            if (this.xDefOnAfterEdit(rId,cInd,oldVal,newVal)==false) {
                return false;
            } else {
                if (oldVal != newVal) {
                    this.xSetRowIsChanged(rId);
//                    var rInd = this.xRow(rId);
//                    var curFlag = this.xGetInsFlag(rId)
//                    if (curFlag == 'Q') { this.xSetInsFlag(rId,'U') };
//                    if (curFlag == '') { this.xSetInsFlag(rId,'I') };
//                    gDocModified = true;
                }
            }
            break;
    }
    return true;
}
;

dGrid.prototype.xDefOnColumnButtonClick = function(id,buttonId) {

    if (eval('self.'+this.xId+"_"+buttonId+'_Click')){if (eval(this.xId+"_"+buttonId+'_Click').call(this,id)==false) return false;}
    event.cancelBubble = true;
    return true;
}
;

dGrid.prototype.xDefOnBeforeEdit = function(id, ind, val) {
    if (this.xColData(ind)['R'] == true) { return false; } //Stop editing readonly column
    if (eval('self.' + this.xId + "_" + this.getColumnId(ind) + '_BeforeEdit')) { if (eval(this.xId + "_" + this.getColumnId(ind) + '_BeforeEdit').call(this, this.getRowIndex(id), ind, val) == false) return false; }

    return true;
}
;

dGrid.prototype.xDefOnAfterEdit = function(id, ind, oldVal, newVal) {

    if (eval('self.' + this.xId + "_" + this.getColumnId(ind) + '_AfterEdit')) { if (eval(this.xId + "_" + this.getColumnId(ind) + '_AfterEdit').apply(this, [this.getRowIndex(id), ind, oldVal, newVal]) == false) return false; }
    //Forge upper|lowercase if need
    if (this.xIsAllCaps(ind)) {
        if (typeof (newVal) == 'string') {
            this.matrix(id, ind).setValue(newVal.toUpperCase());
        }
    }
    return true;
}
;

dGrid.prototype.xDefOnKeyPress = function(keyCode, ctrlFlag, shiftFlag) {
    if (this.editor && this.cell == this.editor.cell) { //cell is being edited
        var col = this.xCol();
        //Catch F2 for codepopup
        if (keyCode == 113 && this.xColData(col)['isCode']) {
            var codeinfo = this.xColData(col)['CodeInfo'];
            codeinfo.gridRow = this.xRowId();
            this.editStop();
            this._search_code(codeinfo)
            return false;
        }
        //Live prevent invalid keys
        var dataType = this.xColData(col)['DT'];
        if (dataType && dataType.toUpperCase().indexOf('N') >= 0) {//Numeric datatype
            var isPassed;
            if (keyCode >= 48 && keyCode <= 57 && !shiftFlag) { isPassed = true };
            if ('8,9,13,27,46,109,110,189,190'.indexOf(keyCode) >= 0 && !shiftFlag) { isPassed = true };
            if ('43,45,107,187'.indexOf(keyCode) >= 0 && shiftFlag) { isPassed = true };
            if (keyCode >= 96 && keyCode <= 105 || keyCode == 109 || keyCode == 110) { isPassed = true }  //Number pad
            if (keyCode >= 37 && keyCode <= 40) { isPassed = true }  //arrow keys
            if (!isPassed) { return false; }
        }
    } else {
        //F2 -> no edit readonly cell
        if (keyCode == 113 && this.xColData(this.xCol())['RO'] == 'True') { return false; }
    }

    if (eval('self.' + this.xId + '_KeyPress')) { if (eval(this.xId + '_KeyPress').call(this, keyCode, ctrlFlag, shiftFlag) == false) return false; }
    return true;
}
;

dGrid.prototype.xDefOnRowSelect = function(id,ind) {

    if (eval('self.'+this.xId+'_Click')){if (eval(this.xId+'_Click').call(this,this.getRowIndex(id),ind)==false) return false;}
    if (eval('self.'+this.xId+"_"+this.getColumnId(ind)+'_Click')){if (eval(this.xId+"_"+this.getColumnId(ind)+'_Click').call(this,this.getRowIndex(id))==false) return false;}

    return true;
}
;

dGrid.prototype.xDefOnBeforeSelect = function(new_row,old_row) {

    if (this.useClones) this.bindRowCloneCells(new_row);

    return true;
}
;

dGrid.prototype.xDefOnValidateCorrect = function(id,ind,val,obj,rule) {

    if (eval('self.'+this.xId+'_ValidateCorrect')){if (eval(this.xId+'_ValidateCorrect').call(this,this.getRowIndex(id),ind)==false) return false;}
    if (eval('self.'+this.xId+"_"+this.getColumnId(ind)+'_ValidateCorrect')){if (eval(this.xId+"_"+this.getColumnId(ind)+'_ValidateCorrect').call(this,this.getRowIndex(id))==false) return false;}
    return true;
}
;

dGrid.prototype.xDefOnValidateError = function(id,ind,val,rule) {

    //if (eval('self.'+this.xId+'_ValidateError')){if (eval(this.xId+'_ValidateError').call(this,this.getRowIndex(id),ind)==false) return false;}
    //if (eval('self.'+this.xId+"_"+this.getColumnId(ind)+'_ValidateError')){if (eval(this.xId+"_"+this.getColumnId(ind)+'_ValidateError').call(this,this.getRowIndex(id))==false) return false;}

    //alert("error: "+this.getColumnId(ind));
    
    //return true;
    //this.selectCell(this.getRowIndex(id), ind, false, false, this.isEditable, true);
    window.event.cancelBubble = true;
    var self = this;
    window.setTimeout(
    function(){
          self.selectCell(self.getRowIndex(id), ind, false, false, self.isEditable, true);
    }
    ,1);
    return false;
}
;

dGrid.prototype.xDefOnDblClick = function(rId, cInd) {

    if (eval('self.' + this.xId + '_DblClick')) { if (eval(this.xId + '_DblClick').call(this, this.getRowIndex(rId), cInd) == false) return false; }
    if (eval('self.' + this.xId + "_" + this.getColumnId(cInd) + '_DblClick')) { if (eval(this.xId + "_" + this.getColumnId(cInd) + '_DblClick').call(this, this.getRowIndex(rId)) == false) return false; }

    //no edit readonly cell
    if (this.xColData(cInd)['RO']=='True') { return false; }
    return true;
};

dGrid.prototype.xDefOnRowAdded = function(rId) {

    if (eval('self.'+this.xId+'_RowAdded')){if (eval(this.xId+'_RowAdded').call(this,this.getRowIndex(rId))==false) return false;}

    return true;
}
;

dGrid.prototype.xDefOnMCClick = function(ind) {

    if (eval('self.'+this.xId+"_"+this.getColumnId(ind)+'_MC_Click')){if (eval(this.xId+"_"+this.getColumnId(ind)+'_MC_Click').call(this)==false) return false;}

    return true;
}
;

dGrid.prototype.xDefOnMCClickRowChanged = function(id,ind) {

    if (eval('self.'+this.xId+"_"+this.getColumnId(ind)+'_MC_ClickRowChanged')){if (eval(this.xId+"_"+this.getColumnId(ind)+'_MC_ClickRowChanged').call(this,this.getRowIndex(id))==false) return false;}

    return true;
}
;

/*********************************************************************************************/
/*********************************************************************************************/
// KEYMAPS - ACCESS STYLE (DEFAULT IN DHTMLX INCLUDE IS EXCEL STYLE)
/*********************************************************************************************/
/*********************************************************************************************/
dhtmlXGridObject.prototype._key_events={
	k13_1_0:function(){
		this.editStop();
	},
	k13_0_1:function(){
		this._key_events.k9_0_1.call(this)
	},
	k13_0_0:function(){
		this._key_events.k9_0_0.call(this)
    },
    k9_0_0:function(){
		this.editStop();
		if (window.event.cancelBubble == true) return;
		var z=this._getNextCell(null,1);
		if (z) {
			if (this.pagingOn) this.showRow(z.parentNode.idd);
			this.selectCell(z.parentNode,z._cellIndex,(this.row!=z.parentNode),false,true);
			this._still_active=true;
		}
		if (this.editor && this.editor.obj && this.editor.obj.select) this.editor.obj.select();
    },
	k9_0_1:function(){
		this.editStop();
		var z=this._getNextCell(null,-1);
		if (z) {
			this.selectCell(z.parentNode,z._cellIndex,(this.row!=z.parentNode),false,true);
			this._still_active=true;
		}
		if (this.editor && this.editor.obj) this.editor.obj.select();
    },
    k113_0_0:function(){
    	if (this._f2kE) this.editCell();
    },
    k32_0_0:function(){
    	var c=this.cells4(this.cell);
    	if (!c.changeState || (c.changeState()===false)) return false;
    },
    k27_0_0:function(){
    	this.editStop(true);
    },
    k33_0_0:function(){
    	if(this.pagingOn)
    		this.changePage(this.currentPage-1);
    	else this.scrollPage(-1);            		
    },
	k34_0_0:function(){
    	if(this.pagingOn)
    		this.changePage(this.currentPage+1);
    	else this.scrollPage(1);
    },
	k37_0_0:function(){
		if (this.editor) return false;
    	if(this.isTreeGrid())
    		this.collapseKids(this.row)
    	else this._key_events.k9_0_1.call(this);
    },
	k39_0_0:function(){
		if (this.editor) return false;
		if(!this.editor && this.isTreeGrid())
    		this.expandKids(this.row)
    	else this._key_events.k9_0_0.call(this);
    },
	k37_1_0:function(){
		if (this.editor) return false;
		this.selectCell(this.row,0,false,false,true);
		if (this.editor && this.editor.obj) this.editor.obj.select();
    },
	k39_1_0:function(){
		if (this.editor) return false;
		this.selectCell(this.row,this._cCount-1,false,false,true);
		if (this.editor && this.editor.obj) this.editor.obj.select();
    }, 
	k38_1_0:function(){
		//if (this.editor || !this.rowsCol.length) return;
		this.selectCell(this.rowsCol[0],this.cell._cellIndex,false,false,true);
		if (this.editor && this.editor.obj) this.editor.obj.select();
    },
	k40_1_0:function(){
		//if (this.editor || !this.rowsCol.length) return;
		this.selectCell(this.rowsCol[this.rowsCol.length-1],this.cell._cellIndex,false,false,true);
		if (this.editor && this.editor.obj) this.editor.obj.select();
    },
	k38_0_1:function(){
		//if (this.editor || !this.rowsCol.length) return;
		var rowInd = this.getRowIndex(this.row.idd);
		var nrow=this._nextRow(rowInd,-1);
		if (!nrow || nrow._sRow || nrow._rLoad) return false;
        this.selectCell(nrow,this.cell._cellIndex,false,false,true);
        if (this.editor && this.editor.obj) this.editor.obj.select();
    },
	k40_0_1:function(){
		//if (this.editor || !this.rowsCol.length) return;
		var rowInd = this.getRowIndex(this.row.idd);
		var nrow=this._nextRow(rowInd,1);
		if (!nrow || nrow._sRow || nrow._rLoad) return false;
        this.selectCell(nrow,this.cell._cellIndex,false,false,true);
        if (this.editor && this.editor.obj) this.editor.obj.select();
    },     
	k38_1_1:function(){
		//if (this.editor || !this.rowsCol.length) return;
		var rowInd = this.getRowIndex(this.row.idd);
		for (var i = rowInd; i >= 0; i--){
			this.selectCell(this.rowsCol[i],this.cell._cellIndex,true,false,true);
		}
    },
	k40_1_1:function(){
		//if (this.editor || !this.rowsCol.length) return;
		var rowInd = this.getRowIndex(this.row.idd);
		for (var i = rowInd+1; i <this.rowsCol.length; i++){
			this.selectCell(this.rowsCol[i],this.cell._cellIndex,true,false,true);
		}
    },                   
	k40_0_0:function(){
		if (this.editor && this.editor.combo)
			this.editor.shiftNext();
		else{
			//if (this.editor) return;
			if (!this.row.idd) return;
			var rowInd = rowInd=this.getRowIndex(this.row.idd)+1;
			if (this.rowsBuffer[rowInd]){
				var nrow=this._nextRow(rowInd-1,1);
				if (this.pagingOn && nrow) this.showRow(nrow.idd);
				this._Opera_stop=0;
                this.selectCell(nrow,this.cell._cellIndex,false,false,true);
            }
            else this._key_events.k34_0_0.apply(this,[]);
		}
		this._still_active=true;								
    },
    k36_0_0:function(){ //home
    	return this._key_events.k37_1_0.call(this);
    },
    k35_0_0:function(){ //ctrl-home
    	return this._key_events.k39_1_0.call(this);
    },            
    k36_1_0:function(){ //home
    	if (this.editor || !this.rowsCol.length) return false;
		this.selectCell(this.rowsCol[0],0,false,false,true);
		if (this.editor && this.editor.obj) this.editor.obj.select();
    },
    k35_1_0:function(){ //ctrl-end
    	if (this.editor || !this.rowsCol.length) return false;
		this.selectCell(this.rowsCol[this.rowsCol.length-1],this._cCount-1,false,false,true);
		if (this.editor && this.editor.obj) this.editor.obj.select();
    },  
    k33_0_0:function(){
    	if(this.pagingOn)
    		this.changePage(this.currentPage-1);
    	else this.scrollPage(-1);            		
    },
	k34_0_0:function(){
    	if(this.pagingOn)
    		this.changePage(this.currentPage+1);
    	else this.scrollPage(1);
    },                                  
	k38_0_0:function(){			
		if (this.editor && this.editor.combo)
			this.editor.shiftPrev();
		else{
			//if (this.editor) return;
			if (!this.row.idd) return;
			var rowInd = rowInd=this.getRowIndex(this.row.idd)+1;
			if (rowInd!=-1){
				var nrow=this._nextRow(rowInd-1,-1);
                this._Opera_stop=0;
                if (this.pagingOn && nrow) this.showRow(nrow.idd);
                this.selectCell(nrow,this.cell._cellIndex,false,false,true);
            }
			else this._key_events.k33_0_0.apply(this,[]);
		}
		this._still_active=true;
    }
// NEED TO TEST
//	k45_0_0:function(){// INSERT - add new line at current position
//		if (!this.editor) this.addRow((new Date()).valueOf(),[],this.row.rowIndex)
//    },
//	k46_0_0:function(){	//DEL - delete value of current cell on 
//		if (!this.editor) this.cells4(this.cell).setValue("");
//    }
};


/************ yet to be implemented ***************/
//  grid.enableLightMouseNavigation(true|false);
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
