function validation(which) {

	var required = getElementsByClassName(which, "div", "required");

	var errors = new Array();
	var error_header = "The following errors occured";
	var warning_color = "#FFCCCC";
	var normal_color = "#EEEEEE";
	
	function missing_content(field, label) {
		var missing_empty = "";
		if (field.value == "") {
			missing_empty+= label + "\n";
			field.style.backgroundColor = warning_color;
		} else {
			field.style.backgroundColor = normal_color;
		}
		return missing_empty;
	}
	
	// Function for validating radio buttons
	function val_radio(fields, label) {
		var missing_empty ="";
		var checked = 0;
		for (var j = 0; j < fields.length; j++) {
			if (fields[j].checked) {
				checked = 1;
			}
		}
		if (!checked) {
			missing_empty+= label + "\n";
		}
		return missing_empty;
	}
	
	// Function for validating checkboxes
	function val_checkbox(fields, label) {
		var missing_empty ="";
		var checked = 0;
		for (var j = 0; j < fields.length; j++) {
			if (fields[j].checked) {
				checked = 1;
			}
		}
		if (!checked) {
			missing_empty+= label + "\n";
		}
		return missing_empty;
	}
	
	// email validation
	function checkEmail(myForm) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm)){
			return (true)
		}
		return (false)
	}
	
	for (var i = 0; i < required.length; i++) {
		//alert(required[i]);
		
		var input;
		var label;
		
		if (required[i].getElementsByTagName("input")[0]) {
		
			input = required[i].getElementsByTagName("input")[0];
			
			switch (input.getAttribute("type")) {
			
			// Validate Text input
			case "text":
			if (required[i].getElementsByTagName("label")[0].firstChild.nodeType == 3) {
				label = required[i].getElementsByTagName("label")[0].firstChild.nodeValue.trim();
			} else if (required[i].getElementsByTagName("label")[0].firstChild.firstChild.nodeType == 3) {
				label = required[i].getElementsByTagName("label")[0].firstChild.firstChild.nodeValue.trim();
			}
			if (missing_content(input, label) != "") {
				errors.push(missing_content(input, label) + " is required");
			}			
			
			if (required[i].className.indexOf('number') != '-1' && input.value != '') {
				if (isNaN(input.value) || input.value <= 0) {
					errors.push(label + ' must be a positive number');
				}
			}
			
			if (input.id == "email") {
			
				// if the email field is filled in, we need to verify that the email is correct
				if (input.value != ""){
					var email = checkEmail(input.value);
				
					if (email == false){
						errors.push("Invalid Email Address\n");
						input.style.backgroundColor = warning_color;
					} else {
						input.style.backgroundColor = normal_color;
					}
				}
			}
			
			if (input.id == "x_Card_Num" && input.value != "") {
				// Any additional validation goes here
				// check card number
				var cc_number = document.getElementById('x_Card_Num');
				
				// check card type
				var cc_type = document.getElementById('CC_TYPE');
				
				// check the card expiry month
				var cc_month = document.getElementById('CC_MONTH');
			
				// check the card expiry year
				var cc_year = document.getElementById('CC_YEAR');
				
			
				// run the validation
				// return missing:
				cc_valid = validateCard(cc_number.value,cc_type.value,cc_month.value,cc_year.value);
				if (cc_valid != "") {
					cc_number.style.backgroundColor = warning_color;
					errors.push(cc_valid);
				} else {
					cc_number.style.backgroundColor = normal_color;
				}			
			}
			break;
			
			// Validate Radio buttons		
			case "radio":
			label = required[i].getElementsByTagName("p")[0].firstChild.nodeValue.trim();
			if (val_radio(required[i].getElementsByTagName("input"), label) != "") {
				errors.push(val_radio(required[i].getElementsByTagName("input"), label) + " is required");
			}
			break;
			
			// Validate Checkboxes
			case "checkbox":
			label = required[i].getElementsByTagName("p")[0].firstChild.nodeValue.trim();
			if (val_checkbox(required[i].getElementsByTagName("input"), label) != "") {
				errors.push(val_checkbox(required[i].getElementsByTagName("input"), label) + " is required");
			}
			break;
			
			}

		// Validate textarea
		} else if (required[i].getElementsByTagName("textarea")[0]) {
			input = required[i].getElementsByTagName("textarea")[0];
			label = required[i].getElementsByTagName("label")[0].firstChild.nodeValue.trim();
			if (missing_content(input, label) != "") {
				errors.push(missing_content(input, label) + " is required");
			}
	
		// Validate select drop downs
		} else if (required[i].getElementsByTagName("select")[0]) {
			input = required[i].getElementsByTagName("select")[0];
			label = required[i].getElementsByTagName("label")[0].firstChild.nodeValue.trim();
			if (missing_content(input, label) != "") {
				errors.push(missing_content(input, label) + " is required");
			}
		}
	}
	
	// Any additional validation goes here
	
	// If there are any errors, don't submit the form
	if (errors.length != 0) {
		//alert(error_header + errors);
		
		if (document.getElementById('error_container')) {
			which.removeChild(document.getElementById('error_container'));
		}
		
		var error_container = document.createElement('div');
		error_container.className = 'errors';
		error_container.id = 'error_container';
		
		var error_head = document.createElement('h2');
		var error_head_txt = document.createTextNode(error_header);
		error_head.appendChild(error_head_txt);
		error_container.appendChild(error_head);
		var error_list = document.createElement('ul');
		var error_list_items = new Array();
		
		//alert(errors.length);
		for (var k = 0; k < errors.length; k++) {
			error_list_items[k] = document.createElement('li');
			error_list_items[k].appendChild(document.createTextNode(errors[k]));
			error_list.appendChild(error_list_items[k]);
		}
		
		error_container.appendChild(error_list);
		which.insertBefore(error_container, which.firstChild);
		
		// Anchor to the top of the error_container div
		if (window.location.href.indexOf('#error_container') == -1) {
			window.location = window.location + "#error_container";
		} else {
			var url = window.location.href;
			url = url.slice(0,url.indexOf('#error_container'));
			window.location = url + '#error_container';
		}
		
		return false;
	} else {
		return true;
	}

}

function validateForm(which) {
	
	if (!document.getElementById || !document.getElementById(which)) return false;
	
	var e = document.getElementById(which);
	
	e.onsubmit = function() {
		
		return validation(this);
		
	}
}

addLoadEvent(function() {
	validateForm('contact_form');
	validateForm('donate_form');
});
