//alert ("file loading");
//put focus on first field that is wrong
var welder = "";

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus( );
    //elem.select( );
}
function focusElementRadio(formName, elemName) {
	var id = document.forms[formName].elements[elemName][1].id;
	var elem = document.getElementById(id);
	//alert(elem);
    //var elem = document.forms[formName].elements[elemName][1].id;
    elem.focus( );
    //elem.select( );
}
// validate form fields, check required fields are filled in
function checkform(theform,formname){
	//alert(theform.name.value);
	var errormsg = "";
	if((valname(theform.name.value) != null) || (valname(theform.name.value) != "")) {
		errormsg += valname(theform.name.value);
		setTimeout("focusElement('" + formname + "', '" + theform.name.name + "')", 0);
		//alert(theform.name.name);
	}
	if((checkEmail(theform.email.value) != null) || (checkEmail(theform.email.value) != "")) {
		if (errormsg == "")
			setTimeout("focusElement('" + formname + "', '" + theform.email.name + "')", 0);
		errormsg += checkEmail(theform.email.value);
	}
	if((isValidRadio(theform.welding, 'welding') != null) || (isValidRadio(theform.welding, 'welding') != "")) {
		if (errormsg == "")
			setTimeout("focusElementRadio('" + formname + "', 'welding')", 0);
		errormsg += isValidRadio(theform.welding, 'welding');
	}
	if (welder == "No") {
		if((isValidRadio(theform.workwelding, 'workwelding') != null) || (isValidRadio(theform.workwelding, 'workwelding') != "")) {
			if (errormsg == "")
				setTimeout("focusElementRadio('" + formname + "', 'workwelding')", 0);
			errormsg += isValidRadio(theform.workwelding, 'workwelding')
		}	
	}
	if((isValidRadio(theform.doctor, 'doctor') != null) || (isValidRadio(theform.doctor, 'doctor') != "")) {
		if (errormsg == "")
			setTimeout("focusElementRadio('" + formname + "', 'doctor')", 0);
		errormsg += isValidRadio(theform.doctor, 'doctor')
	}
	if((isValidRadio(theform.diagnosed, 'diagnosed') != null) || (isValidRadio(theform.diagnosed, 'diagnosed') != "")) {
		if (errormsg == "")
			setTimeout("focusElementRadio('" + formname + "', 'diagnosed')", 0);
		errormsg += isValidRadio(theform.diagnosed, 'diagnosed')
	}
	//check to see if errors--if so, display error message
	if (!(errormsg == "")){
		var strerrormsg = "Please correct the following items and resubmit your form:\n"
		strerrormsg = strerrormsg + errormsg
		alert(strerrormsg);
		return false;
	} 
}
// validate e-mail
function checkEmail (strng) {
	//alert("okay");
	var error="";
	if (strng == "") {
   		error = "Please enter an e-mail address.\n";
		return error;
	}
	//test valid e-mail format
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
	   return error;
    }
    else {
	//test email for illegal characters
       var illegalChars= /[\&\s\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          	error = "The email address contains illegal characters.\n";
		 	return error;
       }
    }
	return error;   
}

// full name, required, alpha characters only
function valname(strng){
	var error = "";
	if (strng == ""){
		error = "Please enter your name.\n";
		return error;
	}
	//test for numbers
    var illegalChars= /\d/
	if (strng.match(illegalChars)){
		error = "Your name contains numbers.\n"
		return error;
	}
	return error;
}
// validate that the user has checked one of the radio buttons
function isValidRadio(radio,radname) {
	var error = "";
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
			if (radname == "welding")
				welder = radio[i].value;
            return error;
        }
    }
	error = "Please answer whether ";
	switch(radname) {
		case 'welding':
			error += "you worked as a welder.\n";
			break;
		case 'workwelding':
			error += "you worked around welding if you are not a welder.\n";
			break;
		case 'doctor':
			error += "you have seen a doctor for any of the symptoms listed in question No. 3.\n";
			break;
		case 'diagnosed':
			error += "you have been diagnosed with Parkinson's or a disease similar to Parkinson.\n";
			break;
	}
    return error;
}

