/**----前台模板里必须调用的通用JS文件--**/


var appurl='/';        //程序安装目录,请正确填写,否则可能导到JS不能用,如是网站有启用于级域名，请填写主站域名
//===========================Ajax标签结束开始===============================================
$(document).ready(function(){
						   
	jQuery("span[type=ajax]").each(function(){
		     var idarr=jQuery(this).attr("id").split('_');
			 var labelid=idarr[0].replace("ks","").replace("SQL","");
			 var typeid=idarr[1];   //sql标签时为用户的参数
			 var channelid=idarr[2];
			 var classid=idarr[3];
			 var infoid=idarr[4];
		    if (jQuery(this).attr("id").substring(0,2)=="ks"){
			  getlabelcontent("plus/ajax.aspx",jQuery(this)[0],labelid,"Label",typeid,channelid,classid,infoid)
		    }else if (jQuery(this).attr("id").substring(0,3)=="SQL") {
			getlabelcontent("plus/ajax.aspx",jQuery(this)[0],labelid,"SQL",typeid,channelid,classid,infoid);   
		   }
	});
	
	jQuery("*[linkref]").click(function(){
			var linkid=$(this).attr("linkref");
			var url=appurl+"plus/Ajaxs.ashx?a=linkhits&linkid="+linkid+"&anticache=" + Math.floor(Math.random()*1000);
			jQuery.ajax({type:"get",async:false,url:url,cache:false,success:function(d){ }});
	});
	
});
function getlabelcontent(posturl,obj,labelid,action,typeid,channelid,classid,infoid){  
 jQuery.get(appurl+posturl,{action:action,labelid:escape(labelid),typeid:typeid,channelid:channelid,classid:classid,infoid:infoid},function(data){obj.innerHTML=data;});
}
//========================Ajax标签结束==================================================

 //检查是否中文字符
is_zw=function(str)
{
	exp=/[0-9a-zA-Z_.,#@!$%^&*()-+=|\?/<>]/g;
	if(str.search(exp) != -1){return false;}
	return true;
}

// 检查是否有效的扩展名
IsExt=function(FileName, AllowExt){
		var sTemp;
		var s=AllowExt.toUpperCase().split("|");
		for (var i=0;i<s.length ;i++ ){
			sTemp=FileName.substr(FileName.length-s[i].length-1);
			sTemp=sTemp.toUpperCase();
			s[i]="."+s[i];
			if (s[i]==sTemp){
				return true;
				break;
			}
		}
		return false;
}
//检查是否数字方法一
is_number=function(str)
{
	exp=/[^0-9()-]/g;
	if(str.search(exp) != -1)
	{
		return false;
	}
	return true;
}
//检查数字方法二
function CheckNumber(Obj,DescriptionStr)
{
	if (Obj.value!='' && (isNaN(Obj.value) || Obj.value<0))
	{
		alert(DescriptionStr+"应填有效数字！");
		//Obj.value="";
		Obj.focus();
		return false;
	}
	return true;
}
//检查电子邮件有效性
is_email=function(str)
{
	if((str.indexOf("@")==-1)||(str.indexOf(".")==-1)){return false;}
	return true;
}
//检查是否手机号
function ismobile(s){
   var p = /^(013|015|13|15|018|18)\d{9}$/;
   if(s.match(p) != null){
  return true;
  }
  return false;
}
OpenWindow=function(Url,Width,Height,WindowObj)
{
	var ReturnStr=showModalDialog(Url,WindowObj,'dialogWidth:'+Width+'pt;dialogHeight:'+Height+'pt;status:no;help:no;scroll:no;status:0;help:0;scroll:0;');
	return ReturnStr;
}

function OpenThenSetValue(Url,Width,Height,WindowObj,SetObj)
{
	var ReturnStr=showModalDialog(Url,WindowObj,'dialogWidth:'+Width+'pt;dialogHeight:'+Height+'pt;status:no;help:no;scroll:no;status:0;help:0;scroll:0;');
	if (ReturnStr!='' && ReturnStr!=undefined ) SetObj.value=ReturnStr;
	return ReturnStr;
}

//后台统一调用选择文件FieldID 返回的text ID号,ChannelID模型ID
function SelectFiles(FieldID,ChannelID,pic){
	if (document.all){
	var str=OpenThenSetValue('../Include/SelectFile.aspx?channelid='+ChannelID,550,350,window,jQuery('#'+FieldID)[0]);
	if (pic!=''&& pic!=undefined){jQuery("#"+pic).attr("src",str);}
	}else{
	    parent.openWin('选择文件','Include/SelectFile.aspx?fieldID='+FieldID+"&channelid="+ChannelID,false,800,480);
	}
}
//后台选择模板
var dialogType=0;
function SelectTemplate(FieldID){
	if (dialogType==1){
	OpenThenSetValue('../include/KS.SelectTemplate.aspx',500,350,window,jQuery('#'+FieldID)[0]);
	}else{
	parent.openWin('选择模板','include/KS.SelectTemplate.aspx?fieldID='+FieldID,false,750,480);
	}
}


CheckEnglishStr=function(Obj,DescriptionStr)
{
	var TempStr=Obj.value,i=0,ErrorStr='',CharAscii;
	if (TempStr!='')
	{
		for (i=0;i<TempStr.length;i++)
		{
			CharAscii=TempStr.charCodeAt(i);
			if (CharAscii>=255||CharAscii<=31)
			{
				ErrorStr=ErrorStr+TempStr.charAt(i);
			}
			else
			{
				if (!CheckErrorStr(CharAscii))
				{
					ErrorStr=ErrorStr+TempStr.charAt(i);
				}
			}
		}
		if (ErrorStr!='')
		{
			alert("出错信息:\n\n"+DescriptionStr+'发现非法字符:'+ErrorStr);
			Obj.focus();
			return false;
		}
		if (!(((TempStr.charCodeAt(0)>=48)&&(TempStr.charCodeAt(0)<=57))||((TempStr.charCodeAt(0)>=65)&&(TempStr.charCodeAt(0)<=90))||((TempStr.charCodeAt(0)>=97)&&(TempStr.charCodeAt(0)<=122))))
		{
			alert(DescriptionStr+'首字符只能够为数字或者字母');
			Obj.focus();
			return false;
		}
	}
	return true;
}
CheckErrorStr=function(CharAsciiCode)
{
	var TempArray=new Array(43,46,34,47,92,42,58,60,62,63,124);
	for (var i=0;i<TempArray.length;i++)
	{
		if (CharAsciiCode==TempArray[i]) return false;
	}
	return true;
}

InsertKeyWords=function(obj,KeyWords)
{
	if (KeyWords!='')
	{
		if (obj.value.search(KeyWords)==-1)
		{
			if (obj.value=='') obj.value=KeyWords;
			else obj.value=obj.value+' '+KeyWords;
			
		}
	}
	if (KeyWords == 'Clean')
	{
		obj.value = '';
	}
	return;
}
Getcolor=function(img_val,Url,input_val){
	var arr = showModalDialog(Url, "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
	if (arr != null){
		document.getElementById(input_val).value = arr;
		img_val.style.backgroundColor = arr;
		}
}

CheckBox=function(checked){
		jQuery("input[type=CheckBox]").attr("checked",checked);
}
CheckAll=function(form)
{ 
    for (var i=0;i<form.elements.length;i++)
    {
        var e = form.elements[i];
        if (e.name != 'chkall' && e.disabled==false && e.name.indexOf('$')<0 && e.type=="checkbox")
		{
            e.checked = document.getElementById("chkall").checked;
		}
    }
}
//判断是否有选择复选框的值，参数op：edit修改
GetIDS=function(op)
  {
  var aa=document.getElementsByName("ids");   
  var n=0;
  for (var i=0; i<aa.length;i++)   
    if(aa[i].checked)  n++;   
   if (n==0) 
   {
	KesionJS.Alert('对不起，你没有选择记录!',null);
    return false;
   }
   if(op=='del')
  {
	  return (confirm('确定删除选中的记录吗?'));
  }
  else if (op=='edit' && n>1)
    {
     KesionJS.Alert('对不起，一次只能编辑一条记录!',null);
     return false;
    }
  else
    return true;
  }
chk=function(obj)
{   var el=event.srcElement;
    var selectedcolor='#E7F1FA';
	if (el.type=='checkbox') 
	{
		if (el.checked==true)
		 obj.style.background=selectedcolor;
		 else
		 obj.style.background='';
	}
	else
	{
		var cobj=null;
		for(var i=0;i<obj.children.length;i++)
		{
			var em=obj.children[i];
			for(var j=0;j<em.children.length;j++)
			{
				//if (em.children[j].tagName=='INPUT')
				if (em.children[j].type=='checkbox'){
					cobj=em.children[j];
					if (cobj.checked==''){
						 cobj.checked='checked';
						 obj.style.background=selectedcolor;
					 } else {
					   cobj.checked='';
					   obj.style.background='#ffffff';
					 }
				}
			}
		}
	}
}
function Select(flag)
{  
 //alert(document.forms[0].elements.length);
 for(var i=0;i<document.forms[0].elements.length;i++)
  {
	  var e = document.forms[0].elements[i];
      if (e.type=="checkbox")
	  {
		  switch (flag)
		  {
			  case 0:
			   e.checked='checked';
			   e.parentElement.parentElement.style.background='#D2E3F2';
			   break;
			  case 1: //反选
				if (e.checked==''){
						e.checked='checked';
						e.parentElement.parentElement.style.background='#D2E3F2';
				}else{
						e.checked='';
						e.parentElement.parentElement.style.background='';
				}
				break;
			 case 2:
			  e.checked='';
			  e.parentElement.parentElement.style.background='';
			 break;
		  }
         //   e.checked='checked';
	  }
  }
}

// 修改编辑栏高度
function zoomtextarea(num,objname)
{ 
	var obj=document.getElementById(objname)
	if (parseInt(obj.rows)+num>=3) {
		obj.rows = parseInt(obj.rows) + num;	
	}
	if (num>0)
	{
		obj.width="90%";
	}
}

function showtips(str)
{
	document.write('<div id="tips" style="display:none;" class="message">');
	document.write(str + '</div><div style="clear:both;"></div>');
	jQuery("#tips").fadeIn("slow");
}

//显示提示层代码
function showhintinfo(obj, objleftoffset,objtopoffset, title, info , objheight, showtype ,objtopfirefoxoffset)
{
   var p = getposition(obj);
   
   if((showtype==null)||(showtype =="")) 
   {
       showtype =="up";
   }
   document.getElementById('hintiframe'+showtype).style.height= objheight + "px";
   
   //document.getElementById('hinttitle').innerHTML = title;
   document.getElementById('hintinfo'+showtype).innerHTML = info;
   document.getElementById('hintdiv'+showtype).style.display='block';
   
   if((objtopfirefoxoffset != null)&& !IsIE)
   {
        document.getElementById('hintdiv'+showtype).style.top=p['y']+objtopfirefoxoffset+"px";
   }
   else
   {
        
        if(objtopoffset == 0)
        { 
			if(showtype=="up")
			{
				 document.getElementById('hintdiv'+showtype).style.top=p['y']-document.getElementById('hintinfo'+showtype).offsetHeight-40+"px";
			}
			else
			{
				 document.getElementById('hintdiv'+showtype).style.top=p['y']+obj.offsetHeight+5+"px";
			}
        }
        else
        {
			document.getElementById('hintdiv'+showtype).style.top=p['y']+objtopoffset+"px";
        }
   }
   document.getElementById('hintdiv'+showtype).style.left=p['x']+objleftoffset+"px";
   
}
function getposition(obj) {
	var r = new Array();
	r['x'] = obj.offsetLeft;
	r['y'] = obj.offsetTop;
	while(obj = obj.offsetParent) {
		r['x'] += obj.offsetLeft;
		r['y'] += obj.offsetTop;
	}
	return r;
}


//隐藏提示层代码
function hidehintinfo()
{
    document.getElementById('hintdivup').style.display='none';
    document.getElementById('hintdivdown').style.display='none';
}


//==================================================Digg模块开始==================================================
digg=function(diggtype,channelid,infoid,installdir){ 
 jQuery.ajax({url:installdir+'plus/digg.aspx',cache:false,data:'action=hits&ChannelID='+channelid+'&infoid=' +infoid+'&diggtype='+diggtype,success:function(returnvalue){
	switch (returnvalue){
	    case "err":
		 KesionJS.Message('error.gif','digg error!',null);
		 break;
		case "over":
		 KesionJS.Message('error.gif','您已评分过了，不能重复评分！',null);
		 break;
		case "nologin":
		  KesionJS.Message('error.gif','您还没有登录，不能推荐!',null);
		  break;
		default:
		  try{
		   var sarr=returnvalue.split('|');
		   $('#s'+diggtype+sarr[0]).html(sarr[1+diggtype]);
		   $('#d'+diggtype+sarr[0]).html("<a href='"+installdir+"/plus/dig.aspx' target='_blank'>查看</a>");
		  }catch(e){ }
		  try{ showdigg(returnvalue); }catch(e){ }
	}
}});
}

show_digg=function(diggtype,channelid,infoid,installdir){ 
   jQuery('#s'+diggtype+infoid).html("<img src='"+installdir+"/sysimg/loading.gif' />");
   jQuery.ajax({url:installdir+"/plus/digg.aspx",cache:false,data:"channelid="+channelid+"&infoid="+infoid+"&action=show&diggtype="+diggtype,success:function(obj){
	ShowDiggNum(obj,diggtype);
}});
   
}
ShowDiggNum=function(returnvalue,diggtype){
	var rarr=returnvalue.split('|');
	jQuery('#s'+diggtype+rarr[0]).html(rarr[1+diggtype]);
}
show_digg1=function(channelid,infoid,installdir)
{
	jQuery.ajax({url:installdir+"plus/digg.aspx",cache:false,data:"channelid="+channelid+"&infoid="+infoid+"&action=show",success:function(obj){
	showdigg(obj);
}});
}
showdigg=function(r){
	  try{
		 var rarr=r.split('|');
		 jQuery("#s"+rarr[0]).html(rarr[1]);
		 jQuery("#c"+rarr[0]).html(rarr[2]);
		 var znum=jQuery("#s"+rarr[0]).html();
		 var cnum=jQuery("#c"+rarr[0]).html();
		 var totalnum=parseInt(znum)+parseInt(cnum);
					if (parseInt(znum)==0){
					jQuery("#perz"+rarr[0]).html('0%');
					}else{
					jQuery("#perz"+rarr[0]).html(((znum*100)/totalnum).toFixed(2)+'%');
					}
					if (parseInt(cnum)==0){
					jQuery("#perc"+rarr[0]).html('0%');
					}else{
					jQuery("#perc"+rarr[0]).html(((cnum*100)/totalnum).toFixed(2)+'%');
					}
					jQuery("#digzcimg").width(parseInt((znum/totalnum)*55));
					jQuery("#digcimg").width(parseInt((cnum/totalnum)*55));
		}catch(e){
		}
}
//==================================================Digg模块结束==================================================




//===========================以下是一些常用JS===============================================
var KesionJS={
		//通用对话框
		Alert:function(msg,act){ 
		  if (frameElement==null || frameElement.api==undefined){
			 this.Alert1(msg,act); 
		  }else{
			 var api = frameElement.api, W = api.opener;
			 W.jQuery.dialog.alert(msg,function(){   setTimeout(function(){ eval(act);},1);});
		  }
		},
		Alert1:function(msg,act){ 
		  jQuery.dialog.alert(msg,function(){eval(act);});
		},
		AlertByTime:function(src,msg,t){
		   if (src==1){
			    src="success.gif";
		   }else{
			   src="error.gif";
		   }
		   if (t=='') t=2;
		   jQuery.dialog.tips(msg,t,src,function(){});

		},
		Confirm:function(msg,fun1,fun2){
		   if (frameElement==null ||frameElement.api==undefined){
			   this.Confirm1(msg,fun1,fun2);
		   }else{
			   var api = frameElement.api, W = api.opener;
			   W.jQuery.dialog.confirm(msg,function(){setTimeout(function(){ eval(fun1);},1);},function(){ setTimeout(function(){ eval(fun2);},1);});
		   }
		},
		Confirm1:function(msg,fun1,fun2){
			jQuery.dialog.confirm(msg,function(){eval(fun1);},function(){eval(fun2);});
		},
		Message:function(ico,msg,fun){
			if (frameElement==null ||frameElement.api==undefined){
			 this.Message1(ico,msg,fun); 
		  }else{
			  var api = frameElement.api, W = api.opener;
			  W.jQuery.dialog.tips(msg,2,ico,function(){ setTimeout(function(){ eval(fun);},1); });
			  
		  }
		},
		Message1:function(ico,msg,fun){
			jQuery.dialog.tips(msg,2,ico,function(){eval(fun);});
		},
		Tips:function(title,msg,w,h){
			jQuery.dialog({ title:title,
				content: msg,
				width:w,
				height:h,
				max:false,
				min:false
			});
			
		},
		GetFileNameArea:function(val){
		if (val==0){
		   jQuery('#filearea').css("display","none");}
		else{
		   jQuery('#filearea').css("display","");}
       },
	   GetTemplateArea:function(val){
		 if (val==2){
		   jQuery('#templatearea').css("display","none");}
		 else{
		   jQuery('#templatearea').css("display","");}
        },
		//word存图
		saveWordPic:function(){
		  var html=CKEDITOR.instances.Content.getData();
		  var wordpic;
	        try {
	            wordpic = new ActiveXObject("Kesion.Word");
	        }
	        catch (exception) {
				KesionJS.Alert('您没有安装KesionCMS Word图片上传插件或您的浏览器不支持!',null);
	            return ;
	        }
		 if (html==''){
			KesionJS.Alert('编辑器中没有内容!',null);
		    return;
		  }

			jQuery('#ajaxmsg',window.parent.document).toggle();
	        var temp = "";
	        var strBase64 = "";
	        var reg = /<img[\s\S]*?src="file:\/\/\/[\s\S]*?"[\s\S]*?>/ig;
	        var result = html.match(reg);
	        if (result) {
	            for (i = 0; i < result.length; i++) {
	                result[i].replace(/src="(file:\/\/\/([\s\S]*?))"/ig, function($1, $2, $3) {
	                    try {			
	                        var temppic = wordpic.ImageToBase64($3);
	                        if (temppic != "") {
	                            temp += $2 + "|" + temppic + "$";
	                        }
	                    }
	                    catch (exception)
                   { return; }
	                });
	            }
	            if (temp != "") {
	                strBase64 = temp.substring(0, temp.length - 1);
	            }
	        }
	        //        return strBase64;
			//document.write(html);
			//return;
	        strBase64 = strBase64.replace(/\+/ig, "*");
	        try {
	            var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	            var postdata = "html=" + escape(html) + "&strbase64=" + escape(strBase64) ;
				//alert(postdata);
	            oXmlHttp.open("POST", "KS.WordUploadPic.aspx", false);
	            oXmlHttp.setRequestHeader("Content-Length", postdata.length);
	            oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	            oXmlHttp.send(postdata);
	            if (oXmlHttp.readyState == 4) {
	                if (oXmlHttp.status == 200) {
	                    html = oXmlHttp.responseText;
						CKEDITOR.instances.Content.setData(html);
						jQuery('#ajaxmsg',window.parent.document).toggle();
						return;
	                }
	            }
	        }
	        catch (exception) {
			   return;
	        }
			return ;
		 }

};
//===========================JS结束=============================================================


function showuserlogin(){
    jQuery.dialog({title:"用户登录",content:"url:"+appurl+"user/userlogin.aspx",width:400,height:200,max:false,min:false});
}
//检查是不是数字
isNumber = function (e) {   
    if ($.browser.msie) {   
        if ( ((event.keyCode > 47) && (event.keyCode < 58)) ||   
              (event.keyCode == 8) ) {   
            return true;   
        } else {   
            return false;   
        }   
    } else {   
        if ( ((e.which > 47) && (e.which < 58)) ||   
              (e.which == 8) ) {   
            return true;   
        } else {   
            return false;   
        }   
    }   
}  

var sAgent=navigator.userAgent.toLowerCase();
var IsIE=sAgent.indexOf("msie")!=-1;


/*******显示等待提示消********/
function showWaitInfo(info,obj)
	{
	   try{
	   if(obj==null)return;
	   clearWaitInfo();
	   var newd=document.createElement("span");
	   newd.className='waitInfo';
	   newd.id='waitInfo';
	   newd.innerHTML=info;
	   obj.parentNode.appendChild(newd);
	   }catch(e){}
}
function clearWaitInfo(){
	   try{
	   if(jQuery('#waitInfo')){jQuery('#waitInfo')[0].parentNode.removeChild(jQuery('#waitInfo')[0]);}
	   }catch(e){}
}

//显示剩余时间
var serverTime = new Date() ;
var dateTime = new Date(); 
var difference = dateTime.getTime() - serverTime.getTime(); 
function StringToDate(DateStr)  
{   
    var converted = Date.parse(DateStr);  
    var myDate = new Date(converted);  
    if (isNaN(myDate))  
    {    
	   var arys= DateStr.split(' '); 
       var arys1=arys[0].split('-');
	   var arys2=arys[1].split(':');
        myDate = new Date(arys1[0],--arys1[1],arys1[2],arys2[0],-arys2[1],arys2[2]);  
    }  
    return myDate;  
}  
function show_date_time(begin,end,bjq,f){
	    var beginTime =StringToDate(begin)
	    var endTime =StringToDate(end)
        var nowTime = new Date(); 
		
		
        var nMS=endTime.getTime() - nowTime.getTime() + difference; 
        var myD=Math.floor(nMS/(1000 * 60 * 60 * 24)); 
        var myH=Math.floor(nMS/(1000*60*60)) % 24; 
        var myM=Math.floor(nMS/(1000*60)) % 60; 
        var myS=Math.floor(nMS/1000) % 60; 
        var myMS=Math.floor(nMS/100) % 10; 
	    if(myD>= 0){ 
            var str ='';
			if (myD>0) str=myD+"天";
			str+=myH+"小时"+myM+"分"+myS+"."+myMS+"秒"; 
        }else{ 
           var str = "";     
		   if (f==0) {
			 var tt=beginTime.getTime()-nowTime.getTime();
			  if (tt>0){
			   str="火爆进行中...";
			  }else{
			   str="已结束.";
			  }
		   }else{str = "已结束.";}
        } 
		document.getElementById(""+bjq+"").innerHTML=str;
	window.setTimeout("show_date_time('"+begin+"','"+end+"','"+bjq+"','"+f+"')", 100);
}

function scrollDoor(){
}
scrollDoor.prototype = {
	sd : function(menus,divs,openClass,closeClass){
		var _this = this;
		if(menus.length != divs.length)
		{
			alert("菜单层数量和内容层数量不一样!");
			return false;
		}				
		for(var i = 0 ; i < menus.length ; i++)
		{	
			_this.$$(menus[i]).value = i;				
			_this.$$(menus[i]).onmouseover = function(){
					
				for(var j = 0 ; j < menus.length ; j++)
				{						
					_this.$$(menus[j]).className = closeClass;
					_this.$$(divs[j]).style.display = "none";
				}
				_this.$$(menus[this.value]).className = openClass;	
				_this.$$(divs[this.value]).style.display = "block";				
			}
		}
		},
	$$ : function(oid){
		if(typeof(oid) == "string")
		return document.getElementById(oid);
		return oid;
	}
}