	/* Usage - place into form's "onsubmit" handler as follows:
	  onsubmit="this.Email.email = true;
							this.Title.optional = true;
							this.DaysToKeep.numeric = true;
							return verify(this);"
		*** NOTE *** that the title field should be filled in on all fields that will be checked.
		Otherwise Google supplants it with its fill-in message
	*/
// A utility function that returns true if a string contains only whitespace characters.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
// Parameters are: f       - form (string or object)
//								 errorCallback - document element to show errors (optional)
//								 pw1Name 			 - optional first password field name
//								 pw2Name 			 - .. and second password for comparison
verify.radios = new Array();
verify.radioNames = new Array();
function verify(f, errorCallback, pw1Name, pw2Name) {
	var msg = "";
	var empty_fields = "";
	var errors = "";
	var doPw = (pw1Name && pw2Name),
			password1 = "";
	var errorNl = (errorCallback) ? "</div>" : "\n";	// specify new line according to type of message
	var errorSt = (errorCallback) ? "<div class='verify'>" : "";
	
	f = getEl(f);		// make sure DOM object
	debug ("verifying", 1);
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // verify that they are numbers and in the right range.
    // If the element has a "numeric" property defined, verify that
    // it is a number, but don't check its range.
    // Put together error messages for fields that are wrong.
    for (var i = 0; i < f.length; i++) {
        var e = f.elements[i];
				// debug (e.name + ", type " + e.type + ", value " + e.value, 1);
				// debug (e.name + " optional " + e.optional + " value " + e.value, 1); 
				if (e.type == "radio") {			// must gather list of radio elements then verify one is is checked
					var er = getEl(e.name);			// check if identical id has been defined
					// if (er) debug (e.name + " id is defined, optional is " + er.optional, 1);
					if (!e.optional) {
						if (verify.radios[e.name]==null) {				// doesn't exist
							// debug ("Adding radio " + e.name, 1);
							verify.radios[e.name] = 0;		// initialise false
							verify.radioNames[e.name] = ((e.title) ? e.title : e.name);
						}
						if (er && er.optional) {
							// debug (e.name + " is optional", 1);
							verify.radios[e.name] = 1;
						}
						else if (e.checked)
							verify.radios[e.name] = 1;
						isEmpty = false;				// leave for now + check at the end
					} // if (!e.optional}
				}
				else if (e.type == "select-one") {			// *** need to generalise
					// debug ("SELECT.value is " + e.value, 1);
					// debug ("SELECT[" + e.selectedIndex + "] is " + e.options[e.selectedIndex].value + "; ", 1);
					isEmpty = (e.value < 0);							// *** assume -ve value for not assigned - DOCUMENT this
					/*
					debug ("         SELECT  " + e.selectedIndex + " is " + e.options[e.selectedIndex].value + "; ", 0);
					debug ("", 1);
					*/
				}
				else
					isEmpty = ((e.value == null) || (e.value == "") || isblank(e.value));
        if (((e.type == "text") || (e.type == "textarea") || (e.type == "radio") || (e.type == "select-one")) && !e.optional) {
            // first check if the field is empty
            if (isEmpty) {
                empty_fields += ((errorCallback) ? "" : errorNl) + "          '" + ((e.title) ? e.title : e.name) + "'";
                continue;
            }
        } // non-optional
				
			// now check for valid e-mail
			if (!isEmpty && (e.email != null)) {
			  if ((e.value.indexOf('@') < 1) || (e.value.indexOf('@') == (e.value.length-1)))
			    errors += errorSt + "- e-mail address not properly formed." + errorNl;
			}
			
			// Now check for fields that are supposed to be numeric.
			if (!isEmpty && (e.numeric || (e.min != null) || (e.max != null))) { 
					var v = Number(e.value);
					if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) {
							errors += errorSt + "- The field " + ((e.title) ? e.title : e.name) + " must be a number";
							if (e.min != null) 
									errors += " that is greater than " + e.min;
							if (e.max != null && e.min != null) 
									errors += " and less than " + e.max;
							else if (e.max != null)
									errors += " that is less than " + e.max;
							errors += "." + errorNl;
					}
			} // numeric
			/* */
			// Check for password
			if (doPw && ((e.type == "password"))) {
				if (e.name == pw1Name) { // first password - store it
					password1 = e.value;
				}
				else if (e.name == pw2Name)				// compare 2nd with first
					if (e.value != password1) {
						errors += errorSt + "- 'password' and 'confirm password' fields should be identical - remember they are case-sensitive." + errorNl;
					}
				} // if (doPw)
			/* */

    } // for i
	
		// check up on our radios
		for (var rn in verify.radios		) {
			// debug ("checking verify.radios[" + rn + "] value = " + verify.radios[rn], 1);
			if (!verify.radios[rn]) {
				empty_fields += ((errorCallback) ? "" : errorNl) + "          '" + verify.radioNames[rn] + "'";
			}
				// empty_fields += ((errorCallback) ? "" : errorNl) + "          '" + ((e.title) ? e.title : e.name) + "'";
		}

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) {
			// form1.Username.disabled = false;		// so we transmit field to server
			if (errorCallback) {
				// setElHTML(errorCallback, "");			// clear errors
				errorCallback();
			}
			debug ("returning TRUE");
    	return true;
    }

		if (!errorCallback) {
			msg  = "______________________________________________________\n\n"
			msg += "The form was not submitted because of the following error(s).\n";
			msg += "Please correct these error(s) and re-submit.\n";
			msg += "______________________________________________________\n\n"
		}

    if (empty_fields) {
        msg += errorSt + "- The following required field(s) are empty:" 
                + empty_fields + errorNl;
        if (errors && !errorNl) msg += "\n";
    }
    msg += errors;
		if (errorCallback)
			// setElHTML(errorCallback, msg);
			errorCallback(msg);
		else
	    alert(msg);
		debug ("returning FALSE");
    return false;
}// verify


