
  
  
/*
Daniel Marsh
4/25/2008
This is a js file that will validate any form.
The form needs the following requirements:
1. A button with id = 'send'
2. A form with the id form (this is the form that will be validated
3. Any field that is required will need the class name "required"
4. The error message the field is connected with will need the id = to required# where # is the number where
the field falls in the required list. So the first required field will be connected to the error message with id = required1
5. If you want to confirm an email the ids on those fields need to be email and confirm-email

Note: This will only validate if a select field has a value it does not validate if a specific term has been selected.
checkMailingBox(); is a custom function that changes certain class names to required
 */


var errors = Array(); //array of locations of triggered error fields
var count = 0; //where you are in the array
var countRequired = 1; //the location of the required error message
var confirmEmail;

function checkForm() {
    var button = document.getElementById('send');
    if(button != null){
        button.onclick=function(){
            errors = Array();
			count = 0; 
			countRequired = 1; 
			var form = document.getElementById('form');
            confirmEmail = document.getElementById('emailConfirm');
			if(form != null){
				checkMailingBox();
				
                // Grab all required inputs
                var formElements = form.elements;

                //loop through all formElements
                for (i=0;i<formElements.length;i++) {
                    //check to see if the formElement's className is required
                    if(formElements[i].className == 'required text' || formElements[i].className == 'required'){
                        checkElement(formElements[i]);
                        countRequired++;
                    }
                }

                //show error messages or hide them
                if(errors.length > 0){
                    errorMessages();
					return false;
                }
            }

        }
    }
}


function checkElement(formElement){
    //determine what type of element the formElement[i] is

    type = formElement.type;
    if(type == 'radio' || type == 'checkbox'){
        if(!formElement.checked){
            errors[count] = countRequired;
            count++;
        }

    }else if(formElement.value.length <= 0){
        errors[count] = countRequired;
        count++;

    }

    if(formElement.id == 'email' && confirmEmail != null){
        //Advanced count for unique error message
        countRequired++;
        if(formElement.value != confirmEmail.value){
            errors[count] = countRequired;
            count++;
        }
    }
}

function errorMessages(){
    var k=0;
    for (j=0;j<countRequired;j++) {
        errorId = "required" + j;
		errorLabel = "errorLabel" + j;
        errorElem = document.getElementById(errorId);
		errorLabelElem = document.getElementById(errorLabel);
        if(errorElem != null && errorLabelElem != null){
            if(j == errors[k]){
                errorElem.className = "error";
                errorLabelElem.className = "error";
				if(k == 0){
					scroll(0,0)			
				}
				if(k < errors.length){
                    k++;
                }
            }else{
                errorElem.className = "hidden";
				errorLabelElem.className = " ";
            }
        }
    }
}

function checkMailingBox(){	
	var mailingBox = document.getElementById('packet');
	if(mailingBox != null){
		if(mailingBox.checked){
			element = document.getElementById('first-name');
			element.className = "required text";
			element = document.getElementById('last-name');
			element.className = "required text";
			element = document.getElementById('address1');
			element.className = "required text";
			element = document.getElementById('city');
			element.className = "required text";
			element = document.getElementById('state');
			element.className = "required text";
			element = document.getElementById('zip');
			element.className = "required text";
			element = document.getElementById('country');
			element.className = "required text";
		}else{
		element = document.getElementById('first-name');
			element.className = "text";
			element = document.getElementById('last-name');
			element.className = "text";
			element = document.getElementById('address1');
			element.className = "text";
			element = document.getElementById('city');
			element.className = "text";
			element = document.getElementById('state');
			element.className = "text";
			element = document.getElementById('zip');
			element.className = "text";
			element = document.getElementById('country');
			element.className = "text";
		}
	}
}


addEvent(window, 'load', checkForm, false);
