function $(id) { return document.getElementById(id); }

function isIE()
{
	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);

	return is_ie;
}

//父窗口跳转页面
function pSkip(skip_url)
{
    parent.location.href = skip_url;
}

function moo_relatekw(obj, subject_old_value)
{
    if (obj.value != subject_old_value) {
        relatekw();
    }
}


function mbStringLength(s) 
{
	var totalLength = 0;
	var i;
	var charCode;
	for (i = 0; i < s.length; i++) {
	　charCode = s.charCodeAt(i);
	　if (charCode < 0x007f) {
	　　totalLength = totalLength + 1;
	　} else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
	　　totalLength += 2;
	　} else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
	　　totalLength += 3;
	　}
	}

	return totalLength;
}

function addFavorite(url, title)
{
	if (window.opera)
	{
		alert('请按“CTRL + D” 键收藏！');
	}

	else if (window.MessageEvent && !document.getBoxObjectFor)
	{
		alert('请按“CTRL + D” 键收藏！');
	}

	else if (window.sidebar)
	{
		window.sidebar.addPanel(title, url,"");
	}

	else if( document.all )
	{
		window.external.AddFavorite(url,title);
	}
}

function urlto(url) {
	window.location.href = url;
}

function handleEvent(oEvent, form_obj, aid, lastctrls)
{
	if (oEvent.keyCode == 13) {
		if (lastctrls != '') {
			var arr_lctrl = lastctrls.split(',');
			var is_submit = false;
			for (var i = 0;i < arr_lctrl.length;++i) {
				var obj = document.getElementById(arr_lctrl[i]);
				if (document.activeElement == obj) {
					is_submit = true;
					break;
				}
			}
		} else {
			var is_submit = true;
		}

		if (is_submit) {
			if (form_obj.onsubmit()) {
				document.getElementById(aid).focus();
				form_obj.submit();
			}
		}
	}
}

function handleEvent2(oEvent, aid, func)
{
	if (oEvent.keyCode == 13) {
		document.getElementById(aid).focus();
		func();
	}
}

function handleEventCtrl(oEvent, form_obj, aid, lastctrls, func)
{
	if (oEvent.keyCode == 13 && oEvent.ctrlKey == true) {
		if (lastctrls != '') {
			var arr_lctrl = lastctrls.split(',');
			var is_submit = false;
			for (var i = 0;i < arr_lctrl.length;++i) {
				var obj = document.getElementById(arr_lctrl[i]);
				if (document.activeElement == obj) {
					is_submit = true;
					break;
				}
			}
		} else {
			var is_submit = true;
		}

		if (is_submit) {
			if (form_obj.onsubmit()) {
				document.getElementById(aid).focus();
				if (func)
					func();
				else
					form_obj.submit();
			}
		}
	}
}

function submitForm(form_obj)
{
	if (form_obj.onsubmit()) {
		form_obj.submit();
	} else {
	//
	}
}


function moo_show_click(id)
{
    var ids = id.split('_');
	var id = ids[1];
	var clickid = ids[2];
	
	var url = siteUrl+'/do.php?action=click&op=num&clickid='+clickid+'&id='+id;
	
	var x = new Ajax();
	//x.setLoading(loading);
	//x.setWaitId(waitid);
	x.display = typeof display == 'undefined' || display == null ? '' : display;
	if(url.substr(strlen(url) - 1) == '#') {
		url = url.substr(0, strlen(url) - 1);
		x.autogoto = 1;
	}
    
    x.showId = $('click_div_' + clickid);
    if (!x.showId)
        x.showId = $('clicknum_' + id + '_' + clickid);
        
    url = url + '&ismoosee=1&inajax=1&ajaxtarget=' + 'click_div';
	x.get(url, function(s, x) {
	    //alert(s);
		evaled = false;
		if(s.indexOf('ajaxerror') != -1) {
			evalscript(s);
			evaled = true;
		}
		if(!evaled && s) {
			if(x.showId) {
				//changedisplay(x.showId, x.showId.orgdisplay);
				//changedisplay(x.showId, x.display);
				//x.showId.orgdisplay = x.showId.style.display;
				x.showId.innerHTML = s;
				//ajaxupdateevents(x.showId);
				//if(x.autogoto) scroll(0, x.showId.offsetTop);
			}
		}
		if(!evaled)evalscript(s);
		ajaxerror = null;
	});
}

function mooCheckSubmit(form_obj)
{
    return true;
}
/*
function mooSetCookie(name, value) 
{  
    var expdate = new Date();
    var argv = mooSetCookie.arguments;
    var argc = mooSetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    
    if(expires!=null && expires>=0) 
        expdate.setTime(expdate.getTime() + ( expires * 1000 ));
        
    document.cookie = name + "=" + escape (value) +((expires == null || expires < 0) ? ((expires==-1)?"; expires=-1":"") : ("; expires="+ expdate.toGMTString()))
                        + ((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
                        + ((secure == true) ? "; secure" : "");
}
*/
function mooSetCookie(name, value, expire_sec)
{
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + expire_sec * 1000);
    document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString();
}

function mooGetCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    
    if(arr != null) 
    return unescape(arr[2]); 
    
    return null;
}

function mooDelCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString();
}


function ajaxSendMsg(load_id, show_id, this_id, is_click, callback_func)
{
	var div = $(show_id + '_menu');
	if (!div) {
		div = document.createElement('div');
		div.id = show_id + '_menu';
		div.style.display = 'none';
		div.className = 'popupmenu_popup';
		$('append_parent').appendChild(div);
		div.innerHTML = '<div id="' + show_id + '_menu_s" class="popupmenu_option"></div>';
	}
	
	if(isUndefined(is_click)) is_click = false;
	
	var x = new Ajax('HTML', load_id);
	x.showId = $(show_id + '_menu_s');
	x.thisId = $(this_id);
	
	var url = x.thisId.name;
	x.get(url, function(s, x) {
		//alert(s);
		evaled = false;
		if(s.indexOf('ajaxerror') != -1) {
			evalscript(s);
			evaled = true;
		}
		if(!evaled && s) {
			if(x.showId) {
				//alert(s);
				arr = s.split('|');
				x.showId.innerHTML = arr[1];
				x.thisId.innerHTML = arr[2];
				x.thisId.name = arr[3];
				showMenu(show_id, is_click, 0, 0, 2000, 0, show_id, 500, 1);
				
				if (!isUndefined(callback_func)) callback_func(arr);
			}
		}
		if(!evaled)evalscript(s);
		ajaxerror = null;
	});
}
