// created by Jarred and Mike
var pass;
var brokeobj;
var brokeobj_className;
var inRadioGroup;
var RadioChecked;

	function checkrequired(which)
	{
		var i;
		var prefix;
		
		if (document.images)
		{
			//restore previous border; need to loop because previously highlighted objects could be ahead of new broken object
			if (brokeobj) {
				document.getElementById(brokeobj).className=brokeobj_className; 
			}
			inRadioGroup="";
			RadioChecked=false;
			pass=true;
		
			for (i=0;i<which.length;i++)
			{
				var tempobj=which.elements[i]
				if (tempobj.name.substring(0,8)=="required")
				{
					//check radio
					if (tempobj.type=="radio") {
						//alert("required object: " + tempobj.name + ":" + tempobj.type + "=" + tempobj.value);
						if (inRadioGroup) {
						//alert("inRadioGroup");
							if (tempobj.name !=inRadioGroup) {
								// entered new radio group
								if (!RadioChecked) {
									highlightInput(tempobj.parentNode);
									break;									
								}
								inRadioGroup=tempobj.name;
								RadioChecked=false;
							}
							// in current radio group
						}
						else {
							inRadioGroup = tempobj.name;
						}
						
						if (tempobj.checked) {
							RadioChecked=true;
						}						
					}
					else if (i>1&&(isInvalidRadioGroup(which.elements[i-1].parentNode))) {
						//alert("break");
						highlightInput(which.elements[i-1].parentNode);
						break;
					}
					
					//check text & textarea
					if (tempobj.type=="text"||tempobj.type=="textarea") {
						if (isInvalidTextInput(tempobj)) {						
							highlightInput(tempobj)
							break;
						}
					}
					
					//check select
					else if (tempobj.type=="select-one") {
						if (isInvalidSelectInput(tempobj)) {
							highlightInput(tempobj)
							break;							
						}
					}
				}
				//found non-required object, and radio group didn't validate
				else if (i>1&&(isInvalidRadioGroup(which.elements[i-1].parentNode))) {
					highlightInput(which.elements[i-1].parentNode);
					break;
				}
				// validate confirm email
				if (tempobj.name.substr(tempobj.name.length-12) == "ConfirmEmail") {
					prefix=tempobj.name.substr(0,tempobj.name.length-12);
					email=document.getElementById(prefix + "Email");
					if (email.value != tempobj.value) {
						alert("Confirm email does not match email.");
						highlightInput(tempobj);
						return false;
					}
				}
			}
		}

		if (!pass) {
			alert("One or more of the required elements are not completed. Please complete them, then submit again.");
			return false
		}

	}
	
function charLoop(obj) { 
/// Only checks ALPHA characters ///
/// It is possible to check numbers :: start the loop at 48 ///
	value = obj.value.toUpperCase();
		var o = 1;
	for(i=48; i<=90; i++){
		var alpha = String.fromCharCode(i);
		if(value.indexOf(alpha) > -1){
		break;
		}
		o ++;
		if(i >=90){
			highlightInput(obj);
			break;
		}
	}
}
	
function isInvalidTextInput(obj) {
	if (obj.value=='' || charLoop(obj)) {
		return true;
	}
	return false;
}

function isInvalidSelectInput(obj) {
	if (obj.selectedIndex==0) {
		return true;
	}
	return false;
}

function isInvalidRadioGroup(pobj) {
	if (inRadioGroup && !RadioChecked) {
		highlightInput(pobj);
		return true;
	return false;
	}
}		
		
function highlightInput(obj) {
		pass=false;
		brokeobj=obj.id;
		brokeobj_className=obj.className;
		obj.className = "requiredHighlight";
		obj.focus();
		return;
}