$(document).ready(function(){

  $("#keywords").focus(function() {
	$(this).autocomplete(data,{
		matchContains: true,
		minChars: 1,
		formatItem: function(row, i, max) {
			return row.to;
		},
		formatMatch: function(row, i, max) {
			return row.to;
		},
		formatResult: function(row) { 
			return row.to;
		}
	}).result(function(event, item) {
	location.href = item.url;
	});  	
  });   	
	
  function clear(item){
	  if(item.tagName=='SELECT') {
	      item.options.length = 0;
	      $(item).hide('slow');
	      $(item).attr('disabled','disabled');
	  }
  }
  
/*	var allSelect=$('.category');		
	allSelect.each(function(index, item){
		if(index > 0)
		 clear(item);
	});  */ 
  
	
  $('.category').change(function(){
  	//alert($(this).val());
  	//alert($(this).attr('id'));
  	
  	var category = $(this).val();
  	var selectId = $(this).attr('id');	  	
  	var levelId = selectId.match(/\d+$/gi);
  	var tmpSelect = $('#category'+(levelId-(-1)));
  	
  	
	var allSelect=$('.category');		
	allSelect.each(function(index, item){
		if(index > (levelId-(-1)))
		 clear(item);
	});   	
  	
  	if(category>0)
  	{
	 $.ajax({
	      url: 'http://www.mybizplanet.com/cascadeSelect/selectcategory.php',
	      type: "POST",
	      data: 'category='+category,	   
	      dataType: 'json',   
	      error: function(){
	         alert('Error. Please try again later');
	      },	      
	      success: function(result){
			if(result.length>1)
			{	
			 tmpSelect.clearSelect();
			  $.each(result,function(index,data){
				  var option = new Option(data.text,data.value);
					 tmpSelect.attr('disabled','').show();
					 tmpSelect.append($("<option></option>").attr("value",data.value).text(data.text)); 
			  });			
			}
	      }
	   });
  	}
  	else
  	 tmpSelect.clearSelect();
  });  
  
});

