// JScript File

function GridHelper()
{
	this.createGrid=function(columns,localData)
	{
		var localGrid = new Active.Controls.Grid;
		localGrid.setColumnProperty("count", columns.length);
		localGrid.setColumnProperty("texts", columns);
		localGrid.setRowProperty("count", localData.length);
		document.write(localGrid);
		localGrid.DataSource=localData;
		//createMsgDiv();
		return localGrid;
	}
	this.refresh=function(localGrid,localData)
	{
		localGrid.DataSource=localData;
		localGrid.setRowProperty("value", function(i){return i}); 
        localGrid.setRowProperty("order", function(i){return i}); 
		localGrid.setRowProperty("count", localData.length);
		//    clear selection model 
        localGrid.setSelectionProperty("index", -1); 

        //    clear sort model 
        localGrid.setSortProperty("index", -1); 
        localGrid.setSortProperty("direction", "none"); 
		localGrid.refresh();
	}
	
	// add double click event handle for grid
	this.setOnDblClickHandle=function (localGrid,handle)
	{
		var row = localGrid.getTemplate("row");
		row.setEvent("ondblclick", function(){this.action("myAction")});
		localGrid.setAction("myAction", function(src){ handle(src);});
	}
	
	//execute callback function with selected row
	this.doSomethingForOneRow=function(grid,callback)
	{
		var selected=grid.getSelectionProperty("Index");
	    if(selected.length>=0)
			callback(selected);
		else
			alert("请先选择一行再执行操作"); 
	}
	
}

var lastFoundWhat=""; //content what  last find;

//found something in grid
function findInGrid(grid,what,columnIndex)
{
	var start=0;
	if(grid.lastFoundIndex==null) grid.lastFoundIndex=-1;//execute finding firstly
	if(grid.lastFoundWhat==null) grid.lastFoundWhat="";

	//whether a new search starts?
	if(grid.lastFoundWhat!=what)
	{
		grid.lastFoundIndex=-1;
		grid.lastFoundWhat=what;
	}	
	
	//search from the next row;
	start=grid.lastFoundIndex - 1 +2;
	
	var thisFoundIndex=-1;
	for(var i=start;i<grid.DataSource.length;i++)
	{
		if(columnIndex==null) //not define the search column ,search all columns
		{
			var columnsLength=grid.getColumnProperty("count");
			for(var j=0;j<columnsLength;j++)
			{
				if(grid.DataSource[i][j]!=null && grid.DataSource[i][j].indexOf(what)>=0)
				{
					thisFoundIndex=i;
					break;
				}
			}
		}
		else//define the search column
			if(grid.DataSource[i][columnIndex]!=null && grid.DataSource[i][columnIndex].indexOf(what)>=0)
				{
					thisFoundIndex=i;
				}
		if(thisFoundIndex>=0)	break;
	}
	if(thisFoundIndex==-1 && grid.lastFoundIndex==-1)
		alert('没有查找到相匹配的结果');
	else if(thisFoundIndex==-1)
	{
		alert('已经查找到最后一行,没有找到其它相互匹配的记录!');
		thisFoundIndex=grid.lastFoundIndex;
	}
	grid.setProperty("selection/index", thisFoundIndex);
	grid.lastFoundIndex=thisFoundIndex;
}

//Grid find panel,which can search everything in grid.
function GridSearchPanel(container)
{
	this._container=$(container);
	this._container.obj=this;
	this._container.acc='abcde';
	this._grid=null;
	this.title="查找:";
	this.cssClass="";
	this._buttonType='image';
	
	this._label=null;
	this._textbox=null;
	this._button=null;
	this._searchColumn=null;
	
	//set target grid
	this.setTargetGrid=function(grid)
	{
		this._grid=grid;
	}
	
	//set target column index
	this.setTargetColumn=function(columnIndex)
	{
		this._searchColumn=searchColumn;
	}
	
	//create search panel,
	//argument :searchColumn search target column,if null that search all columns in grid
	this.create=function()
	{
		this._label = document.createElement("label");
		this._textbox=document.createElement("<input type='text' style='border:0;border-bottom:1px solid black'>");
		
		if(this._buttonType!='image')
			this._button=document.createElement("<input type='button' value='查 找' class='normalbutton' onclick='searchClick(this)'>");
		else
			this._button=document.createElement("<img title='查 找' src='../images/icon_search.gif' border=0  onclick='searchClick(this)'>");
		this._textbox.style.pixelWidth=100;
		//init label object
		this._label.htmlFor=this._textbox.id;
		this._label.innerHTML=this.title;
	
		//init button object

		this._container.appendChild(this._label);
		this._container.appendChild(this._textbox);
		this._container.appendChild(this._button);

	}
}

function searchClick(obj)
{
	var src=obj;
	var searchPanel=src.parentNode.obj;
	var searchWhat=searchPanel._textbox.value;
	if(searchWhat.length==0)
	{
		alert("请输入要查找的内容");
		searchPanel._textbox.focus();
	}
	if(searchPanel._grid==null) 
	{
		alert("Use setTargetGrid(grid) to set the target grid first ");
		return ;
	}
	findInGrid(searchPanel._grid, searchWhat,searchPanel._searchColumn);
}

if (!window.My) My=[]; 
if (!My.Templates) My.Templates=[]; 

My.Templates.Input = Active.Templates.Text.subclass(); 

My.Templates.Input.create = function() 
{ 
    var obj = this.prototype; 

//    editor is not part of the template, 
//    there is only one single instance of editor object. 
    var editor = new Active.HTML.INPUT; 
    editor.setClass("templates", "input"); 
    editor.setAttribute("type", "text"); 
    editor.setAttribute("value", function(){ 
        return template.getItemProperty("text"); 
    }); 

//    template variable provides temporary reference 
//    to the parent template during edit mode. 
    var template; 

    function switchToEditMode(){ 
        if (template) { 
            switchToTextMode() 
        } 
        template = this; 
        template.element().style.padding = 0; 
        template.element().innerHTML = editor; 
        editor.element().focus(); 
    } 

    obj.setEvent("ondblclick", switchToEditMode); 

    function switchToTextMode(){ 
        var value =    editor.element().value; 
        template.setItemProperty("text", value); 
        template.refresh(); 
        template = null; 
    } 

    editor.setEvent("onblur", switchToTextMode); 
}; 

My.Templates.Input.create(); 

My.Templates.Select = Active.Templates.Text.subclass();  
My.Templates.Select.prototype._options = new Array(); 
My.Templates.Select.create = function()  
{  
    var obj = this.prototype;  
    //------------------------------------------------------------  
    //Add the text value pair to the select dropdown list  
    //------------------------------------------------------------  
    obj.addOption = function( text, value )  
    {  
        this._options.push( new Array( value ? value : text, text) );  
    }  
    obj.clearOptions = function()  
    {  
        this._options = new Array();  
    }  
     
    obj.getOptions = function()  
    {  
        return this._options; 
    }  
    // editor is not part of the template,  
    // there is only one single instance of editor object.  
    var editor = new Active.HTML.DIV;  
    editor.setTag("select"); 
    editor.setClass("templates", "input");  
    editor.setAttribute("type", "text");  
    editor.setEvent("onblur", function(event) { this.switchToTextMode( event ); } );  
    editor.setContent( "options", function()  
    { 
        var text = template.getItemProperty("text"); 
        var inputOptions = obj._options; 
        var optionsHTML = new Array();  
        for( i=0; i<inputOptions.length; i++ ) 
        {  
            var oTag = new Active.System.HTML();  
            var val = inputOptions[i][0];  
            var txt = inputOptions[i][1]; 
            oTag.setTag("option");  
            oTag.setAttribute( "value", val );  
            oTag.setContent("text",inputOptions[i][1]);          
            if ( text==txt ) 
            { 
                oTag.setAttribute( "selected","true" );  
            }  
            optionsHTML.push( oTag );  
        }  
         
        return optionsHTML.join("");  
    });  
    // template variable provides temporary reference  
    // to the parent template during edit mode.  
    var template;  
    function switchToEditMode() 
    { 
        template = this;  
        template.element().style.padding = 0;  
        template.element().innerHTML = editor;  
        editor.element().focus();  
    }  
    obj.setEvent("ondblclick", switchToEditMode);  
    function switchToTextMode() 
    {  
        var originalText = template.getItemProperty("text"); 
        var value = editor.element().value;  
        var selectedIndex = editor.element().selectedIndex; 
        var text = editor.element().options[selectedIndex].text; 
        // we want to compare the text in the grid 
        // grid display only the text 
        if(originalText != text) 
        { 
            template.setItemProperty("text", text); 
            template.setItemProperty("value", value);  
            if(obj.onChangeEvent) 
            { 
                obj.onChangeEvent(); 
            } 
        } 
        template.refresh();  
        template = null;  
    } 
    obj.onChangeEvent = function() 
    { 
        // alert("User must override this function to recieve the events"); 
    } 
    editor.setEvent("onblur", switchToTextMode);  
};  
My.Templates.Select.create();  
