function set_var(vars)
{
	return typeof(vars)!='undefined'&&vars!=''&&vars!=0&&vars!=false ? vars : '';
}
function is_element(id)
{
	return document.getElementById(id) ? 1 : 0;
}
function _G(id)
{
	return document.getElementById(id);
}

function redirect(url)
{
	window.location.replace(url);
}

function get_ext(path)
{
	return path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
}

function is_image(file)
{
	return $.inArray(get_ext(file),['jpg','gif','png','bmp','jpeg'])!=-1;
}

function preview(src,str,w,e)
{
	e = e || window.event;
	if(!w)	var w=400;
	setXY('img_preview',w,null,e)
	_G('img_preview').style.display = (src || str) ? '' : 'none';
	$('#img_preview').empty();
	if(!is_image(get_ext(src)))	return;
	if(src)	$('#img_preview').html(insert_img(decodeURIComponent(src),w));
	if(str)
	{
		$('#img_preview').append('<p Style="width:'+w+'px;overflow:hidden;text-align:left;line-height:18px">'+(decodeURIComponent(str).replace(/\+/g,' '))+'</p>');
	}
}

function check_all(id,same)
{
	if(same)
	{
		(id ? $('#'+id+' input[type="checkbox"]') : $('form input[type="checkbox"]')).each(function(){$(this).attr('checked',(same ? true : false))});
	}
	else
	{
		(id ? $('#'+id+' input[type="checkbox"][name!="chkall"]') : $('form input[type="checkbox"][name!="chkall"]')).each(function(){$(this).attr('checked',($(this).attr('checked') ? false : true))});
	}
}
//jquery下显示采用display:block，这导致在火狐下的tbody格式乱套了
function show_hide(id)
{
	return _G(id).style.display = _G(id).style.display=='none' ? '' : 'none';
}
//删除指定节点
function $RN(id)
{
	if(is_element(id))	_G(id).parentNode.removeChild(_G(id));
}
function $CE(tagName)
{
    return document.createElement(tagName);
}
function $RC(father,son)
{
	return _G(father).removeChild(_G(son)); 
}

if(typeof(decode)!='function')
{
	function decode(str)
	{
		return decodeURIComponent(str).replace(/\+/g,' ');
	}
}

function set_tr_class(id)
{
	if(!id && is_element("mytable"))	id = 'mytable';
	if(!$('#'+id+' tr'))	return false;
	$("#"+id+" tr:odd[class!='title'][class!='tfoot']").addClass("altbg1");
	$("#"+id+" tr:even[class!='title'][class!='tfoot']").addClass("altbg2");
	var cl;
	$("#"+id+' tr').each(function(k){
		$(this).hover(
			function()
			{
				$(this).addClass("altbg3");
			},
			function ()
			{
				$(this).removeClass("altbg3");
			}); 
	});
}
function set_td_class(id)
{
	if(!id && is_element("mytable"))	id = 'mytable';
	if(!$('#'+id+' td'))	return false;
	$('#'+id+' td[class!="tfoot"][class!="header"]').each(function(k){
		if($(this).attr('colspan')>1)	return true;
		if(k%2==0)	$(this).addClass('subject');
	});
}
//在ie6下,select的级别最高，通常情况下所有弹出的窗口总是在选择框之下
function toggle_select()
{
	$('select').each(function(){$(this).toggle()});
}

function insert_img(path,maxwh){
	var str = '<img src="' + path + '" ';
	var img = new Image();
	img.src = path;
	if(img.width>maxwh || img.height>maxwh){
		str += ((img.width/img.height)>1 ? 'width' : 'height') + '="' + maxwh + '" ';
	}
	str += '/>';
	return str;
}
function get_wh(is_width)
{
	var height = width = null;
	if(is_width)
	{
		if (window.innerWidth) width = window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth) width = document.documentElement.clientWidth;
		else if (document.body && document.body.clientWidth) width = document.body.clientWidth;
		return width;
	}
	else
	{
		if (window.innerHeight) height = window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight) height = document.documentElement.clientHeight;
		else if (document.body && document.body.clientHeight) height = document.body.clientHeight;
		return height;
	}
}
function setXY(id,w,h,e) {
	if(_G(id)) {
		divElement = _G(id);
	} else {
		var divElement = document.createElement("DIV");
		divElement.id = id;
		document.body.appendChild(divElement);
	}
	divElement.className = "ajaxdiv";
	divElement.style.cssText = "width:"+w+"px;";
	divElement.style.cssText = "height:"+(h?h:w)+'px;';
	divElement.style.position = 'absolute';
		
	var offX = 4;
	var offY = 4;
	var width = 0;
	var height = 0;
	var scrollX = 0;
	var scrollY = 0;  
	var x = 0;
	var y = 0;
		
	if (window.innerWidth) width = window.innerWidth - 18;
	else if (document.documentElement && document.documentElement.clientWidth) 
		width = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth) 
		width = document.body.clientWidth;
	
	if (window.innerHeight) height = window.innerHeight - 18;
	else if (document.documentElement && document.documentElement.clientHeight) 
		height = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight) 
		height = document.body.clientHeight;

	if (typeof window.pageXOffset == "number") scrollX = window.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft)
		scrollX = document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft) 
		scrollX = document.body.scrollLeft; 
	else if (window.scrollX) scrollX = window.scrollX;
	  
	if (typeof window.pageYOffset == "number") scrollY = window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		scrollY = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		scrollY = document.body.scrollTop; 
	else if (window.scrollY) scrollY = window.scrollY;
		
	x=e.pageX?e.pageX:e.clientX+scrollX;
	y=e.pageY?e.pageY:e.clientY+scrollY;

	if(x+divElement.offsetWidth+offX>width+scrollX){
		x=x-divElement.offsetWidth-offX;
		if(x<0)x=0;
	}else x=x+offX;
	if(y+divElement.offsetHeight+offY>height+scrollY){
		y=y-divElement.offsetHeight-offY;
		if(y<scrollY)y=height+scrollY-divElement.offsetHeight;
	}else y=y+offY;

	divElement.style.left = x+"px";
	divElement.style.top = y+"px";
}

function top_window()
{
	var tw = window.parent;
	if(window==tw)	return window;
	while(tw && tw.parent!=tw)
	{
		try
		{
			if ( tw.parent.document.domain != document.domain )	break ;
			if ( tw.parent.document.getElementsByTagName( 'frameset' ).length > 0 )	break ;
		}
		catch ( e )	{break ;}
		tw = tw.parent ;
	}
	return tw;
}
//顶层的载入图片
function show_loading(id)
{
	top_window().$('#loading').show();
	if(id)	top_window().$('#loading').html('<img src="'+site_root+'skin/img/default/loading'+id+'.gif" align="absmiddle" />');
}
function hide_loading()
{
	top_window().$('#loading').hide();
}
function big_loading(msg)
{
	var id = 'big_loading_div';
	if(!msg)	msg = '数据载入中...';
	if(!is_element(id))	$('body').append('<div id="'+id+'"></div>');
	$('#'+id).css({left:(get_wh(1)/2-225)+'px',top:(get_wh()/3+72)+'px'}).show().html('<span>'+msg+'</span>');
}
function hide_big_loading()
{
	$('#big_loading_div').empty().hide();
}

function show_msg(msg,time)
{
	top_window().$('#message').show().html('<p><span class="icon"></span>'+msg+'</p>');
	if(time)	hide_msg(time);
}
function hide_msg(time)
{
	if(time)
	{
		setTimeout(function(){top_window().$('#message').empty().hide();},time);
	}
	else	top_window().$('#message').empty().hide();
}

//把内容添加到左边的菜单快捷操作
function add_quite_operation(id)
{
	if(!is_element(id))	return false;
	return top_window().$('#quick_operation').html($('#'+id).html());
}

function is_check(id)
{
	var c = 0;
	id = id ? '#'+id : 'form';
	$(id+' input[type="checkbox"][name!="chkall"]').each(function(){if($(this).attr('checked')){c=1;return false;}});
	return c;
}
function datepicker()
{
	$(function() {
		$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['zh-CN']));
		$('.datepicker').datepicker({
			//numberOfMonths: 2,
			showButtonPanel: true,
			altFormat:'yy-mm-dd',
			changeMonth: true,
			changeYear: true,
			showOn: 'button', buttonImage: site_root+'stuff/img/icon/calendar.gif', buttonImageOnly: true
		});
	});
}

function png()
{
	if(!$.browser.msie) return;
	var vs = parseInt((navigator.userAgent.match(/msie\s+(\d+)/i))[1]);
	if(vs > 6)	return;
	$('img').each(
		function()
		{
			if(get_ext($(this).attr('src'))=='png')
			{
				$(this).css({'filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+$(this).attr('src')+")",'width':$(this).attr('width')+'px','height':$(this).attr('height')+'px'});
				$(this).attr('src',site_root+'stuff/img/admin/none.gif');
				
			}
		});
}

function make_select(arr,name,selectid,ext,first_option)
{
	var s = '<select name="'+name+'" '+(ext?ext:'')+'>'+(first_option?first_option:'');
	$.each(arr,function(i,n)
	{
		s += '<option value='+n+(n==selectid ? ' selected':'')+'>'+i+'</option>';
	});
	s += '</select>';
	return s;
}
function show_tip(id)
{
	$('#'+id).dialog('destroy').dialog({
		title:"小贴士",
		bgiframe: true,
		resizable:false,
		autoOpen: false,
		width:500
	}).dialog('open');
}
//根据图片的宽度等比例缩放某张图片
function resize_img(obj,w)
{
	var img_w = obj.width;	//图片的宽度
	var img_h = obj.height;	//图片的高度
	if(w && img_w>w)
	{
		obj.style.width = w+'px';
		obj.style.height = w/img_w*img_h+'px';
		obj.alt += ' [点击图片在新窗口打开]';
		obj.onclick = function(){window.open(this.src);}
		obj.ondblclick = function(){obj.style.display="none";}
		obj.style.cursor = 'pointer';
		//obj.align = 'right';
		return;
	}
}