﻿function CheckAll(o) {
    var aReturn = document.getElementsByTagName("input")
    for (i = 0; i < aReturn.length; i++) {
        if (aReturn[i].type == "checkbox" && aReturn[i].disabled == false) {

            aReturn[i].checked = o.checked;
        }
    }
}

function CheckSelect() {
    var isCheck = false;
    var aReturn = document.getElementsByTagName("input")
    for (i = 0; i < aReturn.length; i++) {
        if (aReturn[i].type == "checkbox" && aReturn[i].id != "allCheckBox" && aReturn[i].checked) {
            isCheck = true;
            break;
        }
    }
        
    if (!isCheck) 
    {
        alert("请至少选择一项!");
        return false;
    }
}


function CheckAllByName(obj, sName) {
  
    var aReturn = document.getElementsByTagName("input");
    for (i = 0; i < aReturn.length; i++) {
        if (aReturn[i].type == "checkbox" && aReturn[i].disabled == false) {
            var ssName = aReturn[i].name;
            if (ssName.indexOf(sName) != -1) 
            {
                aReturn[i].checked = obj.checked;
            }
            //aReturn[i].checked = obj.checked;
        }
    }
}

function checkAllByName(checked) //getElementsByName方法实现全选函数
{
   var allCheckBoxs=document.getElementsByName("isBuy") ;//获得所有HTML元素name为isBuy的标签
   for (var i=0;i<allCheckBoxs.length ;i++)
   {
       if(allCheckBoxs[i].type=="checkbox"){  //可能有重名的其他类型元素，如图片、控件等，所以要判断类型
	   		allCheckBoxs[i].checked=checked;

		}
   }
}

//控制多行输入框的长度
function doKeypress(control, maxLength) {
    var element = control
    if (!isNaN(maxLength)) {
        maxLength = parseInt(maxLength)
        var oTR = element.document.selection.createRange()
        if (oTR.text.length >= 1)
            event.returnValue = true
        else if (element.value.length > maxLength - 1)
            event.returnValue = false
    }
}

function doKeydown(control, maxLength) {
    var element = control
    setTimeout(function() {
        maxLength = parseInt(maxLength)
        if (!isNaN(maxLength)) {
            if (element.value.length > maxLength - 1) {
                var oTR = window.document.selection.createRange()
                oTR.moveStart("character", -1 * (element.value.length - maxLength))
                oTR.text = ""
            }
        }
    }, 1)
}

function doBeforePaste(control, maxLength) {
    if (!isNaN(maxLength))
        event.returnValue = false
}

function doPaste(control, maxLength) {
    var element = control
    if (!isNaN(maxLength)) {
        event.returnValue = false
        maxLength = parseInt(maxLength)
        var oTR = element.document.selection.createRange()
        var iInsertLength = maxLength - element.value.length + oTR.text.length
        var sData = window.clipboardData.getData("Text").substr(0, iInsertLength)
        oTR.text = sData;
    }
}

//只能输入数字
function keypress(e) {
    if ([e.keyCode || e.which] == 8) //this is to allow backspace
        return true;
    if ([e.keyCode || e.which] < 48 || [e.keyCode || e.which] > 57)
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
}



//全选控件内的checkbox
function CheckAllInControl(self, pre) {
    var inputList = document.getElementById(pre).getElementsByTagName("input");
    for (var i = 0; i < inputList.length; i++) {
        if (inputList[i].type == 'checkbox' && inputList[i].id.indexOf(pre) != -1) {
            inputList[i].checked = self.checked;
        }
    }
}



function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_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 = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
    }


function OpenNewPageNoMenu(url) {
    window.open(url, '', 'width=830,scrollbars=yes,resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
}

function CheckSelectOne() {
    var aReturn = document.getElementsByTagName("input");
    var count = 0;
    for (i = 0; i < aReturn.length; i++) {
        if (aReturn[i].type == "checkbox" && aReturn[i].id != "allCheckBox" && aReturn[i].checked) {
            count++;
            if(count>1)
                break;
        }
    }

    if (count > 1) {
        alert("只能选择一项!");
        return false;
    }
    else if(count==0) {
        alert("请选择一项!");
        return false;
    }
}
