//--------------------------------------------------------------------------- // Tauris FRAMEWORK: Client validation functions //--------------------------------------------------------------------------- /************************************************************************** * Write the fields in the window that called for lookup * * fieldname: string with the updated fields (ex. "loc_id\tloc_naam\tlhv_id") * fieldvalue: string with the values (ex. "45452\tAntwerpen\t2") * formname: name of the form where that contains the update-fields * suffix: ex. loc_naam_entry -> '_entry' is suffix */ function Afterlookup(fieldname, fieldvalue, formname, suffix) { RestoreArgs = 0; // We did lookup: be sure 'page unload' does not restore originals cols_field = fieldname.split("\t"); // When parent window is already closed or the form does not exits no action needed here if (window.opener.document == null) return; if (window.opener.document.forms[formname] == null) return; if (fieldvalue == '') { // user heeft het lookup-window gesloten zonder te selecteren // we moeten de originele waarde van de lookup-field weer terugzetten... for (i=0; i < cols_field.length; i++) { field = cols_field[i] + suffix; org_field = 'org' + field; if (window.opener.document.forms[formname].elements[field] && window.opener.document.forms[formname].elements[org_field]) window.opener.document.forms[formname].elements[field].value = window.opener.document.forms[formname].elements[org_field].value; } } else { // user heeft iets geselecteerd (of op de extra link (die niks selecteerd)) // de kolommen worden ge-update cols_value = fieldvalue.split("\t"); if (cols_field.length == cols_value.length) { for (i=0; i < cols_field.length; i++) { field = cols_field[i] + suffix; if (window.opener.document.forms[formname].elements[field]) window.opener.document.forms[formname].elements[field].value = cols_value[i]; } } /* Enkel in dit geval het window sluiten, in het ander geval is * het window reeds gesloten * of er een andere pagina ingeladen (andere lookup) in het window en dus mag het window dan niet (opnieuw) gesloten worden) */ window.close(); } } /************************************************************************** * Prepare args for Lookup and open Window * * lookup_url: URL of the window to open for lookup * frm_obj: reference to the form which contains the element * elementname: name of the element where we use the lookup on * extra_arg: retrieval arguments (ex. 'lhv_id=2&loc_eep_soort_entry=EEP') * * Return: a reference to the openened window */ function Lookup(lookup_url, frm_obj, elementname, extra_arg) { search = escape(frm_obj.elements[elementname].value); // remove user's input as lookup still needs to 'validate' it frm_obj.elements[elementname].value = ""; url = lookup_url + '?' + elementname + '=' + search + '&formname=' + frm_obj.name; if (extra_arg != '') url += '&' + extra_arg; PopupReference = OpenPopup(url, "lookup", 400, 600); // attention: first height and then width PopupReference.focus(); // lookupwindow was mogelijks nog open (in background)... } /************************************************************************** * Opens a new Window with the given arguments. * * url: URL of the window to open * name: name of the openened window * height: height of the new window * width: width of the new window * * Remark: width and height property -> Internet Explorer * innerwidth and innerheight property -> Netscape * * Return: a reference to the openened window */ function OpenPopup(url, name, height, width) { var config='innerHeight=' + height + ',innerWidth=' + width + ',height=' + height + ',width=' + width + ',toolbar=no,scrollbars=yes,menubar=no,resizable=yes,location=no,directories=no,status=yes'; return window.open(url, name, config); } /************************************************************************** * Try to close a lookup-window (only in case we opened it) * * Return: None */ function AbortLookup() { if (PopupReference) { // Close the popup when not already done if (!PopupReference.closed) PopupReference.close(); PopupReference = null; } } /************************************************************************** * Validate date/time input; if invalid: alert user and set focus back to col. * Empty is allowed (user must check required fields upon form submit !) * * col: the target column * label: label for the errormsg * format: 'Y' for a date formatted as year-only (YYYY) * 'D' for date as DD/MM/YYYY * 'T' or 'H' for time as 'hh:mm' * 't' or 'h' for time as 'hh:mm:ss' * 'DT', 'Dt', 'DH', 'Dh' for sum of both * !! when 'H' or 'h' is used, the validation will accept * a date without time also and a time without seconds * alert: 'J' -> show an alert if DT is not valid * 'N' -> don't show alert (use the return value) * * Return: True if valid, False if not */ function ValidateDT(col, label, format, alert) { var error = false; var val = col.value; var sep = "- /"; var timeoffset = 0; var errorfield = ""; var showformat = ""; if (val == "") return true; // Empty... // Target format dd/mm/yyyy hh:mm:ss, following given format if (format == 'Y') { showformat = "yyyy"; error = true; // Suppose wrong YEAR if (val.length == 4) { if (val >= '1990') error = false; } if (error) if (document.frmClientScript.language.value == 1) errorfield = 'year'; else errorfield = 'jaar'; } else if (format.substring(0, 1) == 'D') { showformat = "dd/mm/yyyy"; error = true; // Suppose wrong DT format = format.substring(1, 2); // Remaining time format timeoffset = 11; if (val.length >= 10 && sep.indexOf(val.substring(2, 3)) >= 0 && sep.indexOf(val.substring(5, 6)) >= 0) { var maxdays = 31; day = val.substring(0, 2); month = val.substring(3, 5); year = val.substring(6, 10); if (month == 2) { // Leap year: if can divide by 4, not by 100 but leap if by 400 maxdays = 28; if ((year % 4) == 0) { if ((year % 100) != 0 || (year % 400) == 0) maxdays = 29; } } else if (month == 4 || month == 6 || month == 9 || month == 11) maxdays = 30; if (year >= '1990' && month >= 1 && month <= 12 && day >= 1 && day <= maxdays) error = false; } if (error) if (document.frmClientScript.language.value == 1) errorfield = 'date'; else errorfield = 'datum'; } if (format == 'H' || format == 'h') { // When used did enter a time: also validate the time if (val.length - timeoffset > 0) { // let rest of function act as normal time fields if (format == 'H' || (val.length - timeoffset) == 5) format = 'T'; else format = 't'; } } if (format == 'T' || format == 't') { if (showformat != "") showformat += " "; showformat += "hh:mm"; if (format == 't') showformat += ":ss"; if (!error) { error = true; // Suppose wrong T if ( ( (format == 'T' && (val.length - timeoffset) == 5) || (format == 't' && (val.length - timeoffset) == 8) ) && val.substring(timeoffset + 2, timeoffset + 3) == ':' && ( format == 'T' || val.substring(timeoffset + 5, timeoffset + 6) == ':' ) ) { hours = val.substring(timeoffset, timeoffset + 2); mins = val.substring(timeoffset + 3, timeoffset + 5); if (format == 't') secs = val.substring(timeoffset + 6, timeoffset + 8); else secs = 0; if (hours >= 0 && hours <= 23 && mins >= 0 && mins <= 59 && secs >= 0 && secs <= 59) error = false; } if (error) { if (errorfield != "") errorfield += "/"; if (document.frmClientScript.language.value == 1) errorfield += 'time'; else errorfield += 'tijd'; } } } if (error) { if (alert == 'J') { if (document.frmClientScript.language.value == 1) window.alert(label + ": please fill in a valid " + errorfield + " (" + showformat + ")"); else window.alert(label + ": gelieve een geldige " + errorfield + " in te vullen (" + showformat + ")"); } col.focus(); return false; } else return true; } /************************************************************************** * Validate numeric input; if invalid: alert user and set focus back to col. * Empty is allowed (user must check required fields upon form submit !) * Remark : translation is not completed... * * col: the target column * label: label for the errormsg * format: combination of '9' characters with max. 1 '.' for decimal digit * e.g. "999.99" -> numeric(5, 2) * "999" -> numeric(3) * min: minimum value * max: maximum value * * Return: True if valid, False if not */ function ValidateNumeric(col, label, format, min, max) { var error = false; var val = col.value; if (val == "") return true; // Empty... if (val.length > format.length) error = true; /*** else { v = parseFloat(val); //!!! Causes MS Explorer 4 to crash !!! if (isNaN(v)) error = true; else if (v < min || v > max) error = true; } ***/ if (error) { if (document.frmClientScript.language.value == 1) window.alert(label + ": please insert a valid number (min " + min + " max " + max + ")"); else window.alert(label + ": gelieve een geldig getal in te geven (van " + min + " tot " + max + ")"); col.focus(); return false; } else return true; } /************************************************************************** * Show 'required' message for given column * * col: target column * label: column label * * Return: true error (value is empty) * false ok (value exists) */ function CheckMissingRequired(col, label, id_col) { var missing = false; if (id_col != null) if (id_col.value == "") missing = true; if (col.value == "") missing = true; if (missing == true) { if (document.frmClientScript.language.value == 1) alert("Please fill in the field '" + label + "'"); else alert("Gelieve het veld '" + label + "' in te vullen"); col.value = ""; // remove current value, might be the ID that's missing while user's input is still visible but not 'looked up' col.focus(); return true; } return false; } /************************************************************************** * Test whether given character is a digit * * c: char. to test * * Return: true if digit * false if not a digit */ function IsDigit(c) { if (c >= '0' && c <= '9') return true; return false; } /************************************************************************** * KeyPress Eventhandler * This function can be used to force a submit of the form whenever the * enter key is pressed. By setting this function on some selected fields * the user will be able to submit the form from those fields by pressing * enter. * * target_form: the form to submit when ENTER is pressed * event: the complete event structure * */ function SubmitOnEnter(target_form, event) { var key; if (document.layers) // NN key = event.which; if (document.all) // MSIE key = event.keyCode; // Simply submit the form when enter is pressed if (key == 13) { target_form.submit(); event.returnValue = false; // MSIE return false; // NN } } /************************************************************************** * Remove the trailing space/s of an argument. * * argvalue: stringValue is the string that you want to remove its * trailing space/s. * * Return: return the trimmed value * */ function rtrim(argvalue) { while (1) { if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ") break; argvalue = argvalue.substring(0, argvalue.length - 1); } return argvalue; } /************************************************************************** * Remove the leading space/s of an argument. * * argvalue: stringValue is the string which the leading space/s * will be removed. * * Return: return the trimmed value * */ function ltrim(argvalue) { while (1) { if (argvalue.substring(0, 1) != " ") break; argvalue = argvalue.substring(1, argvalue.length); } return argvalue; } /************************************************************************** * Remove both the leading and the trailing space/s of an argument. * * argvalue: stringValue is the string which the leading and the * trailing space/s will be removed. * * Return: return the trimmed value * */ function trim(argvalue) { var tmpstr = ltrim(argvalue); return rtrim(tmpstr); } /************************************************************************** * Function to write a cookie-variable, based on code from www.cookiecentral.com * * name and variable are required arguments * optional arguments are: expires, path, domain and secure */ function SetCookie(name, value) { var argv = SetCookie.arguments; var argc = SetCookie.arguments.length; var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : '/'; // Indien geen path gegeven: geldig voor de volledige site var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false; if (expires == null) { expires = new Date(); expires.setTime(expires.getTime() + 1000*60*60*24*365); // one year } value = escape(value); if (value == "") { // When no value: let it expire right now expires = new Date(); value = '...'; } document.cookie = name + "=" + value + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : ""); } /************************************************************************** * Function to read a cookie-variable (=code from Netscape sample) * * name is the name of the variable to read * * Return: current value of the specified cookie-variable */ function GetCookie(name) { var search = name + "="; if (document.cookie.length > 0) { offset = document.cookie.indexOf(search); if (offset != -1) { offset += search.length; end = document.cookie.indexOf(";", offset); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(offset, end)); } } return null; } /************************************************************************** * This function writes a link on the document so that the user can set his * Homepage to the specified URL (MSIE 5 and up only) * Note: the link will only be writen when the homepage is not already set so * * url: target homepage URL * url_link: target text for the link to set the homepage * * Return: nothing */ function SetHomepageLink(url, url_link) { var navVer = navigator.appVersion; var IEPos = navVer.indexOf('MSIE'); var isIE5up = false; if (IEPos !=-1) { var ver = parseFloat(navVer.substring(IEPos+5,navVer.indexOf(';',IEPos))); if (ver >= 5) isIE5up = true; } //if (isIE5up) //{ document.writeln(""); if (!hp.isHomePage(url)) { // refresh current page after update document.write("
" + url_link + "
"); } //} }