﻿// JavaScript Document
var STYLEID = '1', IMGDIR = 'images', VERHASH = 'WS0', charset = 'utf-8', attackevasive = '0', allowfloatwin = '1';
	//FloatWin
	var hiddenobj = new Array();
	var floatwinhandle = new Array();
	var floatscripthandle = new Array();
	var floattabs = new Array();
	var floatwins = new Array();
	var InFloat = '';
	var floatwinreset = 0;
	var floatwinopened = 0;
	
	var userAgent = navigator.userAgent.toLowerCase();
	var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
	var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
	var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
	
	function $(id) {
		return document.getElementById(id);
	}
	
	var cssloaded= new Array();
	function loadcss(cssname) {
		if(!cssloaded[cssname]) {
			css = document.createElement('link');
			css.type = 'text/css';
			css.rel = 'stylesheet';
			css.href = '/Cms/images/style_' + STYLEID + '_' + cssname + '.css?' + VERHASH;
			var headNode = document.getElementsByTagName("head")[0];
			headNode.appendChild(css);
			cssloaded[cssname] = 1;
		}
	}
	
	function floatwin(action, script, w, h, scrollpos) {
		var floatonly = !floatonly ? 0 : 1;
		var actione = action.split('_');
		action = actione[0];
		
		if((!allowfloatwin || allowfloatwin == 0) && action == 'open' && in_array(actione[1], ['register','login','newthread','reply','edit']) && w >= 600){
			location.href = script;
			return;
		}
		
		var handlekey = actione[1];
		var layerid = 'floatwin_' + handlekey;
		
		if(is_ie) {
			var objs = $('wrapper').getElementsByTagName("OBJECT");
		} else {
			var objs = $('wrapper').getElementsByTagName("EMBED");
		}
		
		if(action == 'open') {
			loadcss('float');
			floatwinhandle[handlekey + '_0'] = layerid;
			if(!floatwinopened) {
				$('wrapper').onkeydown = floatwin_wrapkeyhandle;
				for(i = 0;i < objs.length; i ++) {
					if(objs[i].style.visibility != 'hidden') {
						objs[i].setAttribute("oldvisibility", objs[i].style.visibility);
						objs[i].style.visibility = 'hidden';
					}
				}
			}
			scrollpos = !scrollpos ? '' : 'floatwin_scroll(\'' + scrollpos + '\');';
			var clientWidth = document.body.clientWidth;
			var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
			var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
			
			if(script && script != -1) {
				//if(script.lastIndexOf('/') != -1) {
				//	script = script.substr(script.lastIndexOf('/') + 1);
				//}
				var scriptfile = script.split('?');
				
				scriptfile = scriptfile[0];
				
				if(floatwinreset || floatscripthandle[scriptfile] && floatscripthandle[scriptfile][0] != script) {
					if(!isUndefined(floatscripthandle[scriptfile])) {
						$('append').removeChild($(floatscripthandle[scriptfile][1]));
						$('append').removeChild($(floatscripthandle[scriptfile][1] + '_mask'));
					}
					floatwinreset = 0;
				}
				
				floatscripthandle[scriptfile] = [script, layerid];
			}

			if(!$(layerid)) {
				floattabs[layerid] = new Array();
				div = document.createElement('div');
				div.className = 'floatwin';
				div.id = layerid;
				div.style.width = w + 'px';
				div.style.height = h + 'px';
				div.style.left = floatwinhandle[handlekey + '_1'] = ((clientWidth - w) / 2) + 'px';
				div.style.position = 'absolute';
				div.style.zIndex = '999';
				div.onkeydown = floatwin_keyhandle;
				$('append').appendChild(div);
				$(layerid).style.display = '';
				$(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((clientHeight - h) / 2 + scrollTop) + 'px';
				
				$(layerid).innerHTML = '<div><h3 class="float_ctrl"><em><img src="' + IMGDIR + '/loading.gif"> {lang loading}...</em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&nbsp</a></span></h3></div>';
				divmask = document.createElement('div');
				divmask.className = 'floatwinmask';
				divmask.id = layerid + '_mask';
				divmask.style.width = (parseInt($(layerid).style.width) + 14) + 'px';
				divmask.style.height = (parseInt($(layerid).style.height) + 14) + 'px';
				divmask.style.left = (parseInt($(layerid).style.left) - 6) + 'px';
				divmask.style.top = (parseInt($(layerid).style.top) - 6) + 'px';
				divmask.style.position = 'absolute';
				divmask.style.zIndex = '998';
				divmask.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=90,finishOpacity=100,style=0)';
				divmask.style.opacity = 0.9;
	
				$('append').appendChild(divmask);
				
				if(script && script != -1) {
					script += (script.search(/\?/) > 0 ? '&' : '?') + 'infloat=yes&handlekey=' + handlekey;
					try {
						ajaxget(script, layerid, '', '', '', scrollpos);
					} catch(e) {
						setTimeout("ajaxget('" + script + "', '" + layerid + "', '', '', '', '" + scrollpos + "')", 1000);
					}
				} else if(script == -1) {
					$(layerid).innerHTML = '<div><h3 class="float_ctrl"><em id="' + layerid + '_title"></em><span><a href="javascript:;" class="float_close" onclick="floatwinreset = 1;floatwin(\'close_' + handlekey + '\');">&nbsp</a></span></h3></div><div id="' + layerid + '_content"></div>';
					$(layerid).style.zIndex = '1099';
					$(layerid + '_mask').style.zIndex = '1098';
				}
			} else {
				$(layerid).style.width = w + 'px';
				$(layerid).style.height = h + 'px';
				$(layerid).style.display = '';
				$(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((clientHeight - h) / 2 + scrollTop) + 'px';
				$(layerid + '_mask').style.width = (parseInt($(layerid).style.width) + 14) + 'px';
				$(layerid + '_mask').style.height = (parseInt($(layerid).style.height) + 14) + 'px';
				$(layerid + '_mask').style.display = '';
				$(layerid + '_mask').style.top = (parseInt($(layerid).style.top) - 6) + 'px';
			}
			floatwins[floatwinopened] = handlekey;
			floatwinopened++;
		} else if(action == 'close' && floatwinhandle[handlekey + '_0']) {
			floatwinopened--;
			for(i = 0;i < floatwins.length; i++) {
				if(handlekey == floatwins[i]) {
					floatwins[i] = null;
				}
			}
			if(!floatwinopened) {
				for(i = 0;i < objs.length; i ++) {
					if(objs[i].attributes['oldvisibility']) {
						objs[i].style.visibility = objs[i].attributes['oldvisibility'].nodeValue;
						objs[i].removeAttribute('oldvisibility');
					}
				}
				$('wrapper').onkeydown = null;
			}
			hiddenobj = new Array();
			$(layerid + '_mask').style.display = 'none';
			$(layerid).style.display = 'none';
		} else if(action == 'size' && floatwinhandle[handlekey + '_0']) {
			if(!floatwinhandle[handlekey + '_3']) {
				var clientWidth = document.body.clientWidth;
				var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
				var w = clientWidth > 800 ? clientWidth * 0.9 : 800;
				var h = clientHeight * 0.9;
				floatwinhandle[handlekey + '_3'] = $(layerid).style.left;
				floatwinhandle[handlekey + '_4'] = $(layerid).style.top;
				floatwinhandle[handlekey + '_5'] = $(layerid).style.width;
				floatwinhandle[handlekey + '_6'] = $(layerid).style.height;
				$(layerid).style.left = floatwinhandle[handlekey + '_1'] = ((clientWidth - w) / 2) + 'px';
				$(layerid).style.top = floatwinhandle[handlekey + '_2'] = ((document.documentElement.clientHeight - h) / 2 + document.documentElement.scrollTop) + 'px';
				$(layerid).style.width = w + 'px';
				$(layerid).style.height = h + 'px';
			} else {
				$(layerid).style.left = floatwinhandle[handlekey + '_1'] = floatwinhandle[handlekey + '_3'];
				$(layerid).style.top = floatwinhandle[handlekey + '_2'] = floatwinhandle[handlekey + '_4'];
				$(layerid).style.width = floatwinhandle[handlekey + '_5'];
				$(layerid).style.height = floatwinhandle[handlekey + '_6'];
				floatwinhandle[handlekey + '_3'] = '';
			}
			$(layerid + '_mask').style.width = (parseInt($(layerid).style.width) + 14) + 'px';
			$(layerid + '_mask').style.height = (parseInt($(layerid).style.height) + 14) + 'px';
			$(layerid + '_mask').style.left = (parseInt($(layerid).style.left) - 6) + 'px';
			$(layerid + '_mask').style.top = (parseInt($(layerid).style.top) - 6) + 'px';
		}
	}
	
	function floatwin_scroll(pos) {
		var pose = pos.split(',');
		try {
			pagescroll.defaultleft = pose[0];
			pagescroll.defaulttop = pose[1];
			pagescroll.init();
		} catch(e) {}
	}
	
	function floatwin_wrapkeyhandle(e) {
		e = is_ie ? event : e;
		if(e.keyCode == 9) {
			doane(e);
		} else if(e.keyCode == 27) {
			for(i = floatwins.length - 1;i >= 0; i--) {
				floatwin('close_' + floatwins[i]);
			}
		}
	}
	
	function floatwin_keyhandle(e) {
		e = is_ie ? event : e;
		if(e.keyCode == 9) {
			doane(e);
			var obj = is_ie ? e.srcElement : e.target;
			var srcobj = obj;
			j = 0;
			while(obj.className.indexOf('floatbox') == -1) {
				obj = obj.parentNode;
			}
			obj.id = obj.id ? obj.id : 'floatbox_' + Math.random();
			if(!floattabs[obj.id]) {
				floattabs[obj.id] = new Array();
				var alls = obj.getElementsByTagName("*");
				for(i = 0;i < alls.length;i++) {
					if(alls[i].getAttribute('tabindex') == 1) {
						floattabs[obj.id][j] = alls[i];
						j++;
					}
				}
			}
			if(floattabs[obj.id].length > 0) {
				for(i = 0;i < floattabs[obj.id].length;i++) {
					if(srcobj == floattabs[obj.id][i]) {
						j = e.shiftKey ? i - 1 : i + 1;break;
					}
				}
				if(j < 0) {
					j = floattabs[obj.id].length - 1;
				}
				if(j > floattabs[obj.id].length - 1) {
					j = 0;
				}
				do{
					focusok = 1;
					try{ floattabs[obj.id][j].focus(); } catch(e) {
						focusok = 0;
					}
					if(!focusok) {
						j = e.shiftKey ? j - 1 : j + 1;
						if(j < 0) {
							j = floattabs[obj.id].length - 1;
						}
						if(j > floattabs[obj.id].length - 1) {
							j = 0;
						}
					}
				} while(!focusok);
			}
		}
	}


	function doane(event) {
		e = event ? event : window.event;
		if(is_ie) {
			e.returnValue = false;
			e.cancelBubble = true;
		} else if(e) {
			e.stopPropagation();
			e.preventDefault();
		}
	}
	
//ajax函数	
	var Ajaxs = new Array();
	function Ajax(waitId) {
		var aj = new Object();
		aj.waitId = waitId ? $(waitId) : null;
		aj.targetUrl = '';
		aj.sendString = '';
		aj.resultHandle = null;
		aj.loading = '<img src="image/common/loading.gif" style="margin: 3px; vertical-align: middle" />Loading... ';
		aj.createXMLHttpRequest = function() {
			var request = false;
			if(window.XMLHttpRequest) {
				request = new XMLHttpRequest();
				if(request.overrideMimeType) request.overrideMimeType('text/xml');
			} else if(window.ActiveXObject) {
				var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
				for(var i=0; i<versions.length; i++) {
					try {
						request = new ActiveXObject(versions[i]);
						if(request) return request;
					} catch(e) {/*alert(e.message);*/}
				}
			}
			return request;
		}
	
		aj.request = aj.createXMLHttpRequest();
	
		if(aj.waitId) {
			aj.waitId.orgdisplay = aj.waitId.style.display;
			aj.waitId.style.display = '';
			aj.waitId.innerHTML = aj.loading;
		}
	
		aj.processHandle = function() {
			if(aj.request.readyState == 4 && aj.request.status == 200) {
				for(k in Ajaxs) {
					if(Ajaxs[k] == aj.targetUrl) Ajaxs[k] = null;
				}
				if(aj.waitId) {
					aj.waitId.style.display = 'none';
					aj.waitId.style.display = aj.waitId.orgdisplay;
				}
				aj.resultHandle(aj.request.responseXML.lastChild.firstChild.nodeValue);
			}
		}
	
		aj.get = function(targetUrl, resultHandle) {
			if(in_array(targetUrl, Ajaxs)) {
				return false;
			} else {
				Ajaxs.push(targetUrl);
			}
	
			aj.targetUrl = targetUrl;
			aj.request.onreadystatechange = aj.processHandle;
			aj.resultHandle = resultHandle;
			if(window.XMLHttpRequest) {
				aj.request.open('GET', aj.targetUrl);
				aj.request.send(null);
			} else {
					aj.request.open("GET", targetUrl, true);
					aj.request.send();
			}
		}
	
	/*	aj.post = function(targetUrl, sendString, resultHandle) {
			aj.targetUrl = targetUrl;
			aj.sendString = sendString;
			aj.request.onreadystatechange = aj.processHandle;
			aj.resultHandle = resultHandle;
			aj.request.open('POST', targetUrl);
			aj.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			aj.request.send(aj.sendString);
		}*/
		return aj;
	}
	
	function show(id, display) {
		if(display == 'auto') {
			$(id).style.display = $(id).style.display == '' ? 'none' : '';
		} else {
			$(id).style.display = display;
		}
	}
	
	/*
		ajaxget('www.baidu.com', 'showid', 'waitid', 'display(\'showid\', 1)');
	*/
	function ajaxget(url, showId, waitId, display, recall) {
		e = is_ie ? event : ajaxget.caller.arguments[0];
		ajaxget2(e, url, showId, waitId, display, recall);
	}
	
	function ajaxget2(e, url, showId, waitId, display, recall) {
		target = e ? (is_ie ? e.srcElement : e.target) : null;
		display = display ? display : '';
		var x = new Ajax(waitId);
		x.showId = showId;
		x.display = display;
		var sep = url.indexOf('?') != -1 ? '&' : '?';
		x.target = target;
		x.recall = recall;
		x.get(url+sep+'inajax=1', function(s) {
			if(x.display == 'auto' && x.target) {
				x.target.onclick = newfunc('show', x.showId, 'auto');
			}
			show(x.showId, x.display);
			$(x.showId).innerHTML = s;
			evalscript(s);
			if(x.recall)eval(x.recall);
		});

	}
	
	/*
	function stripscript(s) {
		return s.replace(/<script.*?>.*?<\/script>/ig, '');
	}*/
	
	var evalscripts = new Array();
	function evalscript(s) {
		if(!s || s.indexOf('<script') == -1) return s;
		var p = /<script[^\>]*?src=\"([^\x00]+?)\"[^\>]*( reload=\"1\")?><\/script>/ig;
		var arr = new Array();
		while(arr = p.exec(s)) appendscript(arr[1], '', arr[2]);
		p = /<script[^\>]*?( reload=\"1\")?>([^\x00]+?)<\/script>/ig;
		while(arr = p.exec(s)) appendscript('', arr[2], arr[1]);
		return s;
	}
	
	function appendscript(src, text, reload) {
		var id = hash(src + text);
		if(!reload && in_array(id, evalscripts)) return;
		if(reload && $(id)) {
			$(id).parentNode.removeChild($(id));
		}
		evalscripts.push(id);
		var scriptNode = document.createElement("script");
		scriptNode.type = "text/javascript";
		scriptNode.id = id;
		if(src) {
			scriptNode.src = src;
		} else if(text){
			scriptNode.text = text;
		}
		$('append').appendChild(scriptNode);
	}
	
	// 得到一個定長的 hash 值， 依賴於 stringxor()
	function hash(string, length) {
		var length = length ? length : 32;
		var start = 0;
		var i = 0;
		var result = '';
		filllen = length - string.length % length;
		for(i = 0; i < filllen; i++){
			string += "0";
		}
		while(start < string.length) {
			result = stringxor(result, string.substr(start, length));
			start += length;
		}
		return result;
	}
	
	function stringxor(s1, s2) {
		var s = '';
		var hash = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		var max = Math.max(s1.length, s2.length);
		for(var i=0; i<max; i++) {
			var k = s1.charCodeAt(i) ^ s2.charCodeAt(i);
			s += hash.charAt(k % 52);
		}
		return s;
	}
	
	function in_array(needle, haystack) {
		for(var i in haystack) 	{if(haystack[i] == needle) return true;}
		return false;
	}
	
	function newfunc(func){
		var args = new Array();
		for(var i=1; i<arguments.length; i++) args.push(arguments[i]);
		return function(e){
			window[func].apply(window, args);
			_cancelBubble(is_ie ? event : e);
		}
	}
	
	function ajaxmenu(url, position) {
		e = is_ie ? event : ajaxmenu.caller.arguments[0];
		controlid = is_ie ? e.srcElement : e.target;
		var menuid = hash(url);// 使每個 url 對應一個彈出層，避免重複請求
		createmenu(menuid);
	
		showmenu2(e, menuid, position, controlid);
		if(!$(menuid).innerHTML) {
			ajaxget2(e, url, menuid, menuid, '', "setposition('" + menuid + "', '" + position + "', '" + controlid + "')");
		} else {
			//alert(menuid.innerHTML);
		}
		_cancelBubble(e);
	}
	
	var ajaxpostHandle = null;
	function ajaxpost(formid, showid, recall) {
		var ajaxframeid = 'ajaxframe';
		var ajaxframe = $(ajaxframeid);
		if(ajaxframe == null) {
			if (is_ie) {
				ajaxframe = document.createElement("<iframe name='" + ajaxframeid + "' id='" + ajaxframeid + "'></iframe>");
			} else {
				ajaxframe = document.createElement("iframe");
				ajaxframe.name = ajaxframeid;
				ajaxframe.id = ajaxframeid;
			}
			ajaxframe.style.display = 'none';
			$('append').appendChild(ajaxframe);
		}
		$(formid).target = ajaxframeid;
		ajaxpostHandle = [formid, showid, ajaxframeid, recall];
		_attachEvent(ajaxframe, 'load', ajaxpost_load);
		$(formid).submit();
		return false;
	}
	
	function ajaxpost_load() {
		var s = (is_ie && $(ajaxpostHandle[2])) ? $(ajaxpostHandle[2]).contentWindow.document.XMLDocument.text : $(ajaxpostHandle[2]).contentWindow.document.documentElement.firstChild.nodeValue;
		evalscript(s);
		if(s) {
	//		setMenuPosition($(ajaxpostHandle[0]).ctrlid, 0);
			$(ajaxpostHandle[1]).innerHTML = s;
			if(ajaxpostHandle[3]) {
				eval(ajaxpostHandle[3]);
			}
	//		setTimeout("hideMenu()", 3000);
		}
	//	$(ajaxpostHandle[2]).target = ajaxpostHandle[3];
	//	ajaxpostHandle = null;
	}	
	var http_request = false;
function AjaxInit(){
	http_request = false;
	if(window.XMLHttpRequest){
		//Mozilla 浏览器
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType){
			//设置MiME类别
		   http_request.overrideMimeType("text/xml");
		}
	}
	else if (window.ActiveXObject){
		// IE浏览器
		try{
		   http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
			  http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e){}
		}
	}              
	if (!http_request){
		 window.alert("您的系统不支持后台提交的方式.");
		 return false;
	}
	else
		return true;
}
function getURL(method,url,parameter){
	var result;
	if(AjaxInit()){
		http_request.onreadystatechange = function(){
			if(http_request.readyState == 4){
				if(http_request.status == 200){
					//对返回的数据进行处理
					result = http_request.responseText;
				}
			}
		};
		http_request.open(method, url, false);
		if(parameter=='')
			http_request.send(null);
		else
			http_request.send(parameter);
		
	}
	return result;
}

function ajaxinnerhtml(showid, s) {
	if(showid.tagName != 'TBODY') {
		showid.innerHTML = s;
	} else {
		while(showid.firstChild) {
			showid.firstChild.parentNode.removeChild(showid.firstChild);
		}
		var div1 = document.createElement('DIV');
		div1.id = showid.id+'_div';
		div1.innerHTML = '<table><tbody id="'+showid.id+'_tbody">'+s+'</tbody></table>';
		$('append_parent').appendChild(div1);
		var trs = div1.getElementsByTagName('TR');
		var l = trs.length;
		for(var i=0; i<l; i++) {
			showid.appendChild(trs[0]);
		}
		var inputs = div1.getElementsByTagName('INPUT');
		var l = inputs.length;
		for(var i=0; i<l; i++) {
			showid.appendChild(inputs[0]);
		}
		div1.parentNode.removeChild(div1);
	}
}
