/************************************************************************
JavaScript.net
File: System.Data.js
Author: Scott Wood
Version: 2.4.0.0
Description: Provides data manipulation objects
This software is provided under the Open Software License v. 2.1 Agreement
*************************************************************************/
using('System.Diagnostics');
using('System.Collections');
namespace('System.Data',2,4,0,0);
System.Data.DataTable = function(name)
{
    this.name = name || '';
	ColumnCollection.inherit(System.Collections.ArrayList);
    this.columns = new ColumnCollection(this);
    RowsCollection.inherit(System.Collections.ArrayList);
    this.rows = new RowsCollection(this);
    with(System.Data)
	{
		if(!DataTable.initialize())
		{
			
		}
	}
    function ColumnCollection(dataTable)
    {
        this.table = dataTable
		if(!ColumnCollection.initialize())
		{
			ColumnCollection.prototype.add = function(name)
			{
				var result = this.base.add.call(this,new Column(this,this.count,name));
				for(var index = 0; index < this.table.rows.count; index++)
				{
					this.table.rows[index][this[this.count - 1].name] = null;
				}				
				return result;				
			}
			ColumnCollection.prototype.removeAt = function(index)
			{
				for(var $index = 0; $index < this.table.rows.count; $index++)
				{
					delete this.table.rows[$index][this[index].name];
				}
				this.base.removeAt.call(this,index);
			}
			ColumnCollection.prototype.remove = function(name)
			{
				for(var index = 0; index < this.count;index++)
				{
					if(this[index].name == name)
					{
						this.removeAt(index);
						break;
					}
				}
			}
		}
        function Column(container,index,name)
        {
            this.table = container.__container;
            this.index = index;
            this.name = name;
        }
    }
    function RowsCollection(dataTable)
    {
		this.table = dataTable;
		if(!RowsCollection.initialize())
		{
			RowsCollection.prototype.newRow = function()
			{
				return new Row(this);
			}
		} 
        function Row(rowsCollection)
        {
            this.table = rowsCollection.table;
            for(var index = 0;index < this.table.columns.count; index++)
            {
                this[this.table.columns[index].name] = null;
            }
        }
    }
}
System.Data.DelimitedDataAdapter = function(request,row,column)
{
    this.request = request;
    this.row = row;
    this.column = column;
    with(System.Data)
    {
		if(!DelimitedDataAdapter.initialize())
		{
			DelimitedDataAdapter.prototype.fill = function(dataTable)
			{
				var document = this.request.send(System.Http.RequestMethod.Get);
				if(document){
					var records = document.split(this.row);
					if(records.length > 0)
					{
						var columns = records[0].split(this.column);
						for(var index = 0; index < columns.length; index++)
						{
							dataTable.columns.add(columns[index].replace(/^\s+/g, '').replace(/\s+$/g, ''));
						}
						for(var recordIndex = 1; recordIndex < records.length; recordIndex++)
						{
							var columns = records[recordIndex].split(this.column);
							var row = dataTable.rows.newRow();
							for(var columnIndex = 0;columnIndex < dataTable.columns.count; columnIndex++)
							{
								if(columns[columnIndex] != 'null')
								{
									row[dataTable.columns[columnIndex].name] = columns[columnIndex]; 
								}
							}
							dataTable.rows.add(row);
						}
					}
				}
			}
		} 
	}
}
System.Data.XmlDataAdapter = function(request,row,column)
{
	this.request = request;
	this.row = row || '*';
	this.column = column || '*';
	if( document.implementation.hasFeature("XPath", "3.0") )
	{
	   // prototying the XMLDocument
	   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	   {
		  if( !xNode ) { xNode = this; } 
		  var oNSResolver = this.createNSResolver(this.documentElement)
		  var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
					   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		  var aResult = [];
		  for( var i = 0; i < aItems.snapshotLength; i++)
		  {
			 aResult[i] =  aItems.snapshotItem(i);
		  }
		  return aResult;
	   }
	
	   // prototying the Element
	   Element.prototype.selectNodes = function(cXPathString)
	   {
		  if(this.ownerDocument.selectNodes)
		  {
			 return this.ownerDocument.selectNodes(cXPathString, this);
		  }
		  else{throw "For XML Elements Only";}
	   }
	}
	with(System.Data)
	{
	
		if(!XmlDataAdapter.initialize())
		{

			XmlDataAdapter.prototype.fill = function(dataTable)
			{
				var document = this.request.send(System.Http.RequestMethod.Get);		
				if(document)
				{
					var records = document.documentElement.selectNodes(this.row);
					if(records && records.length > 0)
					{
						var columns = records[0].selectNodes(this.column);
						for(var index = 0; index < columns.length; index++)
						{
							dataTable.columns.add(columns[index].nodeName);
						}
						for(var recordIndex = 0; recordIndex < records.length; recordIndex++)
						{
							var row = dataTable.rows.newRow();
							var columns = records[recordIndex].selectNodes(this.column);
							for(var columnIndex = 0;columnIndex < dataTable.columns.count; columnIndex++)
							{
								row[dataTable.columns[columnIndex].name] = (columns[columnIndex].text) ? columns[columnIndex].text : columns[columnIndex].textContent;
							}
							dataTable.rows.add(row);
						}
					}
				}
			}
		}
	}
}
