<!-- 

 function validateRules( formObject, validationRules )
 {
  // NOTE: validation rules must be in the following format: 
  //       "@formObjectName.ruleCondition=ruleValue?errorMessage;"
  //
  //       available rule condition are as follows:
  //       - alphabetic
  //	   - email
  //	   - fixlength
  //   	   - floatingnumber
  //       - minlength
  //       - maxlength
  //	   - numeric
  //	   - required
  //	   - textdocument
  //
  //       rule value can be Boolean setting such as:
  //       1 = true 
  // 	   0 = false

  var errorMessage = "";
  var objectElement;
  var objectNames = "";

  // ---- loop through each form object to check whether any rule is broken
  for ( index = 0; index < formObject.length; index++ )
  {
   objectElement = formObject.elements[ index ];

   // ---- make sure same group object will not process twice
   if ( objectNames.search( objectElement.name ) >= 0 ) continue;

   objectNames += objectElement.name + ";";

   errorMessage += validateObject( objectElement, validationRules );   
  }

  return errorMessage;
 }


 function validateObject( formObject, validationRules )
 {
  var startingIndex = 0;
  var endingIndex = 0;
  var objectRule = "";  
  var ruleCondition = "";
  var testValue;
  var errorMessage = "";
  
  // ---- examine each rule associated with the given form object
  while ( startingIndex > -1 )
  {
   // ---- check whether any rule associated with the given form object
   startingIndex = validationRules.indexOf( "@" + formObject.name + ".", startingIndex );

   if ( startingIndex == -1 ) { break; }


   // ---- retrieve the rule description
   endingIndex = validationRules.indexOf( ";", startingIndex );

   objectRule = validationRules.substring( startingIndex + 1, endingIndex );


   // ---- retrieve the rule condition
   ruleCondition = 
     objectRule.substring( objectRule.indexOf( "." ) + 1, objectRule.indexOf( "=" ) );


   // ---- retrieve the rule value
   testValue = 
     objectRule.substring( objectRule.indexOf( "=" ) + 1, objectRule.indexOf( "?" ) );
   

   // ---- retrieve the error message if encounter any rule is broken
   if ( hasBrokenRule( formObject, ruleCondition, testValue ) )
   {
    errorMessage += objectRule.substring( objectRule.indexOf( "?" ) + 1 ) + "\n";
   }

   startingIndex = endingIndex;   
  }

  return errorMessage;
 }


 function hasBrokenRule( formObject, ruleCondition, testValue )
 {
  var objectValue = formObject.value;

  switch( ruleCondition )
  {
   case "alphabetic":
        {
	 if ( testValue == 0 || objectValue.length == 0 ) { return false; }

	 return ( objectValue.search( "[^A-Za-z]" ) >= 0 );

  	 break;
	}

   case "email":
	{
	 if ( testValue == 0 || objectValue.length == 0 ) { return false; }

	 // ---- make sure the value is in valid email format
    	 var splitted = objectValue.match("^(.+)@(.+)$");

	 if ( splitted == null ) { return true; }

	 // ---- make sure user name is valid
    	 if( splitted[ 1 ] != null )
    	 {
      	  return ( splitted[ 1 ].match( /^\"?[\w-_\.]*\"?$/ ) == null );
    	 }

	 // ---- make sure domain name is valid
    	 if( splitted[ 2 ] != null )
    	 {
      	  if ( splitted[ 2 ].match( /^[\w-\.]*\.[A-Za-z]{2,4}$/ ) == null ) 
      	  {
       	   return ( splitted[ 2 ].match( /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/ ) == null );
      	  }
	  else
	  {
	   return false;
	  }
	 }

     	 return true;

	 break;
	}

   case "fixedlength":
	{
	 return ( objectValue.length != testValue );

 	 break;
	}

   case "floatingnumber":
	{
	 if ( testValue == 0 || objectValue.length == 0 ) { return false; }

	 // ---- make sure no more than one decimal point
         if ( objectValue.split( "." ).length > 2 ) { return true; }

	 // ---- remove decimal point to search for any non numeric character
         return ( objectValue.replace( ".", "").search( "[^0-9]" ) >= 0 ); 

	 break;
	}

   case "minlength":
	{
	 return ( objectValue.length < testValue );

 	 break;
	}

   case "maxlength":
	{
    	 return ( objectValue.length > testValue );

 	 break;
	}

   case "numeric":
	{
	 if ( testValue == 0 || objectValue.length == 0 ) { return false; }

         return ( objectValue.search( "[^0-9]" ) >= 0 ); 

	 break;
	}

   case "required":
	{
	 if ( testValue == 0 ) { return false; }

	 switch( formObject.type.toLowerCase() )
	 {
	  case "checkbox":
	  case "radio":
		{
		 // ---- form object reference has been lost
		 // ---- so need to rerefence the actual form object
	 	 var actualObject = formObject.form.elements[ formObject.name ];

		 // ---- test for single object existence
		 if ( isNaN( parseInt( actualObject.length, 10 ) ) )
		 {
		  return ( !actualObject.checked );
		 }

		 // ---- test for multiple object existence
	 	 for ( objectIndex = 0; objectIndex < actualObject.length; objectIndex++ )
  	  	 {
	   	  if ( actualObject[ objectIndex ].checked ) return false;	 
	  	 }

	  	 break;
		}
	
	  case "file":
	  case "password":
	  case "select-multiple":
	  case "select-one":
	  case "text":
		{
		 return ( objectValue.length == 0 );

		 break;	 
		}
	 }
	}

   case "textdocument":
        {
	 if ( testValue == 0 || objectValue.length == 0 ) { return false; }

         var fileExtension = objectValue.substr( objectValue.lastIndexOf( "." ) + 1 ).toUpperCase(); 
 
         return ( !( fileExtension == "DOC" || 
		     fileExtension == "RTF" || 
		     fileExtension == "SDW" || 
		     fileExtension == "SXW" || 
		     fileExtension == "TXT" ) );

  	 break;
	}
  }

  return true;
 }


 function trimAllTextboxValue( formObject, exceptionObject )
 {
  // NOTE: exception object must be in the following format: 
  //       "objectName;objectName;"

  var elementObject;

  for ( elementIndex = 0; elementIndex < formObject.length; elementIndex++ )
  {
   elementObject = formObject[ elementIndex ];

   if ( exceptionObject.search( elementObject.name ) >= 0 ) { continue; }

   switch( elementObject.type.toLowerCase() )
   {
	case "text":
	case "textarea":
		{
		 elementObject.value = 
		   removeLeadingAndTrailingSpaces( elementObject.value );

		 break;
		}
   }   
  }
 }


 function removeLeadingAndTrailingSpaces( stringValue )
 {
  var stringLength = stringValue.length;

  if ( stringLength == 0 ) return "";

  // ---- calculate leading spaces
  var firstIndex = 0;

  for ( leadingIndex = 0; leadingIndex < stringLength; leadingIndex++ )
  {
   if ( stringValue.substr( leadingIndex, 1 ) != " " ) { break; }

   firstIndex++;
  }

  if ( firstIndex == stringLength ) return "";

  // ---- calculate trailing spaces  
  var lastIndex = stringLength;

  for ( trailingIndex = stringLength - 1; trailingIndex >= 0; trailingIndex-- )
  {
   if ( stringValue.substr( trailingIndex, 1 ) != " " ) { break; }

   lastIndex--;
  }

  return stringValue.substring( firstIndex, lastIndex );
 }
 
-->