
YAHOO.widget.DataView = function(elContainer, aColumnDefs, oDataSource, oConfigs)
	{
	if (arguments.length > 0)
		{
		YAHOO.widget.DataView.superclass.constructor.call(this, elContainer , aColumnDefs , oDataSource , oConfigs);
		}
	};

// Inherit from YAHOO.widget.DataTable
YAHOO.lang.extend(YAHOO.widget.DataView, YAHOO.widget.DataTable);

YAHOO.widget.DataView.prototype.isFiltered=false;

YAHOO.widget.DataView.prototype.filterRows=function(filteredRows)
	{
    if(filteredRows == undefined)
    	{
        this.initializeTable();
        this.addRows(this.defaultView);
        this.isFiltered=false;
    	}
    	else
    	{
        var dataView=[];
        for (var i=0; i<filteredRows.length;i++)
        	{
        	var r=filteredRows[i];
			var row= this.getRecordSet().getRecord(r).getData();
            dataView.push(row);
			}
   		 this.initializeTable();
   		 if(filteredRows.length==0)
   		 	{
   		 	this.configs.MSG_EMPTY = "No rows match. Leave at least one option checked in each box, and/or try a different keyword.";
   		 	this.addRows([]);
   		 	} else {
   		 	this.hideTableMessage();
   		 	this.addRows(dataView);
   			}
         this.isFiltered=true;
    	};
    xtCloud.loList.CalcRows();
    //this.renderPaginator();
	};

YAHOO.widget.DataView.prototype.FilterSingle=function(sQuery,sColumnKey)
	{
	this.ClearFilters();
	sQuery=unescape(sQuery);
	var aResults = [];
	var rs=this.getRecordSet()

	if(sQuery && sQuery.length > 0)
		{
		var q= sQuery.toLowerCase();
		for (var i=0; i<rs.getLength(); i++)
			{
			var field=this.getRecord(i).getData(sColumnKey);
			if(!field) { xtCloud.log("Could not find text field",true); return(false); };
			var mIndex=field.toLowerCase().indexOf(q)
			if (mIndex<0) { continue;}
			aResults.push(i); //matched Row index
			}
		}
	this.filterRows(aResults)
	};
	
YAHOO.widget.DataView.prototype.FilterAll=function(sColumnKey,txtArr,filArr)
	{
	this.ClearFilters();
	var aResults = [];
	var rs=this.getRecordSet();
	var rsL = rs.getLength();
	for (var i=0; i<rsL; i++)
		{
		var x = i;
		var included = true;
		for(var j=0; j<txtArr.length; j++)
			{
			if(!included) continue;
			var q = unescape(txtArr[j]);
			if(q=="") continue;
			q = q.toLowerCase();
			var field=this.getRecord(i).getData(sColumnKey);
	    	if(!field) { xtCloud.log("Could not find wing field",true); return(-2); };
			var mIndex=field.toLowerCase().indexOf(q);
			if (mIndex==-1) included=false;
			}
		for(var j=0; j<filArr.length; j++)
			{
			if(!included) continue;
			var q = filArr[j];
			if(q===true) continue;
			if(q===false) continue;
			var field=this.getRecord(i).getData(xtCloud.wings[j].columnKey);
			if(!field) { xtCloud.log("Could not find field "+xtCloud.wings[j].columnKey,true); return(-2); };
			var matchField = false;
			for(var k=0; k<q.length; k++)
				{
				if(field==q[k]) { matchField = true; break; }
				}
			if(!matchField) included=false;
			};
		if(included)
			{
			//Save the index of the match
			aResults.push(i);
			}
		}
	this.filterRows(aResults);
	var res = aResults.length;
	if(res==rsL) res = -1; // indicates no filtering
	return(res);
	};

YAHOO.widget.DataView.prototype.ClearFilters=function()
	{
	this.initializeTable();
	this.addRows(this.defaultView);
	this.render();
	this.isFiltered=false;
	}

YAHOO.widget.DataTable.prototype.onEventSelectRow = function(oArgs)
	{
	var sMode = "xt";
    if ((sMode == "singlecell") || (sMode == "cellblock") || (sMode == "cellrange")) { return; }

    var evt = oArgs.event;
    var elTarget = oArgs.target;

    var bSHIFT = evt.shiftKey;
   // var bCTRL = evt.ctrlKey || ((navigator.userAgent.toLowerCase().indexOf("mac") != -1) && evt.metaKey);
   	var bCTRL = true;
    var i;
    //var nAnchorPos;

    // Validate target row
    var elTargetRow = this.getTrEl(elTarget);
    if(elTargetRow) {
        var nAnchorRecordIndex, nAnchorTrIndex;
        var allRows = this._elTbody.rows;
        var oTargetRecord = this.getRecord(elTargetRow);
        var nTargetRecordIndex = this._oRecordSet.getRecordIndex(oTargetRecord);
        var nTargetTrIndex = this.getTrIndex(elTargetRow);

        var oAnchorRecord = this._oAnchorRecord;
        if(oAnchorRecord) {
            nAnchorRecordIndex = this._oRecordSet.getRecordIndex(oAnchorRecord);
            nAnchorTrIndex = this.getTrIndex(oAnchorRecord);
            if(nAnchorTrIndex === null) {
                if(nAnchorRecordIndex < this.getRecordIndex(this.getFirstTrEl())) {
                    nAnchorTrIndex = 0;
                }
                else {
                    nAnchorTrIndex = this.getRecordIndex(this.getLastTrEl());
                }
            }
        }

        // Both SHIFT and CTRL
        if((sMode != "single") && bSHIFT && bCTRL) {
            // Validate anchor
            if(oAnchorRecord) {
                if(this.isSelected(oAnchorRecord)) {
                    // Select all rows between anchor row and target row, including target row
                    if(nAnchorRecordIndex < nTargetRecordIndex) {
                        for(i=nAnchorRecordIndex+1; i<=nTargetRecordIndex; i++) {
                            if(!this.isSelected(i)) {
                                this.selectRow(i);
                            }
                        }
                    }
                    // Select all rows between target row and anchor row, including target row
                    else {
                        for(i=nAnchorRecordIndex-1; i>=nTargetRecordIndex; i--) {
                            if(!this.isSelected(i)) {
                                this.selectRow(i);
                            }
                        }
                    }
                }
                else {
                    // Unselect all rows between anchor row and target row
                    if(nAnchorRecordIndex < nTargetRecordIndex) {
                        for(i=nAnchorRecordIndex+1; i<=nTargetRecordIndex-1; i++) {
                            if(this.isSelected(i)) {
                                this.unselectRow(i);
                            }
                        }
                    }
                    // Unselect all rows between target row and anchor row
                    else {
                        for(i=nTargetRecordIndex+1; i<=nAnchorRecordIndex-1; i++) {
                            if(this.isSelected(i)) {
                                this.unselectRow(i);
                            }
                        }
                    }
                    // Select the target row
                    this.selectRow(oTargetRecord);
                }
            }
            // Invalid anchor
            else {
                // Set anchor
                this._oAnchorRecord = oTargetRecord;

                // Toggle selection of target
                if(this.isSelected(oTargetRecord)) {
                    this.unselectRow(oTargetRecord);
                }
                else {
                    this.selectRow(oTargetRecord);
                }
            }
        }
        // Only SHIFT
        else if((sMode != "single") && bSHIFT) {
            this.unselectAllRows();

            // Validate anchor
            if(oAnchorRecord) {
                // Select all rows between anchor row and target row,
                // including the anchor row and target row
                if(nAnchorRecordIndex < nTargetRecordIndex) {
                    for(i=nAnchorRecordIndex; i<=nTargetRecordIndex; i++) {
                        this.selectRow(i);
                    }
                }
                // Select all rows between target row and anchor row,
                // including the target row and anchor row
                else {
                    for(i=nAnchorRecordIndex; i>=nTargetRecordIndex; i--) {
                        this.selectRow(i);
                    }
                }
            }
            // Invalid anchor
            else {
                // Set anchor
                this._oAnchorRecord = oTargetRecord;

                // Select target row only
                this.selectRow(oTargetRecord);
            }
        }
        // Only CTRL
        else if((sMode != "single") && bCTRL) {
            // Set anchor
            this._oAnchorRecord = oTargetRecord;

            // Toggle selection of target
            if(this.isSelected(oTargetRecord)) {
                this.unselectRow(oTargetRecord);
            }
            else {
                this.selectRow(oTargetRecord);
            }
        }
        // Neither SHIFT nor CTRL and "single" mode
        else if(sMode == "single") {
            this.unselectAllRows();
            this.selectRow(oTargetRecord);
        }
        // Neither SHIFT nor CTRL
        else {
            // Set anchor
            this._oAnchorRecord = oTargetRecord;

            // Select only target
            this.unselectAllRows();
            this.selectRow(oTargetRecord);
        }

        // Clear any selections that are a byproduct of the click or dblclick
        var sel;
        if(window.getSelection) {
        	sel = window.getSelection();
        }
        else if(document.getSelection) {
        	sel = document.getSelection();
        }
        else if(document.selection) {
        	sel = document.selection;
        }
        if(sel) {
            if(sel.empty) {
                sel.empty();
            }
            else if (sel.removeAllRanges) {
                sel.removeAllRanges();
            }
            else if(sel.collapse) {
                sel.collapse();
            }
        }
    }
    else {
        YAHOO.log("Could not select row " + elTarget, "warn", this.toString());
    }
	};
