﻿ERR_NUMBER = "非法的数值！";
ERR_INT = "非法的整数！";
ERR_BIG_NUMBER = "数额太大，系统无法接受！";
ERR_STRING_LONG = "输入超长。最大长度为";
ERR_YEAR = "非法的年份！";
ERR_MONTH = "非法的月份！";
ERR_DAY = "非法的日期！";
ERR_EMAIL = "非法的Email地址！";
ERR_NUMBER_NULL = "必须输入数值";
ERR_INT_NULL = "必须输入数值";
ERR_ID_NULL = "必须正确指定";
ERR_STRING_NULL = "必须输入";
ERR_SELECT_NULL = "必须选择";
ERR_ALPHA_DIGIT = "只能输入字母、数字和下划线";
ERR_NUMBER_BELOW = "必须小于";

ERR_NUMBER2 = "Invalid number";
ERR_INT2 = "Invalid integer";
ERR_BIG_NUMBER2 = "Number too big to be accepted by system";
ERR_STRING_LONG2 = "Input too long. Maximal length is ";
ERR_YEAR2 = "Invalid year";
ERR_MONTH2 = "Invalid month";
ERR_DAY2 = "Invalid date";
ERR_EMAIL2 = "Invalid e-mail address";
ERR_NUMBER_NULL2 = "Must input";
ERR_INT_NULL2 = "Must input right";
ERR_ID_NULL2 = "Must select";
ERR_STRING_NULL2 = "Must input";
ERR_SELECT_NULL2 = " Must select";
ERR_ALPHA_DIGIT2 = "Must input alphabets or digits";
ERR_NUMBER_BELOW2 = "Must be less than ";

function checknumber(data,lbl){
	var tmp ;
	var tt;
	if (data == "") return true;
//	var re = /^[\-\+]?([1-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)(\.\d+)?([Ee][\-\+]?\d+)?$/;
//	var re = /^[\-\+]?([1-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)(\.\d+)?$/;
//	var re = /^[\-\+]?([0-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)(\.\d+)?$/;   // By Dennis set 04 OK!
//	var re = /^[\-\+]?([0-9]+)(\.\d+)?$/;
	var re = /^[\-\+]?(0|([1-9][0-9]*))(\.\d+)?$/;
	if (lbl!=null)
		tt = lbl+": ";
	else
		tt = "";
	if (re.test(data)){
		gar = data + '.';
		tmp = gar.split('.');
		if (tmp[0].length > 12) {
			if (msgLang==2)
				alert(tt+ERR_BIG_NUMBER2);
			else
				alert(tt+ERR_BIG_NUMBER);
			return false;
		}
		return true;
	}
	if (msgLang==2)
		alert(tt+ERR_NUMBER2);
	else
		alert(tt+ERR_NUMBER);
	return false;
}

function checknumber_below(data,max,lbl)
{
	if (data>max){
		if (msgLang==2)
			alert(lbl+":"+ERR_NUMBER_BELOW2+max);
		else
			alert(lbl+":"+ERR_NUMBER_BELOW+max);
	
		return false;
	}

	return true;
}

function checknumber_null(data,lbl){
	if (trim(data)==""){
		if (msgLang==2)
			alert(lbl+":"+ERR_NUMBER_NULL2);
		else
			alert(lbl+":"+ERR_NUMBER_NULL);
		return false;
	}
	return true;
}

function checkint_null(data,lbl){
	if (trim(data)==""){
		if (msgLang==2)
			alert(lbl+":"+ERR_INT_NULL2);
		else
			alert(lbl+":"+ERR_INT_NULL);
		return false;
	}
	return true;
}

function checkid_null(data,lbl){
	if ((trim(data)=="") || (data * 1 <= 0)){
		if (msgLang==2)
			alert(lbl+":"+ERR_ID_NULL2);
		else
			alert(lbl+":"+ERR_ID_NULL);
		return false;
	}
	return true;
}

function checkstring_null(data,lbl)
{
	 
	if (trim(data)==""){
		if (msgLang==2)
			alert(lbl+":"+ERR_STRING_NULL2);
		else
			alert(lbl+":"+ERR_STRING_NULL);
		return false;
	}
	return true;
}

function checkdropdown_null(data,lbl,dvalue)
{
	
	 //检查下拉菜单是否选择。data是下拉当前值，lbl是字段的显示名称，dvalue是要判断的下拉菜单值
	 //如checkdropdown_null(document.forms[thisForm].ptype_product_str.value,'产品类别','topptype')，参见产品资料设置
	if (trim(data)==dvalue){ //匹配，那么就代表必须选择
		if (msgLang==2)
			alert(lbl+":"+ERR_SELECT_NULL2);
		else
			alert(lbl+":"+ERR_SELECT_NULL);
		return false;
	}
	return true;
}

function checkdate_null(y,m,d,lbl)
{
	var msg;
	if (msgLang==2)
		msg = lbl+":"+ERR_STRING_NULL2;
	else
		msg = lbl+":"+ERR_STRING_NULL;

	if (trim(y+"")=="" || trim(m+"")=="" || trim(d+"")==""){
		alert(msg);
		return false;
	}
	if (y+""=="0" || m+""=="0" || d+""=="0"){
		alert(msg);
		return false;
	}
	return true;
}

function checkint(data,lbl)
{
	if (data == "") return true;
	var re = /^[\-\+]?([1-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)$/;
	if (re.test(data)) 
		return true;
	if (lbl) {
		if (msgLang==2)
			alert(lbl+":"+ERR_INT2);
		else
			alert(lbl+":"+ERR_INT);
	}
	return false;
}

function checkstring(str,maxlen,lbl)
{
	var msg="";
	if (str.length > maxlen){
		if (lbl!=null)
			msg = lbl+":";
		if (msgLang==2)
			alert(msg+ERR_STRING_LONG2+maxlen);
		else
			alert(msg+ERR_STRING_LONG+maxlen);
		return false;
	}
	return true;
}

function checkstrdate_null(data,lbl)
{
	if(!checkstring_null(data,lbl))
		return false;
	var tmp = data.split(" ");
	data = tmp[0];
	var re = /^([1-2]\d{3})\-([1-9]|0[1-9]|11|12|10)\-([1-9]|[1-2][0-9]|0[1-9]|30|31)$/;
	if (re.test(data)) 
	{
		tmp = data.split("-");
		return checkday(tmp[2]*1,tmp[0]*1,tmp[1]*1,lbl);
	}
	else
	{	
		if (lbl) {
			if (msgLang==2)
				alert(lbl+":"+ERR_DAY2);
			else
				alert(lbl+":"+ERR_DAY);
		} else {
			if (msgLang==2)
				alert(ERR_DAY2);
			else
				alert(ERR_DAY);
		}
		return false;
	}
}

function checkstrdate(data,lbl)
{
	if (data.length == 0) return true;
	return checkstrdate_null(data,lbl);
}

function checkinputlen(elem,maxlen,lbl)
{
	if (checkstring(elem.value,maxlen,lbl))
		return true;
	elem.focus();
	var txtrange = elem.createTextRange();
	txtrange.collapse();
	txtrange.moveEnd("character",maxlen-10);
	txtrange.select();
	return -1;
}

function checkyear(year,lbl)
{
	if (year.length == 0) return true;

	if (!checkint(year)){
		errorYear(lbl);
		return false;
	}

	var temp = parseInt(year);
	if (!isNaN(temp)){
		if (year == 0) return true;
		low = 1900;
		high = 2037;
		if ((year >= low) && (year <=high)) return true;
	}

	errorYear(lbl);
	return false;
}

function checkmonth(month,low,high,lbl)
{
	if (!checkint(month)){
		errorMonth(lbl);
		return false;
	}

	var temp = parseInt(month);
	if (!isNaN(temp)){
		temp = parseInt(low);
		if (isNaN(temp)) low = 1;
		temp = parseInt(high);
		if (isNaN(temp)) high = 12;
		if ((month >= low) && (month <=high)) return true;
	}
	errorMonth(lbl);
	return false;
}

function errorYear(lbl)
{
	if (msgLang==2)
		alert(lbl+":"+ERR_YEAR2);
	else
		alert(lbl+":"+ERR_YEAR);
}
function errorMonth(lbl)
{
	if (msgLang==2)
		alert(lbl+":"+ERR_MONTH2);
	else
		alert(lbl+":"+ERR_MONTH);
}
function errorDay(lbl)
{
	if (msgLang==2)
		alert(lbl+":"+ERR_DAY2);
	else
		alert(lbl+":"+ERR_DAY);
}

function checkday(day,year,month,lbl)
{
	err = false;

	if (!checkyear(year,lbl)) {
		return false;
	}
	if (!checkmonth(month,"","",lbl)){
		return false;
	}
	if (!checkint(day) || (day < 1) || (day > 31)){
		errorDay(lbl);
		return false;
	}
	
	switch (parseInt(month)){
		case 2:
			high =28;
			if ((year % 4 == 0) && (year % 100 != 0))
				{high =29;}
			else if (year % 400 == 0) {high=29;}
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			high =31;
			break;
		default:
			high =30;
	}
	if ((day < 1) || (day > high)){
		errorDay(lbl);
		return false;
	}
	return true;
}

function checkemail(umail,lbl)
{
	umail=trim(umail);
	if (umail.length == 0) return true;
	var re=/^[\-!#\$%&'\*\+\\\.\/0-9=\?A-Z\^_`a-z{|}~]+@[\-!#\$%&'\*\+\\\.\/0-9=\?A-Z\^_`a-z{|}~]+(\.[\-!#\$%&'\*\+\\\.\/0-9=\?A-Z\^_`a-z{|}~]+)+$/;
	if (re.test(umail))
		return true;
	if (msgLang==2)
		alert(lbl+":"+ERR_EMAIL2);
	else
		alert(lbl+":"+ERR_EMAIL);
	return false;
}

function checktime(ctime,lbl)
{
	if (ctime.length == 0) return true;

	var re=/^(([0-9]|[01][0-9]|2[0-3])(:([0-9]|[0-5][0-9])){0,2}|(0?[0-9]|1[0-1])(:([0-9]|[0-5][0-9])){0,2}\s?[aApP][mM])?$/;
	return re.test(ctime);
}
function checkdate(bdate){

	if (bdate.length == 0) return true;
	var re = /^(0?[1-9]|11|12|10)\/([1-2]?[0-9]|0[1-9]|30|31)\/([1-2]\d{3})$/;
	return re.test(bdate);
}

//检查Ratio
function checkRatio(data,lbl){
	if (data.toString.length == 0) false;
	var tmp ;
	var re = /^[\+]?([0-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)(\.\d+)?$/;
	if (re.test(data)){
		gar = data + '.';
		tmp = gar.split('.');
		if (tmp[0].length > 12) {
			if (msgLang==2)
				alert(lbl+":"+ERR_BIG_NUMBER2);
			else
				alert(lbl+":"+ERR_BIG_NUMBER);
			return false;
		}
		return true;
	}
	if (msgLang==2)
		alert(lbl+":"+ERR_NUMBER2);
	else
		alert(lbl+":"+ERR_NUMBER);
	return false;
}

function checkcode(data,min,max,lbl){
	if (!checkstring(data,max,lbl))
		return false;
	if (data.length<min){
		if (msgLang==2)
			alert(lbl+":"+ERR_STRING_NULL2);
		else
			alert(lbl+":"+ERR_STRING_NULL);
		return false;
	}
	var re = /^[a-zA-Z0-9_]*$/;
	if (!re.test(data)){
		if (msgLang==2)
			alert(lbl+":"+ERR_ALPHA_DIGIT2);
		else
			alert(lbl+":"+ERR_ALPHA_DIGIT);
		return false;
	}
	return true;
}

function getLastDayInMonth(month,year){

	switch (parseInt(month)){
		case 2:
			high =28;
			if (year + '' != 'undefined'){
				if ((year % 4 == 0) && (year % 100 != 0))
					{high =29;}
				else if (year % 400 == 0) {high=29;}
			} else {
				high = 29;
			}
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			high =31;
			break;
		default:
			high =30;
	}
	return high;
}
function trim(val)
{
	var str = val+"";
	if (str.length == 0) return str;
	var re = /^\s*/;
	str = str.replace(re,'');
	re = /\s*$/;
	return str.replace(re,'');
}
