/*
version: 1.2.2
build : 2007-7
lastUpdate : 2007-12
*/
xpow.JSN = {
	aJsMode : 1,
	aChannel : '',
	aSyb : '',
	aSybs : '',
	aNsyb :'',
	aQs : '',
	aSybApp : '.html',
	aStatu : 'tmplate',
	aOutMode : 0,
	aDir : '\\\\{$xm}{$chn}/',
	aFilter : 'IFRAME,MARQUEE,XML,SCRIPT,CLASS',//TABLE,STYLE,OBJECT,LI,
	oPath : xpow.Use('path'),
	oRegex : xpow.Use('regexp'),
	oLetter : xpow.Use('letter'),
	oEffect : xpow.Use('effect'),
	_data : '',
	_PRE : 'c?action=',
	JSNMODE : 0,
	pathPre : '\\\\',
	queryHttp : '',
	debug : 0,
	QueryString :'',
	xmlHttp : '',
	$j : '',
	params :{quTree:'',items:''},
	setPs : function(ps){ this.aChannel = ps},
	getPs : function(){ return this.aChannel},
	setDir : function(dr){ this.aDir = dr },
	setStatu : function(st){ this.aStatu = st},
	getStatu : function(){
		if (!$(this.aStatu)){xpow.CreateElemt(xpow.m_cfg['ELEMT'].split(',')[2],'id='+this.aStatu+',display=none')}
		return this.aStatu
	},
	Init : function(d,c,jm,s,dbug,jsm){
		this.init()
		if (d)this.aDir = d
		if (!xpow.point.Lang.isUndefined(c))this.aChannel = c
		if (jm)this.aJsMode = jm
		if (s)this.aStatu = s
		if (dbug)this.debug = dbug
		if (jsm)this.JSNMODE = jsm
	},
	Create : function(){
		if (!this.aQs){	this.init()}
		this.xmlHttp = xpow.Try.these(
		  function() {return new XMLHttpRequest()},
		  function() {return new ActiveXObject('Msxml2.XMLHTTP')},
		  function() {return new ActiveXObject('Microsoft.XMLHTTP')}
		) || false;
	},
	Export : function(ismask,istitle,toffset,loffset,der,restr){
		xpow.point.setPop({mHeight:'auto',mWidth:240,PoPCss:'pop-msg',der:'~_none_',isTitle:istitle,isMask:ismask}),
		xpow.point.doPop(restr,5.1);
	},
	doParseDebug : function(str){
		if (!$('debug')){
			xpow.CreateElemt(xpow.m_cfg['ELEMT'].split(',')[2],'id=debug')
		}
		
		if ($('debug') && this.debug){
			$('debug').innerHTML += str+'<br>'	
		}
	},
	doRequestInGET : function(qu){
		this.Create()
		this.oPath.Clear()
		this.parseInitHtml()
		var tQu = qu.split(this.aQs),tPS = (tQu[0].length == 1)?this.getPs():tQu[0];
		this.QueryString = this.aDir + tPS + this.aQs + tQu[1] + '&timeStamp=' + new Date().getTime();
		this.QueryString = this.oPath.getFPath(this.QueryString)
		if (this.debug)alert('qu: '+this.QueryString);
		this.xmlHttp.onreadystatechange = this.handleStateChange;
		this.xmlHttp.open('GET', this.QueryString, true);
		this.xmlHttp.send(null)
	},
	parseDir : function(){
		this.oPath.Clear()
		this.aDir = this.oPath.getFPath(this.aDir)
	},
	parseInitHtml : function(){
		switch(parseInt(this.JSNMODE)){
		case 0 : $(this.getStatu()).innerHTML = this.oEffect.getLoadingIcon(3.2); break;
		case 1 : this.Export(0,0,0,0,0,this.oEffect.getLoadingIcon(3.2)+' 页面加载中...'); break;
		case 2 : 
		}
	},
	Send : function(qu){this.setQuery(this._PRE+qu)},
	SendX : function(qu,dta){this.sendXML(this._PRE+qu,dta)},
	setQuery : function(qu){if (this.debug){alert('setqu: '+qu)};this.doRequestInGET(qu)},
	sendXML : function(qu,dta){
		this.xmlHttp = new XPOWS.shellHTTP()
		this.parseDir()
		this.xmlHttp.setDir(this.aDir);
		this.xmlHttp.setPS(this.aChannel)
		this.xmlHttp.setMethod('POST');
		this.xmlHttp.setURL(qu);
		this.xmlHttp.parseQuery();
		this.xmlHttp.setReqHeader('application/x-www-form-urlencoded');
		this.parseXML(dta)
		this.xmlHttp.setSendData(this._data);
		this.xmlHttp.doParse(true);
		this.parseResults()
	},
	sendQuery : function(qu,dta){
		this.parseInitHtml()
		this.xmlHttp = new XPOWS.shellHTTP()
		this.xmlHttp.setDir(this.aDir);
		this.xmlHttp.setPS(this.aChannel)
		this.xmlHttp.setMethod('GET');
		this.xmlHttp.setURL(qu);
		this.xmlHttp.parseQuery();
		this.xmlHttp.setReqHeader('application/x-www-form-urlencoded');
		this.xmlHttp.setSendData(null);
		this.xmlHttp.doParse(true);
		this.parseResults()
	},
	sendJSN : function(URI,callback){
		this.$j = jQuery ||{}
		this.$j.getJSON(URI+'&jsoncallback=?',callback)
	},
	parseXML : function(dta){
		if(!dta)return
		if (dta.indexOf('[js]')>0){this._data = dta;this.parseData();dta = this._data;}
		if (dta.indexOf(this.aSyb)>0){
		var datAry = dta.split(this.aSyb),_dta = '';
		for (var i = 0; i < datAry.length;i ++){if ($(datAry[i])){_dta += datAry[i] +'='+ $(datAry[i]).value+this.aSyb}else{_dta += datAry[i] + this.aSyb;}}
		var ree = this.aFilter.split(',')
		for (var ix = 0 ; ix < ree.length ; ix ++){_dta = DecodeFilter(_dta,ree[ix])}
		/*_dta = _dta.clearHTML();*/
		if (_dta.lastIndexOf(this.aSyb)>0 ){_dta = _dta.substr(0,_dta.length-1)};_dta = xpow.URLencode(_dta)}else{_dta = xpow.URLencode(dta)}
		this._data = _dta
	},
	parseData : function(){
		var sendData = this._data
		this.oRegex.init(/\[js\]\$(.+?)\[\/js\]/ig,this._data,0)//\[js\](.+?)/*'§'*/\[\/js\]
		var nreg = this.oRegex.Match().toString()
		if(window.frames['Editor']){
		this.Editor = (!this.Editor)?window.frames['Editor'].window.frames['HtmlEditor']:this.Editor
		}
		sendData = sendData.replace(/\[js\]\$(.+?)\[\/js\]/ig,'')
		nreg = nreg.replace(/\[js\]/ig,'').replace(/\[\/js\]/ig,'')
		if (nreg != null){
			var _nreg = toResysbom(nreg,this.oLetter.get(33),"'")
			try{
				nreg = eval(_nreg)
			}catch (e){
				try {
					nreg = this.Editor.document.body.innerHTML
				}catch (e){
					nreg = this.Editor.getData()
				}
			}
		}
		sendData = toResysbom(sendData,',',this.aSyb)
		nreg = toResysbom(nreg,this.aSyb,' ')
		this._data = sendData + nreg
		/*var ree = this.aFilter.split(',')
		for (var ix = 0 ; ix < ree.length ; ix ++){
			this._data = DecodeFilter(this._data,ree[ix])
		}*/
		this._data = this.HtmlToXhtml(this._data);
	},
	HtmlToXhtml : function(str){
		str = str.replace(/<br.*?>/gi, '<br />');
		str = str.replace(/(<hr\s+[^>]*[^\/])(>)/gi, '$1 />');
		str = str.replace(/(<img\s+[^>]*[^\/])(>)/gi, '$1 />');
		str = str.replace(/(<\w+)(.*?>)/gi, function ($0,$1,$2) { return($1.toLowerCase() + this.convertAttribute($2)) } );
		str = str.replace(/(<\/\w+>)/gi, function ($0,$1) { return($1.toLowerCase()) } );
		return str;
	},
	handleStateChange : function(){
		if(xpow.JSN.xmlHttp.readyState == 4) {
			if(xpow.JSN.xmlHttp.status == 200) {xpow.JSN.parseResults()}
			else if (xpow.JSN.xmlHttp.status == 404){alert('XPOW.JSN Error (error:404): \n\nRequest Server Page Not Found!    ')}
			else if (xpow.JSN.xmlHttp.status == 500){alert('XPOW.JSN Error (error:500): \n\nRequest Server Return Error!     \n\nPlease try again!\n\n'+xpow.JSN.xmlHttp.responseText)}
		}
	},
	parseResults : function(){
		var nreg = '',tsctag=/\[script\](.+?)\[\/script\]/ig,tscstag = /\[script\](.+?)\[\/script\]/ig
		if (this.debug){alert(this.getStatu()+'#'+$(this.getStatu()))}
		if ($(this.getStatu())){
			var pScript=/<script.*?>.*?<\/script>/ig, pCss=/<link(.|\n)*\/>/ig;
			var vdata = this.xmlHttp.responseText
			if (vdata){
				this.oRegex.init(tsctag,vdata,0)
				nreg = this.oRegex.Match().toString()
			}
				
			try{
				vdata = vdata.replace(pScript,'')
			}catch(e){
				vdata = this.xmlHttp.getResponseText();
				if (vdata){
				this.oRegex.init(tscstag,vdata,0)
				nreg = this.oRegex.Match().toString()
				}
				vdata = vdata.replace(pScript,'')
			}
			//vdata = vdata.replace(pCss,'')
			vdata = vdata.replace(tscstag,'')
			nreg = nreg.replace(/\[script\]/ig,'').replace(/\[\/script\]/ig,'')
			if (this.debug){alert('nreg:'+nreg); alert('vdata:'+vdata)};
			try{$(this.getStatu()).innerHTML = vdata}catch(e){}
			if (nreg != null){var tSyb = (this.aJsMode)?';':this.aSyb;
			if (nreg.indexOf(tSyb)>0){var scAry = nreg.split(tSyb);for (var i=0; i<scAry.length;i++){if (scAry[i] != ''){try{eval(scAry[i])}catch(e){}}}}else{eval(nreg)}}
			try{if (this.JSNMODE==1)xpow.point.popClear()}catch(e){}
		}
	},
	init : function(){
		if (!isEmpty(this.aSyb)){return}
		this.aSyb = this.oLetter.get(30)
		this.aSybs = this.oLetter.get(31)
		this.aNsyb = this.oLetter.get(32,32,32)
		this.aQs = this.oLetter.get(29)
	},
	format : function(tary,tqu,sp){
		if (isEmpty(sp)){sp=','+this.aSyb+'/'}
		var tAryTree = tary.split(sp.split(this.aSyb)[0]),tAryQury = tqu.split(sp.split(this.aSyb)[1])
		var reQ = 'c?';
		for (var i=0; i<tAryTree.length; i++){
			if (tAryQury[i].indexOf('=')<=0 ){
				reQ += tAryTree[i] +'='+ tAryQury[i] + '&'
			}else{
				reQ += tAryQury[i] + '&'
			}
		}
		reQ = (reQ.lastIndexOf(this.aSybApp)>0)?reQ.substr(0,reQ.length-6):reQ.substr(0,reQ.length-1);this.setQuery(reQ)
	}
}

var Parmas = {HASE:'E|G|X|Z',PS : '',debug:0,ST:'\\\\{$xm}{$chn}/'}
function JSNinit(st,t){xpow.JSN.Init(Parmas.ST,Parmas.PS,1,st,Parmas.debug,t)}
