// 検索項目取得のURL
var searchItemUrl = "search_item.php";

// 価格帯
var priceData = [
	{ id: "3",  value: "3万円" },
	{ id: "5",  value: "5万円" },
	{ id: "7",  value: "7万円" },
	{ id: "10", value: "10万円" },
	{ id: "15", value: "15万円" },
	{ id: "20", value: "20万円" }
];
// 土地坪
var landData = [
	{ id: "2.5",  value: "2.5坪" },
	{ id: "5",    value: "5坪" },
	{ id: "7.5",  value: "7.5坪" },
	{ id: "10",   value: "10坪" },
	{ id: "12.5", value: "12.5坪" },
	{ id: "15",   value: "15坪" },
	{ id: "17.5", value: "17.5坪" },
	{ id: "20",   value: "20坪" },
	{ id: "22.5", value: "22.5坪" },
	{ id: "25",   value: "25坪" },
	{ id: "30",   value: "30坪" },
	{ id: "35",   value: "35坪" },
	{ id: "40",   value: "40坪" },
	{ id: "45",   value: "45坪" },
	{ id: "50",   value: "50坪" },
	{ id: "55",   value: "55坪" },
	{ id: "60",   value: "60坪" },
	{ id: "70",   value: "70坪" },
	{ id: "80",   value: "80坪" },
	{ id: "90",   value: "90坪" },
	{ id: "100",  value: "100坪" },
	{ id: "120",  value: "120坪" },
	{ id: "140",  value: "140坪" },
	{ id: "160",  value: "160坪" },
	{ id: "180",  value: "180坪" },
	{ id: "200",  value: "200坪" },
	{ id: "250",  value: "250坪" },
	{ id: "300",  value: "300坪" },
	{ id: "350",  value: "350坪" }
];
// 建物面積
var dimensionData = [
	{ id: "8",     value: "8㎡" },
	{ id: "16.5",  value: "16.5㎡" },
	{ id: "25",    value: "25㎡" },
	{ id: "33",    value: "33㎡" },
	{ id: "41.5",  value: "41.5㎡" },
	{ id: "50",    value: "50㎡" },
	{ id: "58",    value: "58㎡" },
	{ id: "66",    value: "66㎡" },
	{ id: "74",    value: "74㎡" },
	{ id: "82.5",  value: "82.5㎡" },
	{ id: "99",    value: "99㎡" },
	{ id: "115.5", value: "115.5㎡" },
	{ id: "132",   value: "132㎡" },
	{ id: "148.5", value: "148.5㎡" },
	{ id: "165",   value: "165㎡" },
	{ id: "182",   value: "182㎡" },
	{ id: "198.5", value: "198.5㎡" },
	{ id: "231.5", value: "231.5㎡" },
	{ id: "264.5", value: "264.5㎡" },
	{ id: "297.5", value: "297.5㎡" },
	{ id: "330.5", value: "330.5㎡" },
	{ id: "396.5", value: "396.5㎡" },
	{ id: "463",   value: "463㎡" },
	{ id: "529",   value: "529㎡" },
	{ id: "595",   value: "595㎡" },
	{ id: "661",   value: "661㎡" },
	{ id: "826.5", value: "826.5㎡" },
	{ id: "992",   value: "992㎡" },
	{ id: "1157",  value: "1157㎡" }
];
// 建物坪
var buildingData = [
	{ id: "2.5",  value: "2.5坪" },
	{ id: "5",    value: "5坪" },
	{ id: "7.5",  value: "7.5坪" },
	{ id: "10",   value: "10坪" },
	{ id: "12.5", value: "12.5坪" },
	{ id: "15",   value: "15坪" },
	{ id: "17.5", value: "17.5坪" },
	{ id: "20",   value: "20坪" },
	{ id: "22.5", value: "22.5坪" },
	{ id: "25",   value: "25坪" },
	{ id: "30",   value: "30坪" },
	{ id: "35",   value: "35坪" },
	{ id: "40",   value: "40坪" },
	{ id: "45",   value: "45坪" },
	{ id: "50",   value: "50坪" },
	{ id: "55",   value: "55坪" },
	{ id: "60",   value: "60坪" },
	{ id: "70",   value: "70坪" },
	{ id: "80",   value: "80坪" },
	{ id: "90",   value: "90坪" },
	{ id: "100",  value: "100坪" },
	{ id: "120",  value: "120坪" },
	{ id: "140",  value: "140坪" },
	{ id: "160",  value: "160坪" },
	{ id: "180",  value: "180坪" },
	{ id: "200",  value: "200坪" },
	{ id: "250",  value: "250坪" },
	{ id: "300",  value: "300坪" },
	{ id: "350",  value: "350坪" }
];

/*
function init() {
	var date = new Date();
	new Ajax.Request(searchItemUrl + "?timestamp=" + date.getTime(),
		{method: 'post', onComplete: init2 });
}

function init2() {
	var html = "";
	objJson = request.responseText.evalJSON();
	var type = "";
	if (objJson.type) {
		if (!(objJson.type instanceof Array)) {
			type = getOption(objJson.type);
		} else {
			for (var i = 0; i < objJson.type.length; i++) {
				type += getOption(objJson.type[i]);
			}
		}
		html += "<select name=\"type\" id=\"type\" onchange=\"selectType(this.value)\">\n";
		html += "<option value=\"\">選択してください。</option>\n";
		html += type;
		html += "</select>\n";
	}
	$("td_type").innerHTML = html;
}
*/

function selectType(type) {
	$("type").value = type;
	if (type != 0) {
		var date = new Date();
		new Ajax.Request(searchItemUrl + "?type=" + type + "&timestamp=" + date.getTime(),
			{method: 'post', onComplete: selectType2 });
	} else {
		$("search2").innerHTML = "";
		$("result").innerHTML = "";
	}
}

function selectType2(request) {
	var type = $("type").value;
	var html = "";
	objJson = request.responseText.evalJSON();
	html += "<table class=\"tbl_search\">\n";
	// 物件種目
	var item = "";
	if (objJson.item) {
		if (!(objJson.item instanceof Array)) {
			item = "<ul>\n"
			item += getCheckBox(objJson.item, "item", 0);
			item += "</ul>\n"
		} else {
			item = "<ul>\n"
			for (var i = 0; i < objJson.item.length; i++) {
				item += getCheckBox(objJson.item[i], "item", i);
			}
			item += "</ul>\n"
		}
		html += "<tr>\n";
		html += "<th>物件種目</th>\n";
		html += "<td>\n";
		html += item;
		html += "</td>\n";
		html += "</tr>\n";
	}
	
	
	
	
	
	// アピールポイント
	//------20090521---add--------------------------------------------------------------------
	var aplitem = "";
	if (objJson.aplitem) {
		if (!(objJson.aplitem instanceof Array)) {
			aplitem = "<ul>\n"
			aplitem += getCheckBox(objJson.aplitem, "appealpoint", 0);
			aplitem += "</ul>\n"
		} else {
			aplitem = "<ul>\n"
			for (var i = 0; i < objJson.aplitem.length; i++) {
				aplitem += getCheckBox2(objJson.aplitem[i], "appealpoints", i);
			}
			aplitem += "</ul>\n"
		}
		html += "<tr>\n";
		html += "<th>アピールポイント</th>\n";
		html += "<td>\n";
		html += aplitem;
		html += "</td>\n";
		html += "</tr>\n";
	}

	//------20090521---add--------------------------------------------------------------------

	
	
	
	// 価格帯
	var price = "";
	for (var i = 0; i < priceData.length; i++) {
		price += getOption(priceData[i]);
	}
	html += "<tr>\n";
	html += "<th>価格帯</th>\n";
	html += "<td>\n";
	html += "<select name=\"price_low\">\n";
	html += "<option value=\"0\">下限なし</option>\n";
	html += price;
	html += "</select>\n";
	html += "～\n";
	html += "<select name=\"price_high\">\n";
	html += "<option value=\"0\">上限なし</option>\n";
	html += price;
	html += "</select>\n";
	html += "</td>\n";
	html += "</tr>\n";
	// 建物面積
	switch (type) {
		case "1":	// 賃貸居住用
			var dimension = "";
			for (var i = 0; i < dimensionData.length; i++) {
				dimension += getOption(dimensionData[i]);
			}
			html += "<tr>\n";
			html += "<th>建物面積</th>\n";
			html += "<td>\n";
			html += "<select name=\"dimension_low\">\n";
			html += "<option value=\"0\">下限なし</option>\n";
			html += dimension;
			html += "</select>\n";
			html += "～\n";
			html += "<select name=\"dimension_high\">\n";
			html += "<option value=\"0\">上限なし</option>\n";
			html += dimension;
			html += "</select>\n";
			html += "</td>\n";
			html += "</tr>\n";
			break;
	}
	// 部屋数
	switch (type) {
		case "1":	// 賃貸居住用
			html += "<tr>\n";
			html += "<th>部屋数</th>\n";
			html += "<td>\n";
			html += "<input type=\"text\" name=\"room\" style=\"ime-mode: disabled; width: 50px;\" value=\"\" /> 部屋　（キッチンとリビングは含まず）\n";
			html += "</td>\n";
			html += "</tr>\n";
			break;
	}
	// 間取り
	switch (type) {
		case "1":	// 賃貸居住用
			var layout = "";
			if (objJson.layout) {
				if (!(objJson.layout instanceof Array)) {
					layout = "<ul>\n"
					layout += getCheckBox(objJson.layout, "layout", 0);
					layout += "</ul>\n"
				} else {
					layout = "<ul>\n"
					for (var i = 0; i < objJson.layout.length; i++) {
						layout += getCheckBox(objJson.layout[i], "layout", i);
					}
					layout += "</ul>\n"
				}
				html += "<tr>\n";
				html += "<th>間取り</th>\n";
				html += "<td>\n";
				html += layout;
				html += "</td>\n";
				html += "</tr>\n";
			}
			break;
	}
	// 土地坪
	switch (type) {
		case "2":	// 賃貸事業用
			var land = "";
			for (var i = 0; i < landData.length; i++) {
				land += getOption(landData[i]);
			}
			html += "<tr>\n";
			html += "<th>土地坪</th>\n";
			html += "<td>\n";
			html += "<select name=\"land_low\">\n";
			html += "<option value=\"0\">下限なし</option>\n";
			html += land;
			html += "</select>\n";
			html += "～\n";
			html += "<select name=\"land_high\">\n";
			html += "<option value=\"0\">上限なし</option>\n";
			html += land;
			html += "</select>\n";
			html += "</td>\n";
			html += "</tr>\n";
			break;
	}
	// 建物坪
	switch (type) {
		case "2":	// 賃貸事業用
			var building = "";
			for (var i = 0; i < buildingData.length; i++) {
				building += getOption(buildingData[i]);
			}
			html += "<tr>\n";
			html += "<th>建物坪</th>\n";
			html += "<td>\n";
			html += "<select name=\"building_low\">\n";
			html += "<option value=\"0\">下限なし</option>\n";
			html += building;
			html += "</select>\n";
			html += "～\n";
			html += "<select name=\"building_high\">\n";
			html += "<option value=\"0\">上限なし</option>\n";
			html += building;
			html += "</select>\n";
			html += "</td>\n";
			html += "</tr>\n";
			break;
	}
	// 所在地
	html += "<tr>\n";
	html += "<th>所在地</th>\n";
	html += "<td>\n";
	html += "<input type=\"text\" name=\"address\" id=\"address\" style=\"ime-mode: active; width: 200px;\" value=\"\" />\n";
	html += "<br /><div id=\"address_list\"></div>\n";
	html += "</td>\n";
	html += "</tr>\n";
	// エリア
	var area = "";
	if (objJson.area) {
		if (!(objJson.area instanceof Array)) {
			area = "<ul>\n"
			area += getCheckBox(objJson.area, "area", 0);
			area += "</ul>\n"
		} else {
			area = "<ul>\n"
			for (var i = 0; i < objJson.area.length; i++) {
				area += getCheckBox(objJson.area[i], "area", i);
			}
			area += "</ul>\n"
		}
		html += "<tr>\n";
		html += "<th>エリア</th>\n";
		html += "<td>\n";
		html += area;
		html += "</td>\n";
		html += "</tr>\n";
	}
	html += "<tr>\n";
	html += "<td colspan=\"2\" align=\"center\">\n";
	html += "<input type=\"image\" src=\"../img/cms/bt_search.gif\" value=\"検索する\">\n";
	html += "</td>\n";
	html += "</tr>\n";
	html += "</table>";
	$("search2").innerHTML = html;
	new Ajax.Autocompleter("address", "address_list", "./get_address.php",
		{
			parameters: "type=" + type
		}
	);
}

function getOption(obj) {
	return "<option value=\"" + obj.id + "\">" + obj.value + "</option>\n";
}

function getCheckBox(obj, name, index) {
	var result = "<li>\n";
	result += "<input type=\"checkbox\" id=\"" + name + index + "\" name=\"" + name + "[" + index + "]\" value=\"" + obj.id + "\" />";
	result += "<label for=\"" + name + index + "\">" + obj.value + "</label>\n";
	result += "</li>\n";
	return result;
}

//------20090521---add--------------------------------------------------------------------
function getCheckBox2(obj, name, index) {
ck= "<input type=\"checkbox\" id=\"" + name + index + "\" name=\"" + name + "[" + "]\" value=\"" + obj.id + "\" />";

	var result = "<li>\n";
	result += "<input type=\"checkbox\" id=\"" + name + index + "\" name=\"" + name + "[" + "]\" value=\"" + obj.id + "\" />";
	result += "<label for=\"" + name + index + "\">" + obj.value + "</label>\n";
	result += "</li>\n";
	return result;
}

//------20090521---add--------------------------------------------------------------------



