function open_window(theurl,target)
{
	window.open(theurl,'');	
}

//随机整数
function rand(min,max) 
{
	return Math.round((max-min) * Math.random() + min);
}

function regexp_input(reg,evt,this_obj)
{
	evt = evt ? evt : (window.event ? window.event : null);
    var srcElem    = evt.srcElement ? evt.srcElement : evt.target;

	if(document.selection)
	{
		var oSel    = document.selection.createRange();
		var srcRange    = srcElem.createTextRange();
		oSel.setEndPoint("StartToStart", srcRange);
	}
	else
	{
		var iStart = srcElem.selectionStart;
		var iEnd = srcElem.selectionEnd;
		oSel.text = srcElem.value.substring(0,iEnd);
	}

    var num = oSel.text + String.fromCharCode(evt.keyCode) + srcRange.text.substr(oSel.text.length);
    evt.returnValue = reg.test(num);
}

//将指定表单中的所有元素转化连接成字符串
function form_to_str(form_name)
{
	var form_obj = document.forms[form_name];
	var formstr = sign = select_sign = '';
	for(var i=0;i<form_obj.length;i++)
	{
		if(form_obj[i].type == 'checkbox')
		{
			if(!form_obj[i].checked) continue;
		}

		if(form_obj[i].type == 'radio')
		{
			if(!form_obj[i].checked) continue;
		}
		
		if(form_obj[i].type.indexOf('select') < 0)
		{
			formstr += sign + form_obj[i].name + '=' + encodeURIComponent(form_obj[i].value);
			sign = '&';
		}
		else
		{
			//alert(form_obj[i].length);
			for (var j = 0; j < form_obj[i].length; j++)
			{
				if(form_obj[i].options[j].selected)
				{
					formstr += sign + form_obj[i].name + '=' + encodeURIComponent(form_obj[i].options[j].value);
					sign = '&';
				}
			}
		}
	}

	return formstr;
}

/*
例如:disabled_switch('obj_id');//复选框开启或关闭指定对象
*/
function disabled_switch(obj_id)
{
	var obj = document.getElementById(obj_id);

	if (obj.disabled == true)
	{
		obj.disabled = false;
	}
	else
	{
		obj.disabled = true;
	}
}

/*
全选/全不选
例如:check_box_all('chkall','id[]');//前部分为负责全选服选框的位置,后面是被控制复选框名称
*/
function check_box_all(all_obj_id,op_obj_name) 
{
	var obj = document.getElementById(all_obj_id);
	var chk = obj.checked;

	for (i=0;i<document.all.length;i++) 
	{
		if(document.all[i].name == op_obj_name)
		{
			document.all[i].checked=chk;
		}
	}
}

/*
显示/隐藏指定 单元
例如:display('obj_id',true);
*/
function display(obj_id,state) 
{
	var obj = document.getElementById(obj_id);

	if(state == true)
	{
		obj.style.display = '';
	}
	else
	{
		obj.style.display = 'none';
	}
}

/*
//隐藏显示指定对象
例如:display_switch(obj_id);
*/
function display_switch(obj_id)
{
	var obj = document.getElementById(obj_id);

	if (obj.style.display == 'none')
	{
		obj.style.display = '';
	}
	else
	{
		obj.style.display = 'none';
	}
}

/*
//隐藏显示指定对象
例如:checkbox_switch(obj_id);
*/
function checkbox_switch(obj_id)
{
	var obj = document.getElementById(obj_id);

	if (obj.checked == true)
	{
		obj.checked = false;
	}
	else
	{
		obj.checked = true;
	}
}

/*
开启/关闭指定 单元
例如:disabled(document.newsclass.move,true);
*/
function disabled(obj_id,state) 
{
	var obj = document.getElementById(obj_id);
	obj.disabled = state;
}

/*
选择指定列表框中的指定项目 注意此项需要在列表框对象后使用
例如:set_list_box(document.form.province, "河北");
*/

function set_list_box(obj_id, val)
{
	var obj = document.getElementById(obj_id);

    for (i=0; i<obj.length; i++)
    {
        if (obj.options[i].value == val)
        {
            obj.options[i].selected = true;
            break;
        }
    }
}

/*
选择指定单选框中的指定项目 注意此项需要在单选框对象后使用
例如:set_radio_box(document.form.sex, "男");
*/
function set_radio_box(obj_id, val)
{
	var obj = document.getElementById(obj_id);

    for (i=0; i<obj.length; i++)
    {
        if (obj[i].value == val)
        {
            obj[i].checked = true;
            break;
        }
    }
}

/*
选择指定复选框中的指定项目 注意此项需要在复选框对象后使用
例如:set_check_box(document.form.like, "1,3,4");//字符串用 半角逗号 "," 分割
*/
function set_check_box(obj, strs)
{
	val=strs.split(",");
	for(x=0;x<val.length;x++)
	{
		for (i=0; i<obj.length; i++)
		{
			if (obj[i].value == val[x])
			{
				obj[i].checked = true;
				break;
			}
		}
	}
}

/*
选择指定复选框中的指定项目 注意此项需要在复选框对象后使用
例如:set_check_box(document.form.like, "1,3,4");//字符串用 半角逗号 "," 分割
*/
function link_submit(obj, name, value)
{
	obj.innerHTML += "<input type=\"hidden\" name=\"" + name + "\" value=\"" + value +"\"/>";
	obj.submit();
}

/**
将复选框数据连接成字符串
例如:checkbox_str(document.form.like,document.form.likes);
其中第一个参数为复选框组对象，第二个参数是自定义的一个隐藏域来暂存 连接好的字符串
此函数需放在 form 标键中 激活事件为 onsubmit
如果PHP接收则接收 links这个隐藏域值 而非 link组值
*/
function checkbox_str(itemobj,hiddenobj) 
{ 
　hiddenobj.value = ""; 
　if ( !itemobj.length ) // 只有一个复选框，itemobj.length = undefined 
　{ 
　　if (itemobj.checked)
	{
		hiddenobj.value = itemobj.value; 
	}
　} 
　else 
　{ 
　　for ( i = 0 ; i < itemobj.length ; i++ ) 
　　{ 
　　　if ( itemobj(i).checked ) // 复选框中有选中的框 
　　　{ 
　　　　hiddenobj.value = itemobj(i).value; 
　　　　for ( j = i + 1 ; j < itemobj.length ;j++ ) 
　　　　{ 
　　　　　if ( itemobj(j).checked ) 
　　　　　{ 
　　　　　　hiddenobj.value += ","; //用空格做分割符 
　　　　　　hiddenobj.value += itemobj(j).value; 
　　　　　} 
　　　　} 
　　　　break; 
　　　} 
　　} 
　} 
　//return true; 
} 

/**
 * 选择一个列表的所有选项
 */
function sel_all_option(objSelect)
{
    for(var i = 0; i < objSelect.length; i++)
        objSelect.options[i].selected = true;
}

/**
 * 往一个下拉列表中增加一项
 */
function put_option(objSelect,v,t)
{
    var objOpt = document.createElement("option");
    objOpt.value = v;
    objOpt.text = t;
    try{
        objSelect.add(objOpt);
    }catch(e){
        objSelect.add(objOpt, null);
    }
}

/**
 * 在两个列表中移动项目
 */
function move_option(sour, dest)
{
	for (var i=0 ; i<= sour.options.length -1 ; i++)
	{
		//如果这条被选中则复制倒列表框1中
		if (sour.options [i].selected)
		{
			var element = document.createElement("option");
			element.value = sour.options[i].value;
			element.text = sour.options [i].text;
			element.innerHTML = sour.options [i].text;
			dest.appendChild(element);
		}
	}  

	//删除选定记录 
	for (var i=0 ; i< sour .options .length ; i++)
	{
		if (sour .options [i].selected)
		{
			sour.remove(i);
			i = i - 1 ;
		}
	}
}


/*
取字串的字符个数(包括汉字)
*/
function strlen( _sz )
{
    if( _sz.length <= 0 ) return 0 ;
    //byteLeng统计byte的长度
    byteLeng = _sz.length;
    for( i =0 ; i < _sz.length  ; i++ )
	{
        //对每个字符进行转义编码：规则：只对不可见字符、空格、非ASCII码进行转码。
        //转码结果为 unicode码。如：“你”，转为：%u4F60
        szTmp = escape(_sz.charAt(i),'UTF-8');
        
		//alert("escape is:"+szTmp);
        if(szTmp.length>=6)
		{
            //如果为双字节的字符，则再加1。
            byteLeng ++;
        }
    }
    return byteLeng;
}

/**去掉前后空格:包括全角空格*/
function trim(str)
{   
	str += "";
	while( (str.charAt(0)==' ')||(str.charAt(0)=='　')||(escape(str.charAt(0))=='%u3000') )     
	{
		str=str.substring(1,str.length);
	}

	while( (str.charAt(str.length-1)==' ')||(str.charAt(str.length-1)=='　')||(escape(str.charAt(str.length-1))=='%u3000') )
	{
		str=str.substring(0,str.length-1);
	}
	return str;
}

/*
显示疑问窗口多在删除数据前最后确认使用
例如:doubt('确定删除此项吗？');
*/
function doubt(val)
{
	if(confirm(val))
	{
		return true;
	}
	return false;
}

/*
设置鼠标滑过后当前对象背景颜色
移开 onmouseout="setbgcolor('#999999');"
在上 onmouseover="setbgcolor('#666666');"
*/
function setbgcolor(obj,val)
{
	obj.style.backgroundColor= val;
}

/*
设置鼠标滑过后当前对象背景颜色
移开 onmouseout="setbgcolor('#999999');"
在上 onmouseover="setbgcolor('#666666');"
*/
function setbgimage(obj,val)
{
	obj.style.backgroundImage='url('+val+')';
}

/*
拷贝内容至剪贴板
onclick="setCopy('[smile]')";
*/
function set_copy(str)
{
	try
	{
		clipboardData.setData('Text',str)
	}
	catch(e)
	{
	}
}

//获取并返回页面宽高
function get_page_size() 
{
	var xScroll,yScroll;
	if(window.innerHeight&&window.scrollMaxY) 
	{
		xScroll=document.body.scrollWidth;
		yScroll=window.innerHeight+window.scrollMaxY;
	} 
	else if(document.body.scrollHeight>document.body.offsetHeight) 
	{
		xScroll=document.body.scrollWidth;
		yScroll=document.body.scrollHeight;
	} 
	else 
	{
		xScroll=document.body.offsetWidth;
		yScroll=document.body.offsetHeight;
	}

	var windowWidth,windowHeight;
	if(self.innerHeight) 
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else if(document.documentElement&&document.documentElement.clientHeight) 
	{
		windowWidth=document.documentElement.clientWidth;
		windowHeight=document.documentElement.clientHeight;
	} 
	else if(document.body) 
	{
		windowWidth=document.body.clientWidth;
		windowHeight=document.body.clientHeight;
	} 

	if(yScroll<windowHeight) 
	{
		pageHeight=windowHeight;
	} 
	else 
	{
		pageHeight=yScroll;
	} 
	
	if(xScroll<windowWidth) 
	{
		pageWidth=windowWidth;
	} 
	else 
	{
		pageWidth=xScroll;
	} 
	
	array_page_size = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return array_page_size;
} 

//设置指定层对象处于屏幕中央
function center_div(obj_id)
{
	var div_obj = document.getElementById(obj_id);
	var page_size_array = get_page_size();
	
	x = page_size_array[0]/2 - Number(div_obj.style.width.replace('px','')) / 2;
	y = page_size_array[3]/2 - Number(div_obj.style.height.replace('px','')) / 2;

	var y = document.body.scrollTop + y ;

	//document.all.aaa.innerHTML = document.body.scrollTop;

	eval("div_obj.style.top = y");
	eval("div_obj.style.left = x");//移动层

	setTimeout("center_div('" + obj_id + "');", 50);//设置50毫秒后再调用函数center_div()
}

//将指定层放大到整个页面
function max_div(obj_id)
{
	var div_obj = document.getElementById(obj_id);
	var page_size_array = get_page_size();
	div_obj.style.width = '100%';//page_size_array[0];
	div_obj.style.height = page_size_array[1];
}

//
function div_box(center,bg)
{
	max_div(bg);
	center_div(center);
	display_switch(bg);
	display_switch(center);

}


////////////////////////////// 气泡提示 //////////////////////////////

function getParam(varName) 
{ 
	var query = location.search; 
	if (query != null || query != "") 
	{ 
		query = query.replace(/^\?+/, ""); 
		var qArray = query.split("&"); 
		var len = qArray.length; 
		if (len > 0) 
		{ 
			for (var i=0; i<len; i++) 
			{ 
				var sArray = qArray[i].split("=", 2); 
				if (sArray[0] && sArray[1] && sArray[0] == varName) 
				{ 
					return unescape(sArray[1]); 
				} 
			} 
		} 
	} 
	return null; 
}


var idialog = 
{
	is_ie : true,
	new_ie : false,

	checkup:function()
	{
		//检测是否为IE并判断版本
		try
		{
			var ievar = navigator.userAgent.match(/msie\s+([0-9\.]+)/i)[1];	
			if(ievar > 6)
			{
				this.new_ie = true;
			}
			
			this.is_ie = true;
		}
		catch (e)
		{
			this.is_ie = false;
		}
	},
	
	open:function(url,return_id,width,height,resizable,dialog)
	{
		this.checkup();

		resizable = resizable ? 1 : 0;
		dialog = dialog == null ? 1 : 0;

		if(width == 'auto') width = screen.width - 50;
		if(height == 'auto') height = this.is_ie ? screen.height - 250 : screen.height - 50;
		
		if(this.new_ie)
		{
			height = height - 50;
		}

		if (url.indexOf("?") > 0)
		{
			url += "&dialogCallbackId=" + return_id;
		}
		else
		{
			url += "?dialogCallbackId=" + return_id;
		}

		var x = parseInt(screen.width / 2.0) - (width / 2.0); 
		var y = parseInt(screen.height / 2.0) - (height / 2.0);

		if (this.is_ie && dialog) 
		{
			retval = window.showModelessDialog(url, window, "dialogWidth:"+width+"px; center:yes ;dialogHeight:"+height+"px; dialogLeft:"+x+"px; dialogTop:"+y+"px; help:no;status:no; directories:yes;scroll:no;resizable:"+resizable);
		} 
		else
		{
			var win = window.open(url, return_id, "top=" + y + ",left=" + x + ",scrollbars="+(dialog ? 0 : 1)+",dialog="+dialog+",modal=yes,width=" + width + ",height=" + height + ",resizable=" + resizable );
			eval('try { win.resizeTo(width, height); } catch(e) {}');
			win.focus();
		}
	},
	
	parent:function()
	{
		this.checkup();

		if (this.is_ie)
		{
			return window.dialogArguments;
		}
		else
		{
			return window.opener;
		}
	},

	/*
	返回指定数值到父窗口的input对象value中
	*/
	return_val : function(val)
	{
		var param = getParam('dialogCallbackId');
		try
		{
			this.parent().$(param).value = val ;
		}
		catch (e){alert(e);}

		window.close();
	},

	/*
	回调父窗口函数
	*/
	setCallback : function(val)
	{
		var param = getParam('dialogCallbackId');
		
		try
		{
			if(typeof(val) == 'string')
			{
				eval("this.parent()." + param +"('" + val + "');");
			}
			else
			{
				this.parent().eval(param)(val);
			}
		}
		catch (e){alert(e);}
	},
	
	/*
	回调父窗口函数并关闭窗口
	*/
	callback : function(val)
	{
		this.setCallback(val);
		window.close();
	}
}

/////////////////// 标签对象 //////////////////////

function ilabel()
{
	this.item = [];

	this.add = function(label,content,url)
	{
		try	
		{
			var className = document.getElementById(label).className;
		}
		catch (e)
		{
			var className = '';
		}
		
		for(var i = 0;i < this.item.length;i++){}

		this.item[i] = {label:label,content:content,className:className,url:url}
	}

	this.click = function(label,className)
	{
		for(var i = 0 ; i < this.item.length ; i++)
		{
			try
			{
				document.getElementById(this.item[i].label).className = this.item[i].className;
				document.getElementById(this.item[i].content).style.display = 'none';
			}
			catch (e){}

			if(this.item[i].label == label )
			{
				var content = this.item[i].content;
				var url = this.item[i].url;
			}
		}

		try
		{
			document.getElementById(label).className = className;
			document.getElementById(content).style.display = '';

			if(url)
			{
				xmlhttp.sendRequest('GET', url,'', xmlhttp.get_html, content);
			}
		}
		catch (e){}
	}
}
