var xmlHttp = false;

try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

//--------------------------------------------------

function sendRequest(url){

	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = updatePage;
	
	xmlHttp.send(null);
}

//---------------------------------------------------

function action_List(response, list, selected){
	var sel_value;
//	alert(response);

	var response = response.split("|");
	
	var category = document.getElementById(list);
		
	category.options.length = 0;
	
	if (list != 'Brand') {
		for(var i = 0; i < response.length; i++){
			var menuitem = response[i].split('#');
			category.options.length = i + 1;

			if (menuitem[1] == 'Все категории') {
                            sel_value = '0';
                        } else sel_value = menuitem[0];

			category.options[i] = new Option(menuitem[1], menuitem[0]);			
			if (sel_value == selected) {
				category.options[i].selected = true;
			}
		}
	} else {
			for(var i = 0; i < response.length; i++){
				var menuitem = response[i].split('#');
				category.options.length = i + 1;

//			if (menuitem[1] == 'Все бренды') {
//                            sel_value = 'Все бренды';
//                        } else sel_value = menuitem[1];

				category.options[i] = new Option(menuitem[1],menuitem[1]);			
				if (menuitem[1] == selected) {
					category.options[i].selected = true;
				}
			}
		}		
}

//---------------------------------------------------

function updatePage(){
	if (xmlHttp.readyState == 4) {
	
		var response = xmlHttp.responseText.split("||");
		
//alert(response);

		var selected_sub = response[0];
		var selected_brand = response[1];
		var selected_price = response[2];
		//alert(selected_sub);

		//action_List(response[2],'Category', 0);
		action_List(response[3],'Sub', selected_sub);
		action_List(response[4],'Brand', selected_brand);
		action_List(response[5],'Price', selected_price);

	}
}

//---------------------------------------------------

function sendQuery () {

	var category = document.getElementById('Category');
	var sub = document.getElementById('Sub');
	var brand = document.getElementById('Brand');
	var price = document.getElementById('Price');
	var param_t = document.getElementById('param_t');
 	var param_v = param_t.value;

	var categoryId = 0;
	var brand_name = ''; 
	var prices = price.options[price.selectedIndex].text.split('-');
	var priceFrom = prices[0];
	var priceTo = prices[1];
	
	if (priceFrom == 'Все категории' || priceTo == 'Все категории') {
		priceFrom = '';
		priceTo = '';
	} 
	
	
	if (sub.selectedIndex > 0) {
		categoryId = sub.options[sub.selectedIndex].value;
	} else {
			categoryId = category.options[category.selectedIndex].value;
		} 
	
	brand_name = brand.options[brand.selectedIndex].value;

	if ((sub.options[sub.selectedIndex].text == 'Все бренды') || (sub.options[sub.selectedIndex].text == 'All') ){
		categoryId = category.options[category.selectedIndex].value;
	} else {
		categoryId = sub.options[sub.selectedIndex].value;
	}
	
	
	if ((brand_name == 'Все бренды') || (brand_name == 'All')) { brand_name = ''; }
 
	window.location.href = '?categoryID='+categoryId+'&search=1&search_name=&search_price_from='+priceFrom+'&search_price_to='+priceTo
				+'&'+param_v+'='+brand_name+'&search_in_subcategory=1&advanced_search_in_category=%CD%E0%E9%F2%E8%21&Category='+category.options[category.selectedIndex].value
				+'&Sub='+sub.options[sub.selectedIndex].value+"&Brand="+brand_name;


}

//---------------------------------------------------

function getSub (selObj) {
	if(selObj.options[selObj.selectedIndex].value > 0)
	    sendRequest("ajax_filter.php?Category=" + selObj.options[selObj.selectedIndex].value);
}                                             

//---------------------------------------------------

function getSubCategories(selObj) {
	if(selObj.options[selObj.selectedIndex].value > 0){
		sendRequest("ajax_filter.php?Sub=" + selObj.options[selObj.selectedIndex].value);
	}
}

//---------------------------------------------------

function getBrand(selObj) {
//alert(selObj.options[selObj.selectedIndex].value);
	if(selObj.options[selObj.selectedIndex].value)
	     sendRequest("ajax_filter.php?Brand=" + selObj.options[selObj.selectedIndex].value);
}

//---------------------------------------------------

function getPrice(selObj) {
	if(selObj.options[selObj.selectedIndex].value) {
	    var brand = document.getElementById('Brand'); 
	    var brand_name = brand.options[brand.selectedIndex].value;
	    sendRequest("ajax_filter.php?Price=" + selObj.options[selObj.selectedIndex].value+"&Brand="+brand_name);
	}
}

//---------------------------------------------------
//http://www.neoncomputers.az/ajax_filter.php?Category=5370
//http://www.neoncomputers.az/ajax_filter.php?Sub=5365
