//校验字符串中的中、英文字符总长度
function checkDigit(str){
  var digit = str.length;
  var arr = str.match(/[^\x00-\x80]/ig);
  if(arr != null) digit += arr.length;
  return digit;
}
//校验字符串中是否仅为整数,返回true表示为一整数(不含小数点).
function checkInteger(str){
  return /^[-|+]?\d+$/g.test(str);
}
//校验字符串中是否仅为数字,返回true表示为一实数(首尾不为小数点).
function checkNumber(str){
  return /^[-|+]?\d+(\.\d+)?$/g.test(str);
}
/**
*校验普通电话 除数字外，可含有“-” 
**/
function checkTel(s) 
{ 
	var patrn=/^(\d)+([-]?(\d)+)?$/; 
	if (!patrn.exec(s)) return false 
	return true 
}
/**
*校验手机号码：必须以数字开头，除数字外，可含有“-” 
**/
function checkMobile(s) 
{ 
	var patrn=/^1(\d){10}$/; 
	if (!patrn.exec(s)) return false 
	return true 
}

function window.onhelp()
{
    return false
}

// 将字符串转为日期
// 格式错误时返回null
function parseDate(str)
{
	var mResult = str.match(/^(\d{1,4})\-([01]?\d)\-([0123]?\d)$/);
	if(mResult == null || mResult.length != 4)
	{
		return null;
	}
	var yyyy = parseInt(mResult[1],10);
	var mm = parseInt(mResult[2],10) - 1;
	var dd = parseInt(mResult[3],10);
	var dResult = new Date(yyyy,mm,dd);
	if(dResult.getFullYear() != yyyy || dResult.getMonth() != mm || dResult.getDate() != dd)
	{
		return null;
	}
	return dResult;
}

//按回车键跳到下一个区域
function document_onkeydown() 
{
	if(event.keyCode == 13)
	{
		var ftype = event.srcElement.type;
		if((typeof(ftype))+"" != "undefined" 
		&&(
			ftype == "text" 
		|| ftype == "checkbox" 
		|| ftype == "radio" 
		|| ftype.indexOf("select") != -1
			)){
			event.keyCode = 9;
		}
	}
}
		
//解析Url获取Param 如果是多个Param则中间以逗号分隔
function GetParam(name)
{
	var input = document.location.href.split('#')[0];
	var result = input.replace(/\?/g," ").replace(/\&/g," ");
	var items = result.split(' ');
	var m = new RegExp("^" + name + "=","i");
	var param = "";
	for(var i=0;i<items.length;i++)
	{
		var temp = items[i];
		temp = temp.replace(/\s/g,"");
		if(temp=="")
		continue;
		if(temp.match(m))
		{
			if(param!="")
				param += ",";
			param += temp.replace(m,"");
		}
	}
	return param;
}

//构造StringHelper对象
function StringHelper()
{

}
//定义StringHelper对象的Format方法
StringHelper.Format = function(format)
{
	if ( arguments.length == 0 )
	{
		return '';
	}
	if ( arguments.length == 1 )
	{
		return String(format);
	}

	var strOutput = '';
	for ( var i=0 ; i < format.length-1 ; )
	{
		if ( format.charAt(i) == '{' && format.charAt(i+1) != '{' )
		{
			var index = 0, indexStart = i+1;
			for ( var j=indexStart ; j <= format.length-2 ; ++j )
			{
				var ch = format.charAt(j);
				if ( ch < '0' || ch > '9' ) break;
			}
			if ( j > indexStart )
			{
				if ( format.charAt(j) == '}' && format.charAt(j+1) != '}' )
				{
					for ( var k=j-1 ; k >= indexStart ; k-- )
					{
						index += (format.charCodeAt(k)-48)*Math.pow(10, j-1-k);
					}  
					var swapArg = arguments[index+1];
					strOutput += swapArg;
					i += j-indexStart+2;
					continue;
				}
			}
			strOutput += format.charAt(i);
			i++;
		}
		else
		{
			if ( ( format.charAt(i) == '{' && format.charAt(i+1) == '{' )
				|| ( format.charAt(i) == '}' && format.charAt(i+1) == '}' ) )
			{
				i++
			}
			strOutput += format.charAt(i);
			i++;
		}
	}
	strOutput += format.substr(i);
	return strOutput;
}

/**
* 去除多余空格函数
* trim:去除两边空格 lTrim:去除左空格 rTrim: 去除右空格
* 用法：
*     var str = "  hello ";
*     str = str.trim();
*/
String.prototype.trim = function()
{
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}
String.prototype.lTrim = function()
{
    return this.replace(/(^[\s]*)/g, "");
}
String.prototype.rTrim = function()
{
    return this.replace(/([\s]*$)/g, "");
}

//统一验证表单输入的数据
function PubCheckData(formName)
{
    var src = document.getElementById(formName).elements
    var des,dType,isNull,maxValue,minValue;
    if(src.length>0)
    {
        for(var i=0;i<src.length;i++)
        {            
            if(src[i].type=="text" && !src[i].disabled && !src[i].readOnly)
            { 
                des = (typeof(src[i].des) == "undefined")?"":src[i].des;
                dType = src[i].datatype;
                isNull = src[i].allowNull;
                if(typeof(dType)!="undefined")
                {
                    if(isNull == "no")
                    {
                        if(src[i].value.trim()=="")
                        {
                            alert(des + "不能为空！");
                            src[i].focus();
                            return false;
                        }
                    }
                    switch(dType)
                    {
                        case "string":
                            //字符
                            if(src[i].value.trim() != "" && checkDigit(src[i].value.trim()) > src[i].maxLength)
                            {
                                alert(des + "超出最大长度，最多为" + Math.ceil(src[i].maxLength / 2).toString() + "个汉字，" + src[i].maxLength.toString() + "个英文字母或数字");
                                src[i].focus();
                                return false;
                            }
                            break;
                        case "int":
                            //整数
                            if(src[i].value.trim() != "")
                            {
                                if(!checkInteger(src[i].value.trim()))
                                {
                                    alert(des + "必须是整数！");
                                    src[i].focus();
                                    return false;
                                }
                                else
                                {
                                    maxValue = src[i].maxvalue;
                                    if(typeof(maxValue) != "undefined" && maxValue != null && (parseInt(src[i].value) > maxValue))
                                    {
                                        alert(des + "超过了允许的最大值。最大值为"+maxValue);
                                        src[i].focus();
                                        return false;
                                    }
                                    minValue = src[i].minvalue;
                                    if(typeof(minValue) != "undefined" && minValue != null && (parseInt(src[i].value) < minValue))
                                    {
                                        alert(des + "超过了允许的最小值。最小值为"+minValue);
                                        src[i].focus();
                                        return false;    
                                    }
                                }
                            }
                            break;
                        case "number":
                            //数字
                            if(src[i].value.trim() != "")
                            {
                                if(!checkNumber(src[i].value.trim()))
                                {
                                    alert(des + "必须是数字！");
                                    src[i].focus();
                                    return false;
                                }
                                else
                                {
                                    maxValue = src[i].maxvalue;
                                    if(typeof(maxValue) != "undefined" && maxValue != null && (parseFloat(src[i].value) > maxValue))
                                    {
                                        alert(des + "超过了允许的最大值。最大值为"+maxValue);
                                        src[i].focus();
                                        return false;
                                    }
                                    minValue = src[i].minvalue;
                                    if(typeof(minValue) != "undefined" && minValue != null && (parseFloat(src[i].value) < minValue))
                                    {
                                        alert(des + "超过了允许的最小值。最小值为"+minValue);
                                        src[i].focus();
                                        return false;    
                                    }
                                }
                            }
                            break;                    
                    }
                }
            }
        }
    }
    return true;
}


//统一验证合同的附件表单输入的数据
function PubCheckAffixData(ctrlName)
{
    var src = document.getElementsByName(ctrlName);
    var des,dType,isNull,maxValue,minValue;
    if(src.length>0)
    {
        for(var i=0;i<src.length;i++)
        {            
            if((src[i].type=="text" || src[i].type=="textarea") && !src[i].disabled && !src[i].readOnly)
            { 
                des = (typeof(src[i].des) == "undefined")?"":src[i].des;
                dType = src[i].datatype;
                isNull = src[i].allowNull;
                if(typeof(dType)!="undefined")
                {
                    if(isNull == "no")
                    {
                        if(src[i].value.trim()=="")
                        {
                            alert(des + "不能为空！");
                            src[i].focus();
                            return false;
                        }
                    }
                    switch(dType)
                    {
                        case "string":
                            //字符
                            if(src[i].value.trim() != "" && checkDigit(src[i].value.trim()) > src[i].maxLength)
                            {
                                alert(des + "超出最大长度，最多为" + Math.ceil(src[i].maxLength / 2).toString() + "个汉字，" + src[i].maxLength.toString() + "个英文字母或数字");
                                src[i].focus();
                                return false;
                            }
                            break;
                        case "int":
                            //整数
                            if(src[i].value.trim() != "")
                            {
                                if(!checkInteger(src[i].value.trim()))
                                {
                                    alert(des + "必须是整数！");
                                    src[i].focus();
                                    return false;
                                }
                                else
                                {
                                    maxValue = src[i].maxvalue;
                                    if(typeof(maxValue) != "undefined" && maxValue != null && (parseInt(src[i].value) > maxValue))
                                    {
                                        alert(des + "超过了允许的最大值。最大值为"+maxValue);
                                        src[i].focus();
                                        return false;
                                    }
                                    minValue = src[i].minvalue;
                                    if(typeof(minValue) != "undefined" && minValue != null && (parseInt(src[i].value) < minValue))
                                    {
                                        alert(des + "超过了允许的最小值。最小值为"+minValue);
                                        src[i].focus();
                                        return false;    
                                    }
                                }
                            }
                            break;
                        case "number":
                            //数字
                            if(src[i].value.trim() != "")
                            {
                                if(!checkNumber(src[i].value.trim()))
                                {
                                    alert(des + "必须是数字！");
                                    src[i].focus();
                                    return false;
                                }
                                else
                                {
                                    maxValue = src[i].maxvalue;
                                    if(typeof(maxValue) != "undefined" && maxValue != null && (parseFloat(src[i].value) > maxValue))
                                    {
                                        alert(des + "超过了允许的最大值。最大值为"+maxValue);
                                        src[i].focus();
                                        return false;
                                    }
                                    minValue = src[i].minvalue;
                                    if(typeof(minValue) != "undefined" && minValue != null && (parseFloat(src[i].value) < minValue))
                                    {
                                        alert(des + "超过了允许的最小值。最小值为"+minValue);
                                        src[i].focus();
                                        return false;    
                                    }
                                }
                            }
                            break;                    
                    }
                }
            }
        }
    }
    return true;
}


//在指定位置插入字符
String.prototype.insertOf = function(index,strValue)
{
    var strText = this;
    var strTemp1,strTemp2;
    if(index >= strText.length)
    {
        return strText.concat(strValue);
    }
    else if(index <= 0)
    {
        return strValue.concat(strText);
    }
    else
    {
        strTemp1 = this.substring(0,index + 1);
        strTemp2 = this.substring(index,strText.length - index);   
        strTemp1 = strTemp1.concat(strValue);
        return strTemp1.concat(strTemp2);
    }
}

// 列表工具类
// 选择checkbox控件时自动将主键拼成一个string
function selectInfo(sender){
	var itemId = sender.value;
	var selectedList = document.getElementById("selectedList").value;
	if (sender.checked)
	{
		document.getElementById("selectedList").value = selectedList + itemId + ",";
	}
	else
	{
		document.getElementById("selectedList").value = selectedList.replace("," + itemId + ",",",");
	}	
}
// 全部选择
function selectAllInfo(){
	//初始化选中列表
	var selectedList = ","
	//设置选中项
	var selectCtrls = document.getElementsByName("cb")
	for(i = 0;i < selectCtrls.length;i ++)
	{
		selectCtrls[i].checked = true;
		
		selectedList += selectCtrls[i].value + ",";
	}
	//更新选中列表
	document.all("selectedList").value = selectedList;
}
// 全部清除选中
function clearAllInfo()
{
	var selectCtrls = document.getElementsByName("cb")
	for(i = 0;i < selectCtrls.length;i ++)
	{
		selectCtrls[i].checked = false;		
	}
	//更新选中列表
	document.all("selectedList").value = ",";
}

// 验证是否选了需要操作的信息ID
function checkSelect(){
	var selectedList = document.getElementById("selectedList").value;
	if(selectedList == ",")
	{
		alert("请先选中要操作的信息");
		return false;
	}
	else
	{
		return true;
	}
}
// 删除方法
function deleteSelect()
{
	if(checkSelect())
	{
		if(confirm("是否真要删除？"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}