/* maxforms.js */ /* ************************************************************ * * Scriptname : form.js * Version : 1.0.0 * ************************************************************ * * Copyright ? 2003 by Max.nl * ************************************************************ * Description : * * JS for form actions * ************************************************************ * * Date : 10-11-2003 * Author : Erik Verhoofstad * Description : Initial Release * * Date : 09-11-2004 * Author : J. Beemsterboer / BeMat Automatisering bv * Description : GetRadioValue / SetRadioValue * ************************************************************ */ function swapSelected() { for( var i = 0; i < this.elements.length; i++ ) { if( this.elements[i].type == 'checkbox' && !this.elements[i].disabled ) { this.elements[i].checked = !this.elements[i].checked; } } } function selectAll() { for( var i = 0; i < this.elements.length; i++ ) { if( this.elements[i].type == 'checkbox' && !this.elements[i].disabled ) { this.elements[i].checked = true; } } } function doBulkDelete() { if( confirm( 'Hiermee verwijdert u de geselecteerde items.' ) ) { this.sub.value = 'dobulkdelete'; return true; } else { return false; } } /* DRAFT function toggleRowSelect( checkbox, oddOrEven ) { var row = checkbox.parentNode.parentNode; if( checkbox.checked ) { row.className = oddOrEven + ' selected'; } else { row.className = oddOrEven } } */ /********************************************************************** * SWAPBOX FUNCTIONS * **********************************************************************/ function setListBox( leftList ) { var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; if( leftList.length == 0 ) { option = document.createElement("option"); option.text = '-------- Geen --------'; option.value = '0'; leftList.add( option, where ); } } function moveOptionLeft( leftList, rightList ) { //var where = document.all ? -1 : null; var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; // remove '---Geen---' if present if( leftList.options[0].value == 0 ) { leftList.remove(0); } for( var i = 0; i < rightList.length; i++ ) { if( rightList.options[i].selected && ( rightList.options[i].value != 0 ) ) { option = document.createElement("option"); option.text = rightList.options[i].text; option.value = rightList.options[i].value; option.className = 'newSelected'; leftList.add( option, where ); rightList.remove( i ); i--; } } rightList.selectedIndex = -1; rightList.blur(); } function moveOptionRight( leftList, rightList ) { var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; for( var i = 0; i < leftList.length; i++ ) { if( leftList.options[i].selected && ( leftList.options[i].value != 0 ) ) { option = document.createElement("option"); option.text = leftList.options[i].text; option.value = leftList.options[i].value; option.className = 'newDeselected'; rightList.add( option, where ); leftList.remove( i ); i--; } } leftList.selectedIndex = -1; leftList.blur(); setListBox( leftList ); } function initSwapBox( leftList, rightList ) { var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; for( var i=0; i < leftList.options.length; i++ ) { if( ( !leftList.options[i].selected ) && ( leftList.options[i].value != 0 ) ) { option = document.createElement("option"); option.text = leftList.options[i].text; option.value = leftList.options[i].value; rightList.add( option, where ); leftList.remove( i ); i--; } } leftList.selectedIndex = -1; setListBox( leftList ); } /********************************************************************** * FUNCTIONS * **********************************************************************/ function jumpNext( currentElement, nextElement ) { if( currentElement.value.length == currentElement.maxLength ) { //nextElement.focus(); } } /********************************************************************** * FILE INPUT FUNCTIONS * **********************************************************************/ function toggleFileInput( element, hide ) { if( hide ) { element.style.display = 'none'; } else { element.style.display = 'block'; } } /********************************************************************** * IMAGE INPUT FUNCTIONS * **********************************************************************/ function toggleImageInput( element, hide ) { element.disabled = hide; var img = document.getElementById( element.name + '_PREVIEW'); if( hide ) { img.src = '#'; } // else { // img.style.display = 'block'; // } setImage( img ) ; } function showPreview( element ) { var img = document.getElementById( element.name + '_PREVIEW'); img.style.display = 'block'; img.src = 'file://' + element.value; img.width = 100; } function setImage( img ) { if( img.src == '#' ) { img.style.display = 'none'; img.width = 10; } else { img.style.display = 'block'; } } function setValue( obj, value ) { try { //just in case the object doesn't support .value switch( obj.type ) { //SINGLE SELECT case 'select-one': var opts = obj.options; var l = opts.length; for( var x=0; x 0 ) { ++nrChecked; } } } ); return ( nrChecked >= 1 ); }, showErrors: function() { this.inputFields.each( function( inputField ) { var labels = $$('label[for=' + inputField.id + ']'); this.setErrorLabels( labels ); }.bind( this ) ); } } ); SelectOnlyOneValidator = Class.create( Validator, { initialize: function( $super, inputFields, options ) { $super(); this.inputFields = inputFields.collect( function( inputField ){ return $(inputField); }); this.options = Object.extend( { useLabel: null }, options||{}); }, isModified: function() { return( this.inputFields.any( function(field) { return( typeof field.modified == 'boolean' && field.modified ); } ) ); }, isValid: function( $super, setLabels ) { var nrChecked = 0; this.inputFields.each( function( inputField ) { // alert( inputField.name + ', ' + inputField.checked + ', ' + inputField.value ); if( typeof( inputField.checked != 'undefined' )) { if( inputField.checked ) { ++nrChecked; } } else if ( typeof inputField.value != 'undefined' ) { if( inputField.value.length > 0 ) { ++nrChecked; } } } ); return ( nrChecked == 1 ); }, showErrors: function() { if( this.options.useLabel ) { this.setErrorLabels( [this.options.useLabel] ); } else { this.inputFields.each( function( inputField ) { var labels = $$('label[for=' + inputField.id + ']'); this.setErrorLabels( labels ); }.bind( this ) ); } } } ); SingleFieldValidator = Class.create( Validator, { initialize: function( $super, inputField ) { $super(); if( !inputField ) { console.log( 'Unknown field: ' + inputField ); } this.input = $( inputField ); /* if( ! this.input ) { alert( 'Veld niet gevonden: ' + inputField ); } */ }, isModified: function() { return( typeof this.input.modified == 'boolean' && this.input.modified ); }, showErrors: function() { var labels = $$('label[for=' + this.input.id + ']'); this.setErrorLabels( labels ); } } ); NotEmptyValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setLabels ) { // Checkboxes that have not been checked if( this.input.type == 'checkbox' ) { return this.input.checked; } // Text fields and select boxes with an empty value if( $F( this.input ).replace( / /g, '' ) == '' ) { return false; } // All ok return true } } ); EmptyValidator = Class.create( NotEmptyValidator, { isValid: function( $super, setLabels ) { // Inverse of not-empty return ! $super( setLabels ); } } ); NumberValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels ) { return ( this.input.value.replace( / /g, '').match( /^[0-9]*$/ ) != null ); } } ); DateFormatValidator = Class.create( SingleFieldValidator, { // validates dates from 2000 and up Note it doesn't check for max days in a month nor does it check for leap years // Formats required to pass dd/mm/yyyy , dd-mm-yyyy isValid: function( $super, setlabels ) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } return ( value.match( /^(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)\d\d$/ ) != null ); } } ); IpAdresValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels ) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } // TODO: IPv6 address validation return ( value.match( /^(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}/ ) != null ); } } ); IpRangeValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels ) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } // TODO: IPv6 address validation return ( ( value.match( /^(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}-(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}$/ ) != null ) || ( value.match( /^(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])){3}\/([1-2][0-9]|3[02])$/ ) != null ) ); } } ); DomainNameValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels ) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } // TODO: better checks? return ( value.match( /^[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)+$/ ) != null ); } } ); DutchPostalCodeValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels ) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } return ( value.match( /^[0-9]{4} ?[A-Za-z]{2}$/ ) != null ); } } ); DutchPhoneNumberValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels ) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } value = value.replace( /-/g, '' ); //remove dashes if any exists value = value.replace( /^\+31/, '0' ); //remove landcode +31 if it exists value = value.replace( /^0031/, '0' ); //remove landcode 0031 if it exists return ( value.match( /^0\d{9}$/ ) != null ); } } ); AIsGreaterThanBValidator = Class.create( Validator, { initialize: function( $super, inputFields ) { $super(); this.inputFields = inputFields.collect( function( inputField ){ return $(inputField); }); }, isModified: function() { return( this.inputFields.any( function(field) { return( typeof field.modified == 'boolean' && field.modified ); } ) ); }, isValid: function( $super, setLabels ) { var checkResult = ( parseInt( this.inputFields[0].value ) >= parseInt( this.inputFields[1].value ) ); return ( checkResult ); }, showErrors: function() { this.inputFields.each( function( inputField ) { var labels = $$('label[for=' + inputField.id + ']'); this.setErrorLabels( labels ); }.bind( this ) ); } } ); EmailValidator = Class.create( SingleFieldValidator, { isValid: function( $super, setlabels, DeniedDomains) { var value = this.input.value.replace( / /g, ''); if( value == '' ) { return true; } var emailaddress = value.split( '@' ); if( emailaddress.length != 2 ) { return false; } return ( ( emailaddress[0].match( /^[a-z0-9_\+\.-]+$/ ) != null ) && ( emailaddress[1].match( /^[0-9a-z-]+(\.[0-9a-z-]+)+$/ ) != null ) ); } } );