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);
}

Array.prototype.push = function(value) {
	this[this.length] = value;
	return this.length;
}

function checkall(form, prefix, checkall) {
	var checkall = checkall ? checkall : 'chkall';
	for(var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if(e.name && e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
			e.checked = form.elements[checkall].checked;
		}
	}
}

function fetchCheckbox(cbn) {
	return $(cbn) && $(cbn).checked == true ? 1 : 0;
}

function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

function setcopy(text, alertmsg){
	if(is_ie) {
		clipboardData.setData('Text', text);
		alert(alertmsg);
	} else if(prompt('Press Ctrl+C Copy to Clipboard', text)) {
		alert(alertmsg);
	}
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function mb_strlen(str) {
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == 'utf-8' ? 3 : 2) : 1;
	}
	return len;
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}


function toggle_collapse(objname, noimg) {
	var obj = $(objname);
	obj.style.display = obj.style.display == '' ? 'none' : '';
	if(!noimg) {
		var img = $(objname + '_img');
		img.src = img.src.indexOf('_yes.gif') == -1 ? img.src.replace(/_no\.gif/, '_yes\.gif') : img.src.replace(/_yes\.gif/, '_no\.gif')
	}
	var collapsed = getcookie('discuz_collapse');
	collapsed =  updatestring(collapsed, objname, !obj.style.display);
	setcookie('discuz_collapse', collapsed, (collapsed ? 86400 * 30 : -(86400 * 30 * 1000)));
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}

function redirect(url) {
	setTimeout('redirect('+url+');', 0);
}

function OpenWin(theURL,winName,winWidth,winHeight,winScroll) {
	window.open(theURL,winName,'width='+winWidth+',height='+winHeight+',left='+(window.screen.width-winWidth)/2+',top='+(window.screen.height-winHeight)/2+',scrollbars='+winScroll)
}

//Open Modal Window
function OpenWindowAndSetValue(Url,Width,Height,WindowObj,SetObj) {
	var ReturnStr=showModalDialog(Url,WindowObj,'dialogWidth:'+Width+'pt;dialogHeight:'+Height+'pt;status:yes;help:no;scroll:yes;');
	if (ReturnStr!='') SetObj.value=ReturnStr;
	return ReturnStr;
}

function OpenWindow(Url,Width,Height,WindowObj) {
	var ReturnStr=showModalDialog(Url,WindowObj,'dialogWidth:'+Width+'pt;dialogHeight:'+Height+'pt;status:no;help:no;scroll:yes;');
	return ReturnStr;
}

// ¹Ø±Õ´°¿ÚµÄº¯Êý
function CloseWin() {
	var ua=navigator.userAgent
	var ie=navigator.appName=="Microsoft Internet Explorer"?true:false
	if(ie) {
		var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE "))))
		if(IEversion< 5.5) {
			var str  = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">'
			str += '<param name="Command" value="Close"></object>';
			document.body.insertAdjacentHTML("beforeEnd", str);
			document.all.noTipClose.Click();
		} else {
			window.opener =null;
			window.close();
		}
	} else {
		window.close()
	}
}

function doZoom(target,size) {
    $(target).style.fontSize=size+'px';
}

function imageShow(img,img_w,img_h,url,target,img_zoom,img_fb,img_info) {
	if (url != '' && typeof(url) != 'undefined') {
		url = '&fo=' + url.replace(/&/g,'||');
	}
	if (target != '' && typeof(target) != 'undefined') {
		target = '&fm=' + target;
	} else {
		target = '&fm=_self';
	}
	if (img_zoom != '' && typeof(img_zoom) != 'undefined') {
		img_info = '&fs=' + img_zoom;
	}
	if (img_fb != '' && typeof(img_fb) != 'undefined') {
		img_fb = '&fb=' + img_fb;
	}
	if (img_info != '' && typeof(img_info) != 'undefined') {
		img_info = '&fi=' + img_info;
	}

	document.write('<object id="FlashShowImageObject" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+ img_w +'" height="'+ img_h +'">');
	document.write('<param name="movie" value="files/common/flash/imageshow.swf">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="FlashVars" value="fu='+ img +'&fr=t&fa=t' + img_fb +'&fia=60' + img_zoom + '&fibc=0x000000'+ target + img_info + url +'">');
	document.write('<embed src="files/common/flash/imageshow.swf" quality="high" id="FlashShowImageObject" name="FlashShowImageObject" FlashVars="fu='+ img +'&fr=t&fa=t' + img_fb +'&fia=60' + img_zoom + '&fibc=0x000000'+ target + img_info + url +'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+ img_w +'" height="'+ img_h +'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>');
	document.write('</object>');
}

function showtitle1(obja,objb){
	obja.style.backgroundColor = '#148ABC';
	obja.style.color = '#fff';
	objb.style.display = "block";
	if(currentObja != null && currentObjb != null && currentObja != obja && currentObjb != objb){
		currentObja.style.backgroundColor = '#EBEBEB';
		currentObja.style.color = '#0096CE';
		currentObjb.style.display = 'none';
	}
	currentObja = obja;
	currentObjb = objb;
}

function showtitle2(objc,objd){
	objc.style.backgroundColor = '#148ABC';
	objc.style.color = '#fff';
	objd.style.display = "block";
	if(currentObjc != null && currentObjd != null && currentObjc != objc && currentObjd != objd){
		currentObjc.style.backgroundColor = '#EBEBEB';
		currentObjc.style.color = '#0096CE';
		currentObjd.style.display = 'none';
	}
	currentObjc = objc;
	currentObjd = objd;
}

function showtitle3(obje,objf){
	obje.style.backgroundColor = '#148ABC';
	obje.style.color = '#fff';
	objf.style.display = "block";
	if(currentObje != null && currentObjf != null && currentObje != obje && currentObjf != objf){
		currentObje.style.backgroundColor = '#EBEBEB';
		currentObje.style.color = '#0096CE';
		currentObjf.style.display = 'none';
	}
	currentObje = obje;
	currentObjf = objf;
}