var FormValidator = Class.create({

	initialize: function() {
		
		this.defaultValues = new Hash();
		this.initValidator();
		
	},

	// Define event handlers for any forms and form elements that need them.

    initValidator: function() {
		
	   // Loop through all forms in the document
        for(var i = 0; i < document.forms.length; i++) {
            var f = document.forms[i];  // the form we're working on now

            // Assume, for now, that this form does not need any validation
            var needsValidation = false;

            // Now loop through the elements in our form
            for(var j = 0; j < f.elements.length; j++) {
                var e = f.elements[j];  // the element we're working on

                if (e.type != "text" && e.type != "textarea") { continue; }

                var pattern = "";

				if(Element.hasClassName(e, 'validate_required')) {
					pattern = "\\S";
					this.defaultValues.set(e.name, e.value);
				}				
				else if(Element.hasClassName(e, 'validate_email')) {
					pattern = "^.+@.+\\..{2,3}$";
					this.defaultValues.set(e.name, e.value);
				}
				else if(Element.hasClassName(e, 'validate_url')) {				
					pattern = "^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$";
				}			
				
                // If this element requires validation,
                if (pattern !== "") {
	                e.setAttribute("pattern", pattern);

                    // validate the element each time it changes
                    e.onchange = this.validateOnChange;
                    // Remember to add an onsubmit handler to this form
                    needsValidation = true;
                }
            }

            // If at least one of the form elements needed validation,
            // we also need an onsubmit event handler for the form
            if (needsValidation) { f.onsubmit = this.validateOnSubmit; }
        }
    },
          
    // This function is the onchange event handler for textfields that require validation.

    validateOnChange: function() {
		
	    var textfield = this;                            // the textfield
        var pattern = textfield.getAttribute("pattern"); // the pattern
        var value = this.value;                          // the user's input
        	
		var hasDefault = textfield.hasClassName("hasDefaultValue"); // this class has to be manually assigned for any value that you want to set a default value on (and validate against)
		
        // If the value does not match the pattern 
		// -OR- if the value is equal to its initially set default value (currently only used in footer), 
		// set the class to "invalid".
        if ((value.search(pattern) == -1) || (hasDefault && fi.formValidator.defaultValues.get(textfield.name) == value)) {
			
			if(this.type == "textarea") {
				Element.addClassName(this.parentNode.parentNode, 'invalid');
			}
			Element.addClassName(this, 'invalid');
			if($(this.id + '_warning')) {
				$(this.id + '_warning').show();
			}

			if($(this.id + '_info')){
				$(this.id + '_info').hide();
			}

			if(this.id == "careers_form_message") {
				this.value= "Please enter your message.";
			}
			
		}
		else {
			if(this.type == "textarea") {
				Element.removeClassName(this.parentNode.parentNode, 'invalid');
			}
 			Element.removeClassName(this, 'invalid');
			if($(this.id + '_warning')) {
				$(this.id + '_warning').hide();
			}

			if($(this.id + '_info')){
				$(this.id + '_info').show();
			}
		}
    },

    // This function is the onsubmit event handler for any form that requires validation.

    validateOnSubmit: function() {
        
		
		// When the form is submitted, we revalidate all the fields in the
        // form and then check their classNames to see if they are invalid.
        // If any of those fields are invalid, display an alert and prevent
        // form submission.
        var invalid = false;  // Start by assuming everything is valid
        // Loop through all form elements
        for(var i = 0; i < this.elements.length; i++) {
            var e = this.elements[i];
			
            // If the element is a text field and has a pattern
              if ((e.type == "text" || e.type == "textarea") && e.hasAttribute('pattern')) {
                e.onchange(); // Invoke the handler to re-validate
                // If validation fails for the element, it fails for the form
				if(Element.hasClassName(e, 'invalid')) {
					invalid = true;
				}
            }
        }
		
		// We cannot use an AJAX request for the fileupload, so don't block the submit on the Contact Business Form
		
		if(this.id == 'contact_business_form') {
			if(invalid) {
				return false;
			}
			else {
				return true;
			}
		}
		else if(this.id == 'careers_position_form') {
	        if (!invalid) {
				fi.careers.submitForm(this.id);
	        }
	
	        return false;
		}
		else if(this.id == 'footerContact') {
			if (!invalid) {
				fi.base.submitForm(this.id);
			}
			return false;
		}
		else if(this.id == 'newsletter_form_sidebar' || this.id == 'newsletter_form_footer') {
			if (!invalid) {
				fi.base.submitNewsletterForm(this.id);
			}
			return false;
		}
		else {
	        if (!invalid) {
				fi.contact.submitForm(this.id);
	        }
	
	        return false;
		}
    }
		
});