//公共函数
function $(obj){return document.getElementById(obj)}
var usernav=new function(){
	this.isOpera=(window.opera&&navigator.userAgent.match(/opera/gi))?true:false;
	this.isIE=(!this.isOpera&&document.all&&navigator.userAgent.match(/msie/gi))?true:false;
	this.isSafari=(!this.isIE&&navigator.userAgent.match(/safari/gi))?true:false;
	this.isGecko=(!this.isIE&&navigator.userAgent.match(/gecko/gi))?true:false;
	this.isFirefox=(!this.isIE&&navigator.userAgent.match(/firefox/gi))?true:false;
}
function DateTime2Str(date){
	var y=date.getFullYear()+"";
	var m=(date.getMonth()+1)+"";
	var d=date.getDate()+"";
	if(m.length==1)m="0"+m;
	if(d.length==1)d="0"+d;
	return y+"-"+m+"-"+d;
}
Date.prototype.addDay = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num));return this;}
Date.prototype.addMonth = function(num){if(!isNaN(num))this.setMonth(this.getMonth() + parseInt(num));return this;}
if(typeof(Number.prototype.toFixed)!="function"){
    Number.prototype.toFixed = function(d){
        var s=this+"";if(!d)d=0;
        if(s.indexOf(".")==-1)s+=".";s+=new Array(d+1).join("0");
        if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+ (d+1) +"})?)\\d*$").test(s)){
            var s="0"+ RegExp.$2, pm=RegExp.$1, a=RegExp.$3.length, b=true;
            if (a==d+2){a=s.match(/\d/g); if (parseInt(a[a.length-1])>4){
                for(var i=a.length-2; i>=0; i--) {a[i] = parseInt(a[i])+1;
                if(a[i]==10){a[i]=0; b=i!=1;} else break;}
            }
            s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");
        }if(b)s=s.substr(1);return (pm+s).replace(/\.$/, "");} return this+"";
		return d;
    };
}
String.prototype.ellipse = function(maxLength,addstr){
	var len=0;
	for(var i=0; i<this.length; i++){
		var c=this.charCodeAt(i);
		 //半角
		 if(c<256||(c>=0xff61&&c<=0xff9f)){
		   len=len+1;
		 }
		 //全角
		 else{
		   len=len+2;
		 }
		 if(len>maxLength)return this.substr(0,i-addstr.length)+addstr;
	   }		
	return this;
}
function getPosition(obj) {
	var top=0;
	var left=0;
	var width=obj.offsetWidth;
	var height=obj.offsetHeight;
	while (obj.offsetParent) {
		top += obj.offsetTop;
		left += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return {"top":top,"left":left,"width":width,"height":height};	
}
function GetVal(src,name){
	var len=name.length;
	var from=src.indexOf(name);
	if(from==-1){return '';}
	to=src.indexOf('&',from);
	if(to==-1){return src.substring(from+len);}
	else{return src.substring(from+len,to);}
}
function getCookie(Name){ 
	var re=new RegExp(Name+"=[^;]+", "i"); 
	if (document.cookie.match(re)){return document.cookie.match(re)[0].split("=")[1];}
	return "";
}

function setCookie(name,value,expiry,path,domain,secure){
	var nameString = name + "=" + value;
	var expiryString = (expiry == null) ? "" : " ;expires = "+ expiry.toGMTString();
	var pathString = (path == null) ? "" : " ;path = "+ path;
	var domainString = (domain == null) ? "" : " ;domain = "+ domain;
	var secureString = (secure) ?";secure" :"";
	document.cookie = nameString + expiryString + pathString + domainString + secureString;
}
//自定义函数
//显示环球指数及中国概念股
function show_value(value,w){
	var tt=value.split("|");
	if(w==1){
		document.frm1.globalm.value=tt[0];
		document.frm1.globalp.value=tt[1];
	}
	else if(w==2){
		document.frm1.chinam.value=tt[0];
		document.frm1.chinap.value=tt[1];
	}
	else if(w==3){
		document.Form5.globalm1.value=tt[0];
		document.Form5.globalp1.value=tt[1];
	}
	else if(w==4){
		document.Form5.chinam1.value=tt[0];
	document.Form5.chinap1.value=tt[1];
	}
}
function show_value_new(value,w){
	var tt=value.split("|");
	if(w==1){
		document.getElementById("globalm").value=tt[0];
		document.getElementById("globalp").value=tt[1];
	}
	else if(w==2){
		document.getElementById("chinam").value=tt[0];
		document.getElementById("chinap").value=tt[1];
	}
	else if(w==3){
		document.Form5.globalm1.value=tt[0];
		document.Form5.globalp1.value=tt[1];
	}
	else if(w==4){
		document.Form5.chinam1.value=tt[0];
	document.Form5.chinap1.value=tt[1];
	}
}
//股票查询
function chkstockcode(e){var event=window.event||e;if(e.keyCode==13){goto();}}
function goto(){
	var theform=document.form3;
	var code = theform.stockcode.value;
	var type = theform.stocktype.value;
	if(code!=""){
		switch(type){
			case "1":
			window.open('/stock/search.shtml?stock='+code);	
			break;
		case "2":
			window.open('http://share.jrj.com.cn/cominfo/default_'+code+'.htm');	
			break;
		default:
			alert("参数错误!");
			break;	
	  }
	}
	else{alert("请输入股票代码!");theform.stockcode.focus()}
	return false;
}	
//   year       年     
//   month      月
//   week       周     
//   day        星期(0-6,   0代表周日)       
function dateFromWeek(year,  month, week,   day){     

	  var   date1   =   new   Date(year,   month-1,   1);     
	  var   dayMS   =   24*60*60*1000;     
	  var   firstDay   =   (7-date1.getDay())*dayMS;     
	  var   weekMS   =   (week-1)*7*dayMS;     
	  var   result   =   date1.getTime()+firstDay+weekMS+day*dayMS;     
	  date1.setTime(result);     
	 return   date1;     
} 
//格式化日期
function format(date , formatStr){ 
	var str = formatStr; 
	str=str.replace(/yyyy|YYYY/,date.getFullYear()); 
	str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():"0" + (date.getYear() % 100)); 
	str=str.replace(/MM/,date.getMonth()>8?(date.getMonth()+1).toString():"0" + (date.getMonth()+1)); 
	str=str.replace(/M/g,date.getMonth()+1); 
	str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():"0" + date.getDate()); 
	str=str.replace(/d|D/g,date.getDate()); 
	str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():"0" + date.getHours()); 
	str=str.replace(/h|H/g,date.getHours()); 
	str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():"0" + date.getMinutes()); 
	str=str.replace(/m/g,date.getMinutes()); 
	str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():"0" + date.getSeconds()); 
	str=str.replace(/s|S/g,date.getSeconds()); 
	return str; 
}
//显示时间脚本
function Time(){
 var Timer=new Date();
 var days=Timer.getDate();
 var daymonth=Timer.getMonth();
 var hours=Timer.getUTCHours();
 var minutes=Timer.getUTCMinutes();
 var seconds=Timer.getUTCSeconds();
 if (minutes<=9)minutes="0"+minutes;
 if (seconds<=9)seconds="0"+seconds;

 var worldtime = new Array()
 worldtime[0]=8
 worldtime[1]=-5
 worldtime[2]=-6
 worldtime[3]=-7
 //美国夏时:从3月的第二个星期天开始，到11月的第一个星期天结束
 var startdate=parseInt(format(dateFromWeek(Timer.getYear(),3, 2 , 0),"yyyyMMdd"));
 var enddate=parseInt(format(dateFromWeek(Timer.getYear(),11, 1 , 0),"yyyyMMdd"));
 var today=parseInt(format(new Date(),"yyyyMMdd"));
 if(today>=startdate && today<=enddate){
	 worldtime[1]=-4;
 }

 var BJhours = eval(worldtime[0]+hours)
 var BJ_DayName = "今天";
 if (BJhours <0) {BJhours=24+BJhours;BJ_DayName = "昨天";}
 if (BJhours >=24) {BJhours=BJhours-24;BJ_DayName = "明天";}
 var myclockBJ="北京时间　"+(daymonth+1)+"月"+days+"日"+" "+BJhours+":"+minutes+":" +seconds;

 var Ehours = eval(worldtime[1]+hours);
 var E_DayName = "今天";
 if (Ehours <0) {Ehours=24+Ehours;E_DayName = "昨天";}
 if (Ehours >=24) {Ehours=Ehours-24;E_DayName = "明天";}
 
 /*美国东部时间*/
 var date = new  Date(new Date().getTime()-1000*60*60*(8-worldtime[1])); 
 myclockUE=	"美国东部时间　"+(date.getMonth()+1)+"月"+date.getDate()+"日"+" "+(date.getHours()<10?"0":"")+date.getHours()+":"+(date.getMinutes()<10?"0":"")+date.getMinutes()+":"+(date.getSeconds()<10?"0":"")+date.getSeconds();
 
 $("bjtime").innerHTML=myclockBJ;
 $("usetime").innerHTML=myclockUE;
 setTimeout("Time()",1000);
}
function StockSearch(){
	var code=GetVal(location.href,"stock=");
	if(code==""){return;}
	var usstock=code;
	var cookie=getCookie("usstock");
	cookie=cookie.replace(code,"");
	if(cookie!=""){usstock=cookie+"|"+usstock;}
	var expdate = new Date().addMonth(1);
	setCookie("usstock",usstock,expdate,"/");
	if($("link1")){$("link1").href="http://quote123.com/aspnet/usmkt/zacks/research.aspx?Symbol="+code;}
	if($("link2")){$("link2").href="http://quote123.com/aspnet/usmkt/zacks/financials.aspx?symbol="+code;}
	if($("link3")){$("link3").href="http://quote123.com/aspnet/usmkt/zacks/fabs.aspx?symbol="+code;}
	if($("link4")){$("link4").href="http://quote123.com/aspnet/usmkt/zacks/fais.aspx?symbol="+code;}
	if($("link5")){$("link5").href="http://quote123.com/aspnet/usmkt/zacks/est_rec.aspx?symbol="+code;}	
	if($("link6")){$("link6").href="http://isou.jrj.com.cn/search.jspa?q="+code+"&f=1";}			
	if($("iframe1")	){$("iframe1").src="http://jrj.quote123.com/jrj/usmkt/quote/quote.aspx?Symbol="+code;}	
}
function AddToCookie(){
	var arr=location.href.split("/");
	var code="";
	if(arr.length==5){		
		if(arr[4].indexOf(".shtml")!=-1){code=arr[4].split(".shtml")[0];}
		if(code=="search"){code=GetVal(location.href,"stock=");}
		if(code=="" || code=="index"){code="JRJC";}				
		var usstock=code.toUpperCase();
		var cookie=getCookie("usstock");
		var newarr=[];
		if(cookie!=""){
			var arrCookie=cookie.split("|");			
			var maxnum=10;
			var str="";
			newarr.push(usstock);
			for(var i=0;i<arrCookie.length && newarr.length<maxnum;i++){
				if(arrCookie[i]!=""){
					if(arrCookie[i].toUpperCase()!=usstock){
						newarr.push(arrCookie[i].toUpperCase());						
					}
				}
			}			
		}		
		cookie=newarr.join("|");		
		var expdate = new Date().addMonth(1);
        var domain=arr[2].replace("share.","");
		setCookie("usstock",cookie,expdate,"/",domain);		
	}
}
function resizeIframe(obj){
  var bobo=document.getElementById(obj); //iframe id
  if (document.getElementById){
   if (bobo && !window.opera){
    if (bobo.contentDocument && bobo.contentDocument.body.offsetHeight){
     bobo.height = bobo.contentDocument.body.offsetHeight;
    }else if(bobo.Document && bobo.Document.body.scrollHeight){
     bobo.height = bobo.Document.body.scrollHeight;
    }
   }
  }
}
