/*
Sets up a Div dialog box...
*/
function Dialog (dlgId, cntrlId) {
	this.dlgId = dlgId;
	this.cntrlId = cntrlId;
	
	this.autoControl = false;
	this.openCommand = "edit >";
	this.closeCommand = "Close";
}

function _setAutoControl(flag) {
	var thisObj = this;
	this.autoControl = flag;
}
function _init() {
	this.closeDlg();
	this.updateControl();
	this.initActions();
}

function _initActions() {
	if (this.submitAction != null) {
		dlg = getElement(this.dlgId);
		var btn = createButton("submit" + this.dlgId, this.submitAction);
		var thisObj = this;
		var cb = callback (thisObj.submitDlg, {bind: thisObj});
		btn.onclick = cb;
		dlg.appendChild(btn);
	}
	if (this.cancelAction != null) {
		dlg = getElement(this.dlgId);
		var btn = createButton("cancel" + this.dlgId, this.cancelAction);
		var thisObj = this;
		var cb = callback (thisObj.cancelDlg, {bind: thisObj});
		btn.onclick = cb;
		dlg.appendChild(btn);
	}
}

function _setCallbackData(cbData) {
	this.cbData = cbData;
}

function _cancelDlg() {
	this.closeDlg();
}
function _closeDlg() {
	hide(this.dlgId);
	this.closed = true;
	this.updateControl();
}
function _submitDlg() {
	var closeNow = true;
	if (this.submitHandler) {
		// closeNow = this.submitHandler.call(null, this.cbData);
		closeNow = this.submitHandler.call(null, this.cbData);
	}
	if (closeNow == true || closeNow == undefined)
		this.closeDlg(this.cbData);
}
function _openDlg () {
	show (this.dlgId);
	this.closed = false;
	this.updateControl();	
}

function _updateControl() {
	if (this.autoControl == false)
		return;
	else {
		var thisObj = this;
		var el = getElement (this.cntrlId);
		if (this.closed) {
			// el.innerHTML = this.openCommand;
			el.onclick = callback (thisObj.openDlg, {bind: thisObj});	
		} else {
			// el.innerHTML = this.closeCommand;
			el.onclick = callback (thisObj.closeDlg, {bind: thisObj});	
		}
	}
}

function _setCallbacks(submitHandler, cancelHandler) {
	this.submitHandler = submitHandler;
	this.cancelHandler = cancelHandler;
}
function _addSubmitAction (val) {
	this.submitAction = val
}
function _addCancelAction (val) {
	this.cancelAction = val;
}

Dialog.prototype.init = _init;
Dialog.prototype.openDlg = _openDlg;
Dialog.prototype.closeDlg = _closeDlg;
Dialog.prototype.submitDlg = _submitDlg;
Dialog.prototype.cancelDlg = _cancelDlg;
Dialog.prototype.initActions = _initActions;
Dialog.prototype.setCallbacks = _setCallbacks;
Dialog.prototype.updateControl = _updateControl;
Dialog.prototype.setAutoControl = _setAutoControl;
Dialog.prototype.setCallbackData = _setCallbackData;
Dialog.prototype.addSubmitAction = _addSubmitAction;
Dialog.prototype.addCancelAction = _addCancelAction;


