function getStrActualLen(sChars)
{
    return sChars.replace(/[^\x00-\xff]/g,"xx").length;
}

// 截取固定长度子字符串 sSource为字符串iLen为长度
function getInterceptedStr(sSource, iLen)
{
    if(sSource.replace(/[^\x00-\xff]/g,"xx").length <= iLen)
    {
        return sSource;
    }

    var str = "";
    var l = 0;
    var schar;
    for(var i=0; schar=sSource.charAt(i); i++)
    {
        str += schar;
        l += (schar.match(/[^\x00-\xff]/) != null ? 2 : 1);
        if(l >= iLen)
        {
            break;
        }
    }

    return str;
}


function setCookie(name, value, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds*1000 );		//失效时间（单位：秒）
	var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function deleteCookie(name, path, domain) {
	if (getCookie(name))
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
		window.location=""+movieurl+"" ;

}

function getCookie(name) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf(prefix);
	if (cookieStartIndex == -1)	return null;
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex != -1)
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
	else
		return unescape(document.cookie.substring(cookieStartIndex + prefix.length));
}


/*******************************点播页使用方法*******************************/
function getData(name) {
	var temp = getCookie(name);
	;
	if (temp == "" || temp == null) return "";
	var temparray = temp.split('$');
	if (temparray.length<1) return "";
	return temparray;
}

function setUlooked() {
	
	var str = movieurl.replace('#','') + "|" + title;			// 用|号间隔开articleid和标题
	var temp = getCookie("Ulooked");
	if (temp == undefined || temp == "") temp = "";
	if (temp.indexOf(str) != -1) return;	// 已存在cookies里则不添加
	if (temp != "")
		str += "$";						// 用$号间隔开多条记录
	setCookie("Ulooked",str+temp,8640000,"\/");// 失效时间100天
}

function showUlooked(objid){
	var obj = document.getElementById(objid);
	
	var temparray = getData("Ulooked");
	var temparray2;
	if (temparray == "" || temparray == undefined) return;
	obj.innerHTML = "";
	for (var i=0;i<temparray.length;i++) {
		if (i>=10) break;				// 最多显示十条
		temparray2 = temparray[i].split('|');
		if (temparray2.length<2) continue;
		if (getStrActualLen(temparray2[1])>25) {
		obj.innerHTML += "<A href="+ temparray2[0] +">" + getInterceptedStr(temparray2[1], 13) + "...</A>";
}else{
		obj.innerHTML += "<A href="+ temparray2[0] +">" + temparray2[1] + "</A>";
}
	}
}

function setCollection() {
	var str = movieurl.replace('#','') + "|" + title;		// 用|号间隔开articleid和标题
	var temp = getCookie("UserCollection");
	if (temp == undefined || temp == "") temp = "";
	if (temp.indexOf(str) == -1) {		// 不存在cookies里时才添加
		if (temp != "") str += "$";	// 用$号间隔开多条记录
		setCookie("UserCollection",str+temp,8640000,"\/");// 失效时间100天
	}
	alert('《' + title + '》已成功添加到您收藏的节目列表！');
	window.location=""+movieurl+"" ;
}

function showCollection(objid) {
	var obj = document.getElementById(objid);
	var temparray = getData("UserCollection");
	var temparray2;
	if (temparray == "" || temparray == undefined) return;
	obj.innerHTML = "";
	for (var i=0;i<temparray.length;i++) {
		if (i>=10) break;				// 最多显示十条
		temparray2 = temparray[i].split('|');
		if (temparray2.length<2) continue;
		if (getStrActualLen(temparray2[1])>25) {
		obj.innerHTML += "<LI><A href="+ temparray2[0] +">" + getInterceptedStr(temparray2[1], 13) + "...</A></LI>";
}else{
		obj.innerHTML += "<LI><A href="+ temparray2[0] +">" + temparray2[1] + "</A></LI>";
}
	}
}

