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 S(a,d){try{return(d||window).document.getElementById(a);}catch(c){return null;}}
var gIEVer="";
if(is_ie){
	var reIE=new RegExp("MSIE (\\d+\\.\\d+);","i");
	reIE.test(navigator.userAgent);
	gIEVer=parseFloat(RegExp["$1"]);
}

function addClass(ob, cc) {
	if(ob) {
		var c = ob.className;
		if(c == "") {
			ob.className = cc;
			return;
		}
		var a = c.split(c);
		var l = a.length;
		var has_c = false;
		for(var i = 0; i < l; i++) {
			if(a[i] == cc) {
				has_c = true;
				break;
			}
		}
		
		if(!has_c) {
			ob.className += " " + cc;
		}
	}
}

function removeClass(ob, cc) {
	if(ob) {
		var c = ob.className;
		if(c == "") {
			return;
		}
		var a = c.split(" ");
		var l = a.length;
		var has_c = false;
		for(var i = 0; i < l; i++) {
			if(a[i] == cc) {
				has_c = true;
				break;
			}
		}

		if(has_c) {
			a.splice(i, 1);
			ob.className = a.join(" ");
		}
	}
}

function hasClass(ob, className) {
	if(ob) {
		var E=ob.className;
		if(E.length==0){
			return false;
		}
		if(E==className||E.match(new RegExp("(^|\\s)"+className+"(\\s|$)"))){
			return true;
		}
		return false
	}
	return false;
}


//缩放
function resizeImage(img, width) {
	 var image=new Image(); 
	 image.src=img.src; 
	 var temp = image.width; 
	 img.width = temp = (temp>width)?width:temp; 
	 img.style.display = "inline"; 
}

function resizeImageInIE6(img, width) {
	if(gIEVer == "6") {
		//ie 6中，css中没有max-width属性，所以用此方法代替下
		resizeImage(img, width);
	}
}


function showError(text) {
	if($("show_info")) {
		$("show_info").style.display = "none";
		$("e_info").className = "r2";
		$("e_info").innerHTML = text;
		$("show_info").style.display = "";
		go_top();
		setTimeout('$("show_info").style.display = "none"', 3000);
	}
}

function showSuccess(text) {
	if($("show_info")) {
		$("show_info").style.display = "none";
		$("e_info").className = "r2_2";
		$("e_info").innerHTML = text;
		$("show_info").style.display = "";
		setTimeout('$("show_info").style.display = "none"', 3000);
	}
}

function go_top() {
	document.documentElement.scrollTop  = 0;
}

function getscrollTop(){
	if(typeof window.pageYOffset!='undefined'){
		return window.pageYOffset;
	}else if(typeof document.compatMode!='undefined'&&document.compatMode!='BackCompat'){
		return document.documentElement.scrollTop;
	}else if(typeof document.body!='undefined'){
		return document.body.scrollTop
	}
}

function get_post_par() {
	var e = document.getElementsByName("pp[]");
	var l = e.length;
	var s = "";

	for(var i = 0; i < l; i++) {
		if(e[i].checked) {
			s += e[i].value + ",";
		}
	}
	return s;
}


function Trim(sStr) {
	return sStr.replace(/(^\s*)|(\s*$)/ig,"");
}


function shop_is_number(val) {
	var reg = /^[\d|\.|,]+$/;
	return reg.test(val);
}

function rowindex(tr)
{
  if (Browser.isIE)
  {
    return tr.rowIndex;
  }
  else
  {
    table = tr.parentNode.parentNode;
    for (i = 0; i < table.rows.length; i ++ )
    {
      if (table.rows[i] == tr)
      {
        return i;
      }
    }
  }
}

function can_only_digital(a, zs){
	pre="";
	v=a.value;
	kc=parseInt(v);
	if(isNaN(kc)){
		kc="";
	}
	kc=pre+kc;
	if(zs && kc.substring(0, 1) == "0") {
		kc = "";
	}
	a.value=kc;
}

String.prototype.ReplaceAll = function(stringToFind,stringToReplace) {
        var result = this;
        var index = result.indexOf(stringToFind);
        while(index != -1){
                result = result.replace(stringToFind,stringToReplace);
                index = result.indexOf(stringToFind);
        }
        return result;
}

function showFailDialog(a){alert(a);}

function resizeImage(c,d){var e=new Image();e.src=c.src;var a=e.width;c.width=a=(a>d)?d:a;c.style.display="inline";}function resizeImageInIE6(a,c){resizeImage(a,c);}

function fetchOffset(d){
	var c=d.offsetLeft;
	//alert(c);
	var a=d.offsetTop;
	while((d=d.offsetParent)!=null){
		c+=d.offsetLeft;
		a+=d.offsetTop;
	}
	return{left:c,top:a};
}
function isUndefined(a){return typeof a=="undefined"?true:false;}

function setMenuPosition(a,d,c, top_s, left_s){
	if(isUndefined(c)){
		c=0;
	}
	if(a){
		if(c == 0) {
			a.pos=fetchOffset(a);
			//alert(a.pos.left);
			a.X=a.pos.left;
			a.Y=a.pos.top;
			a.w=a.offsetWidth;
			a.h=a.offsetHeight;
			d.w=d.offsetWidth;
			d.h=d.offsetHeight;
			
			if(c == 0){
				var ml = (a.X+d.w>document.documentElement.clientWidth)&&(a.X+a.w-d.w>=0)?a.X+a.w-d.w:a.X;

				if(typeof left_s != "undefined") {
					ml += left_s;
				}
				d.style.left=ml+"px";
				var mt = c==1?a.Y:(c==2||((a.Y+a.h+d.h>document.documentElement.scrollTop+document.documentElement.clientHeight)&&(a.Y-d.h>=0))
						?(a.Y-d.h):a.Y+a.h);

				if(typeof top_s != "undefined") {
					mt += top_s;
				}
				d.style.top=mt+"px";
			}else if(c==-1){
				var ml = (document.documentElement.clientWidth-d.w)/2;

				if(typeof left_s != "undefined") {
					ml += left_s;
				}
				d.style.left=ml+"px";
				var e=document.documentElement.scrollTop+(document.documentElement.clientHeight-d.h)/2;
				if(e>100){
					e=e-100;
				}
				var mt = e;

				if(typeof top_s != "undefined") {
					mt += top_s;
				}
				d.style.top=mt+"px";
			}
		} else {
			//alert(d.scrollWidth);
			//alert(d.style.left);
			//var aa = c.split(",");
			var w = parseInt(d.scrollWidth);
			var h = parseInt(d.scrollHeight);
			if(h > 500) {
				h = 500;
			}
			var pos = mad_DialogLoc(w, h);
			var ml = pos.left;
			var mt = pos.top;

			if(typeof top_s != "undefined") {
				mt += top_s;
			}

			if(typeof left_s != "undefined") {
				ml += left_s;
			}
			d.style.left = ml + "px";
			d.style.top = mt + "px";
		}

		if(d.style.clip&&!is_opera){
			d.style.clip="rect(auto, auto, auto, auto)";
		}
	}
}

function mad_mask_menu(event, href, func, drag, post_data) {
	mad_menu(event, "append_parent", -1, 1000000, func, href, 0, drag, post_data);
}

function mad_getscrollTop(){
	if(typeof window.pageYOffset!='undefined'){
		return window.pageYOffset;
	}else if(typeof document.compatMode!='undefined'&&document.compatMode!='BackCompat'){
		return document.documentElement.scrollTop;
	}else if(typeof document.body!='undefined'){
		return document.body.scrollTop;
	}
}

function mad_DialogLoc(width, height){
	var dde = document.documentElement;
	if (window.innerWidth){
		var ww = window.innerWidth;
		var wh = window.innerHeight;
		var bgX = window.pageXOffset;
		var bgY = window.pageYOffset;
	}else{
		var ww = dde.offsetWidth;
		var wh = dde.offsetHeight;
		var bgX = dde.scrollLeft;
		var bgY = dde.scrollTop;
	}
	var bgY = mad_getscrollTop();
	var t_DiglogX = parseInt((bgX + ((ww - width)/2)));
	var t_DiglogY = parseInt((bgY + ((wh - height)/2)));
	//var t_DiglogX = parseInt((bgX + ((ww - t_DiglogW)/2)));
	//var t_DiglogY = parseInt((bgY + ((wh - t_DiglogH)/2)));

	return {left:t_DiglogX, top:t_DiglogY};
}


function do_loading(a){if(S(a)){S(a).innerHTML='<div class="tc p10"><img src="mod/mad/img/circle_loading.gif" /></div>';S(a).style.display="";}}

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 mad_ajax(method,urlstr,postDatastr,callback){
	var http_5617=null;
	msxml_progid=['MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP'];
	try{
		http_5617=new XMLHttpRequest();
	}catch(e){
		for(var i=0;i<msxml_progid.length;++i){
			try{
				http_5617=new ActiveXObject(msxml_progid[i]);
				break;
			}catch(e){}
		}
	}
	
	if(http_5617){
		var url="";
		var poststr="";
		if(method=='GET'){
			url=urlstr+"?"+postDatastr;
		}else if(method=='POST'){
			url=urlstr;
			poststr=postDatastr;
		}
		http_5617.onreadystatechange=function(){
			if(http_5617.readyState==4&&http_5617.status==200){
				if(callback){
					callback(http_5617.responseText);
				}
				http_5617=null;
			}
		};
		
		http_5617.open(method,url,true);
		http_5617.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http_5617.send(poststr);
	}
}

function mad_select_all(flag, name) {
	var e = document.getElementsByName(name);
	var l = e.length;

	for(var i = 0; i < l; i++) {
		e[i].checked = flag;
	}
}

/**
 * 得到分类值
 */
function mad_get_multi_cat_id(name) {
	var cat_pid_str = name + '_';
	var cat_pid_0_str = cat_pid_str + '0';
	var cat_industry = parseInt($(cat_pid_0_str).value);
	if(cat_industry < 0) {
		cat_industry = 0;
	}
	var c_value = cat_industry;
	if(cat_industry > 0) {
		//循环到最低级分类
		for(var i = 1; ; i++) {
			cat_str = cat_pid_str + i;
			if($(cat_str)) {
				cat_industry = parseInt($(cat_str).value);
				if(cat_industry <= 0) {
					//此处没有选择
					cat_industry = c_value;
					break;
				}
			} else {
				break;
			}
			c_value = cat_industry;
		}
	}

	if(isNaN(cat_industry)) {
		//未选择任何分类时
		cat_industry = 0;
	}

	return cat_industry;
}

function mad_oho_adjust() {
	try{
		content_down(0);
		getcontop();
		mad_adjust_center_bd();
	} catch(e) {
			
	}
}

function mad_adjust_center_bd(){
	var v=getLowDiv();
	if(v==0){
		$('bd').height=200;
	}else{
		var v=getLowDiv()-contop;
		if(isNaN(v)){
			$('bd').height=200;
		}else{
			try{
				$('bd').height=v;
			}catch(e){}
		}
	};

	try{
		$('bd_10').style.height=parseInt($('bd').height)+'px';
	}catch(e){};

	var a=parseInt($('bd').offsetHeight);
	if(json_command_bs!=''&&a>100){
		$('bd_4').style.height=a-100+'px';
	};

	//alert($('bd'));
	adjust_ya_mod();
};

function $mad_sH(v){
	var h=$(v+'_content').offsetHeight;
	//alert(h);
	$('ohid_'+v).setAttribute("initH",h);
}

function $mad_eH(v){
	var h=$(v+'_content').offsetHeight;
	var hh=parseInt($('ohid_'+v).getAttribute("initH"));
	if(isNaN(hh)){
		return false;
	};

	//alert(h + '-' + hh);

	if(hh==h){
		return false;
	}else if(h>hh){
		c_error=5;
		mad_checherror1();
	}else{
		var hhh=hh-h;
		var lowerd=$('ohid_'+v).getAttribute("LowerD");

		//alert(hhh);
		if(!lowerd){
			getAllLowDiv(v,[],[],'0','0');
			var lowerd=$('ohid_'+v).getAttribute("LowerD");};
			try{
				$('ohid_'+v).style.height=(parseInt($('ohid_'+v).style.height)-hhh)+'px';
			}catch(e){};

			try{$(v+'_4').style.height=(parseInt($(v+'_4').style.height)-hhh)+'px';}catch(e){};

			if(lowerd){
				var l_a=lowerd.split(',');
				var num=l_a.length;

				for(var j=0;j<num;j++){
					if(l_a[j]){
						var t1=parseInt($('ohid_'+l_a[j]).style.top)
						;if((t1-hhh)>0){$('ohid_'+l_a[j]).style.top=(t1-hhh)+'px';
					}
				}
			}
		};

		getcontop();
		mad_adjust_center_bd();
	}
};

function mad_checherror1(){
	var c=_content_id_a.length;
	if(c==0){
		return 0;
	};

	_content_id=_content_id_a[0];
	if(_content_id!=''&&_content_id){
		mad_content_down(0);
		getcontop();
		mad_adjust_center_bd();
	}else{
		return;
	};

	if(_content_no+1>c){
		_content_no=0;
		c_error=c_error+1;
	}else{
		_content_no=_content_no+1;
	};

	if(c_error>20){
		setTimeout(checherror,100);
	}else{
		setTimeout(checherror1,100);
	}
} ;

function mad_content_down(c_no){
	var c=_content_id_a.length;
	if(c==0){return 0;};

	c_no=parseInt(c_no);
	if(isNaN(c_no)){c_no=0;};

	if(c_no==0){
		_content_have=0;
	};
	_content_id=_content_id_a[c_no];
	if($('ohid_'+_content_id)){
		var str=$(_content_id+'_c').style.top;
		var sTemp=parseInt(str);
		if(isNaN(sTemp)){
			str=0;
		}else{
			str=sTemp;
		};

		var vv=$(_content_id+'_content').offsetHeight+0+str;
		var v=$('ohid_'+_content_id).offsetHeight;

		//alert(vv + '-' + v);

		if($('ohid_'+_content_id).style.display=='none'){
		}else if(vv>v){
			_content_have=1;
			var vvv=vv-v; 
			var ya=$('ohid_'+_content_id).getAttribute('ya');
			ya=parseInt(ya);
			if(isNaN(ya)){
				ya=50;
			}else if(ya<2){
				ya=2;
			};

			vvv=vvv+ya; 

			//alert(vvv);

			downLowDiv3(_content_id,vvv);
			$('ohid_'+_content_id).style.height=vv+ya+'px';
			try{
				$(_content_id+'_4').style.height=(vv+ya-100)+'px';
			}catch(e){}
		};

		if(c_no+1<c){
			c_no=c_no+1;
			mad_content_down(c_no);
		}
	}
};

function mad_oho_adjust_start(g_mad_block_id) {
	try{
		if(typeof g_mad_block_id != "undefined" && g_mad_block_id != "") {
			$_sH(g_mad_block_id);
		}
	} catch(e) {
			
	}
}

function mad_oho_adjust_end(g_mad_block_id) {
	try{
		if(typeof g_mad_block_id != "undefined" && g_mad_block_id != "") {
			$_eH(g_mad_block_id);
		}
	} catch(e) {
			
	}
}

var jsmenu = new Array();
var ctrlobjclassName;
jsmenu['active'] = new Array();
jsmenu['timer'] = new Array();
jsmenu['iframe'] = new Array();

function initCtrl(ctrlobj, click, duration, timeout, layer) {
	if(ctrlobj && !ctrlobj.initialized) {
		ctrlobj.initialized = true;
		ctrlobj.unselectable = 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);
		}

		ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function' ? ctrlobj.onmouseover : null;
		ctrlobj.onmouseover = function(e) {
			doane(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 initMenu(ctrlid, menuobj, duration, timeout, layer, drag) {
	if(menuobj && !menuobj.initialized) {
		menuobj.initialized = true;
		menuobj.ctrlkey = ctrlid;
		menuobj.onclick = ebygum;
		menuobj.style.position = 'absolute';
		if(duration < 3) {
			if(duration > 1) {
				menuobj.onmouseover = function() {
					clearTimeout(jsmenu['timer'][ctrlid]);
				}
			}
			if(duration != 1) {
				menuobj.onmouseout = function() {
					jsmenu['timer'][ctrlid] = setTimeout('hideMenu(' + layer + ')', timeout);
				}
			}
		}
		menuobj.style.zIndex = 50;
		if(is_ie) {
			//menuobj.style.filter += "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#CCCCCC,strength=2)";
		}
		if(drag) {
			menuobj.onmousedown = function(event) {try{menudrag(menuobj, event, 1);}catch(e){}};
			document.body.onmousemove = function(event) {try{menudrag(menuobj, event, 2);}catch(e){}};
			menuobj.onmouseup = function(event) {try{menudrag(menuobj, event, 3);}catch(e){}};
		}
	}
}

var menudragstart = new Array();
function menudrag(menuobj, e, op) {
	//alert(menuobj.id);
	if(op == 1) {
		var oo = is_ie ? event.srcElement : e.target;
		if(in_array(oo.tagName, ['TEXTAREA', 'INPUT', 'BUTTON', 'SELECT'])) {
			return;
		} else if(oo.id == menuobj.id) {
			return;
		}
		menudragstart = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		menudragstart[2] = parseInt(menuobj.style.left);
		menudragstart[3] = parseInt(menuobj.style.top);
		doane(e);
	} else if(op == 2 && menudragstart[0]) {
		var menudragnow = is_ie ? [event.clientX, event.clientY] : [e.clientX, e.clientY];
		menuobj.style.left = (menudragstart[2] + menudragnow[0] - menudragstart[0]) + 'px';
		menuobj.style.top = (menudragstart[3] + menudragnow[1] - menudragstart[1]) + 'px';
		doane(e);
	} else if(op == 3) {
		menudragstart = [];
		doane(e);
	}
}


var mad_maskDiv=null;

function mad_showmask(){
	if(mad_maskDiv==null||!mad_maskDiv){
		mad_maskDiv=document.createElement("span");
		//mad_maskDiv.style.cssText="position:absolute;top:0px;left:0px;";
		if(is_ie) {
			mad_maskDiv.style.cssText="position:absolute;top:0px;left:0px;background-color:#999;filter:alpha(opacity=50);z-index:49;";
		} else {
			mad_maskDiv.style.cssText="position:absolute;top:0px;left:0px;background-color:#999;opacity:0.5;z-index:49;";
		}
		mad_maskDiv.style.height=mad_getscrollHeight() + "px";
		mad_maskDiv.style.width=document.documentElement.clientWidth + "px";
		document.body.appendChild(mad_maskDiv);

		//alert(mad_maskDiv.style.height + "-" + mad_maskDiv.style.width);
	}
}

var mad_maskIframe = null;
function mad_mask_iframe() {
	if(mad_maskIframe==null||!mad_maskIframe){
		mad_maskIframe=document.createElement("iframe");
		var aa = mad_getscrollHeight();
		var bb = document.documentElement.clientWidth;
		if(is_ie) {
			mad_maskIframe.style.cssText="position:absolute;top:0px;left:0px;z-index:1;width:" + bb + "px;height:" + aa + "px;";
		} else {
			mad_maskIframe.style.cssText="position:absolute;top:0px;left:0px;z-index:1;width:" + bb + "px;height:" + aa + "px;";
		}
		mad_maskIframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		//alert(mad_maskIframe);
		//mad_maskIframe.style.height=mad_getscrollHeight() + "px";
		//mad_maskIframe.style.width=document.documentElement.clientWidth + "px";
		$("append_parent").appendChild(mad_maskIframe);
	}
	
}
function mad_closeMaskDiv(){  
	if(mad_maskDiv==null)return;
	//mad_maskDiv.innerHTML='';
	document.body.removeChild(mad_maskDiv);
	mad_maskDiv = null;
}
function mad_closeMaskIframe(){  
	if(mad_maskIframe==null)return;
	//mad_maskDiv.innerHTML='';
	$("append_parent").removeChild(mad_maskIframe);
	mad_maskIframe = null;
}

function mad_getscrollHeight(){
	//alert(document.body.scrollHeight + "-" + document.documentElement.clientHeight);
	if(document.documentElement.scrollHeight>document.documentElement.clientHeight){
		return document.documentElement.scrollHeight;
	}else{
		return document.documentElement.clientHeight;
	}
};

function showMenu(ctrlid, click, offset, duration, timeout, layer, showid, maxh, drag) {
	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;
	if(isUndefined(drag)) drag = false;
	if(jsmenu['active'][layer] == menuobj) {
		hideMenu(layer);
		return;
	} else {
		//hideMenu(layer);
	}
	//停止所有的hidemenu timeout
	var len = jsmenu['timer'].length;
	if(len > 0) {
		for(var i=0; i<len; i++) {
			if(jsmenu['timer'][i]) clearTimeout(jsmenu['timer'][i]);
		}
	}
	
	initCtrl(ctrlobj, click, duration, timeout, layer);
	ctrlobjclassName = ctrlobj.className;
	ctrlobj.className += ' hover';
	initMenu(ctrlid, menuobj, duration, timeout, layer, drag);
	
	menuobj.style.display = '';
	if(!is_opera) {
		menuobj.style.clip = 'rect(auto, auto, auto, auto)';
	}
	//设置位置
	setMenuPosition($(showid), $(showid + '_menu'), offset);

	//if(is_ie && is_ie < 7) {
	if(false) {
		//alert($(showid + "_menu").style.width);
		//ie 6 ，显示个遮罩层
		if(!jsmenu['iframe'][layer]) {
			var iframe = document.createElement('iframe');
			//iframe.style.display = 'none';
			iframe.style.position = 'absolute';
			//iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			$('append_parent') ? $('append_parent').appendChild(iframe) : menuobj.parentNode.appendChild(iframe);
			jsmenu['iframe'][layer] = iframe;
			//alert(iframe);
		}
		jsmenu['iframe'][layer].style.top = menuobj.style.top;
		jsmenu['iframe'][layer].style.left = menuobj.style.left;
		jsmenu['iframe'][layer].style.width = menuobj.style.width;
		jsmenu['iframe'][layer].style.height = menuobj.style.height;
		jsmenu['iframe'][layer].style.display = 'block';

	}

	/*
	var v = menuobj.scrollHeight;
	if(maxh && v > maxh) {
		//menuobj.style.paddingRight = "20px";
		v = maxh;
	}
	//alert(v);
	menuobj.style.height = v + 'px';
	menuobj.style.overflowX = 'hidden';
	if(is_opera) {
		menuobj.style.overflow = 'auto';
	} else {
		menuobj.style.overflowY = 'auto';
	}
	*/
	
	if(!duration) {
		setTimeout('hideMenu(' + layer + ')', timeout);
	}

	jsmenu['active'][layer] = menuobj;
	//return;
}

function hideMenu(layer, no_hide) {
	if(isUndefined(no_hide)) {
		//默认要隐藏掉遮罩，但有时不需要隐藏
		try{mad_closeMaskDiv();mad_closeMaskIframe();} catch(e){}
	}
	if(isUndefined(layer)) layer = 0;
	if(jsmenu['active'][layer]) {
		try {
			$(jsmenu['active'][layer].ctrlkey).className = ctrlobjclassName;
		} catch(e) {}
		clearTimeout(jsmenu['timer'][jsmenu['active'][layer].ctrlkey]);
		jsmenu['active'][layer].style.display = 'none';
		if(is_ie && is_ie < 7 && jsmenu['iframe'][layer]) {
			//jsmenu['iframe'][layer].style.display = 'none';
		}
		jsmenu['active'][layer] = null;
	}
}

function ebygum(eventobj) {
	if(!eventobj || is_ie) {
		window.event.cancelBubble = true;
		return window.event;
	} else {
		if(eventobj.target.type == 'submit') {
			eventobj.target.form.submit();
		}
		eventobj.stopPropagation();
		return eventobj;
	}
}

function mad_clear_menu(ctrlid) {
	var div = $(ctrlid + '_menu');

	if(div) {
		div.innerHTML = "";
	}
}

function mad_menu(e, ctrlid, offset, timeout, func, href_link, layer, drag, post_data) {
	if(document.all && (document.readyState != "complete")) {return;}
	var box = 0;
	if(jsmenu['active'][0] && jsmenu['active'][0].ctrlkey == ctrlid) {
		hideMenu();
		doane(e);
		return;
	} else if(is_ie && is_ie < 7 && document.readyState.toLowerCase() != 'complete') {
		return;
	}
	cache = 0;
	divclass = 'popupmenu_popup';
	optionclass = 'popupmenu_option';
	if(isUndefined(timeout)) timeout = 1000000;
	if(isUndefined(func)) func = '';
	if(isUndefined(offset)) offset = 0;
	if(isUndefined(layer)) layer = 0;
	if(isUndefined(e) || e == null || e == '') {
		et = true;
	} else {
		et = (e.type == 'click') ? true : false;;
	}
	duration = timeout > 10000 ? 3 : 0;
	executetime = duration ? 2000: timeout;
	if(offset != 0) {
		divclass = 'popupmenu_centerbox';
		box = 1;
	}

	if(typeof drag == "undefined") {
		drag = 1;
	}
	var div = $(ctrlid + '_menu');
	if(cache && div) {
		if(offset != 0) {
			mad_showmask();
		}
		showMenu(ctrlid, et, offset, duration, timeout, layer, ctrlid, 500, drag);
		//if(func) setTimeout(func + '(' + ctrlid + ')', executetime);
		if(func) setTimeout(func + '(' + ctrlid + ')', 0);
		doane(e);
	} else {
		mad_showloading('');
		if(!div) {
			div = document.createElement('div');
			div.ctrlid = ctrlid;
			div.id = ctrlid + '_menu';
			div.style.display = 'none';
			//div.innerHTML = '<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td></tr></table>';
			div.className = divclass;
			$('append_parent').appendChild(div);
		}

		//var x = new Ajax();
		var href = (!isUndefined(href_link) && (href_link != "")) ? href_link : (!isUndefined($(ctrlid).href) ? $(ctrlid).href : $(ctrlid).attributes['href'].value);
		href += "&timestamp=" + new Date().getTime();
		//alert(href);
		if(typeof post_data == "undefined") {
			post_data = "";
		}
		startRequest("POST", href, post_data, 
					function(r){set_ajax_menu(r, e, div, optionclass, ctrlid, offset, duration, timeout, layer, et, func, executetime, drag);});
	}
}

function set_ajax_menu(r, e, div, optionclass, ctrlid, offset, duration, timeout, layer, et, func, executetime, drag) {
	mad_showloading('none');
	mad_evalscript(r);
	if(div) {
		//alert(r);
		div.innerHTML = '<div class="' + optionclass + '">' + r + '</div>';
	}
	if(offset != 0) {
		mad_showmask();
	}
	showMenu(ctrlid, et, offset, duration, timeout, layer, ctrlid, 500, drag);
	if(func) {
		//alert(func);
		//Init_option_1();
		//alert(func);
		//setTimeout(func + '("' + ctrlid + '")', executetime);
		setTimeout(func + '("' + ctrlid + '")', 0);
	}

	if(is_ie && is_ie < 7) {
		mad_mask_iframe();
	}
	doane(e);
}

function insertContent(c,d){
	var b=S(c);
	selection=document.selection;
	checkFocus(c);
	if(!isUndefined(b.selectionStart)){
		var e=b.selectionStart+0;
		b.value=b.value.substr(0,b.selectionStart)+d+b.value.substr(b.selectionEnd);
	}else{
		if(selection&&selection.createRange){
			var a=selection.createRange();
			a.text=d;
		}else{
			b.value+=d;
		}
	}
}

function checkFocus(b){var a=S(b);if(!a.hasfocus){a.focus();}}

function mad_attach_event(obj, evt, func, eventobj) {
	eventobj = !eventobj ? obj : eventobj;
	if(obj && obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(eventobj && eventobj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}


function mad_stringxor(s1, s2) {
	var s = '';
	var hash = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var max = Math.max(s1.length, s2.length);
	for(var i=0; i<max; i++) {
		var k = s1.charCodeAt(i) ^ s2.charCodeAt(i);
		s += hash.charAt(k % 52);
	}
	return s;
}

//得到一个定长的hash值,依赖于 stringxor()
function mad_hash(string, length) {
	var length = length ? length : 32;
	var start = 0;
	var i = 0;
	var result = '';
	filllen = length - string.length % length;
	for(i = 0; i < filllen; i++){
		string += "0";
	}
	while(start < string.length) {
		result = mad_stringxor(result, string.substr(start, length));
		start += length;
	}
	return result;
}

var mad_evalscripts = new Array();
function mad_evalscript(s) {
	if(s.indexOf('<script') == -1) return s;
	var p = /<script[^\>]*?src=\"([^\>]*?)\"[^\>]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?><\/script>/ig;
	var arr = new Array();
	while(arr = p.exec(s)) {
		//alert(arr[1]);
		mad_appendscript(arr[1], '', arr[2], arr[3]);
	}
	p = /<script (?!src)[^\>]*?( reload=\"1\")?>([^\x00]+?)<\/script>/ig;
	while(arr = p.exec(s)){
		//alert(arr[2]);
		mad_appendscript('', arr[2], arr[1]);
	}
	return s;
}

function mad_appendscript(src, text, reload, charset) {
	var id = mad_hash(src + text);
	if(!reload && in_array(id, mad_evalscripts)) return;
	if(reload && $(id)) {
		$(id).parentNode.removeChild($(id));
	}

	mad_evalscripts.push(id);
	var scriptNode = document.createElement("script");
	scriptNode.type = "text/javascript";
	scriptNode.id = id;
	scriptNode.charset = charset;
	try {
		if(src) {
			scriptNode.src = src;
		} else if(text){
			scriptNode.text = text;
			//alert(text);
		}
		$('append_parent').appendChild(scriptNode);
	} catch(e) {}
}

function mad_stripscript(s) {
	return s.replace(/<script.*?>.*?<\/script>/ig, '');
}

function mad_showloading(display, waiting) {
	var display = display ? display : 'block';
	var waiting = waiting ? waiting : '页面加载中...';
	if(display == "block") {
		$("ajaxwaitid").style.top = document.documentElement.scrollTop + "px";
	}
	$('ajaxwaitid').innerHTML = waiting;
	$('ajaxwaitid').style.display = display;
}

function mad_copy_clip(text2copy) 
{
	if (window.clipboardData) 
	{
		window.clipboardData.setData("Text",text2copy);
	} 
	else 
	{
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) 
		{
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="mod/mad/img/_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
	return true;
}

if(document.body) {
	if(!S("append_parent") || S("append_parent") == null) {
		aa_div = document.createElement('div');
		aa_div.id = 'append_parent';
		document.body.insertBefore(aa_div, document.body.childNodes[0]);
	}
	if(!S("ajaxwaitid") || S("ajaxwaitid") == null) {
		aa_div = document.createElement('div');
		aa_div.id = 'ajaxwaitid';
		document.body.insertBefore(aa_div, document.body.childNodes[0]);
	}
}


