function validateInput()
{
	var szErrMsg = '';

	var szErrMsg = ErrOfInput();
	if (szErrMsg!="") return(szErrMsg);
	return('');
}

function ErrOfInput(){
	var i;
	var oAllItem = document.all;
	var oItem,oItem1;
	var sID1,sID2,sValue,sDate1,sDate2;
	for(i=0;i<oAllItem.length;i++)
	{
		oItem = oAllItem.item(i);
		if (oItem.id != '')
		{
			if ( oItem.id.substr(0,3) == 'IC_' )
			{
				oAllItem.item(i).visited = 0;
			}
		}
	}
	for(i=0;i<oAllItem.length;i++)
	{
		oItem = oAllItem.item(i);
		if (oItem.id != '')
		{
			if ( oItem.id.substr(0,3) == 'IC_' )
			{
				if (oItem.type.toLowerCase() == 'radio')
				{
					oItem.visited = 1;
					continue;
				}
				if (oItem.visited*1 != 1)
				{
					oItem.visited = 1;
					sValue = trimStr(oItem.value);
					oItem.value = sValue;
					if (oItem.isnull*1 == 0 && sValue =='')
					{
						oItem.focus();
						return(oItem.propname + '不能为空！');
					}
					switch(oItem.datatype*1)
					{
						case 2: //Integer
						case 4: //Single
							if (sValue == '' && oItem.isnull*1 == 1) continue;
							if (parseInt(sValue).toString()!= sValue)
							{
								oItem.focus();
								return(oItem.propname + "必须为整数！");
							}
							switch(oItem.tag*1)
							{
								case 0:
									continue;
									break;
								case 1:
									sID = oItem.id.substr(0,oItem.id.length-2) + '_E';
									break;
								case 2:
									sID = oItem.id.substr(0,oItem.id.length-2) + '_S';
									break;
							}
							oItem1 = document.all.item(sID);
							oItem1.visited = 1;
							if (parseInt(oItem1.value).toString()!= oItem1.value)
							{
								oItem1.focus();
								return(oItem1.propname + "必须为整数！");
							}
							if (oItem.tag*1 == 1 && oItem.value*1 > oItem1.value*1 || oItem.tag*1 == 2 && oItem.value*1 < oItem1.value*1)
							{
								oItem.focus();
								if (oItem.tag*1 ==1)
								{
									return(oItem.propname + '不能大于' + oItem1.propname);
								}
								else
								{
									return(oItem1.propname + '不能大于' + oItem.propname);
								}
							}
							break;
						case 8: //varchart
							if (oItem.iscode*1 == 1)
							{
								if (!validateInputString(oItem.value))
								{
									oItem.focus();
									return(oItem.propname + '只能包含数字、字母和下划线！');
								}
							}
							if(getStrAllocateLen(sValue) > oItem.maxlen*1)
							{
								oItem.focus();
								return(oItem.propname + '长度必须小于'+ oItem.maxlen);
							}
							break;
						
						case 6: //datetime - one input
							sID1 = oItem.id;
							sDate1 = oItem.value;
							if (sDate1 == '')
							{
								if (oItem.isnull*1 == 1) continue;
							}
							if (optimizeDate(sDate1) == '')
							{
								oItem.focus();
								return(oItem.propname + "格式错误！\n正确格式如：1999-12-30日（年份必须大于1800）");
							}
							switch(oItem.tag*1)
							{
								case 0:
									continue;
									break;
								case 1:
									sID2 = oItem.id.substr(0,oItem.id.length-2) + '_E';
									break;
								case 2:
									sID2 = oItem.id.substr(0,oItem.id.length-2) + '_S';
									break;
							}
							sDate2 = document.all.itme(sID2).value;
							if (sDate2 == '')
							{
								if (oItem.isnull*1 == 1) continue;
							}
							if (optimizeDate(sDate2) == '')
							{
								document.all.item(sID2).focus();
								return(document.all.item(sID2).propname + "格式错误！\n正确格式如：1999-12-30日（年份必须大于1800）");
							}
							if (oItem.tag*1 == 1)
							{
								var iSign = compareDate(sDate1,sDate2);
							}
							else
							{
								var iSign = compareDate(sDate2,sDate1);
							}
							switch(iSign)
							{
								case -1:
									continue;
									break;
								case 0:
									if (document.all.item(sID1 + '_T') != null)
									{
										if (oItem.tag*1 == 1)
										{
											if (compareTime(document.all.item(sID1 + '_T').value,document.all.item(sID2 + '_T').value) == -1)
											{
												continue;
											}
										}
										else
										{
											if (compareTime(document.all.item(sID2 + '_T').value,document.all.item(sID1 + '_T').value) == -1)
											{
												continue;
											}
										}
									}
									else
									{
										continue;
									}
									break;
								case 1:
									break;
							}
							
							if (oItem.tag*1 ==1)
							{
								oItem.focus();
								return(oItem.propname + '必须小于' + document.all.item(sID2).propname);
							}
							else
							{
								document.all.item(sID2 + '_Y').focus();
								return(document.all.item(sID2).propname + '必须小于' + oItem.propname);
							}
							break;

						case 7: //datetime - three inputs
							sID1 = oItem.id.substr(0,oItem.id.length-2);
							sDate1 = getDateFromDoc(sID1);
							if (sDate1 == '--')
							{
								if (oItem.isnull*1 == 1) continue;
							}
							if (optimizeDate(sDate1) == '')
							{
								oItem.focus();
								return(oItem.propname + "格式错误！\n正确格式如：1999年12月30日（年份必须大于1800）");
							}
							switch(oItem.tag*1)
							{
								case 0:
									continue;
									break;
								case 1:
									sID2 = oItem.id.substr(0,oItem.id.length-4) + '_E';
									break;
								case 2:
									sID2 = oItem.id.substr(0,oItem.id.length-4) + '_S';
									break;
							}
							sDate2 = getDateFromDoc(sID2);
							if (sDate2 == '--')
							{
								if (oItem.isnull*1 == 1) continue;
							}
							if (optimizeDate(sDate2) == '')
							{
								document.all.item(sID2 + '_Y').focus();
								return(document.all.item(sID2 + '_Y').propname + "格式错误！\n正确格式如：1999年12月30日（年份必须大于1800）");
							}
							if (oItem.tag*1 == 1)
							{
								var iSign = compareDate(sDate1,sDate2);
							}
							else
							{
								var iSign = compareDate(sDate2,sDate1);
							}
							switch(iSign)
							{
								case -1:
									continue;
									break;
								case 0:
									if (document.all.item(sID1 + '_T') != null)
									{
										if (oItem.tag*1 == 1)
										{
											if (compareTime(document.all.item(sID1 + '_T').value,document.all.item(sID2 + '_T').value) == -1)
											{
												continue;
											}
										}
										else
										{
											if (compareTime(document.all.item(sID2 + '_T').value,document.all.item(sID1 + '_T').value) == -1)
											{
												continue;
											}
										}
									}
									else
									{
										continue;
									}
									break;
								case 1:
									break;
							}
							
							if (oItem.tag*1 ==1)
							{
								oItem.focus();
								return(oItem.propname + '必须小于' + document.all.item(sID2 + '_Y').propname);
							}
							else
							{
								document.all.item(sID2 + '_Y').focus();
								return(document.all.item(sID2 + '_Y').propname + '必须小于' + oItem.propname);
							}
							break;
						case 11: //boolean
							break;
					}
				}
			}
		}
	}
	return('');
}

function getDateFromDoc(sID)
{
	var oYear = document.all.item(sID + '_Y');
	var oMonth = document.all.item(sID + '_M');
	var oDate = document.all.item(sID + '_D');
	oYear.visited = 1;
	oMonth.visited = 1;
	oDate.visited = 1;
	oYear.value = trimStr(oYear.value);
	oMonth.value = trimStr(oMonth.value);
	oDate.value = trimStr(oDate.value);
	return(oYear.value + '-' + oMonth.value + '-' + oDate.value);
}

function optimizeDate(szDate)
{
	if (typeof(szDate)!='string') return '';
	var datePart = szDate.split('-');
	if (datePart.length!=3) return '';
	var iYear = datePart[0] * 1;
	var iMonth = datePart[1] * 1;
	var iDay = datePart[2] * 1;
	if (isNaN(iYear) || isNaN(iMonth) || isNaN(iDay)) return '';
	if (iYear<1800 || iYear>9999) return '';
	if (iMonth<1 || iMonth>12) return '';
	if (iDay<1 || iDay>31) return '';
	var szTemp = "" + iMonth + "/" + iDay + "/" + iYear;
	var oDate = new Date(szTemp);
	if (szTemp != ("" + ( oDate.getMonth() + 1) + "/" +oDate.getDate() + "/" + oDate.getFullYear())) return '';
	return "" + iYear + "-" + iMonth + "-" + iDay;
}

function compareDate(t1,t2)
{
	if (typeof(t1)!='string') return null;
	if (typeof(t2)!='string') return null;
	var datePart1 = t1.split('-');
	var datePart2 = t2.split('-');
	
	t1 = new Date(datePart1[1]+"/"+datePart1[2]+"/"+datePart1[0]);
	t2 = new Date(datePart2[1]+"/"+datePart2[2]+"/"+datePart2[0]);
	if (t1.valueOf()>t2.valueOf())
	{
		return(1);
	}
	else
	{
		if (t1.valueOf()<t2.valueOf())
		{
			return(-1);
		}
		else
		{
			return(0);
		}
	}
}

function compareTime(t1,t2)
{
	if (typeof(t1)!='string') return null;
	if (typeof(t2)!='string') return null;
	t1 = new Date('9/1/1999 ' + t1);
	t2 = new Date('9/1/1999 ' + t2);
	
	if (t1.valueOf()>t2.valueOf())
	{
		return(1);
	}
	else
	{
		if (t1.valueOf()<t2.valueOf())
		{
			return(-1);
		}
		else
		{
			return(0);
		}
	}
}
