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 empty (string){
	return $.trim(string) == '' ? true : false;
}
function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}
/*表单全选*/
function checkAllFormData(ids,classes){
	$("#"+ids).click(function(){
		$("."+classes).attr('checked',$("#"+ids).attr('checked'));
	});
}
/*提交表单的时候的处理*/
function checkFormSelect(formNamees,classes){
	$("form[name='"+formNamees+"']").submit(function(){
				var checked = $("."+classes).map(function(){
					if($(this).attr('checked'))return $(this).val();
				}).get().join('');
			if($.trim(checked)==''){
					alert('请选择要操作的对象');<!--请选择要操作的对象.->下同-->
					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);
	}
}

// 扩展prototype By phpwms At 2008-06-23 13:10
function trim() {
	return this.replace(/\s+$|^\s+/g,"");
}
String.prototype.trim=trim;
function getUrl(id) {
	try	{
			url = $N(id)[0].action;
		} catch (e)	{
			try	{
				url = $(id).action;
			} catch (e) {
				alert(e.description);
				return;
			}
		}
	return url;
}
function getData(urls){
	var obj = $.ajax({
		url: urls+'&rand='+Math.random(),
		async: false,
		cache:false
	});
return obj.responseText;
}
function page_function(page_dom_id,append_data_id){
		$("#"+page_dom_id).find('a').each(function(i){
			$(this).click(function(){
				$.get($(this).attr('href'),function(data){
					$("#"+append_data_id).html(data);
					return false;
				});
				return false;
			});
		});
		$("input[name='custompage']").bind('keydown',function(e){
			var k = e.keyCode;
			if(k==13){
				$("#"+append_data_id).html(getData($(this).attr('alt')+parseInt($.trim($(this).val()))));
				return false;
			}
		});
	}

function table_color(table_classes){
	$("."+table_classes).find('tr').each(function(){$(this).css({'background-color':'#FFF','cursor':'default'});$(this).hover(function(){$(this).css({'background-color':'#EFEFEF','cursor':'default'})},function(){$(this).css({'background-color':'#FFFFFF','cursor':'default'})})});
}
function checkall(type, form, value, checkall, changestyle) {
	var checkall = checkall ? checkall : 'chkall';
	for(var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if(type == 'option' && e.type == 'radio' && e.value == value && e.disabled != true) {
			e.checked = true;
		} else if(type == 'value' && e.type == 'checkbox' && e.value == value) {
			e.checked = form.elements[checkall].checked;
		} else if(type == 'prefix' && e.name && e.name != checkall && (!value || (value && e.name.match(value)))) {
			e.checked = form.elements[checkall].checked;
			if(changestyle && e.parentNode && e.parentNode.tagName.toLowerCase() == 'li') {
				e.parentNode.className = e.checked ? 'checked' : '';
			}
		}
	}
}

function fetchCheckbox(cbn) {
	return $(cbn) && $(cbn).checked == true ? 1 : 0;
}

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 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() + parseInt(seconds));
	document.cookie = escape(cookieName) 
		+ '=' + escape(cookieValue)
		+ (seconds ? '; expire=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

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 delCookie(name){
	var expires = new Date();
	expires.setTime(expires.getTime() - 10000);
	document.cookie = escape(name) + '=; expire=' + expires.toGMTString();
}

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 _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

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 Delhtml(id)//删除HTML元素比如层 表格等
{
	var obj = document.getElementById(id)
	var parentObj = obj.parentNode.removeChild(obj);
}
function show_select(){
	$('embed, object, select').css({ 'visibility' : 'visible' });	
}
function hide_select(){
	$('embed, object, select').css({ 'visibility' : 'hidden' });	
}