var sPop = null;
var postSubmited = false;
var userAgent = navigator.userAgent.toLowerCase();
var is_webtv = userAgent.indexOf('webtv') != -1;
var is_kon = userAgent.indexOf('konqueror') != -1;
var is_mac = userAgent.indexOf('mac') != -1;
var is_saf = userAgent.indexOf('applewebkit') != -1 || navigator.vendor == 'Apple Computer, Inc.';
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko' && !is_saf) && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ns = userAgent.indexOf('compatible') == -1 && userAgent.indexOf('mozilla') != -1 && !is_opera && !is_webtv && !is_saf;
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera && !is_saf && !is_webtv) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var Image_url;

function $(id) {
	return document.getElementById(id);
}
function doane(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}
function getcookie(name) {//cookie读取
	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 setcookie(cookieName, cookieValue, seconds, path, domain, secure) {//cookie写入
	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 collapse_change(menucount) {
	var skip_cookie = arguments[1]?arguments[1]:'';
	if (skip_cookie)
	{
		if($('menu_' + menucount).style.display == 'none') {
			$('menu_' + menucount).style.display = '';
			$('menuimg_' + menucount).src = Image_url+'menu_reduce.gif';
		} else {
			$('menu_' + menucount).style.display = 'none';
			$('menuimg_' + menucount).src = Image_url+'menu_add.gif';
		}	
	}
	else
	{
		var collapsed = getcookie('menu_cookie');
		if($('menu_' + menucount).style.display == 'none') {
			$('menu_' + menucount).style.display = '';
			$('menuimg_' + menucount).src = Image_url+'menu_reduce.gif';
			collapsed = collapsed.replace( menucount + '|' , '');	
		} else {
			$('menu_' + menucount).style.display = 'none';
			$('menuimg_' + menucount).src = Image_url+'menu_add.gif';
			collapsed +=  menucount + '|';
		}	
		setcookie('menu_cookie', collapsed, 2592000);
	}
}
function collapsed_remarks(menucount) {	
	if($('remarks_' + menucount).style.display == 'none') {
		$('remarks_' + menucount).style.display = '';
		$('remarksimg_' + menucount).src = Image_url+'collapsed_no.gif';
	} else {
		$('remarks_' + menucount).style.display = 'none';
		$('remarksimg_' + menucount).src = Image_url+'collapsed_yes.gif';
	}	
}
function redirect(url) {
	window.location.replace(url);
}

function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}
function zoomtextarea(objname, zoom) {
	zoomsize = zoom ? 10 : -10;
	obj = $(objname);
	if(obj.rows + zoomsize > 0 && obj.cols + zoomsize * 3 > 0) {
		obj.rows += zoomsize;
		obj.cols += zoomsize * 3;
	}
}
function collapse_input(menu_id) {
	var			Tree=new   Array;
	Tree	=	menu_exit_id[menu_id].split(",");
	if($('menu_' + menu_id).checked ==true ) 
	{		
		open_input(menu_id);
	}
	else
	{
		close_input(menu_id);
	}
}
function open_input(menu_id)
{
	var	Tree= new   Array;
	Tree	= menu_exit_id[menu_id].split(",");
	for(dd in Tree)
	{ 			
		if(Tree[dd])
		{
			$('menu_' + Tree[dd]).disabled = '';
		}
	}	
}
function close_input(menu_id)
{
	var	Tree= new   Array;
	Tree	= menu_exit_id[menu_id].split(",");
	for(dd in Tree)
	{ 			
		if(Tree[dd])
		{
			$('menu_' + Tree[dd]).checked = false;
			$('menu_' + Tree[dd]).disabled = 'disabled';
			if(menu_exit_id[Tree[dd]])
			{
				close_input(Tree[dd]);
			}
		}
	}	
}
function collapse_CheckAll(form)
{
  for (var i=0;i<form.elements.length;i++)
    {
    var e = form.elements[i];
    if (e.name != 'chkall')
		{
			if(e.type=='checkbox')
			{
				e.checked = form.chkall.checked;
				if(form.chkall.checked==false)
				{
					e.disabled = 'disabled';
					open_input(0);
					form.submit.disabled = '';
				}
				else
				{
					e.disabled = '';
				}
			}			
		}
    }
  }
function CheckAll(form)
  {
  for (var i=0;i<form.elements.length;i++)
    {
    var e = form.elements[i];
    if (e.name != 'chkall' && e.type=='checkbox')
		{
			e.checked = form.chkall.checked;			
		}
    }
  }
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

//信息弹出显示层开始
function fetchOffset(obj) {
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}
	return { 'left' : left_offset, 'top' : top_offset };
}

var jsmenu = new Array();
jsmenu['active'] = new Array();
jsmenu['timer'] = new Array();
jsmenu['iframe'] = new Array();
function details_open(ctrlid, click, offset, duration, timeout, layer, showid, maxh) {//弹出信息层
	var ctrlobj = $(ctrlid);
	if(!ctrlobj) return;
	if(isUndefined(click)) click = false;
	if(isUndefined(offset)) offset = 0;
	if(isUndefined(duration)) duration = 2;
	if(isUndefined(timeout)) timeout = 500;
	if(isUndefined(layer)) layer = 0;
	if(isUndefined(showid)) showid = ctrlid;
	var showobj = $(showid);
	var menuobj = $(showid + '_menu');
	if(!showobj|| !menuobj) return;
	if(isUndefined(maxh)) maxh = 400;
	var maxWidth = maxh + 18;			// add by jayliao @ 2008-2-19

	//alert(layer);
	hideMenu(layer);

	for(var id in jsmenu['timer']) {
		if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
	}

	initCtrl(ctrlobj, click, duration, timeout, layer);

	menuobj.style.display = '';
	if(!is_opera) {
		menuobj.style.clip = 'rect(auto, auto, auto, auto)';
	}

	var showobj_pos = fetchOffset(showobj);
	var showobj_x = showobj_pos['left'];
	var showobj_y = showobj_pos['top'];
	var showobj_w = showobj.offsetWidth;
	var showobj_h = showobj.offsetHeight;
	var menuobj_w = menuobj.offsetWidth;
	var menuobj_h = menuobj.offsetHeight;

	menuobj.style.left = (showobj_x + menuobj_w > document.documentElement.clientWidth) && (showobj_x + showobj_w - menuobj_w >= 0) ? showobj_x + showobj_w - menuobj_w + 'px' : showobj_x + 'px';
	menuobj.style.top = offset == 1 ? showobj_y + 'px' : (offset == 2 || ((showobj_y + showobj_h + menuobj_h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj_y - menuobj_h >= 0)) ? (showobj_y - menuobj_h) + 'px' : showobj_y + showobj_h + 'px');

	if(menuobj.style.clip && !is_opera) {
		menuobj.style.clip = 'rect(auto, auto, auto, auto)';
	}	


	if(maxh && menuobj.scrollHeight > maxh) {
		menuobj.style.height = maxh + 'px';
		if((is_ie || is_opera) && menuobj.style.width < maxWidth) {
			menuobj.style.width = menuobj.scrollWidth + 18;
		}
		if(is_opera) {
			menuobj.style.overflow = 'auto';
		} else {
			menuobj.style.overflowY = 'auto';
		}
	}
	//alert(menuobj.style.width);

	if(!duration) {
		setTimeout('hideMenu(' + layer + ')', timeout);
	}
	jsmenu['active'][layer] = menuobj;
}
function initCtrl(ctrlobj, click, duration, timeout, layer) {//设定定时关闭
	if(ctrlobj && !ctrlobj.initialized) {
		ctrlobj.initialized = true;

		ctrlobj.outfunc = typeof ctrlobj.onmouseout == 'function' ? ctrlobj.onmouseout : null;
		ctrlobj.onmouseout = function() {
			if(this.outfunc) this.outfunc();
			if(duration < 3) jsmenu['timer'][ctrlobj.id] = setTimeout('hideMenu(' + layer + ')', timeout);
		}

		if(click && duration) {
			ctrlobj.clickfunc = typeof ctrlobj.onclick == 'function' ? ctrlobj.onclick : null;
			ctrlobj.onclick = function (e) {
				if(jsmenu['active'][layer] == null || jsmenu['active'][layer].ctrlkey != this.id) {
					if(this.clickfunc) this.clickfunc();
					else details_open(this.id, true);
				} else {
					hideMenu(layer);
				}
			}
		}
		ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function' ? ctrlobj.onmouseover : null;
		ctrlobj.onmouseover = function(e) {
			if(this.overfunc) this.overfunc();
			if(click) {
				clearTimeout(jsmenu['timer'][this.id]);
			} else {
				for(var id in jsmenu['timer']) {
					if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
				}
			}
		}
	}
}
function hideMenu(layer) {//关闭处理
	if(isUndefined(layer)) layer = 0;
	if(jsmenu['active'][layer]) {
		clearTimeout(jsmenu['timer'][jsmenu['active'][layer].ctrlkey]);
		jsmenu['active'][layer].style.display = 'none';		
		jsmenu['active'][layer] = null;
	}
}
function details_close(ctrlid,timeout) {//离开直接关闭
	var ctrlobj = $(ctrlid);
	var menuobj = $(ctrlid + '_menu');
	hideMenu('0');
	menuobj.style.display='none';
}
////信息弹出显示层结束

//input textarea select 改变颜色
function brightBox (o)
{
	if (o)
	{
		if (document.all)
		{
			o.style.backgroundColor = "#FFFFFF";
			o.style.borderColor = "#00A8FF";
		}
	}
}
function dimBox(o)
{
	if (o)
	{
		if (document.all)
		{
			o.style.backgroundColor = "#F7F7F7";
			o.style.borderColor = "#87A34D";
		}
	}
}
function trim(str) {//字符过滤
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

////////////////////////

function arraypush(a, value) {//设置一个新值到指定数组的最后一位。数组长度自动加一。 
	a[a.length] = value;
	return a.length;
}

function in_array(needle, haystack) {//数组包括判断
	if(typeof needle == 'string') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

/*ajax开始*/	
	var xml_http_building_link = '<img src="' + Image_url + 'check_loading.gif" width="13" height="13"> 请等待，正在建立连接...';
	var xml_http_sending = '<img src="' + Image_url + 'check_loading.gif" width="13" height="13"> 请等待，正在发送数据...';
	var xml_http_loading = '<img src="' + Image_url + 'check_loading.gif" width="13" height="13"> 请等待，正在接受数据...';
	var xml_http_load_failed = '<img src="' + Image_url + 'check_error.gif" width="13" height="13"> 通信失败，请刷新重新尝试！';
	var xml_http_data_in_processed = '<img src="' + Image_url + 'check_loading.gif" width="13" height="13"> 通信成功，数据正在处理中...';

function ajaxresponse(objname, data ,css1,css2) {//ajax取值并判断
   	var x = new Ajax('XML', objname);
   	x.get('?action=ajax&' + data, function(s){
		var obj = $(objname);
        if( s.substr(0, 1) != '0') {	
			obj.style.display = '';
        	obj.innerHTML = s.substr(1)					
			if(s.substr(0, 1)>5) 
			{
				obj.className =css2;
			}
			else
			{
				obj.className = css1;
			}
        }
		else
		{						
			warning(obj, s.substr(1),css1);
		}
	});
}
 function warning(obj, msg ,css1) {//返回值错误提示
		if( msg.substr(0, 1) == '0') var check_img='check_error.gif';
		else var check_img='check_warn.gif';
		obj.className = css1;
		obj.innerHTML ='<img src="' + Image_url + check_img + '" width="13" height="13"> ' + msg.substr(1);
		obj.style.display = '';
	}
/*ajax结束*/


function show_hidden(id)
{
	if ($(''+ id +'').style.display=='none')
	{
		$(''+ id +'').style.display='block';
	}
	else
	{
		$(''+ id +'').style.display='none';
	}
	
}


function insertRow(isTable,addordel,obj)
{
	if(addordel==1)
	{
		index = $(isTable).rows.length;
		nextRow = $(isTable).insertRow(index);
		inputArea = nextRow.insertCell(0);
		buttonArea = nextRow.insertCell(1);
		index++;
		index = index.toString();
		nextRow.id = 'tr'+index;
		inputArea.innerHTML = "<input type='text' size='20' name='publish_date[]' id='publish_date"+index+"' value='' onclick='showcalendar(event, this)' onfocus='brightBox(this);' onblur='dimBox(this);' />";
		buttonArea.innerHTML = "<input type='button' id='bt"+index+"' value='删除' onclick=\"insertRow('dynTable',"+index+", this)\" >";
	}
	else
	{
		index = obj.id.replace(/[^\d]/g,'');
		var o = $('publish_date'+index);
		var b = $('bt'+index);
		var tr = $('tr'+index);

		o.removeNode(true);
		b.removeNode(true);
		tr.removeNode(true);
	}
}
