Validate = {};
//if this is set to '' then it will popup a js alert. else it will go into a html element - whihc is much neater
Validate.alertArea = 'Validate.alertArea';
//add in comma separated names of the submiting fields eg ['question1','question1'];
Validate.ignore = [];
//if set to true the validate will bomb out after 1 error else it will list them all out at once
Validate.alertImmeadiatly = false;
//make sure that this is set to the exact same text as the Other boxes
Validate.otherDefault = "Please state";
//default error if you don't set your own
Validate.standardError = "Please ensure you have selected the required fields";
Validate.errors = {};
//example errors ['elementname'] = "Error Text"
Validate.errors['question1'] = "There is an error with Question 1";
Validate.errors['question1**Other'] = "Question 1 - as you have selected 'Other' you must state the reason why";


Validate.validateForm = function(whichForm) {
    if (typeof(whichForm) == "string") {
        whichForm = document.getElementById(whichForm);
    }
    var allElms = whichForm.elements;

    var valid = false;
    var whichElement;
    var storedArray = new Array();
    var name;
    var skip = false;
    var namesChecked = {};
    var errorElements = [];
    var errors = "";
    var otherElements = {};

    for (var i = 0; i < allElms.length; i++) {
        //break after the first error?
        if (Validate.alertImmeadiatly && errors != "") {
            whichElement.focus();
            break;
        }
        //assume invalid
        valid = false;

        whichElement = allElms[i];
		if (whichElement.tagName == 'FIELDSET') {
            continue;
        }
        name = whichElement.name

        skip = false;
        //check name against ignore array
        for (var j = 0; j < Validate.ignore.length; j++) {
            if (name == Validate.ignore[j]) {
                skip = true;
                break;
            }
        }
        //if we've looked at this / grouped element(s) before?
        if (namesChecked[name] != null) {
            skip = true;
        }
        if (!skip) {
            if (name.indexOf('**Other') == -1) {
                if (whichElement.type != 'hidden') {
                    if (whichElement.type == 'radio' || whichElement.type == 'checkbox') {
                        namesChecked[name] = true;
                        //if this is a group we need to check at least one is selected
                        var elms = document.getElementsByName(name);
                        for (var j = 0; j < elms.length; j++) {
                            if (elms[j].checked) {
                                valid = true;
                                if (elms[j].value == "Other") {
                                    otherElements[name] = true;
                                }   //don't break as we may be looking at checkboxes?
                            }
                        }
                        if (!valid) {
                            errorElements[errorElements.length - 1] = whichElement;
                            errors += (!Validate.alertImmeadiatly && Validate.errors[name] != null ? Validate.errors[name] : Validate.standardError) + "<br />";
                        }
                    } else if (whichElement.type == 'select-one') {
                        if (whichElement.value == "" || whichElement.value == "-1") {
                            errorElements[errorElements.length - 1] = whichElement;
                            errors += (!Validate.alertImmeadiatly && Validate.errors[name] != null ? Validate.errors[name] : Validate.standardError) + "<br />";
                        } else if (whichElement.value == "Other") {
                            otherElements[name] = true;
                        }
                    } else if (whichElement.type == 'text') {
                        if (whichElement.value == "") {
                            errorElements[errorElements.length - 1] = whichElement;
                            errors += (!Validate.alertImmeadiatly && Validate.errors[name] != null ? Validate.errors[name] : Validate.standardError) + "<br />";
                        }
                    } else if (whichElement.type == 'textarea') {
                        if (whichElement.value == "") {
                            errorElements[errorElements.length - 1] = whichElement;
                            errors += (!Validate.alertImmeadiatly && Validate.errors[name] != null ? Validate.errors[name] : Validate.standardError) + "<br />";
                        }
                    }
                }
            }
            else { //**Other
                if (otherElements[name.substring(0, name.indexOf("**Other"))]) {
                    if (whichElement.value == "" || whichElement.value == Validate.otherDefault) {
                        errors += (!Validate.alertImmeadiatly && Validate.errors[name] != null ? Validate.errors[name] : Validate.standardError) + "<br />";
                    }
                }
            }
        }
    }
    if (errors == "") {
        //do we are fine
        return true;
    } else {
	if(Validate.alertArea == ''){
			alert(errors);
		}else{
			document.getElementById(Validate.alertArea).innerHTML = errors.replace('\n','<br/>');
		}
        return false;
    }
}

