

/// <reference path="../../../../../jQuery/1.3.2/jquery-1.3.2-vsdoc.js" />


/**
 *****************************************************************
 * FormControls
 *****************************************************************
 *
 * Provides core form submit functionality for styled, sifr'd, form submit buttons - through standard activation and architecture
 *
 * Provides customised functionality for specific forms
 *
 * Author : Liam Prescott
 */


	//Check that namespace into which the Class definition will be creates has been defined & if not then create
	
	if (!mrm.global.isNamespaceDefined("mrm.lexus.display.controls")) mrm.global.createNamespace("mrm.lexus.display.controls", "1.0");
	
	mrm.lexus.display.controls.FormControls = Object.subClass(
		{
		
			/*
			 =============================
			 CONSTANTS
			 =============================
			 */
				/*
				 =============================
				 COMMON
				 =============================
				 */
					DEFAULT_FORM_CONTAINER_TARGET			: "body",
					
					CLASS_FORM_SUBMIT_BUTTONS				: "button-form-submit",
					CLASS_FORM_CANCEL_BUTTONS				: "button-form-cancel",
					
					SELECTOR_FORM_SUBMIT_BUTTONS			: "div.button-form-submit",
					
					FORM_ERROR_CLASS_FORM_HIDE_ERROR_FIELDS : "hideErrorFields",
					
					FORM_ERROR_CLASS_ERROR_HIDDEN			: "fieldError-hidden",
					FORM_ERROR_CLASS_ERROR					: "fieldError",
					
					SELECTOR_FORM_CLIENT_SIDE_VALIDATION	: "form.frm-validation",
					CLASS_FORM_CLIENT_SIDE_VALIDATION		: "frm-validation",
					CLASS_CLIENT_SIDE_FORM_ERROR_STATE		: "formError",
				
				
				/*
				 =============================
				 FINANCE QUOTE FORM
				 =============================
				 */
					FINANCE_FORM_PAGE_ID_JQ_TARGET			: "div.IssFinance",
					
					FINANCE_FORM_TYPE_QUOTE_MONTHLY			: "TYPE_QUOTE_MONTHLY",
					FINANCE_FORM_TYPE_QUOTE_DEPOSIT			: "TYPE_QUOTE_DEPOSIT",

					FINANCE_FORM_DISABLED_OVERLAY_HTML			: "<div class=\"disabled-overlay png\"/>",
					FINANCE_FORM_JQ_TARGET_DISABLED_OVERLAY		: "div.disabled-overlay",
					FINANCE_FORM_JQ_TARGET_DISABLED_ITEM_CLASS	: "DISABLED_ITEM_FINANCE_FORM",
				
					FINANCE_FORM_JQ_TARGET_RADIO_BUTTON		: "div.IssFinance div.form-container form div.item.quotetype input:radio",
					
				
				
				/*
				/*
				 =============================
				 SERVICE YOUR LEXUS
				 =============================
                */		
					SERVICE_YOUR_LEXUS_PAGE_ID_JQ_TARGET			    : "div.ServiceYourVehicle",
					
					SERVICE_YOUR_LEXUS_JQ_TARGET_STATE_CONTAINER	    : "div.service-your-lexus",
					
                    SERVICE_YOUR_LEXUS_JQ_ATTRIBUTE_NAME	            : "start",					
					
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_1	: "service-your-lexus-state-1",
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_2	: "service-your-lexus-state-2",
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_3	: "service-your-lexus-state-3",
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_4	: "service-your-lexus-state-4",
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_5	: "service-your-lexus-state-5",
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_6	: "service-your-lexus-state-6",
					SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_7	: "service-your-lexus-state-7",
					
				/*				
				 =============================
				 SELL YOUR LEXUS
				 =============================
				 */
					SELL_YOUR_LEXUS_PAGE_ID_JQ_TARGET			: "div.SellYourLexus",
					
					SELL_YOUR_LEXUS_JQ_TARGET_STATE_CONTAINER	: "div.sell-your-lexus",
					
					SELL_YOUR_LEXUS_JQ_TARGET_CONTINUE_BUTTON	: "div.sell-your-lexus > div.display-state-1 > div.continue-button",
					
					SELL_YOUR_LEXUS_JQ_TARGET_BACK_TO_INSTRUCTIONS_BUTTON : "div.sell-your-lexus div.display-state-2 a.back-to-instructions",
					
					
					SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_1	: "sell-your-lexus-state-1",
					SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_2	: "sell-your-lexus-state-2",
				
			
			
			/*
			 =============================
			 CONFIGURATION PROPERTIES (instance configuration)
			 =============================
			 */
			
			
			
			/*
			 =============================
			 CONSTRUCTOR
			 =============================
			 */
				init : function (instanceReferenceString)
				{
					this.instanceString = instanceReferenceString;
					
					// var referrer =  document.referrer;

                    // alert( document.referrer );
					// alert( document.URL );
												
					// Configure form submit buttons
					//
					// PLEASE NOTE : This ONLY configures buttons that are ON-SCREEN (jQuery will not return buttons hidden vis css styles) - these need to be styled when displayed
					//
					this.configureFormButtons("body form");

					// If 'Service Your Lexus' form : configure functionality
					if (this._isServiceYourLexusPage()) this._configureServiceYourLexus(); 
										
					// If 'Sell Your Lexus' form : configure functionality
					if (this._isSellYourLexusPage()) this._configureSellYourLexus(); 
					
					// If 'Finance quote' form : configure functionality
					if (this._isFinanceQuotePage()) this._configureFinanceQuote();
					
					// If form has errored then configure error display functionality
					if (this._formHasErrored())
						this.configureFormErrorMessages();
				},
			
			
			
			/*
			 =============================
			 INTERNAL RUN-TIME PROPERTIES
			 =============================
			 */
				instanceString : undefined,
			
			
			
			/*
			 =============================
			 PUBLIC METHODS
			 =============================
			 */
			
			
			
			/*
			 =============================
			 INTERNAL METHODS
			 =============================
			 */
				/*
				 =============================
				 UTILITIES
				 =============================
				 */
				 
                    __isServiceYourLexusPage : undefined,
					
					_isServiceYourLexusPage : function ()
					{
						if (!this.__isServiceYourLexusPage)
						{
							this.__isServiceYourLexusPage = ($(this.SERVICE_YOUR_LEXUS_PAGE_ID_JQ_TARGET).length > 0) ? true : false; 
						}
						return this.__isServiceYourLexusPage;
					},

					
					__isSellYourLexusPage : undefined,
					
					_isSellYourLexusPage : function ()
					{
						if (!this.__isSellYourLexusPage)
						{
							this.__isSellYourLexusPage = ($(this.SELL_YOUR_LEXUS_PAGE_ID_JQ_TARGET).length > 0) ? true : false; 
						}
						return this.__isSellYourLexusPage;
					},

					
					__isFinanceQuotePage : undefined,
					
					_isFinanceQuotePage : function ()
					{
						if (!this.__isFinanceQuotePage)
						{
							this.__isFinanceQuotePage = ($(this.FINANCE_FORM_PAGE_ID_JQ_TARGET).length > 0) ? true : false; 
						}
						return this.__isFinanceQuotePage;
					},
					
					
					__formHasErrored : undefined,
					
					_formHasErrored : function ()
					{
					
						if (!this._anyFormHasClientSideValidation())
						{
							if (!this.__formHasErrored)
								this.__formHasErrored = ($("." + this.FORM_ERROR_CLASS_ERROR + ":first").length > 0) ? true : false;
							return this.__formHasErrored;
						}
						else
						{
							return $(this.SELECTOR_FORM_CLIENT_SIDE_VALIDATION + ":first").hasClass(this.CLASS_CLIENT_SIDE_FORM_ERROR_STATE);
						}
					},
					
					
					__anyFormHasClientSideValidation : undefined,
					
					_anyFormHasClientSideValidation : function ()
					{
						if (!this.__anyFormHasClientSideValidation)
						{
							this.__anyFormHasClientSideValidation = ($(this.SELECTOR_FORM_CLIENT_SIDE_VALIDATION + ":first").length > 0) ? true : false;
						}
						return this.__anyFormHasClientSideValidation;
					},
					
					
					_formHasClientSideValidation : function (jQueryTargetForm)
					{
						return (jQueryTargetForm.hasClass(this.CLASS_FORM_CLIENT_SIDE_VALIDATION)) ? true : false;
					},
					
			
				/*
				 =============================
				 SETUP
				 =============================
				 */
					/*
					 * STANDARD, STYLED, FORM SUBMIT BUTTONS
					 */
						configureFormButtons : function(containerTargetString)
						{
							this._configureFormSubmitButtons(containerTargetString);
							this._configureFormCancelButtons(containerTargetString);
						},
						
						_configureFormSubmitButtons : function (containerTargetString)
						{
							var __this = this;
							
							// Store container target string
							var containerTargetString	= (containerTargetString != undefined) ? containerTargetString : this.DEFAULT_FORM_CONTAINER_TARGET;
							
							// Resolve container
							var containerJQObj = $(containerTargetString);
							
							// Find buttons within container
							var buttons	= containerJQObj.find("div." + this.CLASS_FORM_SUBMIT_BUTTONS);
							
							// bind up the buttons and sIFR them, adds inline javascript to msie browsers and remove href of all others
							if (buttons.length > 0)
							{
								this._bindFormButtons(containerTargetString, "div." + this.CLASS_FORM_SUBMIT_BUTTONS, buttons, "_handleSifrFormSubmitLinkInternalClick");
								// bind standard click functionality to buttons
								buttons.bind("click", null, function(e) {__this._handleFormSubmit(e); });
							}
						},
						
						_configureFormCancelButtons : function (containerTargetString)
						{
							var __this = this;
							
							// Store container target string
							var containerTargetString	= (containerTargetString != undefined) ? containerTargetString : this.DEFAULT_FORM_CONTAINER_TARGET;
							
							// Resolve container
							var containerJQObj = $(containerTargetString);
							
							// Find buttons within container
							var buttons	= containerJQObj.find("div." + this.CLASS_FORM_CANCEL_BUTTONS);
							
							if (buttons.length > 0)
							{
								// bind up the buttons and sIFR them, adds inline javascript to msie browsers and remove href of all others
								this._bindFormButtons(containerTargetString, "div." + this.CLASS_FORM_CANCEL_BUTTONS, buttons, "_handleSifrFormCancelLinkInternalClick");
								// bind standard click functionality to buttons
								buttons.bind("click", null, function(e) {__this._handleFormCancel(e); });
							}
						},
						
						
						// bind up the buttons and sIFR them, adds inline javascript to msie browsers and remove href of all others
//
// [ sIFR button ] activation
//						_bindSIFRFormButtons : function(containerTargetString, selectorButtons, jqButtons, clickHandlerName)
						_bindFormButtons : function(containerTargetString, selectorButtons, jqButtons, clickHandlerName)
						{
							var __this = this;
							
							// Store container target string
							var containerTargetString = (containerTargetString != undefined) ? containerTargetString : this.DEFAULT_FORM_CONTAINER_TARGET;

//
// [ sIFR button ] activation
//							
//							// If ie then add href attribute to each buttons internal <a> tag
//							if ($.browser.msie)
//							{
//								// Create form reference storage array
//								if(!this.jQueryFormObjects) this.jQueryFormObjects = [];
//								
//								// Format link string fragment
//								var linkStringFragment = "javascript:" + this.instanceString + "." + clickHandlerName + "(";
//								
//								var startId = this.jQueryFormObjects.length;
//								//alert("Start id = " + startId);
//								
//								// Process buttons
//								jqButtons.each 
//								(
//									function (i)
//									{
//										var ___this = $(this);
//										
//										// Find child link
//										var link	= ___this.find("a");
//										
//										// Find parent form
//										var parentForm = ___this.parents("form:first");
//										
//										// Store parent form in reference array
//										__this.jQueryFormObjects.push(parentForm);
//										
//										// Attach href
//										var jsHref = linkStringFragment + (startId + i) + ");";
//										//alert("href = " + jsHref);
//										link.attr("href", ""); // clean href
//										link.attr("href", jsHref);
//									}
//								)
//							}
//							else
//							{
								// Ensure no href attribute on <a>
								jqButtons.each 
								(
									function (i)
									{
										var ___this = $(this);
										
										// Find child link
										var link	= ___this.find("a");
										
										// Clear href
										link.removeAttr("href"); //link.attr("href", "");
									}
								)
//							}
//							
//
// [ sIFR button ] styling
//							// Style buttons
//							sIFR.runDelayedButtonStyling(containerTargetString + " " + selectorButtons + " " + sIFR.styles.DEFAULT_BUTTON_STYLE_TARGET_STRING_INTERNAL_PATH);
						},
						
						
						
//						/*
//						=============================
//						IE 6 : Sifr'd link click handling method
//						=============================
//						*/
//							jQueryFormObjects : undefined,
//							
//							
//							_handleSifrFormSubmitLinkInternalClick : function (id)
//							{
//								var form = this.jQueryFormObjects[id];
//								
//								// Escape if no form
//								if (!form) return;
//								
//								// Create event object
//								var e		= new jQuery.Event("click"); 
//								e.target	= e.currentTarget = form[0];
//								
//								// Run method
//								this._handleFormSubmit(e);
//							},
//							
//							_handleSifrFormCancelLinkInternalClick : function (id)
//							{
//								var form = this.jQueryFormObjects[id];
//								
//								// Escape if no form
//								if (!form) return;
//								
//								// Create event object
//								var e		= new jQuery.Event("cancel");
//								e.target	= e.currentTarget = form[0];
//								
//								// Run method
//								this._handleFormCancel(e);
//							},
			
			
			
					/*
					 * CONFIGURE FORM ERROR MESSAGES
					 */
						configureFormErrorMessages : function ()
						{
							// If a form has errored the error spans will (should) be overlaying ontop of the input fields
							// As such need to add following functionality :
							//		- error span	: when clicked disappears
							//		- input fields	: when clicked error span disappear
							//		- cursor left focused in correct input
							//		- (?) add error class to PARENT 'div.item'
							
							// Get all spans
							// For each span
								// Get sibling input
								// Attach click event handler to each
							
							var __this = this;
							
							var errorSpans = $("form span." + this.FORM_ERROR_CLASS_ERROR);
							
							errorSpans.each(
								function (i)
								{
									//alert(i);
									
									var ___this = $(this);
									
									var parentForm	= ___this.closest("form");
									var parent		= ___this.closest("div.item");
									var input		= parent.find("input");
									
									// If no input search for textarea
									if (input.length == 0) input = parent.find("textarea");
							
									// Bind click handler methods to span and input
									___this.bind("click", null, function (e) { __this._handleHideFormErrorMessage(e); });
									//input.bind("click", null, function (e) { __this._handleHideFormErrorMessage(e); });
									input.bind("focus", null, function (e) { __this._handleHideFormErrorMessage(e); });
									
									//
									//
									// Add error class to parent if !client-side validating form
									//
									//
									if (!__this._formHasClientSideValidation(parentForm))
										if (!parent.hasClass(__this.FORM_ERROR_CLASS_ERROR)) parent.addClass(__this.FORM_ERROR_CLASS_ERROR);
								}
							);
							
							
							
							/*
							 * Run ie.6 fix 
							 * - Necessary to trigger correct rendering in ie.6 - without which ie.6 doesn't render error spans -> adding an additional class forces the browser to re-render elements and draw correctly!!!
							 */
								
								if ($.browser.msie && $.browser.version < 7)
								{
									errorSpans.each(function ()
									{
										$(this).addClass("fieldError-ie6Fix");
									});
								}
								
						},
						
						
						
						
						_handleHideFormErrorMessage : function (e)
						{
							var parent	= $(e.target).closest("div.item");
							var span	= parent.find("span." + this.FORM_ERROR_CLASS_ERROR);
							var input	= parent.find("input");
							
							// If no input search for textarea
							if (input.length == 0) input = parent.find("textarea");
							
							// Add hidden class
							if (!span.hasClass(this.FORM_ERROR_CLASS_ERROR_HIDDEN)) span.addClass(this.FORM_ERROR_CLASS_ERROR_HIDDEN);
							
							// Remove click handlers as now hidden, otherwise we end up in looped js chain
							span.unbind("click");
							//input.unbind("click");
							input.unbind("focus");
							
							// Focus into input 
							input.focus();
							input.select();
							
							// after focus re-bind span
							var __this = this;
							span.bind("click", null, function (e) { __this._handleHideFormErrorMessage(e); });
							//input.bind("click", null, function (e) { __this._handleHideFormErrorMessage(e); });
							input.bind("focus", null, function (e) { __this._handleHideFormErrorMessage(e); });
						},
						
						
						_hideAllFormErrorMessages : function (jQueryObjectTargetForm)
						{
							var errorSpans = jQueryObjectTargetForm.find("span." + this.FORM_ERROR_CLASS_ERROR);
							
							errorSpans.each(
								function (i)
								{
									var ___this = $(this);
									if (!___this.hasClass(this.FORM_ERROR_CLASS_ERROR_HIDDEN)) ___this.addClass(this.FORM_ERROR_CLASS_ERROR_HIDDEN);
								}
							);
						},
						
						
						
					
					/*
						
					*/
						

			
					/*
					 * CONFIGURE 'SERVICE YOUR LEXUS' FORM/PAGE FUNCTIONALITY
					 */
						
						_configureServiceYourLexus : function ()
						{
							// Get target container
							var target = $(this.SERVICE_YOUR_LEXUS_JQ_TARGET_STATE_CONTAINER);
							
							// Configure according to current state
							this._configureServiceYourLexusDisplayState();
						},

						_serviceYourLexusDisplayStateConfigured : false,
						
						_configureServiceYourLexusDisplayState : function ()
						{
							var __this = this;

                            // alert( document.URL ); // cannot use this because url has already been rewritten to match the workflow and arguments have been stripped							
                            
							// get #divUvlContainer from the parent document
                            var jqParent = $("#divUvlContainer", parent.document.body);
                            
                            // find div.summary-service-your-lexus in #divUvlContainer 
                            var jqPanel = $(jqParent).find("div.summary-service-your-lexus");
                            
                            // find a.Service in div.summary-service-your-lexus
                            var jqAnchor = $(jqPanel).find("#aService");

                            // store whether request has come from the home view
                            var fromHomeView = $(jqAnchor).hasClass(this.SERVICE_YOUR_LEXUS_JQ_ATTRIBUTE_NAME);
                            
                            if (fromHomeView)
                            {
                                // give a debug message
                                // alert( 'this request has come from homeview' );										
                                // remove the class as already on the form
                                $(jqAnchor).removeClass(this.SERVICE_YOUR_LEXUS_JQ_ATTRIBUTE_NAME);
//                                if ($(jqAnchor).hasClass(this.SERVICE_YOUR_LEXUS_JQ_ATTRIBUTE_NAME))
//                                {
//                                    alert( 'class NOT successfully removed!' );										
//                                }
//                                else
//                                {
//                                    alert( 'class was successfully removed!' );										
//                                }
                            }
                            else
                            {
                                // alert( 'this request has NOT come from homeview' );
                            }
                            
                            // Check if request has come from the view view
                            if (fromHomeView)                            
                            {			
                                // request has come from the view view. So, reset the steps as IE is incapable of doing so.
                                				
                                var target = $(this.SERVICE_YOUR_LEXUS_JQ_TARGET_STATE_CONTAINER);
                                
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_1)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_1);
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_2)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_2);
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_3)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_3);
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_4)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_4);
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_5)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_5);
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_6)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_6);
							    if (target.hasClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_7)) target.removeClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_7);
    							
							    target.addClass(this.SERVICE_YOUR_LEXUS_CLASS_SERVICE_VEHICLE_STATE_1);
                            }
                            
							// Set configured = true
							this._serviceYourLexusDisplayStateConfigured = true;
						},
					
								
			
					/*
					 * CONFIGURE 'SELL YOUR LEXUS' FORM/PAGE FUNCTIONALITY
					 */
						
						_configureSellYourLexus : function ()
						{
							// Get target container
							var target = $(this.SELL_YOUR_LEXUS_JQ_TARGET_STATE_CONTAINER);
							
							// Configure according to current state
							if (target.hasClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_1)) this._configureSellYourLexusDisplayState1();
							else if (target.hasClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_2)) this._configureSellYourLexusDisplayState2();
							
						},
						
						
						
						_sellYourLexusDisplayState1Configured : false,
						
						_configureSellYourLexusDisplayState1 : function ()
						{
							var __this = this;
							
							// Find continue button
							var continueButton	= $(this.SELL_YOUR_LEXUS_JQ_TARGET_CONTINUE_BUTTON);
							
							// Attach click event to continue button
							continueButton.bind("click", null, function (e){ __this._switchDisplayStateSellYourLexus(2); });


//
// [ sIFR button ] activation / styling
//							
//							// If ie then add href attributes to <a>
//							if ($.browser.msie)
//							{
//								var link		= continueButton.find("a:first");
//								var linkString	= "javascript:" + this.instanceString + "._switchDisplayStateSellYourLexus(2);";
//								
//								link.attr("href", linkString);
//							}
//							
//							// Style buttons
//							sIFR.runDelayedButtonStyling(this.SELL_YOUR_LEXUS_PAGE_ID_JQ_TARGET + " " + this.SELL_YOUR_LEXUS_JQ_TARGET_CONTINUE_BUTTON + " " + sIFR.styles.DEFAULT_BUTTON_STYLE_TARGET_STRING_INTERNAL_PATH);
							
							
							// Set configured = true
							this._sellYourLexusDisplayState1Configured = true;
						},
						
						
						_sellYourLexusDisplayState2Configured : false,
						
						_configureSellYourLexusDisplayState2 : function ()
						{
							var __this = this;
							
							// Find back button
							var backButton = $(this.SELL_YOUR_LEXUS_JQ_TARGET_BACK_TO_INSTRUCTIONS_BUTTON);
							
							// Attach click event to back button
							backButton.bind("click", null, function (e){ __this._switchDisplayStateSellYourLexus(1); });
							
							// Set configured = true
							this._sellYourLexusDisplayState2Configured = true;
						},
					
					
					
					/*
					 * CONFIGURE 'FINANCE-QUOTE' FORM/PAGE FUNCTIONALITY
					 */
						_configureFinanceQuote : function ()
						{
							var __this = this;
							
							$(this.FINANCE_FORM_JQ_TARGET_RADIO_BUTTON + ":eq(0)").click(
								function() 
								{
									var _parentFieldset = $(this).parents("fieldset");

									__this._switchDisplayFinanceQuoteType(_parentFieldset,
																		  _parentFieldset.find("div.depositamount, div.termmonthsdeposit"),
																		  __this.FINANCE_FORM_TYPE_QUOTE_MONTHLY
																		  );
								}
							)

							$(this.FINANCE_FORM_JQ_TARGET_RADIO_BUTTON + ":eq(1)").click(
								function() 
								{
									var _parentFieldset = $(this).parents("fieldset");

									__this._switchDisplayFinanceQuoteType(_parentFieldset,
																		  _parentFieldset.find("div.amountmonthly, div.termmonthsmonthly"),
																		  __this.FINANCE_FORM_TYPE_QUOTE_DEPOSIT
																		  );
								}
							)

							//Set our default option by programatically firing the click handler on the selected radio button.
							$(this.FINANCE_FORM_JQ_TARGET_RADIO_BUTTON + ":checked").trigger("click");
							
						},
					
					
			
				/*
				 =============================
				 DISPLAY CREATION
				 =============================
				 */
			
			
			
				/*
				 =============================
				 DISPLAY CONTROL
				 =============================
				 */
				 
					/*
					 * 'SELL YOUR LEXUS'
					 */
						_switchDisplayStateSellYourLexus : function (id)
						{
							var target = $(this.SELL_YOUR_LEXUS_JQ_TARGET_STATE_CONTAINER);
							if (id == 1)
							{
								if (target.hasClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_2)) target.removeClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_2);
								target.addClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_1);
								
								// If display state 1 not configured then configure
								if (!this._sellYourLexusDisplayState1Configured) this._configureSellYourLexusDisplayState1();
							}
							else if (id == 2)
							{
								if (target.hasClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_1)) target.removeClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_1);
								target.addClass(this.SELL_YOUR_LEXUS_CLASS_SELL_VEHICLE_STATE_2);
								
								// If display state 2 not configured then configure
								if (!this._sellYourLexusDisplayState2Configured) this._configureSellYourLexusDisplayState2();
							}
						},
					
					
					
					/*
					 * 'FINANCE QUOTE'
					 */
						_switchDisplayFinanceQuoteType : function (parentFieldset, itemsToDisable, type)
						{
							__this = this;
							
							// Remove any existing disabled overlays
							parentFieldset.find(this.FINANCE_FORM_JQ_TARGET_DISABLED_OVERLAY).remove();
							
							// Remove any disabling classes
							var currentlyDisabledItems = $("div." + this.FINANCE_FORM_JQ_TARGET_DISABLED_ITEM_CLASS);
							currentlyDisabledItems.removeClass(this.FINANCE_FORM_JQ_TARGET_DISABLED_ITEM_CLASS);
							
							// Add disabled overlay to required items
							itemsToDisable.each(
								function(i) 
								{
									var ___this = $(this);
									___this.append(__this.FINANCE_FORM_DISABLED_OVERLAY_HTML);
									
									if (!___this.hasClass(__this.FINANCE_FORM_JQ_TARGET_DISABLED_ITEM_CLASS)) ___this.addClass(__this.FINANCE_FORM_JQ_TARGET_DISABLED_ITEM_CLASS) 
								}
							);
						},
					
			
			
			
				/*
				 =============================
				 INTERNAL EVENT HANDLERS
				 =============================
				 */
					// carries out a search form submit event
					_handleFormSubmit : function (e)
					{
						this._handleFormAction(e,"submit");
					},
					
					// carries out a search form cancel event
					_handleFormCancel : function (e)
					{
						this._handleFormAction(e,"cancel");
					},
					
					_handleFormAction : function(e, action)
					{
						e.stopPropagation();
						e.stopImmediatePropagation();
						e.preventDefault();
						
						var form = $(e.target);
						
						// If target !form find closest parent form
						if (!form.is("form"))
							form = form.closest("form"); 
						
						// If no form found escape
						if (form.length == 0) return;
						
						// Submit form
						form.trigger(action);
					}
					
		}
	);
	
	//alert("...//...");

