/*
 * 系统全局js脚本
 */
/*非空校验*/
function isNull(id,title){
	var obj = document.getElementById(id);
	var value = obj.value;
	if(value==null || value.length==0){
		alert(title+"不能为空");
		obj.value="";
		obj.focus();
		return false; 
	}
	return true;
}

function disableSubmit(finalResult, submitButtonId){
    if (finalResult) {
        document.getElementById(submitButtonId).disabled = true;
        return finalResult;
    }
    else {
        return finalResult;
    }
}

function getElementById(name){
	return document.getElementById(name);
}

function batchDelete(action, checkboxName, form){
    if (!hasOneChecked(checkboxName)) {
    	alert('请选择要操作的对象!');
        return;
    }
    if (confirm('确定执行[删除]操作?')){
        form.action = action;
        form.submit();
    }
}

function hasOneChecked(name){
    var items = document.getElementsByName(name);
    if (items.length > 0) {
        for (var i = 0; i < items.length; i++) {
            if (items[i].checked == true) {
                return true;
            }
        }
    }
    else {
        if (items.checked == true) {
            return true;
        }
    }
    return false;
}

function setAllCheckboxState(name, state){
    var elms = document.getElementsByName(name);
    for (var i = 0; i < elms.length; i++) {
        elms[i].checked = state;
    }
}

function getReferenceForm(elm){
    while (elm && elm.tagName != 'BODY') {
        if (elm.tagName == 'FORM') 
            return elm;
        elm = elm.parentNode;
    }
    return null;
}
//1.判断select选项中 是否存在Value="paraValue"的Item        
function selectIsExitItem(objSelect, objItemValue) {        
    var isExit = false;        
    for (var i = 0; i < objSelect.options.length; i++) {        
        if (objSelect.options[i].value == objItemValue) {        
            isExit = true;        
            break;        
        }        
    }        
    return isExit;        
}       
//2.向select选项中 加入一个Item 
function addItemToSelect(objSelect, objItemText, objItemValue) {        
    //判断是否存在        
    if (selectIsExitItem(objSelect, objItemValue)) {        
        //alert("该Item的Value值已经存在");    
    	return false;
    } else {        
        var varItem = new Option(objItemText, objItemValue);      
        objSelect.options.add(varItem);
        return true;
    }        
} 
//3.从select选项中 删除一个Item        
function removeItemFromSelect(objSelect, objItemValue) {        
    //判断是否存在        
    if (selectIsExitItem(objSelect, objItemValue)) {        
        for (var i = 0; i < objSelect.options.length; i++) {        
            if (objSelect.options[i].value == objItemValue) {        
                objSelect.options.remove(i);        
                break;        
            }        
        }        
        alert("成功删除");        
    } else {        
        alert("该select中 不存在该项");        
    }        
}    
   
   
// 4.删除select中选中的项    
function removeSelectedItemFromSelect(objSelect) {        
    var length = objSelect.options.length - 1;    
    for(var i = length; i >= 0; i--){    
        if(objSelect[i].selected == true){    
            objSelect.options[i] = null;    
        }    
    }    
}      
   
// 5.修改select选项中 value="paraValue"的text为"paraText"        
function updateItemToSelect(objSelect, objItemText, objItemValue) {        
    //判断是否存在        
    if (selectIsExitItem(objSelect, objItemValue)) {        
        for (var i = 0; i < objSelect.options.length; i++) {        
            if (objSelect.options[i].value == objItemValue) {        
                objSelect.options[i].text = objItemText;        
                break;        
            }        
        }        
        alert("成功修改");        
    } else {        
        alert("该select中 不存在该项");        
    }        
}        
   
// 6.设置select中text="paraText"的第一个Item为选中        
function selectItemByValue(objSelect, objItemValue) {            
    //判断是否存在        
    var isExit = false;        
    for (var i = 0; i < objSelect.options.length; i++) {        
        if (objSelect.options[i].value == objItemValue) {        
            objSelect.options[i].selected = true;        
            isExit = true;        
            break;        
        }        
    }              
    //Show出结果
    return isExit;
    /*if (isExit) {        
        alert("成功选中");        
    } else {        
        alert("该select中 不存在该项");        
    }  */      
}  

//7.将select中对应value的项上移一位        
function selectItemToUpOne(objSelect, objItemValue) {            
    //是否成功        
    var isSuccess = false;  
    var backText;
    var backValue;
    for (var i = 0; i < objSelect.options.length; i++) {        
        if (objSelect.options[i].value == objItemValue) {        
            if(i==0){
            	break;
            }else{
            	backText = objSelect.options[i-1].text;
            	backValue = objSelect.options[i-1].value;
            	
            	objSelect.options[i-1].text = objSelect.options[i].text;
            	objSelect.options[i-1].value = objSelect.options[i].value;
            	
            	objSelect.options[i].text = backText;
            	objSelect.options[i].value = backValue;
            	
            	isSuccess = true;        
                break;
            }
        }        
    }              
    return isSuccess;
}

//8.将select中对应value的项下移一位        
function selectItemToDownOne(objSelect, objItemValue) {            
    //是否成功        
    var isSuccess = false;  
    var backText;
    var backValue;
    for (var i = 0; i < objSelect.options.length; i++) {        
        if (objSelect.options[i].value == objItemValue) {        
            if(i==objSelect.options.length-1){
            	break;
            }else{
            	backText = objSelect.options[i+1].text;
            	backValue = objSelect.options[i+1].value;
            	
            	objSelect.options[i+1].text = objSelect.options[i].text;
            	objSelect.options[i+1].value = objSelect.options[i].value;
            	
            	objSelect.options[i].text = backText;
            	objSelect.options[i].value = backValue;
            	isSuccess = true;        
                break;
            }
        }        
    }              
    return isSuccess;
}

//9.将select中对应value的项置顶        
function selectItemToTop(objSelect, objItemValue) {            
    //是否成功        
    var isSuccess = false;  
    var backText;
    var backValue;
    var index;
    for (var i = 0; i < objSelect.options.length; i++) {        
        if (objSelect.options[i].value == objItemValue) {        
            if(i==0){
            	return false;
            }else{
            	backText = objSelect.options[i].text;
            	backValue = objSelect.options[i].value;
            	index = i;
            	isSuccess = true;        
                break;
            }
        }        
    }
    for (var i = index;i>0;i--){
    	objSelect.options[i].text = objSelect.options[i-1].text;
    	objSelect.options[i].value = objSelect.options[i-1].value;
    	
    }
    objSelect.options[0].text = backText;
    objSelect.options[0].value = backValue;
    return isSuccess;
}

//10.将select中对应value的项置底      
function selectItemToBottom(objSelect, objItemValue) {            
    //是否成功        
    var isSuccess = false;  
    var backText;
    var backValue;
    var index;
    for (var i = 0; i < objSelect.options.length; i++) {        
        if (objSelect.options[i].value == objItemValue) {        
            if(i==objSelect.options.length-1){
            	return false;
            }else{
            	backText = objSelect.options[i].text;
            	backValue = objSelect.options[i].value;
            	index = i;
            	isSuccess = true;        
                break;
            }
        }        
    }
    for (var i = index;i<objSelect.options.length-1;i++){
    	objSelect.options[i].text = objSelect.options[i+1].text;
    	objSelect.options[i].value = objSelect.options[i+1].value;
    	
    }
    objSelect.options[objSelect.options.length-1].text = backText;
    objSelect.options[objSelect.options.length-1].value = backValue;
    return isSuccess;
}

function winopen(url,width,height){
	try{
	 	var l = (screen.availWidth - width) / 2;
		var t = (screen.availHeight - height) / 2;
		window.open(url, "create", "width="+width+",height="+height+",top=" + t + ",left=" + l + ",toolbar=no,resizable=no,scrollbars=auto");
 	}catch(e){}
}

function showDialog(url,obj,width,height){
	try{
		return window.showModalDialog(url,obj,"status:no;dialogWidth="+width+"px;dialogHeight="+height+"px");
	}catch(e){}
	
}
//限制文字长度,如<td limit="15">xxxxxxx</td>
function limit(){ 
    var self = $("td[limit]"); 
    self.each(function(){ 
        var objString = $(this).text(); 
        var objLength = $(this).text().length; 
        var num = $(this).attr("limit"); 
        if(objLength > num){ 
			$(this).attr("title",objString); 
            objString = $(this).text(objString.substring(0,num) + "..."); 
        } 
    }) 
} 
//鼠标移动时，改变TR背景颜色
function changeTrBackground(){
	var tabletrbg="";
	$("#frame table tbody tr").hover(
		function(){
			tabletrbg=$(this).css("background");
			$(this).css("background","#B4D4FF");
		},
		function(){
			$(this).css("background",tabletrbg);
		}
	);
}

//获取字符串字节数
function len(s) {  
	 var l = 0;  
	 var a = s.split("");  
	 for (var i=0;i<a.length;i++) {  
	  if (a[i].charCodeAt(0)<299) {  
	   l++;  
	  } else {  
	   l+=2;  
	  }  
	 }  
	 return l;  
}