// Copyright (c) NimbleCat, 2008

/*
* this object implements a modal dialog
* 
* */
// make sure that the required includes are there
 if (typeof NC == 'undefined') {
       alert("xfermodaldialog.js requires the NC JavaScript framework");
 }
 
 if (typeof NC.widget == 'undefined') {
       alert("xfermodaldialog.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
 	xferForm: The name of the form to transfer the variables to
 	updateFunc: If not null, The update function to invoke after transfering the variables. This function has one parameter, 
 				namely, the name of the div to be updated
 	updateTarget: The div to update with the results provided by the updateUrl
 	*/
  NC.widget.XferModalDialog = function(sourceUrl, parameters, targetDiv, targetFormName, xferFormName, 
 						 updateFunc, updateTarget) {
 		var fnSuperClass = NC.widget.XferModalDialog.superclass.constructor;
		fnSuperClass.call( this, sourceUrl, parameters, targetDiv, targetFormName, true);
 		this.xferFormName = xferFormName;
 		this.updateFunc = updateFunc;
 		this.updateTarget = updateTarget;
 	}

  NC.lang.extend(NC.widget.XferModalDialog, NC.widget.ModalDialog, 
  {
 	
 	post: function() {
 		var element = document.getElementById(this.target);
 		var children = NC.util.Dom.descendants(element);
 		for (var i = 0; i < children.length; ++i) {   
 			var item = children[i]; 
			if (item.name == this.targetFormName) {
    			var xferForm = document.forms[this.xferFormName];
				var controls = item.elements;
				for (var ci = 0; ci < controls.length; ci++) {
					// now transfer the value of this control to a similarly named element
					// in the xferForm
					// first get the element whose value is to be set
					if (controls[ci] != 'undefined' && controls[ci].name != 'undefined' && controls[ci].name.length > 0) {
						for (var j = 0; j < xferForm.elements.length; j++) {
							var element = xferForm.elements[j];
							if (element != 'undefined' && element.name != 'undefined' && element.name == controls[ci].name) {
								element.value = NC.util.Dom.getValue(controls[ci]); // set the base control value
							}
						}
					}
				}
				this.updateFunc( this.xferFormName, this.updateTarget);
			}
 		}
 		this.hide();
 	}
 
 }
 );
 