// JavaScript Document

/**/

function comboboxVars(obj) {
	outputArr = Array()
	
	for (i=0;i<obj.options.length;i++) {
		if (obj.options[i].selected == true) {
			outputArr.push(obj.options[i].value)
		}
	}
	return outputArr
}

function comboString(ar, fieldname) {
	str = "";
	for (n=0;n < ar.length; n++) {
		str = str + fieldname+"[]="+ar[n]+"&"	
	}
	if (ar.length < 1) {
		str = fieldname+"[]=&"	
	}
	return str
}

function advSearch() {
	// get vars and put in post string
	c = document.getElementById('country_adv')
	country_sel = c.options[c.selectedIndex].value
	// quick validate check to make sure country is selected
	if (country_sel == "null") {
		alert('Country is required');
		return
	}
	/*counrty_sel = "united Kingdom";*/
	
	
	var towncity = document.getElementById('towncity').value
	
	var price_sel = document.getElementById('price').value
	
	b = document.getElementById('beds')
	bed_sel = b.options[b.selectedIndex].value;
	
	br = document.getElementById('bedrooms')
	bedr_sel = br.options[br.selectedIndex].value;
	
	s = document.getElementById('sleeps')
	sleeps_sel = s.options[s.selectedIndex].value
	//get the operands
	bedop = document.getElementById('bedsoperand')
	bedop_sel = bedop.options[bedop.selectedIndex].value
	
	sleepsop = document.getElementById('sleepsoperand')
	sleepsop_sel = sleepsop.options[sleepsop.selectedIndex].value
	
	bedroomsop = document.getElementById('bedroomsoperand')
	bedroomsop_sel = bedroomsop.options[bedroomsop.selectedIndex].value
	
	facil = document.getElementById('fac')
	facil = comboboxVars(facil)
	facilities = comboString(facil, 'facilities');
	
	actv = document.getElementById('act')
	actv = comboboxVars(actv)
	activities = comboString(actv, 'activities')
	
	
	st = document.getElementById('sty')
	st = comboboxVars(st)
	style = comboString(st, 'style')
	
	post =facilities + style + activities + "country_adv="+country_sel+"&towncity="+towncity+"&price="+price_sel+"&beds="+bed_sel+"&bedrooms="+bedr_sel+"&sleeps="+sleeps_sel+"&bedsoperand="+bedop_sel+"&sleepsoperand="+sleepsop_sel+"&bedroomsoperand="+bedroomsop_sel

	
	//now need to build ajax request
	
	url = "advancedsearch_script.php";
	
	loadspace = document.getElementById('searchresults')
	loadspace.innerHTML = "";
	loadspace.innerHTML = "<div class=\"loadergifwrapper\" ><img src=\""+gPath+"images/ajax-loader.gif\" alt=\"loading...\"/></div>";
	PAGExmlHttp=GetXmlHttpObject()

	if (PAGExmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	} 
	
	PAGExmlHttp.onreadystatechange=function () { 
		if (PAGExmlHttp.readyState==4) { 
			//loadspace.innerHTML=PAGExmlHttp.responseText;
			//alert(PAGExmlHttp.responseText);
			//resText = document.getElementById("restext");
			centreAd = country_sel
			//set zoom and centre
			//se the centre and zoom
			zoom = 5
			if (country_sel == "United States of America") { //zoom out for USA
				zoom = 4;
			}
			if (country_sel == "United Kingdom") {
				centreAd = "isle of man, united kingdom";	
			} else {
				centreAd = country_sel
			}
			
			
			if (towncity != "" && towncity != "Town or City") {
				centreAd = towncity+", "	+ country_sel
				zoom=12
			}
			
			var xml = PAGExmlHttp.responseXML; 
			var markers = xml.documentElement.getElementsByTagName("marker");
			
			//build search list container
			resultsHeader(markers.length)
			
			initializeAdv (centreAd, zoom)
			//display markers
		
			
			arrayKeys = Array();
			//fill with integers upto maerker total
			for (n=0;n<markers.length;n++) {
				arrayKeys.push(n)
			}
			
			//shuffle arrayKeys 
			arrayKeys.sort(function() {return 0.5 - Math.random()})
			
			for (var n = 0; n < arrayKeys.length; n++) {
				i = arrayKeys[n];
				var address = HtmlEncode(markers[i].getAttribute("address")); 
				var type = markers[i].getAttribute("type");
				var title = HtmlEncode(markers[i].getAttribute("title"));
				var pid = markers[i].getAttribute("id");
				var ownerid = markers[i].getAttribute("ownerid");
				var owneridnum = markers[i].getAttribute("owneridnum");
				var Lprice = HtmlEncode(markers[i].getAttribute("lowprice"));
				var cIcon = HtmlEncode(markers[i].getAttribute("curicon"));
				var cText = HtmlEncode(markers[i].getAttribute("curtext"));
				var mainpic = markers[i].getAttribute("mainpic");
				var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("long")));
				var M = addMarker(point, title, address,pid,type, mainpic, ownerid, owneridnum, 'return')
				searchResList(title,address,mainpic,Lprice,cIcon,cText, owneridnum, pid, point, M)
				
			}
			poststring = encodeURIComponent(post)
			
			storeRes("\"reloadAdvancedSearch('"+poststring+"','"+country_sel+"')\"")
			//add a button at the endof the res
			addCLearResBtn()
		}

	}
	url=url+"?sid="+Math.random();
	
	PAGExmlHttp.open("POST",url,true);
	//PAGExmlHttp.send(null);	
	PAGExmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	PAGExmlHttp.send(post)
	/**/
	
}

function reloadAdvancedSearch(postString, country_sel) {
	
	url = "advancedsearch_script.php";
	
	
	loadspace = document.getElementById('searchresults')
	loadspace.innerHTML = "";
	loadspace.innerHTML = "<div class=\"loadergifwrapper\" ><img src=\""+gPath+"images/ajax-loader.gif\" alt=\"loading...\"/></div>";
	PAGExmlHttp=GetXmlHttpObject()
	
	if (PAGExmlHttp==null)  {
		  alert ("Your browser does not support AJAX!");
		  return;
	} 
	PAGExmlHttp.onreadystatechange=function () { 
		if (PAGExmlHttp.readyState==4) { 
			
			var xml = PAGExmlHttp.responseXML; 
			var markers = xml.documentElement.getElementsByTagName("marker");
			centreAd = country_sel
			//set zoom and centre
			//se the centre and zoom
			zoom = 5
			if (country_sel == "United States of America") { //zoom out for USA
				zoom = 4;
			}
			if (country_sel == "United Kingdom") {
				centreAd = "isle of man, united kingdom";	
			} else {
				centreAd = country_sel
			}
			
			
			//build search list container
			resultsHeader(markers.length)
		
			initializeAdv (centreAd, zoom)
			//display markers
		
			
			arrayKeys = Array();
			//fill with integers upto maerker total
			for (n=0;n<markers.length;n++) {
				arrayKeys.push(n)
			}
			
			//shuffle arrayKeys 
			arrayKeys.sort(function() {return 0.5 - Math.random()})
			
			for (var n = 0; n < arrayKeys.length; n++) {
				i = arrayKeys[n];
				var address = HtmlEncode(markers[i].getAttribute("address")); 
				var type = markers[i].getAttribute("type");
				var title = HtmlEncode(markers[i].getAttribute("title"));
				var pid = markers[i].getAttribute("id");
				var ownerid = markers[i].getAttribute("ownerid");
				var Lprice = HtmlEncode(markers[i].getAttribute("lowprice"));
				var cIcon = HtmlEncode(markers[i].getAttribute("curicon"));
				var cText = HtmlEncode(markers[i].getAttribute("curtext"));
				var mainpic = markers[i].getAttribute("mainpic");
				var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("long")));
				var M = addMarker(point, title, address,pid,type, mainpic, ownerid, 'return')
				searchResList(title,address,mainpic,Lprice,cIcon,cText, ownerid, pid, point, M)
				
			}
			addCLearResBtn()
		}

	}
	url=url+"?sid="+Math.random();
	
	PAGExmlHttp.open("POST",url,true);
	//PAGExmlHttp.send(null);	
	PAGExmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	PAGExmlHttp.send(postString)
}

function openSearch() {
	clearInterval(expandContract)
	btn = document.getElementById('advsearch_link');
	if (btn.className == "adv_off") {
		btn.className = "adv_on"
		openBox()
	} else {
		closeBox()
		//document.getElementById('searchresults').innerHTML = "";
	}
}
var expandContract = null
var contract = null

function growBox() {
	box = document.getElementById('advancedsearch_container');
	h = box.style.height
	
	if (h=="") {
		h = 0;
		box.style.paddingTop = "8px";
	} else {
		h = parseFloat(h);
		if (h==0) {
			box.style.paddingTop = "8px";
		}	
	}
	h += 22
	
	if (h <460) {
		box.style.height = h+"px"
	} else {
		box.style.height = "455px"
		clearInterval(expandContract)	
	}
	
}

function openBox() {
	expandContract = setInterval("growBox()",70)
}

function shrinkBox() {
	box = document.getElementById('advancedsearch_container');
	h = box.style.height
	if (h=="") {
		h = 0;
		box.style.paddingTop = "0px";
	} else {
		h = parseFloat(h);	
	}
	h -= 22
	
	if (h > 0) {
		box.style.height = h+"px"
	} else {
		box.style.height = "0px"
		box.style.paddingTop = "0px";
		document.getElementById('advsearch_link').className = "adv_off";
		clearInterval(expandContract)	
	}
	
}

function closeBox() {
	expandContract = setInterval("shrinkBox()",70)
}

