//put focus on first field that is wrong
//alert ("file loading");
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.email.value);
	var errormsg = "";
	if((valname(theform.name.value) != null) || (valname(theform.name.value) != "") || (valname(theform.name.value) != " ")) {
		errormsg += valname(theform.name.value);
		if (errormsg != "")
			setTimeout("focusElement('" + formname + "', '" + theform.name.name + "')", 0);
	}
	//check phone number present
	if((checkPhone(theform.phone.value) != null) || (checkPhone(theform.phone.value) != "")) {
		errormsg += checkPhone(theform.phone.value);
		/*if (errormsg != "")
			setTimeout("focusElement('" + formname + "', '" + theform.phone.name + "')", 0);*/
	}
	//check email present
	if((checkEmail(theform.contact_email.value) != null) || (checkEmail(theform.contact_email.value) != "")) {
		/*if ((errormsg == "") && (checkEmail(theform.email.value) != ""))
			setTimeout("focusElement('" + formname + "', '" + theform.email.name + "')", 0);*/
		errormsg += checkEmail(theform.contact_email.value);
	}
	//insert check number format for zip-code, phone number here
	if(theform.zip) {
		if(!((theform.zip.value == null) || (theform.zip.value.length == 0))) {
			//alert("zip is: " + theform.zip.value);
			var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
			//check for valid US Zipcode
			if((objRegExp.test(theform.zip.value)) == false) {
				errormsg += "Please enter a valid zip code.\n";
			}
		}
	}
	if (formname == "enroll_request") {
		if(theform.workshop_date.selectedIndex == 0) {
			errormsg += isSelected(theform.workshop_date.selectedIndex);
			setTimeout("focusElement('" + formname + "', '" + theform.workshop_date.name + "')", 0)
		}
	}
	if (formname == "vioxxrequest") {
		if((isNotEmpty(theform.state.value, 'state') != null) || (isNotEmpty(theform.state.value, 'state') != "")) {
			if ((errormsg == "") && (isNotEmpty(theform.state.value, 'state') != "")) {
				setTimeout("focusElement('" + formname + "', '" + theform.state.name + "')", 0);
			}
			errormsg += isNotEmpty(theform.state.value, 'state');
		}
	}
	if (formname == "rhogamrequest") {
		if((isNotEmpty(theform.state.value, 'state') != null) || (isNotEmpty(theform.state.value, 'state') != "")) {
			if (errormsg == "") {
				setTimeout("focusElement('" + formname + "', '" + theform.state.name + "')", 0);
			}
			errormsg += isNotEmpty(theform.state.value, 'state');
		}
		if((isValidRadio(theform.injections, 'injections') != null) || (isValidRadio(theform.injections, 'injections') != "")) {
			if (errormsg == "")
				setTimeout("focusElementRadio('" + formname + "', 'injections')", 0);
			errormsg += isValidRadio(theform.injections, 'injections')
		}
		if((isNotEmpty(theform.children_info.value, 'children_info') != null) || (isNotEmpty(theform.children_info.value, 'children_info') != "")) {
			if (errormsg == "") {
				setTimeout("focusElement('" + formname + "', '" + theform.children_info.name + "')", 0);
			}
			errormsg += isNotEmpty(theform.children_info.value, 'children_info');
		}
		//alert("in rhogam form checker");
	}
	//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 phone
function checkPhone (strng) {
	//alert("okay - phone");
	var error="";
	if ((strng == "") || (strng == null)) {
		error = "Please enter a phone number so we may contact you.\n";
		return error;
	}
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(stripped)) {
		error = "Please enter a valid phone number, including area code, so we may contact you.\n";
	}
	if (!(stripped.length == 10)) {
		error = "Please enter a valid phone number, including area code, so we may contact you.\n";	
	}
	return error;
}
// validate e-mail
function checkEmail(string) {
	var error="";
	if ((string == "") || (string == null)) {
   		error = "Please enter an e-mail address.\n";
		return error;
	}
	//test valid e-mail format
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(string))) { 
       error = "Please enter a valid email address.\n";
	   return error;
    }
    else {
	//test email for illegal characters
       var illegalChars= /[\&\s\(\)\<\>\,\;\:\\\"\[\]]/;
         if (string.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 (possibly space in name?)
    var illegalChars= /\d/
	if (strng.match(illegalChars)){
		error = "Your name contains numbers.\n"
		return error;
	}
	return error;
}
//validate that form element is not empty
function isNotEmpty(elem,elname) {
	var error = ""
    //var str = elem.value;
    if(elem == null || elem.length == 0) {
        error = "Please fill in "
		switch(elname) {
			case 'state':
				error += "the state you reside in.\n";
				break;
			case 'children_info':
				error += "the names and birthdates of your children.\n";
				break;
		}
	}
    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) {
            return error;
        }
    }
	error = "Please choose ";
	switch(radname) {
		case 'frequency':
			error += "the frequency with which you took Vioxx.\n";
			break;
		case 'dosage':
			error += "the dosage of Vioxx you took.\n";
			break;
		case 'heartproblems':
			error += "the type of heart problems you or a loved one may have experienced.\n";
			break;
		case 'injections':
			error += "whether you received RhoGAM injections while pregnant.\n";
			break;
	}
    return error;
}
function isSelected(option_value) {
	var error = "";
	if (option_value == 0) {
		error = "Please choose the date you wish to attend the workshop.";
	}
	return error;
}
