// JavaScript Document
function validate_form(thisform) {
	if (!validateArea(thisform))
		return false;
	with (thisform) {	
		var inputs = thisform.getElementsByTagName("input");
		var textareas = thisform.getElementsByTagName("textarea");
		var selects = thisform.getElementsByTagName("select");
		var checkboxes = thisform.getElementsByTagName("checkbox");
		
		for (var i=0; i < inputs.length; i++) {
			inputs[i].style.background = "#ffffff";//sets the background color of the required field back to white
		}
		for (var i=0; i < textareas.length; i++) {
			textareas[i].style.background = "#ffffff";//sets the background color of the required field back to white
		}
		for (var i=0; i < selects.length; i++) {
			selects[i].style.background = "#ffffff";//sets the background color of the required field back to white
		}
		for (var i=0; i < inputs.length; i++) {
			if(inputs[i].type == "checkbox" && !inputs[i].checked && inputs[i].className.toUpperCase() == "REQ"){
				alert("Please read the disclaimer before proceeding.");
				inputs[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				inputs[i].focus();
				return false;
			}
			if (inputs[i].className.toUpperCase() == "REQ" && (inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].value.length==0) {
				alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + inputs[i].name.replace("*", "") + " is required. Please complete all required fields in form.");
				inputs[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				inputs[i].focus();
				return false;
			}
		}
		for (var i=0; i < textareas.length; i++) {
			if(textareas[i].className.toUpperCase() == "REQ" && textareas[i].value.length < 1) {
				alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + textareas[i].name.replace("*", "") + " is required. Please complete all required fields in form.");
				textareas[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				textareas[i].focus();
				return false;
			}
		}
		//This code does not work for Internet Explorer 7
		for (var i=0; i < selects.length; i++) {
			if (selects[i].className.toUpperCase() == "REQ" && selects[i].value.length==0 || selects[i].value == "Choose One") {
				alert("\t\tREQUIRED FIELD IS BLANK!\n\n" + selects[i].name.replace("*", "") + " is required. Please select an item from drop down");
				selects[i].style.background = "#FFBBBB";//sets the background color of the required field yellow
				selects[i].focus();
				return false;
			}
		} 
		if(isdefined(document, 'email')) {
			if (validate_required(email,"Email must be filled out!")==false) {
				email.focus();
				return false;
			}
			else if(validate_email(email,"Not a valid e-mail address!")==false) {
				email.focus();
				return false;
			}
		}
	}
}


function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="")	{
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}


function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}


// use this function like this...
//<textarea name="comment" rows="6" cols="30" onkeypress="return validateChars(event, document.frm);"></textarea>
function validateChars(e, thisForm) {
	var keynum;
	var keychar;
	var charCheck;
	if(window.event) { // IE 
		keynum = e.keyCode;
	}
	else if(e.which) {// Netscape/Firefox/Opera
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);
	charCheck = /[[@]/;
	if (charCheck.test(keychar))
		alert("Square braces( [ ) and at symbols( @ ) are not allowed.");
	if (!charCheck.test(keychar))
		validateArea(thisForm);
	return !charCheck.test(keychar);
}

function validateArea(thisForm) {
	textAreas = thisForm.getElementsByTagName("textarea");
	returnValue = true;
	for (i = 0; i < textAreas.length; i++) {
		if (textAreas[i].value.indexOf("[") != -1 || textAreas[i].value.indexOf("@") != -1) {
			do {
				textAreas[i].value = textAreas[i].value.replace("[", "");
			} while (textAreas[i].value.indexOf("[") != -1);
			do {
				textAreas[i].value = textAreas[i].value.replace("@", "");
			} while (textAreas[i].value.indexOf("@") != -1);
			returnValue = false;
		}
	}
	if (!returnValue)
		alert("Square braces( [ ) and at symbols( @ ) are not allowed. They have been removed.");
	return returnValue;
}


function isdefined(object, variable) {
	return (typeof(eval(object)[variable]) != 'undefined');
}