// JavaScript Document

// this function will put focus on the first field of a form,
// if one exists
function firstFieldFocus() {
	if (document.forms.length != 0) {
		var i = 0;
		// uncheck any disabled radios
		for (i = 0;i <= document.forms.length; i++) {
			element = document.forms[0][i];
			if (element.disabled && element.type == "radio") {
				element.checked = false;
			}
		}
		// focus the first non-hidden, non-disabled element
		element = document.forms[0][i=0];
		while (element.type == "hidden" || element.disabled) {
			element = document.forms[0][++i];
		}
		document.forms[0][i].focus();
	}
}

// use this to validate form fields which have an alt= tag
// and return the text in that tag if there's it's not validated.
// returns true if validated successfully
function validateForm(f) {
	for (i=0; i < f.length; i++) {
		element = f[i];

		// an alt tag signifies a required field.
		//alert("element "+i+" is a "+element.type);
		//alert("alt of "+element.name+" is "+element.alt);
		if ((element.type != "hidden" && element.type != "option")
						&& element.alt != null && element.alt != "") { 
			//alert(element.name+" is "+(element.value == "null"?"a null value":element.value));
			if (element.type == "text" 
						|| element.type == "password" 
						|| element.type == "textarea" 
						|| element.type == "select-one"
						|| element.type == "select-multiple"
						|| element.type == "file") {
				if (element.disabled == false && (element.value == "" || element.value == null || element.value == "null")) {
					alert (element.alt); //display the alert from the alt tag.
					element.focus();
					return false;
				}
				if (element.alt.indexOf("email") >= 0) {
					atSign = element.value.indexOf("@");
					// the at sign can't be at the beginning or end!
					if (atSign <= 0 || atSign == element.value.length - 1) {
						alert ("Please enter a valid email address");
						element.focus();
						return false;
					}
				}
			}

			else if ( element.type == "radio" 
							|| element.type == "checkbox") {
				firstOne = i; focusOne = i;
				//alert ("focusOne is starting as "+focusOne);
				oneWasChecked = false;
				while (i < f.length && element.name == f[firstOne].name) {
					if (element.checked) {
						oneWasChecked = true;
					}
					// looking for a non-disabled one
					if (!element.disabled && f[focusOne].disabled) {
						focusOne = i;
						//alert ("focusOne is now "+focusOne);
					}
					element = f[++i];
				}
				--i; //let the for statement take us to the next element.
				if (!oneWasChecked) {
					alert (f[firstOne].alt); //display the alert from the alt tag.
					f[focusOne].focus();
					return false;
				}
			}
		}
	}
	//f.submit();
	return true;
}

function selectSelector(selectBox,selectValue) {
	i = selectBox.length - 1;
	while (i >= 0 && selectBox.options[i].value != selectValue) {
		--i;
	}
	//alert("i="+i+" "+selectBox.name+".value="+selectBox.options[i].value+" selectValue="+selectValue);
	selectBox.selectedIndex = i;
	
	//selectBox.value = selectValue;
}


