function validateForm(frm){ 
	var bFldError
	var sClassName
	var bPageError = false
	var msg = "";
	var bContinue = true;
	
	// Call enforceBusinessRules() which will allow the form to massage the form
	// data as necessary before attempting to validate it.
	try
	{
		bContinue = enforceBusinessRules();
	}
	catch (e){}
	
	if (!bContinue)
		return false;
	
	for (var n=0; n<frm.elements.length; n++){
		bFldError = false;

		obj = frm.elements[n];
		switch (obj.className)
		{		
		case "rqrdNumeric":
		case "rqrdNumericErr":
			bFldError = !isNumeric(obj.value);
			break;
		case "optionalNumeric":
		case "optionalNumericErr":
			if (obj.value != "")
				bFldError = !isNumeric(obj.value);
			break;
		case "rqrdDate":
		case "rqrdDateErr":
			if (obj.value == "") 
				bFldError = true;
			else
				bFldError = !chkdate(obj.value);
			break;
		case "optionalDate":
		case "optionalDateErr":
			if (obj.value != "") 
				bFldError = !chkdate(obj.value);
			break;
		case "rqrdBoolean":
		case "rqrdBooleanErr":
			switch (obj.value.toLowerCase())
			{
			case "true":
			case "false":
				bFldError = false;
				break;
			default:
				bFldError = true;
			}
			if (bFldError)
				bFldError = !isNumeric(obj.value);
         	break;			
		case "optionalBoolean":
		case "optionalBooleanErr":
			if (obj.value != "")
			{
				switch (obj.value.toLowerCase())
				{
				case "true":
				case "false":
					bFldError = false;
					break;
				default:
					bFldError = true;
				}
				if (bFldError)
					bFldError = !isNumeric(obj.value);
			}
         	break;
		case "rqrdEMailAddress":
		case "rqrdEMailAddressErr":
			bFldError = (!ValidateEmailAddress(obj.value));
			break;
		case "optionalEMailAddress":
		case "optionalEMailAddressErr":
			if (obj.value != "")
				bFldError = (!ValidateEmailAddress(obj.value));
			break;
		case "rqrd":
      	case "rqrdErr":   
			bFldError = ValidateRequiredField(obj);        
			break;
		}
			
      // Pass the validation off to the user
      try {
         bUserDefinedError = FieldValidation(obj, bFldError);
      } catch (e){
         bUserDefinedError = false;
      }
      
      var sClass = obj.className;
      if (sClass.substring(sClass.length - 3) == "Err")
         sClass = sClass.substr(0, sClass.length - 3);
            
      if (bFldError || bUserDefinedError){
      	obj.className = sClass + "Err";
         bPageError = true;
      } else {
         obj.className = sClass;
      }
	}
   // Pass the validation off to the user
   try {
      bPageError = ValidatePage(bPageError);
   } catch (e) {
      // do nothing
   }
   
	return !(bPageError);
}


function ValidateRequiredField(obj){
	var bFldError = false;

   if ((obj.type == "select-multiple") || (obj.type == "select-one"))
   {
	if (obj.selectedIndex == 0)
         bFldError = true;
   } else {
		if (obj.value == "")
			bFldError = true;
   }
 
   return bFldError;
}

function isNumeric(sValue){
   if (sValue == "") 
      return false;
      
   if (isNaN(sValue))
      return false;
      
   return true;
}

function chkdate(strDate) {
   var strDatestyle = "US"; //United States date style Month Day Year
   //var strDatestyle = "EU"; //European date style Day Month Year
   var runTime = new Date();
   var strCurrentYear = runTime.getYear();
   var strDateArray;
   var strDay;
   var strMonth;
   var strYear;
   var intday;
   var intMonth;
   var intYear;
   var booFound = false;
   var strSeparatorArray = new Array("-"," ","/",".");
   var intElementNr;
   var err = 0;
   var strMonthArray = new Array(12);
   strMonthArray[0] = "Jan";
   strMonthArray[1] = "Feb";
   strMonthArray[2] = "Mar";
   strMonthArray[3] = "Apr";
   strMonthArray[4] = "May";
   strMonthArray[5] = "Jun";
   strMonthArray[6] = "Jul";
   strMonthArray[7] = "Aug";
   strMonthArray[8] = "Sep";
   strMonthArray[9] = "Oct";
   strMonthArray[10] = "Nov";
   strMonthArray[11] = "Dec";
   
   if (strDate.length < 1) {
      return true;
   }
   for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
      if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
         strDateArray = strDate.split(strSeparatorArray[intElementNr]);
         if ((strDateArray.length < 2) || (strDateArray.length > 3)) {
            err = 1;
            return false;
         }
         else {
            strDay = strDateArray[0];
            strMonth = strDateArray[1];
            if (strDateArray.length == 2) 
               strYear = strCurrentYear
            else
               strYear = strDateArray[2];
         }
         booFound = true;
      }
   }
   if (booFound == false) {
      if (strDate.length>5) {
         strDay = strDate.substr(0, 2);
         strMonth = strDate.substr(2, 2);
         strYear = strDate.substr(4);
      }
   }

   if (strYear == null) {
      return false;
   }

   if (strYear.length == 1) {
      strYear = '0' + strYear
   }

   if (strYear.length == 2) {
      strYear = '20' + strYear;
   }

   // US style
   if (strDatestyle == "US") {
      strTemp = strDay;
      strDay = strMonth;
      strMonth = strTemp;
   }

   if (!isNumeric(strDay))
      return false;
   intday = parseInt(strDay, 10);
   if (isNaN(intday)) {
      err = 2;
      return false;
   }

   if (!isNumeric(strMonth))
      return false;
   intMonth = parseInt(strMonth, 10);
   if (isNaN(intMonth)) {
      for (i = 0;i<12;i++) {
         if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
            intMonth = i+1;
            strMonth = strMonthArray[i];
            i = 12;
         }
      }

      if (isNaN(intMonth)) {
         err = 3;
         return false;
      }
   }

   if (!isNumeric(strYear))
      return false;
      
   intYear = parseInt(strYear, 10);
   if (isNaN(intYear)) {
      err = 4;
      return false;
   }

   if (intMonth>12 || intMonth<1) {
      err = 5;
      return false;
   }

   if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
      err = 6;
      return false;
   }

   if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
      err = 7;
      return false;
   }

   if (intMonth == 2) {
      if (intday < 1) {
         err = 8;
         return false;
      }
      if (LeapYear(intYear) == true) {
         if (intday > 29) {
            err = 9;
            return false;
         }
      }
      else {
         if (intday > 28) {
            err = 10;
            return false;
         }
      }
   }

   return true;
}

function LeapYear(intYear) {
   if (intYear % 100 == 0) {
      if (intYear % 400 == 0) { return true; }
   }
   else {
      if ((intYear % 4) == 0) { return true; }
   }
   return false;
}

function ValidateEmailAddress(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
		return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	
	if (str.indexOf(" ")!=-1){
		return false;
	}

	return true;
}

/*
sub isvaliddate {
  my $input = shift;
  if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) {
    # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
    if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
      return 0; # 31st of a month with 30 days
    } elsif ($3 >= 30 and $2 == 2) {
      return 0; # February 30th or 31st
    } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 <> 0 or $1 % 400 == 0))) {
      return 0; # February 29th outside a leap year
    } else {
      return 1; # Valid date
    }
  } else {
    return 0; # Not a date
  }
}

*/
