/*
* ============================================================================
* 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>
*/
// you are free to change the values below to fit your style

var nnjscommontxt     = "Entered value is not valid";   // Generic error message
var nnjsisnotemptytxt = "Пожалуйста заполните пустые поля!";    // 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    = "Пожалуйста введите корректный e-mail адрес!"; // 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
