var dlgStack = new Array();
var activeDlg = null;
var overlayMan = new YAHOO.widget.OverlayManager();

YAHOO.namespace("CommSoft.widget");

YAHOO.CommSoft.widget.CSDialog = function(el, userConfig)
{
	var div = $(el);
	if (div.parentNode != document.body)
		{
			div.parentNode.removeChild(div);
			document.body.appendChild(div);
		}
	div.style.top = "0px";
	div.style.display = "block";
	YAHOO.CommSoft.widget.CSDialog.superclass.constructor.call(this, el, userConfig);
	this.initCS();
};

YAHOO.extend(YAHOO.CommSoft.widget.CSDialog, YAHOO.widget.Dialog, {
	tables: [],

	handleSubmit: function()
	{
		return false;
	},
	
	handleCancel: function()
	{
		return true;
	},

	initCS: function()
	{
		overlayMan.register(this);
		
		this.showMaskEvent.subscribe(function() {
			YAHOO.util.Dom.addClass(this.element, "yui-panel-modal");
			var behind = this.getDialogBehind();
			if (behind != null) behind.hideMask();
		}, this, true);
		this.hideMaskEvent.subscribe(function() {
			YAHOO.util.Dom.removeClass(this.element, "yui-panel-modal");
			var behind = this.getDialogBehind();
			if (behind != null) behind.hideMask();
		}, this, true);
	},
	
	getDialogBehind: function()
	{
		if (this.isActiveDialog())	
			if (dlgStack.length > 1)
				return dlgStack[dlgStack.length - 2];
			else
				return null;
		else
			return null;
	},
	
	showCS: function()
	{
		this.show();
		dlgStack.push(this);
		activeDlg = this;
		overlayMan.bringToTop(this);
	},
	
	hideCS: function()
	{
		if (this.isActiveDialog())
			{
				dlgStack.pop();
				if (dlgStack.length > 0)
					activeDlg = dlgStack[dlgStack.length - 1];
				else
					activeDlg = null;
			}
		this.hide();
	},
	
	cancelCS: function()
	{
		if (this.isActiveDialog())
			{
				dlgStack.pop();
				if (dlgStack.length > 0)
					activeDlg = dlgStack[dlgStack.length - 1];
				else
					activeDlg = null;
			}
		this.cancel();
	},
	
	submitCS: function()
	{
		if (this.isActiveDialog())
			{
				dlgStack.pop();
				if (dlgStack.length > 0)
					activeDlg = dlgStack[dlgStack.length - 1];
				else
					activeDlg = null;
			}
		this.submit();
	},

	isActiveDialog: function()
	{
		return this == activeDlg;
	},

	hasActiveCellEditor: function()
	{
		for (var i=0; i<this.tables.length; i++)
			{
				var ce = this.tables[i].myDataTable._oCellEditor;
				if (ce && ce.isActive)
					return true;
			}
		return false;
	},
	
	setupTables: function(tables)
	{
		this.tables = tables;
	},

	enterPressed: function()
	{
		if (this.isActiveDialog())
			if (!this.hasActiveCellEditor())
				if (this.handleSubmit())
					this.submitCS();
	},

	escPressed: function()
	{
		if (this.isActiveDialog())
			if (!this.hasActiveCellEditor())
				if (this.handleCancel())
					this.cancelCS();
	},

	getData: function () {
	
			var oForm = this.form,
					aElements,
					nTotalElements,
					oData,
					sName,
					oElement,
					nElements,
					sType,
					sTagName,
					aOptions,
					nOptions,
					aValues,
					oOption,
					sValue,
					oRadio,
					oCheckbox,
					i,
					n;    

			if (oForm) {
	
					aElements = oForm.elements;
					nTotalElements = aElements.length;
					oData = {};

	
					for (i = 0; i < nTotalElements; i++) {
	
							sName = aElements[i].name;
	
							/*
									Using "Dom.getElementsBy" to safeguard user from JS 
									errors that result from giving a form field (or set of 
									fields) the same name as a native method of a form 
									(like "submit") or a DOM collection (such as the "item"
									method). Originally tried accessing fields via the 
									"namedItem" method of the "element" collection, but 
									discovered that it won't return a collection of fields 
									in Gecko.
							*/
	
							if (typeof sName != "undefined" && sName != "")
								{
									oElement = aElements[sName];
									if (typeof oElement.tagName != "undefined")
										oElement = [oElement];
									else if (typeof oElement.length == "undefined")
										oElement = [oElement];
								}
							else
								oElement = [];
							nElements = oElement.length;
	
							if (nElements > 0) {
	
									if (nElements == 1) {
	
											oElement = oElement[0];
	
											sType = oElement.type;
											sTagName = oElement.tagName.toUpperCase();
	
											switch (sTagName) {
	
											case "INPUT":

													if (sType == "checkbox") {

															oData[sName] = oElement.checked;

													}
													else if (sType != "radio") {

															oData[sName] = oElement.value;

													}

													break;

											case "TEXTAREA":

													oData[sName] = oElement.value;

													break;

											case "SELECT":

													aOptions = oElement.options;
													nOptions = aOptions.length;
													aValues = [];

													for (n = 0; n < nOptions; n++) {

															oOption = aOptions[n];

															if (oOption.selected) {

																	sValue = oOption.value;

																	if (!sValue || sValue === "") {

																			sValue = oOption.text;

																	}

																	aValues[aValues.length] = sValue;

															}

													}

													oData[sName] = aValues;

													break;
	
											}
	
	
									}
									else {
	
											sType = oElement[0].type;
	
											switch (sType) {
	
											case "radio":

													for (n = 0; n < nElements; n++) {

															oRadio = oElement[n];

															if (oRadio.checked) {

																	oData[sName] = oRadio.value;
																	break;

															}

													}

													break;

											case "checkbox":

													aValues = [];

													for (n = 0; n < nElements; n++) {

															oCheckbox = oElement[n];

															if (oCheckbox.checked) {

																	aValues[aValues.length] = 
																			oCheckbox.value;

															}

													}

													oData[sName] = aValues;

													break;
	
											}
	
									}
	
							}
	
					}
	
			}
 
			return oData;
	
	}
});
