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 strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}

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 Xinge At 2008-06-23 13:10
function trim() {
	return this.replace(/\s+$|^\s+/g,"");
}
String.prototype.trim=trim;

function $N() {
	return document.getElementsByName(arguments[0]);
}

function $T() { // $T('input','box')
	if (arguments.length==1) {
			return document.getElementsByTagName(arguments[0]);
	} else if(arguments.length==2) {
		if (typeof(arguments[1])=="object"){
			return arguments[1].getElementsByTagName(arguments[0]);
		} else {
			return $(arguments[1]).getElementsByTagName(arguments[0]);
		}
	}
}



/* AJAX By Xinge At 2008-06-23 13:10 ----------------------start */

var debug = false; // debug          : 是否开启错误报告

// request_url    : 请求目录网址（可带参数）
// request_method : 请求方法，post/get，默认为get
// frm            : 表单名
// fun            : 自定义回调函数
// response_target: html标签id名，如<span id="response_target"></span>
// show_state     : 是否显示请求状态
function ajaxRequest(request_url,request_method,frm,fun,response_target,show_state) {   
	var pars   = '';
	var method = request_method;
	var url    = request_url;
	var target = response_target;
	
	if (typeof(show_state) == 'undefined' || typeof(show_state) !='boolean') {
		show_state = true;		
	}

	if (!url) {
		url = getUrl(frm);
	}

	if (method == 'post') {
		if(typeof(frm) == 'undefined' || !frm) {
			pars = Form.serialize(document.forms[0]);
		} else {
			pars = Form.serialize(frm);
		}
	} else {
		method = 'get';
	}

	var myAjax = new Ajax.Request(   
		url,{
			method     : method,
			parameters : pars,
			onLoading  : loading(target,show_state),
			onComplete : function(obj) {
							showResponse(obj,target,fun);
						},
			onFailure  : reportError,
			evalScripts: true
			}   
	);
}

function getUrl(id) {
	try	{
			url = $N(id)[0].action;
		} catch (e)	{
			try	{
				url = $(id).action;
			} catch (e) {
				alert(e.description);
				return;
			}
		}
	return url;
}

function loading(target,show_state) {
	if (target)	{
		try	{
			if (show_state)	
				$(target).innerHTML = '<img src="'+root+'files/common/image/loading.gif" alt="loading..." align="absmiddle"> 数据载入中...';
		} catch (e) {
			if (debug == true) 
				alert(e.description);
		}
	}
}

function showResponse(obj,target,fun) {
	var result = obj.responseText;

	if (fun) {
		try	{
			eval(fun + "(result,target)");
		} catch (e)	{
			if (debug == true) 
				alert(e.description);
		}
	} else {
		if (target)	{
			try	{
				$(target).innerHTML = result;
			} catch (e) {
				if (debug == true) 
					alert(e.description);
			}
		}
	}
}

function reportError(request) {
	alert('Sorry. There was an error.');
}
/* AJAX By Xinge At 2008-06-23 13:10 ----------------------end */

Array.prototype.push = function(value) {
	this[this.length] = value;
	return this.length;
}

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 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('phpwms_collapse');
	collapsed =  updatestring(collapsed, objname, !obj.style.display);
	setCookie('phpwms_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 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 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>');
}

/*JS 读取GET值*/
function getUrlParameter(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");  
	var r	= window.location.search.substr(1).match(reg);  
	if (r!=null) {
		return unescape(r[2]);
	} else {
		return null;
	}
}

function digg(channel_type,type,data_id) {
	var name = 'digg_'+channel_type+'_'+type+'_'+data_id;
	var ding = 'digg_'+channel_type+'_d_'+data_id;
	var cai  = 'digg_'+channel_type+'_c_'+data_id;
	if (!getCookie(ding) && !getCookie(cai)) {
		setCookie(name,'1',3600);
		ajaxRequest(root+'include/digg.php?m='+channel_type+'&t='+type+'&id='+data_id,'get','','diggAlert',type);
	} else {
		alert('您已经投过票了，请勿重复提交！');
	}
}

function diggAlert(data,type) {
	if (data == 1) {
		if (type == 'd') {
			$('ding_num').innerHTML = parseInt($('ding_num').value)+1;
		} else {
			$('cai_num').innerHTML = parseInt($('cai_num').value)+1;
		}
		alert('感谢参与，谢谢！');
	} else {
		alert('提交失败，请联系管理员！');
	}
}

function getHits(id,type) {
	//location.href=root+'include/hits.php?id='+id+'&type='+type;
	ajaxRequest(root+'include/hits.php?id='+id+'&type='+type,'get','','','hits',false);	
}

/*参数说明:n为标签编号,
	   m为标签数量,
	   q为标签名称的前缀，如id=tb_1中的"tb_",
	   p为内容层的前缀,如id=tbc_01的"tbc_0"
*/
function HoverLi(n,m,q,p){
	var nid = n-1;
	if (nid == 0) {
		$("tab_more").innerHTML = "<a href=\""+root+"?article-1.html\">更多</a>";
	} else {
		$("tab_more").innerHTML = "<a href=\""+root+"?article-1-"+nid+".html\">更多</a>";
	}
	for(var i=1;i<=m;i++) {
		$(q+i).className='normaltab';
		$(p+i).className='undis';
	}
	$(p+n).className='dis';
	$(q+n).className='hovertab';
}

function setTab(m,n) {
	if (n == 0) $("more1").innerHTML = "<a href=\""+root+"?picture-6-53.html\">更多</a>";
	if (n == 1) $("more1").innerHTML = "<a href=\""+root+"?picture-6-54.html\">更多</a>";
	if (n == 2) $("more1").innerHTML = "<a href=\""+root+"?picture-6-55.html\">更多</a>";
	var menu=$("tab"+m).getElementsByTagName("li");
	var div=$("tablist"+m).getElementsByTagName("div");
	var showdiv=[];
	for (i=0; j=div[i]; i++){
		if ((" "+div[i].className+" ").indexOf(" tablist ")!=-1){
			showdiv.push(div[i]);
		}
	}
	for(i=0;i<menu.length;i++) {
		menu[i].className=i==n?"now":"";
		showdiv[i].style.display=i==n?"block":"none";
	}
}