// JavaScript Document

var ie = document.all;


//获取当前日期
function date(xtype){
	var today	= new Date();
	var TmpStr	= "";
	var Datetime = "";
	
	Datetime	= xtype.replace("yy",today.getFullYear());
	Datetime	= Datetime.replace("y",(""+today.getFullYear()).substr(1,2));
	TmpStr		= "0"+(today.getMonth()+1);
	Datetime	= Datetime.replace("mm",TmpStr.substr(TmpStr.length-2,2));
	TmpStr		= "0"+today.getDate();
	Datetime	= Datetime.replace("d",TmpStr.substr(TmpStr.length-2,2));
	TmpStr		= "0"+today.getHours();
	Datetime	= Datetime.replace("h",TmpStr.substr(TmpStr.length-2,2));
	TmpStr		= "0"+today.getMinutes();
	Datetime	= Datetime.replace("m",TmpStr.substr(TmpStr.length-2,2));
	TmpStr		= "0"+today.getSeconds();
	Datetime	= Datetime.replace("s",TmpStr.substr(TmpStr.length-2,2));
	
	return Datetime;
}


//获取星期名称
function weekday(){
	var d = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
	var today = new Date();
	return d[today.getDay()];
}


//获取当前时间
function time(){
	var today = new Date();
	return today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
}


//获取本页面地址(相对于根目录的绝对路径)
function getURL(){
	var sURL;
	if(arguments.length===0){
		sURL = location.href;
	}else if(arguments.length===1){
		sURL = arguments[0];
	}else{
		return;
	}
	
	sURL = sURL.replace("http://","");
	var oURL = sURL.split("/");
	sURL = "";
	for(var i=1;i<oURL.length;i++){
		sURL = sURL+"/"+oURL[i];
	}
	return sURL;
}


//设为首页
function HomePage(obj,startPage){
	if(startPage==""){
		startPage = "http://"+document.domain;
		var sPort = location.port;
		if(sPort!=""){
			startPage = startPage+":"+sPort
		}
		startPage = startPage+getURL();
	}
	
	if(ie){
		obj.style.behavior="url(#default#homepage)";
		obj.setHomePage(startPage);
	}else if(window.sidebar){
		if(window.netscape){
			try{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}catch(e){
				alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入about:config,然后将项 signed.applets.codebase_principal_support 值改为true" );
			}
		}
		var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref("browser.startup.homepage",startPage);
	}
}


//加入收藏夹
function favorite(_title,_page){
	if(_page==""){
		_page = "http://"+document.domain;
		var sPort = location.port;
		if(sPort!=""){
			_page = _page+":"+sPort
		}
		_page = _page+getURL();
	}
	
	if(ie){
		window.external.AddFavorite(_page,_title);
	}else if(window.sidebar){
		window.sidebar.addPanel(_title,_page,"");
	}
}


//打开窗口(位于屏幕中间)
function winOpen(url,width,height){
	return window.open(url,"","width="+width+",height="+height+",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no,top="+(window.screen.height-height-40)/2+",left="+(window.screen.width-width)/2);
}


//复制文本
function copyText(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)
			return;
		var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
		if(!trans)
			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);
	}
}


//获取浏览器的(cw)窗体宽度、(ch)窗体高度、(nw)内容宽度、(nh)内容高度、(sw)滚动宽度、(sh)滚动高度
function getPixel(o){
	var iPixel = 0;
	
	var doc = document;
	var docb = doc.body;
	var docd = doc.documentElement;
	
	switch(o){
		case "cw" :
			iPixel = docd.clientWidth?docd.clientWidth:docb.clientWidth;
			break;
		case "ch" :
			iPixel = docd.clientHeight?docd.clientHeight:docb.clientHeight;
			break;
		case "nw" :
			iPixel = docb.offsetWidth;
			break;
		case "nh" :
			iPixel = docb.offsetHeight;
			break;
		case "sw" :
			iPixel = docb.scrollLeft?docb.scrollLeft:docd.scrollLeft;
			break;
		case "sh" :
			iPixel = docb.scrollTop?docb.scrollTop:docd.scrollTop;
			break;
	}
	return iPixel;
}


//去掉HTML标记
function replaceHTML(sStr){
	var reg = /<(.[^>]*)>/g;
	var s_str = sStr.toLowerCase();
	s_str = s_str.replace(reg,"");
	return s_str;
}


//无提示关闭窗口
function closeWin(){
	window.opener = null;
	window.open("","_self");
	window.close();
}


//打开时间对话框
function SetTimeValue(Width,Height,WindowObj,SetObj){
	var Url = "/YM_Include/SelectDate.html";
	var time = showModalDialog(Url,WindowObj,'dialogWidth:'+Width+'pt;dialogHeight:'+Height+'pt;status:no;help:no;scroll:yes;');
	if(time != '007007007007')
		SetObj.value = time;
	return time;
}


//倒计时
function Countdown(Year,Month,Day,Hour,Minute,Second,CountdownID){
	var now = new Date();
	var thetime = new Date(Year,Month,Day,Hour,Minute,Second);
	
	var total = (thetime.getTime()-now.getTime())/1000;
	var temp = total/(24*60*60);
	var day = Math.floor(temp);
	temp = ((temp-day)*24*60*60)/(60*60);
	var hour = Math.floor(temp);
	temp = ((temp-hour)*60*60)/60;
	var minute = Math.floor(temp);
	temp = (temp-minute)*60;
	var second = Math.floor(temp);
	
	var innerStr = day + "天" + hour + "时" + minute + "分" + second + "秒";
	if(day<1){
		innerStr = innerStr.replace(day+"天","");
	}
	
	var _$ = document.getElementById(CountdownID);
	_$.innerHTML = innerStr;
	
	window.setTimeout("Countdown("+Year+","+Month+","+Day+","+Hour+","+Minute+","+Second+",'"+CountdownID+"');",1000);
}
