// Preload loading image
loading = new Image(); 
loading.src = "/images/loading.gif";

function ajaxGet(url, id) {
//window.alert('Test');

// Loading bars
document.getElementById(id).innerHTML = '<center><img src="/images/loading.gif" alt="Loading" /></center>';
function connect() {
	http.open("GET", url, true);
	http.onreadystatechange = getHttpRes;
	http.send(null);
}

function getHttpRes() {
	if (http.readyState == 4) { 
		res = http.responseText;  
		document.getElementById(id).innerHTML = res;
	}
}

function getXHTTP() {
	var xhttp;
	try {   
	// The following "try" blocks get the XMLHTTP object for various browsers�
		xhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
     	try {
        	xhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e2) {
 	 // This block handles Mozilla/Firefox browsers...
	try {
	      	xhttp = new XMLHttpRequest();
	} catch (e3) {
	   	xhttp = false;
	}
      	}
    }	
  return xhttp; // Return the XMLHTTP object
}
var http = getXHTTP(); // This executes when the page first loads.
connect();
}

function rand (n) {
	return ( Math.floor (Math.random() * n + 1 ));
}

function applyFilter() {
	var f = document.filter;
	var query = '&bsize=' + f.bedsize.value + '&bounce=' + f.bounce.value + 
				'&manufacturer=' + f.manufacturer.value + 
				'&all_types=' + f.alltypes.checked + 
				'&type_ps=' + f.pocketsprung.checked + 
				'&type_mf=' + f.memoryfoam.checked + 
				'&type_oc=' + f.opencoilsprung.checked + 
				'&type_lf=' + f.latexfoam.checked + 
				'&type_b=' + f.budget.checked +
				'&delivery=' + f.express.checked +
				'&pricefrom=' + f.price_from.value +
				'&priceto=' + f.price_to.value +
				'&sort=' + f.order.value +
				'&filter=true';
	ajaxGet('/shop/mattress_results.php?cc=' + rand(999999999999999) + query,'ajaxContainer');
	//alert('Filters applied!');	
}

function showTypes() {
	if(document.getElementById('mattressTypes').style.display == 'block') {
		document.getElementById('mattressTypes').style.display='none';
		document.getElementById('mattressTypes').style.visibility='hidden';
		document.getElementById('mtDropdown').innerHTML='Select Mattress Type [+]';
		return false;
	} else {
		document.getElementById('mattressTypes').style.display='block';
		document.getElementById('mattressTypes').style.visibility='visible';
		document.getElementById('mtDropdown').innerHTML='Select Mattress Type [&minus;]';
		return false;
	}
}

function hideTypes() {
	if(document.getElementById('mattressTypes').style.display='block') {
		document.getElementById('mattressTypes').style.display='none';
		document.getElementById('mtDropdown').innerHTML='Select Mattress Type [+]';
	}
}

function uncheckAll() {
	if(document.getElementById('allTypes').checked='checked') {
		document.getElementById('allTypes').checked='';
	}
}

function checkNum(el) {
	var string = el.value;
	if(isNaN(string)) {
		el.value='';
		alert('Error: Input must be a number. Please try again.');
		el.select();
		return false;
	}
}
