﻿function $(id){
	var d = document;
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	if(d.layers) el.style=el;
	return el;
}

function IsIE(){
  return (window.navigator.appName.toUpperCase().indexOf("MICROSOFT")>=0); 
}
function homeUrl(){
	var url=document.URL;
	return url.substring(0,url.indexOf("/",7));
}

//更改验证码
function changeCode(id){
	var d=new Date();
	var tm=d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
	$(id).innerHTML='<img src="../inc/checkCode.asp?tm='+tm+'" height=18 align=absmiddle />';
}

//限制上传文件类型,ftype扩展名列表,以“,”分隔
function upFileType(fname,ftype){
	if(fname!=''){
		var kzms=ftype.toUpperCase();
		var kzm='/';
		var pnt=fname.lastIndexOf(".");
		if(pnt!=-1) kzm=fname.substr(pnt);
		if(kzms.indexOf(kzm.toUpperCase())==-1){
			return false;
		}else{
			return true;
		}
	}else{
		return false;
	}
}

//圖片預載入
function imgLoad(obj,src,m,n){
	obj.onload=null;
	var newimg=new Image();
	newimg.onload=function(){
		newimg.onload=null;
		var w=newimg.width;
		var h=newimg.height;
		var l=w/h;
		obj.src=src;
		if(w>m || h>n){
			if( w>=m && l>m/n){
				obj.width=m; 
				obj.height=m/l
			}else {
				obj.width=n*l;
				obj.height=n;
			}
		}
	}
	newimg.src=src;
}

//改变文本显示的字号
function txtSize(id,size){
	var obj=$(id);
	if(obj) obj.style.fontSize=size+"px";
}

//iframe自适应高度
function SetWinHeight(obj){
	var win=obj;
	if (document.getElementById){
		if (win && !window.opera){
			if (win.contentDocument && win.contentDocument.body.offsetHeight>50) 
				win.height = win.contentDocument.body.offsetHeight; 
			else if(win.Document && win.Document.body.scrollHeight>50)
				win.height = win.Document.body.scrollHeight;
			else
				win.height=50;
			win.height=Number(win.height)+20;
		}
	}
}

//自适应窗口大小显示图片
var imgObj;
function checkImag(theURL,winName){
  if (typeof(imgObj) == "object"){
	if ((imgObj.width != 0) && (imgObj.height != 0))
	  openImg(theURL,winName, ",width=" + (imgObj.width+20) + ",height=" + (imgObj.height+30));
	else
	  setTimeout("checkImag('" + theURL + "','" + winName + "')", 100)
  }
}
function openImg(theURL,winName,features) {
	var aNewWin, sBaseCmd;
	sBaseCmd = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,";
	if (features == null || features == ""){
		imgObj = new Image();
		imgObj.src = theURL;
		checkImag(theURL, winName)
	}else{
		aNewWin = window.open(theURL,winName, sBaseCmd + features);
		aNewWin.focus();
	}
}

//限制只能输入数字和符号
function isNum(evt){ 
	var nList="0123456789.";
	evt = evt ? evt : (window.event ? window.event : null);
	var stc=evt.keyCode || evt.which;
	var reg=new RegExp(String.fromCharCode(stc),"i");
	if(nList.search(reg)>=0 || checkKey(stc) )
		return true;
	else
		return false;
}
function checkKey(key){
	var keys=[8,9,13,37,39,45,46];
	for(k=0;k<keys.length;k++){
		if(key==keys[k]) return true;
	}
	return false;
}

//检查E-Mail
function IsEmail(str){
	var nLen;
	var nCnt1, nCnt2;
	nCnt1=0;
	nCnt2=0;
	nLen = str.length;
	for(var i=0; i<nLen; i++){
		if(str.charAt(i)==' ' || str.charAt(i)==',') return false;
		if(str.charAt(i)=="'" || str.charAt(i)=='"' ) return false;
		if(str.charAt(i)=='<' || str.charAt(i)=='>' ) return false;
		if(str.charAt(i)=='@'){
			if(str.charAt(i+1)=='.') return false;
			nCnt1++;
		}
		if(str.charAt(i)=='.'){
			if(str.charAt(i+1)=='.') return false;
			nCnt2++;
		}
	}
	if(str.charAt(0)=='@' || str.charAt(nLen-1)=='.') return false;
	if( nCnt1!=1 || nCnt2<1){
		return false;
	}else
		return true;
}

//增加 cookie
function addCookie(name,value,expireHours){
      var cookieString=name+"="+escape(value)+"; path=/";
      //判断是否设置过期时间
      if(expireHours>0){
             var date=new Date();
             date.setTime(date.getTime+expireHours*3600*1000);
             cookieString=cookieString+"; expires="+date.toGMTString();
      }
      document.cookie=cookieString;
}
//读取 cookie
function getCookie(name){
      var strCookie=document.cookie;
      var arrCookie=strCookie.split("; ");
      for(var i=0;i<arrCookie.length;i++){
            var arr=arrCookie[i].split("=");
            if(arr[0]==name)return unescape(arr[1]);
      }
      return "";
}
//删除 cookie
function delCookie(name){
       var date=new Date();
       date.setTime(date.getTime()-(86400 * 1000));
	   var cval=getCookie(name);
       document.cookie=name+"=; expires="+date.toGMTString()+"; path=/";
}


//打印网页内容（<!--startprint-->和<!--endprint-->之间的内容）
function doPrint(til) { 
	bdhtml=window.document.body.innerHTML; 
	var d=new Date();
	sprnstr="<!--startprint-->"; 
	eprnstr="<!--endprint-->"; 
	prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); 
	prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); 
	prnhtml=prnhtml.replace(/#FFFFFF/gi,'#FF0000');
	prnhtml=prnhtml.replace(/#FFF/gi,'#F00');
	var printcode="<OBJECT classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2' height=0 id=wb name=wb width=0></OBJECT>"
	var printctrl="<div class='print_ctrl print_div'><input value='打印' type='button' onclick='window.print()' style='margin:0px 3px' /><input value='打印预览' type='button' onclick='$(\"wb\").execwb(7,1)' style='margin:0px 3px' /><input value='页面设置' type='button' onclick='$(\"wb\").execwb(8,1)' style='margin:0px 3px' /><input value='关闭' type='button' onclick='window.close()' style='margin:0px 3px' /></div>";
	
	if(IsIE()){
		var pwin=window.open("../inc/print.htm","pwin");
		pwin.document.write("<html><head>"+document.getElementsByTagName("HEAD")(0).innerHTML+"</head><body class='print_body'>"+printcode+printctrl+"<div><div align=center style='padding:20px 0px;font-size:20px;font-weight:bold;'>"+til+"</div><div align=left>"+prnhtml+"</div><div align=right style='padding:10px 20px;font-size:12px;'>打印时间："+d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日 "+d.getHours()+":"+tenNum(d.getMinutes())+":"+tenNum(d.getSeconds())+"</div></div>"+"</body></html>"); 
		pwin.location.reload();
	}else{
		window.document.body.innerHTML="<div class='print_body'><div align=center style='padding:20px 0px;font-size:20px;font-weight:bold;'>"+til+"</div><div align=left>"+prnhtml+"</div><div align=right style='padding:10px 20px;font-size:12px;'>打印时间："+d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日 "+d.getHours()+":"+tenNum(d.getMinutes())+":"+tenNum(d.getSeconds())+"</div></div>"; 
		window.print(); 
		location.reload();
		//$('wb').execwb(7,1);
	}
}
function tenNum(n){
	if(n<10){
		return "0"+n;
	}else
		return n;
}

/**
* 将数值四舍五入后格式化.
*
* num 数值(Number或者String)
* cent 要保留的小数位(Number)
* isThousand 是否需要千分位 0:不需要,1:需要(数值类型);
* return 格式的字符串,如'1,234,567.45'
*/
function formatNumber(num,cent,isThousand) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";	//检查传入数值为数值类型.
	if(isNaN(cent)) cent = 0;	//确保传入小数位为数值型数值.
	cent = parseInt(cent);
	cent = Math.abs(cent);	//求出小数位数,确保为正整数.
	if(isNaN(isThousand)) isThousand = 0;	//确保传入是否需要千分位为数值类型.
	isThousand = parseInt(isThousand);
	if(isThousand < 0) isThousand = 0;
	if(isThousand >=1) isThousand = 1;	//确保传入的数值只为0或1
	sign = (num == (num = Math.abs(num)));//获取符号(正/负数)
	//Math.floor:返回小于等于其数值参数的最大整数
	//把指定的小数位先转换成整数.多余的小数位四舍五入.
	num = Math.floor(num*Math.pow(10,cent)+0.50000000001);	
	cents = num%Math.pow(10,cent); //求出小数位数值.
	num = Math.floor(num/Math.pow(10,cent)).toString();//求出整数位数值.
	cents = cents.toString();//把小数位转换成字符串,以便求小数位长度.
	while(cents.length<cent){//补足小数位到指定的位数.
		cents = "0" + cents;
	}
	if(isThousand == 0) return (((sign)?'':'-') + num + '.' + cents);//不需要千分位符.
	//对整数部分进行千分位格式化.
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

//IE6正确显示PNG
function correctPNG(){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:pointer;" + imgStyle    ; 
			var strNewHTML = "<span " + imgID + imgClass + imgTitle+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i--;
		}
	}
}
function alphaBackgrounds(){
	for (i=0; i<document.all.length; i++){
		var bg = document.all[i].currentStyle.backgroundImage;
		if (bg){
			if (bg.match(/.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='crop')";
				document.all[i].style.backgroundImage = 'none';
			}                                               
		}
	}
}
function isIE6(){
	var rslt = navigator.appVersion.match(/MSIE (\d+.\d+)/);
	return (rslt != null && Number(rslt[1]) == 6);
}
if(isIE6()){
	window.attachEvent("onload", correctPNG);
	//window.attachEvent("onload", alphaBackgrounds);
}
//*******************************

var imgW=0;
function csimgLoad(obj){
	obj.onload=null;
	obj.style.border='1px solid #999';
	var newimg=new Image();
	newimg.onload=function(){
		newimg.onload=null;
		var w=newimg.width;
		var h=newimg.height;
		var l=w/h;
		h=90;
		w=Math.floor(h*l);
		imgW+=w+4;
		if(obj.parentNode){
			if(obj.parentNode.parentNode){
				oTD=obj.parentNode.parentNode;
				if(oTD.tagName=="DIV" && w>0) oTD.style.width=(w+2)+"px";
			}
		}
	}
	newimg.src=obj.src;
}

