DialogController = {
	findFirstForm: function() {
		var forms = $$('form');
		if (forms && forms.length > 0) {
			return forms[0];
		}

		return null;
	},

	init: function() {
		Form.focusFirstElement(this.findFirstForm());
	},

	adjustButton: function(theButton, adjust) {
		if (adjust == 'notVisible') {
			theButton.setStyle({ visibility: 'hidden' });
		}
		else if (adjust == 'visible') {
			theButton.setStyle({ visibility: 'visible' });
			theButton.enable();
		}
		else if (adjust == 'disabled') {
			theButton.setStyle({ visibility: 'visible' });
			theButton.disable();
		}
	},

	adjust: function(back, next, done, cancel) {
		this.adjustButton($('back'),   back);
		this.adjustButton($('next'),   next);
		this.adjustButton($('done'),   done);
		this.adjustButton($('cancel'), cancel);
	},

	submit: function(action) {
		var theForm = this.findFirstForm();
		if (theForm.onsubmit != null) {
			result = theForm.onsubmit();
			if (result != null && ! result) {
				return false;
			}
		}

		theForm.action.value = action;
		theForm.submit();
	}

}
    
function closeWindow() {
	KWindow.close();
}

function parentRefresh() {
	if(parent.opener) {
		parent.opener.location.href=parent.opener.location.href;		
	}
}
 
function submitForm(theForm, action) {
	theForm.action.value = action;
	if (theForm.onsubmit != null) {
	  result = theForm.onsubmit();
	  if (result != null && ! result) {
	    return false;
	  }
	}
	theForm.submit();
}

//DOM-ready watcher
function dataPanelOnLoad() {
  //start or increment the counter
  this.n = typeof this.n == 'undefined'? 0: this.n + 1;
  
  if (typeof(parent.frames[0]) != "undefined") {
    if (typeof(parent.frames[0].document) != "undefined") {
      if (typeof(parent.frames[0].document.getElementsByTagName) != 'undefined') {
        if (parent.frames[0].document.getElementsByTagName('body')[0] != null || parent.frames[0].document.body != null) {
          if (parent.frames[0].document.body.onload != null) {
            aFunc = eval(parent.frames[0].document.body.onload);
            aFunc();
          }
          return;
        }
      }
    }
  }
  if (this.n < 60) {
    //restart the watcher
    //using the syntax ('domReady()', n) rather than (domReady, n)
    //because the latter doesn't work in Safari 1.0
    setTimeout('dataPanelOnLoad()', 250);
  }
};

function enableControls() {
	//whenever the data panel loads, enable the buttons on the control panel
	if (typeof(parent.frames[1]) != "undefined") {
	    if (typeof(parent.frames[1].document) != "undefined") {
		    if (typeof(parent.frames[1].document.getElementsByTagName) != 'undefined') {
					if(typeof enableForm != 'undefined') {
						enableForm(parent.frames[1], "this.document.forms[0]");						
		        return;
					}
		    }
	    }
	}
}
