﻿
function get_querystring(q){
	var pair = [];var obj = {};
	if(q.length>1){
		q = q.substring(1, q.length);
		for(var i=0;i<q.split('&').length;i++){
			pair.push(q.split('&')[i]);
		}
	}
	for(var j in pair){
		search_option.selected[pair[j].split('=')[0]]=pair[j].split('=')[1];
	}
	return obj;
}
search_option = {
	selected:{},//{'category':'','manufacturer':''},//['industry','category','manufacturer'],
	gallery_filter:'',
	enable:3
}
function init(){
	document.getElementById('gallery').innerHTML = '<img src="images/loading.gif">';
	if(location.search){
		get_querystring(window.location.search);
		filter();//alert('filtering');
	}else{
		get_data('php/category.php');//alert('wtf');
		document.getElementById('gallery').innerHTML = 'Use the drop down menus to filter your search results.';

	}
}
function get_data(page){
	document.getElementById('gallery').innerHTML = '<img src="images/loading.gif">';
	//alert(page);
	loadXMLDoc(page,'process_data()');
	}
function process_data(){
	var data = eval('('+document.myData+')');
	for(var i in data){//alert(i);
		create_select(data[i],i);
		}
		if(search_option.selected){//alert(eval('search_option.selected'));
			for(var j in search_option.selected){//alert(j);
				var oElement         = document.getElementById(j)
				oElement.value       = search_option.selected[j];
				//oElement.style.color = '#009';		
				//alert(j)//if(
				//oElement.disabled    = 'disabled';
			}
		}
	if(search_option.gallery_filter!=''){
		make_gallery(search_option.gallery_filter);//oSearch.query=str;	
	}
	black_out();	
	}
function create_select(content,elem){
	//expecting content as an object containing key value pairs
	//elem as a string matching the id of a select element to replace
	var container   = document.getElementById(elem);
	var _select     = document.createElement('select');
	var _option     = document.createElement('option');
		_select.options.add(_option);
		_select.id        = elem;
		_option.innerHTML = elem+'...';
		_option.value     = '';
		container.parentNode.replaceChild(_select,container);
		//for(var i=0;i<content.length;i++){alert(content[i].length);//alert(content[i]+','+i);
		for(var i in content){
			var _option = document.createElement('option');
			_select.options.add(_option);
			_select.onchange = function(){action(this)};
			_option.innerHTML = content[i]['innerHTML'];
			_option.value     = content[i]['value'];
		}

}
function action(obj){
	document.getElementById('gallery').innerHTML = '<img src="images/loading.gif">';
	//alert('action');
	//document.next_action.prototype = function(){alert('next')}
	search_option.selected[obj.id] = obj.value;
	/*for(var i in search_option.selected){
	alert(i+' '+search_option.selected[i])}*/
	//if(XMLHttpRequest){XMLHttpRequest.prototype.thistypeof = 'json';}
	filter();
	//black_out();
	//get_data('php/category.php');
	//search_option.default_value = ;
	//alert(search_option.selected)
}
function filter(){
	var str = '';
		//alert(typeof oSearch.lastUpdate+' '+oSearch.lastUpdate.length);
	//if(search_option.selected){//alert(search_option.selected);
		selectElement = document.body.getElementsByTagName('select');//alert(selectElement.length);
		for(var i = 0;i<selectElement.length;i++){
			if(selectElement[i].value!=''){//alert(selectElement[i].id+'='+selectElement[i].value);
				if(str==''){
					str+=('?'+selectElement[i].id+'='+selectElement[i].value);
				}else{
					str+=('&'+selectElement[i].id+'='+selectElement[i].value);
				}
			}
		//}
	}		
	if(str==''){
		for(var j in search_option.selected){
			if(str==''){
				str+='?'+j+'='+search_option.selected[j];
			}else{
				str+='&'+j+'='+search_option.selected[j];
			}
			//alert(j);
		}
		//alert(search_option.selected)
	}
	search_option.gallery_filter = str;
	get_data('php/category.php'+str);//alert(str);
}
function reset_search(){
	document.getElementById('gallery').innerHTML = '<img src="images/loading.gif">';
	search_option.selected = {};//category':'','manufacturer':''};
	search_option.gallery_filter='';
	get_data('php/category.php');
	document.getElementById('gallery').innerHTML = 'Use the drop down menus to filter your search results.';
}
function black_out(){
	search_option.enable = '';
	var _table = document.getElementsByTagName('table');
	for(var i = 0;i<_table.length;i++){
	if(_table[i].className == 'product_selector'){
		_select = _table[i].getElementsByTagName('select');
		for(var j = _select.length-1;j>=0;j--){//alert(_select[j].value);
			if(_select[j].value!=''){
				search_option.enable = 'disabled';
				}
			_select[j].disabled = search_option.enable;
			}
		}
	}
}

