function checkTheForm (form)
{
	for (var i = 0 ; i < form.elements.length ; i ++)
	{
		var inputObject = form.elements [i] ;
		if (!checkOneObject (form , inputObject) )
		{
		   inputObject.focus();
		   return false;
		}
	}
	return true ;
}

function checkOneObject (form , inputObject)
{
	if (inputObject.getAttribute ("needCheck") != 'true')
		return true ;
	// check Input Object.
	if (inputObject.tagName == "INPUT" || inputObject.tagName == "TEXTAREA")
		return checkInput (form , inputObject) ;
	else
	if (inputObject.tagName == "SELECT")
		return checkSelect (form , inputObject) ;
	
	return true ;
}


function checkSelect (form , selectObject)
{
	var label = selectObject.getAttribute ("textLabel") ;
	if (label == null)
		label = selectObject.name ;
	
	if (selectObject.multiple)
	{
		minVlaue = parseInt (selectObject.getAttribute ("minValue")) ;
		if (isNaN (minValue) || minValue < 0)
			minValue = 0 ;
			
		if ("true" == selectObject.getAttribute ("notEmpty")
			&& minValue <= 0)
			minValue = 1 ;
			
		maxValue = parseInt (selectObject.getAttribute ("maxValue")) ;

		if (isNaN (maxValue) || maxValue < minValue || maxValue > selectObject.options.length)
			maxValue = selectObject.options.length ;
		var count = 0 ;
		
		for (var i = 0 ; i < selectObject.options.length ; i ++)
		{
			if (selectObject.options [i].selected)
				count ++ ;
		}
		if (count < minValue)
		{
			alert ("[" + label + "]必须至少选择" + minValue + "个！") ;
			return false ;
		}
		if (count > maxValue)
		{
			alert ("[" + label + "]最多只能选择" + maxValue + "个！") 
			return false ;
		}
		
	}
	else
	{
		var notEmpty = "true" == selectObject.getAttribute ("notEmpty") ;
		if (! notEmpty)
			return true ;
			
		var minValue = parseInt (selectObject.getAttribute ("minValue")) ;
		if (isNaN (minValue) || minValue < 0)
			minValue = 0 ;
		if (notEmpty && selectObject.selectedIndex < 0)
		{
			alert ("[" + label + "]必须选择一个！") ;
			return false ;
		}
		
		if (notEmpty && selectObject.selectedIndex < minValue)
		{
			alert ("[" + label + "]必须选择第" + (minValue + 1) + "个以后的选项！") ;
			return false ;
		}
	}
	
	return true ;
}
function checkInput (form , inputObject)
{
	// load basic information ;
	var label = inputObject.getAttribute ("textLabel") ;
	if (label == null)
		label = inputObject.name ;
	
	if (inputObject.type == "checkbox" )
		return checkCheckBox (label , form , inputObject)
	else
		return checkTextInput (label , form , inputObject) ;
}
function checkCheckBox (label , form , inputObject)
{
	// check checkbox
	var maxValue = parseInt (inputObject.getAttribute ("maxValue")) ;
	if (isNaN (maxValue))
		maxValue = 0 ;
	var minValue = parseInt (inputObject.getAttribute ("minValue")) ;
	
	if (isNaN (minValue))
		minValue = 0 ;
	if (minValue <= 0 && inputObject.getAttribute ("notEmpty") == "true")
		minValue = 1 ;

	var boxs = form.all (inputObject.name) ;
	if (boxs == null || boxs.length <= 0)
		return true ;
		
	if (maxValue >= 0 && boxs.length < maxValue)
		maxValue = boxs.length ;
		
	if (minValue <= 0 && maxValue <= 0)
		return true ;
		
	var count = 0 ;
	for (var i = 0 ; boxs != null && i < boxs.length ; i ++)
	{
		if (boxs [i].checked)
			count ++ ;
	}

	if (count < minValue && minValue > 0)
	{
		alert ("至少选中" + minValue + "项[" + label + "]！") ;
		return false ;
	}
	if (count > maxValue && maxValue > 0) 
	{
		alert ("不能选中多于" + maxValue + "项[" + label + "]！") ;
		return false ;
	}	
	return true ;
}

function checkTextInput (label , form , inputObject)
{
	// check sameAs
	var samedObjectName = inputObject.getAttribute ("sameAs") ;

	if (samedObjectName != null && samedObjectName != '')
	{
		var samedObject = form.all (samedObjectName) ;
		if (samedObject.length == undefined)
		{
			samedObject = new Array (samedObject) ;
		}
		if (samedObject != null && samedObject.length > 0)
		{
			var bol = true ;
			var samedLabel = samedObjectName ;
			
			for (var i = 0 ; i < samedObject.length ; i ++)
			{
				if (samedObject [i].value != inputObject.value)
				{
					bol = false ;
					if (samedObject [i].getAttribute ("textLabel") != null 
						&& samedObject [i].getAttribute ("textLabel") != '')
						samedLabel = samedObject [i].getAttribute ("textLabel") ;
					break ; 
				}
			}
			
			if (! bol)
			{
				alert ("[" + label + "]必须与[" + samedLabel + "]相同！") ;
				return false ;
			}
		}
			
	}
	
	// ignore spaces, trim value
	if (inputObject.getAttribute ("ignoreSpaces") == "true")
	{
		inputObject.value = inputObject.value.replace(/(^\s*)|(\s*$)/g, "") ;
	}
	
	// Not Empty.
	if (inputObject.getAttribute ("notEmpty") == "true" && inputObject.value == "")
	{
		alert ("[" + label + "] 不能为空！") ;
		
		return false ;
	}
	
	// check regularExpression.
	var regExpString = inputObject.getAttribute ("regularExpression") ;
	if (regExpString != null && regExpString != '')
	{
		// check regExp ;
		var regExp = new RegExp (regExpString) ;
		
		var rs = regExp.exec (inputObject.value) ;
		
		if (! rs)
			alert ([inputObject.name] + "不符合规定！") ;
			
		return rs ;
	}
	
	// check dataType
	var dataType = inputObject.getAttribute ("dataType") ;
	
	if (dataType == "integer")
		return checkInteger (label , form , inputObject) ; 
	else
	if (dataType == "float")
		return checkFloat (label , form , inputObject) ; 
	else
	if (dataType == "date")
		return checkDate (label , form , inputObject) ;
	else
	if (dataType == "email")
		return checkEmail (label , form , inputObject) ;
	else
		return checkString (label , form , inputObject) ;
}


function checkString (label , form , inputObject)
{
	// check length
	var maxValue = inputObject.getAttribute ("maxValue") ;
	if (maxValue != null && inputObject.value.length > maxValue)
	{
		alert ("[" + label + "]不能超过" + maxValue + "个字符！") ;
		return false ;
	}

	var minValue = inputObject.getAttribute ("minValue") ;
	if (minValue != null && inputObject.value.length < minValue)
	{
		alert ("[" + label + "]不能少于" + minValue + "个字符！") ;
		return false ;
	}
	
	// allowedCharSet and notAllowedCharSet
	var allowedCharSet = inputObject.getAttribute ("allowedCharSet") ;
	if (allowedCharSet != null && allowedCharSet.length > 0)
	{
		for (var i = 0 ; i < inputObject.value.length ; i ++)
		{
			var ch = inputObject.value.charAt (i) ;
			if (allowedCharSet.indexOf (ch) < 0)
			{
				alert ("[" + label + "]仅允许输入下列字符：" + allowedCharSet) ;
				return false ;
			}
		}
	}
	
	var notAllowedCharSet = inputObject.getAttribute ("notAllowedCharSet") ;
	if (notAllowedCharSet != null && notAllowedCharSet.length > 0)
	{
		for (var i = 0 ; i < notAllowedCharSet.length ; i ++)
		{
			var ch = notAllowedCharSet.charAt (i) ;
			if (inputObject.value.indexOf (ch) >= 0)
			{
				alert ("[" + label + "]不允许输入字符\"" + ch + "\"") ;
				return false ;
			}
		}
	}
	return true ;
}

function checkFloat (label , form , inputObject)
{
	if (inputObject.value == "") return ture;
	var f = parseFloat (inputObject.value) ;
	
	if (isNaN (f))
	{
		alert ("[" + label + "]应当输入数值！") ;
		return false ;
	}
	var maxValue = inputObject.getAttribute ("maxValue") ;
	if (maxValue != null && f > maxValue)
	{
		alert ("[" + label + "]不能大于" + maxValue + "！") ;
		return false ;
	}

	var minValue = inputObject.getAttribute ("minValue") ;
	if (minValue != null && f < minValue)
	{
		alert ("[" + label + "]不能小于" + minValue + "！") ;
		return false ;
	}
	return true;
}

function checkInteger (label , form , inputObject)
{
	var int = parseInt (inputObject.value) ;
	
	if (isNaN (int))
	{
		alert ("[" + label + "]应当输入整数！") ;
		return false ;
	}
	inputObject.value = int ;
	var maxValue = inputObject.getAttribute ("maxValue") ;
	if (maxValue != null && int > maxValue)
	{
		alert ("[" + label + "]不能大于" + maxValue + "！") ;
		return false ;
	}

	var minValue = inputObject.getAttribute ("minValue") ;
	if (minValue != null && int < minValue)
	{
		alert ("[" + label + "]不能小于" + minValue + "！") ;
		return false ;
	}
	return true;
}

function getControlValue (control)
{
	if (control.tagName == "SELECT")
	{
		var option = control.options [control.selectedIndex] ;
		if (option.value == null || option.value == '')
			return option.text ;
		else
			return option.value ;
	}
	else
		return control.value ;
}

function checkDate (label , form , inputObject)
{
	// hidden field should makeup a full length date string using <select>
	if (inputObject.type == 'hidden')
	{
		var currentDate = new Date () ;
		var year = null ;
		for (var i = 0 ; i < form.elements.length ; i ++)
		{
			if (form.elements [i].name == inputObject.name + "_year")
			{
				year = getControlValue (form.elements [i]) ;
				break ;
			}
		}
		
		if (year == null || year == '')
			year = currentDate.getFullYear () ;
			
		var month = null ;
		for (var i = 0 ; i < form.elements.length ; i ++)
		{
			if (form.elements [i].name == inputObject.name + "_month")
			{
				month = getControlValue (form.elements [i]) ;
				break ;
			}
		}
		
		if (month == null || month == '')
			month = currentDate.getMonth () + 1 ;
			
		var day = null ;
		for (var i = 0 ; i < form.elements.length ; i ++)
		{
			if (form.elements [i].name == inputObject.name + "_day")
			{
				day = getControlValue (form.elements [i]) ;
				break ;
			}
		}
		
		if (day == null || day == '')
			day = currentDate.getDate () ;
		
		inputObject.value = year + "-" + makeDateNumber (month) + "-" + makeDateNumber (day) ;
	}
	
	var regExp = /^([0-9]{4}|[0-9]{2})-(0[0-9]|1[0-2])-([0-2][0-9]|3[0-1])$/ ; 
	var bol = regExp.exec (inputObject.value) ;
		
	if (! bol)
		alert ([inputObject.name] + "不是合法的日期！") ;
		
	return bol ;
}
function makeDateNumber (num)
{
	num = parseInt (num) ;
	if (isNaN (num))
		return "01" ;
	if (num > 10)
		return num ;
	else
		return "0" + num ;
}
function checkEmail (label , form , inputObject)
{
	var regExp = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/ ; 
	var bol = regExp.exec (inputObject.value) ;
		
	if (! bol)
		alert ([inputObject.name] + "不是合法的邮件地址！") ;
		
	return bol ;
}
