window.addEvent('domready', function()
{
    var myImages = new Asset.images(['images/advancedtyresearch1.jpg', 'images/advancedtyresearch2.jpg', 'images/advancedtyresearch3.jpg', 'images/pressonbrandsearch1.jpg', 'images/pressonbrandsearch2.jpg', 'images/pressonbrandsearch3.jpg'], {
    });
	
	if ($('OverallDiameterPOB'))
	{
		//We can use one Request object many times.
		var req = new Request.JSON(
			{url:'ajax.php?action=advancedtyresearch&runajax=1&advancedsearch=1',
				onSuccess: function(response) {
					$('OverallDiameter').set('html', response.OverallDiameter);
					$('Box2').set('html', response.Box2);
					$('Box3').set('html', response.Box3);
					$('results').set('html', response.Results);
				},
				//Our request will most likely succeed, but just in case, we'll add an
				//onFailure method which will let the user know what happened.
				onFailure: function() {				
				}
			}
		);
		
		$('OverallDiameter').addEvent('change', function() {
			req.get({'OverallDiameter': $('OverallDiameter').get('value'), 'Box2': $('Box2').get('value'), 'Box3': $('Box3').get('value')});
		});
		$('Box2').addEvent('change', function() {
			req.get({'OverallDiameter': $('OverallDiameter').get('value'), 'Box2': $('Box2').get('value'), 'Box3': $('Box3').get('value')});
		});  
		$('Box3').addEvent('change', function() {
			req.get({'OverallDiameter': $('OverallDiameter').get('value'), 'Box2': $('Box2').get('value'), 'Box3': $('Box3').get('value')});
		});  
		
		//We can use one Request object many times.
		var pobreq = new Request.JSON(
			{url:'ajax.php?action=pobsearch&runajax=1&pobsearch=1',
				onSuccess: function(response) {
					$('OverallDiameterPOB').set('html', response.OverallDiameterPOB);
					$('WidthPOB').set('html', response.WidthPOB);
					$('RimDiameterPOB').set('html', response.RimDiameterPOB);
					$('resultspob').set('html', response.Results);
				},
				//Our request will most likely succeed, but just in case, we'll add an
				//onFailure method which will let the user know what happened.
				onFailure: function() {				
				}
			}
		);
		
		$('OverallDiameterPOB').addEvent('change', function() {
			pobreq.get({'OverallDiameter': $('OverallDiameterPOB').get('value'), 'Width': $('WidthPOB').get('value'), 'RimDiameter': $('RimDiameterPOB').get('value')});
		});
		$('WidthPOB').addEvent('change', function() {
			pobreq.get({'OverallDiameter': $('OverallDiameterPOB').get('value'), 'Width': $('WidthPOB').get('value'), 'RimDiameter': $('RimDiameterPOB').get('value')});
		});  
		$('RimDiameterPOB').addEvent('change', function() {
			pobreq.get({'OverallDiameter': $('OverallDiameterPOB').get('value'), 'Width': $('WidthPOB').get('value'), 'RimDiameter': $('RimDiameterPOB').get('value')});
		});
	}  
	
	if ($('typesolid'))
	{	
		// Hide the forms initially				
		$('typesolid').style.display = 'none';
		$('typestandard').style.display = 'none';
		$('typepneumatic').style.display = 'none';
		$('typepob').style.display = 'none';
		
		// Hide the brand selections and manufacturer
		$('SolidBrand').style.display = 'none';
		$('StandardBrand').style.display = 'none';
		$('PneumaticBrand').style.display = 'none';
		$('SolidManufacturer').style.display = 'none';
		$('StandardManufacturer').style.display = 'none';
		$('PneumaticManufacturer').style.display = 'none';
		
		// Enable onclick functionality of the images
		$$('#solidoptions img').addEvent('click', function() {
			$('typesolid').style.display = 'block';
			$('typestandard').style.display = 'none';
			$('typepneumatic').style.display = 'none';
			$('typepob').style.display = 'none';			
			$('SolidBrand').value = this.get('alt');	
			$$('#solidform div.filteroptions img').set('src', this.get('src'));			
			
			// Get the relevant sizes
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&solidsearch=1',
					onSuccess: function(response) {						
						$('SolidSize').set('html', response.SizeResults);
						$('SolidRimWidth').set('html', response.RimWidthResults);
						$('SolidTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': this.get('alt')});
		});
		
		$$('#standardoptions img').addEvent('click', function() {
			$('typestandard').style.display = 'block';
			$('typesolid').style.display = 'none';
			$('typepneumatic').style.display = 'none';
			$('typepob').style.display = 'none';			
			$('StandardBrand').value = this.get('alt');	
			$$('#standardform div.filteroptions img').set('src', this.get('src'));			
			
			// Get the relevant sizes
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&solidsearch=1',
					onSuccess: function(response) {						
						$('StandardSize').set('html', response.SizeResults);
						$('StandardRimWidth').set('html', response.RimWidthResults);
						$('StandardTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': this.get('alt')});
		});
		
		$$('#pneumaticoptions img').addEvent('click', function() {
			$('typesolid').style.display = 'none';
			$('typestandard').style.display = 'none';
			$('typepneumatic').style.display = 'block';
			$('typepob').style.display = 'none';			
			$('PneumaticBrand').value = this.get('alt');
			$$('#pneumaticform div.filteroptions img').set('src', this.get('src'));		
			
			// Get the relevant sizes
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&pneumaticsearch=1',
					onSuccess: function(response) {						
						$('PneumaticSize').set('html', response.SizeResults);
						$('PneumaticPlyRating').set('html', response.PlyRatingResults);
						$('PneumaticTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': this.get('alt')});		
		});
		
		$$('#poboptions img').addEvent('click', function() {
			$('typesolid').style.display = 'none';
			$('typestandard').style.display = 'none';
			$('typepneumatic').style.display = 'none';
			$('typepob').style.display = 'block';			
			$('POBBrand').value = this.get('alt');
			$$('#pobform div.filteroptions img').set('src', this.get('src'));
			
			// Get the relevant sizes
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&pobsearch=1',
					onSuccess: function(response) {						
						$('POBSize').set('html', response.SizeResults);
						$('POBTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': this.get('alt')});					
		});
			
		var aFormReq = new Request.JSON(
			{url:'ajax.php?ProductGroup=Solid&advancedsearch=1&searchbytype=1&runajax=1&action=typesearch',
				onSuccess: function(response) {
					$('resultsareasolid').set('html', response.Results);
				}
			}
		);
		
		$('solidform').addEvent('submit', function(e) {
			aArgs = {'Size': $('SolidSize').get('value'), 'Tread': $('SolidTread').get('value'), 'Brand': $('SolidBrand').get('value'), 'Manufacturer': $('SolidManufacturer').get('value')};			
			aInputs = $$('#solidform input', '#solidform select');
			$each(aInputs, function(value, key){
				if ((value.get('type') == 'checkbox' && value.get('checked') == true) || (value.get('type') != 'checkbox' && value.get('value') != '')) aArgs[value.get('name')] = value.get('value');
			});
			aFormReq.get(aArgs);
			return false;			
		});
		
		var aStandardFormReq = new Request.JSON(
			{url:'ajax.php?ProductGroup=Solid&advancedsearch=1&searchbytype=1&runajax=1&action=typesearch',
				onSuccess: function(response) {
					$('resultsareastandard').set('html', response.Results);
				}
			}
		);
		
		$('standardform').addEvent('submit', function(e) {
			aArgs = {'Size': $('StandardSize').get('value'), 'Tread': $('StandardTread').get('value'), 'Brand': $('StandardBrand').get('value'), 'Manufacturer': $('StandardManufacturer').get('value')};			
			aInputs = $$('#standardform input', '#standardform select');
			$each(aInputs, function(value, key){
				if ((value.get('type') == 'checkbox' && value.get('checked') == true) || (value.get('type') != 'checkbox' && value.get('value') != '')) aArgs[value.get('name')] = value.get('value');
			});
			aStandardFormReq.get(aArgs);
			return false;			
		});
		
		var aPneumaticFormReq = new Request.JSON(
			{url: 'ajax.php?ProductGroup=Pneumatic&advancedsearch=1&searchbytype=1&runajax=1&action=typesearch',
				onSuccess: function(response) {
					$('resultsareapneumatic').set('html', response.Results);
				}
			}
		);
		
		$('pneumaticform').addEvent('submit', function(e) {	
			aArgs = {'Size': $('PneumaticSize').get('value'), 'PlyRating': $('PneumaticPlyRating').get('value'), 'Tread': $('PneumaticTread').get('value'), 'Brand': $('PneumaticBrand').get('value'), 'Manufacturer': $('PneumaticManufacturer').get('value')};			
			aInputs = $$('#pneumaticform input', '#pneumaticform select');
			$each(aInputs, function(value, key){
				if ((value.get('type') == 'checkbox' && value.get('checked') == true) || (value.get('type') != 'checkbox' && value.get('value') != '')) aArgs[value.get('name')] = value.get('value');
			});
			aPneumaticFormReq.get(aArgs);
			return false;			
		});
		
		var aPOBFormReq = new Request.JSON(
			{url:'ajax.php?ProductGroup=Press on Band&pobsearch=1&searchbytype=1&runajax=1&action=typesearch',
				onSuccess: function(response) {
					$('resultsareapob').set('html', response.Results);
				}
			}
		);
		
		$('pobform').addEvent('submit', function(e) {
			aArgs = {'Tread': $('POBTread').get('value'), 'Brand': $('POBBrand').get('value')};			
			aInputs = $$('#pobform input', '#pobform select');
			$each(aInputs, function(value, key){
				if ((value.get('type') == 'checkbox' && value.get('checked') == true) || (value.get('type') != 'checkbox' && value.get('value') != '')) aArgs[value.get('name')] = value.get('value');
			});
			aPOBFormReq.get(aArgs);
			return false;			
		});
		
		
		// Enable onclick functionality of the dropdowns
		$('SolidSize').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&solidsearch=1',
					onSuccess: function(response) {						
						$('SolidRimWidth').set('html', response.RimWidthResults);
						$('SolidTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('SolidBrand').get('value'), 'Size': this.get('value')});
		});
		
		$('SolidRimWidth').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&solidsearch=1',
					onSuccess: function(response) {						
						$('SolidTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('SolidBrand').get('value'), 'Size': $('SolidSize').get('value'), 'RimWidth': this.get('value')});
		});
		
		$('StandardSize').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&solidsearch=1',
					onSuccess: function(response) {						
						$('StandardRimWidth').set('html', response.RimWidthResults);
						$('StandardTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('StandardBrand').get('value'), 'Size': this.get('value')});
		});
		
		$('StandardRimWidth').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&solidsearch=1',
					onSuccess: function(response) {						
						$('StandardTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('StandardBrand').get('value'), 'Size': $('StandardSize').get('value'), 'RimWidth': this.get('value')});
		});
		
		$('PneumaticSize').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&pneumaticsearch=1',
					onSuccess: function(response) {						
						$('PneumaticPlyRating').set('html', response.PlyRatingResults);
						$('PneumaticTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('PneumaticBrand').get('value'), 'Size': this.get('value')});
		});
		
		$('PneumaticPlyRating').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&pneumaticsearch=1',
					onSuccess: function(response) {						
						$('PneumaticTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('PneumaticBrand').get('value'), 'Size': $('PneumaticSize').get('value'), 'PlyRating': this.get('value')});
		});
		
		$('POBSize').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&pobsearch=1',
					onSuccess: function(response) {						
						$('POBTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': $('POBBrand').get('value'), 'Size': this.get('value')});
		});
		
		$('POBBrand').addEvent('change', function() {			
			var sizereq = new Request.JSON(
				{url:'ajax.php?action=typesearchsize&runajax=1&pobsearch=1',
					onSuccess: function(response) {						
						$('POBSize').set('html', response.SizeResults);
						$('POBTread').set('html', response.TreadResults);
					},
					onFailure: function() {				
					}
				}
			);			
			sizereq.get({'Brand': this.get('alt')});
		});
				
	}
});
