





function validateFileFormField(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var formName = form.getAttributeNode("name");
        
        oFileFormField = eval('new ' + formName.value + '_fileFormField()');
        
        var extnMatched = null;
        
        for ( x in oFileFormField ) 
        {
          var sAttrName = oFileFormField[x][0];
          var field = form[oFileFormField[x][0]];
          var sFilepathName = field.value;
          
          var sFileExtnAllowed = oFileFormField[x][2]("fileExtensionAllowed");
          var sMaxFileSize = oFileFormField[x][2]("maxFileSize");
          var iMaxFileSize = parseInt(sMaxFileSize);

          if ( field.type == 'file' && (sFilepathName.length > 0) 
            && (sFileExtnAllowed != null || iMaxFileSize != null) && field.disabled == false) {
              if (!validateFileField(sAttrName, iMaxFileSize, sFileExtnAllowed)) {
                if (i == 0) {
                    focusField = field;
                }
                bValid = false;
              }
            }
          }
                 
          return bValid;   
            
      }
    
      function validateFileField(sAttrName, iMaxFileSizeAllowed, sAllowedExtns) {
        var bAllCorrect = true;
        //store errors that propted during validation
        var errors = new Array();
        var index = 0;   
      
        // to check if it's IE
        if (document.all && document.getElementById) {
          var oFileObj = document.getElementById(sAttrName)
          var oFileObjValue;
          if ( oFileObj != null) {
            //when the user select a file, this field may looks like "C:\XXX.txt"    
            oFileObjValue = oFileObj.getAttribute("value");      
            if (oFileObjValue != null && oFileObjValue != "") {
              var oas = new ActiveXObject("Scripting.FileSystemObject");
              if (oas.FileExists(oFileObjValue)) {
                var oFile = oas.getFile(oFileObjValue);
                var iSize = oFile.size;
                
                if (iMaxFileSizeAllowed > 0 && iSize > iMaxFileSizeAllowed) {
                  errors[index++] = oFile.Name + " has exceeded " + iMaxFileSizeAllowed + " bytes";        
                } 
        
                //validate if the extension of selected file is valid
                var isMatched = false;
                
                if (sAllowedExtns != null) {
                  var aAllowedExtns = sAllowedExtns.split(",");
                  for (validExtn in aAllowedExtns) {
                    var startIndex = oFileObjValue.lastIndexOf(".");
                    var endIndex = oFileObjValue.length;
                    var fileExtension=oFileObjValue.substring(startIndex+1, endIndex);    
                      if (aAllowedExtns[validExtn] == fileExtension) {
                         isMatched = true;
                         break;
                      }
                    } 
                    if (!isMatched) {
                        errors[index++] = fileExtension + " is not a supported file extension. \n" +
                          "The file you select should be in the following extensions: \n " + aAllowedExtns.toString();
                    }    
                  }
               }
            }   
          }
          else {
            //alert("No file attached");
          }
          
          if (errors.length > 0) 
          {
            bAllCorrect = false;
            alert(errors.join('\n'));
            //alert(oFileObj.innerHTML);
            errors == null;
            oFileObj.parentElement.innerHTML += "";
          } else {
             bAllCorrect = true;
          }
          return bAllCorrect;
        }
      }
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateByte.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid byte.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateByte(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        
        var oByte = eval('new ' + jcv_retrieveFormName(form) + '_ByteValidations()');

        for (var x in oByte) {
            if (!jcv_verifyArrayElement(x, oByte[x])) {
                continue;
            }
            var field = form[oByte[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'select-one' ||
                field.type == 'radio')) {

                var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else {
                    value = field.value;
                }

                if (value.length > 0) {
                    if (!jcv_isDecimalDigits(value)) {
                        bValid = false;
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oByte[x][1];

                    } else {

                        var iValue = parseInt(value, 10);
                        if (isNaN(iValue) || !(iValue >= -128 && iValue <= 127)) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oByte[x][1];
                            bValid = false;
                        }
                    }
                }

            }
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return bValid;
    }

function replaceString ( s1, s2, s3 )
      {
        var sTmp = s1;
        var iIndexPos1 = sTmp.indexOf(s2);
        
        while ( iIndexPos1 >= 0 )
        {
          var iIndexPos2 = iIndexPos1 + s2.length;
          sTmp = sTmp.substring(0,iIndexPos1) 
          + s3 
          + sTmp.substring(iIndexPos2);
          iIndexPos1 = sTmp.indexOf(s2);
        }
        return sTmp;
      }
      
      function getElementName(simpleElExp)
      {
        return simpleElExp.replace("[\"","").replace("\"].value","");
      }
      
      var defArrIdx = "defIdxer";
      function addIndexerForArrayObj(arrObj)
      {
        var sTmp = arrObj;
        var ps = arrObj.indexOf(".");
        if(ps >=0){
          sTmp = sTmp.substring(0, ps)
            + "["
            + defArrIdx
            + "]"
            + sTmp.substring(ps);
        }
        return sTmp;
      }
            
      function validateCondition(form) 
      {
        var i = 0;
        var focusField = null;
        var fields = new Array();
        var formName = form.getAttributeNode("name");
        oValidateCondition = eval('new ' + formName.value + '_validateCondition()');
        
        //regular expression for searching form element, like ["button1"].value
        var formElReg = /\[\"[a-zA-Z0-9\[\]\.]*\"\].value/g; 
        
        for ( x in oValidateCondition ) 
        {
          var field = form[oValidateCondition[x][0]];
          var sCondition = oValidateCondition[x][2]("condition");
          var sIteratorIndex1 = oValidateCondition[x][2]("iteratorIndex1");
          var bUseSimpleArr = false;
          
          var propArr = sCondition.match(formElReg);
          for(j=0; j<propArr.length; j++)
          {
            var propName = getElementName(propArr[j]);
            if(typeof form[propName] == "undefined")
            {
              if(sIteratorIndex1 == null || sIteratorIndex1 == '')
              {
                propName = addIndexerForArrayObj(propName);
                if(form[propName.replace(defArrIdx,0)] != null)
                {
                  bUseSimpleArr = true;
                }
              }
            }
            
            if(bUseSimpleArr)
            {
              sCondition = sCondition.replace(propArr[j], "form[\"" + propName + "\"].value");
            }else{
              sCondition = sCondition.replace(propArr[j], "form" + propArr[j]);
            }
            
            bUseSimpleArr = false;
          }
          
          
          if ( sIteratorIndex1 == null || sIteratorIndex1 == '' )
          {
            if(sCondition.indexOf(defArrIdx) >=0 ){
              fields = checkArrayObject(form, sCondition, addIndexerForArrayObj(oValidateCondition[x][0]), defArrIdx, oValidateCondition[x][1], fields);
            }else{
              var bValidate = eval ( sCondition );
              if ( !bValidate )
              {
                if ( i == 0 )
                {
                  focusField = field;
                }
                fields[i++] = oValidateCondition[x][1];
              } 
            }
          }
          else
          {
            fields = checkArrayObject(form, sCondition, oValidateCondition[x][0], sIteratorIndex1,oValidateCondition[x][1], fields);
          }
        }
        
        if (fields.length > 0) 
        {
          //if ( focusField.type != null )
          //{
          //	focusField.focus();
          //}
          alert(fields.join('\n'));
          return false;
        }
        else
        {
          return true;
        }
      }
      
      function checkArrayObject(form, sCondition, arrObj, indexer, errMsg, fields){
        
        var i = 0;
        var focusField = null;
        //var fields = new Array();
        
        var iLoopIndex = 0;
        var sField;
        var sCondition1;
        var bValidate;
            
        while ( (sField = form[replaceString ( arrObj, indexer, iLoopIndex )]) != null )
        {
          sCondition1 = replaceString ( sCondition, indexer, iLoopIndex ); 
          bValidate = eval ( sCondition1 );
              
          if ( !bValidate )
          {
            if ( i == 0 )
            {
              focusField = form[sField];
            }
            //fields[i++] = replaceString ( errMsg, indexer, iLoopIndex+1);
            fields[fields.length] = replaceString ( errMsg, indexer, iLoopIndex+1);
          }
            
          iLoopIndex++;
        } 
        
        return fields;
      }
function validateCurrency(form) {
        
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        var formName = form.getAttributeNode("name"); 
        
        oCurrency = eval('new ' + formName.value + '_currency()');
        
        for (x in oCurrency) {
        
        var field = form[oCurrency[x][0]];
        var value = field.value;
        
        var currencyPattern = oCurrency[x][2]("currencyPatternStrict");
        
        
        if ( ( field.type == 'hidden' || field.type == 'text' || field.type == 'textarea') &&
        (value.length > 0) && (currencyPattern.length > 0) && (field.disabled == false) )
        {
        
        var COM_FIG = "0";
        var OPT_FIG = "#";
        
        
        // FORMAT : <prefix>00000[<decimal point ( . )>0000]optional
        
        // REGULAR EXPRESSION
        // ====================
        // ^() --> matches at the start of the string
        // \d  --> matches digits 0-9
        // {n} --> get the no. of character
        // $   --> matches at the end of the string
        // [c] --> matches all characters in the bracket
        // +   --> repeat the previous item one or more
        // ?   --> previous item is optional
        
        var regExp = "^";
        
        for ( index = 0; index < currencyPattern.length; index++) {
        currencyChar = currencyPattern.charAt(index);
        if (currencyChar == COM_FIG)
        regExp = regExp + "\\d";
        else if (currencyChar == OPT_FIG) {
        
        nextCurChar = currencyPattern.charAt(index + 1);
        if (nextCurChar != COM_FIG && nextCurChar != OPT_FIG) {
        regExp = regExp + "(\\d[" + nextCurChar + "])?";
        index = index + 1;
        }
        else 
        regExp = regExp + "(\\d)?";
        
        } else 
        regExp = regExp + "[" + currencyChar + "]";
        }
                
        regExp = regExp + "$";
        
        dateRegexp = new RegExp(regExp);
        
        var matched = dateRegexp.exec(value);
        
        if(matched == null) {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oCurrency[x][1];
        bValid =  false;
        }
        }
        }
        
        if (fields.length > 0) {
        focusField.focus();
        alert(fields.join('\n'));
        }
        
        return bValid;
        }
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
   /*$RCSfile: validateDate.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid date.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateDate(form) {
       var bValid = true;
       var focusField = null;
       var i = 0;
       var fields = new Array();
 
       var oDate = eval('new ' + jcv_retrieveFormName(form) +  '_DateValidations()');

       for (var x in oDate) {
            if (!jcv_verifyArrayElement(x, oDate[x])) {
                continue;
            }
           var field = form[oDate[x][0]];
           if (!jcv_isFieldPresent(field)) {
             continue;
           }
           var value = field.value;
           var isStrict = true;
           var datePattern = oDate[x][2]("datePatternStrict");
           // try loose pattern
           if (datePattern == null) {
               datePattern = oDate[x][2]("datePattern");
               isStrict = false;
           }    
           if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea') &&
               (value.length > 0) && (datePattern.length > 0)) {
                 var MONTH = "MM";
                 var DAY = "dd";
                 var YEAR = "yyyy";
                 var orderMonth = datePattern.indexOf(MONTH);
                 var orderDay = datePattern.indexOf(DAY);
                 var orderYear = datePattern.indexOf(YEAR);
                 if ((orderDay < orderYear && orderDay > orderMonth)) {
                     var iDelim1 = orderMonth + MONTH.length;
                     var iDelim2 = orderDay + DAY.length;
                     var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
                     var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
                     if (iDelim1 == orderDay && iDelim2 == orderYear) {
                        dateRegexp = isStrict 
                             ? new RegExp("^(\\d{2})(\\d{2})(\\d{4})$") 
                             : new RegExp("^(\\d{1,2})(\\d{1,2})(\\d{4})$");
                     } else if (iDelim1 == orderDay) {
                        dateRegexp = isStrict 
                             ? new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$")
                             : new RegExp("^(\\d{1,2})(\\d{1,2})[" + delim2 + "](\\d{4})$");
                     } else if (iDelim2 == orderYear) {
                        dateRegexp = isStrict
                             ? new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$")
                             : new RegExp("^(\\d{1,2})[" + delim1 + "](\\d{1,2})(\\d{4})$");
                     } else {
                        dateRegexp = isStrict
                             ? new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$")
                             : new RegExp("^(\\d{1,2})[" + delim1 + "](\\d{1,2})[" + delim2 + "](\\d{4})$");
                     }
                     var matched = dateRegexp.exec(value);
                     if(matched != null) {
                        if (!jcv_isValidDate(matched[2], matched[1], matched[3])) {
                           if (i == 0) {
                               focusField = field;
                           }
                           fields[i++] = oDate[x][1];
                           bValid =  false;
                        }
                     } else {
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oDate[x][1];
                        bValid =  false;
                     }
                 } else if ((orderMonth < orderYear && orderMonth > orderDay)) {
                     var iDelim1 = orderDay + DAY.length;
                     var iDelim2 = orderMonth + MONTH.length;
                     var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
                     var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
                     if (iDelim1 == orderMonth && iDelim2 == orderYear) {
                         dateRegexp = isStrict 
                            ? new RegExp("^(\\d{2})(\\d{2})(\\d{4})$")
                            : new RegExp("^(\\d{1,2})(\\d{1,2})(\\d{4})$");
                     } else if (iDelim1 == orderMonth) {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$")
                            : new RegExp("^(\\d{1,2})(\\d{1,2})[" + delim2 + "](\\d{4})$");
                     } else if (iDelim2 == orderYear) {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$")
                            : new RegExp("^(\\d{1,2})[" + delim1 + "](\\d{1,2})(\\d{4})$");
                     } else {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$")
                            : new RegExp("^(\\d{1,2})[" + delim1 + "](\\d{1,2})[" + delim2 + "](\\d{4})$");
                     }
                     var matched = dateRegexp.exec(value);
                     if(matched != null) {
                         if (!jcv_isValidDate(matched[1], matched[2], matched[3])) {
                             if (i == 0) {
                                  focusField = field;
                             }
                             fields[i++] = oDate[x][1];
                             bValid =  false;
                          }
                     } else {
                         if (i == 0) {
                             focusField = field;
                         }
                         fields[i++] = oDate[x][1];
                         bValid =  false;
                     }
                 } else if ((orderMonth > orderYear && orderMonth < orderDay)) {
                     var iDelim1 = orderYear + YEAR.length;
                     var iDelim2 = orderMonth + MONTH.length;
                     var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
                     var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
                     if (iDelim1 == orderMonth && iDelim2 == orderDay) {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{4})(\\d{2})(\\d{2})$")
                            : new RegExp("^(\\d{4})(\\d{1,2})(\\d{1,2})$");
                     } else if (iDelim1 == orderMonth) {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})$")
                            : new RegExp("^(\\d{4})(\\d{1,2})[" + delim2 + "](\\d{1,2})$");
                     } else if (iDelim2 == orderDay) {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})$")
                            : new RegExp("^(\\d{4})[" + delim1 + "](\\d{1,2})(\\d{1,2})$");
                     } else {
                         dateRegexp = isStrict
                            ? new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})$")
                            : new RegExp("^(\\d{4})[" + delim1 + "](\\d{1,2})[" + delim2 + "](\\d{1,2})$");
                     }
                     var matched = dateRegexp.exec(value);
                     if(matched != null) {
                         if (!jcv_isValidDate(matched[3], matched[2], matched[1])) {
                             if (i == 0) {
                                 focusField = field;
                             }
                             fields[i++] = oDate[x][1];
                             bValid =  false;
                         }
                     } else {
                          if (i == 0) {
                              focusField = field;
                          }
                          fields[i++] = oDate[x][1];
                          bValid =  false;
                     }
                 } else {
                     if (i == 0) {
                         focusField = field;
                     }
                     fields[i++] = oDate[x][1];
                     bValid =  false;
                 }
          }
       }
       if (fields.length > 0) {
          jcv_handleErrors(fields, focusField);
       }
       return bValid;
    }
    
    function jcv_isValidDate(day, month, year) {
	    if (month < 1 || month > 12) {
            return false;
        }
        if (day < 1 || day > 31) {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) &&
            (day == 31)) {
            return false;
        }
        if (month == 2) {
            var leap = (year % 4 == 0 &&
               (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day == 29 && !leap)) {
                return false;
            }
        }
        return true;
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateCreditCard.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid creditcard number based on Luhn checksum.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateCreditCard(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oCreditCard = eval('new ' + jcv_retrieveFormName(form) +  '_creditCard()');

        for (var x in oCreditCard) {
            if (!jcv_verifyArrayElement(x, oCreditCard[x])) {
                continue;
            }
            var field = form[oCreditCard[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }
            if ((field.type == 'text' ||
                 field.type == 'textarea') &&
                (field.value.length > 0)) {
                if (!jcv_luhnCheck(field.value)) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oCreditCard[x][1];
                    bValid = false;
                }
            }
        }
        if (fields.length > 0) {
            jcv_handleErrors(fields, focusField);
        }
        return bValid;
    }

    /**
     * Checks whether a given credit card number has a valid Luhn checksum.
     * This allows you to spot most randomly made-up or garbled credit card numbers immediately.
     * Reference: http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html
     */
    function jcv_luhnCheck(cardNumber) {
        if (jcv_isLuhnNum(cardNumber)) {
            var no_digit = cardNumber.length;
            var oddoeven = no_digit & 1;
            var sum = 0;
            for (var count = 0; count < no_digit; count++) {
                var digit = parseInt(cardNumber.charAt(count));
                if (!((count & 1) ^ oddoeven)) {
                    digit *= 2;
                    if (digit > 9) digit -= 9;
                };
                sum += digit;
            };
            if (sum == 0) return false;
            if (sum % 10 == 0) return true;
        };
        return false;
    }

    function jcv_isLuhnNum(argvalue) {
        argvalue = argvalue.toString();
        if (argvalue.length == 0) {
            return false;
        }
        for (var n = 0; n < argvalue.length; n++) {
            if ((argvalue.substring(n, n+1) < "0") ||
                (argvalue.substring(n,n+1) > "9")) {
                return false;
            }
        }
        return true;
    }

function validateSingaporeNRICNumber(form) {
      var formName = form.getAttributeNode("name");
      oSGNricNumber = eval('new ' + formName.value + '_sgNricCheck()');
      var sErrorMessage = '';
    
      for (x in oSGNricNumber) {
      var field = form[oSGNricNumber[x][0]];
      var sInput = field.value;

      var sFieldName = oSGNricNumber[x][0];
      var sItemErrorMessage = oSGNricNumber[x][1];
      if (!sItemErrorMessage || sItemErrorMessage == '') {
        sItemErrorMessage = 'Invalid NRIC (field: ' + sFieldName + ')';
      }
      
      var sPrefix = sInput.substring (0, 1);
      sPrefix = sPrefix.toUpperCase();
      var iComputation = 0;
      
      if (sPrefix == "T" || sPrefix == "G")
      iComputation += 4;
      
      var nric1 = Number(sInput.substring(1, 2));
      var nric2 = Number(sInput.substring(2, 3));
      var nric3 = Number(sInput.substring(3, 4));
      var nric4 = Number(sInput.substring(4, 5));
      var nric5 = Number(sInput.substring(5, 6));
      var nric6 = Number(sInput.substring(6, 7));
      var nric7 = Number(sInput.substring(7, 8));
      var nricalpha = sInput.substring(8,9);
      
      var iDigit;
      iDigit = nric1;
      
      iComputation += (2 * iDigit);
      iDigit = nric2;
      iComputation += (7 * iDigit);
      iDigit = nric3;
      iComputation += (6 * iDigit);
      iDigit = nric4;
      iComputation += (5 * iDigit);
      iDigit = nric5;
      iComputation += (4 * iDigit);
      iDigit = nric6;
      iComputation += (3 * iDigit);
      iDigit = nric7;
      iComputation += (2 * iDigit);
      
      // Get the official reference
      var sCheckChar = "";
      iComputation = iComputation % 11;
      
      if (sPrefix == "S" || sPrefix == "T") {
      if (iComputation == 0) sCheckChar = "J";
      if (iComputation == 1) sCheckChar = "Z";
      if (iComputation == 2) sCheckChar = "I";
      if (iComputation == 3) sCheckChar = "H";
      if (iComputation == 4) sCheckChar = "G";
      if (iComputation == 5) sCheckChar = "F";
      if (iComputation == 6) sCheckChar = "E";
      if (iComputation == 7) sCheckChar = "D";
      if (iComputation == 8) sCheckChar = "C";
      if (iComputation == 9) sCheckChar = "B";
      if (iComputation == 10) sCheckChar = "A";
      } 
      else if (sPrefix == "F" || sPrefix == "G") {
      if (iComputation == 0) sCheckChar = "X";
      if (iComputation == 1) sCheckChar = "W";
      if (iComputation == 2) sCheckChar = "U";
      if (iComputation == 3) sCheckChar = "T";
      if (iComputation == 4) sCheckChar = "R";
      if (iComputation == 5) sCheckChar = "Q";
      if (iComputation == 6) sCheckChar = "P";
      if (iComputation == 7) sCheckChar = "N";
      if (iComputation == 8) sCheckChar = "M";
      if (iComputation == 9) sCheckChar = "L";
      if (iComputation == 10) sCheckChar = "K";
      }

      var booleanVar = false;
      
      if (sCheckChar != "" && nricalpha != "") {
        if (sCheckChar == nricalpha) {
          booleanVar = true;
        }
      }
      
      if (booleanVar != true) {
        if (sErrorMessage != '') sErrorMessage += '\n';
        sErrorMessage += sItemErrorMessage;
      }
      
      }

      if (sErrorMessage != '') {
        alert (sErrorMessage);
        return false;
      } else return true;
      
      }
function validateDateTime(form) {
        
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        var formName = form.getAttributeNode("name"); 
        
        oDate = eval('new ' + formName.value + '_dateTime()');
        
        for (x in oDate) {
        
        var field = form[oDate[x][0]];
        var value = field.value;
        
        var datePattern = oDate[x][2]("datePatternStrict");
        // try loose pattern
        if (datePattern == null)
        datePattern = oDate[x][2]("datePattern");
        
        if ( ( field.type == 'hidden' || field.type == 'text' || field.type == 'textarea') &&
        (value.length > 0) && (datePattern.length > 0) && (field.disabled == false) )
        {
        
        var MONTH = "MM";
        var DAY = "dd";
        var YEAR = "yyyy";
        var HOUR = "HH";
        var MINUTE = "mm";
        
        var orderMonth = datePattern.indexOf(MONTH);
        var orderDay = datePattern.indexOf(DAY);
        var orderYear = datePattern.indexOf(YEAR);
        
        // ** Assume time will always be at the back and in the format of HH:mm
        var orderHour = datePattern.indexOf(HOUR);
        var orderMinute = datePattern.indexOf(MINUTE);
        
        // MM dd yyyy
        if ((orderDay < orderYear && orderDay > orderMonth)) {
        
        var iDelim1 = orderMonth + MONTH.length; // 1st delimiter index
        var iDelim2 = orderDay + DAY.length; // 2nd delimiter index
        var iDelim3 = orderYear + YEAR.length; // 3rd delimiter index - b/w date n time
        var iDelim4 = orderHour + HOUR.length; // 4th delimiter index - b/w hour n minute
        
        var delim1 = datePattern.substring(iDelim1, iDelim1 + 1); 
        var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
        var delim3 = datePattern.substring(iDelim3, iDelim3 + 1);
        var delim4 = datePattern.substring(iDelim4, iDelim4 + 1);
        
        
        // REGULAR EXPRESSION
        // ====================
        // ^() --> matches at the start of the string
        // \d  --> matches digits 0-9
        // {n} --> get the no. of character
        // $   --> matches at the end of the string
        // [c] --> matches all characters in the bracket
        
        if (iDelim1 == orderDay && iDelim2 == orderYear && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 010120051200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderDay && iDelim2 == orderYear && iDelim3 == orderHour) { 
        
        // Eg, 0101200512:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderDay && iDelim2 == orderYear && iDelim4 == orderMinute) { 
        
        // Eg, 01012005 1200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderDay && iDelim2 == orderYear) { 
        
        // Eg, 01012005 12:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderDay && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 0101/20051200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderDay && iDelim3 == orderHour) { 
        
        // Eg, 0101/200512:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderDay && iDelim4 == orderMinute) { 
        
        // Eg, 0101/2005 1200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderDay) { 
        
        // Eg, 0101/2005 12:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim2 == orderYear && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 01/0120051200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim2 == orderYear && iDelim3 == orderHour) { 
        
        // Eg, 01/01200512:00
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim2 == orderYear && iDelim4 == orderMinute) { 
        
        // Eg, 01/012005 1200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim2 == orderYear) { 
        
        // Eg, 01/0120051200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 01/01/20051200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim3 == orderHour) { 
        
        // Eg, 01/01/200512:00
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim4 == orderMinute) { 
        
        // Eg, 01/01/2005 1200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else { 
        
        // Eg, 01/01/2005 12:00
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        }
        
        var matched = dateRegexp.exec(value);
        if(matched != null) {
        if (!isValidDateTime(matched[2], matched[1], matched[3], matched[4], matched[5])) {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        } else {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        
        } else if ((orderMonth < orderYear && orderMonth > orderDay)) { // dd MM yyyy
        
        var iDelim1 = orderDay + DAY.length;
        var iDelim2 = orderMonth + MONTH.length;
        var iDelim3 = orderYear + YEAR.length; // 3rd delimiter index - b/w date n time
        var iDelim4 = orderHour + HOUR.length; // 4th delimiter index - b/w hour n minute
        
        var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
        var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
        var delim3 = datePattern.substring(iDelim3, iDelim3 + 1);
        var delim4 = datePattern.substring(iDelim4, iDelim4 + 1);
        
        if (iDelim1 == orderMonth && iDelim2 == orderYear && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 010120051200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim2 == orderYear && iDelim3 == orderHour) { 
        
        // Eg, 0101200512:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim2 == orderYear && iDelim4 == orderMinute) { 
        
        // Eg, 01012005 1200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim2 == orderYear) { 
        
        // Eg, 01012005 12:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 0101/20051200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim3 == orderHour) { 
        
        // Eg, 0101/200512:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim4 == orderMinute) { 
        
        // Eg, 0101/2005 1200
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth) { 
        
        // Eg, 0101/2005 12:00
        dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim2 == orderYear && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 01/0120051200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim2 == orderYear && iDelim3 == orderHour) { 
        
        // Eg, 01/01200512:00
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim2 == orderYear && iDelim4 == orderMinute) { 
        
        // Eg, 01/012005 1200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim2 == orderYear) { 
        
        // Eg, 01/0120051200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 01/01/20051200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})(\\d{2})(\\d{2})$");
        
        } else if (iDelim3 == orderHour) { 
        
        // Eg, 01/01/200512:00
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim4 == orderMinute) { 
        
        // Eg, 01/01/2005 1200
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else { 
        
        // Eg, 01/01/2005 12:00
        dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        }
        
        var matched = dateRegexp.exec(value);
        if(matched != null) {
        if (!isValidDateTime(matched[1], matched[2], matched[3], matched[4], matched[5])) {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        } else {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        
        } else if ((orderMonth > orderYear && orderMonth < orderDay)) { // yyyy MM dd
        
        var iDelim1 = orderYear + YEAR.length;
        var iDelim2 = orderMonth + MONTH.length;
        var iDelim3 = orderYear + YEAR.length; // 3rd delimiter index - b/w date n time
        var iDelim4 = orderHour + HOUR.length; // 4th delimiter index - b/w hour n minute
        
        var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
        var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
        var delim3 = datePattern.substring(iDelim3, iDelim3 + 1);
        var delim4 = datePattern.substring(iDelim4, iDelim4 + 1);
        
        if (iDelim1 == orderMonth && iDelim2 == orderDay && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 010120051200
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim2 == orderDay && iDelim3 == orderHour) { 
        
        // Eg, 0101200512:00
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim2 == orderDay && iDelim4 == orderMinute) { 
        
        // Eg, 01012005 1200
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim2 == orderDay) { 
        
        // Eg, 01012005 12:00
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 0101/20051200
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})(\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim3 == orderHour) { 
        
        // Eg, 0101/200512:00
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim1 == orderMonth && iDelim4 == orderMinute) { 
        
        // Eg, 0101/2005 1200
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim1 == orderMonth) { 
        
        // Eg, 0101/2005 12:00
        dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim2 == orderDay && iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 01/0120051200
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})(\\d{2})(\\d{2})$");
        
        } else if (iDelim2 == orderDay && iDelim3 == orderHour) { 
        
        // Eg, 01/01200512:00
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim2 == orderDay && iDelim4 == orderMinute) { 
        
        // Eg, 01/012005 1200
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else if (iDelim2 == orderDay) { 
        
        // Eg, 01/0120051200
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim3 == orderHour && iDelim4 == orderMinute) { 
        
        // Eg, 01/01/20051200
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})(\\d{2})(\\d{2})$");
        
        } else if (iDelim3 == orderHour) { 
        
        // Eg, 01/01/200512:00
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})(\\d{2})[" + delim4 + "](\\d{2})$");
        
        } else if (iDelim4 == orderMinute) { 
        
        // Eg, 01/01/2005 1200
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})[" + delim3 + "](\\d{2})(\\d{2})$");
        
        } else { 
        
        // Eg, 01/01/2005 12:00
        dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})[" + delim3 + "](\\d{2})[" + delim4 + "](\\d{2})$");
        
        }
        
        
        var matched = dateRegexp.exec(value);
        if(matched != null) {
        if (!isValidDateTime(matched[3], matched[2], matched[1], matched[4], matched[5])) {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        } else {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        
        } else {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oDate[x][1];
        bValid =  false;
        }
        }
        }
        
        if (fields.length > 0) {
        focusField.focus();
        alert(fields.join('\n'));
        }
        return bValid;
        }
        
        function isValidDateTime(day, month, year, hour, minute) {
        
        if (month < 1 || month > 12) {
        return false;
        }
        
        if (day < 1 || day > 31) {
        return false;
        }
        
        if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31)) {
        return false;
        }
        
        if (month == 2) {
        var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day == 29 && !leap)) {
        return false;
        }
        }
        
        if (minute < 0 || minute > 59) {
        return false;
        }
        
        if (hour < 0 || hour > 23) {
        return false;
        }
        
        
        return true;
        }
function validateTwoFields(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        var formName = form.getAttributeNode("name");
        oTwoFields = eval('new ' + formName.value + '_twofields()');
        for (x in oTwoFields) {
        var field = form[oTwoFields[x][0]];
        var secondField = form[oTwoFields[x][2]("secondProperty")];
        
        if (field.type == 'text' ||
        field.type == 'textarea' ||
        field.type == 'select-one' ||
        field.type == 'radio' ||
        field.type == 'password') {
        
        var value;
        var secondValue;
        // get field's value
        if (field.type == "select-one") {
        var si = field.selectedIndex;
        value = field.options[si].value;
        secondValue = secondField.options[si].value;
        } else {
        value = field.value;
        secondValue = secondField.value;
        }
        
        if (value != secondValue) {
        
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oTwoFields[x][1];
        bValid = false;
        }
        }
        }
        
        if (fields.length > 0) {
        focusField.focus();
        alert(fields.join('\n'));
        }
        
        return bValid;
        }
function isFilled(field)
      {
      var isValid = false;
      if ((field.type == 'hidden' ||
      field.type == 'text' ||
      field.type == 'textarea' ||
      field.type == 'file' ||
      field.type == 'checkbox' ||
      field.type == 'select-one' ||
      field.type == 'password') &&
      field.disabled == false) 
      {
      var value = '';
      // get field's value
      if (field.type == "select-one") 
      {
      var si = field.selectedIndex;
      if (si >= 0) 
      {
      value = field.options[si].value;
      }
      } 
      else if (field.type == 'checkbox') 
      {
      if (field.checked) 
      {
      value = field.value;
      }
      } 
      else 
      {
      value = field.value;
      }
      
      if (trim(value).length > 0) 
      {
      isValid = true;
      }
      } 
      else if (field.type == "select-multiple") 
      { 
      var numOptions = field.options.length;
      lastSelected=-1;
      for(loop=numOptions-1;loop>=0;loop--) 
      {
      if(field.options[loop].selected) 
      {
      isValid = true;
      value = field.options[loop].value;
      break;
      }
      }
      } 
      else if ((field.length > 0) && (field[0].type == 'radio' || field[0].type == 'checkbox')) 
      {
      for (loop=0;loop < field.length;loop++) 
      {
      if (field[loop].checked) 
      {
      isValid=true;
      break; // only one needs to be checked
      }
      }
      }
      return isValid;
      }
      
      function validateRequireOne(form) 
      {
      var i = 0;
      var fields = new Array();
      var formName = form.getAttributeNode("name");
      oRequireOne = eval('new ' + formName.value + '_requireOne()');
      for ( x in oRequireOne ) 
      {
      var field = form[oRequireOne[x][0]];
      var sFieldList = oRequireOne[x][2]("baseFieldList");
      
      if ( isFilled(field) )
      {
      continue;
      }
      
      if ( sFieldList == null || sFieldList == '' )
      {
      field.focus();
      alert(oRequireOne[x][1]);
      return false;
      }
      
      var asFields = new Array();
      if ( sFieldList.indexOf(',') >= 0 )
      {
      asFields = sFieldList.split(',');
      }
      else
      {
      asFields[0] = sFieldList;
      }
      
      var field1;
      var bFound = false;
      for ( j=0; j<asFields.length && !bFound; j++ )
      {
      field1 = form[asFields[j]];
      if ( isFilled(field1) )
      {
      bFound = true;
      }
      }
      if ( bFound )
      {
      continue;
      }
      
      fields[i++] = oRequireOne[x][1];
      }
      
      if (fields.length > 0) 
      {
      alert(fields.join('\n'));
      return false;
      }
      else
      {
      return true;
      }
      }
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateFloat.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid float.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateFloat(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oFloat = eval('new ' + jcv_retrieveFormName(form) +  '_FloatValidations()');
        for (var x in oFloat) {
            if (!jcv_verifyArrayElement(x, oFloat[x])) {
                continue;
            }
        	var field = form[oFloat[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }
        	
            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'select-one' ||
                field.type == 'radio')) {
        
            	var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else {
                    value = field.value;
                }
        
                if (value.length > 0) {
                    // remove '.' before checking digits
                    var tempArray = value.split('.');
                    //Strip off leading '0'
                    var zeroIndex = 0;
                    var joinedString= tempArray.join('');
                    while (joinedString.charAt(zeroIndex) == '0') {
                        zeroIndex++;
                    }
                    var noZeroString = joinedString.substring(zeroIndex,joinedString.length);

                    if (!jcv_isAllDigits(noZeroString) || tempArray.length > 2) {
                        bValid = false;
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oFloat[x][1];

                    } else {
	                var iValue = parseFloat(value);
	                if (isNaN(iValue)) {
	                    if (i == 0) {
	                        focusField = field;
	                    }
	                    fields[i++] = oFloat[x][1];
	                    bValid = false;
	                }
                    }
                }
            }
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return bValid;
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateMinLength.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * A field is considered valid if greater than the specified minimum.
    * Fields are not checked if they are disabled.
    *
    *  Caution: Using validateMinLength() on a password field in a 
    *  login page gives unnecessary information away to hackers. While it only slightly
    *  weakens security, we suggest using it only when modifying a password.
    * @param form The form validation is taking place on.
    */
    function validateMinLength(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();

        var oMinLength = eval('new ' + jcv_retrieveFormName(form) +  '_minlength()');

        for (var x in oMinLength) {
            if (!jcv_verifyArrayElement(x, oMinLength[x])) {
                continue;
            }
            var field = form[oMinLength[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'password' ||
                field.type == 'textarea')) {

                /* Adjust length for carriage returns - see Bug 37962 */
                var lineEndLength = oMinLength[x][2]("lineEndLength");
                var adjustAmount = 0;
                if (lineEndLength) {
                    var rCount = 0;
                    var nCount = 0;
                    var crPos = 0;
                    while (crPos < field.value.length) {
                        var currChar = field.value.charAt(crPos);
                        if (currChar == '\r') {
                            rCount++;
                        }
                        if (currChar == '\n') {
                            nCount++;
                        }
                        crPos++;
                    }
                    var endLength = parseInt(lineEndLength);
                    adjustAmount = (nCount * endLength) - (rCount + nCount);
                }

                var iMin = parseInt(oMinLength[x][2]("minlength"));
                if ((trim(field.value).length > 0) && ((field.value.length + adjustAmount) < iMin)) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oMinLength[x][1];
                    isValid = false;
                }
            }
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return isValid;
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
   /*$RCSfile: validateFloatRange.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are in a valid float range.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateFloatRange(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        
        var oRange = eval('new ' + jcv_retrieveFormName(form) +  '_floatRange()');
        for (var x in oRange) {
            if (!jcv_verifyArrayElement(x, oRange[x])) {
                continue;
            }
            var field = form[oRange[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }
            
            if ((field.type == 'hidden' ||
                field.type == 'text' || field.type == 'textarea') &&
                (field.value.length > 0)) {
        
                var fMin = parseFloat(oRange[x][2]("min"));
                var fMax = parseFloat(oRange[x][2]("max"));
                var fValue = parseFloat(field.value);
                if (!(fValue >= fMin && fValue <= fMax)) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oRange[x][1];
                    isValid = false;
                }
            }
        }
        if (fields.length > 0) {
            jcv_handleErrors(fields, focusField);
        }
        return isValid;
    }

function validateExistCSSAttack(form) {
            // regular expression definition
            // check whether string contains "<...>,<img src=''>,</...>" html tag
            var cssRegExpStr = "^(((\%3C)|<)(\s)*((\%2F)|\/)*[^>]+((\%3E)|>))|(((\%3C)|<)(\s)*((\%69)|i|(\%49))((\%6D)|m|(\%4D))((\%67)|g|(\%47))[^\n]+((\%3E)|>))$";
            
            var isValid = true;
                var focusField = null;
                var i = 0;
                var fields = new Array();
             var formName = form.getAttributeNode("name"); 

                oCSSMasked = eval('new ' + formName.value + '_cssCheck()');     
                for (x in oCSSMasked) {
                    var field = form[oCSSMasked[x][0]];
              var value = field.value;

                    if ((field.type == 'hidden' ||
                        field.type == 'text' ||
                         field.type == 'textarea' ||
                 field.type == 'file') &&
                         (field.value.length > 0) &&
                         field.disabled == false) {

                if(oCSSMasked[x][2]("cssRegExp") != null){
                  cssRegExpStr = oCSSMasked[x][2]("cssRegExp");
                }
                
                cssRegExp = new RegExp(cssRegExpStr);
                        if (jcv_matchPattern(field.value, cssRegExp)) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oCSSMasked[x][1];
                            isValid = false;
                        }
                    }
                }
        
                if (fields.length > 0) {
                   focusField.focus();
                   alert(fields.join('\n'));
                }

                return isValid;
          }
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
  /*$RCSfile: validateUtilities.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
  /**
  * This is a place holder for common utilities used across the javascript validation
  *
  **/

  /**
   * Retreive the name of the form
   * @param form The form validation is taking place on.
   */
  function jcv_retrieveFormName(form) {

      // Please refer to Bugs 31534, 35127, 35294, 37315 & 38159
      // for the history of the following code

      var formName;

      if (form.getAttributeNode) {
          if (form.getAttributeNode("id") && form.getAttributeNode("id").value) {
              formName = form.getAttributeNode("id").value;
          } else {
              formName = form.getAttributeNode("name").value;
          }
      } else if (form.getAttribute) {
          if (form.getAttribute("id")) {
              formName = form.getAttribute("id");
          } else {
              formName = form.attributes["name"];
          }
      } else {
          if (form.id) {
              formName = form.id;
          } else {
              formName = form.name;
          }
      }

      return formName;

  }  

  /**
   * Handle error messages.
   * @param messages Array of error messages.
   * @param focusField Field to set focus on.
   */
  function jcv_handleErrors(messages, focusField) {
      if (focusField && focusField != null) {
          var doFocus = true;
          if (focusField.disabled || focusField.type == 'hidden') {
              doFocus = false;
          }
          if (doFocus && 
              focusField.style && 
              focusField.style.visibility &&
              focusField.style.visibility == 'hidden') {
              doFocus = false;
          }
          if (doFocus) {
              focusField.focus();
          }
      }
      alert(messages.join('\n'));
  }

  /**
   * Checks that the array element is a valid
   * Commons Validator element and not one inserted by
   * other JavaScript libraries (for example the
   * prototype library inserts an "extends" into
   * all objects, including Arrays).
   * @param name The element name.
   * @param value The element value.
   */
  function jcv_verifyArrayElement(name, element) {
      if (element && element.length && element.length == 3) {
          return true;
      } else {
          return false;
      }
  }

  /**
   * Checks whether the field is present on the form.
   * @param field The form field.
   */
  function jcv_isFieldPresent(field) {
      var fieldPresent = true;
      if (field == null || (typeof field == 'undefined')) {
          fieldPresent = false;
      } else {
          if (field.disabled) {
              fieldPresent = false;
          }
      }
      return fieldPresent;
  }

  /**
   * Check a value only contains valid numeric digits
   * @param argvalue The value to check.
   */
  function jcv_isAllDigits(argvalue) {
      argvalue = argvalue.toString();
      var validChars = "0123456789";
      var startFrom = 0;
      if (argvalue.substring(0, 2) == "0x") {
         validChars = "0123456789abcdefABCDEF";
         startFrom = 2;
      } else if (argvalue.charAt(0) == "0") {
         validChars = "01234567";
         startFrom = 1;
      } else if (argvalue.charAt(0) == "-") {
          startFrom = 1;
      }

      for (var n = startFrom; n < argvalue.length; n++) {
          if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
      }
      return true;
  }

  /**
   * Check a value only contains valid decimal digits
   * @param argvalue The value to check.
   */
  function jcv_isDecimalDigits(argvalue) {
      argvalue = argvalue.toString();
      var validChars = "0123456789";

      var startFrom = 0;
      if (argvalue.charAt(0) == "-") {
          startFrom = 1;
      }

      for (var n = startFrom; n < argvalue.length; n++) {
          if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
      }
      return true;
  }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateMaxLength.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * A field is considered valid if less than the specified maximum.
    * Fields are not checked if they are disabled.
    *
    *  Caution: Using validateMaxLength() on a password field in a 
    *  login page gives unnecessary information away to hackers. While it only slightly
    *  weakens security, we suggest using it only when modifying a password.
    * @param form The form validation is taking place on.
    */
    function validateMaxLength(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oMaxLength = eval('new ' + jcv_retrieveFormName(form) +  '_maxlength()');        
        for (var x in oMaxLength) {
            if (!jcv_verifyArrayElement(x, oMaxLength[x])) {
                continue;
            }
            var field = form[oMaxLength[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'password' ||
                field.type == 'textarea')) {

                /* Adjust length for carriage returns - see Bug 37962 */
                var lineEndLength = oMaxLength[x][2]("lineEndLength");
                var adjustAmount = 0;
                if (lineEndLength) {
                    var rCount = 0;
                    var nCount = 0;
                    var crPos = 0;
                    while (crPos < field.value.length) {
                        var currChar = field.value.charAt(crPos);
                        if (currChar == '\r') {
                            rCount++;
                        }
                        if (currChar == '\n') {
                            nCount++;
                        }
                        crPos++;
                    }
                    var endLength = parseInt(lineEndLength);
                    adjustAmount = (nCount * endLength) - (rCount + nCount);
                }

                var iMax = parseInt(oMaxLength[x][2]("maxlength"));
                if ((field.value.length + adjustAmount)  > iMax) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oMaxLength[x][1];
                    isValid = false;
                }
            }
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return isValid;
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateRequired.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    *  Check to see if fields must contain a value.
    * Fields are not checked if they are disabled.
    *
    * @param form The form validation is taking place on.
    */

    function validateRequired(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();

        var oRequired = eval('new ' + jcv_retrieveFormName(form) +  '_required()');

        for (var x in oRequired) {
            if (!jcv_verifyArrayElement(x, oRequired[x])) {
                continue;
            }
            var field = form[oRequired[x][0]];

            if (!jcv_isFieldPresent(field)) {
                fields[i++] = oRequired[x][1];
                isValid=false;
            } else if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'file' ||
                field.type == 'radio' ||
                field.type == 'checkbox' ||
                field.type == 'select-one' ||
                field.type == 'password')) {

                var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else if (field.type == 'radio' || field.type == 'checkbox') {
                    if (field.checked) {
                        value = field.value;
                    }
                } else {
                    value = field.value;
                }

                if (trim(value).length == 0) {

                    if ((i == 0) && (field.type != 'hidden')) {
                        focusField = field;
                    }
                    fields[i++] = oRequired[x][1];
                    isValid = false;
                }
            } else if (field.type == "select-multiple") { 
                var numOptions = field.options.length;
                lastSelected=-1;
                for(loop=numOptions-1;loop>=0;loop--) {
                    if(field.options[loop].selected) {
                        lastSelected = loop;
                        value = field.options[loop].value;
                        break;
                    }
                }
                if(lastSelected < 0 || trim(value).length == 0) {
                    if(i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oRequired[x][1];
                    isValid=false;
                }
            } else if ((field.length > 0) && (field[0].type == 'radio' || field[0].type == 'checkbox')) {
                isChecked=-1;
                for (loop=0;loop < field.length;loop++) {
                    if (field[loop].checked) {
                        isChecked=loop;
                        break; // only one needs to be checked
                    }
                }
                if (isChecked < 0) {
                    if (i == 0) {
                        focusField = field[0];
                    }
                    fields[i++] = oRequired[x][1];
                    isValid=false;
                }
            }   
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return isValid;
    }
    
    // Trim whitespace from left and right sides of s.
    function trim(s) {
        return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateInteger.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid integer.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateInteger(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oInteger = eval('new ' + jcv_retrieveFormName(form) +  '_IntegerValidations()');
        for (var x in oInteger) {
            if (!jcv_verifyArrayElement(x, oInteger[x])) {
                continue;
            }
            var field = form[oInteger[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'select-one' ||
                field.type == 'radio')) {

                var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else {
                    value = field.value;
                }

                if (value.length > 0) {

                    if (!jcv_isDecimalDigits(value)) {
                        bValid = false;
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oInteger[x][1];

                    } else {
                        var iValue = parseInt(value, 10);
                        if (isNaN(iValue) || !(iValue >= -2147483648 && iValue <= 2147483647)) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oInteger[x][1];
                            bValid = false;
                       }
                   }
               }
            }
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return bValid;
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateIntRange.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields is in a valid integer range.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateIntRange(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oRange = eval('new ' + jcv_retrieveFormName(form) +  '_intRange()');        
        for (var x in oRange) {
            if (!jcv_verifyArrayElement(x, oRange[x])) {
                continue;
            }
            var field = form[oRange[x][0]];
            if (jcv_isFieldPresent(field)) {
                var value = '';
                if (field.type == 'hidden' ||
                    field.type == 'text' || field.type == 'textarea' ||
                    field.type == 'radio' ) {
                    value = field.value;
                }
                if (field.type == 'select-one') {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                }
                if (value.length > 0) {
                    var iMin = parseInt(oRange[x][2]("min"));
                    var iMax = parseInt(oRange[x][2]("max"));
                    var iValue = parseInt(value, 10);
                    if (!(iValue >= iMin && iValue <= iMax)) {
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oRange[x][1];
                        isValid = false;
                    }
                }
            }
        }
        if (fields.length > 0) {
            jcv_handleErrors(fields, focusField);
        }
        return isValid;
    }

function validateRadioButtonRequired(form) 
      {
      var isValid=false;
      var field;
      var iCount = 0;
      var formName = form.getAttributeNode("name");
      oRadioButtonRequired = eval('new ' + formName.value + '_radioButtonRequired()');
      for (x in oRadioButtonRequired) 
      {
      field = form[oRadioButtonRequired[x][0]];
      iCount = field.length;
      if ( iCount > 1 )
      {
      for ( var i = 0; i < iCount; i++ )
      {
      if ( field[i].checked )
      {
      isValid = true;
      break;
      }
      }
      }
      else if ( iCount == null || iCount == 0 )
      {
      if ( field.checked )
      {
      isValid = true;
      }
      }
      }
      if ( !isValid )
      {
      alert ( oRadioButtonRequired[x][1] + "\n" );
      }
      return isValid;
      }
function validateExchangeRate(form) {
        
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        var formName = form.getAttributeNode("name"); 
        
        oExchangeRate = eval('new ' + formName.value + '_exchangeRate()');
        
        for (x in oExchangeRate) {
        
        var field = form[oExchangeRate[x][0]];
        var value = field.value;
        
        var currencyPattern = oExchangeRate[x][2]("exchangeRatePatternStrict");
        
        
        if ( ( field.type == 'hidden' || field.type == 'text' || field.type == 'textarea') &&
        (value.length > 0) && (currencyPattern.length > 0) && (field.disabled == false) )
        {
        
        var COM_FIG = "0";
        var OPT_FIG = "#";
        
        // REGULAR EXPRESSION
        // ====================
        // ^() --> matches at the start of the string
        // \d  --> matches digits 0-9
        // {n} --> get the no. of character
        // $   --> matches at the end of the string
        // [c] --> matches all characters in the bracket
        // +   --> repeat the previous item one or more
        // ?   --> previous item is optional
        
        var regExp = "^";
        
        for ( index = 0; index < currencyPattern.length; index++) {
        currencyChar = currencyPattern.charAt(index);
        if (currencyChar == COM_FIG)
        regExp = regExp + "\\d";
        else if (currencyChar == OPT_FIG) {
        
        nextCurChar = currencyPattern.charAt(index + 1);
        if (nextCurChar != COM_FIG && nextCurChar != OPT_FIG) {
        regExp = regExp + "(\\d[" + nextCurChar + "])?";
        index = index + 1;
        }
        else 
        regExp = regExp + "(\\d)?";
        
        } else 
        regExp = regExp + "[" + currencyChar + "]";
        }
        
        regExp = regExp + "$";
        
        dateRegexp = new RegExp(regExp);
        
        var matched = dateRegexp.exec(value);
        
        if(matched == null) {
        if (i == 0) {
        focusField = field;
        }
        fields[i++] = oExchangeRate[x][1];
        bValid =  false;
        }
        }
        }
        
        if (fields.length > 0) {
        focusField.focus();
        alert(fields.join('\n'));
        }
        
        return bValid;
        }
function validateCheckboxRequired(form) 
      {
      var isValid=false;
      var field;
      var iCount = 0;
      var formName = form.getAttributeNode("name");
      oCheckboxRequired = eval('new ' + formName.value + '_checkboxRequired()');
      
      for (x in oCheckboxRequired) 
      {
      field = form[oCheckboxRequired[x][0]];
      if ( field == null )
      {
      alert ( oCheckboxRequired[x][1] + "\n" );
      return false;
      }
      iCount = field.length;
      if ( iCount > 1 )
      {
      for ( var i = 0; i < iCount; i++ )
      {
      if ( field[i] != null)
      {
      if ( field[i].checked )
      {
      isValid = true;
      break;
      }
      }
      }
      }
      else if ( iCount == null || iCount == 0 )
      {
      if ( field.checked )
      {
      isValid = true;
      }
      }
      }
      if ( !isValid )
      {
      alert ( oCheckboxRequired[x][1] + "\n" );
      }
      return isValid;
      }
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateShort.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    *  Check to see if fields are a valid short.
    * Fields are not checked if they are disabled.
    *
    * @param form The form validation is taking place on.
    */
    function validateShort(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oShort = eval('new ' + jcv_retrieveFormName(form) +  '_ShortValidations()');

        for (var x in oShort) {
            if (!jcv_verifyArrayElement(x, oShort[x])) {
                continue;
            }
            var field = form[oShort[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                field.type == 'textarea' ||
                field.type == 'select-one' ||
                field.type == 'radio')) {

                var value = '';
                // get field's value
                if (field.type == "select-one") {
                    var si = field.selectedIndex;
                    if (si >= 0) {
                        value = field.options[si].value;
                    }
                } else {
                    value = field.value;
                }

                if (value.length > 0) {
                    if (!jcv_isDecimalDigits(value)) {
                        bValid = false;
                        if (i == 0) {
                            focusField = field;
                        }
                        fields[i++] = oShort[x][1];

                    } else {

                        var iValue = parseInt(value, 10);
                        if (isNaN(iValue) || !(iValue >= -32768 && iValue <= 32767)) {
                            if (i == 0) {
                                focusField = field;
                            }
                            fields[i++] = oShort[x][1];
                            bValid = false;
                        }
                   }
               }
            }
        }
        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return bValid;
    }

function validateFromToDate(form) 
      {
      var bValid = true;
      
      var focusField = null;
      var i = 0;
      var fields = new Array();
      var formName = form.getAttributeNode("name");
      
      var abResult = new Array();
      var iLoop = 0;
      
      oFromToDate = eval('new ' + formName.value + '_fromToDate()');
      
      var fromMatched = null;
      var toMatched = null;
      
      for ( x in oFromToDate ) 
      {
      var field = form[oFromToDate[x][0]];
      var value = field.value;
      
      var datePattern = oFromToDate[x][2]("datePatternStrict");
      if (datePattern == null)
      datePattern = oFromToDate[x][2]("datePattern");
      
      if ( ( field.type == 'hidden' || field.type == 'text' || field.type == 'textarea') &&
      (value.length > 0) && (datePattern.length > 0) && field.disabled == false) 
      {
      
      toMatched = validDate(value, datePattern);
      
      if (toMatched == null) {
      
      if (i == 0) {
      focusField = field;
      }
      
      fields[i++] = oFromToDate[x][1];
      bValid = false;
      
      } else {
      
      var sFromDateField = oFromToDate[x][2]("fromDate");
      var sFromDate = form.elements[sFromDateField].value;
      
      fromMatched = validDate(sFromDate, datePattern);
      
      if (fromMatched == null) {
      
      if (i == 0) {
      focusField = field;
      }
      
      fields[i++] = oFromToDate[x][1];
      bValid = false;
      
      } else {
      
      var bAllowsSameDay = oFromToDate[x][2]("allowsSameDay");
      
      if ( sFromDate == null || value == null || sFromDate == "" || value == "" )
      {
      continue;
      }
      
      if ( i == 0 )
      {                       
      focusField = field;
      }
      
      
      var sFromToken = fromMatched[3] + fromMatched[2] + fromMatched[1];
      
      var sToToken = toMatched[3] + toMatched[2] + toMatched[1];
      
      if ( bAllowsSameDay == null || bAllowsSameDay == "true" ) 
      {
      if ( parseInt(sFromToken) <= parseInt(sToToken) )
      {
      bValid = true;
      }
      else
      {
      fields[i++] = oFromToDate[x][1];
      bValid = false;
      }
      }
      else 
      {
      if ( parseInt(sFromToken) < parseInt(sToToken) )
      {
      bValid = true;
      }
      else
      {
      fields[i++] = oFromToDate[x][1];
      bValid = false;
      }
      }
      
      iLoop++;
      }
      }
      }
      }
      
      if (fields.length > 0) 
      {
      focusField.focus();
      alert(fields.join('\n'));
      }
      
      return bValid;
      }
      
      function validDate(value, datePattern) {
      
      var MONTH = "MM";
      var DAY = "dd";
      var YEAR = "yyyy";
      
      var orderMonth = datePattern.indexOf(MONTH);
      var orderDay = datePattern.indexOf(DAY);
      var orderYear = datePattern.indexOf(YEAR);
      
      var matched = null;
      
      if ((orderDay < orderYear && orderDay > orderMonth)) {
      
      var iDelim1 = orderMonth + MONTH.length;
      var iDelim2 = orderDay + DAY.length;
      
      var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
      var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
      
      if (iDelim1 == orderDay && iDelim2 == orderYear) {
      dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$");
      } else if (iDelim1 == orderDay) {
      dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$");
      } else if (iDelim2 == orderYear) {
      dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$");
      } else {
      dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
      }
      
      matched = dateRegexp.exec(value);
      
      if(matched != null) {
      
      if (!isValidDate(matched[2], matched[1], matched[3])) {
      matched = null;
      }
      
      }
      
      } else if ((orderMonth < orderYear && orderMonth > orderDay)) {
      
      var iDelim1 = orderDay + DAY.length;
      var iDelim2 = orderMonth + MONTH.length;
      
      var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
      var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
      
      if (iDelim1 == orderMonth && iDelim2 == orderYear) {
      dateRegexp = new RegExp("^(\\d{2})(\\d{2})(\\d{4})$");
      } else if (iDelim1 == orderMonth) {
      dateRegexp = new RegExp("^(\\d{2})(\\d{2})[" + delim2 + "](\\d{4})$");
      } else if (iDelim2 == orderYear) {
      dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})(\\d{4})$");
      } else {
      dateRegexp = new RegExp("^(\\d{2})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{4})$");
      }
      
      var matched = dateRegexp.exec(value);
      if(matched != null) {
      
      if (!isValidDate(matched[1], matched[2], matched[3])) {
      matched = null;
      }
      
      } 
      
      } else if ((orderMonth > orderYear && orderMonth < orderDay)) {
      
      var iDelim1 = orderYear + YEAR.length;
      var iDelim2 = orderMonth + MONTH.length;
      
      var delim1 = datePattern.substring(iDelim1, iDelim1 + 1);
      var delim2 = datePattern.substring(iDelim2, iDelim2 + 1);
      
      if (iDelim1 == orderMonth && iDelim2 == orderDay) {
      dateRegexp = new RegExp("^(\\d{4})(\\d{2})(\\d{2})$");
      } else if (iDelim1 == orderMonth) {
      dateRegexp = new RegExp("^(\\d{4})(\\d{2})[" + delim2 + "](\\d{2})$");
      } else if (iDelim2 == orderDay) {
      dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})(\\d{2})$");
      } else {
      dateRegexp = new RegExp("^(\\d{4})[" + delim1 + "](\\d{2})[" + delim2 + "](\\d{2})$");
      }
      
      var matched = dateRegexp.exec(value);
      
      if(matched != null) {
      
      if (!isValidDate(matched[3], matched[2], matched[1])) {
      
      matched = null;
      }
      
      } 
      
      } else {
      matched = null;
      }
      
      return matched;
      }
      
      function isValidDate(day, month, year) {
      if (month < 1 || month > 12) {
      return false;
      }
      if (day < 1 || day > 31) {
      return false;
      }
      if ((month == 4 || month == 6 || month == 9 || month == 11) &&
      (day == 31)) {
      return false;
      }
      if (month == 2) {
      var leap = (year % 4 == 0 &&
      (year % 100 != 0 || year % 400 == 0));
      if (day>29 || (day == 29 && !leap)) {
      return false;
      }
      }
      return true;
      }
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateEmail.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid email address.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateEmail(form) {
        var bValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();

        var oEmail = eval('new ' + jcv_retrieveFormName(form) +  '_email()');

        for (var x in oEmail) {
            if (!jcv_verifyArrayElement(x, oEmail[x])) {
                continue;
            }
            var field = form[oEmail[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }
            if ((field.type == 'hidden' || 
                 field.type == 'text' ||
                 field.type == 'textarea') &&
                (field.value.length > 0)) {
                if (!jcv_checkEmail(field.value)) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oEmail[x][1];
                    bValid = false;
                }
            }
        }
        if (fields.length > 0) {
            jcv_handleErrors(fields, focusField);
        }
        return bValid;
    }

    /**
     * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
     * http://javascript.internet.com
     */
    function jcv_checkEmail(emailStr) {
        if (emailStr.length == 0) {
            return true;
        }
        // TLD checking turned off by default
        var checkTLD=0;
        var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
        var emailPat=/^(.+)@(.+)$/;
        var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
        var validChars="\[^\\s" + specialChars + "\]";
        var quotedUser="(\"[^\"]*\")";
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
        var atom=validChars + '+';
        var word="(" + atom + "|" + quotedUser + ")";
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
        var matchArray=emailStr.match(emailPat);
        if (matchArray==null) {
            return false;
        }
        var user=matchArray[1];
        var domain=matchArray[2];
        for (i=0; i<user.length; i++) {
            if (user.charCodeAt(i)>127) {
                return false;
            }
        }
        for (i=0; i<domain.length; i++) {
            if (domain.charCodeAt(i)>127) {
                return false;
            }
        }
        if (user.match(userPat)==null) {
            return false;
        }
        var IPArray=domain.match(ipDomainPat);
        if (IPArray!=null) {
            for (var i=1;i<=4;i++) {
                if (IPArray[i]>255) {
                    return false;
                }
            }
            return true;
        }
        var atomPat=new RegExp("^" + atom + "$");
        var domArr=domain.split(".");
        var len=domArr.length;
        for (i=0;i<len;i++) {
            if (domArr[i].search(atomPat)==-1) {
                return false;
            }
        }
        if (checkTLD && domArr[domArr.length-1].length!=2 && 
            domArr[domArr.length-1].search(knownDomsPat)==-1) {
            return false;
        }
        if (len<2) {
            return false;
        }
        return true;
    }

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
    /*$RCSfile: validateMask.js,v $ $Rev: 478676 $ $Date: 2006-11-23 21:35:44 +0000 (Thu, 23 Nov 2006) $ */
    /**
    * Check to see if fields are a valid using a regular expression.
    * Fields are not checked if they are disabled.
    * @param form The form validation is taking place on.
    */
    function validateMask(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
 
        var oMasked = eval('new ' + jcv_retrieveFormName(form) +  '_mask()');      
        for (var x in oMasked) {
            if (!jcv_verifyArrayElement(x, oMasked[x])) {
                continue;
            }
            var field = form[oMasked[x][0]];
            if (!jcv_isFieldPresent(field)) {
              continue;
            }

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                 field.type == 'textarea' ||
				 field.type == 'file') &&
                 (field.value.length > 0)) {

                if (!jcv_matchPattern(field.value, oMasked[x][2]("mask"))) {
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oMasked[x][1];
                    isValid = false;
                }
            }
        }

        if (fields.length > 0) {
           jcv_handleErrors(fields, focusField);
        }
        return isValid;
    }

    function jcv_matchPattern(value, mask) {
       return mask.exec(value);
    }


