var highLightIndex = -1;
$(function() {
	var O_Type = $("#O_Type");
	var mainlist = $("#mainlist");
	var O_SimpleSearchBtn = $("#O_SimpleSearchBtn");
	var O_Field = $("#simpleSearch");
	var O_Key = $("#simpleSearchKey");
	var O_KeyOffset = O_Key.offset();
	var O_Flush = $("#O_Flush");
	O_Flush.hide();
	mainlist.find(".page").click(function() {
		mainlist.find("#loading").show();
		pageClick(this);
	});
	//点击快捷搜索按钮
	O_SimpleSearchBtn.click(function() {
		simpleSearch();
	});
	O_Key.focus();
	getCount();
});
//快捷搜索
function simpleSearch() {
	var O_Type = $("#O_Type");
	var mainlist = $("#mainlist");
	var O_Flush = $("#O_Flush");
	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/member.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();
	});
}
//点击状态按钮
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/member.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/member.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/member.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/member.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/member.info", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setView();
	});
	//启动浏览模式
	openView();
}
//状态更新
function updateStatusClick(t) {
	var page = $("#O_CurrentPage").val();
	var id = t.id.replace(/o_/g, '');
	highLightRow(id);//高亮行
	$.post("?m=admin/member.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/member.update", { id:id }, function(data){//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { update(); });
	});
	//启动浏览模式
	openUpdate();
}
//密码更新
function updatePasswordClick(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/member.updatePassword", { id:id }, function(data) {//alert(data);
		O_Content.html(data);
		setUpdate();
		$("#O_SumbitUpdate").attr("disabled", false).unbind("click").click(function() { updatePassword(); });
	});
	//启动浏览模式
	openUpdate();
}
//删除
function delClick(t) {
	if(confirm("删除所选会员，确认？")) {
		var id = t.id.replace(/o_/g, '');
		$.post("?m=admin/member.delHandle", { id:id }, function(data) {//alert(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/member.delSelectHandle", { ids:v_ids }, function(data) {//alert(data);
			//刷新列表
			var page = $("#O_CurrentPage").val();
			loadList(page);
			//刷新统计
			getCount();
		});
		return true; 
	}
	return false;
}
//获取统计
function getCount() {
	$.post("?m=admin/member.getCount", {}, function(data) {
		var str = "共 <span class=\"f_14b_f00\">" + data.all + "</span> 个, 已激活 <span class=\"f_14b_f00\">" + data.active + "</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 update() {
	var page = $("#O_CurrentPage").val();
	var id = $("#O_ID").val();
	var O_Company = $("#O_Company");
	var O_Name = $("#O_Name");
	var O_Mobile = $("#O_Mobile");
	var O_Phone = $("#O_Phone");
	var O_QQ = $("#O_QQ");
	var O_Email = $("#O_Email");
	var O_Address = $("#O_Address");
	var O_Postcode = $("#O_Postcode");
	
	var v_company = O_Company.val().replace(/ /g, '').replace(/'/g, '');
	var v_name = O_Name.val().replace(/ /g, '').replace(/'/g, '');
	var v_sex = $("[name='O_Sex']:checked").val(); 
	var v_mobile = O_Mobile.val().replace(/ /g, '').replace(/'/g, '');
	var v_phone = O_Phone.val().replace(/ /g, '').replace(/'/g, '');
	var v_qq = O_QQ.val().replace(/ /g, '').replace(/'/g, '');
	var v_email = O_Email.val().replace(/ /g, '').replace(/'/g, '');
	var v_address = O_Address.val().replace(/ /g, '').replace(/'/g, '');
	var v_postcode = O_Postcode.val().replace(/ /g, '').replace(/'/g, '');

	if(v_name == "")  {
		alert("姓名不能为空！");
		O_Name.val("");
		O_Name.focus();
	} else if(v_mobile == "") {
		alert("手机号码不能为空！");
		O_Mobile.val("");
		O_Mobile.focus();
	} else if(v_mobile != "" && !/^(13|15|18)\d{9}$/.test(v_mobile)) {
		alert("请输入正确的手机号码！");
		O_Mobile.focus();
		return false;
	} else if(v_qq != "" && !/\d{5,}$/.test(v_qq)) {
		alert("联系QQ有误！");
		O_QQ.focus();
	} else if(v_email != "" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(v_email)) {
		alert("Email有误！");
		O_Email.focus();
	} else if(v_postcode != "" && !/\d{6}$/.test(v_postcode)) {
		alert("邮编有误！");
		O_Postcode.focus();
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		$.post("?m=admin/member.updateHandle", { id:id, company:v_company, name:v_name, sex:v_sex, mobile:v_mobile, phone:v_phone, qq:v_qq, email:v_email, address:v_address, postcode:v_postcode }, 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] == "mobile is null") {
					O_Mobile.focus();
				}
			}
			O_Submit.attr("disabled", false);
		}, "json");
	}
}
function updatePassword() {
	var page = $("#O_CurrentPage").val();
	var id = $("#O_ID").val();
	var O_Psd = $("#O_Psd");
	var O_Psd_Re = $("#O_Psd_Re");
	var v_psd = O_Psd.val().replace(/ /g, '').replace(/'/g, '');
	var v_psd_re = O_Psd_Re.val();
	if(v_psd == "") {
		alert("登录密码不能为空！");
		O_Psd.val("");
		O_Psd.focus();
	} else if(v_psd != v_psd_re) {
		alert("两次输入密码不一致！");
		O_Psd_Re.val("");
		O_Psd_Re.focus();
	} else {
		var O_Submit = $("#O_SumbitUpdate");
		O_Submit.attr("disabled", true);
		$.post("?m=admin/member.updatePasswordHandle", { id:id, password:v_psd }, function(data) {//alert(data);
			if(data[0] == "success") {
				//刷新列表
				var page = $("#O_CurrentPage").val();
				loadList(page, id);
				//刷新统计
				getCount();
				//隐藏阴影
				closeUpdate();
			} else if(data[0] == "success") {
				alert(data[1]);
			}
			O_Submit.attr("disabled", false);
		}, "json");
	}
}