﻿/*
	This module controls the switching of a form from its
	'Basic' mode to its 'Advanced' mode. 
		
	Basic mode shows a subset of the values in the advanced form.

	This is achieved by tagging any HTML elements (rows, cols, spans, divs) 
	with the classes: 'basic' or 'adv' or both.	
		
	The code then determines which elements to hide/show at runtime.
		
	Note: There are a few things that must be done to enable this functionality on a page:
		
	2. There must be an encapsulting HTML element around the INPUT field such as those listed above.
		
	This is for 2 reasons: 
	1) The fields collapse better when hidden 
	2) both the label and input are hidden at the same time
				
	3. The client must determine the initial type from the searchType FIELD 
	and initialize the form with the following code:
		
	$(document).ready(function() 
	{
	helperToggleForm(<'adv'|'basic'>);
	[optional] setFormName(<name>);
	});

	4. The client must provide buttons for switching between views. They must be tagged with 
	the appropriate class and call helperToggleForm(<'adv'|'basic'>);
			
	<div class="adv">
	<a href="javascript:void(0);" onclick="helperToggleForm('basic');">Basic Form</a>
	</div>

	5. The client must provide a reference to this script such as:
		
	<!-- SCRIPTS -->
	<xsl:template match="*" mode="userscripts">
	<script type="text/javascript" src="js/forms/formAdvBasic.js"></script>
	</xsl:template>

	Limitations:
		
	* Form elements that remain visible will be shifted and packed as other elements are hidden
	This may be undisirable. If it is, then a different mechanism must be used.
*/

// Form type constants
var formTypeAdv = 'adv';
var formTypeBasic = 'basic';
// Current form type
var formType = null;
// The opposite form type - when formType == 'basic', this is 'adv'
var formTypeOpp = null;
// The HTML form to submit
var formName = null;


/*
Call with 'adv' or 'basic' to toggle the form display
between the 2 modes.
*/
function helperToggleForm(formTypeNew) {
    formType = formTypeNew;
    if ((formType == null) || (formType == '')) {
        formType = 'basic';
    }
	
	setFormTypeOpp(formType);
	toggleForm();
}

/*
Sets the formTypeOpp value
*/
function setFormTypeOpp(aFormType) {
	if (aFormType == formTypeAdv)
		formTypeOpp = formTypeBasic;
	else
		formTypeOpp = formTypeAdv;
}

/*
Toggles the display of the elements
*/
function toggleForm() {
	$("." + formTypeOpp).hide();
	$("." + formType).show();
}

/* 
	Clear the adv fields.
	This clears any INPUT fields found below an HTML element 
	marked only with class="adv" and not class="basic"
*/
var blockClearFields = false;
function blockClearAdvFields() {
	blockClearFields = true;
}
function clearAdvFields() {
	// only when currently basic
	if (!blockClearFields && formType == formTypeBasic) {
		var qry = "." + formTypeAdv + ":not(." + formTypeBasic + ") input";
		$(qry).val("");
		// TODO: Add selector for input class="<adv|basic>" when supported
	}
}
