vdwVal = {
	error_header: 'The following errors occurred',
	warningClass: 'error',
	validation:function(e) {
		var form = vdwDOM.getTarget(e);
		required = vdwDOM.getElementsByClassName(form, '*', 'required');
		var errors = new Array();
		for (var i = 0; i < required.length; i++) {
			var input;
			var label;
			if (required[i].getElementsByTagName("input")[0]) {
				input = required[i].getElementsByTagName("input")[0];
				switch (input.getAttribute("type")) {
					case "text":
					if (required[i].getElementsByTagName("label")[0].firstChild.nodeType == 3) {
						label = vdwUtil.trimString(required[i].getElementsByTagName("label")[0].firstChild.nodeValue);
					} else if (required[i].getElementsByTagName("label")[0].firstChild.firstChild.nodeType == 3) {
						label = vdwUtil.trimString(required[i].getElementsByTagName("label")[0].firstChild.firstChild.nodeValue);
					}
					if (vdwVal.missingContent(input, label) != "") {
						errors.push(vdwVal.missingContent(input, label) + " is required");
						vdwVal.setErrorClass(input, true);
					} else {
						vdwVal.setErrorClass(input, false);
					}
					if (required[i].className.indexOf('number') != '-1' && input.value != '') {
						if (!vdwVal.valNumber(input.value)) {
							errors.push(label + ' must be a positive number');
							vdwVal.setErrorClass(input, true);
						} else {
							vdwVal.setErrorClass(input, false);
						}
					}
					if (required[i].className.indexOf('currency') != '-1' && input.value != '') {
						var tmp = input.value.replace(/\$/g, '');
						tmp = tmp.replace(/,/g, '');
						if (!vdwVal.valNumber(tmp)) {
							errors.push(label + ' must be a positive number');
							vdwVal.setErrorClass(input, true);
						} else {
							input.value = tmp;
							vdwVal.setErrorClass(input, false);
						}
					}
					if (required[i].className.indexOf("email") != "-1") {
						if (input.value != "") {
							var email = vdwVal.checkEmail(input.value);
							if (email == false) {
								errors.push("Invalid Email Address\n");
								vdwVal.setErrorClass(input, true);
							} else {
								vdwVal.setErrorClass(input, false);
							}
						}
					}	
					if (input.id == "cc_number" && input.value != "") {
						var cc_number = document.getElementById('cc_number');
						var cc_type = document.getElementById('cc_type');
						var cc_month = document.getElementById('cc_month');
						var cc_year = document.getElementById('cc_year');
						cc_valid = validateCard(cc_number.value,cc_type.value,cc_month.value,cc_year.value);
						if (cc_valid != "") {
							errors.push(cc_valid);
							vdwVal.setErrorClass(input, true);
						} else {
							vdwVal.setErrorClass(input, false);
						}		
					}
					break;
					case "radio":
					label = vdwUtil.trimString(vdwVal.getRadioLabel(required[i]).nodeValue);
					if (vdwVal.valRadio(required[i].getElementsByTagName("input"), label) != "") {
						errors.push(vdwVal.valRadio(required[i].getElementsByTagName("input"), label) + " is required");
					}
					break;
					case "checkbox":
					label = vdwUtil.trimString(required[i].getElementsByTagName("p")[0].firstChild.nodeValue);
					if (vdwVal.valCheckbox(required[i].getElementsByTagName("input"), label) != "") {
						errors.push(vdwVal.valCheckbox(required[i].getElementsByTagName("input"), label) + " is required");
					}
					break;
					case "password":
					if (required[i].getElementsByTagName("label")[0].firstChild.nodeType == 3) {
						label = vdwUtil.trimString(required[i].getElementsByTagName("label")[0].firstChild.nodeValue);
					} else if (required[i].getElementsByTagName("label")[0].firstChild.firstChild.nodeType == 3) {
						label = vdwUtil.trimString(required[i].getElementsByTagName("label")[0].firstChild.firstChild.nodeValue);
					}
					if (vdwVal.missingContent(input, label) != "") {
						errors.push(vdwVal.missingContent(input, label) + " is required");
					}
					break;
				}
			} else if (required[i].getElementsByTagName("textarea")[0]) {
				input = required[i].getElementsByTagName("textarea")[0];
				label = vdwUtil.trimString(required[i].getElementsByTagName("label")[0].firstChild.nodeValue);
				if (vdwVal.missingContent(input, label) != "") {
					errors.push(vdwVal.missingContent(input, label) + " is required");
					if (input.className.indexOf(vdwVal.warningClass) == -1) {
						vdwDOM.addClass(input, vdwVal.warningClass);
					}
				}
			} else if (required[i].getElementsByTagName("select")[0]) {
				input = required[i].getElementsByTagName("select")[0];
				label = vdwUtil.trimString(required[i].getElementsByTagName("label")[0].firstChild.nodeValue);
				if (vdwVal.missingContent(input, label) != "") {
					errors.push(vdwVal.missingContent(input, label) + " is required");
					vdwVal.setErrorClass(input, true);
				} else {
					vdwVal.setErrorClass(input, false);
				}
			}
		}
		return errors;
	},
	missingContent:function(field, label) {
		var missing_empty = '';
		if (field.value == '') {
			missing_empty += label;
			if (field.className.indexOf(vdwVal.warningClass) == -1) {
				vdwDOM.addClass(field, vdwVal.warningClass);
			}
		} else {
			vdwDOM.removeClass(field, vdwVal.warningClass);
		}
		return missing_empty;
	},
	valRadio:function(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;
		}
		return missing_empty;
	},
	valCheckbox:function(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;
		}
		return missing_empty;
	},
	valNumber:function(str) {
		if (isNaN(str) || str <= 0) {
			return false;
		} else {
			return true;
		}
	},
	valCurrency:function(value) {
		// Remove commas and dollar signs
		var tmp = value.replace(/\$/g, '');
		tmp = tmp.replace(/,/g, '');
		if (tmp == '' || !vdwVal.valNumber(tmp)) {
			return false;
		} else {
			return true;
		}
	},
	checkEmail:function(myForm) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm)){
			return (true)
		}
		return (false)
	},
	displayErrors:function(errors, e) {
		var form = vdwDOM.getTarget(e);
		if (errors.length != 0) {
			if (document.getElementById('error_container')) {
				form.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('h3');
			var error_head_txt = document.createTextNode(vdwVal.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();
			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);
			form.insertBefore(error_container, form.firstChild);
			vdwUtil.fadeUp(error_container, 255, 0, 0);
			var anchor = '#' + form.id;
			if (window.location.href.indexOf(anchor) == -1) {
				window.location = window.location + anchor;
			} else {
				var url = window.location.href;
				url = url.slice(0,url.indexOf(anchor));
				window.location = url + anchor;
			}
			vdwDOM.cancelClick(e);
			return false;
		} else {
			return true;
		}	
	},
	errorsToString:function(errors, e) {
		var errorMsg = vdwVal.error_header + '\n';
		for (var i = 0; i < errors.length; i++) {
			errorMsg += errors[i] + '\n';
		}
		vdwDOM.cancelClick(e);
		return errorMsg;		
	},
	setErrorClass:function(input, error) {
		var hasErrorClass = (input.className.indexOf(vdwVal.warningClass) == -1) ? false : true;
		if (error) {
			if (!hasErrorClass) {
				vdwDOM.addClass(input, vdwVal.warningClass);
			}
		} else {
			vdwDOM.removeClass(input, vdwVal.warningClass);
		}
	},
	getRadioLabel:function(parent) {
		if (parent.getElementsByTagName('p')[0]) {
			return vdwDOM.firstChild(parent.getElementsByTagName('p')[0], 3);
		} else if (parent.parentNode.getElementsByTagName('legend')[0]) {
			return vdwDOM.firstChild(parent.parentNode.getElementsByTagName('legend')[0], 3);
		} else if (parent.parentNode.parentNode.getElementsByTagName('legend')[0]) {
			return vdwDOM.firstChild(parent.parentNode.parentNode.getElementsByTagName('legend')[0], 3);
		}
	},
	updateTotals:function(baseValue, addValue, totalFld, updateCase) {		
		if (updateCase && (vdwVal.valCurrency(addValue))) {
			var total = addValue.replace(/\$/g, '');
			total = total.replace(/,/g, '');
			total = (!isNaN(total)) ? Number(total).toFixed(2) : 0.00;
			totalFld.value = (baseValue + Number(total)).toFixed(2);
		} else {
			totalFld.value = baseValue.toFixed(2);
		}		
	}
}

vdwDOM.addEvent(window, 'load', function() {

	// Attach the validation to the submit event of all forms with a class of 'validate'
	var forms = document.getElementsByTagName('form');
	for (var i = 0; i < forms.length; i++) {
		if (forms[i].className.indexOf('validate') != '-1') {
			vdwDOM.addEvent(forms[i], 'submit', function(e) {
				var errors = vdwVal.validation(e);
				vdwVal.displayErrors(errors, e);
			}, false);
		}
	}
	
	if (document.getElementById('employment_application_form')) {
		vdwDOM.addEvent(document.getElementById('employment_application_form'), 'submit', function(e) {
			var errors = vdwVal.validation(e);
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('online_donation')) {
		vdwDOM.addEvent(document.getElementById('online_donation'), 'submit', function(e) {
			var errors = vdwVal.validation(e);
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('directors_donation')) {
		vdwDOM.addEvent(document.getElementById('directors_donation'), 'submit', function(e) {
			var errors = vdwVal.validation(e);
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('honoring_donation')) {
		vdwDOM.addEvent(document.getElementById('honoring_donation'), 'submit', function(e) {
			var errors = vdwVal.validation(e);
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('thanking_staff')) {
		vdwDOM.addEvent(document.getElementById('thanking_staff'), 'submit', function(e) {
			var errors = vdwVal.validation(e);
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('fun_run_registration')) {	
	
		var donationAmtFld = document.getElementById('donation_amount');
		var totalFld = document.getElementById('total');
		var additionalDonationFld = document.getElementById('additional_donation');
		vdwVal.updateTotals(20.00, donationAmtFld.value, totalFld, additionalDonationFld.checked);

		vdwDOM.addEvent(additionalDonationFld, 'click', function() {
			vdwVal.updateTotals(20.00, donationAmtFld.value, totalFld, additionalDonationFld.checked);
		}, false);
		vdwDOM.addEvent(donationAmtFld, 'change', function() {
			vdwVal.updateTotals(20.00, donationAmtFld.value, totalFld, additionalDonationFld.checked);
		}, false);

		vdwDOM.addEvent(document.getElementById('fun_run_registration'), 'submit', function(e) {
			
			vdwVal.updateTotals(20.00, donationAmtFld.value, totalFld, additionalDonationFld.checked);
			var errors = vdwVal.validation(e);
			if (!document.getElementById('waiver').checked) { errors.push('Please sign the waiver'); }
			
			var teamNameFld = document.getElementById('team_name');
			if (document.getElementById('participating_as_team_member').checked && teamNameFld.value == '') {
				vdwVal.setErrorClass(teamNameFld, true);
				errors.push('Team name is required');
			} else {
				vdwVal.setErrorClass(teamNameFld, false);
			}
			
			var ageFld = document.getElementById('age');
			if (document.getElementById('event_5k_run').checked && ageFld.value == '') {
				vdwVal.setErrorClass(ageFld, true);
				errors.push('Age is required');
			} else {
				vdwVal.setErrorClass(ageFld, false);
			}			
			
			if (document.getElementById('additional_donation').checked) {
				if (donation_amt_fld.value == '') {
					errors.push('Please enter a donation amount');					
				} else if (!vdwVal.valCurrency(donation_amt_fld.value)) {
					errors.push('Please enter a valid donation amount');
				}
			}
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('fun_run_sponsorship')) {

		var contribAmtFld = document.getElementById('contribution_amount');
		var totalFld = document.getElementById('total');
		vdwVal.updateTotals(0, contribAmtFld.value, totalFld, true);

		vdwDOM.addEvent(contribAmtFld, 'change', function() {
			vdwVal.updateTotals(0, contribAmtFld.value, totalFld, true);
		}, false);
		
		vdwDOM.addEvent(document.getElementById('fun_run_sponsorship'), 'submit', function(e) {
			vdwVal.updateTotals(0, contribAmtFld.value, totalFld, true);
			var errors = vdwVal.validation(e);
			if (!vdwVal.valCurrency(contribAmtFld.value)) { errors.push('Please enter a valid contribution amount'); }
			vdwVal.displayErrors(errors, e);
		}, false);
	}
	
	if (document.getElementById('conference_form')) {
			function formatCurrency(num) {
			num = num.toString().replace(/\$|\,/g,'');
			if(isNaN(num))
			num = "0";
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num*100+0.50000000001);
			cents = num%100;
			num = Math.floor(num/100).toString();
			if(cents<10)
			cents = "0" + cents;
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
			return (((sign)?'':'-') + '$' + num + '.' + cents);
			}
		
			
			function ck_currency_format(fld, amt){
				// this checks to make sure the monetary value doesn't have any $ signs or commas
				myStr = amt;
				
				for (s=0; s < myStr.length; s++){
					myChar = myStr.substring(s,s+1);
							
					// test if the character is an integer.. periods are allowed as well.
					var myMod = myChar % 1;
			
					if (myMod != 0) {
						// it is not an integer
						// check to make sure the character is not a period
						if (myChar != "."){
							alert ("Sorry, the character '" + myChar + "' is not allowed. Monetary values need to be displayed as XXXX.XX (ie. 1234.56)");
							return false;
						}
					}
				}
			}
			function calcTotals(){
			
				var today = new Date();
				var earlyDeadline = new Date();
				earlyDeadline.setMonth(6);
				earlyDeadline.setDate(31);
				regularDeadline = new Date();
				regularDeadline.setMonth(8);
				regularDeadline.setDate(14);
				
				 if (today <= earlyDeadline) {
					document.registration["TOTAL"].value = 150.00;
				 } else if (today <= regularDeadline) {
					document.registration["TOTAL"].value = 200.00;
				 } else if (today > regularDeadline) {
					document.registration["TOTAL"].value = 200.00;
				 }
				
				ttl_amount = Number(document.registration["TOTAL"].value);
				document.registration["TOTALHOLDER"].value = ttl_amount;
				
				// format the form field
				document.registration["TOTAL"].value = formatCurrency(ttl_amount);
			}
			
			calcTotals();
		vdwDOM.addEvent(document.getElementById('conference_form'), 'submit', function(e) {
			var errors = vdwVal.validation(e);

			if (Number(document.registration["TOTALHOLDER"].value) > 0){
					// check the cvv number
					if (document.registration["CVV"].value == ""){
						errors.push("Please enter your CVV number from your credit card.");
					}
					// check card number
					cc_number = document.registration["CCNUMBER"].value;
					
					// check card type
					cc_type = document.registration["CCTYPE"].value;
					
					// check the card expiry month
					cc_month = document.registration["CCMONTH"].value;
					// check the card expiry year
					cc_year = document.registration["CCYEAR"].value;
					
					// run the validation
					// return missing:
					cc_valid = validateCard(cc_number,cc_type,cc_month,cc_year);
					if (cc_valid != ""){ errors.push(cc_valid); };
					
					
				}
				
				var prioritized = false;
				if ((document.getElementById('workshop1A').value == '') || 
					(document.getElementById('workshop1B').value == '') ||
					(document.getElementById('workshop1C').value == '') || 
					(document.getElementById('workshop1D').value == '') || 
					(document.getElementById('workshop2A').value == '') || 
					(document.getElementById('workshop2B').value == '') || 
					(document.getElementById('workshop2C').value == '') || 
					(document.getElementById('workshop2D').value == '')) {
					prioritized = false;
				} else {
					prioritized = true;
				}
				
				if (!prioritized) {
					errors.push("Please prioritize the workshops for each session from 1 to 4");
				}			
			calcTotals();
			vdwVal.displayErrors(errors, e);
		}, false);
	}	
	
}, false);
