﻿/// <reference path="../../../../jQuery/1.3.2/jquery-1.3.2-vsdoc.js" />

/**
*
* Results
*
* This class used to manage the results page of the Uvl-Lite application
*
*/

// Check that namespace into which the Class definition will be creates has been defined & if not then create

if (!mrm.global.isNamespaceDefined("mrm.cms.display")) mrm.global.createNamespace("mrm.cms.display", "1.0");

mrm.cms.display.QuickSearch = mrm.cms.display.SearchCriteria.subClass(
	{
		/*
		=============================
		CONSTANTS
		=============================
		*/
		CONTROL_NAME: "QuickSearch",
		QUICKSEARCH_SELECTOR: ".quick-search",

		/*
		=============================
		CONSTRUCTOR
		=============================
		*/
		"init": function () {
			//find the multi manufacturer search container and form
			var container = $(document).find('div.quick-search');
			var form = container.find('form#frmQuickSearchForm');

			//Temporary solution for postoce until DB updated for ROI postcodes.			
			this.BindPostCodeInput(form);
			var budgetForm = $(document).find('form#frmBudgetCalculator');
			this.BindPostCodeInput(budgetForm);

			//bind the form action
			this.BindForm(form);

			//bvind the search button
			this.BindSearchButton(form);

			// set default ajax manager
			if ($.ajaxManager) {
				this.AJAX_MANAGER = $.ajaxManager;
			}
		},

		/*
		=============================
		METHODS
		=============================
		*/

		"BindSearchButton": function (form) {
			//scope
			var _this = this;

			//find the search button
			var button = form.find('button.btn-search');
			var postcodeInput = form.find('input.postcode');
			var postcodeContainer = postcodeInput.parents('div.dyanmiclookuplist-container');
			var countrySelect = form.find('select.country');
			var errorSummaryContainer = form.find('div.error-summary');
			var postcodeError = errorSummaryContainer.find('span.postcode.error');

			//bind up the click event
			button.click(function (event) {
				//stop the default action from occurring
				event.preventDefault();

				//reset errors be re-evaluating
				postcodeError.addClass('none');
				errorSummaryContainer.addClass('none');

				//is PC validation required?
				var pcValidationRequired = (countrySelect.val() == _this.UK);

				//attempt to validate the postcode and stock count
				var postcode = postcodeInput.val();
				//var postcodeValid = ((_this.validatePostcode(postcode) && !postcodeContainer.hasClass('fieldError')) || !pcValidationRequired);
				//var postcodeValid = (!(postcode == "") && !postcodeContainer.hasClass('fieldError')) || !pcValidationRequired;
				var postcodeValid = (_this.validatePostcode(postcode) || !pcValidationRequired);

				if (postcodeValid) {					
					form.attr(_this.ACTION, _this.SUBMIT_ACTION);
					form.submit();
				}
				else {
					errorSummaryContainer.removeClass('none');
					postcodeError.removeClass('none');
				}
			});
		},

		"onSyncBefore": function (jqEvent) {
			//setup the UI elements and scope pointer
			var _this = this;
			var quickSearchContainer = $(this.QUICKSEARCH_SELECTOR);

			//set the visibility on the UI elements
			quickSearchContainer.css('opacity', '0.2');
		},

		"onSyncSuccess": function (jqEvent, p_response) {
			//scope pointer
			var _this = this;

			//check we have a valid response
			if (!p_response || !p_response.controls) return;

			//iterate the controls collection to output the controls to the page
			for (var i = 0; i < p_response.controls.length; i++) {
				//get the controls ajax object from the returned collection
				var control = p_response.controls[i];

				//only process the search criteria control
				if (control.controlName == _this.CONTROL_NAME) {

					//locate the target dom element
					var target = $(_this.QUICKSEARCH_SELECTOR);

					//if we have located the target then output the html
					if (target.length > 0) {
						//output the new html
						target.replaceWith(control.content);

						//rebind the ajax calls
						var form = $('form#frmQuickSearchForm');

						//Temporary solution for postoce until DB updated for ROI postcodes.			
						_this.BindPostCodeInput(form);
						var budgetForm = $(document).find('form#frmBudgetCalculator');
						_this.BindPostCodeInput(budgetForm);

						_this.BindForm(form);
						_this.BindSearchButton(form);

						//rebind the html list items	
						mrm.global.utilities.defaultControlBinder.bindControls();

						// Setup runtime namespace if doesn't exist
						if (!mrm.global.isNamespaceDefined("mrm.donnellyGroup.run.utilities")) mrm.global.createNamespace("mrm.donnellyGroup.run.utilities", "1.0");

						// Create instance
						mrm.donnellyGroup.run.utilities.LinkEnhancer = new mrm.lib.utilities.LinkEnhancer(true, false, true, true);

						//re-sIFR
						sIFR.replaceCoreAssets();
					}
				}
			}
		}
	}
);
