var query = {
	pageSize:		10,
	nPages:			0,
	currentPage:	1,
	searchType:		"residential",
	
	offices:	new Array(),
	cache:		new Array(),
		
	elements: {
		area:				"78",
		areaquick:			"78-quick",
		city:				"1109",
		streetnumber:		"246",
		streetname:			"245",
		zipcode:			"42",
		listpricemin:		"161-min",
		listpricemax:		"161-max",
		bedrooms:			"30",
		bathrooms:			"1129",
		listpriceminquick:	"161-min-quick",
		listpricemaxquick:	"161-max-quick",
		bedroomsquick:		"30-quick",
		bathroomsquick:		"1129-quick",
		squarefeetmin:		"1123-min",
		squarefeetmax:		"1123-max",
		garages:			"138",
		yearbuiltmin:		"51-min",
		yearbuiltmax:		"51-max",
		type:				"205",
		style:				"247",
		newlistings:		"newlistings",
		virtualtour:		"57",
		openhouse:			"1296",
		dishwasher:			"107",
		microwave:			"168",
		oven:				"192",
		regrigerator:		"207",
		fenced:				"127",
		sprinkler:			"265",
		well:				"274",
		fireplace:			"255",
		basement:			"284",
		mlnumber:			"169",
		searchresults:		"searchresults"
	},
	
	setEnabledBySearchType: function(){
		for(var i in constants.searchElements){
			var el = util.get(constants.searchElements[i].id);
			var en = false;
			for(var j=0; j<constants.searchElements[i].enabled.length; j++){
				if(query.searchType == constants.searchElements[i].enabled[j])
					en = true;
			}
			el.disabled = !en;
		}
	},
	
	/** retrieve the respective search url
	  */
	getSearchUrl: function(){
		var url = constants.searchTypes.residential;
		switch(query.searchType){
			case constants.searchTypes.residential:
				url = constants.url.search.residential;
				break
			case constants.searchTypes.multifamily:
				url = constants.url.search.multifamily;
				break;
			case constants.searchTypes.openhouse:
				url = constants.url.search.openhouse;
				break;
			case constants.searchTypes.lots:
				url = constants.url.search.lots;
				break;
			default:
				url = constants.url.search.residential;
				break;
		}
		return url;
	},
	/** set the search type based on the radio button selected
	  */
	setSearchType: function(){
		var type = null;
		for(var i in constants.searchTypes){
			if(util.get("search."+constants.searchTypes[i]).checked){
				type = constants.searchTypes[i];	
			}
		}
		query.searchType = type;
		var ltypes = null;
		var stypes = null;
		switch(type){
			case constants.searchTypes.residential:
				ltypes = constants.listTypes.residential;
				stypes = constants.styleTypes.residential;
				// enable stuff
				// disable stuff
				break
			case constants.searchTypes.multifamily:
				ltypes = constants.listTypes.multifamily;
				stypes = constants.styleTypes.multifamily;
				// enable stuff
				// disable stuff
				break;
			case constants.searchTypes.openhouse:
				ltypes = constants.listTypes.residential;
				stypes = constants.styleTypes.residential;
				// enable styff
				// disable stuff
				break;
			case constants.searchTypes.lots:
				ltypes = constants.listTypes.lots;
				// enable stuff
				// disable stuff
				break;
		}
		var sel = util.get("205");
		if(ltypes != null){// set the listing type optionss
			for(var i=sel.length-1; i>=0; i--){// remove all listing type options
				try{ sel.remove(i); } catch(e){}	
			}
			for(var i in ltypes){
				var op = document.createElement("option");
				op.text = ltypes[i].txt;
				op.value = ltypes[i].val;
				try{// standards compliant
					sel.add(op, null);
				} catch(d){// IE
					sel.add(op);	
				}
			}
		}
		sel = util.get("247");
		if(stypes != null){// set the style type optionss
			for(var i=sel.length-1; i>=0; i--){// remove all listing type options
				try{ sel.remove(i); } catch(e){}	
			}
			for(var i in stypes){
				var op = document.createElement("option");
				op.text = stypes[i].txt;
				op.value = stypes[i].val;
				try{// standards compliant
					sel.add(op, null);
				} catch(d){// IE
					sel.add(op);	
				}
			}
		}
		query.setEnabledBySearchType();// enable/disable respective elements
	},
	
	/* executed when the user clicks the main search button
	*/
	go: function(){	
		var o = query.buildObject();
		var q = query.buildResidentialQuery(o);
		switch(query.searchType){
			case constants.searchTypes.residential:
				q = query.buildResidentialQuery(o);
				break
			case constants.searchTypes.multifamily:
				q = query.buildMultiFamilyQuery(o);
				break;
			case constants.searchTypes.openhouse:
				q = query.buildOpenHouseQuery(o);
				break;
			case constants.searchTypes.lots:
				q = query.buildLotsQuery(o);
				break;
			default:	
				q = query.buildResidentialQuery(o);
				break;
		}
		query.execute(q, query.getSearchUrl());
	},
	
	/** build an object from all inputs and cache it so the user can reload previous searches later
	 */
	buildObject: function(){
		try{
			//TODO error reporting
			var o = new Object();
			o.areas = new Array();
			o.cities = new Array();
			o.streetnumber = util.NULL;
			o.streetname = util.NULL;
			o.zipcode = util.NULL;
			o.mlnumber = util.NULL;
			// location
			var t = util.get(query.elements.area);
			for(var i=0; i<t.options.length; i++){
				if(t.options[i].selected) o.areas.push(t.options[i].value);
			}
			t = util.get(query.elements.city);
			for(var i=0; i<t.options.length; i++){
				if(t.options[i].selected) o.cities.push(t.options[i].value);
			}
			t = util.get(query.elements.streetnumber).value;
			if(util.validText(t) && util.isNumeric(t) && t != "Street Number")
				o.streetnumber = t;
			t = util.get(query.elements.streetname).value;
			if(util.validText(t) && t != "Street Name")
				o.streetname = t;
			t = util.get(query.elements.zipcode).value;
			if(util.validText(t) && t != "Zip Code" && util.isZipCode(t))
				o.zipcode = t;
			t = util.get(query.elements.mlnumber).value;
			if(util.validText(t) && t != "ML Number")
				o.mlnumber = t;
			// criteria
			o.listpricemin = util.NULL;
			o.listpricemax = util.NULL;
			o.bedrooms = util.NULL;
			o.bathrooms = util.NULL;
			o.squarefeetmin = util.NULL;
			o.squarefeetmax = util.NULL;
			o.garages = util.NULL;
			o.yearbuiltmin = util.NULL;
			o.yearbuiltmax = util.NULL;
			o.types = new Array();
			o.styles = new Array();
			o.newlistings = util.NULL;
			o.virtualtour = util.NULL;
			t = util.get(query.elements.listpricemin).value;
			if(!util.isNULL(t)) o.listpricemin = t;
			t = util.get(query.elements.listpricemax).value;
			if(!util.isNULL(t)) o.listpricemax = t;
			t = util.get(query.elements.bedrooms).value;
			if(!util.isNULL(t)) o.bedrooms = t;
			t = util.get(query.elements.bathrooms).value;
			if(!util.isNULL(t)) o.bathrooms = t;
			t = util.get(query.elements.squarefeetmin).value;
			if(!util.isNULL(t)) o.squarefeetmin = t;
			t = util.get(query.elements.squarefeetmax).value;
			if(!util.isNULL(t)) o.squarefeetmax = t;
			t = util.get(query.elements.garages).value;
			if(!util.isNULL(t)) o.garages = t;
			t = util.get(query.elements.yearbuiltmin).value
			if(!util.isNULL(t)) o.yearbuiltmin = t;
			t = util.get(query.elements.yearbuiltmax).value
			if(!util.isNULL(t)) o.yearbuiltmax = t;
			t = util.get(query.elements.type);
			for(var i=0; i<t.options.length; i++){
				if(t.options[i].selected) o.types.push(t.options[i].value);
			}
			t = util.get(query.elements.style);
			for(var i=0; i<t.options.length; i++){
				if(t.options[i].selected) o.styles.push(t.options[i].value);
			}
			t = util.get(query.elements.newlistings).checked;
			if(t) o.newlistings = true;
			t = util.get(query.elements.virtualtour).checked;
			if(t) o.virtualtour = true;
			// amenities
			t = util.get(query.elements.dishwasher).checked;
			if(t) o.dishwasher = true;
			t = util.get(query.elements.microwave).checked;
			if(t) o.microwave = true;
			t = util.get(query.elements.oven).checked;
			if(t) o.oven = true;
			t = util.get(query.elements.regrigerator).checked;
			if(t) o.regrigerator = true;
			t = util.get(query.elements.fenced).checked;
			if(t) o.fenced = true;
			t = util.get(query.elements.sprinkler).checked;
			if(t) o.sprinkler = true;
			t = util.get(query.elements.well).checked;
			if(t) o.well = true;
			t = util.get(query.elements.fireplace).checked;
			if(t) o.fireplace = true;
			t = util.get(query.elements.basement).checked;
			if(t) o.basement = true;
			
			//open house date
			o.openhousedate = util.NULL;
			t = util.get("1236").value;
			if(!util.isNULL(t)) o.openhousedate = t;
			//open house type
			o.openhousetypes = new Array();
			t = util.get("openhouse.residential").checked;
			if(t) o.openhousetypes.push("RES");
			t = util.get("openhouse.multifamily").checked;
			if(t) o.openhousetypes.push("MUL");
			t = util.get("openhouse.lots").checked;
			if(t) o.openhousetypes.push("LOT");
			
			// zone (multi-family)
			o.zones = new Array();
			t = util.get("1115");
			for(var i=0; i<t.options.length; i++){
				if(t.options[i].selected) o.zones.push(t.options[i].value);
			}
			query.cache.push(o);
			return o;
		} catch(e){
			d.bug("query.buildObject(): "+e);
		}
	},
	/** build a residential query from the generated object
	  */
	buildResidentialQuery: function(o){
		try{
			var q = "";
			if(o.areas && o.areas.length > 0){
				q += "(78=|"
				for(var i=0; i<o.areas.length-1; i++){
					q += o.areas[i] + ",";
				}
				q += o.areas[o.areas.length-1];
				q += "),";
			}
			if(o.cities && o.cities.length > 0){
				q += "(1109=|"
				for(var i=0; i<o.cities.length-1; i++){
					q += o.cities[i] + ",";
				}
				q += o.cities[o.cities.length-1];
				q += "),";
			}
			if(o.streetnumber && o.streetnumber != util.NULL){
				q += "(246="+o.streetnumber+"),";
			}
			if(o.streetname && o.streetname != util.NULL){
				q += "(245="+o.streetname+"),";
			}
			if(o.zipcode && o.zipcode != util.NULL){
				q += "(42="+o.zipcode+"),";
			}
			if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax == util.NULL)){// no upper bound
				q += "(161="+o.listpricemin+"+),";
			}
			else if(o.listpricemax && o.listpricemin && (o.listpricemax != util.NULL && o.listpricemin == util.NULL)){//no lower bound
				q += "(161="+o.listpricemax+"-),";
			}
			else if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax != util.NULL)){//upper & lower bound
				if(o.listpricemin*1 > o.listpricemax*1){
					alert("min price is greater than max price");
					return;
				}
				q += "(161="+o.listpricemin+"-"+o.listpricemax+"),";
			} 
			if(o.bedrooms && o.bedrooms != util.NULL){
				q += "(30="+o.bedrooms+"+),";
			}
			if(o.bathrooms && o.bathrooms != util.NULL){
				q += "(1129="+o.bathrooms+"+),";
			}
			if(o.squarefeetmin && o.squarefeetmax && (o.squarefeetmin != util.NULL && o.squarefeetmax == util.NULL)){// no upper bound
				q += "(1123="+o.squarefeetmin+"+),";
			}
			else if(o.squarefeetmax && o.squarefeetmin && (o.squarefeetmax != util.NULL && o.squarefeetmin == util.NULL)){//no lower bound
				q += "(1123="+o.squarefeetmax+"-),";
			}
			else if(o.squarefeetmin && o.squarefeetmax && (o.squarefeetmin != util.NULL && o.squarefeetmax != util.NULL)){//upper & lower bound
				if(o.squarefeetmin*1 > o.squarefeetmax*1){
					alert("min footage is greater than max footage");
					return;
				}
				q += "(1123="+o.squarefeetmin+"-"+o.squarefeetmax+"),";
			} 
			if(o.garages && o.garages != util.NULL){
				q += "(138="+o.garages+"+),";
			}
			if(o.yearbuiltmin && o.yearbuiltmax && (o.yearbuiltmin != util.NULL && o.yearbuiltmax == util.NULL)){// no upper bound
				q += "(51="+o.yearbuiltmin+"+),";
			}
			else if(o.yearbuiltmax && o.yearbuiltmin && (o.yearbuiltmax != util.NULL && o.yearbuiltmin == util.NULL)){//no lower bound
				q += "(51="+o.yearbuiltmax+"-),";
			}
			else if(o.yearbuiltmin && o.yearbuiltmax && (o.yearbuiltmin != util.NULL && o.yearbuiltmax != util.NULL)){//upper & lower bound
				if(o.yearbuiltmin*1 > o.yearbuiltmax*1){
					alert("min year built is greater than max year built");
					return;
				}
				q += "(51="+o.yearbuiltmin+"-"+o.yearbuiltmax+"),";
			} 
			if(o.types && o.types.length > 0){
				q += "(205=|"
				for(var i=0; i<o.types.length-1; i++){
					q += o.types[i] + ",";
				}
				q += o.types[o.types.length-1];
				q += "),";
			}
			if(o.styles && o.styles.length > 0){
				q += "(247=|";
				for(var i=0; i<o.styles.length-1; i++){
					q += o.styles[i] + ",";
				}
				q += o.styles[o.styles.length-1];
				q += "),";
			}
			if(o.virtualtour == true) q += "(57=*http*,*www*,*.com*,*.net*,*.org*),";
			if(o.newlistings == true){
				var dt = new Date((new Date().getTime()-((3600*24*1000)*7)));// go back 1 week
				var date = dt.getFullYear() + "-"+(dt.getMonth()+1) + "-" + dt.getDate();
				q += "(98="+date+"-NOW),";	
			}
			if(o.dishwasher == true) q += "(107=y,Y),";	
			if(o.fenced == true) q += "(127=y,Y),";
			if(o.microwave == true) q += "(168=y,Y),";
			if(o.sprinkler) q += "(265=y,Y),";
			if(o.basement) q += "(284=~None,Partial,Unfinished),";
			if(o.oven == true) q += "(192=y,Y),";
			if(o.well) q += "(274=y,Y),";
			if(o.regrigerator == true) q += "(207=y,Y),";
			if(o.fireplace) q += "(255=1+),";
			if(o.mlnumber != util.NULL) q += "(169="+o.mlnumber+"),";
			q = q.substring(0, q.length-1);
			return q;
		} catch(e){
			d.bug("query.buildResidentialQuery(): " + e);
		}
	},
	/** build a multi-family query from the generated object
	  */
	buildMultiFamilyQuery: function(o){
		try{
			var q = "";
			if(o.areas && o.areas.length > 0){
				q += "(78=|"
				for(var i=0; i<o.areas.length-1; i++){
					q += o.areas[i] + ",";
				}
				q += o.areas[o.areas.length-1];
				q += "),";
			}
			if(o.cities && o.cities.length > 0){
				q += "(1109=|"
				for(var i=0; i<o.cities.length-1; i++){
					q += o.cities[i] + ",";
				}
				q += o.cities[o.cities.length-1];
				q += "),";
			}
			if(o.streetnumber && o.streetnumber != util.NULL){
				q += "(246="+o.streetnumber+"),";
			}
			if(o.streetname && o.streetname != util.NULL){
				q += "(245="+o.streetname+"),";
			}
			if(o.zipcode && o.zipcode != util.NULL){
				q += "(42="+o.zipcode+"),";
			}
			if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax == util.NULL)){// no upper bound
				q += "(161="+o.listpricemin+"+),";
			}
			else if(o.listpricemax && o.listpricemin && (o.listpricemax != util.NULL && o.listpricemin == util.NULL)){//no lower bound
				q += "(161="+o.listpricemax+"-),";
			}
			else if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax != util.NULL)){//upper & lower bound
				if(o.listpricemin*1 > o.listpricemax*1){
					alert("min price is greater than max price");
					return;
				}
				q += "(161="+o.listpricemin+"-"+o.listpricemax+"),";
			} 
			if(o.squarefeetmin && o.squarefeetmax && (o.squarefeetmin != util.NULL && o.squarefeetmax == util.NULL)){// no upper bound
				q += "(430="+o.squarefeetmin+"+),";
			}
			else if(o.squarefeetmax && o.squarefeetmin && (o.squarefeetmax != util.NULL && o.squarefeetmin == util.NULL)){//no lower bound
				q += "(430="+o.squarefeetmax+"-),";
			}
			else if(o.squarefeetmin && o.squarefeetmax && (o.squarefeetmin != util.NULL && o.squarefeetmax != util.NULL)){//upper & lower bound
				if(o.squarefeetmin*1 > o.squarefeetmax*1){
					alert("min footage is greater than max footage");
					return;
				}
				q += "(430="+o.squarefeetmin+"-"+o.squarefeetmax+"),";
			} 
			if(o.yearbuiltmin && o.yearbuiltmax && (o.yearbuiltmin != util.NULL && o.yearbuiltmax == util.NULL)){// no upper bound
				q += "(51="+o.yearbuiltmin+"+),";
			}
			else if(o.yearbuiltmax && o.yearbuiltmin && (o.yearbuiltmax != util.NULL && o.yearbuiltmin == util.NULL)){//no lower bound
				q += "(51="+o.yearbuiltmax+"-),";
			}
			else if(o.yearbuiltmin && o.yearbuiltmax && (o.yearbuiltmin != util.NULL && o.yearbuiltmax != util.NULL)){//upper & lower bound
				if(o.yearbuiltmin*1 > o.yearbuiltmax*1){
					alert("min year built is greater than max year built");
					return;
				}
				q += "(51="+o.yearbuiltmin+"-"+o.yearbuiltmax+"),";
			} 
			if(o.types && o.types.length > 0){
				q += "(369=|"
				for(var i=0; i<o.types.length-1; i++){
					q += o.types[i] + ",";
				}
				q += o.types[o.types.length-1];
				q += "),";
			}
			if(o.styles && o.styles.length > 0){
				q += "(481=|";
				for(var i=0; i<o.styles.length-1; i++){
					q += o.styles[i] + ",";
				}
				q += o.styles[o.styles.length-1];
				q += "),";
			}
			if(o.virtualtour == true) q += "(57=*http*,*www*,*.com*,*.net*,*.org*),";
			if(o.newlistings == true){
				var dt = new Date((new Date().getTime()-((3600*24*1000)*7)));// go back 1 week
				var date = dt.getFullYear() + "-"+(dt.getMonth()+1) + "-" + dt.getDate();
				q += "(98="+date+"-NOW),";	
			}
			if(o.mlnumber != util.NULL) q += "(169="+o.mlnumber+"),";
			q = q.substring(0, q.length-1);
			return q;
		} catch(e){
			d.bug("query.buildMultiFamilyQuery(): " + e);
		}
	},
	/** build an open house query from the generated object
	  */
	buildOpenHouseQuery: function(o){
		try{
			var q = "";
			if(o.areas && o.areas.length > 0){
				q += "(1231=|"
				for(var i=0; i<o.areas.length-1; i++){
					q += o.areas[i] + ",";
				}
				q += o.areas[o.areas.length-1];
				q += "),";
			}
			if(o.cities && o.cities.length > 0){
				q += "(1464="
				for(var i=0; i<o.cities.length-1; i++){
					q += o.cities[i] + ",";
				}
				q += o.cities[o.cities.length-1];
				q += "),";
			}
			if(o.streetnumber && o.streetnumber != util.NULL){
				q += "(1228="+o.streetnumber+"),";
			}
			if(o.streetname && o.streetname != util.NULL){
				q += "(1230="+o.streetname+"),";
			}
			if(o.zipcode && o.zipcode != util.NULL){
				q += "(1235="+o.zipcode+"),";
			}
			if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax == util.NULL)){// no upper bound
				q += "(1227="+o.listpricemin+"+),";
			}
			else if(o.listpricemax && o.listpricemin && (o.listpricemax != util.NULL && o.listpricemin == util.NULL)){//no lower bound
				q += "(1227="+o.listpricemax+"-),";
			}
			else if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax != util.NULL)){//upper & lower bound
				if(o.listpricemin*1 > o.listpricemax*1){
					alert("min price is greater than max price");
					return;
				}
				q += "(1227="+o.listpricemin+"-"+o.listpricemax+"),";
			} 
			if(o.bedrooms && o.bedrooms != util.NULL){
				q += "(1234="+o.bedrooms+"+),";
			}
			if(o.bathrooms && o.bathrooms != util.NULL){
				q += "(1456="+o.bathrooms+"+),";
			}
			if(o.squarefeetmin && o.squarefeetmax && (o.squarefeetmin != util.NULL && o.squarefeetmax == util.NULL)){// no upper bound
				q += "(1462="+o.squarefeetmin+"+),";
			}
			else if(o.squarefeetmax && o.squarefeetmin && (o.squarefeetmax != util.NULL && o.squarefeetmin == util.NULL)){//no lower bound
				q += "(1462="+o.squarefeetmax+"-),";
			}
			else if(o.squarefeetmin && o.squarefeetmax && (o.squarefeetmin != util.NULL && o.squarefeetmax != util.NULL)){//upper & lower bound
				if(o.squarefeetmin*1 > o.squarefeetmax*1){
					alert("min footage is greater than max footage");
					return;
				}
				q += "(1462="+o.squarefeetmin+"-"+o.squarefeetmax+"),";
			} 
			if(o.garages && o.garages != util.NULL){
				q += "(1461="+o.garages+"+),";
			}
			if(o.styles && o.styles.length > 0){
				q += "(1457=|";
				for(var i=0; i<o.styles.length-1; i++){
					q += o.styles[i] + ",";
				}
				q += o.styles[o.styles.length-1];
				q += "),";
			}
			if(o.newlistings == true){
				var dt = new Date((new Date().getTime()-((3600*24*1000)*7)));// go back 1 week
				var date = dt.getFullYear() + "-"+(dt.getMonth()+1) + "-" + dt.getDate();
				q += "(1220="+date+"-NOW),";	
			}
			if(o.mlnumber != util.NULL) q += "(169="+o.mlnumber+"),";
			if(o.openhousedate != util.NULL) q += "(1236="+o.openhousedate+"T00:00:00),";
			if(o.openhousetypes.length > 0){
				q += "(1226=|";	
				for(var i=0; i<o.openhousetypes.length-1; i++){
					q += o.openhousetypes[i] + ",";;	
				}
				q += o.openhousetypes[o.openhousetypes.length-1];
				q += "),";
			}
			q = q.substring(0, q.length-1);
			return q;
		} catch(e){
			d.bug("query.buildOpenHouseQuery(): " + e);
		}
	},
	/** build a lots and land query from the generated object
	  */
	buildLotsQuery: function(o){
		try{
			var q = "";
			if(o.areas && o.areas.length > 0){
				q += "(78=|"
				for(var i=0; i<o.areas.length-1; i++){
					q += o.areas[i] + ",";
				}
				q += o.areas[o.areas.length-1];
				q += "),";
			}
			if(o.cities && o.cities.length > 0){
				q += "(1109=|"
				for(var i=0; i<o.cities.length-1; i++){
					q += o.cities[i] + ",";
				}
				q += o.cities[o.cities.length-1];
				q += "),";
			}
			if(o.streetnumber && o.streetnumber != util.NULL){
				q += "(246="+o.streetnumber+"),";
			}
			if(o.streetname && o.streetname != util.NULL){
				q += "(245="+o.streetname+"),";
			}
			if(o.zipcode && o.zipcode != util.NULL){
				q += "(42="+o.zipcode+"),";
			}
			if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax == util.NULL)){// no upper bound
				q += "(161="+o.listpricemin+"+),";
			}
			else if(o.listpricemax && o.listpricemin && (o.listpricemax != util.NULL && o.listpricemin == util.NULL)){//no lower bound
				q += "(161="+o.listpricemax+"-),";
			}
			else if(o.listpricemin && o.listpricemax && (o.listpricemin != util.NULL && o.listpricemax != util.NULL)){//upper & lower bound
				if(o.listpricemin*1 > o.listpricemax*1){
					alert("min price is greater than max price");
					return;
				}
				q += "(161="+o.listpricemin+"-"+o.listpricemax+"),";
			} 
			if(o.yearbuiltmin && o.yearbuiltmax && (o.yearbuiltmin != util.NULL && o.yearbuiltmax == util.NULL)){// no upper bound
				q += "(51="+o.yearbuiltmin+"+),";
			}
			else if(o.yearbuiltmax && o.yearbuiltmin && (o.yearbuiltmax != util.NULL && o.yearbuiltmin == util.NULL)){//no lower bound
				q += "(51="+o.yearbuiltmax+"-),";
			}
			else if(o.yearbuiltmin && o.yearbuiltmax && (o.yearbuiltmin != util.NULL && o.yearbuiltmax != util.NULL)){//upper & lower bound
				if(o.yearbuiltmin*1 > o.yearbuiltmax*1){
					alert("min year built is greater than max year built");
					return;
				}
				q += "(51="+o.yearbuiltmin+"-"+o.yearbuiltmax+"),";
			} 
			if(o.types && o.types.length > 0){
				q += "(558=|"
				for(var i=0; i<o.types.length-1; i++){
					q += o.types[i] + ",";
				}
				q += o.types[o.types.length-1];
				q += "),";
			}
			if(o.virtualtour == true) q += "(57=*http*,*www*,*.com*,*.net*,*.org*),";
			if(o.newlistings == true){
				var dt = new Date((new Date().getTime()-((3600*24*1000)*7)));// go back 1 week
				var date = dt.getFullYear() + "-"+(dt.getMonth()+1) + "-" + dt.getDate();
				q += "(98="+date+"-NOW),";	
			}
			if(o.mlnumber != util.NULL) q += "(169="+o.mlnumber+"),";
			q = q.substring(0, q.length-1);
			return q;
		} catch(e){
			d.bug("query.buildLotsQuery(): " + e);
		}
	},
	/** execute a search with the current query and  urul
	  */
	execute: function(q, url){
		if(q == util.NULL || q.length < 1){
			alert("Invalid Search Parameters");
			return;
		}
		q = escape(q);
		util.showStatusIcon();
		new Ajax.Request(url+q, {
			method:		"get",
			onSuccess:	function(response){
				util.hideStatusIcon();
				try{
					if(response == null || response == "undefined"){
						var s = "<br/><br/><span style='font-size:14px;color:red;'>Your search did not return any results</span>";
						util.get(query.elements.searchresults).innerHTML = s;
						return;	
					}
					var count = response.responseXML.getElementsByTagName(rets.xml.count)[0].getAttribute("Records");
					if(!count || count < 1){
						var s = "<br/><br/><span style='font-size:14px;color:red;'>Your search did not return any results</span>";
						util.get(query.elements.searchresults).innerHTML = s;
						return;
					}
					query.currentCount = count;
					var cols  = response.responseXML.getElementsByTagName(rets.xml.columns)[0].firstChild.nodeValue.split(rets.delimiter);
					var datas = response.responseXML.getElementsByTagName(rets.xml.data);
					var results = util.buildResultObject(cols, datas);
					results.id = new Date().getTime();
					query.currentId = results.id;
					// sort results by price desc
					var holder;
					for(var x=0; x<results.length; x++){
						for(var y=0; y<(results.length-1); y++){
							if(query.searchType == constants.searchTypes.openhouse){
								results[y][161] = results[y][1227];// fuse open house	
								results[y+1][161] = results[y+1][1227];
							}
							if(Number(results[y][161].replace(/,/g,"")) < Number(results[y+1][161].replace(/,/g,""))) {
								holder = results[y+1];
								results[y+1] = results[y];
								results[y] = holder;
							}
						}
					}
					cache.results.push(results);
					query.currentResults = results;
					query.currentPage = 1;
					query.showResults();					
				} catch(e){
					util.hideStatusIcon();
					d.bug("query.execute(): " + e);
				}
			}
		});
	},
	
	setPage: function(page){
		if(page == "next"){
			if(query.currentPage < query.nPages){
				query.currentPage++;
				query.showResults();
			}
		} else if(page == "previous"){
			if(query.currentPage > 1){
				query.currentPage--;
				query.showResults();
			}
		} else {
			query.currentPage = page;
			query.showResults();
		}
	},
	
	showResults: function(){
		var results = new Array();
		results.id = query.currentId;
		var resultIdx = query.pageSize*(query.currentPage-1);
		var arrayIdx=0;
		for(var p=resultIdx; p<(resultIdx+query.pageSize); p++){
			results[arrayIdx] = query.currentResults[p];
			arrayIdx++;
		}
		var html = "<table width='100%' border='0'>";
		html += "<tr><td colspan='4' style='font-size:14px;' align='center'>Your Search Returned <b>" + query.currentCount + "</b> Results&nbsp;&nbsp;|&nbsp;&nbsp;";
		html += "<a href='javascript:void(0);' onclick='query.compareResults(\""+results.id+"\");'>Compare Selected Results</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
		html += "<a href='javascript:void(0);' onclick='query.mapResults(\""+results.id+"\");'>Map Selected Results</a></td></tr>";
		html += "<tr><td colspan='4'><br/></td></tr>";
		html += "<tr><td colspan='4' align='center'>";
		html += "<table width='100%' align='center' border='0'><tr>";
		html += "<td align='right' width='80'><a href='javascript:void(0);' width='80' onclick='query.setPage(\"previous\");'>< previous</a></td>";
		query.nPages = Math.ceil(query.currentCount/query.pageSize);
		for(var i=0; i<query.nPages; i++){
			if(i<10){
				if(query.currentPage == (i+1))
					html += "<td width='40' style='border:solid 1px #bbbbbb;cursor:pointer;background-color:#dddddd;' align='center'><b>"+(i+1)+"</b></td>";	
				else
					html += "<td width='40' style='border:solid 1px #bbbbbb;cursor:pointer;' align='center' onmouseover='this.style.backgroundColor=\"#dddddd\";' onmouseout='this.style.backgroundColor=\"white\";' onclick='query.setPage("+(i+1)+");'>"+(i+1)+"</td>";	
			}
		}
		html += "<td width='80'><a href='javascript:void(0);' align='center' onclick='query.setPage(\"next\");'>next ></a></td>";
		html += "</tr></table>";
		html += "</td></tr>";
		html += "<tr><td colspan='4'><br/></td></tr>";
		switch(query.searchType){
			case constants.searchTypes.residential:
				html += query.buildResidentialResults(results, query.currentCount);
				break
			case constants.searchTypes.multifamily:
				results = query.fuseResults(results, constants.searchTypes.multifamily);
				html += query.buildMultiFamilyResults(results, query.currentCount);
				break;
			case constants.searchTypes.openhouse:
				results = query.fuseResults(results, constants.searchTypes.openhouse);
				html += query.buildOpenHouseResults(results, query.currentCount);
				break;
			case constants.searchTypes.lots:
				results = query.fuseResults(results, constants.searchTypes.lots);
				html += query.buildLotsResults(results, query.currentCount);
				break;
		}
		html += "<tr><td colspan='4' align='center'>";
		html += "<table width='100%' align='center' border='0'><tr>";
		html += "<td align='right' width='80'><a href='javascript:void(0);' width='80' onclick='query.setPage(\"previous\");'>< previous</a></td>";
		for(var i=0; i<query.nPages; i++){
			if(i<10){
				if(query.currentPage == (i+1))
					html += "<td width='40' style='border:solid 1px #bbbbbb;cursor:pointer;background-color:#dddddd;' align='center'><b>"+(i+1)+"</b></td>";	
				else
					html += "<td width='40' style='border:solid 1px #bbbbbb;cursor:pointer;' align='center' onmouseover='this.style.backgroundColor=\"#dddddd\";' onmouseout='this.style.backgroundColor=\"white\";' onclick='query.setPage("+(i+1)+");'>"+(i+1)+"</td>";	
			}
		}
		html += "<td width='80'><a href='javascript:void(0);' align='center' onclick='query.setPage(\"next\");'>next ></a></td>";
		html += "</tr>";
		html += "<tr><td colspan='4'><br/></td></tr>";
		html += "</table>";
		html += "</td></tr>";
		html += "<tr><td colspan='4' style='font-size:14px;' align='center'>Your Search Returned <b>" + query.currentCount + "</b> Results&nbsp;&nbsp;|&nbsp;&nbsp;";
		html += "<a href='javascript:void(0);' onclick='query.compareResults(\""+results.id+"\");'>Compare Selected Results</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
		html += "<a href='javascript:void(0);' onclick='query.mapResults(\""+results.id+"\");'>Map Selected Results</a></td></tr>";
		html += "</table>";
		try{
			util.get(query.elements.searchresults).innerHTML = html;
			util.get(query.elements.searchresults).scrollTop = util.get(query.elements.searchresults).offsetTop;
			query.loadImages(results, true, null, null);
		} catch(a){//wait two more seconds and try again
			setTimeout(function(){
				try{
					util.get(query.elements.searchresults).innerHTML = html;
					util.get(query.elements.searchresults).scrollTop = util.get(query.elements.searchresults).offsetTop;
					query.loadImages(results, true, null, null);
				} catch(b){ }
			}, 2000);
		}
	},
	/** build results list for residential listings
	  */
	buildResidentialResults: function(results, n){
		try{
			var s = "";
			for(var i=0; i<results.length; i++){
				try{
					s += "<tr>";
					s += "	<td align='center'><input type='checkbox' id='compare."+results[i][169]+"' title='compare' valign='middle'></input></td>";
					s += "	<td width='160'><div id='image-"+results[i][169]+"'>loading image...</div></td>";
					s += "	<td valign='top'>";
					s += "		<span style='font-size:16px;color:#73a43b;'>$"+results[i][161]+" </span>";
					s += "		<a href='javascript:void(0);' onclick='util.showDetails("+results[i][169]+","+results.id+");' title='View Details' style='font-size:16px;'> "+results[i][246]+" "+results[i][245]+" "+results[i][1109]+" "+results[i][1147]+", "+results[i][42]+"</a><br/><br/><br/>";
					s += "		<ul style='font-size:12px;margin-left:50px;line-height:19px;'>";
					s += "			<li style='list-style-type:circle;'>"+results[i][30]+" bedrooms, " + results[i][1129] + " bathrooms</li>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1123]+" square feet</li>";
					s += "			<li style='list-style-type:circle;'>built in "+results[i][51]+"</li>";
					s += "		</ul><br/><br/>";
					var ourl = office.getOfficePropertyById(results[i][159], 988);
					if(!util.isNULL(ourl)){// if the office has a url, link the name to it
						s += "		<table><tr><td>";
						s += "				<span style='color:#666666;font-size:14px;'>Listed with <a href='"+ourl+"' target='_blank'>" + results[i][1111]+"</a></span>";
						s += "			</td><td>";
						s += "				<a href='"+ourl+"' target='_blank'><img src='includes/images/right-arrow-circle-orange.jpg' border='0' width='18'/></a>";
						s += "		</td></tr></table>";
					} else {
						s += "		<span style='color:#666666;font-size:14px;'>Listed with " + results[i][1111]+"</span>";
					}
					s += "	</td>";
					s += "	<td>";
					s += "		<table style='font-size:12px;' cellspacing='7'>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/view-details.jpg'/></td>";
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='javascript:void(0);' onclick='util.showDetails("+results[i][169]+","+results.id+");'>View Details</a></td>";
					//s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='/details.html');'>View Details</a></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/request-more-info.jpg'/></td>";
					//var tid = "email-"+results[i][15].toLowerCase()+"-"+new Date().getTime();
					var tid = "email-"+results[i][15].toLowerCase();
					results[i].tid = tid;
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><div mls='"+results[i][169]+"' id='"+tid+"'><span style='color:#888888;'>Request More Info</span></div></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/map-property.jpg'/></td>";
					s += "				<td><a href='javascript:void(0);' onclick='parent.mapUtil.mapPropertyById("+results[i][169]+","+results.id+");'>Map Property</a></td>";
					s += "			</tr>";
					if(results[i][57] != null && results[i][57] != util.NULL && results[i][57].length > 1){// if virtual tour is available
						s += "			<tr>";
						s += "				<td><img src='includes/images/virtual-tour.jpg'/></td>";
						s += "				<td style='padding-left:3px;border-top:solid 1px #cccccc;'><a href='"+results[i][57]+"' target='_blank'>Virtual Tour</a></td>";
						s += "			</tr>";
					}
					s += "		</table>";
					s += "	</td>";
					s += "</tr>";
					s += "<tr><td style='border-bottom:solid 1px #dddddd;' colspan='3'>&nbsp;</td></tr>";
				} catch(f){}
			}
			return s;
		} catch(e){
			d.bug("query.buildResidentialResults(): " + e);
		}
	},
	/** build results for Multi-Family listings
	  */
	buildMultiFamilyResults: function(results, n){
		try{
			var s = "";
			for(var i=0; i<results.length; i++){
				try{
					s += "<tr>";
					s += "	<td align='center'><input type='checkbox' id='compare."+results[i][169]+"' title='compare' valign='middle'></input></td>";
					s += "	<td width='160'><div id='image-"+results[i][169]+"'>loading image...</div></td>";
					s += "	<td valign='top'>";
					s += "		<span style='font-size:16px;color:#73a43b;'>$"+results[i][161]+" </span>";
					s += "		<a href='javascript:void(0);' onclick='util.showMultiFamilyDetails("+results[i][169]+","+results.id+");' title='View Details' style='font-size:16px;'> "+results[i][246]+" "+results[i][245]+" "+results[i][1109]+" "+results[i][1147]+", "+results[i][42]+"</a><br/><br/><br/>";
					s += "		<ul style='font-size:12px;margin-left:50px;line-height:19px;'>";
					//s += "			<li style='list-style-type:circle;'>"+results[i][30]+" bedrooms, " + results[i][1129] + " bathrooms</li>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1119]+"</li>";
					s += "			<li style='list-style-type:circle;'>"+results[i][481]+"</li>";
					s += "			<li style='list-style-type:circle;'>built in "+results[i][51]+"</li>";
					s += "		</ul><br/><br/>";
					var ourl = office.getOfficePropertyById(results[i][159], 988);
					if(!util.isNULL(ourl)){// if the office has a url, link the name to it
						s += "		<table><tr><td>";
						s += "				<span style='color:#666666;font-size:14px;'>Listed with <a href='"+ourl+"' target='_blank'>" + results[i][1111]+"</a></span>";
						s += "			</td><td>";
						s += "				<a href='"+ourl+"' target='_blank'><img src='includes/images/right-arrow-circle-orange.jpg' border='0' width='18'/></a>";
						s += "		</td></tr></table>";
					} else {
						s += "		<span style='color:#666666;font-size:14px;'>Listed with " + results[i][1111]+"</span>";
					}
					s += "	</td>";
					s += "	<td>";
					s += "		<table style='font-size:12px;' cellspacing='7'>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/view-details.jpg'/></td>";
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='javascript:void(0);' onclick='util.showMultiFamilyDetails("+results[i][169]+","+results.id+");'>View Details</a></td>";
					//s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='/details.html');'>View Details</a></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/request-more-info.jpg'/></td>";
					//var tid = "email-"+results[i][15].toLowerCase()+"-"+new Date().getTime();
					var tid = "email-"+results[i][15].toLowerCase();
					results[i].tid = tid;
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><div mls='"+results[i][169]+"' id='"+tid+"'><span style='color:#888888;'>Request More Info</span></div></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/map-property.jpg'/></td>";
					s += "				<td><a href='javascript:void(0);' onclick='parent.mapUtil.mapPropertyById("+results[i][169]+","+results.id+");'>Map Property</a></td>";
					s += "			</tr>";
					if(results[i][57] != null && results[i][57] != util.NULL && results[i][57].length > 1){// if virtual tour is available
						s += "			<tr>";
						s += "				<td><img src='includes/images/virtual-tour.jpg'/></td>";
						s += "				<td style='padding-left:3px;border-top:solid 1px #cccccc;'><a href='"+results[i][57]+"' target='_blank'>Virtual Tour</a></td>";
						s += "			</tr>";
					}
					s += "		</table>";
					s += "	</td>";
					s += "</tr>";
					s += "<tr><td style='border-bottom:solid 1px #dddddd;' colspan='3'>&nbsp;</td></tr>";
				} catch(f){}
			}
			return s;
		} catch(e){
			d.bug("query.buildMultiFamilyResults(): " + e);
		}
	},
	/** build results for open houses
	  */
	buildOpenHouseResults: function(results, n){
		try{
			var s = "";
			for(var i=0; i<results.length; i++){
				try{
					s += "<tr>";
					s += "	<td align='center'><input type='checkbox' id='compare."+results[i][169]+"' title='compare' valign='middle'></input></td>";
					s += "	<td width='160'><div id='image-"+results[i][169]+"'>loading image...</div></td>";
					s += "	<td valign='top'>";
					s += "		<span style='font-size:16px;color:#73a43b;'>$"+results[i][1227]+" </span>";
					s += "		<a href='javascript:void(0);' onclick='util.showDetails("+results[i][169]+","+results.id+");' title='View Details' style='font-size:16px;'> "+results[i][1228]+" "+results[i][1230]+" "+results[i][1464]+", "+results[i][1235]+"</a><br/><br/><br/>";
					s += "		<ul style='font-size:12px;margin-left:50px;line-height:19px;'>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1234]+" bedrooms, " + results[i][1456] + " bathrooms</li>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1462]+" square feet</li>";
					var da = results[i][1236].split("-");
					var date = new Date(da[0], da[1]-1, da[2].substring(0, da[2].indexOf("T")));
					s += "			<li style='list-style-type:circle;'>"+date.toDateString()+" from "+results[i][1237] + " to "+results[i][1455]+"</li>";
					s += "		</ul><br/><br/>";
					
					var ourl = office.getOfficePropertyById(results[i][1225], 988);
					if(!util.isNULL(ourl)){// if the office has a url, link the name to it
						s += "		<table><tr><td>";
						s += "				<span style='color:#666666;font-size:14px;'>Listed with <a href='"+ourl+"' target='_blank'>" + results[i][1459]+"</a></span>";
						s += "			</td><td>";
						s += "				<a href='"+ourl+"' target='_blank'><img src='includes/images/right-arrow-circle-orange.jpg' border='0' width='18'/></a>";
						s += "		</td></tr></table>";
					} else {
						s += "		<span style='color:#666666;font-size:14px;'>Listed with " + results[i][1459]+"</span>";
					}
					s += "	</td>";
					s += "	<td>";
					s += "		<table style='font-size:12px;' cellspacing='7'>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/view-details.jpg'/></td>";
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='javascript:void(0);' onclick='util.showDetails("+results[i][169]+","+results.id+");'>View Details</a></td>";
					//s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='/details.html');'>View Details</a></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/request-more-info.jpg'/></td>";
					//var tid = "email-"+results[i][15].toLowerCase()+"-"+new Date().getTime();
					var tid = "email-"+results[i][1458].toLowerCase();
					results[i].tid = tid;
					if(results[i][1465] != null && results[i][1465].length > 0){// if email address is available
						s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='mailto:"+results[i][1465]+"?Subject=Inquiry From Billings.org Website.  ML#: "+results[i][169]+"'>Request More Info</a></td>";
					} else {
						s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><span style='color:#888888;'>Request More Info</span></td>";
					}
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/map-property.jpg'/></td>";
					s += "				<td><a href='javascript:void(0);' onclick='parent.mapUtil.mapPropertyById("+results[i][169]+","+results.id+");'>Map Property</a></td>";
					s += "			</tr>";
					s += "		</table>";
					s += "	</td>";
					s += "</tr>";
					s += "<tr><td style='border-bottom:solid 1px #dddddd;' colspan='3'>&nbsp;</td></tr>";
				} catch(f){}
			}
			return s;
		} catch(e){
			d.bug("query.buildOpenHouseResults(): " + e);
		}
	},
	/** build results for lots and land
	  */
	buildLotsResults: function(results, n){
		try{
			var s = "";
			for(var i=0; i<results.length; i++){
				try{
					s += "<tr>";
					s += "	<td align='center'><input type='checkbox' id='compare."+results[i][169]+"' title='compare' valign='middle'></input></td>";
					s += "	<td width='160'><div id='image-"+results[i][169]+"'>loading image...</div></td>";
					s += "	<td valign='top'>";
					s += "		<span style='font-size:16px;color:#73a43b;'>$"+results[i][161]+" </span>";
					s += "		<a href='javascript:void(0);' onclick='util.showLotsDetails("+results[i][169]+","+results.id+");' title='View Details' style='font-size:16px;'> "+results[i][45] + " " + results[i][1109]+" "+results[i][1147]+", "+results[i][42]+"</a><br/><br/><br/>";
					s += "		<ul style='font-size:12px;margin-left:50px;line-height:19px;'>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1119]+"</li>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1113]+"</li>";
					s += "			<li style='list-style-type:circle;'>"+results[i][1127]+" square feet</li>";
					s += "		</ul><br/><br/>";
					var ourl = office.getOfficePropertyById(results[i][159], 988);
					if(!util.isNULL(ourl)){// if the office has a url, link the name to it
						s += "		<table><tr><td>";
						s += "				<span style='color:#666666;font-size:14px;'>Listed with <a href='"+ourl+"' target='_blank'>" + results[i][1111]+"</a></span>";
						s += "			</td><td>";
						s += "				<a href='"+ourl+"' target='_blank'><img src='includes/images/right-arrow-circle-orange.jpg' border='0' width='18'/></a>";
						s += "		</td></tr></table>";
					} else {
						s += "		<span style='color:#666666;font-size:14px;'>Listed with " + results[i][1111]+"</span>";
					}
					s += "	</td>";
					s += "	<td>";
					s += "		<table style='font-size:12px;' cellspacing='7'>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/view-details.jpg'/></td>";
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='javascript:void(0);' onclick='util.showLotsDetails("+results[i][169]+","+results.id+");'>View Details</a></td>";
					//s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><a href='/details.html');'>View Details</a></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/request-more-info.jpg'/></td>";
					//var tid = "email-"+results[i][15].toLowerCase()+"-"+new Date().getTime();
					var tid = "email-"+results[i][15].toLowerCase();
					results[i].tid = tid;
					s += "				<td style='padding-left:3px;border-bottom:solid 1px #cccccc;'><div mls='"+results[i][169]+"' id='"+tid+"'><span style='color:#888888;'>Request More Info</span></div></td>";
					s += "			</tr>";
					s += "			<tr>";
					s += "				<td><img src='includes/images/map-property.jpg'/></td>";
					s += "				<td><a href='javascript:void(0);' onclick='parent.mapUtil.mapPropertyById("+results[i][169]+","+results.id+");'>Map Property</a></td>";
					s += "			</tr>";
					s += "		</table>";
					s += "	</td>";
					s += "</tr>";
					s += "<tr><td style='border-bottom:solid 1px #dddddd;' colspan='3'>&nbsp;</td></tr>";
				} catch(f){}
			}
			return s
		} catch(e){
			d.bug("query.buildLotsResults(): " + e);
		}
	},
	/** add common properties to all results
	  */
	fuseResults: function(results, type){
		for(var i=0; i<results.length; i++){
			try{
				if(type == constants.searchTypes.openhouse){
				
				results[i][169]  = results[i][1222];
				results[i][246]  = results[i][1228];
				results[i][245]  = results[i][1230];
				results[i][161]  = results[i][1227];
				results[i][30]   = results[i][1234];
				results[i][1129] = results[i][1456];
				results[i][1123] = results[i][1462]; 
				results[i][1109] = results[i][1464];
				results[i][1147] = "MT";
				results[i][42]   = results[i][1235];
			} else if(type == constants.searchTypes.multifamily){	
				results[i][1123] = results[i][430];
				results[i][1147] = "MT";
			} else if(type == constants.searchTypes.lots){
				results[i][1147] = "MT";
			}
			} catch(e){}
		}
		return results;
	},
	
	getUrlParam: function(name){
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp(regexS);
	  var results = regex.exec(window.location.href);
	  if( results == null )
		return null;
	  else
		return results[1];
	},
	/** use asynchronous calls to get images
	  */
	loadImages: function(results, bIsSearch, callback){
		try{
			for(var i=0; i<results.length; i++){
				try{//TODO see if the image exists first
					new Ajax.Request("/services/getobject/?mls="+results[i][169], {
						method:		"get",
						onSuccess:	function(response){
							var photos = response.responseXML.getElementsByTagName("response")[0].getAttribute("photos");
							var mls = response.responseXML.getElementsByTagName("response")[0].getAttribute("mls");
							if(photos != "ERROR" && photos != "1"){
								photos = photos.split(",");
								// cache photos
								var o = new Object();
								o.id = mls;
								o.photos = photos;
								cache.photos.push(o);
								
								if(bIsSearch){// if we are running a search
									var s = "<img id='img-"+mls+"' src='"+constants.url.photos+photos[0]+"' onclick='util.showDetails("+mls+","+results.id+");' height='150' width='150' border='0' style='cursor:pointer;'/><br/>view photos: ";
									for(var j=0; j<photos.length; j++){
										s += "<a href='javascript:void(0);' onmouseover='photoOP.setThumb("+mls+", "+j+");'>"+Number(j+1)+" </a>";	
									}
									try{
										util.get("image-"+mls).innerHTML = s;
									} catch(e){}
								}
							} else {// no photos
								try{
									util.get("image-"+mls).innerHTML = "No Photos Available";
								} catch(e){}
							}
						}
					});
				} catch(f){}
			}
			if(callback != null) callback(results);
		} catch(e){
			d.bug("query.loadImages(): " + e);
		}
		if(bIsSearch) query.loadRequestEmails(results);
	},
	/** user asynchronous call to get the listing agent email
	  */
	loadRequestEmails: function(results){
		var q = "";
		for(var i=0; i<results.length; i++){
			q += results[i][15] + ",";
		}
		q = q.substring(0, q.length-1);
		new Ajax.Request(constants.url.getuserinfobyusername+q, {
			method:		"get",
			onSuccess:	function(response){
				if(response != null && response.responseXML != null){
					var cols  = response.responseXML.getElementsByTagName(rets.xml.columns)[0].firstChild.nodeValue.split(rets.delimiter);
					var datas = response.responseXML.getElementsByTagName(rets.xml.data);
					var user = util.buildResultObject(cols, datas);
					for(var j=0; j<user.length; j++){
						try{
							var e = util.get("email-"+user[j][1053].toLowerCase());
							var mls = e.getAttribute("mls");
							e.innerHTML = "<a href='mailto:"+user[j][1044]+"?Subject=Inquiry From Billings.org Website, ML#: "+mls+"'>Request More Info</a>";
						} catch(e){}
					}
				}
			}
		});
	},
	mapResults: function(cacheId){
		var results = util.get(query.elements.searchresults).getElementsByTagName("input");
		var r = new Array();
		for(var i=0; i<results.length; i++){
			if(results[i].id.substring(0, 8) == "compare."){
				if(results[i].checked){
					var ml = results[i].id.substring(8, results[i].id.length);
					mapUtil.mapPropertyById(ml, cacheId);
				}
			}
		}
	},
	/** generate an array of selected listings and pass it to the function to display them
	  */
	compareResults: function(cacheId){
		var results = util.get(query.elements.searchresults).getElementsByTagName("input");
		var r = new Array();
		for(var i=0; i<results.length; i++){
			if(results[i].id.substring(0, 8) == "compare."){
				if(results[i].checked){
					var ml = results[i].id.substring(8, results[i].id.length);
					r.push(cache.getResult(ml, cacheId));
				}
			}
		}
		query.showCompareResults(r);
	},
	/** display the selected listings
	  */
	showCompareResults: function(results, searchType){
		if(!searchType) searchType = query.searchType;
		var header = "";
		switch(searchType){
			case constants.searchTypes.residential:
				header = "<td>&nbsp;</td><td><b>ML#</b></td><td><b>Address</b></td><td><b>Listing Price</b></td><td><b>Bedrooms</b></td><td><b>Bathrooms</b></td><td nowrap='nowrap'><b>Square Ft.</b></td><td><b>Notes</b></td>";
				break;
			case constants.searchTypes.openhouse:
				header = "<td>&nbsp;</td><td><b>ML#</b></td><td><b>Address</b></td><td><b>Listing Price</b></td><td><b>Bedrooms</b></td><td><b>Bathrooms</b></td><td nowrap='nowrap'><b>Square Ft.</b></td><td><b>Notes</b></td>";
				break;
			case constants.searchTypes.multifamily:
				header = "<td>&nbsp;</td><td><b>ML#</b></td><td><b>Address</b></td><td><b>Listing Price</b></td><td><b>Type</b></td><td><b>Style</b></td><td nowrap='nowrap'><b>Square Ft.</b></td><td><b>Notes</b></td>";
				break;
			case constants.searchTypes.lots:
				header = "<td>&nbsp;</td><td><b>ML#</b></td><td><b>Address</b></td><td><b>Listing Price</b></td><td><b>Type</b></td><td><b>Subdivision</b></td><td nowrap='nowrap'><b>Lot SqFt</b></td><td><b>Notes</b></td>";
				break;
		}
		var s = "<table width='100%' cellpadding='2' style='font-size:14px;'>";
		s += "		<tr>";
		s += header;
		s += "		</tr>";
		for(var c=0; c<results.length; c++){
			switch(searchType){
				case constants.searchTypes.multifamily:
					// fuse
					results[c][30] = results[c][1119];
					results[c][1129] = results[c][481];
					break;
				case constants.searchTypes.lots:
					results[c][30] = results[c][1119];
					results[c][1129] = results[c][1113];
					results[c][1123] = results[c][1127];
					break;
				case constants.searchTypes.openhouse:
					results[c][246] = results[c][1228];
					results[c][245] = results[c][1230];
					break;
			}
			var photos = cache.getPhotos(results[c][169]);
			s += "<tr>";
			s += "	<td width='100'><img src='"+constants.url.photos+photos[0]+"' height='100' width='100'/></td>";
			s += "	<td>"+((results[c][169] != "") ? results[c][169] : "N/A") +"</td>";
			s += "	<td nowrap='nowrap'>"+results[c][246] + " asdf " + results[c][245]+"</td>";
			s += "	<td>$"+results[c][161]+"</td>";
			s += "	<td>"+results[c][30]+"</td>";
			s += "	<td>"+results[c][1129]+"</td>";
			s += "	<td>"+((results[c][1123] != "") ? results[c][1123] : "N/A")+"</td>";
			s += "	<td width='250' style='border-left:solid 1px #dddddd;'>&nbsp;</td>";
			s += "</tr><tr><td colspan='6' style='border-bottom:solid 1px #dddddd;'>&nbsp;</td></tr>";
		}
		s += "</table>";
		util.get("compare-body").innerHTML = s;
		util.showLayer("compare");	
	}
}