
  /////////////////////////////////////////////////////
  //                                                 //
  //          C-NOOFS Forecast Viewer v0.6           //
  //               Web User Interface                //
  //                                                 //
  /////////////////////////////////////////////////////

/**
 * Results Table Updater class
 * @param theTable The table to update
 * @param uig Source of user input (UserInputGroup object)
 */
ResultsTableUpdater = function(theTable, uig)
{
	this.table = new Object();
	this.uig = new Object();
	this.ResultSet = new Object();
	this.ResultSet.RecordSet = new Array();
	
	this.table = theTable;
	this.uig = uig;
	this.currentPage = 1;
}

ResultsTableUpdater.prototype.table;
ResultsTableUpdater.prototype.uig;
ResultsTableUpdater.prototype.ResultSet;

ResultsTableUpdater.prototype.currentPage;

ResultsTableUpdater.prototype.triggerUpdate = function()
{
	// Do the AJAX request
	var RTU = this;
	var DS = this.table.getTableConfig().DataSource;
	if ( !YAHOO.lang.isUndefined(DS) )
	{
		var XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ViewerRequest page=\"" + this.currentPage + "\" language=\"" + Lang.Code + "\">\n" + this.uig.getSelectedXML() + "</ViewerRequest>";
	 
		YAHOO.util.Connect.asyncRequest(DS.Method, DS.URI, 
		{
			success:function(o)
			{
				if ( o.responseXML != null )
				{
				 	var xmlDoc = o.responseXML;
				 	RTU.ResultSet = new Object();

				 	if ( o.responseText.length > 0 )
				 	{
					 	RTU.ResultSet = XMLDocumentParser.ParseViewerResponse(xmlDoc);
					}

				 	RTU._refreshDisplay();
				}
				else
				{
					alert("FAILURE: " + o.responseText);
				}

			 	if ( document.getElementById("ProcessingRequest") != null )
					document.body.removeChild(document.getElementById("ProcessingRequest"));
			},
			failure:function(o)
			{
				alert("FAILURE: " + o.responseText);
			 	if ( document.getElementById("ProcessingRequest") != null )
					document.body.removeChild(document.getElementById("ProcessingRequest"));
			},
			timeout:5000
		}, "request=" + encodeURIComponent(XML));
		
	}
}


ResultsTableUpdater.prototype._refreshDisplay = function()
{
	var tBody = this.table.getTableBody();
	
	// Clear it's contents
	tBody.removeAllRows();
	
	var i = 0;
	if ( !YAHOO.lang.isUndefined(this.ResultSet.RecordSet) )
	{
	 	if ( this.ResultSet.RecordSet.length > 0 )
	 	{
			for ( rsKey in this.ResultSet.RecordSet )
			{
				var item = this.ResultSet.RecordSet[rsKey];
				tBody.rowClass = ( i++ % 2 ) ? "odd" : "even";
				tBody.addRow(item,this);
			}
		}
		else
		{
			this.table.clearPagination();
			tBody.showEmptyBodyMessage();
		}

		this.table.clearPagination();
		this.table.updatePagination(this, this.ResultSet.CurrentPage, this.ResultSet.AvailablePages);
	}
	else
	{
		this.table.clearPagination();
		tBody.showEmptyBodyMessage();
	}
}


