﻿/*********************************************************************************************/
/*********************************************************************************************/
// CUSTOM EXCELL
/*********************************************************************************************/
/*********************************************************************************************/

/*********************************************************************************************/
// BUTTON CELL
/*********************************************************************************************/
function eXcell_button(cell){
    if (cell){
        this.cell = cell;
        this.grid = this.cell.parentNode.grid;
    }
    this.edit = function(){}
    this.isDisabled = function(){ return true; }
    this.setValue=function(val){

        var row_id=this.cell.parentNode.idd;
        var fnVal = val.replace(/\s/g, '');
        if (fnVal=='') {
            this.setCValue('');
        } else {
            this.setCValue('<input type="button" value="'+val+'" class="dGridButton" onclick="javascript:'+this.grid.xId+'.xDefOnColumnButtonClick(\''+row_id+'\',\''+fnVal+'\');">',val);
        }
    }
}
eXcell_button.prototype = new eXcell;    // nest all other methods from base class


/*********************************************************************************************/
// CLONE CELL
/*********************************************************************************************/
/**
*   @desc: gets clone mould object of specified column. Use it to store copy of object to clone.
*   @type: public
*   @param: col_ind - index of the column to get clone mould object for
*/
dhtmlXGridObject.prototype.getClone=function(col_ind){
	if (!this.clones[col_ind]){
		this.clones[col_ind] = new dgridCloneObject();
	}
	return this.clones[col_ind];
};

/**
*   @desc: clones row
*   @type: public
*   @param: rowId - ID of the row to clone
*/
dhtmlXGridObject.prototype.bindRowCloneCells=function(rowId){

	this.forEachCell(rowId,function(cellObj){
	    if (cellObj.grid.clones[cellObj.cell._cellIndex]) {
	        cellObj.bind();
	    }
	});

	return;
};

/**
	*	@desc: clone object constructor (shouldn't be accessed directly - instead please use getClone(...) method of the grid)
	*	@type: private
	*	@returns: clone for dhtmlxGrid/dgrid
	*/
function dgridCloneObject(){
    this.cloneId;
	this.original;
	this.shape; // currently supported shapes: INPUT, RADIO, CHECKBOX, SELECT, TEXTAREA, DCOMBO
	/**
	*	@desc: copies object specified by id for later cloning
	*	@type: public
	*	@param: id - id of object to use as a original for cloning (string)
	*/
	this.copy=function(id){
        //this.original = document.getElementById(id); 
        this.original = document.getElementById(id); 

        // check if dcombo
        if (!this.original) {
            if (window["dCombo"]) {
                if (dCombo.xInstances[id]) {
                    this.original = dCombo.xInstances[id];
                    this.shape = "DCOMBO";
                }
            }
        }

        if (!this.original) {
            alert('Cannot copy object: ' + id); 
            return;
        }

        if (!this.shape) {
            switch (this.original.tagName.toUpperCase()) {
                case "INPUT":
                    switch (this.original.type.toUpperCase()) {
                        case "TEXT":
                            this.shape = "INPUT";
                            break;
                        case "RADIO":
                            this.shape = "RADIO";
                            this.original = document.all(id);
                            break;
                        case "CHECKBOX":
                            this.shape = "CHECKBOX";
                        default:
                            break;
                    }
                    break;
                case "SELECT":
                    this.shape = "SELECT";
                    break;
                case "TEXTAREA":
                    this.shape = "TEXTAREA";
                    break;
                default:
                    break;
            }
        }
        
	    this.cloneId = id;
        
        
	};
	/**
	*	@desc: clones object based on object type and returns dgrid usable value object {val: value, html: html to display}
	*	@type: public
	*	@param: val
	*	@returns: grid usable HTML
	*/
	this.clone=function(val){
        if (!this.original) { alert('Cannot clone object: ' + this.cloneId); return; }
        var obj = this.original;
        
        switch (this.shape) {
            case "INPUT":
                return {val: obj.value, html: obj.value};
                break;
                
            case "RADIO":
                var rval='';
                var eLength = obj.length;
                if(eLength == undefined) {
                    rval = (obj.checked)?obj.value:'';
                } else {
                    for(var idx = 0; idx < eLength; idx++) {
                        if (obj[idx].checked==true) {
                            rval = obj[idx].value;
                            break;
                        }
                    }
                }
                return {val: rval, html: rval};
                break;
                
            case "CHECKBOX":
                var rval = (obj.checked)?'Y':'';
                return {val: rval, html: rval};
                break;
                
            case "SELECT":
                var rval;
                if (obj.selectedIndex) {
                    rval = obj.options[obj.selectedIndex].value;
                    html = obj.options[obj.selectedIndex].text;
                } else {
                    rval = "";
                    html = "";
                }
                return {val: rval, html: html};
                break;
                
            case "TEXTAREA":
                return {val: obj.value, html: obj.value};
                break;
                
            case "DCOMBO":
                var rval;
                rval = (obj.getActualValue()||'');
                html = obj.getComboText();
                return {val: rval, html: html};
                break;
                
            default:
                break;
        }
		return;
	}

	/**
	*	@desc: for combo objects, copies and returns combo data
	*	@type: public
	*	@param: 
	*/
	this.getComboData=function(){
        if (!this.original) { alert('Cannot get combo object: ' + this.cloneId); return; }
        var obj = this.original;
        var id  = this.cloneId;
        var objData = obj.options;

        // check if dcombo
//        if (window["dCombo"]) {
//            if (dCombo.xInstances[id]) {
//            }
//        }

        var data=[];
        var idx=0;
        
        for (var i=0; i<objData.length; i++) {
            if (objData[i].text.toUpperCase()=='-- SELECT --') {
                data[idx] = [objData[i].value,''];
            } else {
                data[idx] = [objData[i].value,objData[i].text];
            }
            idx++;
        }
        
		return data;
	};
	/**
	*	@desc: updates value of and binds cell to and target object so that cell value is updated when target object is modified
	*	@type: public
	*	@param: cell (object)
	*	@returns: grid usable HTML
	*/
	this.bind=function(cell){
        if (!cell) { alert('Cannot bind, cell is missing.'); return; }
        if (!this.original) { alert('Cannot bind, original object is invalid.'); return; }

        // unbind previously bound cells
        $(this.original).unbind('.dgridClone');
        
        switch (this.shape) {
            case "INPUT":
                // update current value of cell
                var original = this.original;
                setTimeout(function(){
                    $(original).val(cell.getValue());
                },1);
                
                // bind to target object
                $(this.original).bind('keydown.dgridClone change.dgridClone', {clone: this}, function(event){
                    setTimeout(function(){
                        var rval = event.data.clone.clone();

		                if (rval.val != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }

                        cell.setValue(rval.html);
                    },1);
                });

//                $(this.original).bind('blur.dgridClone', {clone: this}, function(event){
//                    setTimeout(function(){
//                        cell.grid.editStop();
//                    },1);
//                });

                break;
                
            case "RADIO":
                // update current value of cell
                var original = this.original;
                setTimeout(function(){
                    var cellval=cell.getValue();
                    var eLength = original.length;
                    if(eLength == undefined) {
                        original.checked = (original.value == cellval);
                    } else {
                        for(var idx = 0; idx < eLength; idx++) {
                            original[idx].checked = false;
                            if(original[idx].value == cellval) {
                                original[idx].checked = true;
                            }
                        }
                    }
                },1);

                // bind to target object
                $(this.original).bind('change.dgridClone click.dgridClone', {clone: this}, function(event){
                    setTimeout(function(){
                        var rval = event.data.clone.clone();
		                if (rval.val != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }
                        cell.setValue(rval.html);
                    },1);
                });
                
                break;
                
            case "CHECKBOX":
                // update current value of cell
                var original = this.original;
                setTimeout(function(){
                    original.checked=(cell.getValue()=='Y')?true:false;
                },1);
                
                // bind to target object
                $(this.original).bind('click.dgridClone', {clone: this}, function(event){
                    setTimeout(function(){
                        var rval = event.data.clone.clone();
		                if (rval.val != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }
                        cell.setValue(rval.html);
                    },1);
                });
                break;
                
            case "SELECT":
                // update current value of cell
                var original = this.original;
                setTimeout(function(){
                    $(original).val(cell.getValue());
                },1);

                // bind to target object
                $(this.original).bind('change.dgridClone', {clone: this}, function(event){
                    setTimeout(function(){
                        var rval = event.data.clone.clone();
//                        cell.setActValue(rval.val);
//                        cell.setValue(rval.html);
		                if (rval.val != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }
                        cell.setValue(rval.val);
                    },1);
                });
                
                break;
                
            case "TEXTAREA":
                // update current value of cell
                var original = this.original;
                setTimeout(function(){
                    $(original).val(cell.getValue());
                },1);
                
                // bind to target object
                $(this.original).bind('keydown.dgridClone change.dgridClone', {clone: this}, function(event){
                    setTimeout(function(){
                        var rval = event.data.clone.clone();
		                if (rval.val != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }
                        cell.setValue(rval.html);
                    },1);
                });
                break;
                
            case "DCOMBO":
                var original = this.original;

                // unbind previously bound cells
                if (self.dComboOnChangeEvtId!=undefined) {
                    this.original.detachEvent(dComboOnChangeEvtId);
                }
                if (self.dComboOnSelectionChangeEvtId!=undefined) {
                    this.original.detachEvent(dComboOnSelectionChangeEvtId);
                }

                // update current value of cell
                setTimeout(function(){
                    original.setComboValue(cell.getValue());
                    original.setComboText(cell.getText());
                },1);
                
                // bind to target object
                dComboOnChangeEvtId = original.attachEvent("onChange", function(){
                    setTimeout(function(){
		                if ((original.getActualValue()||'') != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }
                        cell.setActValue((original.getActualValue()||''));
                        cell.setValue(original.getComboText());
                    },1);
                });
                dComboOnSelectionChangeEvtId = original.attachEvent("onSelectionChange", function(){
                    setTimeout(function(){
		                if ((original.getActualValue()||'') != cell.getValue()) {
		                    var rId = cell.cell.parentNode.idd;
                            cell.grid.xSetRowIsChanged(rId);
		                }
                        cell.setActValue((original.getActualValue()||''));
                        cell.setValue(original.getComboText());
                    },1);
                });
                break;
                
            default:
                break;
        }        
		return;
	}
// TODO: utilize this to make it easier to use the clone columns,
// consider automating the setting focus to the orginal object when edit mode is initiated in the grid
// to see what's possible so far, 
// 1. uncomment the following function
// 2. uncomment eXcell_clone.edit()
// 3. comment eXcell_clone.isDisabled()
// 4. uncomment the binding function for the blur event for INPUT
//	/**
//	*	@desc: sets focus to original object
//	*	@type: public
//	*	@param: 
//	*	@returns: 
//	*/
//	this.focus=function(){
//        if (!this.original) { alert('Invalid original object: ' + this.cloneId); return; }
//        var obj = this.original;
//        
//        switch (this.shape) {
//            case "INPUT":
//                obj.focus();
//                obj.select();
//                break;
//                
//            case "RADIO":
//                var rval='';
//                var eLength = obj.length;
//                if(eLength == undefined) {
//                    rval = (obj.checked)?obj.value:'';
//                } else {
//                    for(var idx = 0; idx < eLength; idx++) {
//                        if (obj[idx].checked==true) {
//                            rval = obj[idx].value;
//                            break;
//                        }
//                    }
//                }
//                return {val: rval, html: rval};
//                break;
//                
//            case "CHECKBOX":
//                var rval = (obj.checked)?'Y':'N';
//                return {val: rval, html: rval};
//                break;
//                
//            case "SELECT":
//                var rval;
//                if (obj.selectedIndex) {
//                    rval = obj.options[obj.selectedIndex].value;
//                    html = obj.options[obj.selectedIndex].text;
//                } else {
//                    rval = "";
//                    html = "";
//                }
//                return {val: rval, html: html};
//                break;
//                
//            case "TEXTAREA":
//                return {val: obj.value, html: obj.value};
//                break;
//                
//            case "DCOMBO":
//                var rval;
//                rval = (obj.getActualValue()||'');
//                html = obj.getComboText();
//                return {val: rval, html: html};
//                break;
//                
//            default:
//                break;
//        }
//		return;
//	}	

	return this;
}

// clone cell
function eXcell_clone(cell){
    if (cell){
        this.cell = cell;
        this.grid = this.cell.parentNode.grid;
		this.combo=(this.cell._combo||this.grid.getCombo(this.cell._cellIndex));
    }
    this.val = null;
    this.edit = function(){
//        this.grid.clones[this.cell._cellIndex].focus();
    };
    this.isDisabled = function(){ return true; };
    this.setActValue=function(val){
        this.cell._val = val;
    };
    this.setValue=function(val){

//        var row_id=this.cell.parentNode.idd;
//        this.setCValue(val);
//        this.cell._val = val;

	    if ((val||"").toString()._dhx_trim() == "")
		    val=null
	    this.cell.combo_value=val;
    	
	    if (val !== null)
		    this.setCValue((this.cell._combo||this.grid.getCombo(this.cell._cellIndex)).get(val)||val, val);
	    else
		    this.setCValue("&nbsp;", val);

    };
    this.getValue=function() {
		return ((this.cell.combo_value == window.undefined) ? "" : this.cell.combo_value);
        //return ((this.cell._val == window.undefined) ? "" : this.cell._val);
    };
    this.getText=function(){
	    return this.cell.innerHTML._dhx_trim();
    }
    this.bind=function() {
        this.grid.clones[this.cell._cellIndex].bind(this);
    };
}
eXcell_clone.prototype = new eXcell;    // nest all other methods from base class

