//页面初始化
$(document).ready(function(){
	
	//已选留言处理
	if($("#top_selectall").length !== 0 && $("#footer_selectall").length !== 0){
		if(CheckCookieEnabled() == true){
			if(document.cookie.indexOf("AdminSelMsg") !== -1){
				var AdminSelMsg = getCookie("AdminSelMsg").split(",");
				var Cboxarr = new Array();
				var IsAllSel = true;
				var Cboxarr = document.getElementsByName('sel_message[]');
				for(var i=0; i<Cboxarr.length; i++){
					if(Cboxarr[i].value !== ''){
						if($.inArray(Cboxarr[i].value, AdminSelMsg) >= 0){
							Cboxarr[i].checked = true;
						}
						else{
							IsAllSel = false;	
						}
					}
				}
				if((Cboxarr.length) == 2){
					IsAllSel = false;	
				}
				if(IsAllSel == true && $("#top_selectall").length !== 0 && $("#footer_selectall").length !== 0){
					document.getElementById('top_selectall').checked = true;
					document.getElementById('footer_selectall').checked = true;
				}
			}
		}
	}
	
	//商业授权查询
	if($("#authorization").length !== 0){
		var xmlhttp = createXMLhttp();
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 0 || xmlhttp.readyState == 1 || xmlhttp.readyState == 2 || xmlhttp.readyState == 3){
				$("#authorization").text("正在查询中……");
			}
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var r = xmlhttp.responseText;
				if(r == 1){
					$("#authorization").html('<b style="color:#00A600;">已授权</b>');
					$(".ServiceInfo").attr("style","display:block");
				}
				else if(r == 2){
					$("#authorization").html('<b style="color:#F00;">这不是您的授权码</b>');
				}
				else if(r == 3){
					$("#authorization").html('<b style="color:#999;">未查询到您的购买记录</b>');
				}
				else{
					$("#authorization").html('授权信息查询失败');
				}
			}
		}
		
		xmlhttp.open("POST", "http://www.chuangxin001.com/studio/authorization.php", true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send("DoMain=" + DOMAIN + "&AUTHORIZATION_CODE=" + AUTHORIZATION_CODE);

	}
	
	//显示已选留言数量	
	ShowSelMsgNum();	
	
});

//创建XMLHttpRequest对象
function createXMLhttp(){
	var xmlhttp;
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

//弹窗代码
;(function($,window,document,undefined){
		   //默认参数
		    var PARAMS;
		    var DEFAULTPARAMS = { Title: "标题", Content: "",BtnL:"确定",BtnR:"取消" , FunL: new Object, FunR: new Object };
		    $.DialogByZ = {
		        //弹出提示框
		        Alert: function (params) {
		            Show(params,"Alert");
		        },
		        //弹出确认框
		        Confirm: function (params) { Show(params,"Confirm"); },
		        //自动显示弹框
		        Autofade: function (params) { Show(params,"Autofade") },
		        //关闭弹出框
		        Close: function () {
		            $(".zbox-popup,.zbox-popup-backdrop").remove();
		        },
		        //加载图形
		        Loading:function(Url, text){
		        	loadBox(Url, text)
		        }
		
		    };
		        //初始化参数
		    function Init(params) {
		        if (params != undefined && params != null) {
		            PARAMS = $.extend({},DEFAULTPARAMS, params);
		        }
		    };
		    function loadBox(Url, text){
		    	var url=Url;
		    	var dislogContainer=$('<div class="zbox-popup" style="display: block;"><div style="color:#FFF; font-weight:bold; height:50px; line-height:50px; font-size:16px; text-align:center; margin-bottom:10px; background-color:#999;">'+text+'</div><img  id="zchange" src="'+url+'"></div>');
		    	var blackFilter=$('<div class="zbox-popup-backdrop" style="display: block;"></div>');
		    	setTimeout(function(){
		    	  		 $(".zbox-popup").addClass('zbox-popup-in');
		    	  		 $(".zbox-popup-backdrop").addClass('zbox-active');
		    	  	},30)
		    	$("body").append(dislogContainer);
		    }
		    function Show(params, caller){
		    	  Init(params);
		    	  var dislogContainer;
		    	  var dialogInner;
		    	  var dialogBtn;
		    	  var blackFilter=$('<div class="zbox-popup-backdrop" style="display: block;"></div>');
		    	  if(caller=='Autofade'){
		    	  	 dislogContainer=$('<div class="zbox-toast-container"><div class="zbox-toast-message">'
		    	  	 +PARAMS.Content+'</div></div>');
		    	  	 $("body").append(dislogContainer);
		    	  	 setTimeout(function(){
		    	  		 $(".zbox-toast-container").addClass('zbox-active');
		    	  	},30)
		    	  	 setTimeout(function(){
		    	  	 	  $(".zbox-toast-container").remove();
		    	  	 },3000)
		    	  }else{
		    	  	dislogContainer=$('<div class="zbox-popup" style="display: block;"></div>');
		    	  	dialogInner=$('<div class="zbox-popup-inner"><div class="zbox-popup-title">'+PARAMS.Title+'</div><div class="zbox-popup-text">'+PARAMS.Content+'</div></div>');
		    	  	dialogBtn=$('<div class="zbox-popup-buttons"><span class="zbox-popup-button" index="0">'+PARAMS.BtnL+'</span></div>');
		    	  	if(caller=='Confirm'){
		    	  		dialogBtn.append($('<span class="zbox-popup-button R" index="1">'+PARAMS.BtnR+'</span>')); 
		    	  	}
		    	  	dislogContainer.append(dialogInner);
		    	  	dislogContainer.append(dialogBtn);
		    	  	setTimeout(function(){
		    	  		 $(".zbox-popup").addClass('zbox-popup-in');
		    	  		 $(".zbox-popup-backdrop").addClass('zbox-active');
		    	  	},10)
		    	  	$("body").append(blackFilter);
		    	    $("body").append(dislogContainer);
		    	    
		    	  	$(".zbox-popup-button").click(function(){
		    	  		 var indexs=$(this).attr('index');
		    	  		 if(indexs==0){
		    	  		 	//左侧按钮
		    	  		 	if($.isFunction(PARAMS.FunL)){
		    	  		 		    PARAMS.FunL();
					    	}else{
					    			$.DialogByZ.Close(); 
					    	}
		    	  		 }else{
		    	  		 	//右侧按钮 
		    	  		 	if($.isFunction(PARAMS.FunR)){
		    	  		 		    PARAMS.FunR();
					    	}else{
					    			$.DialogByZ.Close(); 
					    	}
		    	  		 }
		    	  		 return false;
		    	  	})
		    	  	 
		    	  }
		    	  //	  
		    }
		})(jQuery,window,document);	

//判断客户端是否支持Cookie
function CheckCookieEnabled(){
	if(window.navigator.cookieEnabled){
	   return true;
	}
	else{
	   return false;
	}
}

//创建Cookie
function SetCookie(name, value, expiredays){
	var oDate = new Date();
	oDate.setDate(oDate.getDate()+expiredays);
	document.cookie = name + " = " + value + ";expires = " + oDate;
}

//获取cookie
function getCookie(c_name){
	if(document.cookie.length > 0){
		c_start = document.cookie.indexOf(c_name + "=")
		if(c_start!=-1){ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if(c_end==-1){
				c_end=document.cookie.length
			}
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}

//删除Cookie
function DelCookie(Cname){
	SetCookie(Cname, "none", -1);
}

//刷新验证码
function LoadCheckCode(){
	document.getElementById("CheckCodeImg").src="index.php?action=checkcode&r" + Math.random(); 
}

//增加新栏目
function NewMenu(){
	var LastTr = $("#NavSet tr").eq(-2);
	var LastId = parseInt(LastTr.attr('id'));
	var NewId = parseInt(LastId+1);
	var newRow = "<tr id=\""+NewId+"\"><td></td><td><input class=\"input5\" name=\"NewOrder_"+NewId+"\" type=\"text\" value=\"\" /></td><td><input class=\"input6\" name=\"NewName_"+NewId+"\" type=\"text\" value=\"\" /></td><td><input class=\"input2\" name=\"NewUrl_"+NewId+"\" type=\"text\" value=\"\" /></td><td><select name=\"Newtarget_"+NewId+"\"><option value=\"_self\" selected=\"selected\">本窗口</option><option value=\"_blank\">新窗口</option></select></td><td></td></tr>";

LastTr.after(newRow);

}

//导航栏目全选
function CheckAllNav(c){
	var arr = document.getElementsByName('sel[]');	
	if(c){		//全选
		var Sel = new Array();
		for(var i=0; i<arr.length; i++){
			arr[i].checked = true;		//复选框状态设置为勾选
		}
	}
	else{		//取消全选
		var Cel = new Array();
		for(var i=0; i<arr.length; i++){
			arr[i].checked = false;			
		}
	}
}

//留言全选
function SelAllMsg(c){
	var arr = document.getElementsByName('sel_message[]');	
	if(c){		//全选
		var Sel = new Array();
		for(var i=0; i<arr.length; i++){
			arr[i].checked = true;		//复选框状态设置为勾选
			if(arr[i].value !== ''){
				Sel.push(arr[i].value);		//将勾选的留言ID添加到数组
			}
		}
		//将勾选的留言记录下来
		AddSelMsg(Sel);
		up_Sel_num();
	}
	else{		//取消全选
		var Cel = new Array();
		for(var i=0; i<arr.length; i++){
			arr[i].checked = false;	
			if(arr[i].value !== ''){
				Cel.push(arr[i].value);		//将取消勾选的留言ID添加到数组
			}			
		}
		CelSelMsg(Cel);		//更新已选留言
		up_Sel_num();
	}
}

//显示已选留言数量
function ShowSelMsgNum(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelM = getCookie("AdminSelMsg").split(",");	
			if($(".selected_num").length !== 0){
				if(AdminSelM.length == 1 && AdminSelM[0] == ''){
					$(".selected_num").html(0);
				}
				else{
					$(".selected_num").html(AdminSelM.length);	
				}
			}
		}
	}
}

//记录勾选的留言
function AddSelMsg(SelMsgArray){

	if(CheckCookieEnabled() == true){		//客户端Cookie可用
		if(document.cookie.indexOf("AdminSelMsg") == -1){	//如果没有设置Cookie
		
			SetCookie("AdminSelMsg", SelMsgArray, 100);		//创建Cookie并将勾选的留言ID数组加入
		}
		else{	//如果设置了Cookie
		
			if(getCookie("AdminSelMsg") == ''){
				SetCookie("AdminSelMsg", SelMsgArray, 100);
			}
			else{
				var AdminSelMsg = getCookie("AdminSelMsg").split(",");
				for(var i = 0; i<SelMsgArray.length; i++){
					if($.inArray(SelMsgArray[i], AdminSelMsg) == -1){
						AdminSelMsg.push(SelMsgArray[i]);
						SetCookie("AdminSelMsg", AdminSelMsg, 100);
					}
				}			
			}
		}
	}
	else{
		CookieNotEnabled();		//客户端Cookie不可用
	}
}

//取消全选的留言
function CelSelMsg(CelMsgArray){
	if(CheckCookieEnabled() == true){		//客户端Cookie可用
		if(document.cookie.indexOf("AdminSelMsg") == -1){	//如果没有设置Cookie
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");	//将Cookie中记录的已选留言转换成数组
			for(var i = 0; i <CelMsgArray.length; i++){	//逐一对比取消勾选的留言是否在Cookie中记录的已选留言中，如果存在则删除Cookie中相应的留言
				AdminSelMsg.splice($.inArray(CelMsgArray[i], AdminSelMsg), 1);
			}
			SetCookie("AdminSelMsg", AdminSelMsg, 100);	//更新已选留言数组
		}
		else{	//如果设置了Cookie
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");	//将Cookie中记录的已选留言转换成数组
			for(var i = 0; i <CelMsgArray.length; i++){	//逐一对比取消勾选的留言是否在Cookie中记录的已选留言中，如果存在则删除Cookie中相应的留言
				AdminSelMsg.splice($.inArray(CelMsgArray[i], AdminSelMsg), 1);
			}
			SetCookie("AdminSelMsg", AdminSelMsg, 100);	//更新已选留言数组
		}
	}
	else{
		CookieNotEnabled(); 	//客户端Cookie不可用
	}
}

//留言单选
function checkOne(c, Id){
	if(c){
		var SelBox = document.getElementById("SelBox_" + Id);
		var Mid = SelBox.value;
		var Sel = new Array();
		Sel.push(Mid);
		AddSelMsg(Sel);
		SetSelAllBox();
		up_Sel_num();
	}
	else{
		var SelBox = document.getElementById("SelBox_" + Id);
		var Mid = SelBox.value;
		var Cel = new Array();
		Cel.push(Mid);
		CelSelMsg(Cel);
		up_Sel_num();
		document.getElementById('top_selectall').checked = false;
		document.getElementById('footer_selectall').checked = false;
	}
}

//判断是否全选，重置头部和底部全选框状态
function SetSelAllBox(){
	
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");		
			var IsAllSel = true;
			var Cboxarr = document.getElementsByName('sel_message[]');
			for(var i=0; i<Cboxarr.length; i++){
				if(Cboxarr[i].value !== ''){
					if($.inArray(Cboxarr[i].value, AdminSelMsg) == -1){
						IsAllSel = false;
					}
				}
			}
			if(IsAllSel == true){
				document.getElementById('top_selectall').checked = true;
				document.getElementById('footer_selectall').checked = true;			
			}
		}
	}
	
}

//更新已选留言数
function up_Sel_num(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");
			if(AdminSelMsg.length == 1 && AdminSelMsg[0] == ''){
				$(".selected_num").html(0);
			}
			else{
				$(".selected_num").html(AdminSelMsg.length);	
			}
		}
		else{
			$(".selected_num").html(0);
		}
	}
}

//批量删除留言提示
function DelSelMsg(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");
			if(AdminSelMsg.length == 1 && AdminSelMsg[0] == ''){
				$.DialogByZ.Alert({Title: "提示", Content: "您没有勾选任何留言！", BtnL:"确定"});
			}
			else{
				var Durl = WEB_URL + "/admin/index.php?action=delselmsg";
				$.DialogByZ.Confirm({Title: "", Content: "确认删除选中的 <div id='DelSelMsgNum'>" + AdminSelMsg.length + "</div> 条留言吗？", FunL:function(){DelSelMsgCheck(Durl);}, FunR:null});	
			}
		}
	}
}

//执行批量删除留言操作并提示成功或失败
function DelSelMsgCheck(Durl){
	var xmlhttp = createXMLhttp();
	$.DialogByZ.Close();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 0 || xmlhttp.readyState == 1 || xmlhttp.readyState == 2 || xmlhttp.readyState == 3){
			$.DialogByZ.Loading(WEB_URL+'/admin/view/images/loading.png', '正在删除，请稍后……');
		}
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			$.DialogByZ.Close();
			var r = xmlhttp.responseText;
			if(r == 1){
				SetCookie("AdminSelMsg", "", -1);
				up_Sel_num();
				$(".MessageList").load(window.location.href + " .MessageList");
				$("#header_page_url").load(window.location.href + " #header_page_url");
				$("#footer_page_url").load(window.location.href + " #footer_page_url");
				$.DialogByZ.Alert({Title: "提示", Content: "选定的留言删除成功！", BtnL:"确定"});
			}
			else{
				$.DialogByZ.Alert({Title: "提示", Content: "删除失败！", BtnL:"确定"});
			}
		}
	}
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");
		}
	}
	xmlhttp.open("POST", Durl, true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("SelMsg=" + AdminSelMsg);		
}

//批量审核留言提示
function CkSelMsg(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");
			if(AdminSelMsg.length == 1 && AdminSelMsg[0] == ''){
				$.DialogByZ.Alert({Title: "提示", Content: "您没有勾选任何留言！", BtnL:"确定"});
			}
			else{
				var CkUrl = WEB_URL + "/admin/index.php?action=ckselmsg";
				$.DialogByZ.Confirm({Title: "", Content: "确认审核选中的 <div id='DelSelMsgNum'>" + AdminSelMsg.length + "</div> 条留言吗？", FunL:function(){CkSelMsgCheck(CkUrl);}, FunR:null});	
			}
		}
	}
}

//执行批量审核留言操作并提示成功或失败
function CkSelMsgCheck(CkUrl){
	var xmlhttp = createXMLhttp();
	$.DialogByZ.Close();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 0 || xmlhttp.readyState == 1 || xmlhttp.readyState == 2 || xmlhttp.readyState == 3){
			$.DialogByZ.Loading(WEB_URL+'/admin/view/images/loading.png', '正在审核，请稍后……');
		}
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			$.DialogByZ.Close();
			var r = xmlhttp.responseText;
			if(r == 1){
				SetCookie("AdminSelMsg", "", -1);
				up_Sel_num();
				$(".MessageList").load(window.location.href + " .MessageList");
				$("#header_page_url").load(window.location.href + " #header_page_url");
				$("#footer_page_url").load(window.location.href + " #footer_page_url");
				$.DialogByZ.Alert({Title: "提示", Content: "选定的留言审核通过！", BtnL:"确定"});
			}
			else{
				$.DialogByZ.Alert({Title: "提示", Content: "审核失败！", BtnL:"确定"});
			}
		}
	}
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("AdminSelMsg") !== -1){
			var AdminSelMsg = getCookie("AdminSelMsg").split(",");
		}
	}
	xmlhttp.open("POST", CkUrl, true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("SelMsg=" + AdminSelMsg);		
}

//删除留言提示{Mid:Mid, Durl:Durl}
function DelMsg(Mid){
	var Durl = WEB_URL + "/admin/index.php?action=delmsg";
	$.DialogByZ.Confirm({Title: "", Content: "确认删除此留言吗？", FunL:function(){DelMsgCheck(Mid, Durl);}, FunR:null});	
}

//执行删除留言操作并提示成功或失败
function DelMsgCheck(Mid, Durl){
	$.get(Durl, {id:Mid}, function(data){
		if(data == 1){
			$("#Message_" + Mid).slideUp();
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "指定的留言删除成功！", BtnL:"确定"});
		}
		else{
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "删除失败！", BtnL:"确定"});		
		}
	});
}

//审核留言提示{Mid:Mid, Durl:Durl}
function CheckMsg(Mid){
	var Durl = WEB_URL + "/admin/index.php?action=checkmsg";
	$.DialogByZ.Confirm({Title: "", Content: "确认审核此留言吗？", FunL:function(){CheckMsgCheck(Mid, Durl);}, FunR:null});	
}

//执行审核留言操作并提示成功或失败
function CheckMsgCheck(Mid, Durl){
	$.get(Durl, {id:Mid}, function(data){
		if(data == 1){
			$("#Message_" + Mid).slideUp();
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "指定的留言审核通过！", BtnL:"确定"});
		}
		else{
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "审核失败！", BtnL:"确定"});		
		}
	});
}

//删除用户提示
function DelAdminUser(UserId){
	var Durl = WEB_URL + "/admin/index.php?action=deluser";
	$.DialogByZ.Confirm({Title: "", Content: "确认删除此用户吗？", FunL:function(){CheckDelUser(UserId, Durl);}, FunR:null});
}

//确认删除用户
function CheckDelUser(UserId, Durl){
	$.get(Durl, {id:UserId}, function(data){
		if(data == 1){
			$("#UserId_" + UserId).slideUp();
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "指定的用户已被删除！", BtnL:"确定"});
		}
		else{
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "删除失败！", BtnL:"确定"});		
		}
	});
}

//Cookie不可用提示
function CookieNotEnabled(){
	alert('您的浏览器Cookie不可用，您必须设置您的浏览器Cookie为可用，才能正常使用本功能！');
}

//备份文件全选
function SelBackupFile(c){
	var arr = document.getElementsByName('del[]');	
	if(c){		//全选
		for(var i=0; i<arr.length; i++){
			arr[i].checked = true;		//复选框状态设置为勾选
		}
	}
	else{		//取消全选
		for(var i=0; i<arr.length; i++){
			arr[i].checked = false;			
		}
	}
}