﻿function addTitleHead(obj, tovalue)
{
	var x = eval('obj.form.' + tovalue);
	if (obj.options.selectedIndex > 0)
	{
		x.value = obj.options[obj.options.selectedIndex].text + x.value;
		obj.options.selectedIndex = 0;
	}
}

// 根据ID获取页面元素
function $(id)
{	
	return "string" == typeof id ? document.getElementById(id) : id;
}
function trim(str)
{
	return str.replace(/(^[\s]*)|([\s]*$)/g, '');
}

String.prototype.trim = function()
{
	return this.replace(/(^[\s]*)|([\s]*$)/g, '');
}

function getLength(str){
     return str.replace(/[^\x00-\xff]/g,"**").length;
}

function checkLength(obj,len){ 
    var el = $(obj);
    var l = getLength(el.value);
    if(l>len){
         alert("长度不能超过"+len+"个字符");
         return false;
    }else{
        return true;
    }
}

/*是否为空*/
function IsEmpty(obj)
{
    if(obj == null || obj.value == '') 
        return true;
    return false;
}

//-----------------common----------------------
function doSelectAll(v) 
{
    var chks = document.getElementsByName("gridSelectedCheckbox");
    for(var i = 0; i < chks.length; i++)
    {
        if(! chks[i].disabled){
            chks[i].checked = v;
        }
    }
}

//check operate valid
function doOperate()
{
    var chks = document.getElementsByName("gridSelectedCheckbox");
    var selectedCount = 0;
    for(var i = 0; i < chks.length; i++)
    {
        if(! chks[i].disabled && chks[i].checked)
        {
            selectedCount++;
            break;
        }
    }
    if(selectedCount > 0){
        return confirm('您确定要操作吗？');
    }else{
        alert("请先选择要操作的编号");
        return false;
    }
}

//Max
function resizeImage(img,rw,rh)    
{
	var w=img.width,h=img.height;	
	if (h>rh)    
	{    
		img.height=rh;    
		img.width=(rh/h)*w;    
		w=img.width;   
		h=img.height;   
	}    
	if (w>rw)    
	{    
		img.width=rw;    
		img.height=(rw/w)*h;    
	}  
}

//----------------------------pagination begin----------------------------
//only using in pagination
function goPage(curentpageno,url,cpage,pagecount)
{
    if(document.getElementById){
        var elUrl = document.getElementById(url);
        var elPage = document.getElementById(cpage);
        if(elUrl && elPage){
            checkPage(curentpageno,cpage,pagecount);   
            window.location.href = elUrl.value + elPage.value;
        }
        else{
            alert('控件参数错误');
        }
    }       
}

function clickPageBtn(events,ctrlid)
{
    var txtId = 'txtPage'+ctrlid;
    var btnId = 'btnGO_'+ctrlid;    
    var eventSource;
    var currentKey = events.charCode||events.keyCode;
    if(document.all)    
        eventSource = window.event.srcElement; 
    else
        eventSource = events.target;    
    
    if(currentKey == 13){
        var btn = document.getElementById(btnId);
        btn.focus();
    }
}

//check page is overflow
function checkPage(curentpageno,obj,pagecount)
{
    if(document.getElementById){
        var el = document.getElementById(obj);
        var el2 = document.getElementById(curentpageno);
        if(isNaN(el.value)){
            alert('页码须为数字');
            el.value = el2.value;
            el.focus();
            return false;
        }else{
            if(parseInt(el.value)>0){
                if(parseInt(el.value)>parseInt(pagecount)){ //当输入的页数超过了最大页数时则显示最后一页
                    alert('您输入的页数超过了总的分页数');
                    el.value = pagecount;
                    return false;
                }
            }else{
                alert('您输入的页数格式不正确');
                el.value = el2.value;
                return false;
            }
        }
    }    
}
//----------------------------pagination end----------------------------

function createXMLHTTPRequest()
{
    if(window.XMLHttpRequest) //创建XMLHttpRequest对象
    { //Mozilla 浏览器 
        return new XMLHttpRequest(); 
    }
    else if (window.ActiveXObject)
    { // IE浏览器 
        try { 
            return new ActiveXObject("Msxml2.XMLHTTP"); 
        } 
        catch (e) { 
            try { 
               return new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }
}

function checkByteLength(str,minlen,maxlen)
{
    if (str == null) return false;
    var l = str.length;
    var blen = 0;
    for(i=0; i<l; i++)
    {
        if ((str.charCodeAt(i) & 0xff00) != 0){
            blen ++;
        }
        blen ++;
    }
    if (blen > maxlen || blen < minlen){
        return false;
    }
    return true;
}

function checkIsChinese(str)
{
    if (str == "") return true;
    var pattern = /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi;
    if (pattern.test(str)) 
        return true;
    else
        return false;
}

function toggle() 
{
    for ( var i=0; i < arguments.length; i++ )
    {
        $(arguments[i]).style.display = ($(arguments[i]).style.display != 'none' ? 'none' : '' );
    }
}