/*

Reconfigures a form by hiding/showinf elements, depending on the selection of radio buttons

Requires:
	JQuery 1.2.2 (jquery_1-2-2.js)

Changelog:
	2008-02-15 Thomas Hooper:
	- Added select box support

	2008-02-01 Thomas Hooper:
	- Initial code
*/

function FormFlow(toggleFields) {

	var toggleFieldArray = new Array();

	for (var i = 0; i < toggleFields.length; i++) {
		if ($(toggleFields[i].controlSelector).length > 0 && $(toggleFields[i].fieldSelector).length > 0) {
			toggleFieldArray.push(new toggleField(toggleFields[i].controlSelector, toggleFields[i].fieldSelector, toggleFields[i].showOnSelect, toggleFields[i].initFunction, toggleFields[i].changeFunction));
		}

		globalCheck();
	}


	function toggleField(controlSelector, fieldSelector, showOnSelect, initFunction, changeFunction) {

		var jqControls = $(controlSelector);

		jqControls.each(function(i){

			var jqControl = $(this);

			if (jqControl.is("input[type=radio]")) {
				// Get other radio buttons in the same group
				var jqControlSet = $("input[name="+jqControl.attr("name")+"]");

			} else if (jqControl.is("option")) {
				// Get the parent select
				var jqControlSet = jqControl.parents("select").eq(0);
			}

			jqControlSet.keyup(function(){
				globalCheck();
			});
			jqControlSet.change(function(){
				globalCheck();
			});
			jqControlSet.click(function(){
				globalCheck();
			});
		});



		var jqField = $(fieldSelector);

		var check = function() {
			var jqControl;
			var selectedSelector;

			jqControls.each(function(i){
				var jqControlTemp = $(this);
				var selectedSelectorTemp = jqControlTemp.is("input[type=radio]") ? ":checked" : ":selected";
				if (jqControlTemp.is(selectedSelectorTemp) == showOnSelect) {
					jqControl = jqControlTemp;
					selectedSelector = selectedSelectorTemp;
				}
			});

			if (jqControl) {
				if (changeFunction) {
					changeFunction(jqField, true, jqControl)
				} else {
					$(fieldSelector).show();
				}

			} else {
				if (changeFunction) {
					changeFunction(jqField, false)
				} else {
					$(fieldSelector).hide();
				}
			}

		}

		if (initFunction) {
			initFunction(jqField);
		}

		var init = {
			check:check
		}
		return init
	}

	function globalCheck() {
		for(var i = 0; i < toggleFieldArray.length; i++) {
			toggleFieldArray[i].check();
		}
	}
}


// Initialise
$(document).ready(function(){

	/* Allow a radio button to hide/show other fields

	FormFlow(
		Array of radio button/field associations [{
			controlSelector: jQuery selector for the radio button control <input>,
			fieldSelector: jQuery selector for the elements to hide,
			showOnSelect: true or false; weather the area is shown or hidden when the radio button is selected,
			initFunction: function(jqField) {} action to preform on the field when the form loads
			changeFunction: function(jqField, show) {} by default the field will be shown/hidden, but a custom behaviour can be defined here. The jQuery field element is passed, along with true for 'show' and false for 'hide'
		}]
	)

	*/
	FormFlow([{
		/* Request Form - Telephone? */

			controlSelector: "input[value=Call][name=request-method], input[value=Meeting][name=request-method]",
			fieldSelector: ".row:has(#telephone)",
			showOnSelect: true,
			initFunction: function(jqField) {
				// Remove info span
				jqField.find(".required-info").remove();
			},
			changeFunction: function(jqField, show, jqControl) {
				// Add / Remove required span
				if (show) {
					// Add required span
					if (jqField.find(".required").length == 0) {
						jqField.find("input").eq(0).after("<span class='required'> * </span>");
					}
				} else {
					// Remove required span
					jqField.find(".required").remove();
				}
			}
		},{
		/* Request Form - Receive Method? */

			controlSelector: "input[value=Further Information][name=request-method]",
			fieldSelector: "fieldset:has([name=receive-method])",
			showOnSelect: true,
			initFunction: function(jqField){
				// Remove info span
				jqField.find(".required-info").remove();
				// Add required span
				jqField.find("h3").eq(0).append("<span class='required'> * </span>")
			}
		},{
		/* Request Form - Email? */

			controlSelector: "input[value=Email][name=receive-method]",
			fieldSelector: ".row:has(#email), .row:has(#confirm-email)",
			showOnSelect: true,
			initFunction: function(jqField){
				// Remove info span
				jqField.find(".required-info").remove();
			},
			changeFunction: function(jqField, show, jqControl) {
				// Add / Remove required span
				if (show && $("input[value=Further Information][name=request-method]").is(":checked")) {
					// Add required span
					if (jqField.find(".required").length == 0) {
						jqField.find("input").after("<span class='required'> * </span>");
					}
				} else {
					// Remove required span
					jqField.find(".required").remove();
				}
			}
		},{
		/* Request Form - Postal Details? */

			controlSelector: "input[value=Post][name=receive-method]",
			fieldSelector: "fieldset:has(#address\\.address-line-1)",
			showOnSelect: true,
			initFunction: function(jqField){
				// Remove info span
				jqField.find(".required-info").remove();
			},
			changeFunction: function(jqField, show, jqControl) {
				// find out if the control is actually visible
				if (jqControl) {
					var visible = jqControl.parents("fieldset").eq(0).css("display") != "none";

					// Only show if the control is visible (which is in turn is controlled by another checkbox)
					if (show && visible) {
					// Add required span
						if (jqField.find(".required").length == 0) {
							jqField.find("#address\\.address-line-1, #address\\.town, #address\\.county, #address\\.postcode, #address\\.country").after("<span class='required'> * </span>");
						}
						jqField.show();
					} else {
						jqField.hide();
					}
				} else {
					jqField.hide();
				}
			}
		},{
		/* Ask A Coach Form - Email? */

			controlSelector: "input[value=Email][name=contact-method]",
			fieldSelector: ".row:has(#email), .row:has(#confirm-email)",
			showOnSelect: true,
			initFunction: function(jqField){
				// Remove info span
				jqField.find(".required-info").remove();
			},
			changeFunction: function(jqField, show, jqControl) {
				// Add / Remove required span
				if (show) {
					// Add required span
					if (jqField.find(".required").length == 0) {
						jqField.find("input").after("<span class='required'> * </span>");
					}
				} else {
					// Remove required span
					jqField.find(".required").remove();
				}
			}
		},{
		/* Ask A Coach Form - Telephone? */

			controlSelector: "input[value=Telephone][name=contact-method]",
			fieldSelector: ".row:has(#telephone)",
			showOnSelect: true,
			initFunction: function(jqField){
				// Remove info span
				jqField.find(".required-info").remove();
			},
			changeFunction: function(jqField, show, jqControl) {
				// Add / Remove required span
				if (show) {
					// Add required span
					if (jqField.find(".required").length == 0) {
						jqField.find("input").eq(0).after("<span class='required'> * </span>");
					}
				} else {
					// Remove required span
					jqField.find(".required").remove();
				}
			}
		}
	]);
});