function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

function getProductCount()
{
	
	if(document.filterForm.ceiling.checked){ _optCeiling = 1; } else { _optCeiling = 0; }
	if(document.filterForm.inline.checked){ _optInline = 1; } else { _optInline = 0; }
	if(document.filterForm.wall.checked){ _optWall = 1; } else { _optWall = 0; }
	if(document.filterForm.central.checked){ _optCentral = 1; } else { _optCentral = 0; }
		
	_optBathCount = document.filterForm.bath_count.options[document.filterForm.bath_count.selectedIndex].value;
	
	len = document.filterForm.cfm_range.length;
	chosen = "";
	
	for (i = 0; i <len; i++) {
		if (document.filterForm.cfm_range[i].checked) {
			chosen = document.filterForm.cfm_range[i].value;
		}
	}
	_optCFM = chosen;
	
	len = document.filterForm.duct_size.length;
	chosen = "";
	
	for (i = 0; i <len; i++) {
		if (document.filterForm.duct_size[i].checked) {
			chosen = document.filterForm.duct_size[i].value;
		}
	}
	_optDuct = chosen;
	
	if(document.filterForm.lights.checked){ _optLights = 1; } else { _optLights = 0; }
	if(document.filterForm.heater.checked){ _optHeater = 1; } else { _optHeater = 0; }
	if(document.filterForm.nightlight.checked){ _optNightlight = 1; } else { _optNightlight = 0; }
	if(document.filterForm.energystar.checked){ _optEnergystar = 1; } else { _optEnergystar = 0; }
	if(document.filterForm.humidity.checked){ _optHumidity = 1; } else { _optHumidity = 0; }
	if(document.filterForm.motion.checked){ _optMotion = 1; } else { _optMotion = 0; }
	
	len = document.filterForm.brands.length;
	chosen = "";
	
	for (i = 0; i <len; i++) {
		if (document.filterForm.brands[i].checked) {
			if(chosen != "")
			{
				chosen = chosen + ',';
			}
			chosen = chosen + document.filterForm.brands[i].value;
		}
	}
	
	_optBrands = chosen;
	
	len = document.filterForm.max_price.length;
	chosen = "";
	
	for (i = 0; i <len; i++) {
		if (document.filterForm.max_price[i].checked) {
			chosen = document.filterForm.max_price[i].value;
		}
	}
	_optPrice = chosen;
		
	url = 'http://www.bathroomfanexperts.com/finder_count.php?ceiling=' + _optCeiling + '&inline=' + _optInline + '&wall=' + _optWall + '&central=' + _optCentral + '&cfm_range=' + _optCFM + '&duct_size=' + _optDuct + '&lights=' + _optLights + '&heater=' + _optHeater + '&nightlight=' + _optNightlight + '&energystar=' + _optEnergystar + '&humidity=' + _optHumidity + '&max_price=' + _optPrice + '&motion=' + _optMotion + '&brands=' + _optBrands + '&bath_count=' + _optBathCount;
					
	httpRequest = createRequestObject();
	httpRequest.open('get', url);
	httpRequest.onreadystatechange = returnProductCount;
	httpRequest.send(null);
		
}

function returnProductCount()
{
	if(httpRequest.readyState == 4){ //Finished loading the response
		try
		{
			var response = httpRequest.responseXML;
			var prod = response.getElementsByTagName('products');

			if(prod.length > 0){
				/* this means I received a response from the server */

				var prod_count = parseFloat(unescape(prod[0].childNodes[0].firstChild.nodeValue));
				var min_price = parseFloat(unescape(prod[0].childNodes[1].firstChild.nodeValue));
				var max_price = parseFloat(unescape(prod[0].childNodes[2].firstChild.nodeValue));
				
				if(prod_count != 1){
					output = '<b>' + String(prod_count) + ' models found</b>';
				} else {
					output = '<b>1 model found</b>';
				}
				if(min_price > 0){
					output = output + ' ($' + addCommas(min_price.toFixed(2));
					if(max_price > min_price){
						output = output + ' to $' + addCommas(max_price.toFixed(2));	
					}	
					output += ')';
				}
				
				document.getElementById('matchResults').innerHTML = output;

			}
		}
		catch(e)
		{
		}
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function storeSettings()
{
	try
	{
	
		if(document.filterForm.ceiling.checked){ _optCeiling = 1; } else { _optCeiling = 0; }
		if(document.filterForm.inline.checked){ _optInline = 1; } else { _optInline = 0; }
		if(document.filterForm.wall.checked){ _optWall = 1; } else { _optWall = 0; }
		if(document.filterForm.central.checked){ _optCentral = 1; } else { _optCentral = 0; }
					
		_optBathCount = document.filterForm.bath_count.options[document.filterForm.bath_count.selectedIndex].value;
		
		len = document.filterForm.cfm_range.length;
		chosen = "";
		
		for (i = 0; i <len; i++) {
			if (document.filterForm.cfm_range[i].checked) {
				chosen = document.filterForm.cfm_range[i].value;
			}
		}
		_optCFM = chosen;
		
		len = document.filterForm.duct_size.length;
		chosen = "";
		
		for (i = 0; i <len; i++) {
			if (document.filterForm.duct_size[i].checked) {
				chosen = document.filterForm.duct_size[i].value;
			}
		}
		_optDuct = chosen;
		
		if(document.filterForm.lights.checked){ _optLights = 1; } else { _optLights = 0; }
		if(document.filterForm.heater.checked){ _optHeater = 1; } else { _optHeater = 0; }
		if(document.filterForm.nightlight.checked){ _optNightlight = 1; } else { _optNightlight = 0; }
		if(document.filterForm.energystar.checked){ _optEnergystar = 1; } else { _optEnergystar = 0; }
		if(document.filterForm.humidity.checked){ _optHumidity = 1; } else { _optHumidity = 0; }
		if(document.filterForm.motion.checked){ _optMotion = 1; } else { _optMotion = 0; }
		
		len = document.filterForm.brands.length;
		chosen = "";
		
		for (i = 0; i <len; i++) {
			if (document.filterForm.brands[i].checked) {
				if(chosen != "")
				{
					chosen = chosen + ',';
				}
				chosen = chosen + document.filterForm.brands[i].value;
			}
		}
		
		_optBrands = chosen;
		
		len = document.filterForm.max_price.length;
		chosen = "";
		
		for (i = 0; i <len; i++) {
			if (document.filterForm.max_price[i].checked) {
				chosen = document.filterForm.max_price[i].value;
			}
		}
		_optPrice = chosen;
				
		url = 'http://www.bathroomfanexperts.com/store_settings.php?ceiling=' + _optCeiling + '&inline=' + _optInline + '&wall=' + _optWall + '&central=' + _optCentral + '&cfm_range=' + _optCFM + '&duct_size=' + _optDuct + '&lights=' + _optLights + '&heater=' + _optHeater + '&nightlight=' + _optNightlight + '&energystar=' + _optEnergystar + '&humidity=' + _optHumidity + '&max_price=' + _optPrice + '&motion=' + _optMotion + '&brands=' + _optBrands + '&bath_count=' + _optBathCount;
						
		httpStore = createRequestObject();
		httpStore.open('get', url);
		httpStore.send(null);
	}
	catch(e)
	{
		
	}
	
}

function loadSettings()
{
	
	url = 'http://www.bathroomfanexperts.com/get_settings.php';	
					
	httpLoad = createRequestObject();
	httpLoad.open('get', url);
	httpLoad.onreadystatechange = returnSettings;
	httpLoad.send(null);
		
}

function returnSettings()
{
	
	if(httpLoad.readyState == 4){ //Finished loading the response
		try
		{
			var response = httpLoad.responseXML;
									
			var stg = response.getElementsByTagName('settings');
			
			if(stg.length > 0){
				/* this means I received a response from the server */
				
				_optCeiling = stg[0].childNodes[0].firstChild.nodeValue.toString();
				_optInline = stg[0].childNodes[1].firstChild.nodeValue.toString();
				_optWall = stg[0].childNodes[2].firstChild.nodeValue.toString();
				_optCentral = stg[0].childNodes[3].firstChild.nodeValue.toString();
				_optCFM = stg[0].childNodes[4].firstChild.nodeValue.toString();
				_optDuct = stg[0].childNodes[5].firstChild.nodeValue.toString();
				_optLights = stg[0].childNodes[6].firstChild.nodeValue.toString();
				_optHeater = stg[0].childNodes[7].firstChild.nodeValue.toString();
				_optNightlight = stg[0].childNodes[8].firstChild.nodeValue.toString();
				_optEnergystar = stg[0].childNodes[9].firstChild.nodeValue.toString();
				_optHumidity = stg[0].childNodes[10].firstChild.nodeValue.toString();
				_optBrands = stg[0].childNodes[13].firstChild.nodeValue.toString();
				_optPrice = stg[0].childNodes[11].firstChild.nodeValue.toString();
				_optMotion = stg[0].childNodes[12].firstChild.nodeValue.toString();					
				_optBathCount = stg[0].childNodes[14].firstChild.nodeValue.toString();					
				
				/* now I have to reset the form */
				
				if(_optCeiling == "1") {
					document.filterForm.ceiling.checked = true;
				} else {
					document.filterForm.ceiling.checked = false;
				}
				
				if(_optInline == "1") {
					document.filterForm.inline.checked = true;
				} else {
					document.filterForm.inline.checked = false;
				}
				
				if(_optWall == "1") {
					document.filterForm.wall.checked = true;
				} else {
					document.filterForm.wall.checked = false;
				}
				
				if(_optCentral == "1") {
					document.filterForm.central.checked = true;
				} else {
					document.filterForm.central.checked = false;
				}				
				
				switch(_optBathCount)
				{
					case "2":
						document.filterForm.bath_count.selectedIndex = 1;
						break;
					case "3":
						document.filterForm.bath_count.selectedIndex = 2;
						break;
					case "4":
						document.filterForm.bath_count.selectedIndex = 3;
						break;
					case "5":
						document.filterForm.bath_count.selectedIndex = 4;
						break;
					default:
						document.filterForm.bath_count.selectedIndex = 0;
						break;
				}
				
				len = document.filterForm.cfm_range.length;
				found = false;
				
				for (i = 0; i <len; i++) {
					if (document.filterForm.cfm_range[i].value == _optCFM) {
						document.filterForm.cfm_range[i].checked = true;
						found = true;
					}
				}				
				if(found == false)
				{
					document.filterForm.cfm_range[0].checked = true;
				}
				
				len = document.filterForm.duct_size.length;
				found = false;
				
				for (i = 0; i <len; i++) {
					if (document.filterForm.duct_size[i].value == _optDuct) {
						document.filterForm.duct_size[i].checked = true;
						found = true;
					}
				}				
				if(found == false)
				{
					document.filterForm.duct_size[0].checked = true;
				}
								
				if(_optLights == "1") {
					document.filterForm.lights.checked = true;
				} else {
					document.filterForm.lights.checked = false;
				}
				if(_optHeater == "1") {
					document.filterForm.heater.checked = true;
				} else {
					document.filterForm.heater.checked = false;
				}
				if(_optNightlight == "1") {
					document.filterForm.nightlight.checked = true;
				} else {
					document.filterForm.nightlight.checked = false;
				}
				if(_optEnergystar == "1") {
					document.filterForm.energystar.checked = true;
				} else {
					document.filterForm.energystar.checked = false;
				}
				if(_optHumidity == "1") {
					document.filterForm.humidity.checked = true;
				} else {
					document.filterForm.humidity.checked = false;
				}								
				if(_optMotion == "1") {
					document.filterForm.motion.checked = true;
				} else {
					document.filterForm.motion.checked = false;
				}		
								
				len = document.filterForm.brands.length;				
				for (i = 0; i <len; i++) {
					if (_optBrands.indexOf(document.filterForm.brands[i].value) != -1) {
						document.filterForm.brands[i].checked = true;
					}
				}
				
				len = document.filterForm.max_price.length;
				found = false;
				
				for (i = 0; i <len; i++) {
					if (document.filterForm.max_price[i].value == _optPrice) {
						document.filterForm.max_price[i].checked = true;
						found = true;
					}
				}				
				if(found == false)
				{
					document.filterForm.max_price[0].checked = true;
				}
				
				buildBrands();
				getProductCount();
				
			}
		}
		catch(e)
		{
		}
	}
}

function B( objAny ){
	if(objAny.toString() == "true"){
		return(true);
	}
	if(objAny.toString() == "false"){
		return(false);
	}
}

function buildBrands()
{
	len = document.filterForm.brands.length;
	chosen = "";
	
	for (i = 0; i <len; i++) {
		if (document.filterForm.brands[i].checked) {
			if(chosen != "")
			{
				chosen = chosen + ',';
			}
			chosen = chosen + document.filterForm.brands[i].value;
		}
	}
	
	document.filterForm.brand.value = chosen;
}