//--- 包含声明
var base_tag	= true;

//--- 全局变量 -> 浏览器 --------------------------------------
var ua			= navigator.userAgent.toLowerCase();
var g_ie		= (ua.indexOf("msie") > 0) || (ua.indexOf("webtv") > 0);
var g_opera		= ua.indexOf("opera") > 0;
var g_gecko		= ua.indexOf("gecko") > 0;

//--- 基本函数 ------------------------------------------------
// 根据ID返回对象
function get_id_object(p_id)
{
	return g_ie ? document.all[p_id] : document.getElementById(p_id);
}

// 读取iframe对象
function get_iframe_object(p_id)
{
	return g_ie ? document.frames[p_id] : document.getElementById(p_id).contentWindow;
}

// 返回调用自己的对象
function return_parent_obj(p_this)
{
	return g_ie ? event.srcElement : p_this;
}

// 对象是否存在
function is_object(p_id)
{
	return get_id_object(p_id) == null ? false : true;  
}

// 是否为空及none
function is_null(p_val)
{
	return (p_val == "" || p_val == null || p_val == "undefined");
}

// 输出字符串到浏览器
function dw(p_str,p_id)
{
	is_null(p_id) ? document.write(p_str) : get_iframe_object(p_id).document.write(p_str);
}

// 得到外部定义样式
function getStyleCss(pObj,pStyle)
{
	var iStyle;
	var iCurr	= document.getElementById(pObj);

	if (iCurr.currentStyle)
	{
		iStyle	= iCurr.currentStyle[pStyle];
	}
	else if (window.getComputedStyle)
	{
		iStyle	= window.getComputedStyle(iCurr, "")[pStyle];
	}
	
	return iStyle;
}

// 得到上 N 级对象
function seekParent(p_obj,p_attr,p_count)
{
	var i_child		= p_obj;

	for(var i = 0; i < p_count; i++)
	{
		i_child	= i_child.parentNode;
	}

	return i_child[p_attr];
}

function checkwd_reload(){
        if (document.getElementById("user_login_info")) {
            ob = getFrameNode("user_login_info").document.getElementById("chk_img");
        }       
        else {  
             ob = document.getElementById("chk_img");
         }       
        SENDTIME = new Date("2005","9","8","10","0","0");
        var chk_img_time =new Date().getTime();
        ob.src = '/checkwd_image_new.php?' + chk_img_time;
}

// 转化tab键
function handleKeyDown(i_event)
{
	var i_obj	= i_event.target ? i_event.target : event.srcElement;

	if(i_event.keyCode == 9)
	{
		if(g_ie)
		{
			i_obj.selection			= document.selection.createRange();
			i_obj.selection.text	= String.fromCharCode(9);
			i_event.returnValue		= false;
		}
		else
		{
			var startPos			= i_obj.selectionStart;
			var endPos				= i_obj.selectionEnd;
			var startStr			= i_obj.value.slice(0,startPos);
			var endStr				= i_obj.value.slice(endPos);
			i_obj.value				= startStr + String.fromCharCode(9) + endStr;
			setTimeout(function(){i_obj.focus()}, 200);
			i_obj.selectionEnd		= endPos+1;
		}
	}
}
// 设置标题
function setTitle(p_val)
{
	document.title = p_val;
}

// 得到Url参数值
function getParameter(p_val)
{
   var i_re 	= new RegExp(p_val+"=([^\\&]+)","i");
   var i_str	= window.location.search.substr(1).match(i_re);
   
   if (i_str)
   {
      return unescape(i_str[1]);
   }
   return null;
}

// 累加字符串到 case
function append_str(p_obj,p_body)
{
	if(is_null(typeof(HTMLElement)))
	{
		get_id_object(p_obj).insertAdjacentHTML("beforeend",p_body);
	}
	else
	{
		get_id_object(p_obj).innerHTML	+= p_body;
	}
}

// 为对象添加一个事件
function append_onload(p_obj,p_function)
{
	if (g_ie)
	{	
		if (!is_null(get_id_object(p_obj)["func"]))
		{
			get_id_object(p_obj).detachEvent("onload",get_id_object(p_obj)["func"]);
		}

		get_id_object(p_obj).attachEvent("onload",p_function);
		get_id_object(p_obj)["func"]	= p_function;
	}
	else
	{
		get_id_object(p_obj)["onload"]	= p_function;
	}
}

// 得到对象类型
function get_type(p_val)
{
	if (!is_null(p_val))
	{
		var str = p_val.constructor.toString();
		return str.substring(10,str.indexOf("("));
	}
}

// 得到对象的坐标
function getXY(p_id)
{
	var sumTop=0,sumLeft=0;
	while(p_id!=document.body)
	{
		sumTop	+= parseInt(p_id.offsetTop);
		sumLeft	+= parseInt(p_id.offsetLeft);
		p_id	= p_id.offsetParent;
	}
	return {left:sumLeft,top:sumTop}
}

// 选择框选中
function setSelect(p_id,p_val)
{
	p_obj	= document.getElementsByName(p_id);
	for (var i=0; i<p_obj.length; i++)
	{
		if (p_obj[i].type == "select-one")
		{
			for (var j = 0; j < p_obj[i].options.length; j++)
			{
				if (p_val == p_obj[i].options[j].value)
				{
					p_obj[i].options[j].selected	= true;
				}
			}
		}
		
		if (p_obj[i].type == "checkbox" || p_obj[i].type == "radio")
		{
			if (p_val == p_obj[i].value)
			{
				p_obj[i].checked	= true;
			}
		}
	}
}
//--- iframe函数 -------------------------------------------
// 初始化一个iframe,无边框，不可滚动
function init_iframe (p_id,p_src,p_width,p_height)
{
	p_src		= is_null(p_src) ? "about:blank" : p_src;
	p_width		= is_null(p_width) ? 0 : p_width;
	p_height	= is_null(p_height)? 0 : p_height;
	
	if (!is_object(p_id))
	{
		dw("<iframe id='"+p_id+"' name='"+p_id+"' src='"+p_src+"' width='"+p_width+"px' height='"+p_height+"px' frameborder='0' scrolling='yes'>您的浏览器不支持此功能，请您使用最新的版本。</iframe>");
	}
	else
	{
		get_id_object(p_id).src	= "about:blank";
	}
}

// 使iframe跳转到一个地址
function location_iframe(p_iframe,p_iframe_href,p_onload)
{	
	//append_onload(p_iframe,p_onload);
	get_id_object(p_iframe).src	= p_iframe_href;
}

// 读取一个文件的内容并把内容回显在对象上，如开头字母为 <!DOCTYPE 则表示数据为正确的
function read_iframe_body (p_iframe,p_iframe_href,p_display_obj)
{
	get_id_object(p_display_obj).innerHTML	= "数据读取中……";
	get_id_object(p_iframe).className		= p_display_obj;
	location_iframe(p_iframe,p_iframe_href,return_onload);
}

// 判断是否读取过，如有则显示或隐藏
function load_page(p_iframe,p_iframe_href,p_display_obj)
{
	var i_currText	= get_id_object(p_display_obj).innerHTML;
	var i_state		= i_currText.substr(i_currText.length-11,i_currText.length).toLowerCase() == "<big></big>" ;
	
	if (!i_state)
	{
		read_iframe_body (p_iframe,p_iframe_href,p_display_obj);
		div_visible(p_display_obj);
	}
	else
	{
		div_display(p_display_obj);
	}
}

// 读取完成后处理函数
function return_onload ()
{
	var i_obj	= return_parent_obj(this);
	get_id_object(i_obj.className).innerHTML	= get_iframe_object(i_obj.id).document.body.innerHTML;
}

//--- DIV 控制 ------------------------------------------------------
// 显示/隐藏
function div_display (p_id)
{
	get_id_object(p_id).style.display == "none" ? div_visible(p_id) : div_hidden(p_id);
}

// 隐藏层
function div_hidden (p_id)
{
	get_id_object(p_id).style.display	= "none";
}

// 显示层
function div_visible (p_id)
{
	get_id_object(p_id).style.display	= "";
}

// --- CSS处理 -------------------------------------------------------
// 切换CSS
function swap_css (p_id,p_css_a,p_css_b)
{
	get_id_object(p_id).className = (get_id_object(p_id).className == p_css_a ? p_css_b : p_css_a);
}

// --- 模拟按钮 -------------------------------------------------------
function show_button (p_id,p_class,p_click,p_text)
{
	var i_id	= is_null(p_id) ? "" : "id=\"" + p_id + "\"";
	var i_click	= is_null(p_click) ? "" : "onclick=\"javascript:"+p_click+"\"";
	var i_text	= is_null(p_text) ? "" : p_text;
	
	return "<button "+i_id+" "+i_click+" class=\""+p_class+"\" onfocus=\"this.blur();\" >" + i_text + "</button>";
}

// --- 表现层 ---------------------------------------------------------
// 显示 swf
function dw_swf(p_name,p_swf,p_width,p_height,p_wmode)
{
	if (!is_null(p_wmode))
	{
		p_wmode	= " wmode='transparent' ";
	}
	
	var	i_code	= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+p_width+'" height="'+p_height+'">';
	i_code		+= '<param name="movie" value="'+p_swf+'" />';
	i_code		+= '<param name="quality" value="high" />';
	i_code		+= '<embed name="'+p_name+'" src="'+p_swf+'"'+p_wmode+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+p_width+'" height="'+p_height+'"></embed>';
	i_code		+= '</object>';
	return i_code;
}
// 显示或隐藏所有的type为p_module的组件
function hide_module(p_module,p_mode)
{
	var i_iframe		= top.document.getElementsByTagName("iframe");
	var i_module		= document.getElementsByTagName(p_module);
	var i_iframe_len	= i_iframe.length;
	var i_module_len	= i_module.length;

	for (var i = 0; i < i_module_len; i++)
	{
		i_module[i].style.display	= p_mode;
	}
	
	for (var j = 0; j < i_iframe_len; j++)
	{
		try
		{
			var i_select_len	= i_iframe[j].contentWindow.document.body.getElementsByTagName(p_module).length;
	
			for (var k = 0; k < i_select_len; k++)
			{
				i_iframe[j].contentWindow.document.body.getElementsByTagName(p_module)[k].style.display	= p_mode;
			}
		}
		catch(E){};
	}
}

// 显示屏蔽层
function hide_screen(p_screen,p_display,p_alpha)
{
	var iHeight	= document.body.scrollHeight > screen.height ? document.body.scrollHeight : screen.height;
	var iWidth	= document.body.clientWidth;
	
	if (p_display == "none")
	{
		get_id_object("dialogScreen").innerHTML	= "<div style='position:absolute;top:0;left:0;opacity:"+p_alpha+";filter:alpha(opacity="+(p_alpha*100)+");background:#fff;width:" + iWidth + "px;height:"+iHeight+"px'></div>";
	}
	else
	{
		get_id_object("dialogScreen").innerHTML	= "";
	}
	
	return true;
}

// 使其居中
function to_middle(p_obj)
{
	var i_style			= get_id_object(p_obj).style;
	i_style.display		= "";
	i_style.left		= (document.body.clientWidth / 2) - (get_id_object(p_obj).offsetWidth / 2);
	i_style.top			= (document.body.clientHeight / 2 + document.body.scrollTop) - (get_id_object(p_obj).offsetHeight / 2);
}

// 复制到内存 
function setCopy(p_id, p_start, p_end)
{
	g_ie ? clipboardData.setData('Text',get_id_object(p_id).innerHTML) : alert("对不起，目前此功能只支持IE。");
}

// 设置首页
function setHome(p_id,p_href)
{
	if (g_ie)
	{
		window.external.AddFavorite(window.document.location,window.document.title);
	}
	else
	{
		alert("对不起，目前此功能只支持IE。");
	}
}

//--- 支持moz -------------------------------------------------------
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where)
		{
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this);
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if(this.nextSibling)
				{
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				}
				else
				{
					this.parentNode.appendChild(parsedNode);
				}
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r			= this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML	= r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	}

	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText	= document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	}
}


//-- ADD at 2007年3月23日
//计算一个随机密码
function rand_passwd(maxlen)
{
	var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var rnds = '';
	for (i = 0; i < maxlen; i++)
	{
		rnd = Math.random();
		n = Math.ceil((chars.length + 1) * rnd) % chars.length;
		rnds += chars[n];
		
	}
	return rnds;
}

// 反转按钮
function reverse_button(id)
{
	obj = document.getElementById(id);
	if (obj)
	{
		obj.disabled = !obj.disabled;
	}
}
// 启用按钮
function enable_button(id)
{
	obj = document.getElementById(id);
	if (obj)
	{
		obj.disabled = false;
	}
}
// 禁用按钮
function disable_button(id)
{
	obj = document.getElementById(id);
	if (obj)
	{
		obj.disabled = true;
	}
}

function addtagname(tagname, htmlid) {
	var tagtext = document.getElementById(htmlid).value;
	var tag = tagname;
	var arr_tag = tagtext.split(' ');
	var len = arr_tag.length;
	var isExist = false;
	var newtag = '';
	tag=trim(tag);
	if(checktag(tag)) {
		for(var i=0;i<len;i++)
			if(arr_tag[i] == tag )
				isExist = true;
			else if (arr_tag[i].length > 0)
				newtag = newtag + ' ' + arr_tag[i];
		if(!isExist)		
			newtag = newtag + ' ' + tag;
		document.getElementById(htmlid).value = newtag;
	}
}

function checktag(Sting) {

	if(Sting.length < 2 || Sting.length > 10) {
		alert('TAG长度不符合要求');
		return false;
	}
	//常用的符号
	var compStr = "§№☆★○●◎◇◆□■△▲※→←↑↓〓＃＆＠＼＾＿￣〖〗【】（）［］｛｝．』『」「》《〉〈〕〔‘’“”々～‖∶＂＇｀｜¨ˇˉ·—…！？：；，、。  ~!@#$%^&*()+={}|[]\\:;\"'<,>?/";
    var length2 = Sting.length;
	
	for (var iIndex=0;iIndex<length2;iIndex++) {
		var temp1 = compStr.indexOf(Sting.charAt(iIndex));
		if(temp1>=0){
			alert('TAG名中包含非法字符');
			return false;
		}
	}
	return true;
}

function trim(s)
{
	try
	{
		return s.replace(/^\s+|\s+$/g,"");
	}
	catch(e)
	{
		return s;
	}
}
