﻿	function fnPadLeft(target, fill_chr, max_len) {
		var result = "";
		var target = String(target);

		if (target.length < max_len) {
			for (var i = target.length; i < max_len; i++) {
				result += fill_chr;
			}

			result += target;
		} else {
			result = target;
		}

		return result;
	}

	function fnShowHIdeLayer(group, target, tab_cnt, class_name) {
		for (var i = 1; i <= tab_cnt; i++) {
			oLayer = document.getElementById(group + '_' + fnPadLeft(i, '0', 2));
			oTitle = document.getElementById(group + '_title_' + fnPadLeft(i, '0', 2));

			oLayer.style.display = "none";
			oTitle.className = class_name;
		}

		var oTargetLayer = document.getElementById(group + '_' + target);
		var oTargetTitle = document.getElementById(group + '_title_' + target);

		oTargetLayer.style.display = "block";
		oTargetTitle.className = class_name + "_s";
	}

	function fnShowHIdeSideLayer(group, target, tab_cnt) {
		for (var i = 1; i <= tab_cnt; i++) {
			oLayer = document.getElementById(group + '_' + fnPadLeft(i, '0', 2));
			oLayer.style.display = "none";
		}

		var oTargetLayer = document.getElementById(group + '_' + target);
		oTargetLayer.style.display = "block";
	}

	function fnClickNext(group, max) {
		for (var i = 1; i <= max; i++) {
			var oTarget = document.getElementById(group + "_" + i);

			if (oTarget.style.display == "block") {
				oTarget.style.display = "none";

				if (i == max) {
					var oTargetNext = document.getElementById(group + "_1");
				} else {
					var oTargetNext = document.getElementById(group + "_" + (i + 1));
				}

				oTargetNext.style.display = "block";

				return;
			}
		}
	}

	function fnClickPrev(group, max) {
		for (var i = 1; i <= max; i++) {
			var oTarget = document.getElementById(group + "_" + i);

			if (oTarget.style.display == "block") {
				oTarget.style.display = "none";

				if (i == 1) {
					var oTargetPrev = document.getElementById(group + "_" + max);
				} else {
					var oTargetPrev = document.getElementById(group + "_" + (i - 1));
				}

				oTargetPrev.style.display = "block";

				return;
			}
		}
	}

	function fnResizeiFrame(obj){
		var innerBody = obj.contentWindow.document.body;
		var innerHeight = innerBody.scrollHeight + (innerBody.offsetHeight - innerBody.clientHeight);

		if (innerHeight <= 0) {
			innerHeight = 900;
		}

		obj.style.height = innerHeight + "px";


//		obj = typeof obj == 'string' ? document.getElementById(obj) : obj;
//		obj.setExpression('height',Content.document.body.scrollHeight);
	}
	String.prototype.trim = function() {
		return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	String.prototype.replaceAll = function(search, replace){
		var regex = new RegExp(search, "g");
		return this.replace(regex, replace);
	}
	Array.prototype.inArray = function (value){
	  for (var i=0; i < this.length; i++){
	      if (this[i] === value){
	          return true;
	      }
	  }
	  return false;
	};
	Array.prototype.arrayUnique = function (value){
		return jQuery.unique(this);
	}
	function popup(URL, width, height ,name ) {
		if( width == 0 ) width = 450;
		if( height == 0 ) height = 500;
		var left=(window.screen.width-width)/2;
		var top=(window.screen.height-height)/2;
		var id="pop_wins";
		var params="toolbar=0,scroll=0,location=0,statusbar=0,menubar=0,resizable=0";
		var win=null;
		URL+="?Rnd="+Math.random();
		if (document.all){
			var isIE6=!window.XMLHttpRequest;
			if (isIE6) height+=50;
			params=params.replaceAll(",",";");
			params+=";dialogLeft="+left+"px;dialogTop="+top+"px;dialogWidth="+width+"px;dialogHeight="+height+"px";
			window.showModalDialog(URL,window,params);
		}
		else{
			params+=",dialog=yes,modal=yes,left="+left+"px,top="+top+"px,width="+width+"px,height="+height+"px";
			win=window.open(URL,name,params);
			win.focus();
		}
	}
	function _close_win(){
		if(document.all){
			window.opener=null;
		}
		else{
			window.open('','_parent','');
		}
		window.close();
	}
	function _get_opener(){
		var opener_win=null;
		if (document.all){
			opener_win=window.dialogArguments;
		}
		else{
			opener_win=window.opener;
		}
		return opener_win;
	}	
	function _par_refresh(vrl){
		try {
			var par_win=_get_opener();
			if (vrl==""){
				par_win.location.reload();
			}
			else{
				par_win.location=vrl;
			}
		}
		catch(err){
				
		}
	}
	function popup2(URL, width, height ,name ) {
		if( width == 0 ) width = 450;
		if( height == 0 ) height = 500;
		var left=(window.screen.width-width)/2;
		var top=(window.screen.height-height)/2;
		var id="pop_wins";
		var params="toolbar=0,scroll=0,location=0,statusbar=0,menubar=0,resizable=0";
		var win=null;
		params+=",dialog=yes,modal=yes,left="+left+"px,top="+top+"px,width="+width+"px,height="+height+"px";
		win=window.open(URL,name,params);
		win.focus();
	}

	function fnCleanObj(obj,str) {
		if (obj.value == str) {
			obj.value = "";
		}
	}
	function fnInitObj(obj,str) {
		if (obj.value.length == 0) {
			obj.value = str;
		}
	}

	function fnCleanObjPw(obj,str) {
		if (obj.value == str) {
			if (obj.type == "text") {
				obj.style.display = "none"
				target = document.getElementById(obj.id.substring(0, obj.id.length - 4));
				target.style.display = "block";
				target.focus();
			}
		}
	}
	function fnInitObjPw(obj,str) {
		if (obj.value.length == 0) {
			if (obj.type == "password") {
				obj.style.display = "none"
				target = document.getElementById(obj.id + "_txt");
				target.style.display = "block";
			}
		}
	}

	function fnSetInvalid(obj,str){
		if (obj==null){return;}
		obj.disabled=false;
		obj.style.background="red";
		obj.style.color="black";
		//obj.select();
		//obj.focus();
		fnSetMessage(obj.id+"_message","invalid_message",str);
		fnSetFlag("chk_"+obj.id,0);
	}
	function fnSetValid(obj,str){
		if (obj==null){return;}
		obj.disabled=false;
		obj.style.background="";
		obj.style.color="gray";
		fnSetMessage(obj.id+"_message","valid_message",str);
		fnSetFlag("chk_"+obj.id,1);
	}
	function fnSetProcing(obj,str){
		if (obj==null){return;}
		obj.disabled=true;
		fnSetFlag("chk_"+obj.id,0);
		fnSetMessage(obj.id+"_message","procing_message",str);
	}
	function fnSetError(obj,str,func){
		if (obj==null){return;}
		obj.disabled=false;
		str+="&nbsp;<a href=\"javascript:fnChkAgain('"+func+"','"+obj.id+"')\"><b>重试</b></a>";
		fnSetMessage(obj.id+"_message","procing_message",str);
		fnSetFlag("chk_"+obj.id,0);
	}
	function fnChkAgain(func,obj_id){
		var obj=document.getElementById(obj_id);
		if (obj==null){return;}
		fnSetFlag("chk_"+obj.id,0);
		eval(func+"($('#"+obj_id+"')[0]);");
	}
	function fnSetMessage(name,class_name,message){
		var message_obj=document.getElementById(name);
		if (message_obj){
			message_obj.className=class_name;
			if (message.trim()=="") {message="&nbsp;";}
			message_obj.innerHTML=message;
		}
	}
	function fnSetFlag(id,valve){
		if ($("#"+id)[0]!=null){
			$("#"+id)[0].value=valve;
		}
	}
	function fnClubJoin(id,login_url,join_url,width,height,name){
		if (id<=0){
			window.location=login_url;
		}
		else{
			popup(join_url,width,height,name);
		}
	}
	function fnResizeFont(x){
		$("#IDeditor").removeClass();
    		$("#ID_size_control").removeClass();
    		$("#ID_lv1").removeClass();
    		$("#ID_lv2").removeClass();
    		$("#ID_lv3").removeClass();
    		switch (x){
        		case 1:
        			$("#IDeditor").addClass("editor_area editor_S");
        			$("#ID_size_control").addClass("size_control");
        			$("#ID_lv1").addClass("on");
        			break;
        		case 2:
        			$("#IDeditor").addClass("editor_area editor_M");
        			$("#ID_size_control").addClass("size_control");
        			$("#ID_lv2").addClass("on");
        			break;
        		case 3:
        			$("#IDeditor").addClass("editor_area editor_L");
        			$("#ID_size_control").addClass("size_control");
        			$("#ID_lv3").addClass("on");
        			break;
        }
	}

	function getCookie(name) {
		var nameOfCookie = name + "=";
		var x = 0;
		while ( x <= document.cookie.length ) {
			var y = (x+nameOfCookie.length);

			if ( document.cookie.substring( x, y ) == nameOfCookie ) {
				if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
					endOfCookie = document.cookie.length;

				return unescape( document.cookie.substring( y, endOfCookie ) );
			}

			x = document.cookie.indexOf( " ", x ) + 1;
			if ( x == 0 )
				break;
		}
		return "";
	}

	function setCookie(name, value, expiredays) {
		var todayDate = new Date();
		todayDate.setDate( todayDate.getDate() + expiredays );
		document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
	}

	function fnEnterKeyCode(e, func) {
	    if (typeof(e) != "undefined") {
			if (e.keyCode == 13) {
				eval(func);
			}
	    else
			if (event.keyCode == 13) {
				eval(func);
			}
		}
	}

	function fnAdsPopup(code, width, height) {
		var url = "http://www.cctvfinance.com/cctv/ads_popup/main_popup" + code;
		popup2(url, width, height, "popAds");
	}

	// enabled innerText on firefox
	if (typeof HTMLElement != "undefined" && typeof HTMLElement.prototype.__defineGetter__ != "undefined") {
		HTMLElement.prototype.__defineGetter__("innerText", function() { return this.textContent; });
		HTMLElement.prototype.__defineSetter__("innerText", function(txt) { this.textContent = txt; });
	}

	
	function redirectBuycm()
	{
		if (confirm('直播节目为收费服务，您可以购买“视频卡”或者“视频VIP卡”进行观看。'))
		{
			location.href = "/cctv/my_pay/buy_cm";
		}
	}