// JavaScript Document
//insert the following in form tag 
//document.forms[#].field.optional =true;
//document.forms[#].field.phone=true;
//document.forms[#].field.email =true;
//document.forms[#].field.minlength =#;
//document.forms[#].field.min =#;
//document.forms[#].field.max =#;
//return validateForm(document.forms[#]);

function isBlank(s) {
	for(var i=0; i<s.length; i++) {
	var c = s.charAt[i];
		if((c !=' ')&&(c !='\n')&&(c!='')) return false;
	}
	return true;
}

function isRightSize(element,bottom) {
	if (element.length >= bottom) return false;
return true;
}


function validateForm(f) {
var	msg = 'Your information was not submitted because of the following error(s).\n';
msg += '';
var emptyFields = '';
var errors = '';
var id;
	for (var i=0; i<f.length; i++) {
	var e = f.elements[i];
		if (((e.type=='text') || (e.type=='textarea')) && !e.optional) {
			e.style.backgroundColor='#ffffff';
			if((e.value==null) || (e.value=='') || isBlank(e.value)) {
				emptyFields += 'yes';
				e.style.backgroundColor='#ff0033';
        		                    }
		}
	
		if (e.numeric || (e.min != null) || (e.max != null)) {
			e.style.backgroundColor='#ffffff';
			var v = parseFloat(e.value);
			if (isNaN(v) ||
				 ((e.min != null)&& (v<e.min)) ||
				 ((e.max != null)&&(v>e.max))) {
			errors += 'numerical';
			e.style.backgroundColor='#ff0033';
				}
		}
		
				if (e.minlength && !e.optional) {
			e.style.backgroundColor='#ffffff';
			if (isRightSize(e.value, e.minlength)){
			e.style.backgroundColor='#ff0033';
			errors += 'errors';
			}
		}
		
		
		if (e.selectedIndex>=0 && !e.optional) {
			e.style.backgroundColor='#ffffff';
			if (e.selectedIndex == 0) {
			errors += 'errors';
			e.style.backgroundColor='#ff0033';
			}
		}
		
		if (e.email==true && e.value != "") {
			e.style.backgroundColor='#ffffff';
		var emailpattern = /\w+@\w+\.\w+/;
			if (!emailpattern.test(e.value)){ 
			errors += '- Email address is incorrect\n';
			e.style.backgroundColor='#ff0033';
			}
		}
				if (e.phone==true) {
			e.style.backgroundColor='#ffffff';
		var phonepattern = /\d{3}-\d{3}-\d{4}/;
			if (!phonepattern.test(e.value)){ 
			errors += '- phone number is incorrect\n';
			e.style.backgroundColor='#ff0033';
			}
		}
	}


	if (!emptyFields && !errors) {
	return true;
	}
	if (emptyFields || errors) {
		msg = "The items in red are either not present or are incorrect.\n Please correct your information and try again."
			var newWin = alert(msg);
				return false;
	}

//end of validate top function
}