// Javascript functions for form dependencies

// This version goes with /codelib/js/ilr/form/validation/*


// Hide a div, clear any user input from form elements inside the div, and make the fields
// optional for validation purposes.
// It is necessary to clear user input for 2 reasons: (1) Form input may get inadvertently 
// submitted if it remains in the form. (2) This input should not reappear in the fields if the div is unhidden.
// divs is a single div element, the id of a single div element, or an array of div elements/ids
// NB There are cases where you want to simply hide a div and leave the form input intact. Example:
// on failed credit card transaction, return to the form but hide all fields except the credit card
// fields. Those fields should remain filled for submission. In that case, call addClass() instead of
// hideFields().
function hideFields(divs) {	

	addClass(divs, "hidden");
	clearAndUnrequireFields(divs);
}

function unhideFields(divs) {

	removeClass(divs, "hidden");
	require(divs);
}

function clearAndUnrequireFields(divs) {

	var fields = getFormFields(divs);
	for (i = 0; i < fields.length; i++) {
		reset(fields[i]);
		unrequireField(fields[i]);
	}
}


// Restore the default optionality value. If this is not defined, make it a required field.
// Used, for example, when hiding input fields.
function require(divs) {

	var fields = getFormFields(divs);
	for (var i = 0; i < fields.length; i++) {
		requireField(fields[i]);
	}
}

// Make the form elements in divs optional. Used, for example, when unhiding divs.
function unrequire(divs) {

	var fields = getFormFields(divs);
	for (var i = 0; i < fields.length; i++) {
		unrequireField(fields[i]);
	}
}

// Restore the default optionality value of a field when unhiding the enclosing div. 
// If this is not defined, make it a required field.
// RY 1/25/08 Adapted to new form validation strategy. Both properties optional and
// required will get set in both cases, but one is used in the old strategy and one
// in the new.
function requireField(f) {
	
	// Old form validation strategy	
	if (typeof f.defaultOptional != "undefined") {
		setFormFieldProperty(f, "optional", f.defaultOptional);
	}
	else {
		setFormFieldProperty(f, "optional", false);
	}
	
	// New form validation strategy
	if (typeof f.defaultRequired != "undefined") {
		setFormFieldProperty(f, "required", f.defaultRequired);
	}
	else {
		setFormFieldProperty(f, "required", "true");
	}
}

// Unrequire a field when hiding the enclosing div.
function unrequireField(f) {
	setFormFieldProperty(f, "optional", true);   // old form validation strategy
	setFormFieldProperty(f, "required", false);  // new form validation strategy
}

// Works for text input, selects. Not sure about radio buttons/checkboxes.
function copyInput(from, to) {

	var source, target, i;
	
	from = toArray(from);
	to = toArray(to);
	
	for (i = 0; i < from.length; i++) {
		source = getElement(from[i]);
		target = getElement(to[i]);
		target.value = source.value;
	}
}