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 nnjsNewTxt     = "Please choose a valid Newltter";   // Generic error message

var nnjsIsDateFmt     = "DD/MM/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 myNewsAlert()
{
	alert("Please choose a valid Newsletter");
	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 for IsNews
      if( item.getAttribute('IsNews') != null ){
         alert("test");
	 if(( itemt.selectedIndex == 0 ) 
	 {
	     nnjsValidateFocus( item );
	     myNewsAlert();
	     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 ){
        if( item.value != "" && item.value.search( /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*/ ) == -1){
          nnjsValidateFocus( item );
          nnjsValidateAlert(
            item.IsEmailTxt,
            item.CommonTxt,
            nnjsIsEmailTxt,
            nnjsCommonTxt
            );
          return false;
          }
        }

        //check for IsNews
//	if( item.getAttribute('IsNews') != null ){
//	alert("test");
	
	//(( itemt.selectedIndex == 0 )
	//
	 //njsValidateFocus( item );
	//                                                         myNewsAlert();
//	    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

