// Form behavior

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function EvM_validateForm() { //based on MM v4.0
// Possible 'theTest' strings:
//
// Checked	the single checkbox is checked
//
// R		required
// Rxxxx	where "xxxx" is one of the items below: both Required and formatted as follows...
//
// isEmail	formatted x@y
// isZip	containing 5 or 9 digits
// inRangex:xx	(partially working) Number range

	var fieldName,label,obj,val,theTest,required;
	var i,j,pos,min,max,number,found;
	var errors='';
	var emailFilter=/^.+@.+\..{2,6}$/;
	var allWhitespace=/^\s+$/;
	var args=EvM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		theTest=args[i+2];
		label=args[i+1];
		fieldName=args[i];
		if (label=='') {
			label=fieldName;
		}
		obj=MM_findObj(fieldName);
		if (obj) {
			val=obj.value;
			//Treat a whitespace-only entry as empty.
			if (allWhitespace.test(val)) val="";
			
			//Parse 'theTest' string for "R" (required) at the beginning
			required = false;
			if (theTest.indexOf('R') == 0) {
				required = true;
				theTest = theTest.substring(1);
			}
			
			//Checked
			if (theTest=="Checked") {
				if (!obj.checked) errors+='\n- "'+label+'" checkbox must be checked.';

			//AnyChecked
			} else if (theTest=="AnyChecked") {
				//look at one or more checkboxes with the same "name" property
				if (obj.length) {
					//it's an array of checkboxes
					found = false;
					for (j=0; !found && j<obj.length; j++) {
						if (obj[j].checked) found=true;
					}
					if (!found) errors+='\n- "'+label+'" must have at least one checkbox checked.';
				} else if (!obj.checked) errors+='\n- "'+label+'" checkbox must be checked.';

			} else if (val=="") {
				if (required) errors += '\n- '+label+' is required.';
			
			//isEmail
			} else if (theTest=="isEmail") {
				if (!(emailFilter.test(val))) errors+='\n- '+label+' must contain a proper e-mail address.';

			//isZip
			} else if (theTest=="isZip") {
				number = digitCount(val);
				if ((number!=5) && (number!=9)) errors+='\n- '+label+' must be either 5 digits or 9 digits.';

			//isPhone
			} else if (theTest=="isPhone") {
				number = digitCount(val);
				if (number!=10) errors+='\n- '+label+' must be ten digits.\n     Example: 800-555-1234';

			//isNumber*
			} else if (theTest.indexOf('isNumber')==0) {
				if (isNaN(val)) errors+='\n- '+label+' must contain a number.';
/* Not yet baked...
pos=theTest.indexOf(':');
min=theTest.substring(8,pos);
max=theTest.substring(pos+1);
if (pos > 8) {
	if (isNaN(val)) errors+='- '+label+' must contain a number.\n';
	if (val<min || max<val) errors+='- '+label+' must contain a number between '+min+' and '+max+'.\n';
}
*/
			}
		}
	}
	if (errors) {
		alert('Please correct the following:\n'+errors);
		return false;
	} else return true;
}

function digitCount(str) {
	var i,goodChars="0123456789",total=0;
	for (i=0;i<str.length; i++) {
		if(goodChars.indexOf(str.substring(i,i+1)) > -1) total++;
	}
	return total;
}

function disableByPrefix (whichForm, prefix, newDisabledState, grayId) {
	if (whichForm) {
		for (i=0; i < whichForm.elements.length; i++) {
			if ((whichForm.elements[i].name != null) &&
				(whichForm.elements[i].name.substr(0, prefix.length) == prefix)) {
				whichForm.elements[i].value = "";
				whichForm.elements[i].disabled = newDisabledState;
				whichForm.elements[i].readonly = newDisabledState;
			}
		}
	}
	if (grayId && document.getElementById && document.getElementById(grayId) && document.getElementById(grayId).className) {
		if (newDisabledState) {
			document.getElementById(grayId).className="formSectionDisabled"
		} else {
			document.getElementById(grayId).className="formSectionEnabled";
		}
	}
}

//Return TRUE if a key is pressed which will be visible in a form field (including space bar).
//For use with onKeyPress.
function isTriggerKey(e) {

	if (window.event) {  // IE
		keynum = e.keyCode;
	}
	else if (e.which) {  // Netscape, Firefox, Opera
		keynum = e.which;
	}
	else return false;
	
	//document.forms['mainForm'].Full_Name.value = keynum;  //debug

	return !(keynum < 32 || keynum > 126);
}

function markChecked(whichForm, whichElt) {
	if(whichForm && whichForm.elements[whichElt]) {
		whichForm.elements[whichElt].checked = true;
	}
}

function putFocus(whichForm, whichElt) {
	if(whichForm && whichForm.elements[whichElt]) {
		whichForm.elements[whichElt].focus();
	}
}