// ---------------
// 用于在新版本页面中代替原有城市选择控件，其不需要关联设置行政区与商圈信息 --by ocaso
function cityReadyNoDistrict(rootpath) {
	$("#city").autocomplete(citys, {
				minChars : 0,
				width : 190,
				scrollHeight : 260,
				max : 10,
				matchContains : false,
				mustMatch : true,
				autoFill : false,
				formatItem : function(row, i, max) {
					return "<span>" + row.s + "</span>" + row.c;
				},
				formatMatch : function(row, i, max) {
					return row.k;
				},
				formatResult : function(row) {
					return row.c;
				}
			}, cityInit);
	$("#city").result(function(event, data, formatted) {
				if (data) {
					// $('#city').css('color', '#666');
					$("#cityCode").attr("value", data.o);
				}// end if
			});
}
// ---------------

function cityReady(rootpath) {
	$("#city").autocomplete(citys, {
				minChars : 0,
				width : 190,
				scrollHeight : 260,
				max : 10,
				matchContains : false,
				mustMatch : true,
				autoFill : false,
				formatItem : function(row, i, max) {
					return "<span>" + row.s + "</span>" + row.c;
				},
				formatMatch : function(row, i, max) {
					return row.k;
				},
				formatResult : function(row) {
					return row.c;
				}
			}, cityInit);
	$("#city").result(function(event, data, formatted) {
		if (data) {
			// $('#city').css('color', '#666');
			$("#cityCode").attr("value", data.o);

			var cityid = data.o;
			$.getJSON(rootpath + "/hotelsearch/provinvce.action", {
						"cityid" : cityid
					}, function(json) {
						var obj = $("#district");
						obj.get(0).length = 0;
						obj.append("<option value=''>---全部---</option>");
						for (var i = 0; i < json.length; i++) {

							var opt = json[i].split("#");
							obj.append("<option value='" + opt[0] + "'>"
									+ opt[1] + "</option>");
						}
						obj.attr("disabled", "");

						var obj = $("#district2");
						obj.children().remove();
						obj.append("<option value=''>---全部---</option>");
						for (var i = 0; i < json.length; i++) {

							var opt = json[i].split("#");
							obj.append("<option value='" + opt[0] + "'>"
									+ opt[1] + "</option>");
						}
						obj.attr("disabled", "");
					})
		}// end if
	});
}
function citySearchReady(rootpath) {

	$("#city").autocomplete(citys, {
				minChars : 0,
				width : 190,
				scrollHeight : 260,
				max : 10,
				matchContains : false,
				mustMatch : true,
				autoFill : false,
				formatItem : function(row, i, max) {
					return "<span>" + row.s + "</span>" + row.c;
				},
				formatMatch : function(row, i, max) {
					return row.k;
				},
				formatResult : function(row) {
					return row.c;
				}
			});
	$("#city").result(function(event, data, formatted) {
		if (data) {
			// $('#city').css('color', '#666');
			$("#cityCode").attr("value", data.o);

			var cityid = data.o;
			$.getJSON(rootpath + "/hotelsearch/provinvce.action", {
						"cityid" : cityid
					}, function(json) {
						var obj = $("#district");
						obj.get(0).length = 0;
						obj.append("<option value=''>---全部---</option>");
						for (var i = 0; i < json.length; i++) {

							var opt = json[i].split("#");
							obj.append("<option value='" + opt[0] + "'>"
									+ opt[1] + "</option>");
						}
						obj.attr("disabled", "");
					})

			// hotelsearch.jsp左侧对应的酒店推荐和热闹目的城市
			searchSpecHotel(cityid, rootpath);
		}// end if
	});

	if (window.attachEvent) {
		$('#city').get(0).attachEvent("onfocus", function() {
					$('#selectProvince').attr('checked', 'true');
				});
	} else if (window.addEventListener) {
		$('#city').get(0).addEventListener("focus", function() {
					$('#selectProvince').attr('checked', 'true');
				}, false);
	}

}

function cityDistrictSelected(dtarg, cityCode, districtCode, rootpath) {
	if (cityCode == '')
		cityCode = 'Shanghai';

	$.getJSON(rootpath + "/hotelsearch/provinvce.action", {
				"cityid" : cityCode
			}, function(json) {
				if (json == null || json == "")
					return;
				var obj3 = $(dtarg);
				obj3.get(0).length = 0;
				obj3.append("<option value=''>全部</option>");
				for (var i = 0; i < json.length; i++) {
					var opt = json[i].split("#");
					if (opt[0] == districtCode)
						obj3.append("<option value='" + opt[0]
								+ "' selected='selected'>" + opt[1]
								+ "</option>");
					else
						obj3.append("<option value='" + opt[0] + "'>" + opt[1]
								+ "</option>");
				}
				obj3.attr("disabled", "");
			})
}

function setCityClickEvent(elementname, elementname1, targElementName, rootpath) {
	// 注:元素的disable == true 时，form 对应的name作为传递给action的参数不可用
	// 点击后退按钮或是默认为上海时使用

	if ($("#selectProvince").attr("checked") == true) {
		$("#city").attr("disabled", "");
		$("#cityCode").attr("disabled", "");
	}
	$(elementname).each(function() {
				if ($(this).attr("checked") == true) {
					var cityid = $(this).val();
					$(elementname1).each(function() {
								var temp = $(this).attr("seq");
								if (cityid == temp) {
									$(this).attr("disabled", "");
									searchSpecHotel(cityid, rootpath);
									searchDistrict("#district", cityid,
											rootpath);
								}
							});
				}
			});
	// 点radio时使用
	$(elementname).click(function() {
		// searchSpecHotel(elementname);
		$(this).attr("checked", "true");
		if (this.checked) {
			var cityid = this.value;

			searchSpecHotel(cityid, rootpath);
			/*
			 * $(elementname1).each(function(){ var temp = $(this).attr("seq");
			 * 
			 * if(cityid == temp){ $(this).attr("disabled", ""); }else{
			 * $(this).attr("disabled", "true"); }
			 * 
			 * });
			 */
		}
		/*
		 * if (!$("#selectProvince").attr("checked")) {
		 * $("#city").attr("disabled", "true"); $("#cityCode").attr("disabled",
		 * "true"); }else{ $("#city").attr("disabled", "");
		 * $("#cityCode").attr("disabled", ""); }
		 */
		// 对应的地区数据内容显示
		var cityid = this.value;
		if (cityid == null || cityid == "")
			return;
		$.getJSON(rootpath + "/hotelsearch/provinvce.action", {
					"cityid" : cityid
				}, function(json) {
					var obj = $(targElementName);
					obj.get(0).length = 0;
					obj.append("<option value=''>---全部---</option>");
					for (var i = 0; i < json.length; i++) {
						var opt = json[i].split("#");
						obj.append("<option value='" + opt[0] + "'>" + opt[1]
								+ "</option>");
					}
					obj.attr("disabled", "");
				})

	});
}
