if(typeof Beast == "undefined") Beast = {};

Beast.QuickSearch_428f17e9a53b415e8dd0f57cd61d671d = {
	Data : {AccountID:1793,SearchSetupID:65,Columns:2,PublicId:"428f17e9-a53b-415e-8dd0-f57cd61d671d",Fields:[{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"1", "OrderPosition":"0", "FieldDisplayTypeID":"1", "QuickSearchFieldTypeID1":"1", "Name":"City", "FieldDisplayTypeID1":"1"},{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"2", "OrderPosition":"1", "FieldDisplayTypeID":"1", "QuickSearchFieldTypeID1":"2", "Name":"Price Min", "FieldDisplayTypeID1":"1"},{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"3", "OrderPosition":"2", "FieldDisplayTypeID":"1", "QuickSearchFieldTypeID1":"3", "Name":"Price Max", "FieldDisplayTypeID1":"1"},{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"4", "OrderPosition":"3", "FieldDisplayTypeID":"1", "QuickSearchFieldTypeID1":"4", "Name":"Beds", "FieldDisplayTypeID1":"1"},{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"5", "OrderPosition":"4", "FieldDisplayTypeID":"1", "QuickSearchFieldTypeID1":"5", "Name":"Baths", "FieldDisplayTypeID1":"1"},{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"6", "OrderPosition":"5", "FieldDisplayTypeID":"1", "QuickSearchFieldTypeID1":"6", "Name":"Min SqFt", "FieldDisplayTypeID1":"1"},{ "QuickSearchID":"561", "QuickSearchFieldTypeID":"8", "OrderPosition":"6", "FieldDisplayTypeID":"2", "QuickSearchFieldTypeID1":"8", "Name":"Property Type", "FieldDisplayTypeID1":"2"}],PropertyTypes:[{ "QuickSearchID":"561", "SearchSetupPropertyTypeID":"527", "SearchSetupPropertyTypeID1":"527", "SearchSetupID":"65", "DisplayName":"Condominium", "IsSearchedByDefault":"True", "PropertyIconTypeID":"2"},{ "QuickSearchID":"561", "SearchSetupPropertyTypeID":"530", "SearchSetupPropertyTypeID1":"530", "SearchSetupID":"65", "DisplayName":"Residential", "IsSearchedByDefault":"True", "PropertyIconTypeID":"1"}],Features:[]},
	Stage: {},
	Columns: 2,
	CurrentRow : null,
	SearchUrl : "http://housemeetsowner.com/real-estate-search/",
	FramedPage : true,
	
	BuildDisplay : function(){
		document.write("<table id=\"ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d\" class=\"ds-quick-search\"><tbody></tbody></table>");
		
		this.Stage = document.getElementById("ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d").firstChild;
		
		this.CurrentRow = document.createElement("tr");
		for(var i=0;i<this.Data.Fields.length;i++){
		
			if(this.Columns != 0 && i*2 % this.Columns == 0){
				if(this.CurrentRow != null) this.Stage.appendChild(this.CurrentRow);
				this.CurrentRow = document.createElement("tr");
			}
			
			var FieldName = document.createElement("td");
			FieldName.className = "ds-quick-search-name";
			FieldName.innerHTML = this.Data.Fields[i].Name;
			this.CurrentRow.appendChild(FieldName);
			
			if(this.Columns == 1){
				if(this.CurrentRow != null) this.Stage.appendChild(this.CurrentRow);
				this.CurrentRow = document.createElement("tr");
			}
			
			this.BuildField(this.Data.Fields[i]);
		}
		if(this.CurrentRow != null) this.Stage.appendChild(this.CurrentRow);
		
		if(this.Columns != 0) this.CurrentRow = document.createElement("tr");
		var ButtonCell = document.createElement("td");
		var SubmitButton = document.createElement("input");
		
		SubmitButton.type = "button";
		SubmitButton.value = "Search";
		SubmitButton.className = "ds-quick-search-button";
		SubmitButton.onclick = function(){ Beast.QuickSearch_428f17e9a53b415e8dd0f57cd61d671d.Search(); };
		
		ButtonCell.appendChild(SubmitButton);
		this.CurrentRow.appendChild(ButtonCell);
		if(this.Columns != 0) this.Stage.appendChild(this.CurrentRow);
	},
	
	BuildField : function(f){
		var FieldValue = document.createElement("td");
		var FieldValueInput = null;
		
		FieldValue.className = "ds-quick-search-value";
		
		if(String(f.QuickSearchFieldTypeID) == "8" || String(f.QuickSearchFieldTypeID) == "9"){
			for(var i=0;i < (String(f.QuickSearchFieldTypeID) == "8" ? this.Data.PropertyTypes.length : this.Data.Features.length ); i++){
				var DataItem = (String(f.QuickSearchFieldTypeID) == "8" ? this.Data.PropertyTypes[i] : this.Data.Features[i]);
				var DataId = (String(f.QuickSearchFieldTypeID) == "8" ?  DataItem.SearchSetupPropertyTypeID :  DataItem.SearchSetupFeatureID);
				
				switch(String(f.FieldDisplayTypeID)){
					case "3": // drop down
						if(FieldValueInput == null){
							FieldValueInput = document.createElement("select");
							FieldValueInput.id = "ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID;
							FieldValue.appendChild(FieldValueInput);
							
							FieldValueInput.options[FieldValueInput.options.length] = new Option("Choose an Option", "");
						}
						FieldValueInput.options[FieldValueInput.options.length] = new Option(DataItem.DisplayName, DataId);
							
						break;
					case "2": //checkboxes
					case "4": //radio buttons
						var FieldValueContainer = document.createElement("span");
						var FieldValueName = document.createElement("span");
						
						FieldValueName.innerHTML = DataItem.DisplayName;
						FieldValueName.onclick = function() { this.previousSibling.checked = !this.previousSibling.checked; };
					
						FieldValueInput = document.createElement("input");
						FieldValueInput.type = (String(f.FieldDisplayTypeID) == "2" ? "checkbox" : "radio"); 
						FieldValueInput.id = "ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID + "-" + DataId;
						FieldValueInput.name = "ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID;
						FieldValueInput.value = DataId;
						
						FieldValueContainer.appendChild(FieldValueInput);
						FieldValueContainer.appendChild(FieldValueName);
						
						FieldValue.appendChild(FieldValueContainer);
						break;
				}
			}
		} else {
			switch(String(f.QuickSearchFieldTypeID)){
				case "14":
				case "15":
					FieldValueInput = document.createElement("input");
					FieldValueInput.type = "checkbox"; 
					FieldValueInput.value = "true";
					break;
				default: 
					FieldValueInput = document.createElement("input");
					FieldValueInput.type = "text"; 
					break;
			}
			FieldValueInput.id = "ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID;
			FieldValue.appendChild(FieldValueInput);
		}
		this.CurrentRow.appendChild(FieldValue);
	},
	
	Search : function(){
		var PropertyTypes = "", Features = "", PriceMin = "", PriceMax = "", Beds = "", Baths = "", ImprovedSqft = "", LotSqft = "", 
			Zip = "", Tract = "", Community = "", SchoolOrDistrict = "", City = "";
		var ForeclosureReo = 0;
		var replaceCommanRegex = new RegExp(",", "g");
		
		for(var i=0;i<this.Data.Fields.length;i++) {
			var f = this.Data.Fields[i];
			if(String(f.QuickSearchFieldTypeID) == "8" || String(f.QuickSearchFieldTypeID) == "9"){
				switch(String(f.FieldDisplayTypeID)){
					case "3": // drop down
						var Item = document.getElementById("ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID);
						if(String(f.QuickSearchFieldTypeID) == "8")
							PropertyTypes = Item.value;
						else if(String(f.QuickSearchFieldTypeID) == "9")
							Features = Item.value;
						break;
					case "2": //checkboxes
					case "4": //radio buttons
						var Items = document.getElementsByName("ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID);
						
						for(var j=0;j<Items.length;j++){
							if(Items[j].checked){
								if(String(f.QuickSearchFieldTypeID) == "8"){
									if(PropertyTypes.length > 0) PropertyTypes += ",";
									PropertyTypes += Items[j].value;
								} else if(String(f.QuickSearchFieldTypeID) == "9"){
									if(Features.length > 0) Features += ",";
									Features += Items[j].value;
								}
							}
						}
						break;
				}
			} else {
				var Item = document.getElementById("ds-quick-search-module-428f17e9a53b415e8dd0f57cd61d671d-" + f.QuickSearchFieldTypeID);
				
				switch(String(f.QuickSearchFieldTypeID)){
					case "1": City = Item.value; break;
					case "2": PriceMin = Item.value.replace(replaceCommanRegex, ""); break;
					case "3": PriceMax = Item.value.replace(replaceCommanRegex, ""); break;
					case "4": Beds = Item.value; break;
					case "5": Baths = Item.value; break;
					case "6": ImprovedSqft = Item.value; break;
					case "7": LotSqft = Item.value; break;
					case "10": Zip = Item.value; break;
					case "11": Tract = Item.value; break;
					case "12": Community = Item.value; break;
					case "13": SchoolOrDistrict = Item.value; break;
					case "14": ForeclosureReo += (Item.checked ? 1 : 0); break;
					case "15": ForeclosureReo += (Item.checked ? 2 : 0); break;
				}
			}
		}
		
		var QueryString = 
			(PropertyTypes == "" ? "" : "&PropertyTypes=" + PropertyTypes ) + 
			(Features == "" ? "" : "&Features=" + Features ) + 
			(City == "" ? "" : "&Cities=" + encodeURIComponent(City)) + 
			(PriceMin == "" ? "" : "&MinPrice=" + PriceMin ) + 
			(PriceMax == "" ? "" : "&MaxPrice=" + PriceMax ) + 
			(Beds == "" ? "" : "&MinBeds=" + Beds ) + 
			(Baths == "" ? "" : "&MinBaths=" + Baths ) + 
			(ImprovedSqft == "" ? "" : "&MinImprovedSqFt=" + encodeURIComponent(ImprovedSqft)) + 
			(LotSqft == "" ? "" : "&MinLotSqFt=" + encodeURIComponent(LotSqft)) + 
			(Zip == "" ? "" : "&ZipCodes=" + encodeURIComponent(Zip)) + 
			(Tract == "" ? "" : "&Tracts=" + encodeURIComponent(Tract)) +
			(Community == "" ? "" : "&Communities=" + encodeURIComponent(Community)) +
			(SchoolOrDistrict == "" ? "" : "&Schools=" + encodeURIComponent(SchoolOrDistrict)) +
			(ForeclosureReo == 0 ? "" : "&Foreclosures=" + String(ForeclosureReo));
		
		QueryString += "&PerformSearch";
		
		if(!this.FramedPage){
			window.open(this.SearchUrl + '#' + QueryString, "_blank", "");
		} else {
			top.location.href = this.SearchUrl + (this.SearchUrl.indexOf('?') >= 0 || this.SearchUrl.toLowerCase().indexOf('.aspx/') >= 0 ? '&' : '#') + QueryString; 
		}
	}
}

Beast.QuickSearch_428f17e9a53b415e8dd0f57cd61d671d.BuildDisplay();