// Copyright (c) NimbleCat, 2008

/*
* this object implements a modal dialog
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("validatedmodaldialog.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("validatedmodaldialog.js requires the NC JavaScript framework");
 }
 
 /*
  * constructor
 	sourceUrl: The url to populate the target div with
 	parameters: an anonymous object with a parameter list for the get
 	targetDiv:  The div to populate
 	targetForm: The name of the form in which the user will enter information
 	updateFunc: If not null, the update function to invoke after transfering the variables. This function has one parameter, 
 				namely, the full url to be retrieved
 	updateUrl: The url to call on update
 */

   NC.widget.ValidatedModalDialog = function(sourceUrl, parameters, targetDiv, targetFormName, 
 						 updateFunc, updateUrl, updateTarget, validateFunc, errorDiv, showAsPopup) {
 		var fnSuperClass = NC.widget.ValidatedModalDialog.superclass.constructor;
		fnSuperClass.call( this, sourceUrl, parameters, targetDiv, targetFormName, showAsPopup);
 		this.updateFunc = updateFunc;
 		this.updateUrl = updateUrl;
 		this.updateTarget = updateTarget;
 		this.validateFunc = validateFunc;
 		this.errorDiv = errorDiv;
 	}
 
  NC.lang.extend(NC.widget.ValidatedModalDialog, NC.widget.ModalDialog, 
  {
 	
 	post: function() {
 		var element = document.getElementById(this.target);
 		var descendants = NC.util.Dom.descendants(element);
 		for (var i = 0; i < descendants.length; ++i) {   
 			var item = descendants[i];   
			if (item.name == this.targetFormName) {
				if (this.validateFunc != null && this.validateFunc( item, this.errorDiv)) {
					var parms = NC.util.Dom.serializeForm(item);
					if (this.updateFunc != null && this.updateUrl != null) {
						this.updateFunc( this.updateUrl, parms, this.updateTarget);
					}
					this.hide();
				}
				break;
			}
 		}
  	}
 
 }
 );
