//	字符串校验、操作等相关函数库
//	作者:	胡润刚@浙江联通研发中心
//	日期:	2005-07-14
//	说明:
			/*
			-------------- 函数检索 --------------
			trim函数:							trim() lTrim() rTrim()
			校验字符串是否为空:					checkIsNotEmpty(str)
			校验字符串是否为整型:					checkIsInteger(str)
			校验整型最小值:						checkIntegerMinValue(str,val)
			校验整型最大值:						checkIntegerMaxValue(str,val) 
			校验整型是否为非负数:					isNotNegativeInteger(str)
			校验字符串是否为浮点型:					checkIsDouble(str) 
			校验浮点型最小值:						checkDoubleMinValue(str,val)
			校验浮点型最大值:						checkDoubleMaxValue(str,val)
			校验浮点型是否为非负数:					isNotNegativeDouble(str)
			校验字符串是否为日期型:					checkIsValidDate(str)
			校验两个日期的先后:					checkDateEarlier(strStart,strEnd)
			校验字符串是否为email型:				checkEmail(str)

			校验字符串是否以某字符串开始			checkStartWith(str)
			校验字符串是否以某字符串结束			checkEndWith(str)

			校验字符串是否为手机号码:				checkMobile(str)
			校验字符串是否为联通手机号码:			checkUnicomMobile(str)
			校验字符串是否为联通G网手机号码:			checkUnicomMobileG(str)
			校验字符串是否为联通C网手机号码:			checkUnicomMobileC(str)

			校验字符串是否为中文:					checkIsChinese(str)
			计算字符串的长度，一个汉字两个字符:		realLength()
			校验字符串是否符合自定义正则表达式:		checkMask(str,pat)
			得到文件的后缀名:						getFilePostfix(oFile)  
			*/

//去除多余空格函数
/*		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 checkIsNotEmpty(str)
{
    if(str.trim() == "")
        return false;
    else
        return true;
}

//校验字符串是否为整型
function checkIsInteger(str)
{
    if(str == "")
        return false;
    if(/^(\-?)(\d+)$/.test(str))
        return true;
    else
        return false;
}
//校验整型最小值
function checkIntegerMinValue(str,val)
{
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10)>=parseInt(val,10))
            return true;
        else
            return false;
    }
    else
        return false;
}

//校验整型最大值
function checkIntegerMaxValue(str,val)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10)<=parseInt(val,10))
            return true;
        else
            return false;
    }
    else
        return false;
}

//校验整型是否为非负数
function isNotNegativeInteger(str)
{
    if(str == "")
        return false;
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}

//校验字符串是否为浮点型
function checkIsDouble(str)
{
    if(str == "")
        return false;
    //如果是整数，则校验整数的有效性
    if(str.indexOf(".") == -1)
    {
        if(checkIsInteger(str) == true)
            return true;
        else
            return false;
    }
    else
    {
        if(/^(\\-?)(\\d+)(.{1})(\\d+)$/g.test(str))
            return true;
        else
            return false;
    }
}

//校验浮点型最小值
function checkDoubleMinValue(str,val)
{
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str)>=parseFloat(val))
            return true;
        else
            return false;
    }
    else
        return false;
}

//校验浮点型最大值
function checkDoubleMaxValue(str,val)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    if(typeof(val) != "string")
        val = val + "";
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str)<=parseFloat(val))
            return true;
        else
            return false;
    }
    else
        return false;
}

//校验浮点型是否为非负数
function isNotNegativeDouble(str)
{
    if(str == "")
        return false;
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}

//校验字符串是否为日期型
function checkIsValidDate(str)
{
    if(str == "")
        return false;
    var pattern = /^((\\d{4})|(\\d{2}))-(\\d{1,2})-(\\d{1,2})$/g;
    if(!pattern.test(str))
        return false;
    var arrDate = str.split("-");
    if(parseInt(arrDate[0],10) < 100)
        arrDate[0] = 2000 + parseInt(arrDate[0],10) + "";
    var date =  new Date(arrDate[0],(parseInt(arrDate[1],10) -1)+"",arrDate[2]);
    if(date.getYear() == arrDate[0]
       && date.getMonth() == (parseInt(arrDate[1],10) -1)+""
       && date.getDate() == arrDate[2])
        return true;
    else
        return false;
}

//校验两个日期的先后
function checkDateEarlier(strStart,strEnd)
{
    if(checkIsValidDate(strStart) == false || checkIsValidDate(strEnd) == false)
        return false;
    //如果有一个输入为空，则通过检验
    if (( strStart == "" ) || ( strEnd == "" ))
        return true;
    var arr1 = strStart.split("-");
    var arr2 = strEnd.split("-");
    var date1 = new Date(arr1[0],parseInt(arr1[1].replace(/^0/,""),10) - 1,arr1[2]);
    var date2 = new Date(arr2[0],parseInt(arr2[1].replace(/^0/,""),10) - 1,arr2[2]);
    if(arr1[1].length == 1)
        arr1[1] = "0" + arr1[1];
    if(arr1[2].length == 1)
        arr1[2] = "0" + arr1[2];
    if(arr2[1].length == 1)
        arr2[1] = "0" + arr2[1];
    if(arr2[2].length == 1)
        arr2[2]="0" + arr2[2];
    var d1 = arr1[0] + arr1[1] + arr1[2];
    var d2 = arr2[0] + arr2[1] + arr2[2];
    if(parseInt(d1,10) > parseInt(d2,10))
       return false;
    else
       return true;
}

//校验字符串是否为email型
function checkEmail(str)
{
    if(str == "")
        return false;
    if (str.charAt(0) == "." || str.charAt(0) == "@" || str.indexOf('@', 0) == -1
        || str.indexOf('.', 0) == -1 || str.lastIndexOf("@") == str.length-1 || str.lastIndexOf(".") == str.length-1)
        return false;
    else
        return true;
}

//检验字符串是否以某特定字符串开始
function checkStartWith(str,val)
{
	if(str.length>val.length&&str.substring(0,val.length)==val)
		return true;
	else
		return false;
}

//检验字符串是否以某特定字符串结束
function checkEndWith(str,val)
{
	if(str.length>val.length&&str.substring(str.length-val.length,str.length)==val)
		return true;
	else
		return false;
}

//校验字符串是否为手机号码
function checkMobile(str)
{
    if(/^1[3458]\d{9}$/.test(str))
        return true;
	else
		return false;
}

//校验字符串是否联通手机号码
function checkUnicomMobile(str)
{
    if(/^13[0123]\d{8}$/.test(str))
        return true;
	else if(/^153\d{8}$/.test(str))
        return true;
        else if(/^155\d{8}$/.test(str))
        return true;
        else if(/^156\d{8}$/.test(str))
        return true;
        else if(/^186\d{8}$/.test(str))
        return true;
        else if(/^145\d{8}$/.test(str))
        return true;
	else
		return false;
}

//校验字符串是否联通GSM手机号码
function checkUnicomMobileG(str)
{
    if(/^13[012]\d{8}$/.test(str))
        return true;
        else if(/^155\d{8}$/.test(str))
        return true;
        else if(/^156\d{8}$/.test(str))
        return true;
        else if(/^186\d{8}$/.test(str))
        return true;
        else if(/^145\d{8}$/.test(str))
        return true;
	else
		return false;
}

//校验字符串是否联通CDMA手机号码
function checkUnicomMobileC(str)
{
    if(/^133\d{8}$/.test(str))
        return true;
	else  if(/^153\d{8}$/.test(str))
        return true;
    else if(/^189\d{8}$/.test(str))
        return true;
	else
		return false;
}

//校验字符串是否为中文
function checkIsChinese(str)
{
    //如果值为空，通过校验
    if (str == "")
        return true;
    var pattern = /^([\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0])*$/gi;
    if (pattern.test(str))
        return true;
    else
        return false;
}

//计算字符串的长度，一个汉字两个字符
String.prototype.realLength = function()
{
	return this.replace(/[^\\x00-\\xff]/g,"**").length;
}

//校验字符串是否符合自定义正则表达式
function checkMask(str,pat)
{
    //如果值为空，通过校验
    if (str == "")
        return true;
    var pattern = new RegExp(pat,"gi")
    if (pattern.test(str))
        return true;
    else
        return false;
}


//得到文件的后缀名
function getFilePostfix(oFile)
{
    if(oFile == null)
        return null;
    var pattern = /(.*)\\.(.*)$/gi;
    if(typeof(oFile) == "object")
    {
        if(oFile.value == null || oFile.value == "")
            return null;
        var arr = pattern.exec(oFile.value);
        return RegExp.$2;
    }
    else if(typeof(oFile) == "string")
    {
        var arr = pattern.exec(oFile);
        return RegExp.$2;
    }
    else
        return null;
}