//var test = true; function FormMgr(form, strServerMsg) { this.form = form; this.fields = new Array; if ((typeof (strServerMsg) == "string") && (strServerMsg != "") && (strServerMsg != "null")) { //alert (strServerMsg); displayErrorMessage("Eingabevalidierung fehlgeschlagen", strServerMsg); } } with (FormMgr) { prototype.getForm = function() {return this.form} prototype.getField = function(id) {return this.fields[id]} prototype.reset = function reset() { this.fields = new Array; } prototype.addField = function addField(id, name, type, bRequired) { return this.fields[id] = new FormField(this.form.elements[id], name, type, bRequired); } prototype.validate = function validate() { var aMsgs = null; var firstField = null; if (bDoNotValidate) return true; for (var x in this.fields) { //top.status = "validate field " + x; // alert ("validate field " + x); if (this.form.elements['goto'].value == 99) return true; var field = this.fields[x]; var strMsg = field.validate(); if (strMsg != null) //falls Fehler gefunden { if (aMsgs == null) //falls erste Fehlermeldung { aMsgs = new Array; firstField = field; } aMsgs[aMsgs.length] = "\n=> " + field.getName() + ": " + strMsg; } } if (aMsgs != null) { if ((typeof firstField.getObject().focus != 'undefined') && (firstField.getObject().type != 'hidden')) firstField.getObject().focus(); alert ("Bitte korrigieren Sie folgende " + (aMsgs.length > 1 ? "Eingaben" : "Eingabe") + ":\n" + aMsgs); } return aMsgs == null; } } FormField = function(obj, name, type, bRequired) { this.obj = obj; if (typeof obj == 'undefined') alert ('FormMgr.FormField(' + type + ' ' + name + '): invalid !');this.name = name;this.bRequired = bRequired;this.type = type; if ((typeof type == "string") && (type != "date")) //Manuel: verbessertes Prüfverfahren seit 8/03 { var xpr = tpsTypeXpr[type]; if (typeof xpr != 'undefined') this.addConstraint(xpr, tpsTypeXprError[type]); else if (xpr != "string") alert("Dear Programmer:\n type = '" + type + "' not defined for FormFields !"); } } with (FormField) { prototype.getName = function() {return this.name;} prototype.getObject = function() {return this.obj;} prototype.setRequired = function(bRequired) {this.bRequired = (bRequired == true)} prototype.addConstraint = function addConstraint(xpr, msg) { if (typeof this.regXprs == "undefined") { this.regXprs = new Array; this.errorMsgs = new Array; } this.regXprs[this.regXprs.length] = xpr; this.errorMsgs[this.errorMsgs.length] = msg; return this; } prototype.setMin = function setMin(minValue, msgError) { this.minValue = minValue; this.minError = msgError; } prototype.getMin = function getMin() {return this.minValue} prototype.setMax = function setMax(maxValue, bInclusive, msgError) { this.maxValue = maxValue; this.bInclusiveMaxValue = bInclusive; this.maxError = msgError; } prototype.getMax = function getMax() {return this.maxValue} prototype.validate = function validate() { if (typeof this.obj == "undefined") return null; //alert("FormMgr.Field[" + this.name + "].validate(): " + this.obj.type + " " + this.obj.name); var origValue, newValue; if (typeof this.obj.type == 'undefined') { if (this.obj.length > 0) //&& (this.obj[0].type == "choice")) { for (var n = 0; n < this.obj.length; n++) { //alert(this.obj[n].value + ".checked = " + this.obj[n].checked); if (this.obj[n].checked) { origValue = this.obj[n].value; break; } //else } } } else { switch (this.obj.type) { case "select-multiple": case "select-one": { if (this.obj.selectedIndex < 0) origValue = ""; else { origValue = this.obj.options[this.obj.selectedIndex].value; if ((origValue == null) || (origValue == "")) origValue = this.obj.options[this.obj.selectedIndex].text; } } break; default: origValue = this.obj.value; break; } } if (typeof test != 'undefined') alert ("Field.validate(" + this.obj.type + " " + this.name + "): original value = " + origValue); newValue = trim(origValue); /**/ if (typeof test != 'undefined') alert ("Field.validate(" + this.name + "): trimmed value = " + newValue); /**/ if ((newValue != origValue) && this.obj.value) { this.obj.value = newValue; if (typeof test != 'undefined') alert ("Field.validate(" + this.name + "): write back value = " + newValue); } if (newValue.length < 1) { if (typeof test != 'undefined') alert ("Field.validate(" + this.name + "): value length = " + newValue.length); return this.bRequired ? "Feld darf nicht leer sein" : null; } /**************************************************** Filter white spaces for phone and areacode and strip off german country code *****************************************************/ if (this.type == 'areacode' || this.type == 'phone') { newValue = this.obj.value.replace (/\s/g, ""); this.obj.value = newValue; } if (this.type == 'areacode') { if (this.obj.value.substring(0,4) == '0049') { this.obj.value = '0' + this.obj.value.substring(4,this.obj.value.length); newValue = this.obj.value; } if (this.obj.value.substring(0,3) == '+49') { this.obj.value = '0' + this.obj.value.substring(3,this.obj.value.length); newValue = this.obj.value; } } if (this.regXprs) { for (var nRule = 0; nRule < this.regXprs.length; nRule++) { if (typeof test != 'undefined') alert ("Field.validate(" + this.name + "): evaluating Xpr[" + nRule + "] = " + this.regXprs[nRule]); var rx = this.regXprs[nRule]; if (typeof rx == "string") //JavaScriptlet { alert ("JavaScriptlet =\n" + rx); //todo! } else //reg xpr { if (rx.test(newValue) == false) { if (typeof test != 'undefined') alert("Field.validate(" + this.name + "): '" + newValue + "' : " + rx.source + " -> \n" + rx.exec(newValue) + "\n" + rx.test(newValue) + "\n" + (/^\d+$/).test('123')); return this.errorMsgs[nRule]; } } } } if (this.type == 'date') { var df = new DateFormat(); var datum = df.parse(newValue); if (datum == null) return df.getMessage(); newValue = df.format(datum); //in bevorzugtes Format umwandeln if (this.obj.value) this.obj.value = newValue; } if (typeof this.minValue != 'undefined') { switch (this.type) { case 'date': { var dateFmt = new DateFormat(); var dateMin = typeof this.minValue == 'string' ? dateFmt.parse(this.minValue) : this.minValue; var dateMax = typeof this.maxValue == 'string' ? dateFmt.parse(this.maxValue) : this.maxValue; if (dateFmt.parse(newValue) < dateMin) return this.minError; if (dateFmt.parse(newValue) > dateMax) return this.maxError; } break; default: if (newValue < this.minValue) return this.minError; } } if (typeof this.maxValue != 'undefined') { switch (this.type) { case 'date': { var dateFmt = new DateFormat(); var dateMax = typeof (this.maxValue) == 'string' ? dateFmt.parse(this.maxValue) : this.maxValue; if ( (dateFmt.parse(newValue) > dateMax) || ! (this.bMaxInclusive == true) && (dateFmt.parse(newValue) >= dateMax) ) return this.maxError; } break; default: { if ( (newValue > this.maxValue) || ! (this.bMaxInclusive == true) && (newValue >= this.maxValue) ) return this.maxError; } } } return null; } } tpsTypeXpr = new Array; tpsTypeXprError = new Array; //tpsTypeScript = new Array; { tpsTypeXpr['unsigned'] = /^\d+$/; tpsTypeXpr['integer'] = /^[\+-]?\s*\d+$/; tpsTypeXpr['number'] = /^[\+-]?\s*\d+(\,\d+)?$/; // tpsTypeXpr['date'] = /^\d{1,2}\.\d{1,2}\.(\d\d){1,2}$/; tpsTypeXpr['text'] = /^\D*$/; tpsTypeXpr['string'] = /(.|\n)*$/; tpsTypeXpr['email'] = /^[^@]+@[^@]+\.[^@]+$/; //.NET1.1: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* tpsTypeXpr['simplephone'] = /^\d?(( |-|\/)*\d)+$/; tpsTypeXpr['phone'] = /^[1-9]\d{1,8}$/; tpsTypeXpr['areacode'] = /^0{1}[1-9]{1}[0-9]{1,3}$/; tpsTypeXpr['choice'] = /^.*$/; var now = new Date(); var today = now.getDate() + '.' + (now.getMonth() + 1) + '.' + now.getFullYear(); tpsTypeXprError['unsigned'] = "nur ganze Zahlen erlaubt (z.B.: 1704)"; tpsTypeXprError['integer'] = 'nur ganze Zahlen (evtl. mit Vorzeichen) erlaubt (z.B.: +11)'; tpsTypeXprError['number'] = 'nur Zahlen (Dezimalkomma darf nicht Punkt sein) erlaubt (z.B.: 3141,5)'; tpsTypeXprError['text'] = 'nur Buchstaben erlaubt (keine Ziffern oder Sonderzeichen)'; tpsTypeXprError['string'] = 'beliebige Eingabe zulässig'; tpsTypeXprError['email'] = 'ungültige E-Mail Adresse. Erlaubte Form: info@swb.de'; tpsTypeXprError['phone'] = 'ungültige Telefonnummer. Erlaubte Form: 567890'; tpsTypeXprError['areacode'] = 'ungültige Vorwahl. Erlaubte Form: 01234'; tpsTypeXprError['choice'] = '{Auswahlfehler}'; } function trim(str) { if ((typeof str != "string") || (str.length < 1)) return ""; var whiteSpace = " \r\n\f\t"; var nSOT, nEOT; /* leading white space identifizieren */ for (nSOT = 0;(nSOT < str.length) && (whiteSpace.indexOf(str.charAt(nSOT)) >= 0);nSOT++); for (nEOT = str.length; (--nEOT > nSOT) && (whiteSpace.indexOf(str.charAt(nEOT)) >= 0); ); return str.substring(nSOT, nEOT + 1); } function Session() { /************************************************************************** Browser identifizieren **************************************************************************/ if (navigator.userAgent.indexOf("MSIE") != -1) this.browserType = "IE"; else if (navigator.userAgent.indexOf("Netscape") != -1) this.browserType = "NS"; else this.browserType = "?"; /************************************************************************** **************************************************************************/ var lang = typeof navigator.language != "undefined" ? navigator.language : navigator.browserLanguage; this.browserLocale = ''; for (var n = 0; n < lang.length; n++) { this.browserLocale += n < 2 ? lang.charAt(n).toLowerCase() : n == 2 ? '_' : n < 5 ? lang.charAt(n).toUpperCase() : lang.charAt(n) ; } } /****************************************************************************** ******************************************************************************/ with (Session) { prototype.getBrowserType = function() {return this.browserType;} prototype.getBrowserLocale = function() {return this.browserLocale;} prototype.setLocale = function(locale) {this.locale = locale;} prototype.getLocale = function() {return typeof this.locale != "undefined" ? this.locale : this.getBrowserLocale();} } if (typeof tpsSession == "undefined") { tpsSession = new Session(); tpsSession.setLocale("de"); } function ResourceBundle() { this.bundles = new Array; } with (ResourceBundle) { prototype.getLocale = function() {return tpsSession.getLocale();} ; prototype.getString = function(id, locale) { var string, bundle; if (typeof locale == "undefined") locale = this.getLocale(); while ( (locale != null) && (typeof string == "undefined") ) { bundle = this.bundles[locale]; if (bundle != null) string = bundle[id]; //else alert ("ResourceBundle.getString(id='" + id + "', locale='" + locale + "')"); switch (locale.length) { case 0: case 1: locale = null; break; case 2: locale = ''; break; case 5: case 4: case 3: locale = locale.substr(0, 2); break; default: locale = locale.substr(0,5); break; } } //alert("ResourceBundle.getString(" + id + ", " + locale + ") = " + string); return string; } prototype.setString = function(id, string, locale) { if (typeof locale == "undefined") locale = ''; var bundle = this.bundles[locale]; if (typeof bundle == 'undefined') bundle = this.bundles[locale] = new Array; bundle[id] = string; } } function DateFormat() { //this.test = true; this.strMsg = null; this.locale = null; } DateFormat.STATE_OK = 0; //error states DateFormat.STATE_FORMAT_ERROR = -1; DateFormat.STATE_INVALID_DATE = -2; DateFormat.STATE_INVALID_MONTH = -3; //exceptions DateFormat.STATE_TYPE_ERROR = -4; DateFormat.STATE_ILLEGAL_STATE = -5; DateFormat.i18nTexts = new ResourceBundle(); with (DateFormat.i18nTexts) { setString('dateFormat', 'dd.MM.yyyy'); setString('dateFormat', 'dd/MM/yyyy', 'en'); setString('dateFormat', 'MM/dd/yyyy', 'en_US'); setString('dd.MM.yyyy', 'TT.MM.JJJJ'); setString('dd/MM/yyyy', 'dd/mm/yyyy', 'en'); setString('MM/dd/yyyy', 'mm/dd/yyyy', 'en_US'); setString('Message' + DateFormat.STATE_INVALID_DATE, 'Der Tag des Monats existiert nicht'); setString('Message' + DateFormat.STATE_INVALID_DATE, 'Day doesn\'t exist', 'en'); setString('Message' + DateFormat.STATE_INVALID_MONTH, 'Der Monat ist ungültig'); setString('Message' + DateFormat.STATE_INVALID_MONTH, 'Month doesn\'t exist', 'en'); setString('Message' + DateFormat.STATE_FORMAT_ERROR, 'Ungültiges Datumsformat'); setString('Message' + DateFormat.STATE_FORMAT_ERROR, 'Invalid date format', 'en'); setString('Message' + DateFormat.STATE_TYPE_ERROR, 'Das Datum muß als String übergeben werden'); setString('Message' + DateFormat.STATE_TYPE_ERROR, 'The date parameter must be entered as a string', 'en'); setString('Message' + DateFormat.STATE_ILLEGAL_STATE, 'Fehler in der Validierung des Datumsformats'); setString('Message' + DateFormat.STATE_ILLEGAL_STATE, 'Error in validation routine of date format', 'en'); } with (DateFormat) { prototype.setLocale = function(locale) {this.locale = typeof locale != "undefined" ? locale : null;} prototype.getLocale = function() {return this.locale != null ? this.locale : tpsSession.getLocale();} prototype.parse = function(strDate, locale, strDateFormat) { if (this.test) alert("DateFormat.parse(" + strDate + ", " + locale + ", " + strDateFormat + ")"); var nDateFormatPos = 0, nRepeatFormat = 0; var nParseState = DateFormat.STATE_OK; var nCharPos = 0; var nDay = 0, nMonth = 0, nYear = 0; var chDate, chFormat; if (typeof strDate != "string") nParseState = STATE_TYPE_ERROR; if (typeof strDateFormat == "undefined") strDateFormat = DateFormat.i18nTexts.getString('dateFormat', locale); if (typeof strDateFormat == "undefined") { strDateFormat = '?'; alert ("DateFormat.parse(date='" + strDate + "', locale='" + locale + "', dateFormat='" + strDateFormat + "')"); } for ( nCharPos = 0 , nDateFormatPos = 0 ; (nDateFormatPos < strDateFormat.length) && (nParseState >= 0) ; nCharPos++ ) { chDate = nCharPos < strDate.length ? strDate.charAt(nCharPos) : ''; chFormat = strDateFormat.charAt(nDateFormatPos); if (this.test) alert("DateFormat check " + strDate.substr(0, Math.max(Math.min(nCharPos, strDate.length), 0)) + "<" + chDate + ">" + strDate.substr(Math.min(nCharPos + 1, strDate.length)) + " -- " + strDateFormat.substr(0, Math.max(Math.min(nDateFormatPos, strDateFormat.length), 0)) + "<" + chFormat + ">" + strDateFormat.substr(Math.min(nDateFormatPos + 1, strDateFormat.length)) ); if ((chDate >= '0') && (chDate <= '9')) { switch (chFormat) { case 'd': nDateFormatPos++; //alert('nDay = ' + nDay + ' * 10 + \'' + chDate + '\' - \'0\'\n'); nDay = nDay * 10 + (chDate - '0'); break; case 'M': nDateFormatPos++; nMonth = nMonth * 10 + (chDate - '0'); break; case 'y': nDateFormatPos++; nYear = nYear * 10 + (chDate - '0'); break; default: //literals require exact match { if (chDate == chFormat) nDateFormatPos++; else { if (nDateFormatPos > 0) //try to append to prevoius format { switch (strDateFormat.charAt(nDateFormatPos - 1)) { case 'd': nDay = nDay * 10 + (chDate - '0'); break; case 'M': nMonth = nMonth * 10 + (chDate - '0'); break; case 'y': nYear = nYear * 10 + (chDate - '0'); break; default: nParseState = DateFormat.STATE_FORMAT_ERROR; } } else nParseState = DateFormat.STATE_FORMAT_ERROR; } } } } else //if literal char requested { if (chDate == chFormat) nDateFormatPos++; else if (nDateFormatPos > 0) { switch (strDateFormat.charAt(nDateFormatPos - 1)) { case 'd': case 'M': case 'y': { nDateFormatPos++; //skip 1 format position nCharPos--; //read the current date char again break; } default: nParseState = DateFormat.STATE_FORMAT_ERROR; } } else nParseState = DateFormat.STATE_FORMAT_ERROR; } } if (nDateFormatPos < strDateFormat.length) nParseState = DateFormat.STATE_FORMAT_ERROR; var dateValue = null; //var currentDate = new Date(); if (nParseState == DateFormat.STATE_OK) { if ((nMonth < 1) || (nMonth > 12)) { nCharPos = strDateFormat.indexOf('M'); if (nCharPos != -1) nParseState = DateFormat.STATE_INVALID_MONTH; else nMonth = 1; //currentDate.getMonth() + 1; } } if (nParseState == DateFormat.STATE_OK) { if ((nDay < 1) || (nDay > 31)) { nCharPos = strDateFormat.indexOf('d'); if (nCharPos != -1) nParseState = DateFormat.STATE_INVALID_DATE; else nDay = 1; } if (nYear < 100) nYear += 2000; if ( (nDay - (nMonth + Math.floor(nMonth / 8)) % 2 > 30) || (nMonth == 2) && (nDay + sgn(nYear % 4) - sgn(nYear % 100) + sgn(nYear % 400) - 1 > 28) ) { nCharPos = strDateFormat.indexOf('d'); nParseState = DateFormat.STATE_INVALID_DATE; } else { dateValue = new Date(Date.UTC(nYear, nMonth - 1, nDay)); } } this.strMsg = ''; switch (nParseState) { case DateFormat.STATE_FORMAT_ERROR: { this.strMsg = strDateFormat; } break; case DateFormat.STATE_TYPE_ERROR: { this.strMsg = "Dear Programmer:\nDatum.parse(strDate): data type not supported"; } break; case DateFormat.STATE_ILLEGAL_STATE: { this.strMsg = "Dear Programmer:\nDatum.parse(strDate): Illegal parse state: " + nParseState; } break; } if (nParseState != DateFormat.STATE_OK) { var sDateFmt = DateFormat.i18nTexts.getString(strDateFormat); //this.strMsg = DateFormat.i18nTexts.getString('Message' + nParseState) + ": " + sDateFmt.substr(0, nCharPos) + "?" + sDateFmt.substr(nCharPos); this.strMsg = DateFormat.i18nTexts.getString('Message' + nParseState) + ": " + sDateFmt; } return dateValue; } prototype.format = function(dateValue, strDateFormat) { if (typeof strDateFormat != "string") strDateFormat = DateFormat.i18nTexts.getString('dateFormat'); var nDay = dateValue.getUTCDate() , nMonth = dateValue.getUTCMonth() + 1 , nYear = dateValue.getUTCFullYear() ; var strDate = '', chFormat, nSequence; for ( var nFormatPos = 0 ; nFormatPos < strDateFormat.length ; nFormatPos += nSequence ) { chFormat = strDateFormat.charAt(nFormatPos); //count no of equal chars in a sequence for ( var nFormatPos2 = nFormatPos + 1 ; nFormatPos2 < strDateFormat.length && strDateFormat.charAt(nFormatPos2) == chFormat ; nFormatPos2++ ); nSequence = nFormatPos2 - nFormatPos; switch (chFormat) { case 'd': { switch (nSequence) { case 1: strDate += nDay; break; case 2: strDate += nDay < 10 ? '0' + nDay : nDay; break; default: strDate += chFormat; nSequence = 1; } } break; case 'M': { switch (nSequence) { case 1: strDate += nMonth; break; case 2: strDate += nMonth < 10 ? '0' + nMonth : nMonth; break; default: strDate += chFormat; nSequence = 1; } } break; case 'y': { switch (nSequence) { case 4: strDate += nYear; break; case 2: { nYear = nYear % 100; strDate += nYear < 10 ? '0' + nYear : nYear; } break; default: strDate += chFormat; nSequence = 1; } } break; default: strDate += chFormat; } //switch (chFormat) } //for (...) return strDate; } prototype.getMessage = function() {return this.strMsg;} } function sgn(x) {return x > 0 ? 1 : x < 0 ? -1 : 0;} function guessYear(strBirthDay) { var today = new Date(); var yearT = today.getFullYear(); var birthDay = strBirthDay; var dayB = strBirthDay.substr(0, birthDay.indexOf('.')); var monthB = strBirthDay.substr(birthDay.indexOf('.') + 1, birthDay.lastIndexOf('.') - birthDay.indexOf('.') -1); var yearB = strBirthDay.substr(birthDay.lastIndexOf('.') + 1, 4); if (dayB.length == '1') dayB = '0' + dayB; if (monthB.length == 1) monthB = '0' + monthB; // if user ommited the century, we try to guess it if (yearB.length == 1) yearB = '0' + yearB; if (yearB.length == 2) { if (+yearT - ('20' + yearB) >= 0 ) birthDay = dayB + '.' + monthB + '.20' + yearB; else birthDay = dayB + '.' + monthB + '.19' + yearB; } if (yearB > yearT && yearB < yearT + 100) { yearB = yearB - 100 birthDay = dayB + '.' + monthB + "." + yearB; } return birthDay; }