/*只能操作一条记录的提示*/
var onlyOne = "本操作只能选择一条列表记录！";
/*提醒选择记录*/
var checkFirst = "请选择列表记录！";

function closeWindow()
{
	window.close();
}

function resizeWindow(width, height)
{
	window.resizeTo(width,height);
}

function newin(url,width,height)
{
	window.open(url, "",
				"width=" + width + ",height=" + height
				+ ",menubar=0,status=1,resizable=1");
}

function openCenterWin(url,width,height)
{
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top  = parseInt((screen.availHeight/2) - (height/2));
	
	window.open(url, "",
				"width=" + width + ",height=" + height
				+ ",menubar=0,status=1,resizable=1,left=" + left 
				+ ",top=" + top + ",screeX=" + left + ",screeY=" + top);
}

//选中所有复选框
function selectAll(form)
{
    if(form == null || form.cb == null)
    {
        return;
    }

    if(form.cb.length != null)
    {
        for(i = 0; i < form.cb.length; i++)
            form.cb[i].checked = form.ID_ALL.checked;
    }
    else
    {
        form.cb.checked = form.ID_ALL.checked;
    }
}

//是否只选中一条记录, 是，返回选中的CHeckBox的值，否则返回""
function isCheckedOne(form)
{
    var pkValue = "";
    var count = 0;
    //当前选中的记录数
    for(var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if(e.name != 'ID_ALL' && e.type.toLowerCase() == "checkbox")
        {
            if(e.checked)
            {
                count++;
                pkValue = e.value;
                if(count > 1)
                    break;
            }
        }
    }

    if(count == 1) //只有一个被选中
    {
        return pkValue;
    }
    else if(count == 0)
    {
        alert(checkFirst);
        return "";
    }
    else if(count > 1)
    {
        alert(onlyOne);
        return "";
    }
}

//是否有选中的记录，有选中：返回true,没有选中：返回false
function isChecked(form)
{
    var count = 0;
    for(var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if(e.name != 'ID_ALL' && e.type.toLowerCase() == "checkbox")
        {
            if(e.checked)
            {
                count++;
                break;
            }
        }
    }

    return (count > 0);
}

/*
 *预载入图片
 */
function preloadImages() 
{ //v3.0
  	var d=document; 
	if(d.images)
	{ 
		if(!d.arrImg) 
		{
			d.arrImg=new Array();
		}
    	var i, j=d.arrImg.length, a=preloadImages.arguments; 
		for(i=0; i<a.length; i++)
		{
    		if (a[i].indexOf("#")!=0)
			{ 
				d.arrImg[j]=new Image; 
				d.arrImg[j++].src=a[i];
			}
		}
	}
}

function swapImgRestore() 
{ //v3.0
  	var i,x,a=document.arrImgSrc; 
	for(i=0; a&&i<a.length&&(x=a[i])&&x.oSrc; i++) 
	{
		x.src=x.oSrc;
	}
}

function findObj(n, d) 
{ //v4.01
  	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=findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	
	return x;
}

function swapImage()
{ //v3.0
  	var i,j=0,x,a=swapImage.arguments; 
	document.arrImgSrc=new Array; 
	for(i=0;i<(a.length-2);i+=3)
	{
  	 	if ((x=findObj(a[i]))!=null)
		{
			document.arrImgSrc[j++]=x;
			if(!x.oSrc)
			{ 
				x.oSrc=x.src; 
			}
			x.src=a[i+2];
		}
	}
}

function setTextOfTextfield(objName, x, newText) 
{ //v3.0
  	var obj = findObj(objName); 
	if (obj) 
	{
		obj.value = newText;
	}
}

function validateForm() 
{ //v4.0
  	var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
  	for (i=0; i<(args.length-2); i+=3)
	{ 
		test=args[i+2]; 
		val=findObj(args[i]);
    	if(val) 
		{ 
			nm=val.name; 
			if ((val=val.value)!="") 
			{
      			if (test.indexOf('isEmail')!=-1) 
				{ 
					p=val.indexOf('@');
        			if (p<1 || p==(val.length-1)) 
						errors+='- '+nm+' must contain an e-mail address.\n';
      			} 
				else if (test!='R') 
				{ 
					num = parseFloat(val);
        			if (isNaN(val)) 
						errors+='- '+nm+' must contain a number.\n';
        			if (test.indexOf('inRange') != -1)
					{ 
						p=test.indexOf(':');
          				min=test.substring(8,p); 
						max=test.substring(p+1);
          				if (num<min || max<num) 
							errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    				} 
				} 
			} 
			else if (test.charAt(0) == 'R') 
				errors += '- '+nm+' is required.\n'; 
		}
  	} 
	if (errors) 
		alert('The following error(s) occurred:\n'+errors);
  	document.returnValue = (errors == '');
}

function openModelWin(url, arg, w, h)
{
    return window.showModalDialog(url, arg, "dialogWidth:" + w + "; dialogHeight:" + h + "; status:no; scroll:auto");
}

//得到CheckBox 或 Radio 选中项的值，也可以作为判断选中记录条数
//返回选中的值，多值用逗号分隔，没有选中值则返回""，如果没有选中时提示参数不为空，则提示 noSelHint 的内容。
//form:Form的名字，name:CheckBox 或 Radio 的名字，noSelHint:没有选中时提示的语句，
//limitCheckNum:限定选中数目,如果为0，则不限定
function getCheckedValue(form, name, noSelHint, limitCheckNum, onlyHint)
{
    var pkValue = "";
    var count = 0;

    for(var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if(e.name == name
            && (e.type.toLowerCase() == "checkbox" || e.type.toLowerCase() == "radio" ))
        {
            if(e.checked)
            {
                count++;
                pkValue += "," + e.value;
                if(limitCheckNum > 0 && count > limitCheckNum) //如果限定选中数目
                {
                    alert(onlyHint);
                    return "";
                    break;
                }
            }
        }
    }

    if(pkValue != "")
    {
        pkValue = pkValue.substring(1); //去掉开始的一个逗号
    }

    if(count == 0) //没有选中
    {
        if(noSelHint != null && noSelHint != "")
            alert(noSelHint);
        return "";
    }
    else if(count > limitCheckNum)
    {
        alert(limitCheckNum);
        return "";
    }
    else
    {
        return pkValue;
    }
    
}

