//根据cookie名查找cookie
function _getCookieVal(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var offset = i + alen;
		if (document.cookie.substring(i, offset) == arg) {
			var endstr = document.cookie.indexOf(";",  offset);
			endstr = endstr == -1 ? document.cookie.length : endstr;
			return unescape(document.cookie.substring(offset,  endstr));
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

//设置cookie
function _setCookie(name, value, expires, secure) {
	if(!expires) {
		expires = new Date();
		expires.setTime(expires.getTime() + 30 *24 * 60 * 60 * 1000);
	}
	var path = "/";
	var domain = document.domain;
        domain="jrj.com.cn";
	secure = secure ? secure : false;
	var cookie = name + "=" + escape(value) 
					+ ((expires) ? "; expires=" + expires.toGMTString() : "")
					+ ((path) ? "; path=" + path : "/")
					+ ((domain) ? "; domain=" + domain : "")
					+ ((secure) ? "; secure" : "");
	document.cookie = cookie;
}

var LASTEST_SHARE_COOKIE = "JRJ_LASTEST_FUND_COOKIE"; //最近访问的股票的cookie名
var LASTEST_SHARE_MAXCNT = 10; //记录股票的最大数量
var LASTEST_SHARE_SPLITCHAR = ","; //最近访问的股票的分隔符
/**
 *添加最近访问的股票
 */
function _addLastestShareToCookie(code) {
	if (_getFundInfo(code)==null) {
		return;
	}
	var name = LASTEST_SHARE_COOKIE;	
	var oldVal = _getCookieVal(name);
	var newVal = "";

	if(!oldVal) {
		newVal = code;
	}else {		
		var arrays = oldVal.split(LASTEST_SHARE_SPLITCHAR);
		var isExist = false;
		for(var i = 0; i < arrays.length; i++) {
			if(arrays[i] == code) {
				for(var j = i; j > 0;) { //命中的排到最前面
					arrays[j] = arrays[--j];
				}
				arrays[0] = code;
				isExist = true;
				break;
			}
		}
		var start = 0;
		var end = 0;
		//取出最大数量的关键词
		if(isExist) { 
			end = arrays.length < LASTEST_SHARE_MAXCNT ? arrays.length : LASTEST_SHARE_MAXCNT;
		}else {
			newVal = code + LASTEST_SHARE_SPLITCHAR;
			end = arrays.length < LASTEST_SHARE_MAXCNT ? arrays.length : (LASTEST_SHARE_MAXCNT - 1);
		}
		for(var i = start; i < end; i++) {
			newVal += arrays[i];
			if(i < end - 1)
				newVal += LASTEST_SHARE_SPLITCHAR;
		}
	}
	_setCookie(name, newVal);
}

/**
 * 从cookie中得到最近访问的股票
 */
function _getLastestShareFromCookie(exceptCode, defaultArrays) {
	var name = LASTEST_SHARE_COOKIE;
	var cookieVal = _getCookieVal(name);
	cookieVal = cookieVal == null ? "" : cookieVal;
	var arrays = cookieVal.split(LASTEST_SHARE_SPLITCHAR);
	var max = 10;
	var returnValue = new Array();
	for(var i = 0, cnt = 0; cnt < max && i < arrays.length; i++) {
	//	if(arrays[i] != exceptCode) {
			returnValue[cnt++] = arrays[i];
	//	}
	}

	if(returnValue.length == 0) {
		for(var i = returnValue.length, j = 0; i < max && j < defaultArrays.length; j++) {
			var code = defaultArrays[j];
			if(exceptCode == defaultArrays[j] || cookieVal.indexOf(code) != -1) 
				continue;
			returnValue[i++] = defaultArrays[j];
		}
	}
	return returnValue;
}

/**
 *将数字四舍五入
 */
function _digitRound(Dight,How) {
	Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);
	return Dight;
}

// 对Date的扩展，将 Date 转化为指定格式的String 
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 
Date.prototype.format = function(fmt) { //author: meizz 
	var o = { 
		"M+" : this.getMonth()+1, //月份 
		"d+" : this.getDate(), //日 
		"h+" : this.getHours(), //小时 
		"m+" : this.getMinutes(), //分 
		"s+" : this.getSeconds(), //秒 
		"q+" : Math.floor((this.getMonth()+3)/3), //季度 
		"S" : this.getMilliseconds() //毫秒 
	}; 
	if(/(y+)/.test(fmt)) 
		fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	for(var k in o) 
		if(new RegExp("("+ k +")").test(fmt)) 
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); 
	return fmt; 
} 

/**
 * 得到股票数据请求的url
 */
function _getShareDataUrl(type, code, market) {
	if(type == "hq") //股票的行情数据
		return "/ajax/sharedata/html/" + code + ".js";
		//return "http://jshq.jrj.com.cn/html/" + code + ".js";
	else if(type == "mx") //股票的明细数据
		return "/ajax/sharedata/html/" + code + ".js";
		//return "http://jshq.jrj.com.cn/html/" + code + ".js";
	else if(type == "dd") //股票的大单数据
		//return "/ajax/sharedata/html/dd_" + code + ".js";
		return "http://jshq.jrj.com.cn/html/dd_" + code + ".js";
	else if(type == "fj") //股票的分价数据
		//return "/ajax/sharedata/html/fj_" + code + ".js";
		return "http://jshq.jrj.com.cn/html/fj_" + code + ".js";
	else if(type == "fs") //股票的分时数据
		//return "/ajax/sharedata/html/fs_" + code + ".js";
		return "http://jshq.jrj.com.cn/html/fs_" + code + ".js";
	else 
		return null;
}

/**
 * 得到大盘指数的url
 */
function _getMarketDataUrl(code) {
	return "/ajax/quote/htmdata/html/" + code + ".htm";
	//return "http://quote.jrj.com.cn/htmdata/html/" + code + ".htm";
}

/**
 * 得到用户的自选股的url
 */
function _getUserSharesUrl(userid) {
	return "/ajax/myjrj/DataForStock_" + userid + "_1.htm";
	//return "http://myjrj.jrj.com.cn/DataForStock.aspx?userid=" + userid + "&type=1";
}

/**
 * 用户添加自选股的url
 */
function _getAddUserShareUrl(userid, code) {
	return "/ajax/myjrj/addstock_" + userid + "_" + code + ".htm";
	//return "http://myjrj.jrj.com.cn/mystock/addstock.aspx?userid=" + userid + "&stockcode=" + code;
}

function _getGYDCUrl() {
	return "/ajax/d_share/vote.htm";
	//return "http://research.jrj.com.cn/ajax/vote.jspa";
	//"http://d_share.jrj.com.cn/ajax/vote.jspa";
	//return "http://localhost/ress/ajax/vote.jspa";
}

function _getGYYCUrl() {
	//return "http://research.jrj.com.cn/ajax/forecast.jspa";
	return "/ajax/d_share/forecast.htm";
	//return "http://d_share.jrj.com.cn/ajax/forecast.jspa";
	//return "http://localhost/ress/ajax/forecast.jspa";
}

/**
 * 如果不存在id，创建一个对象(id)，并追加到p中
 */
function _createAndAppendElement(p, id, type) {
	var o = $(id);
	if(o == null) {
		o = document.createElement(type);
		o.id = id;
		p.appendChild(o);
	}
	return o;
}

/**
 * 判断股票是否有效
 */
function _isActiveShare(share) {
	if(share == null) 
		return false;
	var status = share.status;
	return status == 1 || status == 4;
}

/**
 * status:状态 0=未上市,1=在市, 2=终止上市, 3=暂停上市, 4=换股上市,5=未知
 */
function _getShareStatusStr(share) {
	if(share == null) 
		return "未知";
	var status = share.status;
	if(status == 0)
		return "未上市";
	else if(status == 1)
		return "在市";
	else if(status == 2)
		return "终止";
	else if(status == 3)
		return "暂停";
	else if(status == 4)
		return "换股";
	else
		return "未知";
}

/**
 *取子数组
 */
Array.prototype.subArray =function(start, end) {
	if(this.length == 0 || start >= this.length ||start >= end)
		return null;
	
	end = end < this.length ? end : this.length;
	var array = this;
	var subs = new Array();
	for(var i = start; i < end; i++) {
		subs[subs.length] = array[i];
	}
	return subs;
}

/**
 *数组倒序
 */
Array.prototype.reverse =function() {
	if(this.length == 0)
		return null;
		 
	var returnValue = new Array();
	var i = 0;
	var j = this.length - 1;
	for(; i < j; i++, j--) {
		var tmp = this[i];
		this[i] = this[j];
		this[j] = tmp;
	}
}


String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}


/**
 *加载
 */
function _loadLightBox() {
	var obj = "<a id=\"gydc_tu_img\" href=\"/stocks/" + _share_info.code + "/d/gydctj.png\" rel=\"lightbox\"> </a>";
	//zy add
	obj+= "<a id=\"cp_tu_img\" href=\"/stocks/" + _share_info.code + "/d/zysrgc_cp2.png\" rel=\"lightbox\"> </a>";
	obj+= "<a id=\"dq_tu_img\" href=\"/stocks/" + _share_info.code + "/d/zysrgc_dq2.png\" rel=\"lightbox\"> </a>";
	obj+= "<a id=\"hy_tu_img\" href=\"/stocks/" + _share_info.code + "/d/zysrgc_hy2.png\" rel=\"lightbox\"> </a>";
	document.write(obj);
}


function _showpicture(dname) {
	var img = $(dname.toString());
	var ima_url="/stocks/includes/showpic.html?str="+img.toString();
	window.open(ima_url,"图片",'height=500,width=500,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
}

function openwin(url) {
	window.open(url);
}
