// Grid Base Class
function XMLGrid()
{
    this.XMLString = "";
    this.XMLDataSource = null;
    this.SelectedRow = null;
    this.SelectedValue = null;
    this.ValueField = "";
    this.tmpValue = null;
	this.XMLLayout = "";
    
    // Handlers
    this.EditHandler = null;
    this.InsertHandler = null;
    this.UpdateHandler = null;
    this.SelectHandler = null;
    this.BeforeRowDrowing = null;
    this.AfterRowDrowing = null;
	this.AfterRowFormatting = null;
    this.RowFormating = null;
    this.CurrentEditRow = null;
	this.GetLoadParameterValue = null;
	
    // Layout Properties
	this.ControlID = '';
    this.PanelClass = '';
    this.SelectedRowClass = '';
    this.RowClass = '';
    this.AltRowClass = '';
    this.EditRowClass = '';
    this.HeaderClass = '';
    this.ShowHeader = true;
	this.GridRenderElement = 'table';
    this.RowRenderElement = 'tr';
	this.CellRenderElement = 'td';
	this.AllowNew = true;
	this.AllowEdit = true;
	this.InsertURL = "";
	this.UpdateURL = "";
	this.DeleteURL = "";	
	this.LoadURL = "";
	
    // Controls & Properties
    this.PanelID = '';
    this.Columns = null;	
	this.HTMLObject = null;
	this.RowElementName = "";	
	
    // Methods    
    
    this.StartEdit = function ()
		{
			var grd;
			var row;
			if (typeof(this.Grid) == 'undefined') // Grid
			{
				grd = this;
				row = grd.SelectedRow;
			}
			else // Row
			{
				row = this;
				grd = this.Grid;
			}
			
			if (grd.CurrentEditRow != null)
			{
				if (grd.CurrentEditRow.DataRow == null) // new row, check if no data was modified, then don't end edit
				{
					var RowModified = false;
					for (var i = 0; i < grd.Columns.length; i ++)
					{
						if (grd.Columns[i].ColumnName == '' || grd.Columns[i].NewControlName == 'select')
						    continue;
						if (grd.Columns[i].NewControl.GetValue() != grd.Columns[i].DefaultValue)
						{
							RowModified = true;
							break;
						}
					}
					if (RowModified)
					{
						grd.TryEndEdit();
						if (grd.InsertURL == "")
							grd.HTMLObject.removeChild(grd.HTMLObject.lastChild);
					}
					else
						grd.HTMLObject.removeChild(grd.HTMLObject.lastChild);
				}
				else
				{
					if (grd.UpdateURL == "")
					{
						var tmpRow = grd.CurrentEditRow;
						grd.CurrentEditRow = null;						
						grd.EndEdit(false, tmpRow);
					}
					else
						grd.TryEndEdit(grd.CurrentEditRow);
				}
			}
			grd.CurrentEditRow = row;
			
			while(row.childNodes.length != 0)
				row.removeChild(row.childNodes[0]);
			
			row.RenderRow();
			//grd.Columns[0].EditControl.focus();
		}
		
	this.TryEndEdit = function (row) // Called Before Postback, Performs Validation and Postback Actions
		{
			var grd;
			var row;
			if (typeof(this.Grid) == 'undefined') // Grid
			{
				grd = this;
				if (typeof(row) == 'undefined')
				    row = grd.CurrentEditRow;
			}
			else // Row
			{
				row = this;
				grd = this.Grid;
			}
			
			// Handle URL Insertion
			
			var validupdate = true;
			
			if (row.DataRow == null) // new Row
			{
				if (grd.InsertHandler != null)
					validupdate = grd.InsertHandler (row);
			}
			else // Existing Row, Updating
			{
				if (grd.UpdateHandler != null)
					validupdate = grd.UpdateHandler(row);
			}
			if (validupdate)
			{
			    var parray = grd.GetPostArray();
			    grd.RecievePostback.Grid = grd;
			    grd.RecievePostback.RowToUpdate = row;
				
				if (grd.FixPostArray != null)
					grd.FixPostArray(parray);
					
				if (row.DataRow == null)
				{
					if (grd.InsertURL != "")
						res = $.post(
							grd.InsertURL,
							parray, 
							grd.RecievePostback
							);
					else
					{
						row.DataRow = grd.XMLDataSource.createElement(grd.RowElementName);
						for (var i=0; i < grd.Columns.length; i++)
						{
							var col = grd.Columns[i];
							if (col.ReadOnly)
								continue;
							row.DataRow.setAttribute(col.ColumnName, col.NewControl.GetValue());
						}
						grd.EndEdit();
					}
                }
                else
                {
                    if (grd.UpdateURL != "")
						res = $.post(
							grd.UpdateURL,
							parray, 
							grd.RecievePostback
							);
					else
					{
						for (var i=0; i < grd.Columns.length; i++)
						{
							var col = grd.Columns[i];
							if (col.ReadOnly)
								continue;
							row.DataRow.setAttribute(col.ColumnName, col.EditControl.GetValue());
						}
						grd.EndEdit();
					}
                }
			}
		}
	
	this.RecievePostback = function(XMLResult)
		{
			// Result should be in this format 
			/*
				<result><account id='' aname='' aid='' /><error msg='' /></result>
				where "result" & "error" are constants
				"account" is the XMLElement name of the grid row
			*/
			//alert(XMLResult);
			
			XMLResult = ParseXMLText(XMLResult);
			if (XMLResult.getElementsByTagName('error').length == 0 || XMLResult.getElementsByTagName('error')[0].getAttribute('msg') == '')
				// Valid Update Happend
			{
				if (XMLResult.getElementsByTagName(this.success.Grid.RowElementName).length == 1) // Row Data Returned
				{
					var retrow = XMLResult.getElementsByTagName(this.success.Grid.RowElementName)[0];
					if (this.success.RowToUpdate.DataRow == null)
					    this.success.RowToUpdate.DataRow = grd.XMLDataSource.createElement(grd.RowElementName);
					
					for (var i = 0; i < this.success.Grid.Columns.length; i++)
					    if (this.success.Grid.Columns[i].ColumnName != '')
					        this.success.RowToUpdate.DataRow.setAttribute(this.success.Grid.Columns[i].ColumnName, retrow.getAttribute(this.success.Grid.Columns[i].ColumnName));
						
						/* if (this.success.RowToUpdate.DataRow == null) // New Row
							this.success.Grid.Columns[i].NewControl.SetValue(retrow.getAttribute(this.success.Grid.Columns[i].ColumnName));
						else
							this.success.Grid.Columns[i].EditControl.SetValue(retrow.getAttribute(this.success.Grid.Columns[i].ColumnName)); */
					if (this.success.Grid.CurrentEditRow == this.success.RowToUpdate)
					    this.success.Grid.EndEdit(true);
                    else
                        this.success.Grid.EndEdit(false, this.success.RowToUpdate);
				}
			}
			else
			{
				alert(XMLResult.getElementsByTagName('error')[0].getAttribute('msg'));
			}
		}
								
	
	this.EndEdit = function(AddNewRow, RowToUpdate) // Called After Postback has been done. Performs XML Update, Columns Values Update, Redraw rows
		{            
			var grd;
			var row;
			if (typeof(this.Grid) == 'undefined') // Grid
			{
				grd = this;
				if (typeof(RowToUpdate) == 'undefined')
                    row = RowToUpdate = grd.CurrentEditRow;
                else
                    row = RowToUpdate;
			}
			else // Row
			{
                row = RowToUpdate = this;                
				grd = this.Grid;
			}
			if (typeof(AddNewRow) == 'undefined')
				AddNewRow = true;
			// Handle URL Insertion
				var newrow = false;
			
				while(row.childNodes.length != 0)
					row.removeChild(row.childNodes[0]);
				if (AddNewRow && this.AllowNew)
					grd.StartNewRow();
				else
					grd.CurrentEditRow = null;
					
				row.className = row.OldClass;
				row.RenderRow();
				
		}
		
    this.SelectRow = function (Row) {
                        // Disable Event On Parent Elements
                        window.event.cancelBubble = true; 
                        
						if (typeof(Row) == 'undefined' || typeof(Row.altKey) != 'undefined' ) // Call From Row
						{
							Row = this;
							grd = this.Grid;
						}
						else // Call From Grid
						{
							grd = this;
						}
						
                        if (grd.SelectedRow != Row)
                        {
                            Row.className = grd.SelectedRowClass;    
                        }
						if (Row.DataRow != null && typeof (grd.ValueField) != 'undefined' && grd.ValueField != '')
							grd.SelectedValue = Row.DataRow.getAttribute(grd.ValueField);
                        
                        if (grd.SelectedRow != null && grd.SelectedRow != Row)
                            grd.SelectedRow.className = grd.SelectedRow.OldClass;
                        
                        grd.SelectedRow = Row;
                        if (grd.SelectHandler != null)    
                            grd.SelectHandler();    
                        
                    };
                    
    this.GetPostArray = function () {
					var params = new Object();
				
					for (var i = 0; i < this.Columns.length; i++)
					{
						if (this.Columns[i].ControlName == '' || this.Columns[i].ReadOnly == 'true' || this.Columns[i].ReadOnly == true)
						    continue;
						if (this.CurrentEditRow.DataRow == null) // new Row
							params[this.Columns[i].ControlName] = this.Columns[i].NewControl.GetValue();
						else
							params[this.Columns[i].ControlName] = this.Columns[i].EditControl.GetValue();
					}
					return params;
				}	
	
	this.LoadData = function()
	{
		var ParamNode = null;
		for (var i = 0; i < this.baseNode.childNodes.length; i++)
		{
			if (this.baseNode.childNodes[i].tagName == 'loadparameters')
			{
				ParamNode = this.baseNode.childNodes[i];
				break;
			}
		}
		
		var PostParameter = new Object();
		if (ParamNode != null && this.GetLoadParameterValue != null)
		{
			for (var i = 0; i < ParamNode.childNodes.length; i++)			
				if (typeof(ParamNode.childNodes[i].nodeName) != 'undefined' && ParamNode.childNodes[i].nodeName == '#text') // Fix Google Chrome;
					continue;
				else
				{
					var paramname = ParamNode.childNodes[i].getAttribute('paramname');
					PostParameter[paramname] = this.GetLoadParameterValue(paramname);					
				}			
		}
		
		this.DataLoaded = function (Data)
		{			
			this.success.grid.XMLString = Data;
			if (this.success.grid.DataReady != null)
			    this.success.grid.DataReady();
		}
		
		this.DataLoaded.grid = this;
		
		$.post(this.LoadURL, PostParameter, this.DataLoaded);				
	}
	
    this.GetGrid = function (TablenName)  // Returns Grid Control
        {
            if (this.XMLDataSource == null)
				this.XMLDataSource = ParseXMLText(this.XMLString);
            this.DataElement = this.XMLDataSource.getElementsByTagName(TablenName)[0];
            
            var BeforeDrowParameter;
            var odd = false;
            
            //var Panel = document.getElementById(this.PanelID);
            
            //Panel.Grid = this;
            var table = document.createElement(this.GridRenderElement);
			this.HTMLObject = table;
            //table.cellSpacing = "0";
            //table.cellPadding = "0";
            table.className = this.PanelClass;
			if (this.GridRenderElement == 'table')
			{
				var tbody = document.createElement('tbody');
				table.appendChild(tbody);
            }
            var tr;
            if (this.ShowHeader)
            {
                tr = document.createElement(this.RowRenderElement);
                tr.className = this.HeaderClass;
                
                var td;
                for (var j = 0; j < this.Columns.length; j++)
                {
                    if (this.Columns[j].Visible != false && this.Columns[j].Visible != "false")
                    {
                        td = document.createElement(this.CellRenderElement);
						td.className = this.Columns[j].HeaderClass;
                        td.innerText = this.Columns[j].Header;                        
                        tr.appendChild(td);
                    }
                }
				if (this.GridRenderElement == 'table')
					table.tBodies[0].appendChild(tr);
				else
					table.appendChild(tr);
            }
            //var cur = null;
            for (var i =0; this.DataElement != null && i < this.DataElement.childNodes.length; i++)
            {
                var curRow = this.DataElement.childNodes[i];
                BeforeDrowParameter = new DrowParameter();
                BeforeDrowParameter.Row = curRow;
                if (this.BeforeRowDrowing != null)
                    this.BeforeRowDrowing(BeforeDrowParameter);
                if (BeforeDrowParameter.Cancel)
                    continue;
                
                tr = new XMLGridRow(this, curRow);
				
				if (this.ValueField != '')
					tr.id = this.PanelID + curRow.getAttribute(this.ValueField);
				else
					tr.id = this.PanelID + '_' + i;
                
                tr.SetOddOrEven(odd);
                
                if (this.ValueField != '' && this.SelectedValue != null && curRow.getAttribute(this.ValueField) == this.SelectedValue)
                {
                    tr.className = this.SelectedRowClass;
                    //tr.OldClass = (odd? this.RowClass : this.AltRowClass);
                    this.SelectedRow = tr;
                }
                
                if (this.RowFormating != null)
                    RowFormating(tr)
                else
                    tr.RenderRow();
                
                if (this.AfterRowDrowing != null)
                    this.AfterRowDrowing(tr);
				
				if (this.GridRenderElement == 'table')
					table.tBodies[0].appendChild(tr);
				else
					table.appendChild(tr);
					
                odd = !odd;
            }
            //this.HTMLObject = table;
			// New Row
			if (this.AllowNew)
			{
				this.StartNewRow = function()
					{
						var tr = new XMLGridRow(this, null);						
						if (parseInt(this.HTMLObject.childNodes.length / 2) == this.HTMLObject.childNodes.length / 2)						
							tr.SetOddOrEven(true);
						else
							tr.SetOddOrEven(false);
						
						tr.className = this.SelectedRowClass;
						this.CurrentEditRow = tr;
						this.SelectedRow = tr;
						tr.RenderRow();						
						this.HTMLObject.appendChild(tr);
						if (this.AfterRowDrowing != null)
							this.AfterRowDrowing(tr);
						
					}
				this.StartNewRow();
			}
        }
		
	this.GetColumn = function (ColumnName)
	{
		for (var j = 0; j <this.Columns.length; j++)
			if (this.Columns[j].ColumnName == ColumnName)
				return this.Columns[j];
	}
    
	this.GetLayout = function()
		{
			if (typeof(this.XMLLayout) == "string")
				this.XMLLayout = ParseXMLText(this.XMLLayout);
			
			this.baseNode = this.XMLLayout.getElementsByTagName('grid')[0];
			this.ValueField = GetSubElementValue(this.baseNode, 'valuefield', '');
			this.ControlID = GetSubElementValue(this.baseNode, 'controlid', '');
			this.PanelClass = GetSubElementValue(this.baseNode, 'gridcss', '');
			this.SelectedRowClass = GetSubElementValue(this.baseNode, 'selectedcss', '');
			this.RowClass = GetSubElementValue(this.baseNode, 'rowcss', '');
			this.AltRowClass = GetSubElementValue(this.baseNode, 'altrowcss', '');
			this.EditRowClass = GetSubElementValue(this.baseNode, 'editrowcss', '');
			this.HeaderClass = GetSubElementValue(this.baseNode, 'headercss', '');			
			this.ShowHeader = GetSubElementValue(this.baseNode, 'showheader', true);
			this.GridRenderElement = GetSubElementValue(this.baseNode, 'gridelement', 'table');
			this.RowRenderElement = GetSubElementValue(this.baseNode, 'rowelement', 'tr');
			this.CellRenderElement = GetSubElementValue(this.baseNode, 'cellelemnt', 'td');
			this.AllowNew = GetSubElementValue(this.baseNode, 'allownew', true);
			this.AllowEdit = GetSubElementValue(this.baseNode, 'allowedit', true);
			this.InsertURL = GetSubElementValue(this.baseNode, 'inserturl', "");
			this.UpdateURL = GetSubElementValue(this.baseNode, 'updateurl', "");
			this.DeleteURL = GetSubElementValue(this.baseNode, 'deleteurl', "");
	        this.RowElementName = GetSubElementValue(this.baseNode, 'dataelement', "");
			this.LoadURL = GetSubElementValue(this.baseNode, 'loadurl', "");
			var columns = this.XMLLayout.getElementsByTagName('field');
			this.Columns = new Array();
			for (var i = 0; i < columns.length; i++)
			{
				this.Columns[i] = new GridColumn(this, columns[i]);
			}
		}
	
	this.LoadLayoutFromFile = function (fname)
		{
			if (window.XMLHttpRequest)
			{
				xhttp=new XMLHttpRequest();
			}
			else 
			{
				xhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			xhttp.open("GET", fname, false);
			xhttp.send();
			this.XMLLayout = xhttp.responseXML;
			this.GetLayout();
		}
}

function XMLGridRow(Grid, DataRow)
{
    if (typeof(DataRow) == 'undefined')
        DataRow = null;    
    var res = document.createElement(Grid.RowRenderElement);
    res.Grid = Grid;
    res.Select = res.onclick = Grid.SelectRow;
    res.StartEdit = Grid.StartEdit;				
    res.EndEdit = Grid.TryEndEdit;
    res.DataRow = DataRow;
    res.SetOddOrEven = function (odd)
        {
            this.Odd = odd;
            if (odd)
            {
                this.className = this.OldClass = Grid.RowClass;
            }
            else
            {
                this.className = this.OldClass  = Grid.AltRowClass;
            }
        }
    res.RenderRow = function ()
    {
        // There Will Be three cases, Row, New Row, Edit Row        
        if (this == this.Grid.CurrentEditRow)
            this.EditControls = new Array();
        else
            this.EditControls = null;
            
        for (var j = 0; j < this.Grid.Columns.length; j++)        
        {
            var col = this.Grid.Columns[j];
            td = document.createElement(this.Grid.CellRenderElement);
            if (this.Odd)
                td.className = col.RowClass;
            else
                td.className = col.AltRowClass;
            var control;
            
            if (this == this.Grid.CurrentEditRow) // May be New Row, Or Edit Row
            {
                this.ondblclick = this.Grid.TryEndEdit;
                if (this.DataRow == null) // New Row
                {
                    control = col.GetAddControl(this);
                    control.SetValue(col.DefaultValue);
                }
                else    // Edit Row
                    control = col.GetEditControl(this);
                td.appendChild(control.HTMLObject);
                this.EditControls[col.ColumnName] = control;
            }
            else // Row
            {
                control = col.GetControl(this, col.RenderControlName);
                this.ondblclick = this.Grid.StartEdit;
                td.appendChild(control.HTMLObject);
                //td.innerText = this.DataRow.getAttribute(col.ColumnName);                
            }
            
			this.appendChild(td);
            if (col.Visible == false || col.Visible == 'false')
                td.style.display = 'none';
        }
		
		if (this.Grid.AfterRowFormatting != null)
			this.Grid.AfterRowFormatting(this);
    }
        res.TryDelete = function()
        {
            if (this.Grid.DeleteURL == "")
                return;
            var params = this.GetAttributesObject();
            var validupdate = true;
            if (this.Grid.DeleteHandler != null)
                validupdate = this.Grid.DeleteHandler();
            if (validupdate)
			{
			    this.ReciveDeleteResult.Grid = this.Grid;
			    this.ReciveDeleteResult.Row = this;
                if (this.Grid.DeleteURL != "")
					res = $.post(
						this.Grid.DeleteURL,
						params,
						this.ReciveDeleteResult
						);
			}
        }
        
        res.ReciveDeleteResult = function(Result)
						{
							
						    XMLResult = ParseXMLText(Result);
			                if (XMLResult.getElementsByTagName('error').length == 0 || XMLResult.getElementsByTagName('error')[0].getAttribute('msg') == '')
				                // Valid Delete Happend
			                {
				                this.success.Row.parentElement.removeChild(this.success.Row);
			                }
			                else
			                {
				                alert(XMLResult.getElementsByTagName('error')[0].getAttribute('msg'));
			                }
						}
        
        res.GetAttributesObject = function()
        {
            if (this.DataRow == null)
                return null;
             var params = new Object();
				
			for (var i = 0; i < this.Grid.Columns.length; i++)
			{
				if (this.Grid.Columns[i].ControlName == '' || this.Grid.Columns[i].ReadOnly == 'true' || this.Grid.Columns[i].ReadOnly == true)
				    continue;
				params[this.Grid.Columns[i].ControlName] = this.DataRow.getAttribute(this.Grid.Columns[i].ColumnName);
			}
			return params;
        }
        
		res.GetCell = function (ColumnName)
		{
			for (var j = 0; j <this.Grid.Columns.length; j++)
				if (this.Grid.Columns[j].ControlID == ColumnName)
					return this.childNodes[j];
		}
		
    return res;
}

function GridColumn(Grid, XMLField)
{    
    this.ColumnName = GetSubElementValue(XMLField, 'name'); // Data Bound Property (name);
    this.Header = GetSubElementValue(XMLField, 'title', ''); // Header Text (title)
    this.Visible = GetSubElementValue(XMLField, 'visible', true); // visible    
    this.ReadOnly = GetSubElementValue(XMLField, 'readonly', false); // For Edit (readonly)
    
    this.RenderControlName = GetSubElementValue(XMLField, 'rowcontroltype', 'label'); // The Control That Will Appear, For Custom Types Use Control Name Directly Like 'XMLDropDownList' (controltype)
    this.EditControlName = GetSubElementValue(XMLField, 'editcontroltype', 'text'); // The Control That Will Appear, For Custom Types Use Control Name Directly Like 'XMLDropDownList' (controltype)
    this.NewControlName = GetSubElementValue(XMLField, 'newcontroltype', 'text'); // The Control That Will Appear, For Custom Types Use Control Name Directly Like 'XMLDropDownList' (controltype)
    
    this.NewControl = null; // The Control That Will Appear In New Mode
    this.EditControl = null; // The Control That Will Appear In Edit Mode
    this.Required = GetSubElementValue(XMLField, 'required', true); // To Enable Validation When Editing (required)
    this.ControlID = GetSubElementValue(XMLField, 'controlid', ""); // The id property Of The Control (when creating only), custom controls will not use this id (controlid)
    this.ControlName = GetSubElementValue(XMLField, 'controlname', ""); // The name property of the control (when creating only), custom controls will not use this id (controlname)
    //this.BoundProperty = GetSubElementValue(XMLField, 'boundproperty', "value"); // The property of the control to be bound (boundproperty)
	this.DefaultValue = GetSubElementValue(XMLField, 'defaultvalue', ""); //(defaultvalue)
	this.HeaderClass = GetSubElementValue(XMLField, 'headercss', "");	
	this.RowClass = GetSubElementValue(XMLField, 'rowcss', '');
	this.AltRowClass = GetSubElementValue(XMLField, 'altrowcss', '');
	this.XMLField = XMLField;	
	this.Grid = Grid;
		
    this.GetAddControl = function (row)
		{
//			if (this.NewControl != null)
//				return this.NewControl;
			
			var res = this.GetControl(row, this.NewControlName);
			this.NewControl = res;			
			res.SetValue(this.DefaultValue);
			return res;
		};
    
    this.GetEditControl = function (row)
    {
        // Means Start New Edit
		//if (this.EditControl != null)
				//return this.EditControl;
        var res = this.GetControl(row, this.EditControlName);
        this.EditControl = res;
        if (this.ColumnName != '')
            res.SetValue(row.DataRow.getAttribute(this.ColumnName));
        return res;
    };
    
    this.GetControl = function(row, ElementName)
        {
            var res = null;
		    switch(ElementName)
		    {
			    case 'text':					
				    res = new XMLText();				    
				    break;
				case 'textarea':					
				    res = new XMLTextArea();
				    break;
		        case 'hidden':
				    res = new XMLHidden();
				    break;
			    case 'button':
				    res = new XMLButton(this.XMLField);
				    break;
			    case 'check':
				    res = new XMLCheck();
				    break;				
                case 'edit':
                    res = new XMLEdit(this.XMLField);
                    res.CurrentRow = row;
                    res.onclick = function () 
                        { 
                            window.event.cancelBubble = true; 
                            this.CurrentRow.StartEdit(); 
                        };
                    break;
                case 'update':
                    res = new XMLUpdate(this.XMLField);
                    res.CurrentRow = row;
                    res.onclick = function () 
                        { 
                            window.event.cancelBubble = true; 
                            this.CurrentRow.EndEdit(); 
                        };
                    break;
                case 'delete':
                    res = new XMLDelete(this.XMLField);
                    res.CurrentRow = row;
                    res.onclick = function () 
                        {   
                            window.event.cancelBubble = true; 
                            this.CurrentRow.TryDelete(); };
                    break;
                case 'cancel':
                    res = new XMLCancel(this.XMLField);
                    res.CurrentRow = row;
                    res.onclick = function () 
                        { 
                            window.event.cancelBubble = true; 
                            this.CurrentRow.Grid.EndEdit(true); };                    
                    break;
                case 'none':
                    res = document.createElement('span'); //new XMLCancel(this.XMLField);
					res.SetValue = function (value)
						{
						}
					res.GetValue = function ()
						{ return ''; }						
                    res.CurrentRow = row;
					res.HTMLObject = res;
                    res.onclick = function () {window.event.cancelBubble = true; };
                    break;
				case 'select':
					res = new XMLSelect(this);
					break;
				case 'selectlabel':
					res = new XMLValuedLabel(this);
					res.SetValue (row.DataRow == null? '' : row.DataRow.getAttribute(this.ColumnName));
					break;
				case "link":
					res = new XMLLink(this.XMLField);					
					res.SetValue (row.DataRow == null? '' : row.DataRow.getAttribute(this.ColumnName));
					break;
                case 'label':
				default:
                    res = document.createElement('span');
                    res.innerHTML = (row.DataRow == null || this.ColumnName == '') ? '' : row.DataRow.getAttribute(this.ColumnName);
                    res.HTMLObject = res;
                    res.GetValue = function() { return this.innerText; };
                    res.SetValue = function(NewValue) { this.innerText = NewValue; }                    
                    break;
		    }		   
		    //res.ondblclick = function (){ window.event.cancelBubble = true; }; 
			res.id = GetSubElementValue(this.XMLField, 'controlid', '');
		    return res;		    
        };
}

function ParseXMLText(txt)
{
    if (typeof(txt) != "string")
		return txt;
	try //Internet Explorer
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.loadXML(txt);
        return xmlDoc; 
    }
    catch(e)
    {
        parser= new DOMParser();
        xmlDoc=parser.parseFromString(txt,"text/xml");
        return xmlDoc;
    }
}

function GetSubElementValue(XMLElement, SubElement, DefaultValue)
{
	if (typeof(DefaultValue) == 'undefined')
		DefaultValue = '';
	
	var curNode = null;
	
	for (var i = 0; i < XMLElement.childNodes.length; i++)
	{
		if (XMLElement.childNodes[i].tagName == SubElement)
		{
			curNode = XMLElement.childNodes[i];
			break;
		}
	}
	
	if (curNode == null || curNode.firstChild == null)
		return DefaultValue;
	return curNode.firstChild.nodeValue;
	
	/* var subelements = XMLElement.getElementsByTagName(SubElement);
	if (subelements.length != 1 || subelements[0].firstChild == null)
		return DefaultValue;	
	return subelements[0].firstChild.nodeValue; */
}

function DrowParameter()
{
    this.Cancel = false;
    this.Row = null;
}

