if(typeof dataTable == "undefined") {
	var dataTable = function() {
		this.dtID;
		this.allowEdit;
		this.allowNew;
		this.allowDelete;
		this.ColumnDefs;
		this.DataSource;
		this.Config;
		this.dataURL;
		this.dialog;
		this.redirectURL;
        this.autoRefresh;
		
		var dataTableObj;
		var dialogObj;
		var newButtonObj;
		var delButtonObj;
		var loadingObj;
		var savingObj;
		var errorObj;

		this.render = function() {
			dataTableObj = new YAHOO.widget.DataTable(this.dtID, this.ColumnDefs, this.DataSource, this.Config);
			var me = this;

			if (this.allowNew == true) {
				newButtonObj = new YAHOO.widget.Button("newButton_" + this.dtID);
				newButtonObj.addListener("click", function(o){
					newDialog(me);
				});
			}
			
			if (this.allowDelete == true) {
				delButtonObj = new YAHOO.widget.Button("delButton_" + this.dtID);
				delButtonObj.addListener("click", function(o){
					delRecord(me);
				});
			}
										
			if(this.allowEdit == true) {
				var updateDataTable = this.updateDataTable;
				var delRecord = this.delRecord;
				var dataURL = this.dataURL;
				var dtID = this.dtID;
				var openDialog = this.openDialog;
				var newDialog = this.newDialog;
				var redirectURL = this.redirectURL;
				
				loadingObj = new YAHOO.widget.SimpleDialog("dlg_loading_" + this.dtID, { 
					width: "20em", 
					fixedcenter:true,
					modal:true,
				    visible:false,
					close: false,
					draggable:false });
				loadingObj.setHeader("Please Wait");
				loadingObj.setBody("Please wait while the selected item is loaded...");
				loadingObj.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
				loadingObj.render(document.body);	
				
				savingObj = new YAHOO.widget.SimpleDialog("dlg_saving_" + this.dtID, { 
					width: "20em", 
					fixedcenter:true,
					modal:true,
				    visible:false,
					close: false,
					draggable:false });
				savingObj.setHeader("Please Wait");
				savingObj.setBody("Please wait while the selected item is saved...");
				savingObj.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
				savingObj.render(document.body);
				
				errorObj = new YAHOO.widget.SimpleDialog("dlg_saving_" + this.dtID, { 
					width: "20em", 
					fixedcenter:true,
					modal:true,
				    visible:false,
					close: true,
					draggable:false });
				errorObj.setHeader("Error");
				errorObj.setBody("An unknown error has occurred");
				errorObj.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
				var handleOK = function() {this.hide();}
				var errorButtons = [ { text:"OK", handler:handleOK, isDefault: true } ];
				errorObj.cfg.queueProperty("buttons", errorButtons);
				errorObj.render(document.body);															
				
				dialogObj = new YAHOO.widget.Dialog("editDialog_" + this.dtID, {fixedcenter:false, close:false});
				dialogObj.cfg.queueProperty("buttons", [ 
					{ text:"Submit", 
					  handler:function() {
					  	savingObj.show();
						if(window.presubmit) window.presubmit();
						this.submit();
					  }, 
					  isDefault:true
					},
					{ text:"Cancel", 
					  handler:function() {
					  	if (redirectURL == "") {
							this.cancel();
						} else {
							window.location.href = redirectURL;
						}
					  }
					} 
				]);
				dialogObj.callback.success = function(o) {
					if (o.responseText.indexOf("Error") > -1) {
						// There was an error
						savingObj.hide();
						errorObj.setBody(o.responseText);
						errorObj.render(document.body);
						errorObj.show();
					}
					else {
						if (redirectURL == "") {
							updateDataTable(dataURL);
							savingObj.hide();
						}
						else {
							window.location.href = redirectURL;
						}
					}
				};
				dialogObj.callback.failure = function(o) {
					savingObj.hide();
					errorObj.setBody("Form could not be submitted");
					errorObj.render(document.body);
					errorObj.show();
				};
				dialogObj.callback.upload = function(o) {
					if (o.responseText.indexOf("Error") > -1) {
						// There was an error
						savingObj.hide();
						errorObj.setBody(o.responseText);
						errorObj.render(document.body);
						errorObj.show();
					}
					else {
						if (redirectURL == "") {
							updateDataTable(dataURL);
							savingObj.hide();
						}
						else {
							window.location.href = redirectURL;
						}
					}
				};	
				
				dataTableObj.set("selectionMode","single");
				dataTableObj.subscribe("rowClickEvent", dataTableObj.onEventSelectRow);
				dataTableObj.subscribe("rowDblclickEvent", function(o) {
					openDialog(me);
				});
			}
		}
						
		this.openDialog = function(dt) {
			loadingObj.show();
			var row = dataTableObj.getSelectedRows();
			var record = dataTableObj.getRecord(row[0]);
			var id = record.getData("id");
			
			var callback =
			{
				success: function(o) {
					var body = o.responseText;

					dialogObj.setBody(body);
					dialogObj.render(document.body);
					dialogObj.show();
					loadingObj.hide();
					//dialogObj.bringToTop();
										
					/* This runs any SCRIPT tags found in the dialog
					 * I don't really know how to make this global.
					 * todo: move to a global location.
					 */
					var node = document.createElement("div");
					node.innerHTML = body;
					var scripts = node.getElementsByTagName("script");
					for(var x = 0; x < scripts.length; x++) {
						try {
							eval(scripts[x].innerHTML);
						} catch(e) {
							
						}
					}
					
				},
				
				failure: function(o) {
					loadingObj.hide();
					errorObj.setBody(o.statusText);
					errorObj.render(document.body);
					errorObj.show(); 
				},
				
				cache: false
			}			

			var transaction = YAHOO.util.Connect.asyncRequest("GET", "index.php?dialog=" + dt.dialog + "&id=" + id, callback, null);
		}
		
		this.newDialog = function(dt) {	
			loadingObj.show();
			var callback =
			{
				success: function(o) {
					var body = o.responseText;
					
					dialogObj.setBody(body);
					dialogObj.render(document.body);
					dialogObj.show();
					loadingObj.hide();
					//dialogObj.bringToTop();
					
					/* This runs any SCRIPT tags found in the dialog
					 * I don't really know how to make this global.
					 * todo: move to a global location.
					 */
					var node = document.createElement("div");
					node.innerHTML = body;
					var scripts = node.getElementsByTagName("script");
					for(var x = 0; x < scripts.length; x++) {
						eval(scripts[x].innerHTML);
					}
				},
				
				failure: function(o) { 
					errorObj.setBody(o.statusText);
					errorObj.render(document.body);
					errorObj.show(); 
				},
				
				cache: false
			}			
			
			var transaction = YAHOO.util.Connect.asyncRequest("GET", "index.php?dialog=" + dt.dialog + "&id=", callback, null);
		}
		
		this.delRecord = function(dt) {
			var row = dataTableObj.getSelectedRows();
			if(row != "") {
				var record = dataTableObj.getRecord(row[0]);
				var id = record.getData("id");
	
				if(confirm("Are you sure?")) {
					var callback =
					{
						success: function(o) { dt.updateDataTable(dt.dataURL); },
						failure: function(o) { }, 					
						cache: false
					}			
					
					var transaction = YAHOO.util.Connect.asyncRequest("GET", "index.php?dialog=" + dt.dialog + "&action=delete&id=" + id, callback, null);
				}
			} else {
					errorObj.setBody("No Record Selected");
					errorObj.render(document.body);
					errorObj.show(); 
			}
		}
		
		this.updateDataTable = function(dataURL) {
			if(dataTableObj) {
                dataTableObj.initializeTable();
                var oCallback = {
                    success : dataTableObj.onDataReturnSetRows,
                    failure : dataTableObj.onDataReturnSetRows,
                    scope : dataTableObj
                };
                dataTableObj.getDataSource().sendRequest(dataURL, oCallback);
            }
		}

		this.refresh = function() {
			this.updateDataTable(this.dataURL);
		}	
				
	}
}
if(typeof dataTables == "undefined") {
	var dataTables = [];
}