/**
 * @author: trs-ldr
 * @date:	06-08-14
 */
function getContentByUrl2(url, caller){
	/*var timeStamp = "";

	if(url.indexOf("?") == -1)
		timeStamp = "?"
	else
		timeStamp = "&";
	*/
	//timeStamp = timeStamp + "timeStamp=" + new Date().getTime(); + timeStamp,
	getContentByUrl(url, caller);
}
function getContentByUrl(url, caller){
	/*var timeStamp = "";

	if(url.indexOf("?") == -1)
		timeStamp = "?"
	else
		timeStamp = "&";
	*/
	//timeStamp = timeStamp + "timeStamp=" + new Date().getTime();
	if(isIE5()){
		if(!net.ContentLoader)
			return;
		var myAjax = new net.ContentLoader(url, function(){caller.parseResponse(this.req)});
	}
	else {
		var myAjax = new Ajax.Request(url,
			{
				method:'get',parameters:'',onComplete:function(xhr){
					caller.parseResponse(xhr);
				}			
			}
		);
	}
}//return the status type of a xmlHttpRequest object
function isIE5() {
	if(getIEVersonNumber() >= 5 && getIEVersonNumber() < 6)
		return true;
	return false;
}
function getIEVersonNumber(){
    var ua = navigator.userAgent;
    var msieOffset = ua.indexOf("MSIE ");
    if(msieOffset < 0){
        return 0;
    }
    return parseFloat(ua.substring(msieOffset + 5, ua.indexOf(";", msieOffset)));
}
//if the status is 404, then the type is 4
function getStatusType(xhr){
	return Math.floor(xhr.status / 100);
}
//fill the container width method innerHTML
//showError can be true or false
function TextParser(url, target, showError){
	this.target =target;
	this.showError = showError;
	getContentByUrl2(url, this);
}
TextParser.prototype.parseResponse = function(xhr){
	var xhrStatus = getStatusType(xhr);
	if( xhrStatus == 4 || xhrStatus == 5)
		if(typeof(this.showError) == "undefined" || !this.showError){
			return;
		}

	var text =  xhr.responseText;
	if ( navigator.appVersion.indexOf( "KHTML" ) > -1 ) {
		var esc = escape( text );
		if ( esc.indexOf("%u") < 0 && esc.indexOf("%") > -1 ) {
			text = decodeURIComponent( esc );
		}
	}
	
	setInnerHTML(document.getElementById(this.target), text);
	//setInnerHTML(document.getElementById(this.target), xhr.responseText);
	//document.getElementById(this.target).innerHTML = xhr.responseText;
}
//fill the container with method value, mostly towards textarea
function TextParserOfValue(url, target, showError){
	this.target =target;
	this.showError = showError;
	getContentByUrl(url, this);
}
TextParserOfValue.prototype.parseResponse = function(xhr){
	var xhrStatus = getStatusType(xhr);
	var textarea = document.getElementById(this.target);

	if( xhrStatus == 4 || xhrStatus == 5)
		if(typeof(this.showError) == "undefined" || !this.showError){
			return;
		}
	if(textarea.tagName != "textarea" && textarea.tagName != "TEXTAREA")
		setInnerHTML(textarea ,xhr.responseText);
		//textarea.innerHTML = xhr.responseText;
	else
		textarea.value = xhr.responseText;
	if(typeof(this.showError) != "undefined"){
		this.showError();	
	}
}
//fill the container with method innerText, when in FF ,use innerHTML instead
function TextParserOfText(url, target, showError){
	this.target =target;
	this.showError = showError;
	getContentByUrl2(url, this);
}
TextParserOfText.prototype.parseResponse = function(xhr){
	var xhrStatus = getStatusType(xhr);

	if( xhrStatus == 4 || xhrStatus == 5)
		if(typeof(this.showError) == "undefined" || !this.showError){
			return;
		}
	if(!document.all)
		document.getElementById(this.target).innerHTML = xhr.responseText;
	else
		document.getElementById(this.target).innerText = xhr.responseText;
		
}


//add by ldr,2006-09-13
//call onkeydown of the text, use when press enter
function sumitChange(preffix, evt){
	var ev = window.event? window.event : evt;
	//when press enter
	if(ev.keyCode == 13)
		changePageByNum(preffix, evt);
}

// get the img's parent's node,and list its children
// find the input node,and get its value
function changePageByNum(preffix, evt){
	var fromObj = getObjByEvent(evt);
	var parent = fromObj.parentNode;
	var nodes = parent.childNodes;
	var node = null;

	//parse when press enter
/*	if(typeof(fromObj) != "undefined" && fromObj.tagName.toLowerCase() == "input"){
		if(isNotSearchNum(fromObj.value))
			return false;
		doChange(preffix, fromObj.value);
		return false;
	}
*/
	for(var i=0; i<nodes.length; i+=1){
		node = nodes[i];
		if(node.tagName != null && node.tagName.toLowerCase()=="input" && node.className=="input1"){
			if(isNotSearchNum(node.value))
				return false;
			doChange(preffix, node.value);
			return false;
		}
	}
}
function doChange(preffix, str){
	window.location.href= preffix + str;
}
function isNotSearchNum(value){
	if(isNaN(value) || value == ""){//is not a number
		alert("请输入一个数字\n Please input a Number!");
		return true;
	}

	var num = parseInt(value);
	var max = parseInt($("pageCountNum").innerHTML);
	if(num > max || num < 1){//not in the range
		alert("请输入 1 至 " + max + " 之间的数字\nPleae input a Number between 1 and " + max);
		return true;
	}
	return false;
}

//function to get the object by event
function getObjByEvent(ev){
	if(document.all){
		return window.event.srcElement;
	}
	return ev.target;
}
function getValue(node){
	if(node.childNodes[0] == null || node.childNodes[0].childNodes[0] != null)
		return "null";
	return node.childNodes[0].nodeValue;
}
//*********************** new XML Parser  ***************************
//实现对tag的按名访问功能
function XParser(url, func, con_id){
	this.url = url ;
	this.xmlDoc = null;
	this.state = 0; //start
	this.func = func;
	var caller = this;
	this.res = [];
	if(typeof(con_id) != "undefined")
		this.container_id = con_id;
	
	var caller = this;
	var myAjax = new Ajax.Request(url,
		{
			method:'get',parameters:'',onComplete:function(xhr){
				caller.parseResponse(xhr);
			}
		}
	);
}
XParser.prototype.parseResponse = function(xhr){
	this.xmlDoc =  xhr.responseXML;
	this.state = xhr.status; //success
	//this.get("ITEM");
	delete xhr;

	this.func();
}
//XParser.prototype.setParent = function(parent){
//	this.parent = parent;
//}
XParser.prototype.get = function(tagName){
	if(typeof(tagName) == "undefined" || tagName == ""){
		alert("Please input the right tagname");
		return false;
	}
	this.tagName = tagName;
	
	var resState = this.getStatusType(this.state);
	if(resState == 4 || resState == 5){
		this.result = "fail";
		return "抱歉，没有找到指定的XML文档！<!-- state:" + this.state + " -->";
	}
	if(this.xmlDoc == null){
		//idle(tagName);
		//shrink(this);
		this.result = "fail";
		return "抱歉，没有找到指定的XML文档！<!-- state:" + this.state + " -->";
	}

	this.result = "sucess";
	return this.parseNode();
}
//使用完后清理数据
XParser.prototype.close = function(){
	delete this.xmlDoc;
	delete this.func;
}
//标记名要小写
XParser.prototype.parseNode = function(){
	var nodes = this.xmlDoc.getElementsByTagName(this.tagName);
	var node = null;
	var res = new Array();

	if(nodes.length == 1){
		res[0] = this.getChildValue(nodes[0]);
		delete node;
		delete nodes;
		this.close();
		return res;
	}

	for(var i = 0; i < nodes.length; i+=1){
		node = nodes[i];

		res[i] = this.getChildValue(node);
		//alert(this.getChildValue(node));
	}
	delete node;
	delete nodes;
	return res;
	//this.res[this.tagName.toLowerCase()] = nodes[0].childNodes[0].tagName;
}
XParser.prototype.getValue = function(node){
	if(node.childNodes[0] == null || node.childNodes[0].childNodes[0] != null)
		return "null";
	return node.childNodes[0].nodeValue;
}
XParser.prototype.getChildValue = function(root){
	var nodes = root.childNodes;
	var node = null;
	var res =  new Array();

	for(var i = 0 ; i < nodes.length ; i+=1){
		node = nodes[i];
		//alert(node.tagName.toLowerCase() + " " + this.getValue(node));
		res[node.tagName] = this.getValue(node);
	}
	var child = new Children(res);
	return child;
}
XParser.prototype.getStatusType = function(state){
	return Math.floor(state / 100);
}
//将数据绑定到一个表格上
//targetid is the id of tbody
XParser.prototype.bind = function(source, targetId, colsSelected, colsStyle){
	var tbody = document.getElementById(targetId);
	var select = colsSelected.split(/\s*,\s*/);
	var style = colsStyle.split(/\s*,\s*/);
	var tds = tbody.getElementsByTagName("td");
	var size = tds.length;
	var node = null;
	var tr = null;
	//var td = null;
	var j = 0;

	for(var i = 0; i < source.length; i += 1){
		node = source[i];
		tr = document.createElement("tr");
		tbody.appendChild(tr);
		var td = null;
		for( j = 0; j < size; j += 1){
			td = document.createElement("td");
			tr.appendChild(td);
			td.style.cssText = style[j];
			td.appendChild(document.createTextNode(node.get(select[j])));
		}
	}

	delete souce;
	delete targetid;
	delete colsSelected;
}
/******************************   Class Children    **********************************/
function Children(res){
	this.res = res;
}
Children.prototype.get = function(str){
	if(str == "")
		return "";
	return this.res[str];
}

function getParameter(_sName){
			 if(_sName ==null ||_sName=='undefined')
			  return '';
			 var query=location.search;
			 var aParams=query.replace(/\?/,'').parseQuery();

			 for(var sParam in aParams){
			  if(_sName.toUpperCase()==sParam.toUpperCase()){
			   return aParams[sParam];
			  }
			 }
			 return '';
		}
//used in console/board/include/innerTableView.jsp innerTreeView.jsp
function showContent(id, ev, url){
	var evt= window.event ? window.event:ev;
	var obj = document.getElementById(id);

	obj.style.display = "block";

	obj.style.left = evt.clientX + 14 + document.body.scrollLeft;
	obj.style.top = evt.clientY - evt.offsetY + 30 + document.body.scrollTop; //evt.clientY - evt.offsetY + 30
	obj.innerHTML = "loading...";
	new TextParserOfText(url, id);
}
function showContentOnly(id, ev){
	var evt= window.event ? window.event:ev;
	var obj = document.getElementById(id);

	obj.style.display = "block";

	obj.style.left = evt.clientX + 14 + document.body.scrollLeft;
	obj.style.top = evt.clientY - evt.offsetY + 30 + document.body.scrollTop; //evt.clientY - evt.offsetY + 30
}
function hideContent(id){
	var obj = document.getElementById(id);

	obj.style.display = "none";
}

//new ForumLogin().loginClient(function(){}) ;

/******************************   Forum Login   **********************************/
/*function ForumLogin(userName, password, loginUrl){
	this.userName = userName;
	this.password = password;
	this.loginUrl = loginUrl;
	this.params = 'userName=' + userName + '&password=' + password + "&timeStamp=" + new Date().getTime();
}

ForumLogin.prototype.loginClient = function(callback){
	var url = this.loginUrl + '?' + this.params ;
	//alert("login to client, params is:" + url) ;
	new Ajax.Request( url, {method: "POST", parameters:null, onComplete: function(request){
			callback(request.responseText)
		}
		} );
}

ForumLogin.prototype.loginConsole = function(callback){
	var url = this.loginUrl + '?console=1' + "&" + this.params ;
	new Ajax.Request( url, {method: "POST", parameters:null, onComplete: function(request){
			callback(request.responseText)
		}
		} );
}


function ForumLogout(logoutUrl){
	this.logoutUrl = logoutUrl + "?timeStamp=" + new Date().getTime();;
}

ForumLogout.prototype.logoutClient = function(callback){
	var url = this.logoutUrl  ;
	new Ajax.Request( url, {method: "get", parameters:null, onComplete: function(request){
			callback(request.responseText)
		}
		} );
}

ForumLogout.prototype.logoutConsole = function(callback){
	var url = this.logoutUrl + '?console=1' ;
	new Ajax.Request( url, {method: "get", parameters:null, onComplete: function(request){
			callback(request.responseText)
		}
		} );
}


*/




//错误提示页面3秒定时返回
function autoReturn(url){
	if(typeof(url) == "undefined")
		setTimeout("goBack()", 3000);
	else{
		setTimeout("goByUrl('" + url + "')", 3000);
	}
}
function goBack(){
	window.history.go(-1);
}
function goByUrl(url){
	window.location = url;
}
//自动调整图片的宽度
//obj为图片对象
function ajectToRect(obj){
	var img = new Image();
	var MAX_WIDTH = 740;
	img.src = obj.src;

	if(img.width > MAX_WIDTH){
		obj.width = MAX_WIDTH;
		obj.height = (img.height/ img.width) * MAX_WIDTH;
	}
}
function $destroy(_obj){
  if(_obj == null) return;
  for(var param in _obj){
      try{
         if(_obj[param] == null) continue;
         delete _obj[param];
         _obj[param]=null;
       }catch(err){
        // just skip it
       }
   }
}
function setInnerHTML(el, htmlCode) {
     var ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf('msie') >= 0 && ua.indexOf('opera') < 0) {
          htmlCode = '<div style="display:none">for IE</div>' + htmlCode;
          htmlCode = htmlCode.replace(/<script([^>]*)>/gi,'<script$1 defer>');
          el.innerHTML = '';
          el.innerHTML = htmlCode;
          el.removeChild(el.firstChild);
      } else {
          var el_next = el.nextSibling;
          var el_parent = el.parentNode;
          el_parent.removeChild(el);
          el.innerHTML = htmlCode;
          if (el_next) {
          el_parent.insertBefore(el, el_next)
          } else {
              el_parent.appendChild(el);
          }
     }
}