//公用JS  需要jquery支持
$(document).ready(function(){
	$("#chkall_box").change(function(){
		var ckcls = $(this).attr("control");
		var status = "";
		if($(this).attr("checked"))
			status = "checked";
		else
			status = "";
		$("."+ckcls).each(function(){
			$(this).attr("checked",status);
		});
	});
	$("#chkall_box").click(function(){
		var ckcls = $(this).attr("control");
		var status = "";
		if($(this).attr("checked"))
			status = "checked";
		else
			status = "";
		$("."+ckcls).each(function(){
			$(this).attr("checked",status);
		});
	});
});
function Stand_Ajax(url,para){
	var retdata = "";
	$.ajax({
		type: "post",
		url: url,
		dataType:"html",
		data:para,
		async:false,
		success: function(data, textStatus){
			retdata = data;
		}
	});
	return retdata;
}
//消息对话框
var SysDialog = function(para){
	var defpara = {
		"width":350,
		"height":140,
		"posX":-1,
		"posY":-1,
		"moveX":0, //对话框坐标偏移量
		"moveY":0,
		"buttonText":"_SysClose,关闭",//多按钮用|分隔开,id 与 字符 以，分隔 eg:  id,按钮字符|id,按钮
		"showClose":true,
		"canMove":true,
		"run":""
	};
	var _pubp = {};
	if(typeof(para)=="undefined"){
		_pubp = defpara;
	}else{
		_pubp.width = typeof(para.width)=="undefined"?defpara.width:para.width;
		if(_pubp.width<350) _pubp.width = 350;
		_pubp.height = typeof(para.height)=="undefined"?defpara.height:para.height;
		if(_pubp.height<140) _pubp.height = 140;
		_pubp.bgimg = typeof(para.bgimg)=="undefined"?defpara.bgimg:para.bgimg;
		_pubp.posX = typeof(para.posX)=="undefined"?defpara.posX:para.posX;
		_pubp.posY = typeof(para.posY)=="undefined"?defpara.posY:para.posY;
		_pubp.moveX = typeof(para.moveX)=="undefined"?defpara.moveX:para.moveX;
		_pubp.moveY = typeof(para.moveY)=="undefined"?defpara.moveY:para.moveY;
		_pubp.buttonText = typeof(para.buttonText)=="undefined"?defpara.buttonText:para.buttonText;
		_pubp.showClose = typeof(para.showClose)=="undefined"?defpara.showClose:para.showClose;
		_pubp.canMove = typeof(para.canMove)=="undefined"?defpara.canMove:para.canMove;
		_pubp.run = typeof(para.run)=="undefined"?defpara.run:para.run;
	}
	
	var check = function(){
		if($(".zz").html()!=null)
			return false;
		else
			return true;
	}
	var closedia = function(){
		$(".Sys_dialog").fadeOut("fast",function(){$(".Sys_dialog").remove();});
		$(".zz").fadeOut("fast",function(){$(".zz").remove();});
		unbingevent();
	}
	var bingevent = function(){
		$("#_dia_close,#_SysClose").click(function(){
			closedia();
		});
		$("#_dia_min").hover(
			function(){
				$("#_dia_min").addClass("dia_title_btn_min_hover");
			},function(){
				$("#_dia_min").removeClass("dia_title_btn_min_hover");
			}
		);
		$("#_dia_close").hover(
			function(){
				$("#_dia_close").addClass("dia_title_btn_close_hover");
			},function(){
				$("#_dia_close").removeClass("dia_title_btn_close_hover");
			}
		);
		$(".dia_foot_btn").hover(
			function(){
				$(this).removeClass("dia_foot_btn");
				$(this).addClass("dia_foot_btn_hover");
			},function(){
				$(this).removeClass("dia_foot_btn_hover");
				$(this).addClass("dia_foot_btn");
			}
		);
	}
	var unbingevent = function($obj){
		$("#_dia_close").unbind("click");
		$("#_SysClose").unbind("click");
		$("#_dia_min").unbind("hover");
		$("#_dia_close").unbind("hover");
		$(".dia_foot_btn").unbind("hover");
	}
	return {
		Show : function(_msg,_prep){
			//重设参数
			var p = {};
			if(typeof(_prep)=="undefined"){
				p = _pubp;
			}else{
				p.width = typeof(_prep.width)=="undefined"?_pubp.width:_prep.width;
				if(p.width<350) p.width = 350;
				p.height = typeof(_prep.height)=="undefined"?_pubp.height:_prep.height;
				if(p.height<140) p.height = 140;
				p.bgimg = typeof(_prep.bgimg)=="undefined"?_pubp.bgimg:_prep.bgimg;
				p.posX = typeof(_prep.posX)=="undefined"?_pubp.posX:_prep.posX;
				p.posY = typeof(_prep.posY)=="undefined"?_pubp.posY:_prep.posY;
				p.moveX = typeof(_prep.moveX)=="undefined"?_pubp.moveX:_prep.moveX;
				p.moveY = typeof(_prep.moveY)=="undefined"?_pubp.moveY:_prep.moveY;
				p.buttonText = typeof(_prep.buttonText)=="undefined"?_pubp.buttonText:_prep.buttonText;
				p.showClose = typeof(_prep.showClose)=="undefined"?_pubp.showClose:_prep.showClose;
				p.canMove = typeof(_prep.canMove)=="undefined"?_pubp.canMove:_prep.canMove;
				p.run = typeof(_prep.run)=="undefined"?_pubp.run:_prep.run;
			}
			
			//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
			if(!check()){
				$(".Sys_dialog").remove();
				$(".zz").remove();
			}
			var html = '<div class="zz"></div>';
			$(html).appendTo("body");
			$(".zz").css({height:$(window).height()+"px"});
			$(".zz").fadeTo("fast",0.5);
			var diahtml = '<div class="Sys_dialog">';
				diahtml += '	<div class="dia_title">';
				diahtml += '		<div class="dia_title_left"></div>';
				diahtml += '		<div class="dia_title_text"></div>';
				diahtml += '		<div class="dia_title_right"></div>';
			if(p.showClose){
				diahtml += '		<div class="dia_title_btn">';
				diahtml += '			<div id="_dia_min" class="dia_title_btn_min"></div>';
				diahtml += '			<div id="_dia_close" class="dia_title_btn_close"></div>';
				diahtml += '		</div>';
			}
				diahtml += '	</div>';
				diahtml += '	<div class="dia_bottom">';
				diahtml += '		<div class="dia_content">';
				diahtml += '			<div class="dia_content_text">'+_msg+'</div>';
				diahtml += '		</div>';
				diahtml += '		<div class="dia_foot">';
					var buttons = p.buttonText.length>0?p.buttonText.split("|"):"";
					for(var i=0;i<buttons.length;i++){
						var tempbtn = buttons[i].split(",");
						diahtml += '<input type="button" class="dia_foot_btn" id="'+tempbtn[0]+'" value="'+tempbtn[1]+'" />';
					}
					if(i==0){
						diahtml += '<img src="IMAGES/animate.gif" style="margin:10px 15px;" />';
					}
				diahtml += '		</div>';
				diahtml += '	</div>';
				diahtml += '</div>';
			$(diahtml).appendTo("body");
			
			if(p.canMove)	can_move($(".Sys_dialog"));
			//设置对话框CSS
			$(".Sys_dialog").css({width:p.width+"px",height:p.height+"px"});
			$(".dia_bottom").css({height:p.height-30+"px"});
			$(".dia_content").css({height:p.height-30-33+"px"});
			if(p.posX<0){
				$(".Sys_dialog").css({left:($(window).width()-p.width)/2+p.moveX+"px"});
			}else{
				$(".Sys_dialog").css({left:p.posX+"px"});
			}
			if(p.posY<0){
				$(".Sys_dialog").css({top:($(window).height()-p.height)/2+p.moveY+"px"});
			}else{
				$(".Sys_dialog").css({top:p.posY+"px"});
			}
			//绑定对话框事件
			if(typeof(p.run)!="undefined" && p.run.length>0)
				eval(p.run);
			
			$(".dialog").fadeIn("slow");
			bingevent();
		},
		Close : function(){closedia();}
	}
}
//INPUT提示框
var Hint = function($obj,para){
	var defpara = {
		"ajax":false,
		"page":"",
		"para":"",
		"foreHint":"",
		"backHint":"",
		"maxlen":10
	}
	var p = {};
	if(typeof(para)=="undefined"){
		p = defpara;
	}else{
		p.ajax = typeof(para.ajax)=="undefined"?defpara.ajax:para.ajax;
		p.page = typeof(para.page)=="undefined"?defpara.page:para.page;
		p.para = typeof(para.para)=="undefined"?defpara.para:para.para;
		p.foreHint = typeof(para.foreHint)=="undefined"?defpara.foreHint:para.foreHint;
		p.backHint = typeof(para.backHint)=="undefined"?defpara.backHint:para.backHint;
		p.maxlen = typeof(para.maxlen)=="undefined"?defpara.maxlen:para.maxlen;
	}
	//获取数据
	var getData = function(){
		var retdata = "";
		if(p.ajax){
			$.ajax({
				type: "post",
				url: p.page,
				dataType:"html",
				data: p.para,
				async:false,
				success: function(data, textStatus){
					retdata = data;
				}
			});
		}else{
			retdata = $obj.val();
		}
		return retdata;
	}
	//注册事件
	var setEvent = function(){
		$("#hint").find("ul").find("li").hover(
			function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});
		$("#hint").find("ul").find("li").click(function(){
			$obj.val($(this).attr("setvalue"));
		});
	}
	//返回方法
	return{
		Close : function(){
			$("select").show();
			$("#hint").remove();
		},
		Show : function(){
			$("select").hide();
			if($("#hint").html()!=null){
				$("#hint").remove();
			}
			var hint = '<div class="hint" id="hint"><ul></ul></div>';
			var data = getData();
			if(data.length<1){
				$("select").show();
				return false;
			}
			$(hint).appendTo("body");
			var hw = 180;
			var hh = $obj.height()+5;
			if($obj.width()>180){
				hw = $obj.width();
			}
			$("#hint").css({width:hw,top:$obj.offset().top+hh+"px",left:$obj.offset().left+"px"});
			
			data = data.split(",");
			for(var i=0;i<data.length;i++){
				if(p.backHint.length>0){
					hback = p.backHint.split(",");
					for(var j=0;j<hback.length;j++){
						data[i] = data[i].replace(hback[j],"");
					}
					copydata = data[i];
					for(var j=0;j<hback.length;j++){
						if(data[i].length>p.maxlen){
							data[i] = "..."+data[i].substr(data[i].length-p.maxlen+3,p.maxlen-3);
						}
						$("#hint").find("ul").append("<li setvalue='"+copydata+hback[j]+"'>"+p.foreHint+" "+data[i]+" "+hback[j]+"</li>");
					}
				}else{
					$("#hint").find("ul").append("<li setvalue='"+data[i]+"'>"+p.foreHint+" "+data[i]+"</li>");
				}
			}
			setEvent();
		}
	}
}
function can_move($obj){  //让对象可以移动
		var bclick=false;//获取鼠标点击
	$obj.find("div:first").mousedown(function(event){
		bclick=true;
		var offset = $(this).offset();
		_x=event.clientX-offset.left; 
		_y=event.clientY-offset.top;
		$obj.css({"top":offset.top+"px"});
		var ex,ey;
		var maxlefft,maxtop;
		maxlefft = $(window).width()-$(this).width()-30;
		maxtop = $(window).height()-$(this).height()-30;
		$obj.mousemove(function(event){
			if(!bclick){
				return false;//杨仕何改进
			}
			_xx=event.clientX-_x;
			_yy=event.clientY-_y;
			if(typeof(ey)!="undefined"){
				if($(this).offset().left<=10 && event.clientX < ex) _x-=(ex - event.clientX);
				if($(this).offset().left>=maxlefft && event.clientX > ex) _x+=(event.clientX - ex);
				if($(this).offset().top<=10 && event.clientY < ey) _y-=(ey - event.clientY);
				if($(this).offset().top>=maxtop && event.clientY > ey) _y+=(event.clientY - ey);
			}
			if(($(this).offset().left>10 || ($(this).offset().left<=10 && event.clientX > ex )) && ($(this).offset().left< maxlefft || ($(this).offset().left>=maxlefft && event.clientX < ex) )){$(this).css("left",_xx+"px");}
			if(($(this).offset().top>10 || ($(this).offset().top<=10 && event.clientY > ey )) && ($(this).offset().top< maxtop || ($(this).offset().top>=maxtop && event.clientY < ey) )){$(this).css("top",_yy+"px");}
			ex = event.clientX;
			ey = event.clientY;
			return false;
		}); 
		return false;
	}); 
	$obj.mouseup(function(){ 
		bclick=false;
		$(this).unbind("mousemove");
		return false;
	});
}
function openWin(url,width,height){
	window.open(url,'系统窗口',"width="+width+",height="+height+",toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
}
function openWinDialog(url,width,height){
	window.showModalDialog(url,window,'dialogWidth='+width+'px;dialogHeight='+height+'px;top=100px;status=no;');
}

function copyToClipboard(txt) {    
     if(window.clipboardData) {    
             window.clipboardData.clearData();    
             window.clipboardData.setData("Text", txt);    
     } else if(navigator.userAgent.indexOf("Opera") != -1) {    
          window.location = txt;    
     } else if (window.netscape) {    
          try {    
               netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");    
          } catch (e) {    
               alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
          }    
          var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);    
          if (!clip) {var dia = new Dialog('复制失败...');dia.showdia();return;}
          var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);    
          if (!trans) {var dia = new Dialog('复制失败...');dia.showdia();return;}
          trans.addDataFlavor('text/unicode');    
          var str = new Object();    
          var len = new Object();    
          var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);    
          var copytext = txt;    
          str.data = copytext;    
          trans.setTransferData("text/unicode",str,copytext.length*2);    
          var clipid = Components.interfaces.nsIClipboard;    
          if (!clip)    
               return false;    
          clip.setData(trans,null,clipid.kGlobalClipboard); 
     }
	 var dia = new Dialog('复制成功...',{"bgimg":""});
	 dia.showdia();
}   

function gotoUrl(para){  //生成新的URL,使参数不能重复,para即新参数:a=1&b=1&c=1
	var url = window.location.href;
	newpara = para.split("&");
	var newurl = "";
	if(url.indexOf("?")!=-1){
		var canadd,url2;
		var url1 = url.substr(url.indexOf("?")+1);
		url1 = url1.split("&");
		for(var i=0;i<url1.length;i++){
			canadd = true;
			url2 = url1[i].split("=")[0];
			for(var j=0;j<newpara.length;j++)
				if(url2!="" && url2==newpara[j].split("=")[0]) canadd = false;
			if(canadd) newurl += "&"+url1[i];
		}
		newurl = url.substr(0,url.indexOf("?")+1)+newurl.substr(1)+"&"+para;
	}else{
		newurl = url+"?"+para;
	}
	return newurl;
}


