/**
 * InputCondition
 * by Kramer 051229
 *
 * Conditionally removes or un-removes a node branch based on the value
 * of a given input element.
 *
 * @param conditional DOMNODE  The form element to monitor for changes
 * @param hider       DOMNODE  The element to add/remove based on conditional's value
 * @param conditions  OBJECT   A list of true/false conditions keyed on
 *                             possible input values.  'difetto' is a
 *                             reserved key name, indicating the default
 *                             shown/hidden state when the user's input
 *                             does not match any which are explicitly listed.
 *                             Condition keys are case-insensitive.
 *                             Example:
 *                               {
 *                                 difetto: false,
 *                                 yes: true,
 *                                 no: false,
 *                                 maybe: true
 *                               }
 *
 * @see assignEvent.js
 * @see closure.js
 */
function InputCondition(conditional, hider, conditions) {
	this.n_conditional   = conditional;             // The form element whose determines n_hider's presence
	this.n_hider         = hider;                   // The node to add/remove
	this.n_hiderClone    = null;                    // A clone of a "hidden" node
	this.n_placeHolder   = null;                    // A node put in place of the removed node
	this.s_conditions    = conditions;              // The conditions parameter.

	this.construct = function() {
		//if (!isanode(this.n_hider)) return this.error('Hider is not a node.');
		//if (!isanode(this.n_conditional)) return this.error('Hider is not a node.');
		// Hook el onchange
		assignEvent(this.n_conditional, 'change', this.onchange.closure(this));
		this.onchange();
	}

	this.onchange = function() {
		var newVal = this.n_conditional.value.toLowerCase();
		var add = (this.s_conditions[newVal]) ? true : false;
		if (this.s_conditions[newVal] && !this.n_hider) {  // If it should be present and it is removed...
			if (!isanode(this.n_placeHolder)) return this.error('n_placeHolder is not a DOM node');
			if (!isanode(this.n_placeHolder.parentNode)) return this.error('n_placeHolder.parentNode is not a DOM node');
			this.n_hider = this.n_hiderClone.cloneNode(true);
			this.n_placeHolder.parentNode.replaceChild(this.n_hider, this.n_placeHolder);
			this.n_hiderClone = null;
			this.n_placeHolder = null;
		} else if (!this.s_conditions[newVal] && this.n_hider) { // If it should be removed and it is not...
			this.n_placeHolder = document.createComment('InputCondition: a hider goes here');
			this.n_hiderClone = this.n_hider.cloneNode(true);
			this.n_hider.parentNode.replaceChild(this.n_placeHolder, this.n_hider);
			this.n_hider = null;
		}
	}

	this.error = function(text) {
		alert(text);
		throw('InputCondition(): '+text);
		return false;
	}

	this.construct();
}
