/********************************
 * @File:clientCheck.js
 * @Description: 通用客户端验证
 * @Date:2007-8-15
 * @version v1.0
 * @Copyright:wish
 ********************************/
//<--begin
/*
	功能描述：验证字符串
	方法名：validString
	参数1：elename 控件名称
	参数2：strDisplay 提示字符串
	参数3：mustInput 是否必填，true表示必填
	参数4：stringLen 字符串长度限制，数字
*/
function validString(elename,strDisplay,mustInput,stringLen)
{
	var eles = document.getElementsByName(elename);
	for(var i=0;i<eles.length;i++)
	{
		var ele = eles[i];
		var value = (new String(ele.value)).trim();
		if(mustInput)
		{
			if(value == "")
			{
				alert("The " + strDisplay + " can not be blank!");
				ele.focus(); 
				return false;
			}
		}
		if(value != "")
		{
			if(value.length>parseInt(stringLen))
			{
				alert("The " + strDisplay + " mustn't be over " + stringLen + " characters long!");
				ele.focus();     
				return false;
			}
		}
	}
	return true;
}

/*
	功能描述：验证正整数
	方法名：validPlusInt
	参数1：elename 控件名称
	参数2：strDisplay 提示字符串
	参数3：mustInput 是否必填，true表示必填
	参数4：min 最小值，数字
	参数5：max 最大值，数字
*/
function validPlusInt(elename,strDisplay,mustInput,min,max)
{
	var eles = document.getElementsByName(elename);
	for(var i=0;i<eles.length;i++)
	{
		var ele = eles[i];
		var value = (new String(ele.value)).trim();
		if(mustInput)
		{
			if(value == "")
			{
				alert("The " + strDisplay + " can not be blank!");
				ele.focus(); 
				return false;
			}
		}
		if(value != "")
		{
			if(!isPlusInt(value))
			{
				alert("The " + strDisplay + " must be a numeric character");
				ele.focus();
				return false;
			}
			if(max != null)
			{
				if(parseInt(value)>parseInt(max))
				{
					alert(strDisplay + " 应该小于或等于" + max);
					ele.focus();     
					return false;
				}
			}
			if(min != null)
			{
				if(parseInt(value)<parseInt(min))
				{
					alert(strDisplay + " 应该大于或等于" + min);
					ele.focus();     
					return false;
				}	
			}
		}
	}
	return true;
}

/*
	功能描述：验证正整数和零
	方法名：validZPlusInt
	参数1：elename 控件名称
	参数2：strDisplay 提示字符串
	参数3：mustInput 是否必填，true表示必填
	参数4：min 最小值，数字
	参数5：max 最大值，数字
*/
function validZPlusInt(elename,strDisplay,mustInput,min,max)
{
	var eles = document.getElementsByName(elename);
	for(var i=0;i<eles.length;i++)
	{
		var ele = eles[i];
		var value = (new String(ele.value)).trim();
		if(mustInput)
		{
			if(value == "")
			{
				alert("The " + strDisplay + " can not be blank!");
				ele.focus(); 
				return false;
			}
		}
		if(value != "")
		{
			if(!isZPlusInt(value))
			{
				alert("The " + strDisplay + " must be a numeric character");
				ele.focus();
				return false;
			}
			if(parseInt(value)>parseInt(max))
			{
				alert(strDisplay + " 应该小于或等于" + max);
				ele.focus();     
				return false;
			}
			if(parseInt(value)<parseInt(min))
			{
				alert(strDisplay + " 应该大于或等于" + min);
				ele.focus();     
				return false;
			}
		}
	}
	return true;
}

/*
	功能描述：验证2位小数
	方法名：validDecimal
	参数1：elename 控件名称
	参数2：strDisplay 提示字符串
	参数3：mustInput 是否必填，true表示必填
	参数4：min 最小值，数字
	参数5：max 最大值，数字
*/
function validDecimal(elename,strDisplay,mustInput,min,max)
{
	var eles = document.getElementsByName(elename);
	for(var i=0;i<eles.length;i++)
	{
		var ele = eles[i];
		var value = (new String(ele.value)).trim();
		if(mustInput)
		{
			if(value == "")
			{
				alert("The " + strDisplay + " can not be blank!");
				ele.focus(); 
				return false;
			}
		}
		if(value != "")
		{
			if(!isDecimal(value))
			{
				alert("The " + strDisplay + " does not match the required format of XXXX.XX");
				ele.focus();
				return false;
			}
			if(parseInt(value)>parseInt(max))
			{
				alert(strDisplay + " 应该小于或等于" + max);
				ele.focus();     
				return false;
			}
			if(parseInt(value)<parseInt(min))
			{
				alert(strDisplay + " 应该大于或等于" + min);
				ele.focus();     
				return false;
			}
		}
	}
	return true;
}
/*
	功能描述：判断是否为有效的文件扩展名(验证图片)
	方法名：validFileExt
	参数1：elename 控件名称
	参数2：strDisplay 提示字符串
	参数3：mustInput 是否必填，true表示必填
	参数4：max 最大值，数字
	参数5：min 最小值，数字
*/
function validFileExt(elename,strDisplay,mustInput)
{
	var eles = document.getElementsByName(elename);
	for(var i=0;i<eles.length;i++)
	{
		var ele = eles[i];
		var value = (new String(ele.value)).trim();
		if(mustInput)
		{
			if(value == "")
			{
				alert("The " + strDisplay + " can not be blank!");
				ele.focus(); 
				return false;
			}
		}
		if(value != "")
		{
			if(!checkFileExt(value))
			{
				alert(strDisplay +" is invalid, only these formats are valid,eg: jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga");
				ele.focus(); 
				return false;
			}
		}
	}
	return true;
}
/*
	功能描述：验证开始时间 是否 早于结束时间
	方法名：beginEndCompare
	参数1：begin 开始时间
	参数2：end 结束时间
*/
function beginEndCompare(begin,end)
{
	if(begin=="")
	{
		window.alert("Begin Date can not be blank!");
		return false;
	}
	if(end=="")
	{
		window.alert("End Date can not be blank!");
		return false;
	}
	begin = Date.parse(begin.replace(/-/g,"/"));
	end = Date.parse(end.replace(/-/g,"/"));
	if((begin - end)<=0)
	{	
		return true;
	}
	else
	{
		window.alert("'Begin Date' can't be earlier than 'End Date'");
		return false;
	}
}

/*
	功能描述：验证当前时间 是否 早于选定时间
	方法名：currentEndCompare
	参数：end 结束时间
*/
function currentEndCompare(end)
{
	var current = new Date();
	current = Date.parse(current);
	end = Date.parse(end.replace(/-/g,"/"));
	if((current - end)<=0)
	{
		return true;
	}
	else
	{
		window.alert("'Begin Date' can't be earlier than 'Current Date'");
		return false;
	}
}

//-----------------字符串去掉空格------------------//
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g,"");
}

//-----------------判断是否为正整数------------------//
function isPlusInt(value)
{
	var patrn=/^\+?[1-9][0-9]*$/;
	if(!value.match(patrn))
	{
		return false;
	}
	else
	{
		return true;
	}
}

//-----------------判断是否是数字组合------------------//
function isDigital(value)
{
	var patrn = /^\d+$/ ;
	if(!value.match(patrn))
	{
		return false;
	}
	else
	{
		return true;
	}
}
//-----------------判断是否为正整数或0------------------//
function isZPlusInt(value)
{
	if(!isDigital(value))
	{
		return false;
	}
	else
	{
		if(value.substring(0,1)=="0" && value!=0)
		{
			return false;
		}
	}
	return true;
}

//-----------------判断是否为小数（2位）------------------//
function isDecimal(value)
{
	var arr = value.split(".");
	if(arr.length==1)
	{
		//验证整数部分
		if(!isZPlusInt(arr[0]))
		{
			return false;
		}
	}
	else if(arr.length==2)
	{
		//验证整数部分
		if(!isZPlusInt(arr[0]))
		{
			return false;
		}
		//验证小数部分
		if(!isDigital(arr[1]))
		{
			return false;
		}
		//验证小数部分的位数
		if(arr[1].length > 2)
		{
			return false;
		}
	}
	else
	{
		return false;
	}
	return true;
}

//-----------------验证文件扩展名------------------//
function checkFileExt(filepath)
{
	var re=/(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/i;
	if(filepath.match(re))
	{
		return true;
	}
	else
	{
		return false;
	}
	//return filepath.substring(filepath.lastIndexOf(".")+1,filepath.length);
}

//-----------------验证电子邮箱------------------//
function isEmail(str)
{
	if(str != "")
	{
		var re=/^[\w-]+(\.*[\w-]+)*@([0-9a-z]+(([0-9a-z]*)|([0-9a-z-]*[0-9a-z]))+\.)+[a-z]{2,3}$/i;
		if(str.match(re))
		{
			return true;
		}
		else
		{
			window.alert("Please enter a valid Email address!");
			return false;
		}
	}
	return true;
}

//------------验证数字或字母组成的字符串-----------//
function isString(str)
{
	var re=/^[A-Za-z0-9]+$/i;
	if(str.match(re))
	{
		return true;
	}
	else
	{
		window.alert("Please enter a valid value!");
		return false;
	}
}


//-----------------将数值四舍五入(保留2位小数)后格式化成金额形式------------------//
function formatCurrency(num)
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + '.' + cents);
}
//end-->