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 true;
    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 true;
    if(checkIsInteger(str) == true)
    {
        if(parseInt(str,10) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}//~~~

function checkIsDouble(str)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    //如果是整数，则校验整数的有效性
    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 true;
    if(checkIsDouble(str) == true)
    {
        if(parseFloat(str) < 0)
            return false;
        else
            return true;
    }
    else
        return false;
}//~~~

function checkIsValidDate(str)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    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;
}//~~~

function checkEmail(str)
{
    //如果为空，则通过校验
    if(str == "")
        return true;
    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 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;
}


function right(strTemp,breaker)
{
	var index = strTemp.indexOf(breaker);
	if( index >= 0 )
	{
		return strTemp.substr(index+1);
	}
	return strTemp;
}

function left(strTemp,breaker)
{
	var index = strTemp.indexOf(breaker);
	if( index >= 0 )
	{
		return strTemp.substr(0,index-1);
	}
	return strTemp;
}

 //校验域是否为空:                     alertIsNotEmpty(field, fieldName)
function alertIsNotEmpty(field, fieldName)
{	
	if(!checkIsNotEmpty(field.value))
	{
		alert(fieldName+" 不能为空!");
		field.focus();
		return false;
	}
	return true;
}
//校验域是否正数的整型：		     alertIsNotNegativeInteger(field, fieldName)
function alertIsNotNegativeInteger(field, fieldName)
{

	if(!checkIsInteger(field.value))
	{
		alert(fieldName+" 必须为整数!");
		field.focus();
		return false;
	}
	if(!isNotNegativeInteger(field.value))
	{
		alert(fieldName+" 必须为正数!");
		field.focus();
		return false;
	}
	return true;
}	
//校验域是否正数的浮点型：	     alertIsNotNegativeDouble(field, fieldName)
function alertIsNotNegativeDouble(field, fieldName)
{
	if(!checkIsDouble(field.value))
	{
		alert(fieldName+" 必须为数字!");
		field.focus();
		return false;
	}
	if(!isNotNegativeDouble(field.value))
	{
		alert(fieldName+" 必须为正数!");
		field.focus();
		return false;
	}
	return true;
}
//校验域是否为日期型：	             alertIsValidDate(field, fieldName)
function alertIsValidDate(field, fieldName)
{	
	if(!checkIsValidDate(field.value))
	{
		alert(fieldName+" 必须为日期!");
		field.focus();
		return false;
	}
	return true;
}

function multiplyField(field1, field2, fieldSum)
{

if(!alertIsNotEmpty(field1, "该域"))
		return false;
		
if(!alertIsNotNegativeDouble(field1, "该域"))
		return false;
if(!alertIsNotNegativeDouble(field2, "该域"))
		return false;
		
if(checkIsNotEmpty(field1.value) && checkIsNotEmpty(field2.value))
{
	sum = eval(field1.value+"*"+field2.value);
	fieldSum.value = toMoney(sum.toString());
	fieldSum.onchange();
	if(fieldSum.type == "text")
	{
		fieldSum.onkeyup();		
	}
}
}

function sumField(field1, fieldAll, fieldSum)
{
if(!alertIsNotEmpty(field1, "该域"))
		return false;
		
if(!alertIsNotNegativeDouble(field1, "该域"))
		return false;
		
for(i=0; i < fieldAll.length; i++)
{
if(!alertIsNotNegativeDouble(fieldAll[i], "该域"))
		return false;
}	

//for(i=0; i < fieldAll.length; i++)
//{
//if(!checkIsNotEmpty(fieldAll[i].value))
//		return false;
//}	

sum = 0;
for(i=0; i < fieldAll.length; i++)
{
	if(checkIsNotEmpty(fieldAll[i].value))
		sum = parseFloat(fieldAll[i].value) + sum;
}

	fieldSum.value = toMoney(sum.toString());
	fieldSum.onchange();
	if(fieldSum.type == "text")
	{
		fieldSum.onkeyup();		
	}
}

function floatToMoney(strValue, fieldInteger, fieldDouble)
{
	//var strValue = floatValue.toString();
		
	var start = strValue.indexOf(".");
	
	var strInteger, strDouble;
	if( start==-1 )
	{
		strInteger = strValue;
		strDouble = "00";
	}
	else
	{
		strInteger = strValue.substr(0, start);
		strDouble = strValue.substr(start+1, strValue.length - start)+"00";
	}
	
	var minLen = strInteger.length<fieldInteger?strInteger.length:fieldInteger.length;
	
	for(j=0; j<fieldInteger.length; j++)
	{
		fieldInteger[j].value = "";
	}
	
	for(i=0; i<fieldDouble.length; i++)
	{
		fieldDouble[i].value = "";
	}
	
	for(i=strInteger.length-1, j=fieldInteger.length-1; i>=0 && j>=0; i--, j--)
	{
		fieldInteger[j].value = strInteger.charAt(i);
	}
	
	for(i=0; i<strDouble.length && i<fieldDouble.length; i++)
	{
		fieldDouble[i].value = strDouble.charAt(i);
	}	
}

function floatToBigMoney(strValue, fieldInteger, fieldDouble)
{
	//var strValue = floatValue.toString();
	
	var start = strValue.indexOf(".");
	
	var strInteger, strDouble;
	if( start==-1 )
	{
		strInteger = strValue;
		strDouble = "00";
	}
	else
	{
		strInteger = strValue.substr(0, start);
		strDouble = strValue.substr(start+1, strValue.length - start)+"00";
	}
	
	var minLen = strInteger.length<fieldInteger?strInteger.length:fieldInteger.length;
	
	for(j=0; j<fieldInteger.length; j++)
	{
		fieldInteger[j].value = "";
	}
	
	for(i=0; i<fieldDouble.length; i++)
	{
		fieldDouble[i].value = "";
	}
	
	for(i=strInteger.length-1, j=fieldInteger.length-1; i>=0 && j>=0; i--, j--)
	{
		fieldInteger[j].value = smallToBig(strInteger.charAt(i));
	}
	
	for(i=0; i<strDouble.length && i<fieldDouble.length; i++)
	{
		fieldDouble[i].value = smallToBig(strDouble.charAt(i));
	}	
}

function smallToBig(strSmall, strBig)
{
	switch(strSmall)
	{
	case "0":return "零";
	case "1":return "壹";
	case "2":return "贰";
	case "3":return "叁";
	case "4":return "肆";
	case "5":return "伍";
	case "6":return "陆";
	case "7":return "柒";
	case "8":return "捌";
	case "9":return "玖";
	}
}
//~~~
/*--------------------------------- file --------------------------------------*/



function toMoney(strValue)
{
	var start = strValue.indexOf(".");
	
	var strInteger, strDouble;
	if( start==-1 )
	{
		strInteger = strValue;
		strDouble = "00";
	}
	else
	{
		strInteger = strValue.substr(0, start);
		strDouble = strValue.substr(start+1, strValue.length - start);
		if(strDouble.length>2)
		{
			strDouble = strDouble.substr(0,2);
		}
		else
		{
			if(strDouble.length==0)
				strDouble = "00";
			if(strDouble.length==1)
				strDouble = strDouble + "0";
		}
	}
	
	return strInteger + "." + strDouble;	
}

function setKeyPressFun()
{
 for (var i=0;i<document.forms[0].elements.length;i++)
    {
     e = document.forms[0].elements[i];
     if ((e.type == "text")) 
       {
		e.onkeyup = function(){
			if(this.oldSize)
			{
				if(this.value.length>this.oldSize)
				{
					this.size = this.value.length;
					return;
				}
				if(this.value.length<=this.oldSize)
				{
					this.size = this.oldSize;
					return;
				}
			}
			if(this.value.length>this.size)
			{
				this.oldSize = this.size;
				this.size = this.value.length;
				return;
			}
			};
		e.onkeyup();
       }
    }
}