/**
 * check whether input is numeric.
 * @param  string  form/field name
 * @return boolean
 */
function IsNumeric(f,fld)
{
   var form=document.forms[f];
   var obj=form.item(fld);

   if(obj==null)
      return false;

   if(obj.value=="")
      return true;

   reg=/^\d{1,8}(\.\d{0,4})?$/gi;

   return reg.test(obj.value);
}

/**
 * check whether input is numeric.
 * @param  string  form/field name
 * @return boolean
 */
function IsNumeric(stringValue)
{
	if(stringValue == null)
		return true;

   reg=/^\d{1,8}(\.\d{0,4})?$/gi;
   return reg.test(stringValue);
}

/**
 * check whether input is validate date.
 * if not, focus the field, and make the filed selected
 * @param  string  form/field name(NOT control object)
 * @return boolean
 * @note   call this function in onblur
 */
function validateDate(fld)
{
	var oDate = document.forms[0].item(fld);	// the date input control
	if(oDate == null)
	{
		return true;
	}
	var sDate = oDate.value;				// date in String
	if(sDate == "")
	{
		return true;
	}

	if(!isDateString(sDate, "-"))	//not validate date
	{
		alert('无效日期格式，请按“年年年年-月月-日日”格式输入。');
		oDate.value = "";
		oDate.focus();
		return false;
	}
	return true;
}

/**
 * multi control using the same name
 * @param  string  form/field name(NOT control object)
 * @param  string  index  index of the control (01, 02......)
 * @return boolean
 * @note   call this function in onblur
 */
function validateDate(fld, index)
{
	var oMultiDate = document.forms[0].item(fld);	// the date input control
	var nIndex = index;

	if(oMultiDate == null)	//field not exist
	{
		return true;
	}

	if(oMultiDate.length > 1) // multi
	{
		oDate = oMultiDate[nIndex-1];
	}
	else
	{
		oDate = oMultiDate;
	}

	if(oDate == null )
	{
		return true;
	}

	var sDate = oDate.value;				// date in String
	if(sDate == "")
	{
		return true;
	}

	if(!isDateString(sDate, "-"))	//not validate date
	{
		alert('无效日期格式，请按“年年年年-月月-日日”格式输入。');
		oDate.focus();
		oDate.value = "";
		return false;
	}
	return true;
}

/*
 * validate a date, the date must be seperate by "-" ,like 2002-2-2
 * @param String
 *
 * @return boolean
 */
function isDateString(sDate)
{
	return isDateString(sDate, "-");
}

/*
 * validate a date
 * @param String sDate string date to validate
 * @param String sSpit split
 *
 * @return boolean
 */
function isDateString(sDate, sSpit)
{	var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
	var iaDate = new Array(3)
	var year, month, day

	iaDate = sDate.split(sSpit)
	if (iaDate.length != 3) return false
	if (iaDate[1].length > 2 || iaDate[2].length > 2) return false

	//check numeric
	if(!IsNumeric(iaDate[0]) || !IsNumeric(iaDate[1]) || !IsNumeric(iaDate[2]) )
	{
		return false;
	}

	year = parseFloat(iaDate[0])
	month = parseFloat(iaDate[1])
	day=parseFloat(iaDate[2])

	if (year < 1900 || year > 2200) return false
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
	if (month < 1 || month > 12) return false
	if (day < 1 || day > iaMonthDays[month - 1]) return false
	return true
}


//==================================数据检查方法======================================================


function isNaNStr(value){
	
	if (value =="") return false;
	else{
		return isNaN(value);
	}
}
function checkEmpty(txtField,Name){

	if (txtField.value=="") {
		alert("请输入“"+ Name +"”");
		txtField.focus();
		return false;
	}

	return true;
}

function checkNum(txtField,Name){
	
	if (isNaNStr(txtField.value)) {
		
		alert("请在“"+ Name +"”中输入有效数字");
		txtField.focus();
		txtField.select();
		return false;
	}
	return true;
}
function checkDate(txtField,Name){
	if (txtField.value=="") return true;
    if (!isDateString(txtField.value,"-")){
  		alert("在“"+Name+"”中输入无效的日期格式，请按“YYYY-MM-DD”的格式输入");
		return false;
	}
	return true;
}
//==========================================================================================
