
function trim(str)
{
	str = str.replace(/^\s+|\s+$/g, '');
	str = str.replace(/\s+/g, ' ');
	return str;
}

function OpenWindow(url, name, left, top, width, height, features)	//open a new window;
{
	if(url==null) return;
	if(name==null || name=='') name='win'+Math.floor(Math.random()*100);
	if(width==null || width==0) width=screen.availWidth-5;		//availWidth;
	if(height==null || height==0) height=screen.availHeight;	//availHeight;
	if(left==null) left=Math.floor((screen.availWidth-width)/2);
	if(top==null) top=Math.floor((screen.availHeight-height)/2);
	if(features==null) features='';

	if (left < 1 && left > 0)
		left = left * screen.availWidth;

	if (top < 1 && top > 0)
		top = top * screen.availHeight;

	features += ',alwaysRaised' + ',left=' + left + ',top=' + top + ',width=' + width + ',height=' + height;
	var winref=window.open(url, name, features);
	//winref.moveTo(left, top);
	//winref.topwin=self;
	winref.focus();
	return winref;
}

function GetObj(n, d) {
    var p, i, x;

    if (!d)
        d = document;

    if ((p = n.indexOf("?")) > 0 && parent.frames.length){
        d = parent.frames[n.substring(p+1)].document;
        n = n.substring(0, p);
    }

    if (!(x = d[n]) && d.all)
        x = d.all[n];

    for (i=0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];

    for (i = 0; !x && d.layers && i < d.layers.length; i++)
        x = GetObj(n,d.layers[i].document);

    if (!x && d.getElementById)
        x = d.getElementById(n);

    return x;
}

function checkAll(elm, flag, reg)		//checked all checkbox;
{
	if(!elm) return;
	if(flag==null) flag=true;
	if(!reg) reg='0/1';
	var ary=reg.split('/');
	elm=elm.length?elm:[elm];
	for(var i=parseInt(ary[0]); i<elm.length; i+=parseInt(ary[1])) {
		if(!elm[i].disabled) {
			elm[i].checked=flag;
			if (elm[i].onclick)
				elm[i].onclick();
		}
	}
}

function setElmByVal(elm, value, sep)
{
	if (typeof elm == 'string')
		elm = document.forms[0][elm];

	if(!elm || value==null) return;

	switch(elm.type || elm[0].type){
		case 'text':
		case 'textarea':
		case 'hidden':
		case 'password':
			elm.value=value;
			break;
		case 'select-one':
			setOption(elm, value);
			break;
		case 'select-multiple':
			if(sep==null) sep='&';
			var ary=value.split(sep);
			elm.selectedIndex=-1;
			for(var i=0; i<elm.length; i++)
				for(var j=0; elm.options[i].value && j<ary.length; j++)
					if(elm.options[i].value==ary[j]){
						elm.options[i].selected=true;
						break;
					}
			break;
		case 'radio':
			elm=elm.length?elm:[elm];
			for(var i=0; i<elm.length; i++){
				elm[i].checked = true;
				elm[i].checked = (elm[i].value == value);
				if(elm[i].checked) break;
			}
			break;
		case 'checkbox':
			elm=elm.length?elm:[elm];
			if(sep==null) sep='&';
			var ary=value.split(sep);
			for(var i=0; i<elm.length; i++){
				var j=0;
				for(; j<ary.length; j++)
					if(elm[i].value==ary[j]) break;
				elm[i].checked = (j < ary.length)
			}
			break;
		case 'reset':
		case 'button':
		case 'submit':
		default: //undefine;
	}
}

function setElement()
{
	var form=arguments[0];
	if(typeof form=='string') form=document.forms[form];
	for(var i=1; i+1<arguments.length; i+=2)
		setElmByVal(form[arguments[i]]||arguments[i], arguments[i+1]||arguments[i+1]);
}

//get element's value and(or) text;
// val value, txt text, null value and text
function getElmValTxt(elm, flag)
{
	if (!flag)
		flag = 'both';
	var ary=new Array();

	if(!elm) return ary;
	switch (elm.type || elm[0].type){
		case 'text':
		case 'textarea':
		case 'hidden':
		case 'password':
			ary.push(elm.value);
			break;
		case 'select-one':
			if(elm.selectedIndex!=-1) {
				if (flag != 'txt')
					ary.push(elm.options[elm.selectedIndex].value);
				if (flag != 'val')
					ary.push(elm.options[elm.selectedIndex].text);
			}
			if (ary.length==0) {
				ary.push('');
				if (flag == 'both')
					ary.push('');
			}
			break;
		case 'select-multiple':
			for(var i=0; i<elm.length; i++) {
				if(elm.options[i].selected) {
					if (flag != 'txt')
					 	ary.push(elm.options[i].value);
					if (flag != 'val')
						ary.push(elm.options[i].text);
				}
			}
			if (ary.length==0) {
				ary.push('');
				if (flag == 'both')
					ary.push('');
			}
			break;
		case 'radio':
			if(elm.checked) ary.push(elm.value);
			else for(var i=0; i<elm.length; i++)
				if(elm[i].checked) {ary.push(elm[i].value); break;}
			if(ary.length==0) ary.push('');
			break;
		case 'checkbox':
			if(elm.checked) ary.push(elm.value);
			else for(var i=0; i<elm.length; i++)
					if(elm[i].checked) ary.push(elm[i].value);
			if(ary.length==0) ary.push('');
			break;
		case 'reset':
		case 'button':
		case 'submit':
		default: ary.push('');
	}
	return ary;
}

function getKeyVal(value, index, vsep)	//get key value accordding to index;
{
	vsep = vsep || ':';
	var indexs=index.split(vsep);
	var ary0=value.split(vsep);
	var ary1=new Array();
	for(var i=0; i<indexs.length; i++)
		ary1.push(ary0[indexs[i]]);
	return ary1.join(vsep);
}

function setOption(list, value, index, ischange)
{
	list = document.forms[0][list] || list;
	if (!list) return;

	// if value is int the convert it to string
	value = '' + value;

	var ary = value.split('&');
	for(var i=0; i<list.length; i++){
		list.options[i].selected=false;
		var val = list.options[i].value;
		if (index) val = getKeyVal(val, index);

		for(var j=0; val && j<ary.length; j++)
			if(val == ary[j]){
				list.options[i].selected=true;
				if (ischange&&list.onchange) {
					list.onchange();
				}
				if(list.type=='select-one') return;
				break;
			}
	}

	if (list.type == 'select-one' && list.selectedIndex < 0 && list.length > 0) {
		list.selectedIndex = 0;
	}
}


function getSelCount(elm)
{
	elm = document.forms[0][elm] || elm;
	var count=0;
	if(!elm) return -1;
	if(elm.type!='select-one' && elm.type!='select-multiple') return -1;
	for(var i=0; i<elm.length; i++)
		if(elm.options[i].selected && elm.options[i].value) count++;

	return count;
}

function getChkCount(elm)
{
	if (typeof elm == 'string')
		elm = document.forms[0][elm];

	var count=0;
	if(!elm) return -1;
	elm=elm.length?elm:[elm];
	if(elm[0].type!='radio' && elm[0].type!='checkbox') return -1;
	for(var i=0; i<elm.length; i++)
		if(elm[i].checked) count++;
	return count;
}

function checkNotNull(elm)
{
	if(!elm) return false;

	switch(elm.type || elm[0].type){
		case 'text':
		case 'textarea':
		case 'hidden':
		case 'password':
			return trim(elm.value)?true:false;
		case 'select-one':
		case 'select-multiple':
			return getSelCount(elm)>0;
		case 'radio':
		case 'checkbox':
			return getChkCount(elm)>0;
		case 'reset':
		case 'button':
		case 'submit':
		default: return false;
	}
	return false;
}

function isEmail(str, flag)//str=>a@b.c flag=>[false|true][0|1]
{
	var str0=str.replace(/^\s+|\s+|\s+$/g, '');
	if(str0==null || str0=='') return flag;
	var i=str0.lastIndexOf('@');
	var j=str0.lastIndexOf('.');
	return 0<i && i+1<j && j<str0.length-1;
}

//check number in range [not] between min and max;flag=>[false|true][0|1]
function ckNumber(str, flag, min, max)
{
    var str0=trim(str);
	str0 = str0.replace(/^0+[^0]/, '');
	if(flag==null) flag=true;
	if(str0==null || str0=='') return flag;
	flag=parseInt(str0)==str0;
	if(min==null && max!=null)
		flag &=parseInt(str0)<=max;
	else if(min!=null && max==null)
		flag &=parseInt(str0)>=min;
	else if(min<=max)
		flag &=min<=parseInt(str0) && parseInt(str0)<=max;
	else if(min>max)
		flag &=min<=parseInt(str0) || parseInt(str0)<=max;
	return flag;
}

function chckvid()
{
	var form = arguments[0];

	if (typeof form == 'string')
		form = document.forms[form];

	for (var i = 1; i < arguments.length; i += 3)
	{
		var obj = arguments[ i ];
		var typ = arguments[ i + 1 ];
		var txt = arguments[ i + 2 ];

		if (txt.charAt(0) == ':')
			txt = txt.substr(1, txt.length);
		else
			txt = '请选中' + txt;

		var notnull = arguments[i + 3];

		if (!form || !form[obj]){
			alert(txt);
			return false;
		}
		switch(typ){
			case 'radio':
			case 'chk':
				if(!checkNotNull(form[obj])) {
					alert(txt);
					return false;
				}
				break;
			case 'txt':
			case 'sel':
				if(!checkNotNull(form[obj])) {
					alert(txt);
					form[obj].focus();
					return false;
				}
				break;
			case 'pwd':
				var pwd = form[obj].value;
				var rpwd = form[arguments[i+2]].value;
				if (pwd.length > 16 || pwd.length < 6){
					alert("要求密码是 6-16 个字符");
					form[obj].focus();
					return false;
				}else if (pwd != rpwd){
					alert("确认密码有误");
					form[obj].focus();
					return false;
				}
				break;
			case 'mail':
				notnull=arguments[i+2];
				var flag=notnull=='y'?false:true;
				if(!isEmail(form[obj].value, flag)){
					alert('请输入一个有效的Email地址');
					form[obj].focus();
					return false;
				}
				break;
			case 'digit':
				var flag=notnull=='y'?false:true;
				if(!ckNumber(form[obj].value, flag, 0)){
					alert(txt);
					form[obj].focus();
					return false;
				}
				i++;
				break;
		}
	}
	return true;
}

function blankrows(rows, cols, row, col) {
	row = row || "<TR>"; col = col || "<TD>&nbsp;";
	for (var i = 0; i < rows; i++) {
		document.write("\n" + row);
		for (var j = 0; j < cols; j++)
			document.write(col);
	}
}

function highlightAll(list)			//highlight all select options;
{
	for(var i=0; i<list.length; i++)
		if(list.options[i].value && !list.options[i].disabled) list.options[i].selected=true;
		else list.options[i].selected=false;
}

function page(form, no) {
	form = document.forms[form];
	form['p'].value = no;
	form.submit();
}

<!-- begin (add by wu at 080402) -->
function $(e){
	return (typeof e == 'object') ?  e : document.getElementById(e);
}
//check object's relation 
function chkRel() {
	if (arguments.length<2) { return; }
	var oParent = $(arguments[0]);
	for (var i=1; i<arguments.length; i++) {
		var oChild = $(arguments[i]);
		if (parseInt(oParent.value)) {	
			oChild.disabled = false; 
		} else {
			oChild.disabled = true; oChild.selectedIndex = 0;  
		}	
	}	
}
<!-- end (add by wu at 080402) -->
