function searchddSelectCatalog( value)
{
	// changed by yong
	var brand = $("searchdd_manufacturer").value;
	var family = $("searchdd_model").value;
	var serie = $("searchdd_type").value;
	var model = $("searchdd_subtype").value;
	
	var url = '/catalogsearch/advanced/result/?brand='+brand+'&family='+family+'&serie='+serie+'&model='+value;
//	url = 'http://localhost/live.live/index.php'+url;

	window.location = url;
	
	//end by yong
}

function getSearchReplace()
{
	var sr = Array();
	var list = Array();
	list.push('searchdd_manufacturer');
	list.push('searchdd_model');
	list.push('searchdd_type');
	
	list.each(function(name)
		{
			var test = document.getElementById(name)[document.getElementById(name).selectedIndex].innerHTML;
			if( document.getElementById(name).selectedIndex != 0 )
				sr.push(test);
		}
	);
	
	return sr.join(' ');
}



//changed by yong 2-12-2011
function searchddSelect( val , name)
{	
	var url = '/searchdd/Search/Result';
	
	if( val == 0 )
		return;	

	if(name =='searchdd_subtype'){
		searchddSelectCatalog(val);
		return;
	}
	
	$('searchdd_loading').show();	
	
//	url = "http://localhost/live.live/index.php"+url;

	new Ajax.Request(url, {
		method: 'post',
		parameters: {value:val},
		requestHeaders: {Accept: 'application/json'},
		onComplete: function(transport)
		{
			
			if(transport.status == 200)
			{
				//alert("inside: "+val+" ---- "+transport.status);
				var json = transport.responseText.evalJSON(true);
				var element = '';
				var attribute = '';
				
				//alert(json.toSource());
				switch(name)
				{
					case 'searchdd_product':
						element = 'searchdd_manufacturer';	
						attribute = 'att_product_code';
						break;
					case 'searchdd_manufacturer':
						element = 'searchdd_model';	
						attribute = 'att_brand_code';
						break;
					case 'searchdd_model':
						element = 'searchdd_type';	
						attribute = 'att_model_code';
						break;
					case 'searchdd_type':
						element = 'searchdd_subtype';	
						attribute = 'att_type_code';
						break;
				}
				if( json.count == 0)
					//searchddSelectCatalog( val);
				
				var selected = '';
				if( json.count != 1 )
					selected = 'selected="1" ';
				
				var text = $(element).childNodes[0].innerHTML;
				var res = '<option '+selected+'value="0">'+ text +'</option>';
				json.select.each(
					function (s)
					{
						if( json.count != 1 )
							res += '<option value="'+s.value+'">'+s.label+'</option>';
						else
							res += '<option selected="1" value="'+s.value+'">'+s.label+'</option>';
					}
				);
				
				$(element).update(res);
			
				if( json.count > 0)
					$(element).show();
				
				$('searchdd_loading').hide();
				if( json.count == 1 )
				{	
					$(element).onchange();
				}
			}
		}
	});
	
}
//end by yong 2-12-2011
function loadProducts()
{
	var url = '/searchdd/Search/Product';

//	url= 'http://localhost/live.live/index.php/'+url
	new Ajax.Request(url, {
		method: 'post',
		requestHeaders: {Accept: 'application/json'},
		onComplete: function(transport)
		{
			if(transport.status == 200)
			{
				var json = transport.responseText.evalJSON(true);
				var element = 'searchdd_product';
				var selected = '';
				if( json.count != 1 )
					selected = 'selected="1" ';
				
				var text = $(element).childNodes[0].innerHTML;
				var res = '<option '+selected+'value="0">'+ text +'</option>';
				json.select.each(
					function (s)
					{
						if( json.count != 1 )
							res += '<option value="'+s.value+'">'+s.label+'</option>';
						else
							res += '<option selected="1" value="'+s.value+'">'+s.label+'</option>';
					}
				);
								
				$(element).update(res);
				
				if( json.count > 0)
					$(element).show();
				
				$('searchdd_loading').hide();
				if( json.count == 1 )
				{	
					$(element).onchange();
				}
			}
		}
	});
}

loadProducts();
