function validate(/*form id*/ frm, /*required field class name*/ classId, /*div element to write error message*/ divElement, /*optional button to disable*/btn)  {
	var arr_errors=new Array();
	var arr_passwords=new Array();
	var arrCounter=0;
	var temp="";
	var pattern="";
	if (arguments.length < 3){
		throw new Error("function validate called with missing parameters");
		return false;
	}
	if (!document.forms[frm]){
		throw new Error("Form with id of " + frm + " not found in document.");
		return false;
	}
	if(document.forms[frm].elements.length==0){
		throw new Error("Form with id of " + frm + " appears to have no elements to validate");	
	}
//loop through the form	
	for(var i=0; i<document.forms[frm].elements.length; i++){
//check for blank text boxes and textareas
		if ((document.forms[frm].elements[i].type=="text" || document.forms[frm].elements[i].type=="textarea") && document.forms[frm].elements[i].className==classId){
			if (document.forms[frm].elements[i].value=="") {
				temp=document.forms[frm].elements[i].id;
				while(temp.indexOf("_")!=-1){
					temp=temp.replace("_", " ");
				}
				arr_errors[arrCounter++]=temp;
				continue;
			}
//validate order
			else if(document.forms[frm].elements[i].id=="order_quantity"){
				pattern=/^\d+$/;
				if (document.forms[frm].elements[i].value.search(pattern)==-1){
					arr_errors[arrCounter++]="Order quantity can only contain digits";
				}
			}
//validate name
			else if(document.forms[frm].elements[i].id=="name" || document.forms[frm].elements[i].id=="last_name" || document.forms[frm].elements[i].id=="first_name"){
				pattern=/^([a-zA-Z]+)$/;
				if (document.forms[frm].elements[i].value.search(pattern)==-1){
					arr_errors[arrCounter++]="Name can only contain the letters A-Z";
				}
			}
//validate credit card number
			else if(document.forms[frm].elements[i].id=="credit_card_number"){
				pattern=/^\d{16}$/;
				if (document.forms[frm].elements[i].value.search(pattern)==-1){
					arr_errors[arrCounter++]="Credit Card Number can only contain exactly 16 digits";
				}
			}
//validate zip code
			else if(document.forms[frm].elements[i].id=="zip_code"){
				pattern=/^\d{5}([\-]\d{4})?$/;
				if (document.forms[frm].elements[i].value.search(pattern)==-1){
					arr_errors[arrCounter++]="Invalid U.S. zip code supplied<br />&nbsp;&nbsp;Proper format is either 12345 or 12345-6789";
				}
			}
		}
//check for blank select boxes
		else if(document.forms[frm].elements[i].type=="select-one" && document.forms[frm].elements[i].className==classId){
			if(document.forms[frm].elements[i].selectedIndex==0){
				temp=document.forms[frm].elements[i].id;
				while(temp.indexOf("_")!=-1){
					temp=temp.replace("_", " ");
				}
				arr_errors[arrCounter++]=temp;
				continue;
			}
		}
//check for blank or non-matching password fields
		else if (document.forms[frm].elements[i].type=="password" && document.forms[frm].elements[i].className==classId){
			if(arr_passwords.length==1){
				alert(arr_passwords[0]);
				if(document.forms[frm].elements[i].value==""){
					temp=document.forms[frm].elements[i].id;
					while(temp.indexOf("_")!=-1){
						temp=temp.replace("_", " ");
					}
					arr_errors[arrCounter++]=temp;
					arr_errors[arrCounter++]="Passwords do not match";
					continue;
				}
				else if(arr_passwords[0]!=document.forms[frm].elements[i].value){
					arr_errors[arrCounter++]="Passwords do not match";
					continue;
				}
			}
			else{
				if(document.forms[frm].elements[i].value==""){
					temp=document.forms[frm].elements[i].id;
					while(temp.indexOf("_")!=-1){
						temp=temp.replace("_", " ");
					}
					arr_errors[arrCounter++]=temp;
					continue;
				}
				else{
					arr_passwords[0]=document.forms[frm].elements[i].value;
					continue;
				}
			}
		}
	}
	/*
	else if (document.forms[frm].elements[i].type=="radio" && document.forms[frm].elements[i].className==classId){
				//alert("got this far");
				alert(document.forms[frm].elements[i].value);
				alert("I" + i);
			for (var j=0; j<document.forms[frm].elements[i].length; j++){
				//if (document.forms[frm].elements[i][j].checked ==false) {
					//arr_errors[arrCounter++]=document.forms[frm].elements[i].id.replace("_", " ");
					alert(document.forms[frm].elements[i][j]);
					alert(document.forms[frm].elements[i].length);
				//}	
			}
		}
	*/
	if (arr_errors.length==0){
		if(btn){
			disableButton(btn);
		}
		return true;
	}
	else{
		document.location.href="#errs";
		displayError(arr_errors, divElement);
		return false;
	}
}

function displayError(/*array of missing fields*/ arr, /*element to write to*/ el){
	msg="These required fields were left blank or invalid. Please fix and re-submit.<br />\n";
	msg+="<ul>";
	for(var i=0; i<arr.length; i++){
		msg +="<li>" + arr[i] + "</li>\n";  
	}
	msg+="</ul>\n";
	document.getElementById(el).innerHTML=msg;
}

function disableButton(/*html element*/ el)  {
	if (el){
		document.getElementById(el).disabled = "true";
	}
	else{
		throw new Error("Button named \"" + el + "\" not found in document");
	}
}
/*
REGULAR EXPRESSIONS FOR:
text only, such as first name, last name
var fnameRegxp = /^([a-zA-Z]+)$/;
postal code
var pcodeRegxp = /^([A-Za-z]{1,2})([0-9]{2,3})([A-Za-z]{2})$/;
telephone number
var telnoRegxp = /^([0-9]{11})$/;
email addresses
var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
date
var dobRegxp = /^([0-9]){2}(\/|-){1}([0-9]){2}(\/|-)([0-9]){4}$/;
*/
