/*

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([{
		/* A Demo? - Contact method */

		controlSelector: "select[name=enquiry-type] option[value=Service Enquiry]",
		fieldSelector: "div.row:has(select[name=service])",
		showOnSelect: true,
		initFunction: function(jqField){
			// Remove info span
			jqField.find(".required-info").remove();
			// Add required span
			jqField.find("select").after("<span class='required'> * </span>");
		
      // Row stripe
      $("div.row:has(textarea[id=comments])").addClass("odd");
		},
		changeFunction: function(jqField, show, jqControl) {
      if(show) {
        jqField.show();
        $("div.row:has(textarea[id=comments])").removeClass("odd");
      } else {
        jqField.hide();
        $("div.row:has(textarea[id=comments])").addClass("odd");        
      }
    }
	}]);
});

