//
// called as a result of a __check field,
// verifies that the value matches a regular expression
//
function regexpCheck(_objname,_regexp,_error,_obj)
{
    // find the object in the form
    //alert("regexpCheck "+_textname+" "+_regexp+" "+_error+" "+_obj.name);

    //create a regular expression object
    var re = new RegExp(_regexp);

    //get the name of the object
    var name = "document.forms['"+_obj.form.name+"']."+_objname;

    //get the object handle
    var obj = this.eval(name);

    //if the object doesnt exist, return 'success'
    if (obj == null) return "";

    //do regular expression matching on the text
    var matchArray = re.exec(obj.value);

    //if there were no matches, return the error string
    if (!matchArray) return _error;

    //else return 'success'
    return "";
}

//
// checks for confirmation from the user
// also checks all fields with name__check hidden fields
// if the field exists, a call to its corresponding "check()"
// function is made to verify it is appropriately populated
//
function checkValidity(_formname, _confirm) {
   // an accumulation of error strings
   var errorText = "";

   // if a  confirm string is not specified, then dont bother the user
   if (_confirm!="") doit=false;
   else doit=true;

   // check with the user to see if we can proceed.
   if (_confirm && confirm(_confirm)) {
    doit=true;
   }

   // if all is well, check each field for its assiciated __check hidden field
   if (doit==true)
   {
        // check each field in the form to see if it has a __check hidden field
        for(var i=0; i<document.forms[_formname].elements.length; i++)
        {
            var checkobj = document.forms[_formname].elements[i];  // the current object to check
            var objname = checkobj.name; // the current object's name
            var s = objname.substring(objname.length-7,objname.length);

            // does the name end in __check? if so, persue further
            if (s=="__check")
            {
                var testObjName = objname.substring(0,objname.length-7);
                // the value of the __check object is parameter to validate the object
                var cmd = "regexpCheck('"+testObjName+"',"+checkobj.value+",checkobj)";
                var errorString = eval(cmd);
                if (errorString != "") errorText = errorText + errorString + "\n";
             }
        }

        if (errorText != "")
        {
            alert("Some problems were found in the form\n\n"+errorText);
            return false;
        }

        return true;
   }
   return false;
}


