/*
Form Validation Version .92
Last updated: 10/19/04 by Juan Ulloa
Chunks of code from Juan Ulloa, Joseph Myers, website:Ask fengall, and others.

Script only works with radio buttons if radio buttons that need validation are next to each other. If there is an error with a radion button, only the last radio button highlights

Future updates: 
	-fix radio button inconsistencies, 	
	-set up to display the errors on the page instead of popups
*/

//only to highlight/de-highlight background color of invalid form fields
var highlightFormItems = true; 
var formObjHighlight = "#ffffcc";
var formObjBg = 'white';

//default error message and bullets when listing the error message
var errorMessageIntro = "The following fields are required:";
var errorMessageBullet = " -  ";

// gets rid of white space
function nwts(s) { 
return s.replace(/\s+/g, '');
}

//checks if e-mail address has correct formating
function email(s) 
{
	a = s.match(/\S+@([-\w]+\.)+\w+/g);
	return a;
}

//checks for correct URL formating
function url(s) {
a = s.match(/\w{2,}:\/{2}([-\w]+\.)+\w+\S*/g);
return a;
}

// any number
function number(s) { 

if (nwts(s) == "") return true;
a = isNaN(s);
return a;	 
		 
/*s = nwts(s);
alert ("'" +s+ "'");
if (s=="") return true 
a = isNaN(s);
return a;
*/
}

function highlight(obj) {
	if (highlightFormItems == true)	obj.style.backgroundColor = formObjHighlight;
	return;
}
function clearHighlight(obj) {
	if (highlightFormItems == true) obj.style.backgroundColor = formObjBg;
	return;
}


function validate(form, list) {
	valid = true;
	errorStr = errorMessageIntro;
	errPrefix = "\n" + errorMessageBullet;

	
	
	for (i=0; i<form.elements.length; i++) {
		var element = form.elements[i];
		var n = element.name;
		


		if (list[n] && list[n].verify) {
			clearHighlight(element);
			
			switch (list[n].verify){
				case "required":
					
					//Text field or Text area
					if (element.type == "text" || element.type == "textarea"){
						var trim = nwts(element.value);
						if (trim == ""){
							errorStr+= errPrefix+list[n].message;
							highlight(element);
							valid = false;
						}
					}
					
					//checkbox
					else if (element.type == "checkbox" & element.checked == false){
						errorStr+= errPrefix+list[n].message;
                		highlight(element);
						valid = false;
					}
					
					//select-one
					else if (element.type == "select-one" && element.value == ""){
						errorStr+= errPrefix+list[n].message;
						highlight(element);
						valid = false;
					}
					//select-multiple
					else if (element.type == "select-multiple" && element.value == ""){
						errorStr+= errPrefix+list[n].message;
						highlight(element);
						valid = false;
					}
					

					//radio
					else if (element.type == "radio"){
						
							radName = form.elements[i].name;
							radChecked = false;
							
							do {
							if (form.elements[i].checked)
							radChecked = true;
							i++;
							} while (form.elements[i].name == radName);
							--i;
							
							if(radChecked == false){
								errorStr+= errPrefix+list[n].message;
								highlight(element);
								valid = false;
							}
						
						
					}
					break;
				case "email":
					if ( element.value == "" || email(element.value) == null){
                		errorStr+= errPrefix+list[n].message;
                		highlight(element);
						valid = false;
					};
					break;
				case "url":
					if ( element.value == "" || url(element.value) == null){
                		errorStr+= errPrefix+list[n].message;
                		highlight(element);
						valid = false;
					};
					break;
				case "number":
					if ( number(element.value) == true){
                		errorStr+= errPrefix+list[n].message;
                		highlight(element);
						valid = false;
					};
					break;
				default:
					//if this happens, script does not support some form field.
					alert ("CODE ERROR: the verify option not found");
			}
		}
	}
	if (valid == false){
		alert (errorStr);
	} 
	return valid;
}