var xmlHttp;
var tpt; 

function ajax_removeAllOptions(selectbox) {
	var i;
	for(i=selectbox.options.length-1;i>=0;i--) {
		selectbox.remove(i);
	}
}
function ajax_addOption(selectbox, value, text, index) {
	selectbox.options[index] = new Option(text, value);
}

function Request(listingstatus, propertytype) { 
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX - Please use the Advanced Search!");
		return;
	} 

	
	tpt = propertytype;
	var url="ajaxsearch.php?" + "listingstatus=" + listingstatus +"&propertytype=" + propertytype;

	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	ajax_choosePriceRange();
}

function stateChanged() { 
	//ajax_removeAllOptions(document.ajaxsearch.elements['propertytype[]']);
	ajax_removeAllOptions(document.ajaxsearch.elements['area[]']);
	if (xmlHttp.readyState==4) { 
	
		data = xmlHttp.responseText.split("^");
	
		for (i=0; i<data.length; i++) {
			elementoptions = data[i].split("!");
			for (n=0; n<elementoptions.length; n++) {
				theoption = elementoptions[n].split("~");
				
				theoptiontype = theoption[0].split("|");
				(theoptiontype[0] == "suburb") ? fmted_area = "\ \ " + theoption[1] : fmted_area = theoption[1];
				ajax_addOption(document.ajaxsearch.elements['area[]'], theoption[0], fmted_area, n);
			}
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	return xmlHttp;
}

function ajax_choosePriceRange() {
	
	var store = new Array();
	
	store[0] = new Array(
	'Any',
	'Below R500 000',
	'R500 000 - R1 000 000',
	'R1 000 000 - R1 500 000',
	'R1 500 000 - R2 000 000',
	'R2 000 000 - R2 500 000',
	'R2 500 000 - R3 000 000',
	'R3 000 000 - Above'
	);
	
	store[1] = new Array(
	'',
	'0|500000',
	'500000|1000000',
	'1000000|1500000',
	'1500000|2000000',
	'2000000|2500000',
	'2500000|3000000',
	'3000000|99990000000'
	);
	
	store[2] = new Array(
	'Any',
	'Below R2500',
	'R2 500 - R5 000',
	'R5 000 - R7 500',
	'R7 500 - R10 000',
	'R10 000 - R15 000',
	'R15 000 - R20 000',
	'R20 000 - Above'
	);
	
	store[3] = new Array(
	'',
	'0|2500',
	'2500|5000',
	'5000|7500',
	'7500|10000',
	'10000|15000',
	'15000|20000',
	'20000|99990000000'
	);	
	
	store[4] = new Array(
	'Any',
	'Below R250',
	'R250 - R500',
	'R500 - R750',
	'R750 - R1 000',
	'R1 000 - R1 500',
	'R1 500 - R2 000',
	'R2 000 - Above'
	);
	
	store[5] = new Array(
	'',
	'0|250',
	'250|500',
	'500|750',
	'750|1000',
	'1000|1500',
	'1500|2000',
	'2000|99990000000'
	);	
	
	if(document.ajaxsearch.listingstatus.value=="For Sale|Residential" || document.ajaxsearch.listingstatus.value=="For Sale|Commercial") {
		for(i=0; i<8; i++)
			document.ajaxsearch.pricerange[i] = new Option(store[0][i], store[1][i]);
	
	} else if (document.ajaxsearch.listingstatus.value=="To Let|Residential" || document.ajaxsearch.listingstatus.value=="To Let|Commercial") {
		for(i=0; i<8; i++)
			document.ajaxsearch.pricerange[i] = new Option(store[2][i], store[3][i]);
	} else if (document.ajaxsearch.listingstatus.value=="To Let|Holiday") {
		for(i=0; i<8; i++)
			document.ajaxsearch.pricerange[i] = new Option(store[4][i], store[5][i]);
	} else {
		for(i=0; i<8; i++)
			document.ajaxsearch.pricerange[i] = new Option(store[0][i], store[1][i]);
	}
}