/* Validate Search form - Copyright Barry Scheepers 2008 - 2010 <barry@e-smurf.com> All rights reserved. */
$(document).ready(
	function()
	{
		//toggles the helpbox on clicking the noted link
		if ( $('select#sr_category').children().length <= 0)
			$('div#dv_category').hide();

		if ( $('select#sr_model').children().length <= 0)
			$('div#dv_model').hide();

		//Brand SELECT for categories
		$('select#sr_brand').change( function() {
			$('div#dv_category').hide();
			$('div#dv_model').hide();
			var $nBrandId = $('#sr_brand').val();
			$.get("ajax/searchAjax.php", {op: "cat", bId: $nBrandId}, function(xml) {
			   //format and output result
			   //alert(xml);
			   //Contact Select
			   $('select#sr_category').empty();
			   $('select#sr_model').empty();
			   if ($(xml).find('brand_category').length > 0)
			   {
				   $(xml).find('brand_category').each( function() {
						var $oBrandCategory = $(this);
						var $sHTML = '';
						$sHTML += '<option value="' + $oBrandCategory.attr('id') + '">' + $oBrandCategory.attr('name') + '</option>';
						$('select#sr_category').append($sHTML);
				   });

				   $('select#sr_category option:first').attr('selected','selected');

				   $('div#dv_category').show();
			   }
			});
		});

		//Category Select for models
		$('select#sr_category').change( function() {
			$('div#dv_model').hide();
			var $nCategoryId = $('#sr_category').val();
			$.get("ajax/searchAjax.php", {op: "mdl", cId: $nCategoryId}, function(xml) {
			   // format and output result
			   //alert(xml);
			   //Contact Select
			   $('select#sr_model').empty();
			   if ($(xml).find('brand_model').length > 0)
			   {
				   $(xml).find('brand_model').each( function() {
						var $oBrandModel = $(this);
						var $sHTML = '';
						$sHTML += '<option value="' + $oBrandModel.attr('id') + '">' + $oBrandModel.attr('name') + '</option>';
						$('select#sr_model').append($sHTML);
				   });

				   $('select#sr_model option:first').attr('selected','selected');

				   $('div#dv_model').show();
			   }
			});
		});
	}
);

function valSearchForm()
{
  var sBrand = document.getElementById("sr_brand");
  var sCategory = document.getElementById("sr_criteria");
  var sModel = document.getElementById("sr_model");

  if (sBrand.value == 'selIt')
  {
	alert("Please select brand to search for.");
	sBrand.focus();
	return false;
  }

  return true;
}
