//\u56e0\u4e3aJAVASCRIPT\u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\u65f6\uff0c\u5c06\u6bcf\u4e2a\u4e2d\u6587\u7684\u957f\u5ea6\u53ea\u8ba1\u7b97\u4e3a\u4e00\u4e2a\u5b57\u7b26\uff0c\u800c\u6570\u636e\u5e93\u4e2d\u9700\u5360\u7528\u4e24\u4e2a
//\u56e0\u6b64\u9700\u8981\u6539\u5199\u8ba1\u7b97\u5b57\u7b26\u957f\u5ea6\u7684\u65b9\u6cd5
function calcStrLen(aStr)
{
	var iLen = aStr.length;
	var iLenAdd = 0;
	for (iLoop = 0; iLoop < iLen; iLoop ++)
	{
		if (aStr.charCodeAt(iLoop) > 127)
			iLenAdd ++;
	}
	iLen += iLenAdd;
	return iLen;
}

function ValidData(theCtrl)
{
	aForm = theCtrl.form;
	aRet = "";
	aCtrlRet = "";
	var aCheckValue;
	//return "Y";

	var aSel = 0;
	var aCheckSel = theCtrl.valid;	//\u68c0\u67e5\u662f\u5426\u9009\u62e9
			
	//\u5faa\u73af\u68c0\u67e5\u6570\u636e\u7684
	for (iIndex = 0; iIndex < aForm.elements.length; iIndex++)
	{
		//alert(aForm.elements[iIndex].type);
			
		aCtrlRet = "";
		aCtrl = aForm.elements[iIndex];

		//\u53ea\u5bf9\u975e\u53ea\u8bfb\u505a\u5224\u65ad
		if (aCtrl.type == "text" && 
		    aCtrl.readOnly==false && 
		    aCtrl.name != "iPageno")
		{
			aType = aCtrl.checktype;
			aValue = aCtrl.value;
			
			//\u6700\u5927\u957f\u5ea6\u63a7\u4ef6\u5f55\u5165\u65f6\u5df2\u63a7\u5236\uff0c\u4f46\u5bf9\u4e2d\u82f1\u6587\u6df7\u5408\u60c5\u5f62\u9700\u8981\u4fee\u6b63
			//JavaScript\u4e2d\u4f5c\u4e3aproperty\u4f7f\u7528\u65f6\uff0c\u6700\u5927\u957f\u5ea6\u4e3a\uff1amaxLength
			aMaxLen = parseInt(aCtrl.maxLength,10);
			//alert(aCtrl.cname + aCtrl.maxLength);
			//alert("aMaxLen=" + aMaxLen);
			if (calcStrLen(aValue) > aMaxLen)
				aCtrlRet += " \u8d85\u51fa\u503c\u6700\u5927\u957f\u5ea6\uff0c\u6700\u5927\u957f\u5ea6=" + aMaxLen + "!\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b97\u4e24\u4e2a\u957f\u5ea6\u3002";
			
			aMinLen = parseInt(aCtrl.minlen,10);
			//if (aValue.length < aMinLen)
			if (calcStrLen(aValue) < aMinLen)
				aCtrlRet += " \u503c\u957f\u5ea6\u4e0d\u591f\uff0c\u6700\u5c0f\u957f\u5ea6=" + aMinLen + "!";

			//alert(aCtrl.name+aMinLen+ " "+aCtrlRet);

			if ("N" == aType)
			//\u68c0\u67e5\u662f\u5426\u4e3a\u6574\u6570
			{
				if (aValue.length > 0)
				{
					aCheckValue = parseInt(aValue,10);
					if (aCheckValue != aValue)
					//isNaN(aCheckValue) ||						
						aCtrlRet += " \u8f93\u5165\u975e\u6574\u6570!";
				}
				else
					aCheckValue = 0;
					
				//\u68c0\u67e5\u6700\u5c0f\u503c\u8981\u6c42
				if (!isNaN(aCtrl.minvalue))
				{
					aMinValue = parseInt(aCtrl.minvalue,10);
					if (aMinValue > aCheckValue)
						aCtrlRet += " \u503c\u5e94\u5927\u4e8e" + aMinValue + "!";
				}

				//\u68c0\u67e5\u6700\u5927\u503c\u8981\u6c42
				if (!isNaN(aCtrl.maxvalue))
				{
					aMaxValue = parseInt(aCtrl.maxvalue,10);
					if (aMaxValue < aCheckValue)
						aCtrlRet += " \u503c\u5e94\u5c0f\u4e8e" + aMaxValue + "!";
				}
			}
			else if ("F" == aType)
			//\u68c0\u67e5\u662f\u5426\u4e3a\u6d6e\u70b9\u6570)
			{
				if (aValue.length > 0)
				{
					aCheckValue = parseFloat(aValue);
					if (aCheckValue != aValue)
						aCtrlRet += " \u8f93\u5165\u975e\u5c0f\u6570!";
				}
				else
					aCheckValue = 0;
				
				//\u68c0\u67e5\u6700\u5c0f\u503c\u8981\u6c42
				if (!isNaN(aCtrl.minvalue))
				{
					aMinValue = parseFloat(aCtrl.minvalue);
					if (aMinValue > aCheckValue)
						aCtrlRet += " \u503c\u5e94\u5927\u4e8e" + aMinValue + "!";
					//else
						//alert(aMinValue +">="+aCheckValue);
				}

				//\u68c0\u67e5\u6700\u5927\u503c\u8981\u6c42
				if (!isNaN(aCtrl.maxvalue))
				{
					aMaxValue = parseFloat(aCtrl.maxvalue);
					if (aMaxValue < aCheckValue)
						aCtrlRet += " \u503c\u5e94\u5c0f\u4e8e" + aMaxValue + "!";
					//else
						//alert(aMaxValue +">="+aCheckValue);
				}
				
				//modify by shirleydmr 2005-04-14 begin
				if (!isNaN(aCtrl.scale))
				{
					aScaleValue = parseInt(aCtrl.scale);
					aCheckValue = parseFloat(aValue);
					iStrLen1 = aValue.length;
					iStrNum1 = aValue.indexOf('.');
					if (-1 != iStrNum1 && (iStrLen1 - iStrNum1 -1) > aScaleValue )
						aCtrlRet += " \u5c0f\u6570\u70b9\u540e\u53ea\u80fd"+aScaleValue+"\u4f4d!";
				}
				//modify by shirleydmr 2005-04-14 end
			}
			else if ("D" == aType)
			//\u68c0\u67e5\u662f\u5426\u65e5\u671f\u683c\u5f0f
			{
				if ("" != aValue)
				{
					aCheckValue = IsDate(aValue);
					if ("Y" != aCheckValue)
						aCtrlRet += "\n\u8f93\u5165\u975e\u5e74\u6708\u65e5-" + aCheckValue;
				}
			}
			//add by shirleydmr 2004-11-15 begin
			else if ("M" == aType)
			//\u68c0\u67e5\u662f\u5426\u5e74\u6708\u683c\u5f0f
			{
				if ("" != aValue)
				{
					aCheckValue = IsYearMonth(aValue);
					if ("Y" != aCheckValue)
						aCtrlRet += "\n\u8f93\u5165\u975e\u5e74\u6708-" + aCheckValue;
				}
			}
			else if ("X" == aType)
			//\u68c0\u67e5\u662f\u5426\u5e74\u65ec\u683c\u5f0f
			{
				if ("" != aValue)
				{
					aCheckValue = IsXun(aValue);
					if ("Y" != aCheckValue)
						aCtrlRet += "\n\u8f93\u5165\u975e\u5e74\u6708\u65ec-" + aCheckValue;
				}
			}
			else if ("J" == aType)
			//\u68c0\u67e5\u662f\u5426\u5e74\u5b63\u5ea6\u683c\u5f0f
			{
				if ("" != aValue)
				{
					aCheckValue = IsJi(aValue);
					if ("Y" != aCheckValue)
						aCtrlRet += "\n\u8f93\u5165\u975e\u5e74\u5b63\u5ea6-" + aCheckValue;
				}
			}
			else if ("B" == aType)
			//\u68c0\u67e5\u662f\u5426\u5e74\u534a\u5e74\u683c\u5f0f
			{
				if ("" != aValue)
				{
					aCheckValue = IsHalfYear(aValue);
					if ("Y" != aCheckValue)
						aCtrlRet += "\n\u8f93\u5165\u975e\u5e74\u534a\u5e74-" + aCheckValue;
				}
			}
			else if ("Y" == aType)
			//\u68c0\u67e5\u662f\u5426\u5e74\u534a\u5e74\u683c\u5f0f
			{
				if ("" != aValue)
				{
					aCheckValue = IsYear(aValue);
					if ("Y" != aCheckValue)
						aCtrlRet += "\n\u8f93\u5165\u975e\u5e74-" + aCheckValue;
				}
			}
			//add by shirleydmr 2004-11-15 end
			else if ("E" == aType)
			{
				if ("" != aValue &&
					"N" == IsEmail(aValue))
					aCtrlRet += "\u8f93\u5165\u975e\u90ae\u7bb1-" + aValue;
			}

			//alert(aCtrl.name+ "---" + aCtrlRet);
			if ("" != aCtrlRet)
			{
				if (0 == aValue.length)
					aValue = "\u7a7a"
				aRet += aCtrl.cname + "--" + "\u8f93\u5165=" + aValue + ": \u4e0d\u5408\u683c! " + aCtrlRet + "\n";
			}
		}
		else if (aCtrl.type == "textarea" && 
		    aCtrl.readOnly==false)
		{
			aValue = aCtrl.value;
			//\u68c0\u67e5\u6700\u5927\u503c\u8981\u6c42
			//TEXTAREA\u63a7\u4ef6\u6ca1\u6709maxlength\u5c5e\u6027\uff0c\u6240\u4ee5\u4f7f\u7528\u7684\u662f\u81ea\u5b9a\u4e49\u5c5e\u6027\u540d\uff1amaxLength
			if (!isNaN(aCtrl.maxlength))
			{
				aMaxLen = parseInt(aCtrl.maxlength,10);
				if (calcStrLen(aValue) > aMaxLen)
					aCtrlRet += " \u8d85\u51fa\u503c\u6700\u5927\u957f\u5ea6\uff0c\u6700\u5927\u957f\u5ea6="+aMaxLen + "!";
			}

			if ("" != aCtrlRet)
			{
				if (0 == aValue.length)
					aValue = "\u7a7a"
				aRet += aCtrl.cname + "--" + "\u8f93\u5165\u4e0d\u5408\u683c! " + aCtrlRet + "\n";
			}			
		}
		//\u9700\u8981\u68c0\u67e5\u7ba1\u7406\u7684\u591a\u9009\u754c\u9762
		else if (aCtrl.name == "selitem" && "N" != aCheckSel)
		{
			if (aCtrl.checked)
				aSel ++;
		}
		else if (aCtrl.type == "radio" && aCtrl.readOnly==false)
		{
			//alert("radio");
			//\u5982\u679c\u8981\u6c42\u68c0\u67e5\u975e\u7a7a
			if ("Y" == aCtrl.checknull)
			{
				//alert("radio check");
				if ("Y" == IsNullRadiobox(aForm, aCtrl, iIndex))
					aRet += aCtrl.cname + "--\u8981\u6c42\u9009\u62e9\u4e00\u9879!\n";
			}
		}
		else if (aCtrl.type == "checkbox" && aCtrl.readOnly==false)
		{
			//\u5982\u679c\u8981\u6c42\u68c0\u67e5\u975e\u7a7a
			if ("Y" == aCtrl.checknull)
			{
				if ("Y" == IsNullCheckbox(aForm, aCtrl, iIndex))
					aRet += aCtrl.cname + "--\u8981\u6c42\u9009\u62e9\u4e00\u9879!\n";
			}
		}
		else if (aCtrl.type == "password" && aCtrl.readOnly==false)
		{
			aType = aCtrl.checktype;
			//\u5bc6\u7801\u786e\u8ba4\u5f55\u5165\u63a7\u4ef6\uff0c\u8981\u6c42\u68c0\u67e5\u5bc6\u7801\u662f\u5426\u76f8\u7b49
			if ("Q" == aType)
			{
				aAffirmCtrl = getAffirmCtrl(aForm, aCtrl);
				if (aAffirmCtrl.value != aCtrl.value)
				{
					aRet += aCtrl.cname + "--\u4e0e" + aAffirmCtrl.cname + "\u8f93\u5165\u4e0d\u4e00\u81f4!\n";
				}
			}
			else //\u5bc6\u7801\u63a7\u4ef6\uff0c\u68c0\u67e5\u5f55\u5165\u5408\u6cd5\u6027
			{
				aMinLen = parseInt(aCtrl.minlen,10);
				if (aCtrl.value.length < aMinLen)
					aRet += aCtrl.cname + "--" + " \u503c\u957f\u5ea6\u4e0d\u591f\uff0c\u6700\u5c0f\u957f\u5ea6=" + aMinLen + "!";
			}
		}
	}
	//alert(aCheckSel + " select " + aSel);

	if ("S" == aCheckSel && 1 != aSel)
		aRet += "\u5fc5\u987b\u4e14\u53ea\u80fd\u9009\u62e9\u4e00\u6761\u4fe1\u606f!"
	else if ("M" == aCheckSel && 0 == aSel)
		aRet += "\u81f3\u5c11\u9009\u62e9\u4e00\u6761\u4fe1\u606f!"
	
	if ("" == aRet)
		aRet = "Y";

	return aRet;
}

//\u5047\u8bbe\uff1a\u4e00\u7ec4\u5355\u9009\u6846\u662f\u8fde\u7eed\u8f93\u51fa\u7684
function IsNullRadiobox(aForm, aCtrl, aStart)
{
	aCtrlName = aCtrl.name;
	isNull = "Y";
	
	//alert(aCtrlName);
	//alert(aStart);
	for (iLoop = aStart; iLoop < aForm.elements.length; iLoop++)
	{
		aCheckCtrl = aForm.elements[iLoop];
		//alert(aCheckCtrl.type + "--" + aCheckCtrl.name);
		if (aCheckCtrl.type == "radio" &&
			aCheckCtrl.name == aCtrlName)
		{
			//alert("find one");
			if (aCheckCtrl.checked)
			{
				isNull = "N";
				break;
			}
		}
		else
			break;
	}
	return isNull;
}

//\u5047\u8bbe\uff1a\u4e00\u7ec4\u590d\u9009\u6846\u662f\u8fde\u7eed\u8f93\u51fa\u7684
function IsNullCheckbox(aForm,aCtrl,aStart)
{
	aCtrlName = aCtrl.name;
	isNull = "Y";
	alert(aStart);
	for (iLoop= aStart; iLoop < aForm.elements.length; iLoop++)
	{
		aCheckCtrl = aForm.elements[iLoop];
		if (aCheckCtrl.type == "checkbox" &&
			aCheckCtrl.name == aCtrlName)
		{
			if (aCheckCtrl.checked)
			{
				isNull = "N";
				break;
			}
		}
		else
			break;
	}
	return isNull;
}

function getAffirmCtrl(aForm, aCtrl)
{
	aCtrlName = aCtrl.affirm;
	for (iLoop=0; iLoop<aForm.elements.length; iLoop++)
	{
		aCheckCtrl = aForm.elements[iLoop];
		if (aCheckCtrl.type == "password" &&
			aCheckCtrl.name == aCtrlName)
		{
			return aCheckCtrl;
		}
	}
}
