//FORM VALIDATION
function check_required(myForm) {
	var requiredFields = myForm._required.value.split("|");
	var errorString = '';
	for (var i=0; i<requiredFields.length; i++) {
		var parts = requiredFields[i].split(",");
		var field = parts[0];
		var title = parts[1];
		var requiredType = parts[2];
		var myElement = myForm[field];
		var isNull = false;
		/*
		TYPES check for
			text				//the presence of any text
			text-email		//valid email address format
			text-int			//number
			text-phone		//valid phone: numbers()+-.
			radio				//radio
			checkbox			//checkbox
			select-single	//
			select-multi	//
		*/
		//validating: confirm that text is a string
		if (requiredType == "text") {
			//alert(myElement.name);
			if ((myElement.value == null || myElement.value.search(/\w/) == -1) && errorString.indexOf(title) == -1) {
				isNull = true;
			}
		//validating: confirm that text is a valid email format
		} else if (requiredType == "text-email") {
			//alert(myElement.name);
			var str = myElement.value;
			var at="@";
			var dot=".";
			var lstr=str.length;
			if ((myElement.value == null || myElement.value.search(/\w/) == -1) && errorString.indexOf(title) == -1) {
				isNull = true;
			}					
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				isNull = true;
			}
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
				 isNull = true;
			}
		//validating: confirm that text is a valid email format
		} else if (requiredType == "text-int") {
			var str = myElement.value;
			 for (i = 0; i < s.length; i++) {   
				  // Check that current character is number.
				  var c = s.charAt(i);
				  if (((c < "0") || (c > "9"))) return false;
			 }		
		//validating: confirm that text is a valid email format
		} else if (requiredType == "text-phone") {
		
		//validating: confirm that text is a radio button or check box is selected
		} else if (requiredType == "radio" || requiredType == "checkbox") {
			var buttonCount = myElement.length;
			//alert(myElement[0].name);
			isNull = true;
			for (j = 0; j < buttonCount; j++) {
				if (myElement[j].checked) {
					isNull = false;
				}
			}
		//validating: confirm that a option is selected
		} else if (requiredType == "select-single" || requiredType == "select-multiple") {
			//alert(myElement.options[myElement.selectedIndex].value);
			if ((myElement.options[myElement.selectedIndex].value == null || myElement.options[myElement.selectedIndex].value == '') && errorString.indexOf(title) == -1) {
				isNull = true;
			}
		}
		
		//alert(field+' is invalid: '+isNull);
		//SET ERROR
		if (isNull) {
			errorString += title + ", ";
			if (document.getElementById('label_'+field)) { 
				document.getElementById('label_'+field).className="formlabelerror";
			}
			myElement.className="formfielderror";
		} else {
			if (document.getElementById('label_'+field)) {
				document.getElementById('label_'+field).className="formlabelvalid";
			}
			myElement.className="formfieldvalid";
		}

	}
	if (errorString != '') {
		errorString = errorString.slice(0,errorString.length-2);
		window.alert("Please fill in the following required fields before submitting this form:\n\n"+errorString)
		return false;
	}
	else {
		return true;
	}
}

