function $(key){
	return document.getElementById(key);
}
function $$(key){
	return document.getElementsByName(key);
}
function $$$(key){
	return document.getElementsByTagName(key);
}

function check_all(type){
	var c=$$$("input");
	if(type == 1){
		for(i=0;i<c.length;i++){
			if(c[i].type=='checkbox')
			c[i].checked=true;
		}
	}else{
		for(i=0;i<c.length;i++){
			if(c[i].type=='checkbox'){
				if(c[i].checked==true){
					c[i].checked = false;
				}else{
					c[i].checked = true;
				}
			}
		}
	}
}

function initAjax(){
	var ajax = false;
	try{
		ajax = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			ajax = false;
		}
	}
	if (!ajax && typeof XMLHttpRequest!='undefined'){
		ajax = new XMLHttpRequest();
	}
	return ajax;
}

function getRadioValue(name){
	var radioObject = $$(name);
	try{
		if(radioObject == null){
			return false;
		}
		var length = radioObject.length;
		if(length == null){
			if(radioObject.checked){
				return radioObject.value;
			}else{
				return false;
			}
		}else{
			for(i=0; i<length; i++){
				if(radioObject[i].checked)
				return radioObject[i].value;
			}
			return false;
		}
	}catch(e){
		return false;
	}
}

function selectRadio(name,value){
	var radioObject = $$(name);
	if(value === ''){
		radioObject[0].checked = true;
		return;
	}
	for (var i=0; i<radioObject.length; i++){
		if(radioObject[i].value == value){
			radioObject[i].checked = true;
			break;
		}
	}
}
function selectCheckbox(name,value){
	var checkObject = $$(name);
	var values = value.split(',');
	for(var j=0; j<values.length; j++){
		for (var i=0; i<checkObject.length; i++){
			if(checkObject[i].value == values[j]){
				checkObject[i].checked = true;
				break;
			}
		}
	}
}
function selectOption(name,value){
	var options = $$(name)[0].options;
	for (var i=0; i<options.length; i++){
		if(options[i].value === value){
			options[i].selected = true;
			break;
		}
	}
}

function getBytes(str) {   
	var cArr = str.match(/[^\x00-\xff]/ig);   
	return str.length + (cArr == null ? 0 : cArr.length);   
}

// =========================================================================================================
// ͨ��ģʽ�����(������IE,Firefox,Opera,Netscape)
// ==========================================================================================================
function popWin(theURL,winName,theW,theH,showAsModal){
    theTop = (window.screen.height-theH)/2;
    theLeft = (window.screen.width-theW)/2;
    var features = "toolbar=0,scrollbars=yes,left=" + theLeft + ",top=" + theTop + ",width=" + theW + ",height=" + theH;
    
    window.SubWin = window.open(theURL,winName,features);
    window.SubWin.focus();
    
    if(showAsModal){
        window.CtrlsDisabled = new Array();
        DisableCtrls("INPUT;SELECT;TEXTAREA;BUTTON");
    }

    function DisableCtrls(tagNameStr){
        var arrTags = tagNameStr.split(";");
        for(var i=0;i<arrTags.length;i++){
            var arrEle = document.getElementsByTagName(arrTags[i]);
            PushToCtrlsDisabled(arrEle);
        }

        for(var i=0;i<window.CtrlsDisabled.length;i++){
            window.CtrlsDisabled[i].disabled = true;
            window.CtrlsDisabled[i].readOnly = true;
        }
    }

    function PushToCtrlsDisabled(arrEle){
        for(var i=0;i<arrEle.length;i++){
            if(!arrEle[i].disabled){
                window.CtrlsDisabled.push(arrEle[i]);
            }
        }
    }
    
    window.onfocus = function(){
        if(window.SubWin && showAsModal){
            if(window.SubWin.closed == true || typeof(window.SubWin.closed) == "undefined"){
                for(var i=0;i<window.CtrlsDisabled.length;i++){
                    window.CtrlsDisabled[i].disabled = false;
                    window.CtrlsDisabled[i].readOnly = false;
                }
            }else{
                window.SubWin.focus();
            }
        }
    }
}
