﻿//PBlog2 公用JS代码
//Author:PuterJam

//查找网页内宽度太大的图片进行缩放以及PNG纠正
 function ReImgSize(){
  for (i=0;i<document.images.length;i++)
   {
   if (document.all){
	if (document.images[i].width>550)
	 {
       document.images[i].width="550"
       try{
	       document.images[i].outerHTML='<a href="'+document.images[i].src+'" target="_blank" title="在新窗口打开图片">'+document.images[i].outerHTML+'</a>'
  	 	}catch(e){}
  	 }
   }
  else{
	if (document.images[i].width>400) {
	  document.images[i].title="在新窗口打开图片"
	  document.images[i].style.cursor="pointer"
	  document.images[i].onclick=function(e){window.open(this.src)}
	}
  }
  }
 }

//AccessKey 转换For IE Design By PuterJam
  var NKey=new Array()
  var KeyCode=new Array()
  var KeyInit=false
//======== keyCode ============
  KeyCode["1"]=49
  KeyCode["2"]=50
  KeyCode["3"]=51
  KeyCode["["]=219
  KeyCode["]"]=221
  KeyCode[","]=188
  KeyCode["."]=190
  var EKey="abcdefghijklmnopqrstuwvxyz"
  for (i=0;i<26;i++){
    KeyCode[EKey.substr(i,1)]=65+i
  }
  
  function PressKey(){
	 if (document.all) {
	   for (i=0;i<NKey.length;i++){
		   if (window.event.altKey && window.event.keyCode==NKey[i].Code) {NKey[i].Object.blur();NKey[i].Object.click()}
	   }

	}
  }
  
  function AccessKey(Code,Object){
   this.Code=Code
   this.Object=Object
  }


  function initAccessKey(){
	 if (document.all && !KeyInit) {
	    for (i=0;i<document.links.length;i++){
	      if (document.links[i].accessKey) {
	        NKey[NKey.length]=new AccessKey(KeyCode[document.links[i].accessKey.toLowerCase()],document.links[i])
	      }
	    }
	 }
	 KeyInit=true
  }

//初始化JS代码
function initJS(){
	ReImgSize() //自动缩放代码 
	initAccessKey()  //转换AccessKey For IE
}

//让Mozilla支持innerText
try{
	HTMLElement.prototype.__defineGetter__
	(
	"innerText",
	function ()
	{
		var anyString = "";

		var childS = this.childNodes;
			for(var i=0; i<childS.length; i++)
			{
				if(childS[i].nodeType==1)
				anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
				else if(childS[i].nodeType==3)
				anyString += childS[i].nodeValue;
			}
			return anyString;
	}
	); 
}
catch(e){}

//判断是否是IE
function isIE(){
	if (navigator.appName!="Microsoft Internet Explorer") {return false}
	return true
}
//显示隐藏主题
function TopicShow(e,TopicID){
	 e.className=(e.className=="BttnC")?"BttnE":"BttnC"
	 document.getElementById(TopicID).style.display=(e.className=="BttnC")?"":"none"
	 ReImgSize()
}
//打开新窗口
function popnew(url,title,width,height){
    var w = 1024;
    var h = 768;

    if (document.all || document.layers){
        w = screen.availWidth;
        h = screen.availHeight;
    }

    var leftPos = (w/2-width/2);
    var topPos = (h/2.3-height/2.3);

    window.open(url,title,"width="+width+",height="+height+",top="+topPos+",left="+leftPos+",scrollbars=no,resizable=no,status=no")
}
//运行代码
function runEx(cod1)  {
	 cod=document.getElementById(cod1)
	  var code=cod.value;
	  if (code!=""){
		  var newwin=window.open('','','');  
		  newwin.opener = null 
		  newwin.document.write(code);  
		  newwin.document.close();
	}
}
//复制代码
function doCopy(ID) { 
	if (document.all){
		 textRange = document.getElementById(ID).createTextRange(); 
		 textRange.execCommand("Copy"); 
	}
	else{
		 alert("此功能只能在IE上有效")
	}
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
			}
			else{
				strTemp = VALUE.substring(iTemp,v_length);
				break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


function CheckPost(){
  try{
	if (Trim(document.forms[0].title.value)=="") {
		 alert("标题不能为空,请写日志标题!")
		 document.forms[0].title.focus()
		 return false
		}
	}
  catch(e){}

  try{
    if (document.getElementById("P2").checked) {
    	if (!CheckDate(document.forms[0].PubTime.value)){
    	   alert("日期格式不正确!\n格式:yyyy-mm-dd hh:mm:ss")	
		   return false
    	}
    	else
    	{
	    	document.forms[0].PubTime.value=CheckDate(document.forms[0].PubTime.value)
    	}
    }
  }
  catch(e){}

  try{
   if (GetMessageLength()==0){
		 alert("内容不能为空!")
		 return false
   }
  }
  catch(e){
	if (Trim(document.forms[0].Message.value)=="") {
		 alert("内容不能为空!")
		 try{document.forms[0].Message.focus()}catch(e){}
		 return false
	}
  }
   try{document.forms[0].SaveArticle.disabled=true}catch(e){}
   try{document.forms[0].SaveDraft.disabled=true}catch(e){}
   try{document.forms[0].ReturnButton.disabled=true}catch(e){}
   try{document.forms[0].CancelEdit.disabled=true}catch(e){e}
   try{document.forms[0].DelArticle.disabled=true}catch(e){}
   return true
}

   function CheckDate(str){
        var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/; 
        var r = str.match(reg); 
        if(r==null)return false; 
        var d= new Date(r[1],r[3]-1,r[4],r[5],r[6],r[7]); 
        return (d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
   }


function GetMessageLength()
{
	var oEditor = FCKeditorAPI.GetInstance('Message') ;
	var oDOM = oEditor.EditorDocument ;
	var iLength ;

	if ( document.all )		// If Internet Explorer.
	{
		iLength = oDOM.body.innerText.length ;
	}
	else					// If Gecko.
	{
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
	}
//	oEditor.InsertHtml('')
return iLength
}	

function SetFont(size){
	document.getElementById("logPanel").style.fontSize=size
}
//引用
function addQuote(usr,content){
	try{
	if (document.getElementById("editorbody").style.display=="none") showUBB("Message");
    document.forms["frm"].Message.value="[quote="+usr+"]"+document.getElementById(content).innerText+"[/quote]"
	document.forms["frm"].Message.focus();
	}catch(e){}
}

//写入顶部Flash文件
function WriteHeadFlash(Path,Width,Height,Transparent){
	 var Temp,T=""
	 Temp='<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="FlashH" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="'+Width+'" height="'+Height+'">'
	 Temp+='<param name="movie" value="'+Path+'"/>'
	 Temp+='<param name="quality" value="High"/>'
	 Temp+='<param name="scale" value="ExactFit"/>'
	 if (Transparent) {Temp+=' <param name="wmode" value="transparent"/>';T='wmode="transparent"'}
	 Temp+='<embed src="'+Path+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="FlashH" width="'+Width+'" height="'+Height+'" quality="High"'+T+' scale="ExactFit"/>'
	 Temp+='</object>'
	 document.getElementById("FlashHead").innerHTML=Temp
}

//告诉好友
function getTrackbackURL(id,a){
	var strHTML = "";
	strHTML = '<span id="tbSpan">请输入好友邮件地址 <input id="vCode" size="40" maxlength="36" style="width:260px;border:1px solid #999;"/> <input type="button" value="提交" onclick=getTB("'+id+'","'+a+'")></span>';
	showPopup("告诉好友",strHTML,450,200);
}

function getTB(id,b){

	if (document.getElementById("vCode").value.length <4||chkemail(document.getElementById("vCode").value)=="0"){
		alert("请输入正确的好友邮件地址");
		document.getElementById("vCode").select();
		return;
	}

	  var url = "/down/send.asp"
	  var postStr = "ID=" + id + "&email=" + document.getElementById("vCode").value + "&name=" + b;;
	  var ajax = ecall_InitAjax();
	  ajax.open("POST", url, true);
	  ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	  ajax.send(postStr);
	  ajax.onreadystatechange = function() 
	  {
	    if (ajax.readyState == 4 && ajax.status == 200)
	    {
	      alert(ajax.responseText);
	    }
	  }

//	window._tBID = id;
//	var tJS = document.getElementById("tbJS");
//	if (!tJS){
//		tJS = document.createElement("script");
//		tJS.id = "tbJS";
//		document.getElementsByTagName("HEAD")[0].appendChild(tJS);
//	}
//	tJS.src = "send.asp?ID=" + id + "&email=" + document.getElementById("vCode").value + "&name=" + b;
	hidePopup();
}

function setTBKey(code){
	if (!code || code == "codeError"){
		alert("验证码错误");
		document.getElementById("vCode").select();
		document.getElementById("tbCode").src = 'common/getCode.asp?s='+Math.random();
		return;
	}
	var tURL = document.getElementById("getTBURL");
	var pBody = document.getElementById("popBody");
	pBody.style.width = "400px";
	document.getElementById("tbSpan").style.display = "none";
	tURL.style.display = "";
	tURL.value = /(.+\/)/i.exec(location.href)[0] + "trackback.asp?tbID=" + window._tBID + "&action=addtb&tbKey=" + code;
}

//popup support
function showMask(){
	var mask = document.getElementById("webMask");
	if (!mask){
		mask = document.createElement("div");
		mask.id = "webMask";
		mask.style.cssText = "position:absolute;width:100%;left:0px;top:0px;background:#000;filter:Alpha(opacity=50);-moz-opacity:0.5";
		document.body.appendChild(mask);
	}
	var h = document.documentElement.scrollHeight;
	mask.style.display = "";
	mask.style.height = h + "px";	
}

function hideMask(){
	var mask = document.getElementById("webMask");
	if (mask) mask.style.display = "none";
	
	var vImg = document.getElementById("vcodeImg");
	if (vImg) vImg.src="common/getCode.asp?s="+Math.random();
}

function showPopup(title,html,width){
	showMask();
	displaySelect(false);

	var pContent = document.getElementById("popContent");
	if (!pContent){
		pContent = document.createElement("div");
		pContent.id = "popContent";
		pContent.style.cssText = "position:absolute;width:100%;left:0px;top:0px;text-align:center";
		document.body.appendChild(pContent);
	}
	
	var pBody = document.getElementById("popBody");
	if (!pBody){
		pBody = document.createElement("div");
		pBody.id = "popBody";
		pBody.style.cssText = "background:#fff;margin:auto;padding:3px;text-align:left;-moz-border-radius:5px;";
		pContent.appendChild(pBody);
	}

	pBody.style.width = width + "px";
	var closeButton = isIE()?'<font style="float:right;font-family:webdings;cursor:pointer;margin-top:-3px" onclick="hidePopup()">r</font>':'<font style="float:right;cursor:pointer;margin-top:-2px" onclick="hidePopup()"><u>close</u></font>';

	var strHTML = '<h4 style="color:#000;font-size:14px;margin:4px;margin-bottom:2px">'+ closeButton + (title?title:'无标题窗口') + '</h4>';

	strHTML += '<div style="background:#e5e5e5;margin:4px;padding:4px;color:#333">'+html+'</div>'

	pBody.innerHTML = strHTML;
	
	var height = pContent.offsetHeight;
	var bodyHeight = isIE()?document.documentElement.offsetHeight:document.documentElement.clientHeight;
	pContent.style.display = "";
	pContent.style.top = document.documentElement.scrollTop + ((bodyHeight - height)/2)+ "px";
}

function hidePopup(){
	hideMask();
	displaySelect(true);
	var pContent = document.getElementById("popContent");
	if (pContent) pContent.style.display = "none";
}

function displaySelect(status){
	var s = document.getElementsByTagName("select");
	for (var i=0;i<s.length;i++){
		s[i].style.display = (status)?"":"none";
	}
}
