// function to check required form element
function CheckRequired ( theElement , theElementName )
{
	if ( theElement == undefined )
	{
		alert ( "要求检查的项目（" + theElementName	+ "）并不是一个有效的JavaScript对象" ) ;
		return false;
	}
	theElement.value = trimString ( theElement.value ) ;
	if( theElement.value == "" )
	{
		alert( "\"" + theElementName + "\"" + "必须填写！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check INT form element
function CheckIntNumber ( theElement , theElementName )
{
	/*
	if ( theElement == undefined )
	{
		alert ( "要求检查的项目（" + theElementName	+ "）并不是一个有效的JavaScript对象" ) ;
		return false;
	}
	*/
	if( isNaN( parseInt ( theElement.value ) ) )
	{
		alert( "\"" + theElementName + "\"" + "不是一个有效的整数！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check Float form element
function CheckFloatNumber ( theElement , theElementName )
{
	/*
	if ( theElement == undefined )
	{
		alert ( "要求检查的项目（" + theElementName	+ "）并不是一个有效的JavaScript对象" ) ;
		return false;
	}
	*/
	if( isNaN( parseFloat ( theElement.value ) ) )
	{
		alert( "\"" + theElementName + "\"" + "不是一个有效的数字！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check Email form element
function CheckEmail ( theElement , theElementName )
{
	/*
	if ( theElement == undefined )
	{
		alert ( "要求检查的项目（" + theElementName	+ "）并不是一个有效的JavaScript对象" ) ;
		return false;
	}
	*/
	if( ! isEmail( theElement.value ) )
	{
		alert( "\"" + theElementName + "\"" + "不是一个有效的E-mail地址！" ) ;
		theElement.focus();
		return false ;
	}
	return true;
}

// function to check weburl form element
function CheckURL(theElement , theElementName)		
{   	  
	var regEx =/(\w+):\/\/([^/:]+)(:\d*)?([^# ]*)/i ;	
	var weburl = theElement.value;
	var r = weburl.search(regEx);   	
	if (r == -1)
	{
		alert( "\"" + theElementName + "\"" + "不合法！" ) ;
		theElement.focus();
		return false 
	}
	return true;
}
// function to check telno or faxno form element
function CheckPhone(theElement , theElementName)		
{   	  
	var regEx =/^[0-9-]/i ;	
	var telno = theElement.value;
	var r = telno.search(regEx);   	
	if (r == -1)
	{
		alert( "\"" + theElementName + "\"" + "不合法！" ) ;
		theElement.focus();
		return false 
	}
	return true;
}

// function to check postcode form element
function CheckPostCode(theElement , theElementName)		
{   	  
	var regEx =/^[0-9]+$/i ;	
	var postcode = theElement.value;
	var r = postcode.search(regEx);   	
	if (r == -1)
	{
		alert( "\"" + theElementName + "\"" + "不合法！" ) ;
		theElement.focus();
		return false 
	}
	else
	{
		if (postcode.length != 6)
		{
			alert( "\"" + theElementName + "\"" + "不合法！" ) ;
			theElement.focus();
			return false 
		}	
	}
	
	return true;
}
// function to check or uncheck all checkbox elements of a form by a select-all checkbox value
function ChangeSelectAll( theForm , bSelected )
{
	/*
	if ( theForm == undefined )
	{
		alert ( "要求填写的表单（" + theElementName	+ "）并不是一个有效的JavaScript对象" ) ;
		return false;
	}
	*/
	for ( i = 0 ; i < theForm.elements.length ; i ++ )
	{
		obj = theForm.elements[i] ;
		if ( obj.type == "checkbox" )
		{
			obj.checked = bSelected ;
		}
	}
}

// function to set selected option of select form element by given option value
function FillSelectInput ( theSelectInput , strSelectedValue )
{
	/*
	if ( theSelectInput == undefined )
	{
		alert ( "要求填写的表单列表并不是一个有效的JavaScript对象" ) ;
		return false;
	}
	*/
	for ( i = 0 ; i < theSelectInput.length ; i ++ )
	{
		if ( theSelectInput.options [ i ].value == strSelectedValue )
		{
			theSelectInput.options[i].selected = true ;
			break ;
		}
	}
}