

		var sFormDecoratorSkin = 'Default';
		var bEasySearchLoaded = false;
		var oCurrentRequestExecutor = null;
		var bDebugMode = false;
		var iPriceRangeViewState_SelectionStart = null; //to aide back button
		var iPriceRangeViewState_SelectionEnd = null; //to aide back button
		
		function PriceRangeSlider_HandleClientValueChange(sender, args){
			$get('currentValue').innerHTML = 'From &pound;' + sender.get_selectionStart() + ' to &pound;' + sender.get_selectionEnd() + (sender.get_selectionEnd() == sender.get_maximumValue() ? '+' : '');
			if(!sender.isSliding){
				if(bEasySearchLoaded){
					LoadProductCollection();
				}
			}
		}
		
		function PriceRangeSlider_HandleLoaded(sender, args){
            $get("minValue").innerHTML = '£'+sender.get_minimumValue()+'<small>or less</small>';
            $get("maxValue").innerHTML  = '£'+sender.get_maximumValue()+'<small>or more</small>';
            PriceRangeSlider_HandleClientValueChange(sender, args);
			
			//get rid of the default 'Drag' title of the slider handles
			$get('RadSliderDrag_PriceRangeSlider').title = '';
			$get('RadSliderEndDrag_PriceRangeSlider').title = '';
		}
		
		function PriceRangeSlider_HandleClientSlideStart(sender, args){
			sender.isSliding = true;
		}
		function PriceRangeSlider_HandleClientSlideEnd(sender, args){
			sender.isSliding = false;
			LoadProductCollection();
		}
	
		function CheckboxClicked_Handler(sType, bIgnoreSelectAllBox){
			var iCheckedCount = 0;
			var aTypeCheckboxes = $get(sType).getElementsByTagName('input');
			
			for(i=0;i<aTypeCheckboxes.length;i++){
				if(!aTypeCheckboxes[i].IsSelectAllCheckbox && aTypeCheckboxes[i].checked && !isNaN(aTypeCheckboxes[i].Value)){
					iCheckedCount++;
				}
			}
			
			if(!bIgnoreSelectAllBox){
				if(iCheckedCount == 0){
					aTypeCheckboxes[0].Check();
					aTypeCheckboxes[0].Disable();
				}else{
					aTypeCheckboxes[0].Uncheck();
					aTypeCheckboxes[0].Enable();
				}				
			}
			
			switch(sType){
				case 'colourTypes':
					HandleColourChange();
					break;
				default:
					// Don't load products for colour change until relevant checkboxes have been updated
					setTimeout('LoadProductCollection()', 500);
					break;
			}
		}
	
		function LoadProductCollection(){
			var oPriceRangeSlider = $find('PriceRangeSlider');
			
			if(iPriceRangeViewState_SelectionStart != null && iPriceRangeViewState_SelectionEnd != null){
				oPriceRangeSlider.set_selectionStart(iPriceRangeViewState_SelectionStart);
				oPriceRangeSlider.set_selectionEnd(iPriceRangeViewState_SelectionEnd);	
				iPriceRangeViewState_SelectionStart = null;
				iPriceRangeViewState_SelectionEnd = null;
			}
			
			var oSortByDrop = $find('dropSortBy');
			var iFromPrice = (oPriceRangeSlider.get_selectionStart() == oPriceRangeSlider.get_minimumValue() ? 1 : oPriceRangeSlider.get_selectionStart());
			var iToPrice = (oPriceRangeSlider.get_selectionEnd() == oPriceRangeSlider.get_maximumValue() ? 1000000 : oPriceRangeSlider.get_selectionEnd());;
		
			$get('txtSliderFrom').value = iFromPrice;
			$get('txtSliderTo').value = iToPrice;
			
			DisplayThrobber();
			
			var sRequestParams = '';
			
			var aTypeCheckboxes = $get('colourTypes').getElementsByTagName('input');
			sRequestParams += 'Colours='+serialiseSelectedCheckboxes(aTypeCheckboxes) + '&';

			aTypeCheckboxes = $get('styleTypes').getElementsByTagName('input');
			sRequestParams += 'Styles='+serialiseSelectedCheckboxes(aTypeCheckboxes) + '&';

			aTypeCheckboxes = $get('grapeTypes').getElementsByTagName('input');
			sRequestParams += 'Grapes='+serialiseSelectedCheckboxes(aTypeCheckboxes) + '&';

			sRequestParams += 'Sparkling='+ $get('chkSparkling').checked + '&';
			
			sRequestParams += 'LowestPrice='+iFromPrice + '&';
			
			sRequestParams += 'HighestPrice='+iToPrice + '&';
			
			sRequestParams += 'SortBy='+(oSortByDrop?oSortByDrop.get_value():'priceAscending');  //the drop takes a while to load, so deal with it incase it hasnt yet

			if(oCurrentRequestExecutor != null){
				oCurrentRequestExecutor.abort();
				oCurrentRequestExecutor = null;
			}
			
			if(oCurrentRequestExecutor){
				oCurrentRequestExecutor.abort();
			}
			var request = new Sys.Net.WebRequest();
			request.set_url('/pages/fetchEasySearchProducts.aspx');
			request.set_httpVerb('POST');
			request.set_body(sRequestParams);
			request.add_completed(LoadProductCollection_Callback);
			request.invoke();

			oCurrentRequestExecutor = request.get_executor();
			
			if(bDebugMode){
				$get('debug').innerHTML = '<strong>Product Load Request:<br /></strong>'+sRequestParams.replace(/&/g, '<br />')+'<br />';
			}
		}
		
		function serialiseSelectedCheckboxes(aCheckboxes){
			var sOutp = '';
			var bSelectAllChecked = aCheckboxes[0].checked;

			for(i=0;i<aCheckboxes.length;i++){
				if(bSelectAllChecked || aCheckboxes[i].checked){
					if(!isNaN(aCheckboxes[i].Value) && aCheckboxes[i].Value > 0){
						if(!aCheckboxes[i].disabled){
							sOutp += aCheckboxes[i].Value + ',';
						}
					}
				}
			}
			return sOutp;
		}
		
		function LoadProductCollection_Callback(result, eventArgs){
			if(result.get_timedOut()){
				$get('productsList').innerHTML = 'Your search timed out.  Please refine your search or try again';
			}
//			if(result.get_aborted()){
				//$get('productsList').innerHTML = 'An error occurred when fetching your results.  Please try again.';
//			}
			if(result.get_responseAvailable()){
				var sProductsJSON = result.get_responseData();
				var oResult = ''
				try{
					oResult = eval('(' + sProductsJSON + ')');
				}catch(e){
					alert('ERROR\r\n'+e);
					//alert(sProductsJSON);
				}
				
				$get('productResultCount').innerHTML = oResult.products.length + ' products found';
				
				if(oResult.products.length > 0){
					var sOutp = '';
					
					for(i=0; i < oResult.products.length; i++){
						if(i > 0 && (i % 4) == 0){
							sOutp += '<li>';
							sOutp += 	'<hr />';
							sOutp += '</li>';
						}
					
						var oProduct = oResult.products[i];
						
						if(oProduct.ThumbnailUrl.length == 0){oProduct.ThumbnailUrl = '/img/no-image.gif'};
						
						sOutp += '<li>';
						sOutp += 	'<div class="inner">';
						sOutp += 		'<img style="width:70px;" src="'+oProduct.ThumbnailUrl+'" />';
						sOutp += 		'<a href="'+oProduct.ProductDisplayUrl+'">'+oProduct.Name+'</a>';
						if(oProduct.IsOrganic){
							sOutp += 	'<div class="organicIcon" title="This wine is organic"></div>';
						}
						sOutp +=		'<span>'+oProduct.Price+'</span>';
						sOutp += 	'</div>';
						sOutp += '</li>';
					}
					
					$get('productsList').innerHTML = '<ol>' + sOutp + '</ol>';
				}else{
					$get('productsList').innerHTML = '<h2>There are no products that match your preferences</h2>';
				}
			}
		}
		
		function HandleColourChange(){
			var aTypeCheckboxes = $get('colourTypes').getElementsByTagName('input');
			var sRequestParams = 'Colours='+serialiseSelectedCheckboxes(aTypeCheckboxes);
			
			var request2 = new Sys.Net.WebRequest();
			request2.set_url('/pages/fetchApplicableCheckboxesForColours.aspx');
			request2.set_httpVerb('POST');
			request2.set_body(sRequestParams);
			request2.add_completed(HandleColourChange_Callback);
			request2.invoke();
			
			$get('searchOptionsMask').style.display = 'block';
			$get('searchOptionsMask').style.height = $get('easySearch_Options').scrollHeight+'px';
			$get('searchOptionsMask').style.width = $get('easySearch_Options').scrollWidth+'px';
			
			if(bDebugMode){
				$get('debug').innerHTML += '<strong>Checkbox Applicability Request:<br /></strong>'+sRequestParams.replace(/&/g, '<br />')+'<br />';
			}
		}
		
		function HandleColourChange_Callback(result, eventArgs){
			if(result.get_responseAvailable()){
				var sProductsJSON = result.get_responseData();
				var oResult = ''
				try{
					oResult = eval('(' + sProductsJSON + ')');
				}catch(e){
					alert('ERROR\r\n'+e);
					alert(sProductsJSON);
				}
				
				setTypeCheckboxes('styleTypes', oResult.applicableStyleIds);
				setTypeCheckboxes('grapeTypes', oResult.applicableGrapeIds);

				$get('searchOptionsMask').style.display = 'none';
				
				LoadProductCollection();
			}
		}
		
		function setTypeCheckboxes(sType, aApplicableIds){
			var aTypeCheckboxes = $get(sType).getElementsByTagName('input');

			for(iChkIndex = 0; iChkIndex < aTypeCheckboxes.length; iChkIndex++){
				var bApplicable = false;
				for(iIdIndex = 0; iIdIndex < aApplicableIds.length; iIdIndex++){
					if((aTypeCheckboxes[iChkIndex].Value == aApplicableIds[iIdIndex]) && !isNaN(aTypeCheckboxes[iChkIndex].Value)){
						bApplicable = true;
						break;
					}
				}
				if(bApplicable){
					aTypeCheckboxes[iChkIndex].Enable();
					//aTypeCheckboxes[iChkIndex].Check();
				}else{
					if(!isNaN(aTypeCheckboxes[iChkIndex].Value) && !aTypeCheckboxes[iChkIndex].IsSelectAllCheckbox){
						aTypeCheckboxes[iChkIndex].Disable();
					}
				}
			}
		}
		
		function DisplayThrobber(){
			$get('productsList').innerHTML = '<div style="height:200px; background:transparent url(/img/throbber.gif) center 80px no-repeat; padding-top:120px; font-size:15px; text-align:center;">Searching our wine database&hellip;</div>';
			$get('productResultCount').innerHTML = 'Searching&hellip';
		}
		
		function initTypeCheckboxes(sType){
			var aTypeCheckboxes = $get(sType).getElementsByTagName('input');
			var aTypeIdVars = $get(sType).getElementsByTagName('var');
			var aCheckboxLabels = $get(sType).getElementsByTagName('label');
			var bSpecificTypesSelected = false;
			
			for(i=0;i<aTypeCheckboxes.length;i++){
				aTypeCheckboxes[i].Label = aCheckboxLabels[i];
				aTypeCheckboxes[i].Label.Checkbox = aTypeCheckboxes[i];
				aTypeCheckboxes[i].Value = aTypeIdVars[i].innerHTML;
				aTypeCheckboxes[i].BaseClassName = sFormDecoratorSkin;
				aTypeCheckboxes[i].Enable = function(){
												this.disabled = false;
												this.Label.className = this.BaseClassName + (this.checked ? ' radfdCheckboxChecked' : ' radfdCheckboxUnchecked');
											}
				aTypeCheckboxes[i].Disable = function(){
												//if(this.IsSelectAllCheckbox){return;}
												this.disabled = true;
												this.Label.className = this.BaseClassName + (this.checked ? ' radfdCheckboxChecked' : ' radfdCheckboxUnchecked') + ' radfdInputDisabled';
											}
				aTypeCheckboxes[i].Check = function(bChangeLabelOnly){
												if(!bChangeLabelOnly){ this.checked = true; }
												this.Label.className = this.BaseClassName + (this.disabled ? ' radfdInputDisabled' : '') + ' radfdCheckboxChecked';
											}
				aTypeCheckboxes[i].Uncheck = function(bChangeLabelOnly){
												if(!bChangeLabelOnly){ this.checked = false; }
												this.Label.className = this.BaseClassName + (this.disabled ? ' radfdInputDisabled' : '') + ' radfdCheckboxUnchecked';
											}
				aTypeCheckboxes[i].Toggle = function(){
/*												if(this.IsSelectAllCheckbox){
													this.checked = true;
													return;
												}*/
												if(this.disabled) { return; }
												if(this.checked){
													this.Uncheck(true);
												}else{
													this.Check(true);
												}
											}
				
				aTypeCheckboxes[i].className = 'input';
				aTypeCheckboxes[i].Label.className = sFormDecoratorSkin + ' radfdCheckboxUnchecked';
				aTypeCheckboxes[i].Label.onclick = function(){ this.Checkbox.Toggle(); }
				
				if(aTypeCheckboxes[i].checked){
					//incase of back/forward buttons, the label has to catch up with the already-checked checkboxes
					aTypeCheckboxes[i].Check(true);
					if(aTypeCheckboxes[i].Value != '-1'){
						bSpecificTypesSelected = true;
					}
				}
			}
			
			//dont force the 'All' checkboxes if the back button is used.
			if(!bSpecificTypesSelected){
				for(i=0;i<aTypeCheckboxes.length;i++){
					if(aTypeCheckboxes[i].Value == '-1'){
						aTypeCheckboxes[i].IsSelectAllCheckbox = true;
						aTypeCheckboxes[i].Check();
						//aTypeCheckboxes[i].Label.onclick = function(){return false;}
						aTypeCheckboxes[i].Disable();
						break;
					}
				}
			}

		}
		
		function toggleAllCheckboxesForType(sType, oSender){
			var aTypeCheckboxes = $get(sType).getElementsByTagName('input');
			for(i=0;i<aTypeCheckboxes.length;i++){
				if(!aTypeCheckboxes[i].IsSelectAllCheckbox){
					aTypeCheckboxes[i].Uncheck();
				}else{
					aTypeCheckboxes[i].Disable();
				}
			}

			CheckboxClicked_Handler(sType, true);			
		}
		
		function dropSortByIndexChanged_Handler(sender, eventArgs){
			LoadProductCollection();
		}
		
		function initEasySearch(){
			initTypeCheckboxes('colourTypes');
			initTypeCheckboxes('styleTypes');
			initTypeCheckboxes('grapeTypes');
			
			bEasySearchLoaded = true;

			if($get('txtSliderFrom').value.length > 0 && !isNaN($get('txtSliderFrom').value) && !isNaN($get('txtSliderTo').value)){
				iPriceRangeViewState_SelectionStart = $get('txtSliderFrom').value;
				iPriceRangeViewState_SelectionEnd = $get('txtSliderTo').value;
			}
			CheckboxClicked_Handler('colourTypes', true);
		}
		
		if(window.attachEvent){
			window.attachEvent('onload', initEasySearch);
		} else {
			window.addEventListener('load', initEasySearch, false);
		}

