/*
* ============================================================================
* nnjsValidateForm.js - Universal HTML Form Validator JScript
*
* Copyright (C) 2002 by N-Networks. All Rights Reserved.
* http://www.dpsp-yes.com
*
* Modification history:
*
* Feb 21, 2002  v1.0.0   V.Bulatov     - initially created
* Mar 05, 2002  v1.0.1   V.Zakharychev - cleaned up some names and texts
* Aug 26, 2002  v1.1.0   V.Bulatov     - IsDate attribute added
*
*
* Sample usage: 
* <form .... OnSubmit="return nnjsValidateForm( this );">
*   or 
* <form .... OnSubmit="return nnjsValidateForm( this ) && SomeUsersFunction(....);">
* ....
* <input type=text IsRegex="^[abc]$"  .... IsRegexTxt="bla bla" >
* <input type=text IsNotEmpty IsDigit .... IsNotEmptyTxt="bla1" IsDigitTxt="bla2">
* <input type=text IsNotEmpty IsEmail .... CommonTxt="bla bla">
* ....
* </form>
*
* See nnjsValidateForm.html for more details
* ============================================================================
*
* *** IMPORTANT! THIS SCRIPT IS SHAREWARE ***
* If you find this script useful, please register at http://www.dpsp-yes.com
* to use it legally. This script can not be used for any purpose except
* evaluation without registration. Registered users are entitled to free email
* support and updates. They will also benefit from clean conscience and good
* karma.
*
* Any modifications to the script are expressly prohibited. Please do not
* change or remove any copyright notices and other text and/or functionality.
* If you miss some functionality, please email us and we will gladly implement
* it in the script (provided that you are registered user, that is).
* ============================================================================
*/

// you are free to change the values below to fit your style

var nnjsCommonTxt     = "Entered value is not valid";   // Generic error message
var nnjsIsNotEmptyTxt = "The field cannot be empty";    // Field is empty
var nnjsIsDigitTxt    = "Numeric value expected";       // Field is not numeric
var nnjsIsDateTxt     = "Valid date value expected";    // Field is not a valid date
var nnjsIsEmailTxt    = "Valid email address expected"; // Field is not a valid email address
var nnjsIsRegexTxt    = "Entered value did not match given restrictions"; // Field didn't match regexp

var nnjsIsDateFmt     = "MM/DD/YYYY"; // Default format for date value

// DO NOT CHANGE ANYTHING PAST THIS LINE!

function nnjsValidateFocus( item ){
// bring focus to the form item if it can have focus
  if( item.type == "text" ||
      item.type == "password" || 
      item.type == "checkbox" || 
      item.type == "select-one" ||
      item.type == "select-multiple" ||
      item.type == "button" ||
      item.type == "textarea"
    ){
    item.focus();
    return true;
    }
  else{
    return false;
    }
  }

function nnjsValidateAlert( msg1, msg2, msg3, msg4 ){
// alert user using appropriate message
  if( msg1 ){ alert( msg1 ); }
  else{
    if( msg2 ){ alert( msg2 ); }
    else{
      if( msg3 ){ alert( msg3 ); }
      else{
        if( msg4 ){ alert( msg4 ); }
        }
      }
    }
  return true;
  }

function nnjsValidateDate( year, month, day ){
// validate date
// the full year, for example, 1976 (and not 76 for 1976)
// the month as an integer between 0 and 11 (January to December)
// the date as an integer between 1 and 31

  if( day < 1 || day > 31 || month < 0 || month > 11 || year < 0 )
    return false;
  if( day == 31 && ( month == 3 || month == 5 || month == 8 || month == 10 ))
    return false;
  if( day > 29 && month == 1 )
    return false;
  if( day == 29 && month == 1 && (( year % 4 != 0 ) || ( year % 100 == 0 && year % 400 != 0 )))
    return false;

  return true;
  }

function nnjsValidateForm( fm ){
// main validation routine
  for( i = 0; i < fm.elements.length; i++ ){
    var item = fm.elements.item(i);
    if( item && !item.disabled ){

      // check for NotEmpty
      if( item.getAttribute('IsNotEmpty') != null ){
        // check if item is filled/selected
        if(( item.value == "" ) ||
           ( item.type == "checkbox" && !item.checked )
          ){
          nnjsValidateFocus( item );
          nnjsValidateAlert(
            item.IsNotEmptyTxt,
            item.CommonTxt,
            nnjsIsNotEmptyTxt,
            nnjsCommonTxt
            );
          return false;
          }
        }

      // check if item is numeric
      if( item.getAttribute('IsDigit') != null ){
        var rex = /^-?\d+(\.\d+)?$/;
        if( item.IsDigit != "" ){
          if( item.IsDigit.search( /^-?([=z]?\d)|(z?\*)(\.([=z]?\d)|(z?\*))$/ ) == 0 ){
            //replacing rex with appropriate regexp
            }
          }
        if( item.value != "" && item.value.search( rex ) == -1 ){
          nnjsValidateFocus( item );
          nnjsValidateAlert(
            item.IsDigitTxt,
            item.CommonTxt,
            nnjsIsDigitTxt,
            nnjsCommonTxt
            );
          return false;
          }
        }

      // check if item is date
      if( item.getAttribute('IsDate') != null ){
        var fmt = item.getAttribute('IsDate');
        if( fmt == "" ) fmt = nnjsIsDateFmt;

        // determine order of date fields
        var mpos = fmt.search( /MM/ );
        var dpos = fmt.search( /DD/ );
        var ypos = fmt.search( /YYYY/ );
        var midx = 1, didx = 1, yidx = 1;
        if( mpos > dpos ) midx++;
        if( mpos > ypos ) midx++;
        if( dpos > mpos ) didx++;
        if( dpos > ypos ) didx++;
        if( ypos > mpos ) yidx++;
        if( ypos > dpos ) yidx++;

        // build regular expression according to date format
        fmt = fmt.replace( /\\/g, "\\\\" );
        fmt = fmt.replace( /MM/g, "(\\d{1,2})" );
        fmt = fmt.replace( /DD/g, "(\\d{1,2})" );
        fmt = fmt.replace( /YYYY/g, "(\\d{4})" );
        var rex = new RegExp( '^' + fmt + '$' );
        var sep = item.value.match( rex );

        if( item.value != "" &&
          ( sep == null || !nnjsValidateDate( sep[yidx], sep[midx]-1, sep[didx] ))){
          nnjsValidateFocus( item );
          nnjsValidateAlert(
            item.IsDateTxt,
            item.CommonTxt,
            nnjsIsDateTxt,
            nnjsCommonTxt
            );
          return false;
          }
        }

      // check if item is valid email address
      if( item.getAttribute('IsEmail') != null ){
        if( item.value != "" && item.value.search(/^[\w-\._]+@[\w-_]+\.[\w\.-_]+$/) == -1 ){
          nnjsValidateFocus( item );
          nnjsValidateAlert(
            item.IsEmailTxt,
            item.CommonTxt,
            nnjsIsEmailTxt,
            nnjsCommonTxt
            );
          return false;
          }
        }

      // check if item matches the regular expression
      if( item.getAttribute('IsRegex') != null ){
        var rex = new RegExp( item.IsRegex );
        if( item.value != "" && item.value.search( rex ) == -1 ){
          nnjsValidateFocus( item );
          nnjsValidateAlert(
            item.IsRegexTxt,
            item.CommonTxt,
            nnjsIsRegexTxt,
            nnjsCommonTxt
            );
          return false;
          }
        }

      } // if
    } // for

  // all checks passed - form data is ok.
  return true;
  } // nnjsValidateForm
