var theForms = new Array() ; var theFormsDebug = false ; // Send the object NAME, NOT the ID (unless, of course, they are the same) function registerObj(formName, objName, objDisplayName, objValType, objRequired) { var formHolder = "" ; var arrayHolder = "" ; var objCount = -1 ; if ( ! theForms[formName] ) { theForms[formName] = new Array() ; // Will hold an array of objects for each form that is being handled } objCount = theForms[formName].length ; formHolder = eval("document." + formName) ; if ( formHolder) { if ( eval("formHolder[objName]") ) { theForms[formName][objCount] = new Array() ; theForms[formName][objCount]["objectName"] = objName ; theForms[formName][objCount]["objectPointer"] = eval("formHolder." + objName) ; theForms[formName][objCount]["originalClass"] = theForms[formName][objCount]["objectPointer"].className ; theForms[formName][objCount]["originalBorder"] = theForms[formName][objCount]["objectPointer"].style.border ; theForms[formName][objCount]["displayName"] = objDisplayName ; theForms[formName][objCount]["validationType"] = objValType ; theForms[formName][objCount]["isRequired"] = objRequired ; theForms[formName][objCount]["objectPointer"] = eval("formHolder." + objName) ; theForms[formName][objCount]["originalValue"] = theForms[formName][objCount]["objectPointer"].value ; theForms[formName][objCount]["extraOnBlur"] = "" ; ls_currentOnFocus = theForms[formName][objCount]["objectPointer"].getAttribute("onFocus") ; if ( ( ls_currentOnFocus == "" ) || ( ls_currentOnFocus == null ) ) { theForms[formName][objCount]["objectPointer"].setAttribute("onfocus", "fieldOnFocus(this);", 0) ; } else { theForms[formName][objCount]["objectPointer"].setAttribute("onfocus", ls_currentOnFocus + " " + "fieldOnFocus(this);", 0) ; } ls_currentOnBlur = theForms[formName][objCount]["objectPointer"].getAttribute("onBlur") ; if ( ( ls_currentOnBlur == "" ) || ( ls_currentOnBlur == null ) ) { theForms[formName][objCount]["objectPointer"].setAttribute("onBlur", "fieldOnBlur(this, '" + objValType + "', " + objRequired.toString() +", '" + theForms[formName][objCount]['originalClass'] + "', '' );") ; } else { theForms[formName][objCount]["extraOnBlur"] = ls_currentOnBlur ; theForms[formName][objCount]["objectPointer"].setAttribute("onBlur", "fieldOnBlur(this, '" + objValType + "', " + objRequired.toString() +", '" + theForms[formName][objCount]['originalClass'] + "', " + theForms[formName][objCount]['extraOnBlur'] + " )") ; // theForms[formName][objCount]["objectPointer"].setAttribute("onBlur", "fieldOnBlur(this, '" + objValType + "', " + objRequired.toString() +", '" + theForms[formName][objCount]['originalClass'] + "' )") ; //theForms[formName][objCount]["objectPointer"].setAttribute("onBlur", ls_currentOnBlur + " " + "fieldOnBlur(this, '" + objValType + "', " + objRequired.toString() +")") ; } } } } function theFormsValidate(formName, showError) { var li_x = 0 ; var lb_retval = true ; var ls_msg = "" ; var fieldArray = "" ; var valArray = "" ; var lo_focus = "" ; if ( theForms[formName] ) { for ( li_x = 0; li_x < theForms[formName].length; li_x++ ) { // Store the actual object array within a shorter name for clarity fieldArray = theForms[formName][li_x] ; if ( fieldArray["displayName"] == "Membership Type" ) { foo = 1 ; } // Call the validateObject() function, which returns an assoc. array (struct) valArray = validateObject(fieldArray) ; // If the function marked as invalid, it will provide a message (required or validation type) if ( valArray["isValid"] == false ) { lb_retval = false ; if ( lo_focus == "" ) lo_focus = fieldArray["objectPointer"] ; // If we are going to show errors, contatenate to the variable if ( showError ) { ls_msg = ls_msg + "\n" + valArray.message ; } } } // End (for) loop } else { // We don't have the form that the routine is trying to access. Either no objects have been registered or there's a problem. Either way -- let the developer know during testing. alert("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nNo objects have been registered by form name " + formName + "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") ; } // If we're showing a message and we have validation errors, then do the alert if ( ( showError ) && ( lb_retval == false ) ) { alert(ls_msg) ; lo_focus.focus() ; } return lb_retval ; } function validateObject(objStruct) { var rstruct = new Array() ; var lb_fieldValidated = true ; var field = objStruct["objectPointer"] ; rstruct["isValid"] = true ; rstruct["message"] = "" ; rstruct["isEmpty"] = fieldIsEmpty(objStruct["objectPointer"]) ; rstruct["isRequired"] = objStruct["isRequired"] ; rstruct["validationType"] = objStruct["validationType"] ; rstruct["originalClass"] = objStruct["originalClass"] ; rstruct["extraOnBlur"] = objStruct["extraOnBlur"] ; if ( ( rstruct["isEmpty"] ) && ( rstruct["isRequired"] ) ) { // Check first for empty/required, and return that message. rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " is a required field." ; } else { // If either wasn't required or was required but isn't empty, then check against any type received. if ( rstruct["validationType"] == "AlphaNumeric" ) { if ( ! isAlphaNumeric(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain letters, numbers and spaces." ; } } else if ( rstruct["validationType"] == "AlphaNumeric-NoSpaces" ) { if ( ! isAlphaNumericNoSpaces(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain letters with no spaces." ; } } else if ( rstruct["validationType"] == "AlphaNumeric-Extra" ) { if ( ! isAlphaNumericExtra(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain letters, numbers, spaces and periods." ; } } else if ( rstruct["validationType"] == "NoPipes" ) { if ( ! isNoPipes(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can contain any character except the pipe character." ; } } else if ( rstruct["validationType"] == "Alpha" ) { if ( ! isAlpha(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain letters." ; } } else if ( rstruct["validationType"] == "Numeric") { if ( ! isNumeric(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain numbers." ; } } else if ( rstruct["validationType"] == "Money") { if ( ! isMoney(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain numbers and a decimal point." ; } } else if ( rstruct["validationType"] == "URL") { if ( ! isURL(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " must be a valid URL beginning with http:// or https://." ; } } else if ( rstruct["validationType"] == "Email") { if ( ! isEmail(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " must be a valid email address." ; } } else if ( rstruct["validationType"] == "Phone") { if ( ! isPhone(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain numbers, dashes and parenthesis." ; } } else if ( rstruct["validationType"] == "Date") { if ( ! isDate(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " must be a valid date in the mm/dd/yyyy format." ; } } else if ( rstruct["validationType"] == "Zip") { if ( ! isZip(field.value)) { rstruct["isValid"] = false ; rstruct["message"] = objStruct["displayName"] + " can only contain numbers and a single dash." ; } } } if ( rstruct["isValid"] == false ) { fieldError(field, rstruct["originalClass"]) ; } else { fieldOkay(field, rstruct["originalClass"]) ; } return rstruct ; } function fieldIsEmpty(field) { var lb_retval = false ; if ( ( field ) && ( allTrim(field.value) == "" ) ) { lb_retval = true ; } return lb_retval ; } function fieldOnBlur(field, validationType, required, originalClass, extraOnBlur ) { field.value = allTrim(field.value) ; if ( validationType == "" ) { fieldOkay(field, originalClass) ; } else if ( validationType == "AlphaNumeric" ) { if ( ! isAlphaNumeric(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Alpha" ) { if ( ! isAlpha(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "AlphaNumeric-NoSpaces" ) { if ( ! isAlphaNumericNoSpaces(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "AlphaNumeric-Extra" ) { if ( ! isAlphaNumericExtra(field.value)) { fieldError(field) ; } else { fieldOkay(field) ; } } else if ( validationType == "NoPipes" ) { if ( ! isNoPipes(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Numeric") { if ( ! isNumeric(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Money") { if ( ! isMoney(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "URL") { if ( ! isURL(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Email") { if ( ! isEmail(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Phone") { if ( ! isPhone(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Date") { if ( ! isDate(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } else if ( validationType == "Zip") { if ( ! isZip(field.value)) { fieldError(field, originalClass) ; } else { fieldOkay(field, originalClass) ; } } // If field is required (third param), then make sure it's not blank. if ( ( required == true ) && ( field.value == "" ) ) { fieldError(field, originalClass) ; } else if ( ( required != true ) && ( field.value == "" ) ) { fieldOkay(field, originalClass) ; } } function fieldOnFocus(field) { if ( field.type == "text") field.select() ; } function fieldError(field, originalClass) { field.className = originalClass + "Error" ; } function fieldOkay(field, originalClass) { field.className = originalClass ; } // ———————————————————————————————————————————————————————————————————————————————— // — The majority of these functions return TRUE if the value is empty. They are only checked against the validation if they contain a value... // ———————————————————————————————————————————————————————————————————————————————— function isURL(argvalue) { var li_startPos = 7 ; if ( argvalue == "" ) return true ; if ( argvalue.substring(0, 4) == "https" ) li_startPos = 8 ; if (argvalue.indexOf(" ") != -1) return false; else if ( ( argvalue.indexOf("http://") == -1 ) && ( argvalue.indexOf("https://") == -1 ) ) return false; else if ( ( argvalue == "http://" ) || ( argvalue == "https://" ) ) return false; else if ( ( argvalue.indexOf("http://") > 0 ) || ( argvalue.indexOf("https://") > 0 ) ) return false; argvalue = argvalue.substring(li_startPos, argvalue.length); if (argvalue.indexOf(".") == -1) return false; else if (argvalue.indexOf(".") == 0) return false; else if (argvalue.charAt(argvalue.length - 1) == ".") return false; if (argvalue.indexOf("/") != -1) { argvalue = argvalue.substring(0, argvalue.indexOf("/")); if (argvalue.charAt(argvalue.length - 1) == ".") return false; } if (argvalue.indexOf(":") != -1) { if (argvalue.indexOf(":") == (argvalue.length - 1)) return false; else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".") return false; argvalue = argvalue.substring(0, argvalue.indexOf(":")); if (argvalue.charAt(argvalue.length - 1) == ".") return false; } return true; } function x_isURL(theValue) { // var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/; var urlRegxp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; var lb_retval =theValue.match(urlRegxp) ; if ( lb_retval == null ) lb_retval = false ; if ( theValue.length = 0 ) { lb_retval = true ; } return lb_retval ; } function isEmail(theValue) { var emailregex="^([_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.(([A-Za-z]{2,3})|(aero|coop|info|museum|name)))?$" ; //var emailregex=/^[\w]+\+?\w*@[\w]+\.[\w.]+\w$/; var lb_retval =theValue.match(emailregex); return lb_retval ; } // —————————————————————————————————————————————————————————————— // ———————————————————————— BEGIN DATE FUNCTIONS ———————————————————————— // —————————————————————————————————————————————————————————————— function daysInFebruary (year){ // February has 29 days in any year evenly divisible by four, // EXCEPT for centurial years which are not also divisible by 400. return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 ); } function DaysArray(n) { for (var i = 1; i <= n; i++) { this[i] = 31 if (i==4 || i==6 || i==9 || i==11) {this[i] = 30} if (i==2) {this[i] = 29} } return this } function isDate(dtStr){ // Declaring valid date character, minimum year and maximum year // Validates ONLY to format mm/dd/yyyy (WB) // Also returns true if empty to fit in with other validation functions here if ( allTrim(dtStr) == "" ) return true ; var dateSeparator= "/"; var minYear=1900; var maxYear=2200; var daysInMonth = DaysArray(12) var pos1=dtStr.indexOf(dateSeparator) var pos2=dtStr.indexOf(dateSeparator,pos1+1) var strMonth=dtStr.substring(0,pos1) var strDay=dtStr.substring(pos1+1,pos2) var strYear=dtStr.substring(pos2+1) strYr=strYear if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1) if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1) for (var i = 1; i <= 3; i++) { if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1) } month=parseInt(strMonth) day=parseInt(strDay) year=parseInt(strYr) if (pos1==-1 || pos2==-1){ //alert("The date format should be : mm/dd/yyyy") return false } if (strMonth.length<1 || month<1 || month>12){ //alert("Please enter a valid month") return false } if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){ //alert("Please enter a valid day") return false } if (strYear.length != 4 || year==0 || yearmaxYear){ //alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear) return false } if (dtStr.indexOf(dateSeparator,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dateSeparator))==false){ //alert("Please enter a valid date") return false } return true } // —————————————————————————————————————————————————————————————— // ———————————————————————— END DATE FUNCTIONS ———————————————————————— // —————————————————————————————————————————————————————————————— function isPhone(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^0-9\-\(\) ]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isZip(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" //var charpos = ls_newValue.search("[^0-9\-]") ; var charpos = ls_newValue.search("[^A-Za-z0-9\- ]") ; // Alpha, dashes, numbers, spaces if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isInteger(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^0-9]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isNumeric(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^0-9\.]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isMoney(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^0-9\.]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isAlphaNumeric(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^A-Za-z0-9 ]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isAlphaNumericExtra(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^A-Za-z0-9\.\,\#\(\)\'\&\\-\_\/\\ ]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isNoPipes(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[\|]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isAlphaNumericNoSpaces(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^A-Za-z0-9]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function isAlpha(theValue) { var lb_retval = true ; var ls_newValue = theValue + "" ; // Add empty quotes to allow search() function to operate on a "string" var charpos = ls_newValue.search("[^A-Za-z ]") ; if ( ls_newValue.length > 0 && charpos >= 0 ) { lb_retval = false ; } return lb_retval ; } function roundInteger(value, zeroIfEmpty) { var retval = Math.round(value) ; if ( zeroIfEmpty != false ) { if ( value == "" ) retval = "" ; } if ( ! isNumeric(retval) ) retval = value ; return retval ; } function roundDecimal2(value, zeroIfEmpty) { var retval = (Math.round(value * 100))/100 + "" ; var dot = retval.indexOf(".",0) ; if (dot == -1) { retval = retval + ".00" ; } else if (dot == retval.length - 2) { retval = retval + "0" ; } if ( zeroIfEmpty != false ) { if ( value == "" ) retval = "" ; } if ( isNumeric(retval) == false ) retval = value ; return retval ; } function convertFeetToMeters(value) { var ldec_retval = 0.00 ; if ( isNumeric(value) ) ldec_retval = value * 0.3048 ; return ldec_retval } function convertMetersToFeet(value) { var ldec_retval = 0.00 ; if ( isNumeric(value) ) ldec_retval = value * 3.2808399 ; return ldec_retval } function convertGallonsToLiters(value) { var ldec_retval = 0.00 ; if ( isNumeric(value) ) ldec_retval = value * 3.7854118 ; return ldec_retval } function convertLitersToGallons(value) { var ldec_retval = 0.00 ; if ( isNumeric(value) ) ldec_retval = value * 0.264172051 ; return ldec_retval } function allTrim(theValue) { while (theValue.substring(0, 1) == ' ' || theValue.charCodeAt(0) == 32) { theValue = theValue.substring(1, theValue.length); } while (theValue.substring(theValue.length - 1, theValue.length) == ' ' || theValue.charCodeAt(theValue.length - 1) == 32) { theValue = theValue.substring(0, theValue.length - 1); } return theValue; } function stripCharsInBag(s, bag){ var i; var returnString = ""; // Search through string's characters one by one. // If character is not in bag, append to returnString. for (i = 0; i < s.length; i++){ var c = s.charAt(i); if (bag.indexOf(c) == -1) returnString += c; } return returnString; }