function validate(thisform){

 

        //*** v3.0 *****************************************************

        //

        //        Form fields are validated only if there is a corresponding

        //        hidden form field whose name starts with "validate_" and ends with

        //        the name of the field to be validated.

        //

        //         EG. To set validation on an INPUT field "state", a matching hidden

        //            form field named "validate_state" must exist.

        //

        //            Each validation field's value can contain 1 or 2 semicolon-delimited

        //        values.  The first value is the criterion list (which is comma-delimited)

        //        and the second optional value is the custom warning message that will

        //        appear to prompt the user for valid input.

        //

        //        The criterion list contains the criterion ID (see below) to

        //        be used, and an "isRequired" flag.

        //

        //        NOTE:   Insure that hidden form fields reside within the same

        //                form that the fields to be validated reside in.

        //

        //

        //                VERSION HISTORY: ----------------------

        //

        //                        1.0 - Dynamic Validation based on validationList

        //                        2.0 - Custom warning messages - hidden params,

        //                                  start with "custmsg_" end with fieldname.

        //                        2.5 - Polish warning messages. isPrice() completed

        //                                  isAddress() can contain ()#

        //                        3.0 - remove validationList, just use a "validate_" hidden

        //                                  for each field to be validated.

        //

        //

        //        criterionList FORMAT:  value="criterionID:min:max,isRequired"

        //

        //                   EXAMPLE:  <input type="hidden" name="validate_state"

        //                                     value="address:23:23,0">

        //

        //

        //                criterionList (optional)

        //                        * The name describing the validation criterion.

        //                        * Default is no criterion, and no :min:max

        //                        * See below for syntax

        //

        //                isRequired (optional)

        //                        * 0 = optional , 1 = required

        //                        * Default is 1

        //

        //          --------------------

        //            criterionList - syntax:

        //

        //                              FORMAT :  "criterionID[:min:max]"

        //

        //                             EXAMPLE :  "number:6:10"

        //

        //                         criterionID :         (needs at least first 4 characters of identifiers, below [ie. addr = address)

        //                                        name = alphabetic and ,'.-

                        //                                                                                                                        regex = regular expression in on of the javascript forms. e.g:

                        //                                                                                                                                  /some-regex/     

                        //                                                                                                                                  /some-regex/single-letter-options

                        //

                        //                                                                                                                                  Examples:

                        //                                                                                                                                              regex:/^[a-zA-Z0-9]+$/;Message if match fails

        //                                                                                                                                      regex:/[a-z]{0,8}/g;Message if match fails

                        //                                                                                                                                              regex:/a/,1;This field is required

                        //                                                                                                                                              regex:/foobar/,0;This field isn't required

                        //

                        //                                                                                                                                  NOTES: 

                        //                                                                                                                                              1) Do not include our version of range when using regular expression tests 

                        //                                                                                                                                              2) Currently, colons or semi-colons aren't supported in the regex

                        //                                                                                                                                                          

        //                                        address = alpha-numeric and ,'.-

        //                                        card = credit card

        //                                           ** MUST include another hidden INPUT field named

        //                                              "validate_sys_CardTypeField" with value "[cardTypeField]"

        //                                              AND [cardTypeField] must be of type SELECT

        //                                        email = email address (8 minimum including '@' and '.')

        //                                        social = Social Security Number

        //                                        phUS = US telehone (7 digits or 10 w. area code)

        //                                           ** MAY include a selectbox field called "Country".

        //                                              If so, 10-digit validation will only occur

        //                                              if the value is "US,USA,or United States"

        //                                        number = contains numeric characters only

        //                                        zipcode = is Valid US Zipcode

        //                                        price = dollar amount($x,xxx.yy)

        //                                        password = alpha-numeric, no spaces, can't be based on username,

        //                                                  or be "password", "guest", "user", "test" etc.

        //                                           ** MUST include another hidden INPUT field named

        //                                              "validate_sys_UserNameField" with value "[UserNameField]"

        //                                                     AND [UserNameField] must be of type TEXT

        //                                           ** OPTIONAL hidden field "validate_sys_ConfirmPasswordField" with value "[ConfirmPasswordField]"

        //                                                  AND [ConfirmPasswordField] must be of type TEXT

        //

        //

        //                          [:min:max] :         (optional)

        //                                        minimum and maximum values for TEXT string lengths

        //                                        or number of selected options in multi-selectboxes.

        //

        //

        //        WARNING: You can have more than one radio button with the same name (typical)

        //                 and more than one checkbox with the same name (less typical)...

        //                 BUT you can NOT have more than one select box or more than one

        //                 text field with the same name within the same form!

        //

        //

        //        NOTES:

        //

        //        Place this in the <HEAD> </HEAD> section of the page:

        //

        //                <script src="../common/<%ValidateJS%>"></script>

        //

        //        Place this within the <FORM ...> tag:

        //

        //                onSubmit="return validate(this);"

        //

        //        Place these within the <FORM> </FORM> section of the page:

        //                **NOTE - for modularity, have Perl replace a string <!--#SYS_stdHiddens#--> with the following:

        //

        //        <input type=hidden name="validate_first_name"                 value="name;Please enter your first name.">

        //        <input type=hidden name="validate_last_name"                 value="name;Please enter your last name.">

        //        <input type=hidden name="validate_email"                 value="email;Please enter an email address.">

        //        <input type=hidden name="validate_address1"                 value="address;Please enter an address.">

        //        <input type=hidden name="validate_city"                 value="address;What city do you live in?">

        //        <input type=hidden name="validate_state"                 value="address;Please select a state.">

        //        <input type=hidden name="validate_zip"                         value="zip,number:5:5;Please enter your zip code.">

        //        <input type=hidden name="validate_home_area_code"         value="number:3:3;Please enter your home area code.">

        //        <input type=hidden name="validate_home_phone_prefix"         value="number:3:3;What is the first part of your home phone number?">

        //        <input type=hidden name="validate_home_phone_suffix"         value="number:4:4;What is the last part of your home phone number?">

        //        <input type=hidden name="validate_work_area_code"         value="number:3:3;Please enter your work area code.">

        //        <input type=hidden name="validate_work_phone_prefix"         value="number:3:3;What is the first part of your work phone number?">

        //        <input type=hidden name="validate_work_phone_suffix"         value="number:4:4;What is the last part of your work phone number?">

        //        <input type=hidden name="validate_work_extension"         value="number:1:6;What is your work extension?">

        //        <input type=hidden name="validate_CallTime"                 value=";What is the best time to call?">

        //        <input type=hidden name="validate_sex"                         value=";Please select your gender.">

        //

        //                <input type=hidden name="validate_username"                         value="uname;Please enter a username between 4 and 15 characters in length which consists of only numbers and at least one letter.">

        //

        //

        //******************************************************************

 

        debug1 = false;  // turn on to show alerts for form scan loop

        debug2 = false;  // turn on to show alerts for validation section

        debug3 = false;  // turn on to show alerts for validation functions

        debugSubmit = false;  // turn on to show if SUBMIT() was reached (successful validation)

 

        formLen = thisform.elements.length;

        if(debug1){alert('form length = '+formLen)}

 

        valMA = new Array(formLen);  // build this (validateMultiArray) first, then loop over when validating

        valFieldCount=0;             // (3.0) keep track of exactly how many 'validate_'s fields there are

 

        validateUSphone = false;     // will be set to true if selectBox named "Country" exists

        validateCreditCard = false;  // will be set to true if a hidden field named "validate_sys_CardTypeField" exists.

        // NOTE: "validate_sys_CardTypeField" holds the NAME of the Selectbox FIELD!!!

        validatePassword = false;    // will be set to true if a hidden field named "validate_sys_UserNameField" exists.

        // NOTE: "validate_sys_UserNameField" holds the NAME of the text FIELD!!!

        validateConfirmPassword = false;    // will be set to true if a hidden field named "validate_sys_ConfirmPasswordField" exists.

        // NOTE: "validate_sys_ConfirmPasswordField" holds the NAME of the text FIELD!!!

 

 

        //  scan each form element, search for hidden 'validate_'s with corresponding fields.

        for (e=0; e < formLen; e++){

 

                thisFieldName = thisform.elements[e].name;

                thisFieldType = thisform.elements[e].type

 

                // check to see if a selectBox called "Country" exists

                if ((thisFieldName == 'country') && (thisFieldType == 'select-one')){

                        validateUSphone = true;

                        if(debug1){alert('validateUSphone = '+validateUSphone)}

                }

                // check to see if a selectBox called "validate_sys_CardTypeField" exists

                if ((thisFieldName == 'validate_sys_CardTypeField') && (thisFieldType == 'hidden')){

                        validateCreditCard = true;

                }

                // check to see if a text field called "validate_sys_UserNameField" exists

                if ((thisFieldName == 'validate_sys_UserNameField') && (thisFieldType == 'hidden')){

                        validatePassword = true;

                }

                // check to see if a text field called "validate_sys_ConfirmPasswordField" exists

                if ((thisFieldName == 'validate_sys_ConfirmPasswordField') && (thisFieldType == 'hidden')){

                        validateConfirmPassword = true;

                }

 

 

                // populate valMA() array with info on target validate fields (valXXX)

                if ((thisFieldName.substr(0,9) == 'validate_') && (thisFieldType == 'hidden')){

 

                        valFieldName = thisFieldName.substr(9,thisFieldName.length-9); // truncate to get fieldname to validate

 

                        //  scan whole form quickly, make sure corresponding non-hidden field exists;

                        correspondingField = false;

                        for (scan=0; scan < formLen; scan++){

                                scanFieldName = thisform.elements[scan].name;

                                scanFieldType = thisform.elements[scan].type;

                                if((scanFieldName == valFieldName) && (scanFieldType != 'hidden')){

                                        correspondingField = true;

                                        if(debug2){alert('found hidden: valFieldName = '+valFieldName)}

                                        break;

                                }

                        }

 

                        if(correspondingField){

 

                                valFieldType = eval('thisform.'+valFieldName+'.type');                   //** NOT GOOD FOR MULTIPLE-FIELDS with same name, so...

                                if (typeof valFieldType == "undefined"){

                                        valFieldType = eval('thisform.'+valFieldName+'[0].type'); //** grab type of first instance

                                }

                                thisFieldValue = thisform.elements[e].value;    //grab this hidden fields's value

 

                                // set parameter defaults

                                criterionID = "";

                                isRequired = 1;

                                custMsg = "";

 

                                // parse hidden thisFieldValue (may contain optional values)

                                fieldValueArray = thisFieldValue.split(';');         // each value is delimited by a semicolon

                                criterionList = fieldValueArray[0];

 

                                                                                                // Parse the first part of the criterion, which includes optional stuff

                                if (criterionList != ""){

                                                                                                                        var criterionArray = new Array();

 

                                                                                                                        // Regular expression need special treatment

                                                                                                                        if(criterionList.substr(0,5) == 'regex')

                                                                                                                        {

                                                                                                                                    // defaults to this, could be overwritten below

                                                                                                                                    criterionID = criterionList;

 

                                                                                                                                    // Check if we have a 'required' optional argument

                                                                                                                                    var index = criterionList.lastIndexOf(',');

                                                                                                                                    if(index != -1) // we found a comma

                                                                                                                                    {

                                                                                                                                                var _requiredMarker = criterionList.substr(index+1);

                                                                                                                                                if(_requiredMarker.length == 1 ||  _requiredMarker.length == 0 )  

                                                                                                                                                {

                                                                                                                                                            criterionID = criterionList.substr(0,index);

 

                                                                                                                                                            // If the comma is not part of the regex

                                                                                                                                                            if(_requiredMarker != '/')

                                                                                                                                                            {

                                                                                                                                                                        isRequired = _requiredMarker; // this should be a 0 or 1

                                                                                                                                                            }

                                                                                                                                                }

                                                                                                                                    }

 

                                                                                                                        }

                                                                                                                        else // handle every other type the same

                                                                                                                        {

                                                criterionArray = criterionList.split(',');

                                                criterionID = criterionArray[0];

                                                                                                                                    if (criterionArray.length > 1){

                                                                                                                                                            isRequired = criterionArray[1];

                                                                                                                                    }

                                                                                                                        }

 

                                }

                                if (fieldValueArray.length > 1){

                                        custMsg = fieldValueArray[1];        // second optional value is custom message

                                }

 

 

                                valMA[valFieldCount] = new Array(8); // create (validateMultiArray) sub-array

 

                                // populate valMA() multiArray

                                valMA[valFieldCount][0] = valFieldName;

                                valMA[valFieldCount][1] = criterionID;

                                valMA[valFieldCount][2] = isRequired;

                                valMA[valFieldCount][3] = valFieldType;

                                valMA[valFieldCount][4] = custMsg;  // custmsg, if null it will be set to  default based on type

                                valMA[valFieldCount][5] = 0;           // isSelectedCount - will be overridden by new tally

                                valMA[valFieldCount][6] = false;          // subFields - (depricated) will be set to true for fields with multiple radio buttons and checkboxes.

                                valMA[valFieldCount][7] = "";         // value - will be overidden by actual value derived based on type.

 

                                if(debug1)

                                                                                                {

                                                                                                            alert('found hidden: '+valFieldCount+'\n valFieldName = '+valFieldName+'\n criterionID = '+criterionID+'\n isRequired = '+isRequired+'\n valFieldType = '+valFieldType+'\n custMsg = '+custMsg+'\n isSelectedCount = '+valMA[valFieldCount][5]+'\n subFields = '+valMA[valFieldCount][6]+'\n value = '+valMA[valFieldCount][7])

                                                                                                }// debug

 

                                //  check for multiFields in radio/checkboxes/selects

                                switch (valFieldType.substr(0,1)){

                                        case 's':

                                                flength = eval('thisform.'+valFieldName+'.length');

                                                if(debug1){alert('s-length = '+flength)}

                                                if (!isNaN(flength)) {

                                                        // all selectbox types return the number of options per selectbox.

                                                        // (2 or more selectboxes with the same name return the number of options

                                                        // in the FIRST selectbox).

                                                        valMA[valFieldCount][6] = true ;

                                                }

                                                break;

                                        case 'r':

                                                flength = eval('thisform.'+valFieldName+'.length');

                                                if(debug1){alert('r-length = '+flength)}

                                                if (!isNaN(flength)) {

                                                        valMA[valFieldCount][6] = true ;

                                                }

                                                break;

                                        case 'c':

                                                flength = eval('thisform.'+valFieldName+'.length');

                                                if(debug1){alert('c-length = '+flength)}

                                                if (!isNaN(flength)) {

                                                        valMA[valFieldCount][6] = true ;

                                                }

                                                break;

                                        default :

                                }

 

                                if(debug1 && valMA[valFieldCount][6]){alert('subfields exist')}

 

 

                                // GET VALUE OF CURRENT FIELD based on TYPE

                                if (valFieldType.substr(0,1) == 's'){  // select box

 

                                        // The statement:   fieldVal = eval('thisform.'+pmField+'.value');

                                        //    select-one and select-multiple fields return the FIRST value.

                                        //    multi-selectboxes with the same name returns undefined!!

                                        if(valFieldType == 'select-multiple'){

                                                // TESTING - try to get the value of the first one.

                                                // ** see if it behaves like a multi-selectbox (*IT DOESN'T)

                                                thisSelectOBJ = eval('thisform.'+valFieldName);

                                                if(debug1){alert('thisSelectOBJ = '+thisSelectOBJ)}

                                                var theseSelected = new Array(); // store selected values for possible later use

                                            var index = 0;

                                                var isSelectedCount = 0;

                                            for (var intLoop=0; intLoop < thisSelectOBJ.length; intLoop++) {

                                                          isSelected = thisSelectOBJ[intLoop].selected;

                                                       if (isSelected) {

                                                                index = theseSelected.length;

                                                                if(debug1){alert('isSelected= '+isSelected+' | index= '+index)}

                                                                theseSelected[index] = new Object;

                                                                theseSelected[index].value = thisSelectOBJ[intLoop].value;

                                                                theseSelected[index].index = intLoop;

                                                                // Don't count any null values as selections!

                                                                // Automatically deselect all of the NULL values,

                                                                // then re-assign the valFieldName's value after this loop.

                                                                // If there are any non-NULL values still selected,

                                                                // then this field will pass the isRequired test.

                                                                if (theseSelected[index].value != ""){

                                                                        isSelectedCount++ // save count for :min:max test...

                                                                } else {

                                                                        thisSelectOBJ[intLoop].selected = false; //** automatically de-select them

                                                                }

                                                        }

                                                }

                                                valMA[valFieldCount][5] = isSelectedCount;

                                                if(debug1){alert('isSelectedCount= '+valMA[valFieldCount][5])}

                                                valMA[valFieldCount][7] = eval('thisform.'+valFieldName+'.value'); // just get first selected value for isRequired test

                                        } else { // normal select box (chokes on more than one selectbox with the same name)d

                                                valMA[valFieldCount][7] = eval('thisform.'+valFieldName+'.options[thisform.'+valFieldName+'.selectedIndex].value'); //might be able to use shorter statement - check on other browsers

                                        }

 

                                } else if ((valFieldType.substr(0,1) == 'r') || (valFieldType.substr(0,1) == 'c')) { // radio or checkbox

                                        if (valMA[valFieldCount][6]){

                                                for (var k = 0; k < flength; k++) {

                                                        isChecked = eval('thisform.'+valFieldName+'['+k+'].checked');

                                                        if(isChecked){ // grab last checked value

                                                                valMA[valFieldCount][7] = eval('thisform.'+valFieldName+'['+k+'].value');

                                                        }

                                                }

                                        } else { // only one field exists (a sole radio button, or checkbox)

                                                isChecked = eval('thisform.'+valFieldName+'.checked');

                                                if(isChecked){

                                                        valMA[valFieldCount][7] = eval('thisform.'+valFieldName+'.value');

                                                }

                                        }

 

                                } else {  // text field

                                        valMA[valFieldCount][7] = eval('thisform.'+valFieldName+'.value');

                                }

 

                                if(debug1){

                                        alert(valFieldName+' = ('+valMA[valFieldCount][7]+')');

                                        alert('pmRequired == '+isRequired);

                                        alert('notEmpty(fieldVal) == '+notEmpty(valMA[valFieldCount][7]));

                                }

 

                                valFieldCount++;

 

                        } // end if -- corresponding field found

 

                } // end if -- 'validate_' hidden field found

 

        } // end loop over fields - multi array valMA[valFieldCount][x] created.

 

 

        // VALIDATION BEGINS ----------------------------------------------------

        if(debug2){alert(' ================ VALIDATION BEGINS ================ ')}

 

        // NOW LOOP again, OVER MULTI ARRAY ---- valMA[g][x]

        for ( var g=0; g < valFieldCount; g++ ){

 

                // set friendly variable names - see if you can use associative names, ie. valMA[g]["value"]

                pmField         = valMA[g][0]; // fieldname (pmField)

                pmCriterion         = valMA[g][1]; // criterion

                pmRequired         = valMA[g][2]; // isRequired

                pmType                 = valMA[g][3]; // type

                custmsg         = valMA[g][4]; // hidden param "custmsg_xxx"

                isSelectedCount = valMA[g][5]; // isSelectedCount

                subFields        = valMA[g][6]; // subFields - (depricated)

                fieldVal         = valMA[g][7]; // value

 

                if(debug2){alert(g+': field | type | val | reqd == '+pmField+' | '+pmType+' | '+fieldVal+' | '+pmRequired)}

 

 

                // set default warning messages

                switch (pmCriterion.substr(0,4)){ //  validate criterion

                                                                        case 'rege': // regex

                                defaultmsg ='Please enter a valid value.';

                                validChars = '';

                                                                                                break;

                        case 'name':

                                defaultmsg ='Please enter a valid name.';

                                validChars ='letters, dashes, commas, periods, or apostrophes';

                                break;

                        case 'addr':

                                defaultmsg ="Please enter a valid address.";

                                validChars ='alphanumeric characters and optional commas, periods,\ndashes, slashes, apostrophes, parentheses, or pound signs';

                                break;

                        case 'pric':

                                defaultmsg ="Please enter a valid dollar value.";

                                validChars ='numbers and optional commas, a decimal point, and/or a dollar sign';

                                break;

                        case 'card':

                                defaultmsg ='Please enter a valid Credit Card number.';

                                validChars ='numbers and optional dashes';

                                break;

                        case 'emai':

                                defaultmsg ='Please enter a valid email address. ';

                                validChars ='letters, periods, the @ symbol, and/or numbers in form: user@domain.com';

                                break;

                        case 'soci':

                                defaultmsg ='Please enter a valid social security number.';

                                validChars ='numbers and optional dashes';

                                break;

                        case 'phUS':

                                defaultmsg ='Please enter a valid phone number.';

                                validChars ='numbers, optional parentheses and optional dashes';

                                break;

                        case 'numb':

                                defaultmsg ='Please enter a valid number.';

                                validChars ='numeric characters';

                                break;

                        case 'fnum':

                                defaultmsg ='Please enter a valid number.';

                                validChars ='numeric characters and dot (.)';

                                break;

                        case 'zipc':

                                defaultmsg ='Please enter a valid US zip code.';

                                validChars ='numeric characters';

                                break;

                        case 'pass':

                                defaultmsg ='Please enter a valid Password.';

                                validChars ='letters and numbers, and do not include your username. \nAdditionally, passwords cannot contain common words such as guest, test, user, or password';

                                break;

                        case 'uname':

                                                defaultmsg ='Please enter a username between 4 and 15 characters in length which consists of only numbers and at least one letter.';

                                validChars ='letters and numbers';

                                                break;

                        default :

                                if (pmType.substr(0,1) == 't'){

                                   defaultmsg ='Please make a valid entry.';

                                } else {

                                   defaultmsg ='Please make a selection.';

                                }

                                validChars ='valid characters';

                }

 

                if (custmsg == ""){

                        custmsg = defaultmsg;        // set default message.

                }

 

 

                // check if isRequired

                if ( (pmRequired == 1) && (notEmpty(fieldVal)==false)  ){

 

                        alert(custmsg);

 

                        typeShort = pmType.substr(0,1); // set focus by Type

                        if (typeShort == 't'){          // text,textarea

                                eval('thisform.'+pmField+'.focus();');

 

                        } else if (typeShort == 's'){         // select-one,select-multiple,submit

                                eval('thisform.'+pmField+'.focus();');

 

                        } else { // 'r' or 'c'                 // reset,password,button,hidden,file

                                if (subFields){

                                        eval('thisform.'+pmField+'[0].focus();');

                                } else {

                                        eval('thisform.'+pmField+'.focus();');

                                }

                        }

                        return false;

                }

 

                // NULL VALUES ARE ACCEPTABLE PAST THIS POINT!!!

                // ONLY PERFORM VALIDATION ON NON-EMPTY ENTRIES!!!

 

 

                custmsgPleads = false;

                custmsgPleadsStr = custmsg.substr(0,6);                  // check if starts with "Please"

                custmsgPleadsStr = custmsgPleadsStr.toLowerCase();

                if (custmsgPleadsStr == 'please'){

                        custmsgPleads = true;

                }

 

 

                if (notEmpty(fieldVal)){

 

                        alertStr = custmsg + "\n";  // start building 2 line warning.

 

                        // check if optional Length :Min:Max values given

                        minPos = pmCriterion.indexOf(':');

                        maxPos = pmCriterion.lastIndexOf(':');

                        if ((minPos != -1) && (maxPos != -1) && (maxPos - minPos > 1)){

                                minLen = pmCriterion.substring(minPos+1, maxPos);

                                maxLen = pmCriterion.substring(maxPos+1, pmCriterion.length);

                                if(debug2) { alert('min|max = '+minLen+'|'+maxLen) }

                                if (isNumeric(minLen) && isNumeric(maxLen)){

 

                                        validLen = true;  // assume good until test fails

                                        textEntry = true; // assume text

 

                                        if ((pmType == 'select-multiple') && ((isSelectedCount < minLen) || (isSelectedCount > maxLen))){

                                                textEntry = false;

                                                validLen = false;

                                                noun = 'options';

                                        } else {

                                                if (!isValidLength(fieldVal, minLen, maxLen)){

                                                        validLen = false;

                                                        noun = validChars;

                                                }

                                        }

                                        if (!validLen){

 

                                                if (custmsgPleads){                    // don't beg.

                                                        if(textEntry){

                                                                verb = 'Enter';

                                                        } else {

                                                                verb = 'Choose';

                                                        }

                                                } else {

                                                        alertStr += "Please ";

                                                        if(textEntry){

                                                                verb = 'enter';

                                                        } else {

                                                                verb = 'choose';

                                                        }

                                                }

 

                                                if(maxLen - minLen == 0){

                                                        alertStr += verb+" "+maxLen+" "+noun+".";

                                                } else {

                                                        alertStr += verb+" between "+minLen+" and "+maxLen+" "+noun+".";

                                                }

                                                alert(alertStr);

                                                eval('thisform.'+pmField+'.focus();');

                                                return false;

                                        }

                                }

                        }

 

 

 

                        // Now, the second line of warning message will prompt for valid characters if necessary.

                        if (custmsgPleads && validChars != ''){

                                alertStr += "Use only "+validChars+".";    // don't beg.

                        } else if(validChars != '') {

                                alertStr += "Please use only "+validChars+".";

                        }

 

                        if(debug2){alert('pmCriterion = '+pmCriterion)}

 

                        switch (pmType.substr(0,1)){ //  validate by Type

                        // NOTE --- this switch might be eliminated - TYPE may be unimportant at this stage...

 

                                // ----------------------- type = text

                                case 't':

 

                                        switch (pmCriterion.substr(0,4)){ //  validate criterion

                                                                                                                                                case 'rege': // regex

                                                                                                                                                                        var index = pmCriterion.indexOf(':');

                                                                                                                                                                        if(index == -1)

                                                                                                                                                                        {

                                                                                                                                                                                    if(debug1 || debug2 || debug3)

                                                                                                                                                                                    {

                                                                                                                                                                                                alert('DEBUG: Please supply value for regex for validation of field "'+pmField+'"');

                                                                                                                                                                                    }

                                                                                                                                                                                    return false;

                                                                                                                                                                        }

                                                                                                                                                                        

                                                                                                                                                                        // Get the regular expression

                                                                                                                                                                        var regex = pmCriterion.substr(index+1);

                                                                                                                                                                        if(!isValidRegexMatch(regex,fieldVal))

                                                                                                                                                                        {

                                                            alert(alertStr);

                                                            eval('thisform.'+pmField+'.focus();');

                                                                                                                                                                                    return false;

                                                                                                                                                                        }

 

                                                                                                                                                                        break;

                                                case 'name':

                                                        if (!isName(fieldVal, 1)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'addr':

                                                        if (!isAddress(fieldVal, 1)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'pric':

                                                        if (!isPrice(fieldVal, 1)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        if (!isPriceFormatted(fieldVal)){

                                                                alert(custmsg+'\n... Double check the value you entered.\n... You may have misplaced a comma or a decimal point.');

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'card':

                                                        // the hidden field "validate_sys_CardTypeField" MUST be present in each form

                                                        // where credit card validation occurs. Its value is the name of the selectbox

                                                        // field containing the card type.

                                                        if(validateCreditCard){

                                                                CardTypeField = thisform.validate_sys_CardTypeField.value;

                                                                if(debug2) {alert('CardTypeField = '+CardTypeField)}

                                                                CardType = eval('thisform.'+CardTypeField+'.options[thisform.'+CardTypeField+'.selectedIndex].value');

                                                                if(debug2) {alert('CardType = '+CardType)}

                                                                if (!isValidCreditCard(fieldVal,CardType)){

                                                                        alert(alertStr);

                                                                        eval('thisform.'+pmField+'.focus();');

                                                                        return false;

                                                                }

                                                        }

                                                        break;

                                                case 'emai':

                                                        if (!validateEMail(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'soci':

                                                        if (!validateSSN(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'phUS':

                                                        if (validateUSphone){

                                                                selectedCountry = thisform.Country.options[thisform.Country.selectedIndex].value;

                                                                if(debug2) {alert('selectedCountry= '+selectedCountry)}

                                                                selectedCountry = strip('.',selectedCountry);

                                                                if(debug2) {alert('selectedCountry= '+selectedCountry)}

                                                                selectedCountry = selectedCountry.substring(0,13);

                                                                if(debug2) {alert('selectedCountry= '+selectedCountry)}

                                                                if(!((selectedCountry == 'US')||(selectedCountry == 'USA')||(selectedCountry == 'United States'))){

                                                                        validateUSphone = false;

                                                                }

                                                        }

                                                        if (validateUSphone && !isValidUSPhone(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'numb':

                                                        if (!isNumeric(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'fnum':

                                                        if (!isfNumeric(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'zipc':

                                                        if (!validateZip(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                        break;

                                                case 'pass':

                                                        // the hidden field "validate_sys_UserNameField" MUST be present in each form

                                                        // where password validation occurs. Its value is the name of the text field

                                                        // containing the username.

                                                        if(validateCreditCard){

                                                                UserNameField = thisform.validate_sys_UserNameField.value;

                                                                if(debug2) {alert('UserNameField = '+UserNameField)}

                                                                UserName = eval('thisform.'+UserNameField+'.value');

                                                                if(debug2) {alert('UserName = '+UserName)}

                                                                if (!isValidPassword(fieldVal,UserName,0)){

                                                                        alert(alertStr);

                                                                        eval('thisform.'+pmField+'.focus();');

                                                                        return false;

                                                                }

                                                                if(validateConfirmPassword){  // optional check if user re-entered correct password (confirmed it)

                                                                        ConfirmPasswordField = thisform.validate_sys_ConfirmPasswordField.value;

                                                                        if(debug2) {alert('ConfirmPasswordField = '+ConfirmPasswordField)}

                                                                        ConfirmPassword = eval('thisform.'+ConfirmPasswordField+'.value');

                                                                        if(debug2) {alert('ConfirmPassword = '+ConfirmPassword)}

                                                                        if (fieldVal != ConfirmPassword){

                                                                                alert('Your password doesn\'t match the confirmed password. Please re-enter both.');

                                                                                eval('thisform.'+ConfirmPasswordField+'.value = "";');

                                                                                eval('thisform.'+pmField+'.value = "";');

                                                                                eval('thisform.'+pmField+'.focus();');

                                                                                return false;

                                                                        }

                                                                }

                                                        }

                                                        break;

                                                case 'unam':

                                                                        if (!validateUsername(fieldVal)){

                                                                alert(alertStr);

                                                                eval('thisform.'+pmField+'.focus();');

                                                                return false;

                                                        }

                                                                        break;

                                                default :

                                                        if(debug2) {alert('unknown validationCODE criterion for type: text')}

                                        }

                                        break;

 

                                // ----------------------- type = select

                                case 's':

                                        // implement

                                        break;

                                case 'r':

                                        // implement

                                        break;

                                case 'c':

                                        // implement

                                        break;

                                // ----------------------- type = password

                                case 'p':

 

                                        switch (pmCriterion.substr(0,4)){ //  validate criterion

 

                                                case 'pass':

                                                        // the hidden field "validate_sys_UserNameField" MUST be present in each form

                                                        // where password validation occurs. Its value is the name of the text field

                                                        // containing the username.

                                                        if(validatePassword){

                                                                UserNameField = thisform.validate_sys_UserNameField.value;

                                                                if(debug2) {alert('UserNameField = '+UserNameField)}

                                                                UserName = eval('thisform.'+UserNameField+'.value');

                                                                if(debug2) {alert('UserName = '+UserName)}

                                                                if (!isValidPassword(fieldVal,UserName,0)){

                                                                        alert(alertStr);

 

                                                                        eval('thisform.'+pmField+'.focus();');

                                                                        return false;

                                                                }

                                                                if(validateConfirmPassword){  // optional check if user re-entered correct password (confirmed it)

                                                                        ConfirmPasswordField = thisform.validate_sys_ConfirmPasswordField.value;

                                                                        if(debug2) {alert('ConfirmPasswordField = '+ConfirmPasswordField)}

                                                                        ConfirmPassword = eval('thisform.'+ConfirmPasswordField+'.value');

                                                                        if(debug2) {alert('ConfirmPassword = '+ConfirmPassword)}

                                                                        if (fieldVal != ConfirmPassword){

                                                                                alert('Your password doesn\'t match the confirmed password. Please re-enter both.');

                                                                                eval('thisform.'+ConfirmPasswordField+'.value = "";');

                                                                                eval('thisform.'+pmField+'.value = "";');

                                                                                eval('thisform.'+pmField+'.focus();');

                                                                                return false;

                                                                        }

                                                                }

                                                        }

                                                        break;

                                                default :

                                                        if(debug2) {alert('unknown validationCODE criterion for type: password')}

                                        }

                                        break;

                                default :

                                        if(debug2){alert('unknown validationCODE type')}

                        }

 

                } // end if(notEmpty())

 

        } // end loop over multi-array valMA[g][x]

 

        if(debugSubmit) {

            alert('submitting');

        }

        return true;

}

 

// Support Functions

function strip(filter,str){

        var i,curChar;

        var retStr = '';

        var len = str.length;

        for(i=0; i<len; i++){

                curChar = str.charAt(i);

                if(filter.indexOf(curChar)<0) //not in filter, keep it

                        retStr += curChar;

        }

        return retStr;

}

function reformat(str){

        var arg;

        var pos = 0;

        var retStr = '';

        var len = reformat.arguments.length;

        for(var i=1; i<len; i++){

                arg = reformat.arguments[i];

                if(i%2==1)

                        retStr += arg;

                else{

                        retStr += str.substring(pos, pos + arg);

                        pos += arg;

                }

        }

        return retStr;

}

//End Support Functions

//Validation Rules

function notEmpty(str){

        if(strip(" \n\r\t",str).length ==0)

                return false;

        else

                return true;

}

function validateInteger(str){

        str = strip(' ',str);

        //remove leading zeros, if any

        while(str.length > 1 && str.substring(0,1) == '0'){

                str = str.substring(1,str.length);

        }

        var val = parseInt(str);

        if(isNaN(val)){

                return false;

        } else {

                 // if(debug3) {alert(val)}

                return true;

        }

}

function validateFloat(str){

        str = strip(' ',str);

        //remove leading zeros, if any

        while(str.length > 1 && str.substring(0,1) == '0'){

                str = str.substring(1,str.length);

        }

        var val = parseFloat(str);

        if(isNaN(val))

                return false;

        else

                return true;

}

function isValidUSPhone(str){

        str = strip("*() -./_\n\r\t\\",str);

        if(str.length == 10) // || str.length == 7  ***commented out - must always include area code

                return true;

        else

                return false;

}

function validateSSN(str){

        str = strip(" -.\n\r\t",str);

        if(validateInteger(str) && str.length == 9)

                return true;

        else

                return false;

}

 

/**

 * This method validates the username field. 

 * up to this point, validation for min and max length has been done.

 * Only checks for at least 1 letter and an alphanumeric check is needed

 * 

 * @param string fieldVal

 * @return boolean

 */

function validateUsername(fieldVal)

{

            // first check to make sure we have at least 1 alpha char (not numeric)

            if (isNumeric(fieldVal))

            {

                        return false;

            }

            // validate for alphanumeric only

            if (!isAlphanumeric(fieldVal))

            {

                        return false;

            }

            return true;

}

 

function validateZip(str){

        str = strip("- \n\r\t",str);

        if(validateInteger(str)&&(str.length==9 || str.length==5))

                return true;

        else

                return false;

}

function validateCC(str,type){

        str = strip("-./_\n\r\t\\",str);

        if(type=="1")

                if(str.charAt(0)!="4")

                        return false;

        if(type=="2")

                if(str.charAt(0)!="5")

                        return false;

        if(type=="3")

                if(str.charAt(0)!="6")

                        return false;

        if(type=="4")

                if(str.charAt(0)!="3")

                        return false;

        if(validateInteger(str)&&((str.length==15&&type=="4") || str.length==16))

                return true;

        else

                return false;

}

function validateDate(str){

        var dateVar = new Date(str);

        if(isNaN(dateVar.valueOf()) || (dateVar.valueOf() ==0))

                return false;

        else

                return true;

}

function validateEMail(str){

        str = strip(" \n\r\t",str);

        if((str.indexOf("@")>-1 && str.indexOf(".")>-1) && (str.length > 7) && (str.lastIndexOf('.') < (str.length - 1)))

                return true;

        else

                return false;

}

//End Validation Rules

//Formatting functions

function formatPhone(str){

        str = strip("*() -./_\n\r\t\\",str);

        if(str.length==10)

                return reformat(str,"(",3,") ",3,"-",4);

        if(str.length==7)

                return reformat(str,"",3,"-",4);

}

function formatSSN(str){

        str = strip(" -.\n\r\t",str);

        return reformat(str,"",3,"-",2,"-",4);

}

function formatZip(str){

        str = strip("- \n\r\t",str);

        if(str.length==5)

                return str;

        if(str.length==9)

                return reformat(str,"",5,"-",4);

}

function formatCC(str,type){

        str = strip("-./_\n\r\t\\",str);

        switch(type){

                case "1":

                        return reformat(str,"",4,"-",4,"-",4,"-",4);

                        break;

                case "2":

                        return reformat(str,"",4,"-",4,"-",4,"-",4);

                        break;

                case "3":

                        return reformat(str,"",4,"-",4,"-",4,"-",4);

                        break;

                case "4":

                        return reformat(str,"",4,"-",6,"-",5);

        }

}

function formatDate(str,style){

        var dateVar = new Date(str);

        var year = dateVar.getYear();

        if(year<10)

                year += 2000;

        if(year<100)

                year += 1900;

        switch(style){

                case "MM/DD/YY":

                        return (dateVar.getMonth() + 1) + "/" + dateVar.getDate() + "/" + year;

                        break;

                case "DD/MM/YY":

                        return dateVar.getDate() + "/" + (dateVar.getMonth() + 1) + "/" + year;

                        break;

                case "Month Day, Year":

                        return getMonthName(dateVar) + " " + dateVar.getDate() + ", " + year;

                        break;

                case "Day, Month Day, Year":

                        return getDayName(dateVar) + ", " + getMonthName(dateVar) + " " + dateVar.getDate() + ", " + year;

                        break;

                default:

                        return (dateVar.getMonth() + 1) + "/" + dateVar.getDate() + "/" + year;

                        break;

        }

}

function formatReverse(str)

{

   var sReverse = "";

   for (var k = 0; k < str.length; k++)

   {

      sReverse = str.charAt(k) + sReverse;

   }

   return sReverse;

}

//End Formatting Functions

 

//-------more validation functions

// Check that the number of characters in a string is between a max and a min

function isValidLength(string, min, max) {

        if (string.length < min || string.length > max) return false;

        else return true;

}

 

// Check that a credit card number is valid based using the LUHN formula (mod10 is 0)

function isValidCreditCard(number) {

        number = '' + number;

        if(debug3) {alert(number)}

 

        if (number.length > 16 || number.length < 13 ) {

                 if(debug3) {alert('number out-of-bounds')}

                return false;

                }

        else if (getMod10(number) != 0) {

                 if(debug3) {alert('number failed mod10')}

                return false;

                }

        else if (arguments[1]) {

                var type = arguments[1];

                if(debug3) {alert(type)}

                var first2digits = number.substring(0, 2);

                var first4digits = number.substring(0, 4);

 

                if (type.toLowerCase() == 'visa' && number.substring(0, 1) == 4 &&

                        (number.length == 16 || number.length == 13 )) return true;

                else if (type.toLowerCase() == 'mastercard' && number.length == 16 &&

                        (first2digits == '51' || first2digits == '52' || first2digits == '53' || first2digits == '54' || first2digits == '55')) return true;

                else if (type.toLowerCase() == 'american express' && number.length == 15 &&

                        (first2digits == '34' || first2digits == '37')) return true;

                else if (type.toLowerCase() == 'diners club' && number.length == 14 &&

                        (first2digits == '30' || first2digits == '36' || first2digits == '38')) return true;

                else if (type.toLowerCase() == 'discover' && number.length == 16 && first4digits == '6011') return true;

                else if (type.toLowerCase() == 'enroute' && number.length == 15 &&

                        (first4digits == '2014' || first4digits == '2149')) return true;

                else if (type.toLowerCase() == 'jcb' && number.length == 16 &&

                        (first4digits == '3088' || first4digits == '3096' || first4digits == '3112' || first4digits == '3158' || first4digits == '3337' || first4digits == '3528')) return true;

                else return true; // change this to return false - after verifying this function works for ALL POSSIBLE CARD NUMBERS

        }

        else return true; // change this to return false - after verifying this function works for ALL POSSIBLE CARD NUMBERS

}

 

// Check that an email address is valid based on RFC 821 (?)

function isValidEmail(address) {

        if (address.indexOf('@') < 3) return false;

        var name = address.substring(0, address.indexOf('@'));

        var domain = address.substring(address.indexOf('@') + 1);

        if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1) return false;

        if (domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1) return false;

        return true;

}

 

 

// Check that a US zip code is valid

function isValidZipcode(zipcode) {

        zipcode = removeSpaces(zipcode);

        if (!(zipcode.length == 5) || !isNumeric(zipcode)) return false;

        return true;

}

 

 

// Check that a Canadian postal code is valid

function isValidPostalcode(postalcode) {

        if (postalcode.search) {

                postalcode = removeSpaces(postalcode);

                if (postalcode.length == 6 && postalcode.search(/^\w\d\w\d\w\d$/) != -1) return true;

                else if (postalcode.length == 7 && postalcode.search(/^\w\d\w\-d\w\d$/) != -1) return true;

                else return false;

        }

        return true;

}

 

// Check that a string contains only letters and numbers

function isAlphanumeric(string) 

{

            if (string.search) 

            {

                        // we are only allowing letters and numbers.

                if (string.search(/[^a-zA-Z0-9\s]/) != -1)

                        {

                                    return false;

                        }

            }

            return true;

}

 

 

// Checks that a value matches a given regular expression 

function isValidRegexMatch(regex,val)

{

            eval('var pattern = '+regex); // eg var pattern = /^[a-z]+/

            return pattern.test(val);

}

 

// ***** Check that a string contains only letters and numbers AND dashes, periods, commas, apostrophe's, and ()# characters.

//added allowance for slashes - Gabe - 8/31/04

function isAddress(string, ignoreWhiteSpace) {

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^\.\/\-\,\'\(\)\#\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\.\/\-\,\'\(\)\#\W/) != -1)) return false;

        }

        return true;

}

 

// ***** Check that a string contains only numbers AND $, periods, commas

function isPrice(string, ignoreWhiteSpace) {

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^\.\-\,\$\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\.\-\,\$\D/) != -1)) return false;

        }

        return true;

}

 

// ***** Check that a string contains a strong password that is alphanumeric and not based on username, no spaces, etc.

function isValidPassword(string,UserName,ignoreWhiteSpace) {

        if(debug3) {alert('in isValidPassword')}

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) {

                        if(debug3) {alert('password contains spaces and/or non-alphanumeric characters')}

                        return false;

                }

        }

        stringLower = string.toLowerCase();

        regex = "/"+UserName.toLowerCase()+"/";

        regexReverse = "/"+formatReverse(UserName.toLowerCase())+"/";

        //if((stringLower.search(regex) != -1) || (stringLower.search(regexReverse) != -1)){

        if((stringLower.search(UserName.toLowerCase()) != -1) || (stringLower.search(formatReverse(UserName.toLowerCase())) != -1)){

                if(debug3) {alert('password contains username')}

                return false; // can't contain username (forwards or backwards)

        }

        if( (stringLower.search(/guest/) != -1) || (stringLower.search(/password/) != -1) || (stringLower.search(/user/) != -1) || (stringLower.search(/test/) != -1)  ) {

                if(debug3) {alert('password contains common words')}

                return false; // can't contain common passwords

        }

        return true;

}

 

// ***** also, if there's a decimal point, commas, and/or a $ - make sure they're in the right places.

function isPriceFormatted(string) {

 

        str = strip(" ",string);

        if(    (str.indexOf("$") > 0) || ( (str.indexOf(".") > 0) && (str.indexOf(".") != str.lastIndexOf(".")) )) {

                return false;

        }

 

        str2 = str;

        commaPos = str2.indexOf(",");

        while (commaPos != -1)  {

                str2 = str2.substr(commaPos+1,str2.length - (commaPos+1))

                commaPos = str2.indexOf(",");

                if ((commaPos != -1) && (commaPos != 3)){

                        return false;

                }

        }

 

        if (str.indexOf(".") != -1) {  // there's a decimal

 

                decimalPosFromEnd = str.length - str.indexOf(".");

 

                // right number of decimal places?

                if (decimalPosFromEnd != 3) {

                        // return false;

                        // MODIFIED 05/29/02 ... allow 2 or more decimal places ...

                }

 

                // last comma in right place?

                if ( (str.indexOf(",") != -1) && (str.length - str.lastIndexOf(",") != decimalPosFromEnd + 4) ){

                        return false;

                }

 

        } else {   // no decimal

 

                // last comma in right place?

                if ( (str.indexOf(",") != -1) && (str.length - str.lastIndexOf(",") != 4) ){

                        return false;

                }

        }

 

        return true;

 

}

 

 

// Check that a string contains only letters

function isAlphabetic(string, ignoreWhiteSpace) {

 

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;

        }

        return true;

}

 

// *****  Check that a string contains only letters AND special name characters

function isName(string, ignoreWhiteSpace) {

 

        // if(debug3) {alert('in isName : '+string)}

 

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\.\-\,\'\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z\.\-\,\']/) != -1)) return false;

        }

        return true;

}

 

 

// Check that a string contains only numbers

function isNumeric(string, ignoreWhiteSpace) {

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;

        }

        return true;

}

 

// Check that a string contains only numbers and floats

function isfNumeric(string, ignoreWhiteSpace) {

        if (string.search) {

                if ((ignoreWhiteSpace && string.search(/[^\d\s\.]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;

        }

        return true;

}

 

// Remove characters that might cause security problems from a string

function removeBadCharacters(string) {

        if (string.replace) {

                string.replace(/[<>\"\'%;\)\(&\+]/, '');

        }

        return string;

}

 

// Remove all spaces from a string

function removeSpaces(string) {

        var newString = '';

        for (var i = 0; i < string.length; i++) {

                if (string.charAt(i) != ' ') newString += string.charAt(i);

        }

        return newString;

}

 

// Remove leading and trailing whitespace from a string

function trimWhitespace(string) {

        var newString  = '';

        var substring  = '';

        beginningFound = false;

 

        // copy characters over to a new string

        // retain whitespace characters if they are between other characters

        for (var i = 0; i < string.length; i++) {

 

                // copy non-whitespace characters

                if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {

 

                        // if the temporary string contains some whitespace characters, copy them first

                        if (substring != '') {

                                newString += substring;

                                substring = '';

                        }

                        newString += string.charAt(i);

                        if (beginningFound == false) beginningFound = true;

                }

 

                // hold whitespace characters in a temporary string if they follow a non-whitespace character

                else if (beginningFound == true) substring += string.charAt(i);

        }

        return newString;

}

 

// Returns a checksum digit for a number using mod 10

function getMod10(number) {

 

        // convert number to a string and check that it contains only digits

        // return -1 for illegal input

        number = '' + number;

        number = removeSpaces(number);

        if (!isNumeric(number)) return -1;

 

        // calculate checksum using mod10

        var checksum = 0;

        for (var i = number.length - 1; i >= 0; i--) {

                var isOdd = ((number.length - i) % 2 != 0) ? true : false;

                digit = number.charAt(i);

 

                if (isOdd) checksum += parseInt(digit);

                else {

                        var evenDigit = parseInt(digit) * 2;

                        if (evenDigit >= 10) checksum += 1 + (evenDigit - 10);

                        else checksum += evenDigit;

                }

        }

        return (checksum % 10);

}

 

//--end more validation functions
// JavaScript Document