/**
 * DDformValidation
 * 
 * This class handles form element validation, forcing required fields, displaying error messages,
 * and swapping CSS for element 'state'
 * 
 * @author James Rodenkirch
 * @copyright 2006 Delta Dental of Wisconsin
 */

function addSlash(fieldName) {
    var re = /^(\d{2})(\d{2})(\d{4})$/;
    if (fieldName.value.match(re)) {
        var newDate = fieldName.value.match(re);
        var slashDate = newDate[1] + "/" + newDate[2] + "/" + newDate[3];
        fieldName.value = slashDate;
        //fieldName.style.backgroundColor = "#FFFFFF";
        fieldName.className = 'input';
    }
}

function DDformValidation() 
{
	/**
	 * class attribute to hold the current value of an element. This is set onFocus and checked onBlur
	 */
	this.currentValue = '';
	
	/**
	 * class attribute to hold the current CSS of an element. This is set onFocus and checked onBlur
	 */
	this.currentCSS = '';
	
	/**
	 * class attribute to track if the submit button has been used. Validation of elements on blur
	 * only works after the initial form validation
	 */
	this.secondaryCheck = false;
	
	/**
	 * class attribute to hold an array of errors. This is used to track the number of current errors
	 * in order to hide/show the main error alert span.
	 */
	this.errors = new Array();
	
	/**
	 * class attribute to hold a pointer to the form being processed
	 */
	this.formElement = '';
	
/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to trigger onFocus events
	 * 
	 * @param {DOM} myElement This is the DOM item to apply this function to
	 */
	this.focusWidget = function (myElement) 
	{
		// save current value of the element
		this.currentValue = myElement.value;
		
		// save current CSS
		this.currentCSS = myElement.className;
		
		// swap class
		this.swapClass(myElement, 'focus');
	};

	/**
	 * method to trigger onBlur events
	 * 
	 * @param {DOM} myElement This is the DOM item to apply this function to
	 */
	this.blurWidget = function (myElement) 
	{
		// if the value has changed we can process the data
		if (myElement.value != this.currentValue) {
			
			// default to valid
			valid = true;
			
			// if this is a secondary check, re-validate the data
			if (this.secondaryCheck) {
				valid = this.validateValue(myElement,myElement.getAttribute('validate'));
			}
			
			// if the data is still valid after the possible re-validation, hide the error
			if (valid) {
				this.hideError(myElement);
			}
		}
		
		// if the value has not changed, revert to pre-onFocus css
		else {
			if (this.currentCSS.indexOf("_") != -1) {
				temp = this.currentCSS.split('_');
				this.currentCSS = temp[1];
			}
			else {
				this.currentCSS = '';
			}
			
			this.swapClass(myElement, this.currentCSS);
		}
	};
	
/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to initialize form elements to a 'required' status
	 * 
	 * @param {DOM} myForm This is the form that is being processed
	 */
	this.initializeForm = function (myForm) 
	{
		// grab all items in this form
	    items = myForm.elements;
		
		// grab all <label>s
		labels = document.getElementsByTagName('label');
		
		// loop through each of the items in this form
	    for (var i=0; i < items.length; i++) {
	        // if this item has an attribute of 'required'
	        if (items[i].getAttribute('required')) {
				// swap css to {baseClass}_required
				this.swapClass(items[i], 'required');
				
				// check for matching label and add an '*' to the text
				labelKey = this.inArray(items[i].id,labels);
				if (labelKey >= 0) {
					labels[labelKey].innerHTML = '* ' + labels[labelKey].innerHTML;
				}
			}
			
			// if this item is disabled
			if (items[i].getAttribute('disabled')) {
				// swap css to {baseClass}_required
				this.swapClass(items[i], 'disabled');
			}
		}
	};

/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to change the css of an element
	 * 
	 * @param {DOM} myElement This is the element that will be altered
	 * @param {string} myClass This is the class type to change to.
	 */
	this.swapClass = function (myElement, myClass) 
	{
		// find the current class of the element
		currentClass = myElement.className;
		
		// find the base class name
		if (currentClass.indexOf("_") != -1) {
			temp = currentClass.split('_');
			currentClass = temp[0];
		}
		
		// if we need to use something other than the base class, append the new style
		if (myClass != '') {
			myClass = '_' + myClass;
		}
		
		// change the class
		myElement.className = currentClass + myClass;
	};

/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to handle validation of the form. It controls validating each individual element
	 * 
	 * @param {DOM} myForm This is the form that will be processed
	 */
	this.validateForm = function (myForm) 
	{
		// save form into instance
		this.formElement = myForm;
		
		// specify that the submit button has been pressed
		this.secondaryCheck = true;
		
		valid = true;
		
		// grab all items in this form
	    items = myForm.elements;
		
		// loop through each of the items in this form and check for required fields
	    for (var i=0; i < items.length; i++) {
	        // if this item has an attribute of 'required' and is empty and it is not disabled
	        if (items[i].getAttribute('required') && items[i].value == '' && !items[i].getAttribute('disabled')) {
				this.showError(items[i],'You must enter a value in this field');
				valid = false;
			}
		}
		
		// if we have entered all data for the required fields, validate the pieces.
		if (valid){
			for (var i=0; i < items.length; i++) {
				
				// if the element has a 'validate' attribute and it is not disabled, do the validation
				temp = true;
				if (items[i].getAttribute('validate') && !items[i].getAttribute('disabled')) {
					temp = this.validateValue(items[i],items[i].getAttribute('validate'));
				}
				
				// if validation was not successful
				if (!temp) {
					valid = false;
				}
			}
		}
		
		// if it is not valid, either from required or validation, redirect to top of form.
		if (!valid) {
			document.location.href = '#errorSpan_DDerror';	
		}
	
		//return valid status
		return valid;
	};

/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to handle validation of the form. It controls validating each individual element
	 * 
	 * @param {DOM} myElement This is the element that will be validated
	 * @param {string} myType This is the type of validation to process
	 */
	this.validateValue = function (myElement,myType)
	{
		// default status to valid
		var valid = true;
		
		// initialize the error message
		var errorMsg = '';
		
		if (!myElement.getAttribute('disabled')) {
			// process based on type
			switch (myType) {
				
				/**
				 * username must match the following criteria:
				 * 1) must start with a letter
				 * 2) can only contain [a-z] [0-9] . - _
				 * 3) must be at least 6 characters long
				 */
				case 'username':
					// build the regular expression
					var regEx = new RegExp(/^[a-z][a-z0-9\.\-\_]+$/i);
					
					// test the pattern
					if (!myElement.value.match(regEx)) {
						errorMsg = 'Invalid username';
				        valid = false;
		    		}
					
					// test the string length
					if (myElement.value.length < 6) {
						valid = false;
		    		}
					
		        	break;
					
				/**
				 * passwords must match the following criteria
				 * 1) must contain at least 3 of the following types of characters 
				 * 	  [a-z] [A-Z] [0-9] [^a-zA-Z0-9]
				 * 2) must be at least 8 characters long
				 */
				case 'password':
					// initialize a counter for storing positive type matches
					var categoryCount = 0;
					
					// test for uppercase letters
					var re = /[A-Z]/;
					if (re.test(myElement.value)) { categoryCount++; }
					
					// test for lowercase letters
					var re = /[a-z]/;
					if (re.test(myElement.value)) { categoryCount++; }
					
					// test for digits
					var re = /\d/;
					if (re.test(myElement.value)) { categoryCount++; }
					
					// test for special characters
					var re = /[^a-zA-Z0-9]/;
					if (re.test(myElement.value)) { categoryCount++; }
					
					// make sure that there were at least 3 categories used
					if (categoryCount < 3) {
						errorMsg = 'Invalid password: You must use characters from 3 of the 4 catagories';
					    valid = false;
					}
					
					// test the string length
					if (myElement.value.length < 8) {
						errorMsg = 'Invalid password: Your password must be at least 8 characters long';
					    valid = false;
					}
			
		        	break;
				
				/**
				 * emails must match the following pattern
				 */
				case 'email':
					// build the regular expression
					regEx = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
					
					// test the pattern
				    if (!myElement.value.match(regEx)) {
						errorMsg = 'Invalid email';
				        valid = false;
				    }
		        	break;
			}
		}
		
		// if the validation was not successful, show the generated error
		if (!valid) {
			this.showError(myElement,errorMsg);
		}
		
		// return the valid status
		return valid;
	};

/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to show generated errors 
	 * 
	 * @param {DOM} myElement This is the element that is in error
	 * @param {string} errorMsg This is the error message to display
	 */
	this.showError = function (myElement,errorMsg) 
	{
		// add the element to the error array
		this.errors[myElement.id] = true;
		
		// change the css to alarm
		this.swapClass(myElement, 'alarm');
		
		// display the error if the <span> exisits
		if (document.getElementById('errorSpan_' + myElement.name)) {
			newDomElement = document.getElementById('errorSpan_' + myElement.name);
			
			newDomElement.innerHTML = errorMsg + "<br />";
		}
		
		// create the <span> and display the error if it does not exist
		else {
			// create span to display error
			newDomElement = document.createElement('span');
			newDomElement.setAttribute('id', 'errorSpan_' + myElement.name);
			newDomElement.innerHTML = errorMsg + "<br />";
			newDomElement.setAttribute('class', 'ddErrorSpan');
			newDomElement.setAttribute('className', 'ddErrorSpan');						/* IE Fix */
			
			// insert new error span before the validated element
			myElement.parentNode.insertBefore(newDomElement, myElement);
		}
		
		// display a form level warning if the <span> exists
		if (document.getElementById('errorSpan_DDerror')) {
			newDomElement = document.getElementById('errorSpan_DDerror');
			newDomElement.innerHTML = 'There was an error on the form.';
		}
		
		// create the <span> and display the form level warning if it does not exist
		else {
			// create span to display error
			newDomElement = document.createElement('span');
			newDomElement.setAttribute('id', 'errorSpan_DDerror');
			newDomElement.innerHTML = errorMsg + "<br />";
			newDomElement.setAttribute('class', 'ddErrorSpan');
			newDomElement.setAttribute('className', 'ddErrorSpan'); 					/* IE Fix */
			
			// insert new error span before the validated element
			this.formElement.parentNode.insertBefore(newDomElement, this.formElement);
		}

	};
	
	/**
	 * method to hide generated errors 
	 * 
	 * @param {DOM} myElement This is the element that will be processed
	 */
	this.hideError = function (myElement) 
	{
		// remove the element from the error array
		this.errors[myElement.id] = false;
		
		// find the number of errors still outstanding
		var c = 0;
		for(var n in this.errors) {
			if (this.errors[n]) {
				c++;
			}
		}

		// change CSS back to the default status
		if (myElement.getAttribute('required')) {
			this.swapClass(myElement, 'required');
		}
		else {
			this.swapClass(myElement, '');
		}
		
		// remove error messages if <span> exists
		if (newDomElement = document.getElementById('errorSpan_' + myElement.name)) {
			newDomElement.innerHTML = '';
		}
		
		// remove form level warning if <span> exists and there are no more errors
		if (c == 0) {
			if (document.getElementById('errorSpan_DDerror')) {
				document.getElementById('errorSpan_DDerror').innerHTML = '';
			}
		}
	};
	
/* ---------------------------------------------------------------------------------------------- */

	/**
	 * method to search an array for a value and return the index if found.
	 * returns -1 if not found
	 * 
	 * @param {string} needle This is the value we are searching for
	 * @param {array} haystack This is the array of data that we will parse through
	 */
	this.inArray = function (needle,haystack) 
	{
		for (var i=0; i < haystack.length; i++) {
			// find array index
			if (tempIndex = haystack[i].getAttribute('for')) { }						/* IE Fix */
			else { tempIndex = haystack[i].getAttribute('htmlFor'); }
			
			// if the value matches, return the index
			if (tempIndex == needle) {
				return i;
			}
		}
		
		// if not found, return -1
		return -1;
	};
}

/* ############################################################################################## */
/* ### OLD METHOD ############################################################################### */
/* ############################################################################################## */

/**
 @fileoverview This javascript file is used to hold javascripts for processing form elements

 @author Aaron D Saray
 @copyright Delta Dental of Wisconsin

*/


/**
 focusIt is used when an element is focused and the css needs to be modified.

 Has defined types, on undefined, it sees if its a function.  If so, executes it.

 IMPORTANT NOTE: For this hardset styleSitching routine, your element must have a single class
 only.  It must also have className_focus as another class available.

 This uses switchStyle as one of the functions
 @see #switchStyle

 @param {DOM} item This is the DOM item to apply this function to
 @param {String} type This is the type of action to take on the item
*/
function focusIt(item, type)
{

    /**
    switch the type of action
    */
    switch (type) {

        /**
        case: style
        used for changing the class on this element
        */
        case 'style':

            /**
             call function to switch the class for this element
            */
            switchStyle(item, '_focus', '+');
            break;
    }

}


/**
 blurIt is used when an element is blurred and the css needs to be modified.

 has defined types.  on undefined, it sees if its a function.  if so, executes it

 This uses switchStyle as one of the functions
 @see #switchStyle

 @param {DOM} item This is the DOM item to apply this function to
 @param {String} type This is the type of action to take on the item
*/
function blurIt(item, type)
{

    /**
    switch the type of action
    */
    switch (type) {

        /**
        case: style
        used for changing the class on this element
        */
        case 'style':

            /**
             call function to switch the class for this element
            */
            switchStyle(item, '_focus', '-');
            break;

        /*
         default: checks for a function by this name
        */
        default:
            /*
             check to see if function
            */

            /*
              get index
            */
            start = type.indexOf('(');

            /*
             get function name
            */
            possibleFunction = type.substring(0, start);

            /*
             see if its a function
            */
            if (typeof eval(possibleFunction) == 'function') {
                eval(type);
            }

    }

}



/**
 switches style of item.

 this function is used to switch a class on an existing element's class automatically.
 used in place of psuedo classes, we have class modifications which require that mod
 to be setn to this function.

 for example:
 .inputBox {}
 .inputBox_focus{}

 call: switchStyle(this, '_focus', '+')
 <pre>
 this would add the _focus to the end of the existing class name, aplpying that style.
 it would not add another if its already on it.
 </pre>

 @param {DOM} item The DOM item to apply this to
 @param {String} classExtra The extra part to add/subtract from the class
 @param {String} action The Action to take on the item
*/
function switchStyle(item, classExtra, action)
{

    /*
     retrieve the class name of the current item we're referencing
     NOTE: this only works for one classed items
    */
    itemClass = item.className;


    /*
     switch the action, whethere we're trying to add something, or remove it
    */
    switch (action) {


        /*
         add our classExtra on
        */
        case '+':

            /*
             first make sure that the index is -1.  if it is, that means it doesn't exist in
             the classname already, so we're safe to append it

             if it DOES exist, we goto the else{} part and assign newClass the existing class
            */
            if (itemClass.indexOf(classExtra) == -1) {

                newClass = itemClass + classExtra;

            }
            else {
                newClass = itemClass;
            }
            break;


        /*
         delete our classExtra
        */
        case '-':

            /*
            see above comment - but basically we don't remove something thats not there
            */
            if (itemClass.indexOf(classExtra) !== -1) {
                newClass = itemClass.substring(0, itemClass.indexOf(classExtra));
            }
            else {
                newClass = itemClass;
            }

    }


    /*
     apply the newClass as the className
    */
    item.className = newClass;

}


/**
 formCheckRequired processes required items in a form

 This form is passed the form we're in, and follows through checking all required fields
 and validating their input.

 Uses validEmail as an e-mail type
 @see #validEmail

 @param {DOM} form The Form element
 @return Returns an array of items that are invalid with their error messages
 @type Array
*/
function formCheckRequired(form)
{

    /*
    grab all items in this form
    */
    items = form.elements;

    /*
    set invalid to a new array of items that would hold invalid items
    */
    var invalid = new Array();

    /*
    set the counter so we can reference keys
    */
    var invalidCount = 0;

    /*
    loop through each of the items in this form
    */
    for (var i=0; i < items.length; i++) {

        /*
        if this item has an attribute of 'required'
        */
        if (items[i].getAttribute('required')) {

            /*
             init our variable customError to blank
            */
            customError = '';

            /*
            retrieve a specialized error if there is one.
            */
            if (items[i].getAttribute('error')) {
                customError = items[i].getAttribute('error');
            }

            /*
            well first of all, we know it has to be a value, so why not do that right away
            */
            if (!items[i].value) {

                /*
                if no value, we'll add this item to our array, and then move the count up one
                */
                invalid[invalidCount] = items[i];
                invalidCount++;

                /*
                we check for a custom error message, or just use our standard one if none
                is set for this specific object.  Then, we set the error message TO the object
                */
                if (customError !== '') {
                    items[i].errorMessage = customError;
                }
                else {
                    items[i].errorMessage = 'Please enter a value';
                }
            }
            else {

                /*
                at least we have a value, lets go further
                */

                /*
                Required can be set to a type
                switch out that type now
                */
                switch (items[i].getAttribute('required')) {

                    /*
                     in the case of true, do nothing.  There is no verification needed
                    */
                    case 'true':
                        break;

                    /*
                    int, check for INT only - no special characters
                    */
                    case 'int':

                        if (items[i].value.match(/\D/)) {
                            /*
                            add to our array, increase the count, assign our error messages
                            */
                            invalid[invalidCount] = items[i];
                            invalidCount++;
                            if (customError !== '') {
                                items[i].errorMessage = customError;
                            }
                            else {
                                items[i].errorMessage = 'Please enter a numeric value with no special characters.';
                            }
                        }
                        break;



                    /*
                    numeric, check to see that the value is indeed a numeric item.  Keep in mind
                    the items' value itself is a string, so we'll never be able to do (typeof)
                    unless we were to cast it that way.  So, we use a regular expression
                    instead to check the contents of this string
                    */
                    case 'numeric':

                        if (!items[i].value.match(/[-+]?[0-9]*\.?[0-9]+/)) {

                            /*
                            add to our array, increase the count, assign our error messages
                            */
                            invalid[invalidCount] = items[i];
                            invalidCount++;
                            if (customError !== '') {
                                items[i].errorMessage = customError;
                            }
                            else {
                                items[i].errorMessage = 'Please enter a numeric value only.';
                            }
                        }
                        break;

                    /*
                    string, make sure the value of this is a string only.  This gets rid of some
                    of the special characters... see PERL's \w for the allowed items
                    */
                    case 'string':

                        if (items[i].value.match(/\W/)) {

                            /*
                            doesn't match pattern: add to our array, increase count, and
                            assign the error message
                            */
                            invalid[invalidCount] = items[i];
                            invalidCount++;
                            if (customError !== '') {
                                items[i].errorMessage = customError;
                            }
                            else {
                                items[i].errorMessage = 'Please enter a string.';
                            }
                        }
                        break;


                    /*
                    email: run our email validation function against that vlue
                    */
                    case 'email':

                        if (!validEmail(items[i].value)) {

                            /*
                            not a true email, set to array, increase count, set error
                            */
                            invalid[invalidCount] = items[i];
                            invalidCount++;

                            if (customError !== '') {
                                items[i].errorMessage = customError;
                            }
                            else {
                                items[i].errorMessage = 'Invalid email';
                            }
                        }
                        break;

                    /*
                    password: run our password validation function against that vlue
                    */
                    case 'password':

                        if (!validPassword(items[i].value,'')) {

                            /*
                            not a valid password, set to array, increase count, set error
                            */
                            invalid[invalidCount] = items[i];
                            invalidCount++;

                            if (customError !== '') {
                                items[i].errorMessage = customError;
                            }
                            else {
                                items[i].errorMessage = 'Invalid password';
                            }
                        }
                        break;

                    /**
                     * DATE stamp: MM/DD/YYYY
                     */
                    case 'MMDDYYYY':
                        if (!items[i].value.match(/(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|20)\d\d/)) {
                            /*
                            not a properly formatted date, set to array, increase count, set error
                            */
                            invalid[invalidCount] = items[i];
                            invalidCount++;

                            if (customError !== '') {
                                items[i].errorMessage = customError;
                                if (items[i].className == 'input_focus') {
                                    items[i].className = 'input_alarm';
                                }
                            }
                            else {
                                items[i].errorMessage = 'Invalid Date Stamp.';
                                if (items[i].className == 'input_focus') {
                                    items[i].className = 'input_alarm';
                                }
                            }
                        }
                        break;



                }

            }
        }

    }

    /*
    now don't be confused, this is not a 'type' or 'boolean'.  invalid is the name of our
    array that contains all of the elements that have invalid values.
    */
    return invalid;

}


/**
 *validPassword - returns valid password requirement met boolean
 *
 *This function takes a password and runs our password requirements against it.  If it meets them,
 *it returns true.  if it doesn't, it returns false.
 *
 *@param {String} pwd Password
 *@param {String} username Username sent (optional)
 *@return Returns boolean of whether or not password requirements met
 *@type Boolean
 */
function validPassword(pwd, username)
{

    /**
     *set our initial OK
     */
    var valid = true;

    /**
     *set our category count to 0
     */
	var categoryCount = 0;

    /**
     *first check, make sure at least one capital letter
     */
    var re = /[A-Z]/;
	if (re.test(pwd)) {
        /**
         *if so, increase categoryCount
         */
		categoryCount++
	}

    /**
     *check for a lower case letter
     */
	var re = /[a-z]/;
	if (re.test(pwd)) {
		categoryCount++
	}

    /**
     *check for a number
     */
	var re = /\d/;
	if (re.test(pwd)) {
		categoryCount++
	}

    /**
     *check for a special character
     */
	var re = /[^a-zA-Z0-9]/;
	if (re.test(pwd.value)) {
		categoryCount++
	}

	/**
     *process if their username exists
     */
    if (username) {
        /**
         *they sent one, so lets see that their username is not in their password
         */
        if (pwd.indexOf(username) != -1) {
            valid = false;
        }

    }

    /**
     *check length
     */
    if (pwd.length < 8) {
        valid = false;
    }

    /**
     * check for at least 3 categories
     */
    if (categoryCount < 3) {
        valid = false;
    }

    /**
     *finally return whether or not valid
     */
    return valid;
}



/**
validEmail (string value of email) - returns valid e-mail boolean

This function takes an e-mail address string, runs a regular expression against it, and returns
a bool of whether its a valid e-mail address format or not.  it doesn't actually check to make
sure that it exists.

@param {String} str Email Address string
@return Returns boolean of whether or not the e-mail address is valid
@type Boolean
*/
function validEmail(str)
{

    /*
    set valid to false
    */
    var valid = false;


    /*
    create our regEx that filters our email
    */
    emailRegEx = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);

    /*
    if we match the email address to our regex, set valid to true, otherwise false
    */
    if (str.match(emailRegEx)) {
        valid = true;
    }
    else {
        valid = false;
    }

    /*
    finally return our boolean on whether this was good or not
    */
    return valid;

}


/**
formCheckForm(form element) is used to see if we should allow the current form to submit

This function is ran on submit of a form.  It loops through the form, unsetting any errors
alarms or other types of items.  It then calls our required form function.  Finally, it loops
through all the error items that were returned and adds to the DOM those error messages.

IMPORTANT NOTE
Do to the processing done by this form, a few things are needed:
1) Your element needs to have ONLY ONE class definition
2) You MUST have a class of className_alarm for errors
3) You MUST have a span available class called ddErrorSpan - this is applied to the error span

Uses some other functions to execute such as formCheckRequired.
@see #formCheckRequired
@param {DOM} form The Dom Form element
@return returns boolean of whether or not to allow the form to submit
@type Boolean
*/
function formCheckForm(form) {


    /*
    this section of code gets all the spans in the element.  It then checks to see if the
    span contains ErrorSpan in its ID.  This would be an auto generated ID from further down
    the script.  If it does exist, it sets the innerHTML to it to nothing, effectively
    getting rid of it.  We don't delete it though, because this confuses the DOM
    */

    /*
    grab all the spans, and loop through them
    */
    possiblespans = document.getElementsByTagName('span');
    for (i=0; i<possiblespans.length; i++) {

        /*
         if this span contains 'ErrorSpan' in it, set it to nothing
        */
        if (possiblespans[i].id.substring(2, 11) == 'ErrorSpan') {
            possiblespans[i].innerHTML = '';
        }

    }


    /*
    check for any items in the alarm status.  Clear their status.  We'll reset these if they're
    a problem.
    */
    possiblealarms = form.elements;
    for (i=0; i<possiblealarms.length; i++) {
        switchStyle(possiblealarms[i], '_alarm', '-');
    }

    /*
    submit is the value that is returned to the form.  So far, we're setting it to true, saying
    sure, we'll submit this form.
    We'll allow this function to set it later
    */
    var submit = true;

    /*
    create a new Dom array
    */
    var newDom = new Array();

    /*
    get an array of items that are returned from formCheckRequired
    */
    invalid = formCheckRequired(form);

    /*
    if the length of invalid is above 0, it has items in it
    */
    if (invalid.length > 0) {

        /*
        well we already know that we are no longer submitting this form.  So, set set submit
        to false
        */
        submit = false;

        /*
        loop through each one of the invalid items
        */
        for (i=0; i<invalid.length; i++) {

            /*
            switch the style
            */
            switchStyle(invalid[i], '_alarm', '+');
            //alert (invalid[i].id);

            /*
            add an error to the item

            first, we'll check to see if it already had an error, if so, we'll just add to that
            span.  If not, we'll create a new one
            */
            if (document.getElementById('ddErrorSpan' + invalid[i].name)) {
                /*
                it exists, add to it
                */
                document.getElementById('ddErrorSpan' + invalid[i].name).innerHTML = "<b style='color: #ff0000'>" + invalid[i].errorMessage + '</b><br />';
            }
            else {

                /*
                it doesn't exist, so create it, apply the ddErrorSpan class to it, add the
                error message, and add it above the item in question.
                */
                newDom[i] = document.createElement('span');
                newDom[i].setAttribute('id', 'ddErrorSpan' + invalid[i].name);
                newDom[i].innerHTML="<b style='color: #ff0000'>" + invalid[i].errorMessage + '</b><br />';
                newDom[i].setAttribute('class', 'ddErrorSpan');
                /*
                this complex lil bit of code takes our element, goes to its parent,
                inserts our new element right before our element.
                */
                invalid[i].parentNode.insertBefore(newDom[i], invalid[i]);
            }
        }
    }


    /*
    finally, return whether or not we should submit this form
    */
    return submit;
}