// JavaScript Document
// 核心JS

var Navigation={
	isIE:(navigator.appVersion.indexOf("MSIE")!=-1),
	isIE6:(navigator.appVersion.indexOf("MSIE")!=-1&&navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE"),10).replace(/[^\d.]/g,"").indexOf("6.")!=-1),
	isIE7:(navigator.appVersion.indexOf("MSIE")!=-1&&navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE"),10).replace(/[^\d.]/g,"").indexOf("7.")!=-1),
	isOpera:navigator.userAgent.indexOf("Opera")!=-1,
	isSafari:navigator.appVersion.indexOf("KHTML")!=-1,
	isFirefox:navigator.userAgent.indexOf("Firefox")!=-1,
	isCamino:navigator.userAgent.indexOf("Camino")!=-1,
	isMozilla:navigator.userAgent.indexOf("Gecko/")!=-1
}

//===================================================================================================================== Prototype
String.prototype.toNum=function(){
	var n=this.match(/\d+/,"")
	if(n) return parseInt(n)
	else return 0
}
String.prototype.toDate=function(){
	var d=this.split(/\D+/)
	if(d[1])d[1]--
	d=eval("new Date("+d.join(",")+")")
	if(isNaN(d))d=new Date()
	return d
}
String.prototype.toHTML=function(){
	return this.htmlFilter().replace(/\r\n|\r|\n/g,"<br/>").replace(/\s/g,"&nbsp;")
}
String.prototype.toAttribute=function(){
	return this.htmlFilter().replace(/\r\n|\r|\n/g,"&#13;&#10;").replace(/\s/g,"&nbsp;")
}
String.prototype.removeBlank=function(){
	return this.replace(/^[\s　]*/mg,"").replace(/[\s　]*$/mg,"").replace(/^[\s　]*$/mg,"")
}
String.prototype.toContent=function(){
	return this.removeBlank().htmlFilter().replace(/^([^\r\n]+)/mg,"<p>$1</p>").replace(/\r\n|\r|\n/g,"").replace(/\s/g,"&nbsp;")
}
String.prototype.htmlFilter=function(){
	return this.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")
}
String.prototype.toURL=function(){
	return encodeURIComponent(this)
}
String.prototype.toSafeString=function(){
	return this.replace(/\\/g,"\\\\").replace(/\r\n|\r|\n/g,"\\r\\n").replace(/"/g,"\\\"").replace(/'/g,"\\'")
}
String.prototype.toSafeContent=function(){
	return this.replace(/<\/*a[^>]*>/ig,"").replace(/<script/ig,"<&#83;cript").replace(/<style/ig,"<&#83;tyle").replace(/\bon/ig,"&#79;n")
}
String.prototype.toFix=function(length, esp){
	if(this.replace(/[^\x00-\xff]/g,"dd").length<=length)return String(this)
	if(esp!="")esp=esp||"..."
	if(length<esp.length)esp=""
	length-=esp.length
	var text = this.substr(0,length)
	while(text.replace(/[^\x00-\xff]/g,"dd").length>length){
		text=text.substr(0,text.length-1)
	}
	return text+esp
}
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
　　-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
　　52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
　　-1,　0,　1,　2,　3, 4,　5,　6,　7,　8,　9, 10, 11, 12, 13, 14,
　　15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
　　-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
　　41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
function base64decode(str) {
　var c1, c2, c3, c4;
　var i, len, out;
　len = str.length;
　i = 0;
　out = "";
　while(i < len) {
		do {
　　	c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
		} while(i < len && c1 == -1);
		if(c1 == -1)break;
		do {
			c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
		} while(i < len && c2 == -1);
		if(c2 == -1)break;
		out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
		do {
			c3 = str.charCodeAt(i++) & 0xff;
	　　if(c3 == 61)return out;
			c3 = base64DecodeChars[c3];
		} while(i < len && c3 == -1);
		if(c3 == -1)break;
		out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
		do {
　　	c4 = str.charCodeAt(i++) & 0xff;
　　	if(c4 == 61)return out;
			c4 = base64DecodeChars[c4];
		} while(i < len && c4 == -1);
		if(c4 == -1)break;
		out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
　}
　return out;
}
function utf8to16(str) {
　var out, i, len, c;
　var char2, char3;
　out = "";
　len = str.length;
　i = 0;
　while(i < len) {
		c = str.charCodeAt(i++);
		switch(c >> 4){
		　case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
	　　	out += str.charAt(i-1);
	　	　break;
　		case 12: case 13:
　　		char2 = str.charCodeAt(i++);
		　　out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
　　 		break;
　		case 14:
　　		char2 = str.charCodeAt(i++);
　　		char3 = str.charCodeAt(i++);
　　		out += String.fromCharCode(((c & 0x0F) << 12) |((char2 & 0x3F) << 6) |((char3 & 0x3F) << 0));
		　　break;
		}
　}
　return out;
}
String.prototype.fromBase64=function(){
	return utf8to16(base64decode(this));
}
Date.prototype.toString=function(){
	return new String(this.getFullYear()+"-0"+(this.getMonth()+1)+"-0"+this.getDate()+" 0"+this.getHours()+":0"+this.getMinutes()+":0"+this.getSeconds()).replace(/(\D)0(\d{2})/g,"$1$2")
}
Date.prototype.addYear=function(year){
	return new Date(this.setFullYear(this.getFullYear()+parseInt(year)))
}
Date.prototype.addMonth=function(month){
	return new Date(this.setMonth(this.getMonth()+parseInt(month)))
}
Date.prototype.addDate=function(date){
	return new Date(this.setDate(this.getDate()+parseInt(date)))
}
Date.prototype.addHours=function(hour){
	return new Date(this.setHours(this.getHours()+parseInt(hour)))
}
Date.prototype.addMinutes=function(minute){
	return new Date(this.setMinutes(this.getMinutes()+parseInt(minute)))
}
Date.prototype.addSeconds=function(second){
	return new Date(this.setSeconds(this.getSeconds()+parseInt(second)))
}
Date.prototype.toSimpleCase=function(){
	return this.toString().replace(/-/g,".")
}
Number.prototype.checkSum=function(number){
	return (this^number)==(this-number)
}
Number.prototype.toIP=function(){
	var ip=this.toString(16).split("")
	for(var o=0; o<ip.length; o=o+2){
		ip[o]=parseInt(ip[o]+ip[o+1],16)
		ip[o+1]=""
	}
	ip=ip.join(".").replace(/\.+/g,".").replace(/^\.|\.$/g,"")
	return ip
}
Number.prototype.minMax=function(v1,v2){
	if(v1>v2){
		var v3=v1
		v1=v2
		v2=v3
	}
	if(this<v1)return v1
	if(this>v2)return v2
	return this
}
//===================================================================================================================== Cookie
var Cookie={}
Cookie.set=function(key, value, expires){
	var root = ""
	var base = ""
	var newCookie=""
	if(!value){
		value = ""
		expires = "1986-7-21 00:00:00"
	}
	if(key.indexOf(".")!=-1){
		var rootName=key.substr(0,key.indexOf("."))
		var subName =key.substr(key.indexOf(".")+1)
		root = Cookie.__getRecursion(document.cookie, rootName, 0)
		if(root){
			root = String(root).split("&")
			for(i=0;i <root.length; i++){
				var cName = root[i].substr(0, root[i].indexOf("="))
				var cValue = root[i].substr(root[i].indexOf("=")+1)
				if (cName != subName){
					base += cName + "=" + encodeURIComponent(cValue) + "&"
				}
			}
		}else{
			base = ""	
		}
		newCookie = rootName + "=" + base + subName + "=" + encodeURIComponent(value)
	}else{
		newCookie = key + "=" + encodeURIComponent(value)
	}
 	if(typeof expires == "string")expires=expires.toDate()
	if(expires)expires = "; expires=" + expires.toGMTString()
	else expires=""
	document.cookie = newCookie + expires + "; domain=.qujiao.com; path=/"
}
Cookie.get=function(key){
	return Cookie.__getRecursion(document.cookie, key, 0)
}
Cookie.__getRecursion=function(string, key, level){ //获取Cookie递归函数
	key = key.split(".")
	if(level==0){
		string = string.split(";")
	}else{
		string = string.split("&")
	}
	for(var i=0;i<string.length;i++){
		var cName = string[i].substr(0, string[i].indexOf("=")).replace(/\s/,"")
		var cValue = string[i].substr(string[i].indexOf("=") + 1)
		if(cName == key[level]){
			if(!key[level+1]){
				return String(decodeURIComponent(cValue.replace(/\+/g," ")))
			}else{
				return Cookie.__getRecursion(cValue, key.join("."), level+1)
			}
		}
	}
	return
}
Cookie.alert=function(){alert(document.cookie)}

var Client={}
Client.isLogin=function(redirect){
	if(Cookie.get("ddLogin.ddUserName")) return true
	else{
		Cookie.set("ddLogin")
		if(redirect)window.top.location.href="/login.htm"
		return false
	}
}
//===================================================================================================================== DOM
$=function(objName){
	if(typeof objName == "string") return instanceObject(document.getElementById(objName))
	else return instanceObject(document.getElementById(objName)||objName)
}
$$=function(tagName, className, parentNode){
	parentNode=parentNode||(this==window?document:this)
	var searchTag
	tagName=tagName||""
	if(!tagName){
		searchTag="*"
	}else if(tagName.indexOf("|")!=-1){
		searchTag="*"
	}else{
		searchTag=tagName
	}
	var result=[]
	var element=parentNode.getElementsByTagName(searchTag)
	for(var i=0; i<element.length; i++){
		if(!className || (className && (" "+element[i].className+" ").indexOf(" "+className+" ")!=-1)){
			if(tagName.indexOf("|")==-1 || ("|"+tagName+"|").indexOf("|"+element[i].tagName+"|")!=-1){
				result.push(instanceObject(element[i]))
			}
		}
	}
	return result
}
function newElement(tagName, attributes){
	if(tagName.indexOf("<")==0){
		var object=document.createElement("SPAN")
		object.innerHTML=tagName
		object=object.childNodes[0]
	}else{
		var object=document.createElement(tagName)
	}
	if(attributes){
		for(var i in attributes){
			object.setAttribute(i, attributes[i])
			object[i]=attributes[i]
		}
	}
	return instanceObject(object)
}
function instanceObject(){
	for(var i=0; i<arguments.length; i++){
		if(arguments[i]){
			if(!arguments[i].addEventListener)		arguments[i].addEventListener=addEvent
			if(!arguments[i].removeEventListener)	arguments[i].removeEventListener=removeEvent
			if(!arguments[i].show)					arguments[i].show=objectShow
			if(!arguments[i].hide)					arguments[i].hide=objectHide
			if(!arguments[i].getAbsPosition)		arguments[i].getAbsPosition=getAbsPosition
			if(!arguments[i].getInnerText)			arguments[i].getInnerText=getInnerText
			if(!arguments[i].$$)					arguments[i].$$=$$
			if(!arguments[i].insertAfter)			arguments[i].insertAfter=insertAfter
		}
	}
	return arguments[0]
}
function getInnerText(){
	return this.innerText||this.textContent
}
function addEvent(sEvent,func){
	return this.attachEvent("on"+sEvent,func)
}
function removeEvent(sEvent, func){
	return this.detachEvent("on"+sEvent,func);
}
function objectShow(){
	this.style.display=""
}
function objectHide(){
	this.style.display="none"
}

function eventTarget(evt){ // 根据事件返回源对象
	return evt.target||evt.srcElement
}

function insertAfter(newElement,targetElement){
	var parent=targetElement.parentNode;
	if (parent.lastChild==targetElement){
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}
function getAbsPosition(){
	var object=this
	var x=0, y=0
	do{
		x+=object.offsetLeft
		y+=object.offsetTop
	}while(object=object.offsetParent);
	return {x:x, y:y}
}

instanceObject(window)
instanceObject(document)
if(Navigation.isIE6)document.execCommand("BackgroundImageCache", false, true)
//==================================================================================================================== Debug
function debug(obj){
	var o=[]
	for(var i in obj)o.push(i+"="+obj[i])
	o.sort(function(a,b){
		if(a>b) return 1
		return -1
	})
	if(!$("debugBox"))document.body.appendChild(newElement("DIV", {id:"debugBox"}))
	$("debugBox").innerHTML = o.join("<br />")
}
function ajaxDebug(){
	var iframe=document.$$("IFRAME")
	for(var i=0; i<iframe.length; i++){
		iframe[i].style.left="0px"
		iframe[i].style.top="0px"
		iframe[i].style.position="fixed"
		iframe[i].style.display=""
	}
}
window.addEventListener("load", function(){if($("spanCopy"))$("spanCopy").addEventListener("click",ajaxDebug,true)}, true)

//=====================================================================================================================Response
function writeLogin(){
	document.write(getLoginString())
}
function getLoginString(){
	var userName=Cookie.get("ddLogin.ddUserName")
	if(userName){
		return '欢迎'+(Cookie.get("ddLogin.ddFirstLogin")=="1"?"您":"回来")+', <span class="name">'+(Cookie.get("ddLogin.ddName")||Cookie.get("ddLogin.ddUserName")).toHTML()+'</span>!<a href="/my/">我的个人中心</a><a href="/user/'+Cookie.get("ddLogin.ddUserName").toLowerCase()+'" target="_blank">查看我的主页</a><a id="exitLogin" href="javascript:;" onclick="logout()">退出</a>'
	}else{
		return '游客, 您好! <a href="/login.htm">注册 / 登录</a>'
	}
}
function logout(){
	var iframe=newElement("IFRAME")
	iframe.hide()
	document.body.appendChild(iframe)
	iframe.src='/do/passport.asp?action=exit'
}
function logoutComplete(){
	if($("divLoginContainer")){
		$("divLoginContainer").innerHTML=getLoginStringIndex()
		alert("您已成功退出!")
	}else if($("loginInfoContainer")){
		$("loginInfoContainer").innerHTML=getLoginString()
		alert("您已成功退出! 点击确定将为您转到首页.")
		window.location.href="/"
	}
}
function loginText(selected){
	var userName=Cookie.get("ddLogin.ddUserName")
	if(userName){
		document.write('<a href="/my/">个人中心</a>')
	}else{
		document.write('<a href="/login.htm"'+(selected?' class="selected"':'')+'>登录/注册</a>')
	}
}

function getLoginStringIndex(){
	var userName=Cookie.get("ddLogin.ddUserName")
	if(userName){
		return '<div class="frameTitle"><div><span class="selected">用户信息</span></div></div><div class="loginContainer"><table border="0" cellpadding="0" cellspacing="0" class="inforContainer"><tr><td class="postHeader"><span>欢迎'+(Cookie.get("ddLogin.ddFirstLogin")=="1"?"您":"回来")+', '+(Cookie.get("ddLogin.ddName")||Cookie.get("ddLogin.ddUserName")).toHTML()+'</span></td></tr><tr><td class="postHeader">这是您第 '+Cookie.get("ddLogin.ddLoginCount")+' 次登录 <a id="exitLogin" href="javascript:;" onclick="logout()">[ 退出 ]</a></td></tr></table></div><div class="registerContainer"><a href="/my/">个人中心</a><a href="/user/'+Cookie.get("ddLogin.ddUserName").toLowerCase()+'" target="_blank">我的主页</a><a href="/my/#go_1">我要投稿</a></div>'
	}else{
		return '<div class="frameTitle"><div><span class="selected">用户登录</span></div></div><div class="loginContainer"><form action="/do/passport.asp?action=login" method="post" name="frmLogin" id="frmLogin" target="ajaxDo" onsubmit="return doLogin()"><table border="0" cellspacing="0"><tr><td align="right"><strong>登录名:</strong></td><td><input tabindex="1" name="txtUserName" type="text" class="input" id="txtUserName" size="16" /></td><td rowspan="2"><input tabindex="3" name="btnLogin" type="submit" class="loginBtn" id="btnLogin" value="提交" /></td></tr><tr><td align="right"><strong>密　码:</strong></td><td><input name="txtPassword" tabindex="2" type="password" class="input" id="txtPassword" size="16" /></td></tr></table></form><iframe style="display: none" name="ajaxDo"></iframe></div><div class="registerContainer"><a href="/login.htm">免费注册</a><a>忘记密码</a><a href="help.htm">用户帮助</a></div>'
	}
}

function addBookmark(title,url) {
	if(!title) title="趣教网 - 免费课件、教案、论文、试卷下载"
	if(!url) url="http://www.qujiao.com/"
	if(window.sidebar){ 
		window.sidebar.addPanel(title, url, ""); 
	}else if(document.all){
		window.external.AddFavorite(url, title);
	}
}
function writeFooter(){
	document.write('<script type="text/javascript" src="/javascript/tongji.js"></script>')
}
function writeRecord(){
	document.write('粤ICP备09075268号 <a href="javascript:;" onclick="contactQQ(\'218bd439e1f3cebf50e5ea2778f53c534930c58ed58cb8402626e0647373daa4\')">链接交换</a>')
}
function layerOutWindow(){
	document.body.style.zoom="0.99";
	document.body.style.zoom="1";
}
if(Navigation.isIE6){
	window.addEventListener("resize",layerOutWindow,true)
	window.addEventListener("load",layerOutWindow,true)
}

function initRescorePage(){
	setInterval(function(){
		try{
			var height, iframe=$("iframeComment")
			if (iframe.contentDocument && iframe.contentDocument.body.scrollHeight ){
				height = iframe.contentDocument.body.scrollHeight
			}else if(iframe.Document && iframe.Document.body.scrollHeight){
				height = iframe.Document.body.scrollHeight
			}
			if(iframe.height!=height && height){
				iframe.height=height
			}
		}catch(e){}
	},10)
	var a=$("commentTab").$$("A")
	for(var i=0; i<a.length; i++){
		a[i].onclick=function(){
			for(var i=0; i<a.length; i++){
				a[i].className=""
			}
			this.className="selected"
			this.blur()
		}
	}
}

function favorite(id){
	if(!$("favoriteAjaxDo")){
		var iframe=newElement("IFRAME",{id:"favoriteAjaxDo"})
		iframe.style.display="none"
		document.body.appendChild(iframe)
	}
	$("favoriteAjaxDo").src="/do/passport.asp?action=favorite&id="+id
}

function initProfilePage(){
	setInterval(function(){
		try{
			var height, iframe=$("iframeArticle")
			if (iframe.contentDocument && iframe.contentDocument.body.scrollHeight ){
				height = iframe.contentDocument.body.scrollHeight
			}else if(iframe.Document && iframe.Document.body.scrollHeight){
				height = iframe.Document.body.scrollHeight
			}
			if(iframe.height!=height && height){
				iframe.height=height
			}
		}catch(e){}
	},10)
	setInterval(function(){
		try{
			var height, iframe=$("iframeMessage")
			if (iframe.contentDocument && iframe.contentDocument.body.scrollHeight ){
				height = iframe.contentDocument.body.scrollHeight
			}else if(iframe.Document && iframe.Document.body.scrollHeight){
				height = iframe.Document.body.scrollHeight
			}
			if(iframe.height!=height && height){
				iframe.height=height
			}
		}catch(e){}
	},10)
	var a=$("messageTab").$$("A")
	for(var i=0; i<a.length; i++){
		a[i].onclick=function(){
			for(var i=0; i<a.length; i++){
				a[i].className=""
			}
			this.className="selected"
			this.blur()
		}
	}
}
function contactQQ(sigKey){
	var ifr=$("contactQQIframe")
	if(!ifr){
		ifr=newElement("IFRAME",{id:"contactQQIframe"})
		ifr.style.display="none"
		document.body.appendChild(ifr)
	}
	ifr.src="http://sighttp.qq.com/cgi-bin/check?sigkey="+sigKey
}