/**************************************************
*                                                 *
* Author  : Windy_sk                              *
* Create  : 2003-05-03                            *
* Modified: 2004-1-9                              *
* Email   : windy_sk@126.com                      *
* HomePage: None (Maybe Soon)                     *
* Notice  : U Can Use & Modify it freely,         *
*           BUT PLEASE HOLD THIS ITEM.            *
*                                                 *
**************************************************/

//if(this.top.location.href.replace(/^(http:\/\/[^\/]+).*$/i,"$1")!=this.location.href.replace(/^(http:\/\/[^\/]+).*$/i,"$1")) top.location.href = location.href;

function reportError(msg, url, line) {
	var str = "You have found an error as below: \n\n";
	str += "Err: " + msg + "on line: " + line;
	//alert(str);
	return true;
}

window.onerror = reportError;

if(typeof(HTMLElement)!="undefined") {
	HTMLElement.prototype.__defineGetter__
	(
	   "innerText",function()
	   //define a getter method to get the value of innerText,
	   //so you can read it now!
	   {
	      var textRange = this.ownerDocument.createRange();
	      //Using range to retrieve the content of the object
	      textRange.selectNodeContents(this);
	      //only get the content of the object node
	      return textRange.toString();
	      // give innerText the value of the node content
	   }
	);
}

String.prototype.Tlength = function() {
	var arr=this.match(/[^\x00-\xff]/ig);
	return this.length+(arr==null?0:arr.length);
}

Date.prototype.format = function(format){
/*
eg:format="YYYY-MM-dd hh:mm:ss";
*/
 	var o = {
		"M+" :  this.getMonth()+1,  //month
		"d+" :  this.getDate(),     //day
		"h+" :  this.getHours(),    //hour
		"m+" :  this.getMinutes(),  //minute
		"s+" :  this.getSeconds(), //second
		"q+" :  Math.floor((this.getMonth()+3)/3),  //quarter
		"S"  :  this.getMilliseconds() //millisecond
	}
	if(/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
		}
	}
	return format;
}

Array.prototype.copy = function() {
	var copy = new Array();
	for (var i = 0; i < this.length; i++) {
		copy[i] = this[i];
	}
	return copy;
}

Array.prototype.concat = function (secondArray) {
	var firstArray = this.copy();
	if(typeof(secondArray.length)=="undefined" || secondArray.length==0) {
		firstArray[firstArray.length] = secondArray;
	} else {
		for (var i = 0; i < secondArray.length; i++) {
			firstArray[firstArray.length] = secondArray[i];
		}
	}
	return firstArray;
}

var load_function = new Array();
window.onload = function() {
	for(var i=0; i<load_function.length; i++) {
		if(typeof(load_function[i])=="function") load_function[i]();
	}
}

//load functions while page loaded as below
function loadTest() {
	//alert("Page Loaded!");
}
load_function = load_function.concat(loadTest);

function checkBrowser() {
	this.ver = navigator.appVersion;
	this.dom = document.getElementById ? true : false;
	this.ie7 = (this.ver.indexOf("MSIE 7")>-1 && this.dom) ? true : false;
	this.ie6 = (this.ver.indexOf("MSIE 6")>-1 && this.dom) ? true : false;
	this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom) ? true : false;
	this.ie4 = (document.all && !this.dom) ? true : false;
	this.ns5 = (this.dom && parseInt(this.ver) >= 5 && navigator.appName=="Netscape")  ? true : false;
	this.ns4 = (document.layers && !this.dom) ? true : false;
	this.ff  =  (navigator.userAgent.toLowerCase().indexOf("firefox") != - 1) ? true : false;
	this.saf = (navigator.userAgent.toLowerCase().indexOf("safari") != - 1) ? true : false;
	this.ope = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
	this.mac = (this.ver.indexOf('Mac') != -1) ? true : false;
	this.ie = (this.ie7 || this.ie6 || this.ie5 || this.ie4);
	this.ns = (this.ns4 || this.ns5 || this.ff);
	this.bw = (this.ie || this.ns || this.mac || this.saf || this.ope);
	this.nbw = (!this.bw);
	this.Moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined');
	return this;
}

var checker = checkBrowser();

function Get_Pos(the_obj){
	the_top = the_obj.offsetTop;
	the_left = the_obj.offsetLeft;
	while(the_obj=the_obj.offsetParent){
		the_top += the_obj.offsetTop;
		the_left += the_obj.offsetLeft;
	}
	this.top = the_top;
	this.left = the_left;
}

function Get_Obj_2(the_area, the_tag, att_name, att_value){
	var obj_list = new Array();
	var all_Obj = the_area.tags(the_tag);
	if(att_name!=undefined && att_value!=undefined){
		for(var i=0;i<all_Obj.length;i++){
			if(all_Obj[i][att_name].toLowerCase()==att_value.toLowerCase())
				obj_list[obj_list.length]=all_Obj[i];
		}
		return(obj_list);
	}else{
		return(all_Obj);
	}
}

function Get_Obj(the_area){
	var obj_list = new Array();
	var args = Get_Obj.arguments;
	for(var i=1; i<args.length; i++) {
		try {
			obj_list = obj_list.concat(the_area.getElementsByTagName(args[i]));
		} catch(e) {
			obj_list = obj_list.concat(Get_Obj_single(the_area, args[i]));
		}
	}
	return(obj_list);
}

function Get_Obj_single(the_area, tag_name){
	var obj_list = new Array();
	var obj_all = the_area.childNodes;
	for(var i=0; i<obj_all.length; i++) {
		if(typeof(obj_all[i].tagName)=="undefined") continue;
		if(obj_all[i].tagName.toLowerCase()==tag_name.toLowerCase()) {
			obj_list = obj_list.concat(obj_all[i]);
		}
	}
	return(obj_list);
}

function OpenDialog(url,width,height,mode) {
	var sOrnaments = "dialogWidth:"+width+"px;dialogHeight:"+height+"px;center:1;dialogLeft:200;dialogTop:100;dialogHide:0;edge:raised;help:0;resizable:0;scroll:0;status:0;unadorned:0;center:1;";
	var win = null;
	if(checker.ie) {
		if(mode){
			win = window.showModalDialog(url, window, sOrnaments);
		}else{
			win = window.showModelessDialog(url, window, sOrnaments);
		}
	} else {
		win = OpenWindow(url,width,height);
	}
	return win;
}

function OpenWindow(url,width,height) {
	var win = window.open(url, "showIt","height="+height+", width="+width+", top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no, modal=yes");
	return win;
}

function sleep(the_time) {
	var over_time = new Date(new Date().getTime() + the_time);
	while(over_time > new Date()) {}
}

function removeTextNode(xml_obj) {
	if(typeof(xml_obj.childNodes)!="undefined") {
		for(var i=0; i<xml_obj.childNodes.length; i++) {
			if(xml_obj.childNodes[i].nodeType==3) {
				xml_obj.removeChild(xml_obj.childNodes[i]);
				i--;
			}
		}
	}
	return xml_obj;
}

function gotoAnchor(theAnchor) {
	var the_url = location.href;
	if(theAnchor==null) {
		location.href = the_url + "#";
	} else {
		theAnchor = "#" + theAnchor;
		location.href = the_url.replace(/#+\w*$/, "") + theAnchor;
	}
	return false;
}

var curHighLight = null;
var curNav = null;

function nav_mouseover(obj) {
	var theObj = obj.parentNode;
	if(curHighLight == theObj) return;

	var theText = obj.innerHTML;
	if(curHighLight != null) {
		curHighLight.style.backgroundColor="";
		curHighLight.firstChild.style.color="#FFFFFF";
		//if(theObj!=theObj.parentNode.firstChild) theObj.previousSibling.style.backgroundColor=="";
		//if(theObj!=theObj.parentNode.lastChild) theObj.nextSibling.style.backgroundColor=="";
	}
	theObj.style.backgroundColor="#FFFFFF";
	theObj.firstChild.style.color="#0066CC";
	//if(theObj!=theObj.parentNode.firstChild) theObj.previousSibling.style.backgroundColor=="#FFFFFF";
	//if(theObj!=theObj.parentNode.lastChild) theObj.nextSibling.style.backgroundColor=="#FFFFFF";
	curHighLight = theObj;

	if(curNav != null) {
		curNav.style.display = "none";
	} else {
		document.getElementById("sub_nav").style.display = "none";
	}
	if(theText=="首页") {
		curNav = document.getElementById("sub_nav");
	} else if(theText=="论坛") {
		curNav = document.getElementById("sub_nav_forum");
	} else {
		curNav = document.getElementById("sub_nav_" + theText);
	}
	curNav.style.display = "";
	return;
}

function nav_set(catalog) {
	if(catalog==null) catalog = "";
	var obj = document.getElementById("main_nav_"+catalog);
	if(obj==null) obj = document.getElementById("main_nav");
	nav_mouseover(obj);
	return;
}

function copyStr(txt, msg) {
	try {
		window.clipboardData.setData("text",txt);
		alert(msg);
	} catch(e) {
		alert("浏览器不支持此功能!");
	}
	return;
}

function regCheck(theForm) {
	var flag = true;
	if(theForm.password.value != theForm.password_2.value) {
		alert("两次密码输入不同！");
		theForm.password.select();
		flag = false;
	}
	return flag;
}

function buildPageList(page, show) {
	if(page==null) page = 1;
	if(show==null) show = 6;
	var obj = document.getElementById("page_list");
	obj.innerHTML = "";
	if(page_count<=1) return;
	var page_start = 0;
	var page_end = page_count;
	var page_more_start = '';
	var page_more_end = '';
	
	if(page_count<=show+2) {
		if(page_count>1) {
			page_start = 2;
			page_end = page_count;
		}
	} else if(page<show) {
		page_start = 2;
		page_end = show + 1;
		page_more_end = '…… ';
	} else if(page+show>page_count) {
		page_start = page_count - show;
		page_end = page_count;
		page_more_start = '…… ';
	} else {
		page_start = page - show/2;
		page_end = page + show/2 + 1;
		page_more_start = '…… ';
		page_more_end = '…… ';
	}

	var list = "";
	if(page==1) {
		list += '<em>上一页</em> ';
	} else {
		list += '<a href="###" onclick="gotoPage(' + (page-1) + ')">上一页</a> ';
	}
	list += (page==1 ? '<strong>1</strong> ' : '<a href="###" onclick="gotoPage(1)">1</a> ');
	list += page_more_start;
	
	for(var i=page_start; i<page_end; i++) {
		if(page==i) {
			list += '<strong>' + i + '</strong> ';
		} else {
			list += '<a href="###" onclick="gotoPage(' + i + ')">' + i + '</a> ';
		}
	}
	
	list += page_more_end;
	list += (page==page_count ? '<strong>'+page_count+'</strong> ' : '<a href="###" onclick="gotoPage('+page_count+')">'+page_count+'</a> ');
	if(page==page_count) {
		list += '<em>下一页</em> ';
	} else {
		list += '<a href="###" onclick="gotoPage(' + (page+1) + ')">下一页</a> ';
	}
	obj.innerHTML = list;
	return;
}

function gotoPage(page) {
	var the_url = location.href.replace(/#+.*?$/, "");
	if(url_rewrite) {
		if(the_url.lastIndexOf("/")+1==the_url.length) {
			the_url += "index_" + page + T_ext;
		} else if(the_url.indexOf("_")==-1) {
			the_url = the_url.replace(/^(.+?)(\.\w+)$/, (page==1?"$1$2":("$1_"+page+"$2")));
		} else {
			the_url = the_url.replace(/^(.+?)_\d+(\.\w+)$/, (page==1?"$1$2":"$1_"+page+"$2"));
		}
	} else {
		if(the_url.indexOf("page")==-1) {
			the_url += (the_url.indexOf("?")==-1 ? "?" : "&") + "page=" + page;
		} else {
			the_url = the_url.replace(/page=\d+/i, "page="+page);
		}
	}
	location.replace(the_url);
	return;
}

function loginCheck() {
	//ajax code for mark
	myAjax.init("TEXT", loginCheckDone, loadingStart, loadingEnd);
	myAjax.get(web_url+"/ajax.php?job=login_check", "");
}

function loginCheckDone(content) {
	if(content.length>50) document.getElementById("user_info").innerHTML = content;
}

function layerShow(obj) {
	obj.style.display = "";
	if(checker.ie) {
		obj.style.top = ((document.documentElement.offsetHeight - obj.offsetHeight)/2 + document.documentElement.scrollTop) + "px";
		obj.style.left = ((document.documentElement.offsetWidth - obj.offsetWidth)/2 + document.documentElement.scrollLeft) + "px";
	} else {
		obj.style.top = ((window.innerHeight - obj.offsetHeight)/2 + document.documentElement.scrollTop) + "px";
		obj.style.left = ((window.innerWidth - obj.offsetWidth)/2 + document.documentElement.scrollLeft) + "px";
	}
}

function login() {
	var theForm = document.getElementById("form_login");
	if(theForm.user_name.value=="") {
		alert("请输入用户名称！");
		theForm.user_name.focus();
	} else if(theForm.user_psw.value=="") {
		alert("请输入用户密码！");
		theForm.user_psw.focus();
	} else if(theForm.check_code.value=="") {
		alert("请输入验证码，如不清楚，请点击图片更换！");
		theForm.check_code.focus();
	} else {
		theForm.return_url.value = location.href;
		//ajax code for mark
		myAjax.init("TEXT", loginDone, loadingStart, loadingEnd);
		myAjax.post_form(web_url+"/ajax.php?job=login", theForm);
	}
	return false;
}

function loginDone(errCode) {
	var theForm = document.getElementById("form_login");
	errCode = parseInt(errCode);
	switch(errCode) {
		case 1:
			alert("登录错误，错误的用户名或密码！");
			theForm.user_name.value = "";
			theForm.user_psw.value = "";
			theForm.check_code.value = "";
			theForm.user_name.focus();
			document.getElementById('vcode').src=web_url+'/vcode.php?'+Math.random();
			break;
		case 2:
			alert("校验码不正确，请重新输入！");
			theForm.check_code.value = "";
			theForm.check_code.focus();
			document.getElementById('vcode').src=web_url+'/vcode.php?'+Math.random();
			break;
		default:
			alert("登录成功！");
			theForm.user_name.value = "";
			theForm.user_psw.value = "";
			theForm.check_code.value = "";
			document.getElementById('login').style.display="none";
			loginCheck();
	}
}
