$(function() {
	var O_Type = $("#O_Type");
	var mainlist = $("#mainlist");
	var O_SimpleSearchBtn = $("#O_SimpleSearchBtn");
	var O_Flush = $("#O_Flush");
	O_Flush.hide();
	mainlist.find(".page").click(function() {
		mainlist.find("#loading").show();
		pageClick(this);
	});
	//点击快捷搜索按钮
	O_SimpleSearchBtn.click(function() {
		var v_field = $("#simpleSearch").val();
		var O_Key = $("#simpleSearchKey");
		var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		//设置列表类型
		if(v_key == ""){ O_Type.val("list"); }
		else { O_Type.val("simplesearch"); }
		O_Flush.show();
		$.post("?m=admin/city.simpleSearch", { field:v_field, key:v_key }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			O_Flush.hide();
		});
	});
	getCount();
});
//点击状态按钮
function statusClick(v_status) {
	var mainlist = $("#mainlist");
	var O_Flush = $("#O_Flush");
	//设置列表类型
	$("#O_Type").val("bystatus");
	$("#currentStatus").val(v_status);
	O_Flush.show();
	$.post("?m=admin/city.getListByStatus", { status:v_status }, function(data) {
		mainlist.html(data);
		mainlist.find(".page").click(function() {
			mainlist.find("#loading").show();
			pageClick(this);
		});
		O_Flush.hide();
	});
}
//加载列表
function loadList(page, linehighId) {
	var mainlist = $("#mainlist");
	var v_type = $("#O_Type").val();
	var O_Flush = $("#O_Flush");
	O_Flush.show();
	if(v_type == "list"){
		$.post("?m=admin/city.getList", { page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	} else if(v_type == "simplesearch") {
		var v_field = $("#simpleSearch").val();
		var O_Key = $("#simpleSearchKey");
		var v_key = O_Key.val().replace(/ /g, '').replace(/'/g, '');
		$.post("?m=admin/city.simpleSearch", { field:v_field, key:v_key, page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	} else if(v_type == "bystatus") {
		var v_status = $("#currentStatus").val();
		$.post("?m=admin/city.getListByStatus", { status:v_status, page:page }, function(data) {
			mainlist.html(data);
			mainlist.find(".page").click(function() {
				mainlist.find("#loading").show();
				pageClick(this);
			});
			if(linehighId) {
				mainlist.find("#O_Row_" + linehighId).addClass("on");//高亮行
			}
			O_Flush.hide();
		});
	}
}
//分页点击
function pageClick(t) {
	loadList(t.id);
}
//首页
function firstPage() {
	var O_FirstPage = document.createElement("span");
	O_FirstPage.id = "1";
	$("#loading").show();
	pageClick(O_FirstPage);
}
//上一页
function prevPage() {
	var prevPage = parseInt($("#O_CurrentPage").val()) - 1;
	var O_PrevPage = $("#" + prevPage.toString());
	O_PrevPage.triggerHandler("click");
}
//下一页
function nextPage() {
	var nextPage = parseInt($("#O_CurrentPage").val()) + 1;
	var O_NextPage = $("#" + nextPage.toString());
	O_NextPage.triggerHandler("click");
}
//尾页
function lastPage() {
	var pageCount = parseInt($("#O_PageCount").val());
	var O_LastPage = document.createElement("span");
	O_LastPage.id = pageCount.toString();
	$("#loading").show();
	pageClick(O_LastPage);
}
//跳转到下拉列
function goPage() {
	var O_Page = $("#O_Page");
	var O_Go = $("#O_Go");
	var O_GoPage  = document.createElement("span");
	O_GoPage.id = O_Page.val();
	$("#loading").show();
	pageClick(O_GoPage);
}
//浏览
function viewClick(t) {
	$("#divView h1 span").html("城市信息浏览");
	var O_Content = $("#contentView");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><span style="margin-top:120px;margin-bottom:15px;">正在加载...</span><img src="theme/admin/images/loading.gif"></div>');
	setView();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/city.info", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setView();
	});
	//启动浏览模式
	openView();
}
//添加
function addClick() {
	$("#divUpdate h1 span").html("城市信息添加");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	$.post("?m=admin/city.add", {}, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { add(); });
	});
	//启动浏览模式
	openUpdate();
}
//状态更新
function updateStatusClick(t) {
	var page = $("#O_CurrentPage").val();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/city.updateStatusHandle", { id:id }, function(data) {//alert(data);
		if(data[0] == "success") {
			//刷新列表
			loadList(page, id);
			//刷新统计
			getCount();
		} else {
			alert(data[1]);
		}
	}, "json");
}
//信息更新
function updateClick(t) {
	$("#divUpdate h1 span").html("城市信息更新");
	var O_Content = $("#contentUpdate");
	O_Content.html('<div style="width:580px;height:200px;_height:300px;font-size:14px;text-align:center;"><div style="margin-top:120px;margin-bottom:15px;">正在加载...</div><img src="theme/admin/images/loading.gif"></div>');
	setUpdate();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/city.update", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { update(); });
	});
	//启动浏览模式
	openUpdate();
}
//删除
function delClick(t) {
	if(confirm("删除所选城市，确认？")) { 
		var id = t.id.replace(/o_/g, '');
		$.post("?m=admin/city.delHandle", { id:id }, function(data) {
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page);
				//刷新统计
				getCount();
			} else {
				alert(data[1]);
			}
		}, "json");
		return true;
	}
	return false;
}
//全选删除
function checkDelAll() {
	$(".delAll").attr("checked", $("#o_delall").attr("checked"));
}
//批量删除
function delSelectClick() {
	if(confirm("删除所选城市，确认？")) { 
		//遍历得到每个checkbox的value值
        var arrChk = $(":input[class='delAll']:checked");  
        var v_ids = "";  
        for (var i = 0; i < arrChk.length; i++){ 
            v_ids += arrChk[i].value;
            if(i < arrChk.length - 1) { v_ids += ","; }
        }
		$.post("?m=admin/city.delSelectHandle", { ids:v_ids }, function(data) {
			//刷新列表
			var page = $("#O_CurrentPage").val();
			loadList(page);
			//刷新统计
			getCount();
		});
		return true;
	}
	return false;
}
//获取统计
function getCount() {
	$.post("?m=admin/city.getCount", {}, function(data) {
		var str = "共 <span class=\"f_14b_f00\">" + data.all + "</span> 个, 已审核 <span class=\"f_14b_f00\">" + data.check + "</span> 个, 待审核 <span class=\"f_14b_f00\">" + data.wait + "</span> 个, 已停用 <span class=\"f_14b_f00\">" + data.stop + "</span> 个";
		$("#O_Count").html(str);
		$("#O_Flush").hide();
	}, "json");
}
//高亮行
function highLightRow(id) {
	$(".row").removeClass("on");
	$("#O_Row_" + id).addClass("on");
}
//获取窗体区域
function getRange() {//得到屏幕的大小
    var top = document.body.scrollTop;
    var left = document.body.scrollLeft;
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    if (top == 0 && left == 0 && height == 0 && width == 0) {
        top = document.documentElement.scrollTop;
        left = document.documentElement.scrollLeft;
        height = document.documentElement.clientHeight;
        width = document.documentElement.clientWidth;
    }
    return { top: top, left: left, height: height, width: width };
}
//处理
function add() {
	var page = $("#O_CurrentPage").val();
	var O_Name = $("#O_Name");
	var O_EnglishName = $("#O_EnglishName");
	var O_Province = $("#O_Province");
	var O_Remark = $("#O_Remark");
	var O_DisplayOrder = $("#O_DisplayOrder");
	
	var v_name = O_Name.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_englishName = O_EnglishName.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_province = O_Province.val();
	var v_remark = O_Remark.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_displayOrder = O_DisplayOrder.val().replace(/ /g, '').length > 0 ? O_DisplayOrder.val() : 0;
	var v_status = $("[name='O_Status']:checked").val();
	
    if(v_name == "") {
		alert("名称不能为空！");
		O_Name.val("");
		O_Name.focus();
	} else if(v_englishName == "") {
		alert("拼音/英文名不能为空！");
		O_EnglishName.val("");
		O_EnglishName.focus();
	} else if(v_province == "") {
		alert("请选择所属省份！");
		O_Province.focus();
	} else if(isNaN(v_displayOrder)) {
		alert("排序格式错误！");
		O_DisplayOrder.val("");
		O_DisplayOrder.focus();
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		$.post("?m=admin/city.addHandle", { provinceId:v_province, name:v_name, englishName:v_englishName, remark:v_remark, displayOrder:v_displayOrder, status:v_status }, function(data) {//alert(data);
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page);
				//刷新统计
				getCount();
				//隐藏阴影
				closeUpdate();
			} else {
				alert(data[1]);
				if(data[2] == "name is null") {
					O_Name.focus();
				} else if(data[2] == "englishName is null") {
					O_EnglishName.focus();
				} else if(data[2] == "displayOrder is null") {
					O_DisplayOrder.focus();
				}
			}
			O_Submit.attr("disabled",false);
		}, "json");
	}
}
function update() {
	var page = $("#O_CurrentPage").val();
	var id = $("#O_ID").val();
	var O_Name = $("#O_Name");
	var O_EnglishName = $("#O_EnglishName");
	var O_Province = $("#O_Province");
	var O_Remark = $("#O_Remark");
	var O_DisplayOrder = $("#O_DisplayOrder");
	
	var v_name = O_Name.val().replace(/ /g, '').replace(/'/g, '');
	var v_englishName = O_EnglishName.val().replace(/ /g, '').replace(/'/g, '').replace(/"/g, '');
	var v_province = O_Province.val();
	var v_remark = O_Remark.val().replace(/ /g, '').replace(/'/g, '');
	var v_displayOrder = O_DisplayOrder.val().replace(/ /g, '').replace(/'/g, '').length > 0 ? O_DisplayOrder.val() : 0;

	if(v_name == "") {
		alert("名称不能为空！");
		O_Name.val("");
		O_Name.focus();
	} else if(v_englishName == "") {
		alert("拼音/英文名不能为空！");
		O_EnglishName.val("");
		O_EnglishName.focus();
	} else if(v_province == "") {
		alert("请选择所属省份！");
		O_Province.focus();
	} else if(isNaN(v_displayOrder)) {
		alert("排序格式错误！");
		O_DisplayOrder.val("");
		O_DisplayOrder.focus();
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		$.post("?m=admin/city.updateHandle", { id:id, provinceId:v_province, name:v_name, englishName:v_englishName, remark:v_remark, displayOrder:v_displayOrder }, function(data) {//alert(data);
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page, id);
				//刷新统计
				getCount();
				//隐藏阴影
				closeUpdate();
			} else {
				alert(data[1]);
				if(data[2] == "name is null") {
					O_Name.focus();
				} else if(data[2] == "englishName is null") {
					O_EnglishName.focus();
				} else if(data[2] == "displayOrder is null") {
					O_DisplayOrder.focus();
				}
			}
			O_Submit.attr("disabled", false);
		}, "json");
	}
}