/* requestform.js
 * Author:      David Coleman / Ryan Emling
 * Purpose:     Provides functions to:
 *				get form html,
 *				validate,
 * 				and submit contact request forms to http server (to be emailed) via ajax
 * Modified:    1/23/2009
 * Note:  		This script is a rewrite of Peter Schart's inforequestform.js.
 */

function trim(s) {
   	return s.replace(/^\s+|\s+$/g,"");
}

// validates a date
function validDate(fld) {

	var testMo, testDay, testYr, inpMo, inpDay, inpYr, msg;
    var inp = fld;
    status = "";

	// attempt to create date object from input data
    var testDate = new Date(inp);

	// extract pieces from date object
    testMo = testDate.getMonth() + 1;
    testDay = testDate.getDate();
    testYr = testDate.getFullYear();

	// extract components of input data
    inpMo = parseInt(inp.substring(0, inp.indexOf("/")), 10);
    inpDay = parseInt(inp.substring((inp.indexOf("/") + 1), inp.lastIndexOf("/")), 10);
    inpYr = parseInt(inp.substring((inp.lastIndexOf("/") + 1), inp.length), 10);

	// make sure parseInt() succeeded on input components
    if (isNaN(inpMo) || isNaN(inpDay) || isNaN(inpYr)) {
        msg = "There is some problem with your date entry.";
    }

	// make sure conversion to date object succeeded
    if (isNaN(testMo) || isNaN(testDay) || isNaN(testYr)) {
        msg = "Couldn't convert your entry to a valid date. Try again.";
    }

	// make sure values match
    if (testMo != inpMo || testDay != inpDay || testYr != inpYr) {
        msg = "Check the range of your date value.";
    }

	if (msg) {
        // there's a message, so something failed
        return false;
    } else {
        // ok
        return true;
    }
}

/* getForm()
 * Author:      David Coleman
 * Purpose:     Gets form html from server via ajax
 * Parameters:  form_id: corresponds to form user is requesting via radio buttons
 * Returns:     Nothing; Updates contact_form_fields <div>
 * Modified:    1/23/2009
 */
function getForm(form_id) {
	var formRequest;
	var params = "form_id=" + form_id;
	switch(form_id) {
		case 'sme':
			// Subject Matter Expert Form
			formRequest = new Request({
				url: '/cgi-bin/get_request_form.pl',
				method: 'post',
				onSuccess: function(responseText, responseXML) {
					$('contact_form_fields').innerHTML = responseText;
					document.contact_form.action = 'javascript:submit_form_sme()';
				},
				onFailure: function(xhr) {
					$('contact_form_fields').innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
				}
			});
			break;
		case 'sir':
			// Supervisor's Irregularity Report Form
			formRequest = new Request({
				url: '/cgi-bin/get_request_form.pl',
				method: 'post',
				onSuccess: function(responseText, responseXML) {
					$('contact_form_fields').innerHTML = responseText;
					document.contact_form.action = 'javascript:submit_form_sir()';
				},
				onFailure: function(xhr) {
					$('contact_form_fields').innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
				}
			});;
			break;
		case 'sar':
			// Special Accommodations Request Form
			formRequest = new Request({
				url: '/cgi-bin/get_request_form.pl',
				method: 'post',
				onSuccess: function(responseText, responseXML) {
					$('contact_form_fields').innerHTML = responseText;
					document.contact_form.action = 'javascript:submit_form_sar()';
				},
				onFailure: function(xhr) {
					$('contact_form_fields').innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
				}
			});;
			break;
		case 'tau':
			// Test Administrator Update Form
			formRequest = new Request({
				url: '/cgi-bin/get_request_form.pl',
				method: 'post',
				onSuccess: function(responseText, responseXML) {
					$('contact_form_fields').innerHTML = responseText;
					document.contact_form.action = 'javascript:submit_form_tau()';
				},
				onFailure: function(xhr) {
					$('contact_form_fields').innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
				}
			});;
			break;
    	case 'collateral':
    		// marketing materials request
    		formRequest = new Request({
    			url: '/cgi-bin/get_request_form.pl',
    			method: 'post',
    			onSuccess: function(responseText, responseXML) {
    				$('rightCol').style.display = "none";
    				$('contact_form_fields').innerHTML = responseText;
    				document.contact_form.action = 'javascript:submit_form_collateral()';
    			},
    			onFailure: function(xhr) {
    				$('contact_form_fields').innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
    			}
    		});;
    		break;
		default:
			// default contact form
			if ($('general').checked) {
			    params += "&identify_user=1";
			} else {
			    params += "&identify_user=0";
			}
			formRequest = new Request({
				url: '/cgi-bin/get_request_form.pl',
				method: 'post',
				onSuccess: function(responseText, responseXML) {
					$('contact_form_fields').innerHTML = responseText;
					document.contact_form.action = 'javascript:submit_form_default(\''+form_id+'\')';
				},
				onFailure: function(xhr) {
					$('contact_form_fields').innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
				}
			});
			break;
	}
   	formRequest.send(params);
}

/* submit_form_sme()
 * Author:      David Coleman
 * Purpose:     Validates and sends Subject Matter Expert form data to server
 * Parameters:  None
 * Returns:     Nothing;  Updates contact_form HTML with status message (error or OK)
 * Modified:    1/23/2009
 */
function submit_form_sme() {
	var errors = '';
	var params = '';

	var lastName = document.contact_form.lastName.value;
	if (!$defined(lastName) || lastName == '') {
		errors += 'Last Name is missing<br />';
	} else {
		params += 'last_name='+escape(trim(lastName));
	}

	var firstName = document.contact_form.firstName.value;
	if (!$defined(firstName) || firstName == '') {
		errors += 'First Name is missing<br />';
	} else {
   		params += '&first_name='+escape(trim(firstName));
	}

	var degree = document.contact_form.degree.value;
	if (!$defined(degree) || degree == '') {
		errors += 'Degree of Education is missing<br />';
	} else {
	   	params += '&degree='+escape(trim(degree));
	}

	var currentEmployer = document.contact_form.employer.value;
	if (!$defined(currentEmployer) || currentEmployer == '') {
		//errors += 'Current Employer is missing<br />';
	} else {
		params += '&employer='+escape(trim(currentEmployer));
	}

	var phone = document.contact_form.phone.value;
	if (!$defined(phone) || phone == '') {
		errors += 'Phone number is missing<br />';
	} else {
		params += '&phone='+escape(trim(phone));
	}

	var email = document.contact_form.email.value;
	if (!$defined(email) || email == '') {
		errors += 'E-mail address is missing<br />';
	} else {
		params += '&email='+escape(trim(email));
	}

	// get user's chosen dsst subject matters
	var subjectSelectObj = document.contact_form.subject_matters;
   	if ($defined(subjectSelectObj)) {
		for (var i = 0; i < subjectSelectObj.options.length; i++) {
			if (subjectSelectObj.options[i].selected) {
				params += '&subject_matters='+subjectSelectObj.options[i].value;
			}
		}
	} else {
		errors += 'Error with DSST Subject Matter selection';
	}
	// make sure user has chosen at least one subject
	if (params.indexOf('subject_matters') < 0) {
		// user has not chosen at least one subject
		errors += 'You must choose at least one DSST subject from the list.';
	}

	var comments = document.contact_form.comments.value;
	if ($defined(comments) && comments != '') {
		params += '&comments='+escape(trim(comments));
	}

	if (errors != '') {
		// display errors and return
		errors = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid. Please correct the following errors then click 'Submit' again:</p><p>" + errors + "</p>";
		$('errordiv').innerHTML = errors;
		$('errordiv').style.display = 'block';
		return;
	}

	params += '&form_id=sme';
	// send form data to server
	var formSubmit = new Request({
		url: '/cgi-bin/submit_request_form.pl',
		method: 'post',
		onSuccess: function(responseText, responseXML) {
			document.contact_form.innerHTML = responseText;
		},
		onFailure: function(xhr) {
			document.contact_form.innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
		}
	});
	formSubmit.send(params);
}

/* submit_form_default()
 * Author:      David Coleman
 * Purpose:     Validates and sends the default contact form data to server
 * Parameters:  None
 * Returns:     Nothing;  Updates contact_form HTML with status message (error or OK)
 * Modified:    1/23/2009
 */
function submit_form_default(id) {
	var errors = '';
	var params = '';
    var schoolName = document.contact_form.schoolName.value;
    if (!$defined(schoolName) || schoolName == '') {
		errors += 'School Name is missing<br />';
	} else {
		params += 'school_name='+escape(trim(schoolName));
	}
	
	var lastName = document.contact_form.lastName.value;
	if (!$defined(lastName) || lastName == '') {
		errors += 'Last Name is missing<br />';
	} else {
		params += '&last_name='+escape(trim(lastName));
	}

	var firstName = document.contact_form.firstName.value;
	if (!$defined(firstName) || firstName == '') {
		errors += 'First Name is missing<br />';
	} else {
   		params += '&first_name='+escape(trim(firstName));
	}

	var address1 = document.contact_form.address1.value;
	if (!$defined(address1) || address1 == '') {
		errors += 'Address is missing<br />';
	} else {
		params += '&address1='+escape(trim(address1));
	}

	var address2 = document.contact_form.address2.value;
	if ($defined(address2)) {
		params +='&address2='+escape(trim(address2));
	}

	var city = document.contact_form.city.value;
	if (!$defined(city) || city == '') {
		errors += 'City is missing<br />';
	} else {
		params += '&city='+escape(trim(city));
	}

	var stateProvince = document.contact_form.stateProvince.value;
	if (!$defined(stateProvince) || stateProvince == '') {
		errors += 'State/Province is missing<br />';
    } else {
		params += '&state_province='+escape(trim(stateProvince));
	}

	var postalCode = document.contact_form.postalCode.value;
	if (!$defined(postalCode) || postalCode == '') {
		errors += 'Postal Code is missing<br />';
	} else {
		params += '&postal_code='+escape(trim(postalCode));
	}
	
	var countryCode = document.contact_form.country.value;
	if (!$defined(countryCode) || countryCode == '') {
	    errors += 'Country is missing<br />';
	} else {
	    params += '&country_code='+escape(trim(countryCode));
	}

	var phone = document.contact_form.phone.value;
	if (!$defined(phone) || phone == '') {
		//errors += 'Phone number is missing<br />';
	} else {
		params += '&phone='+escape(trim(phone));
	}

	var fax = document.contact_form.fax.value;
	if ($defined(fax)) {
		params += '&fax='+escape(trim(fax));
	}

	var email = document.contact_form.email.value;
	if (!$defined(email) || email == '') {
		errors += 'E-mail address is missing<br />';
	} else {
		params += '&email='+escape(trim(email));
	}

	var comments = document.contact_form.comments.value;
	if ($defined(comments)) {
		params += '&comments='+escape(trim(comments));
	}

	if (errors != '') {
		// display errors and return
		errors = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid. Please correct the following errors then click 'Submit' again:</p><p>" + errors + "</p>";
		$('errordiv').innerHTML = errors;
		$('errordiv').style.display = 'block';
		return;
	}

    if (id == undefined || id == null || !id) {
        id = 'default';
    }
	params += '&form_id='+id;

	var classification;
	for (var i = 0; i < document.contact_form.classification.length; i++) {
		if (document.contact_form.classification[i].checked) {
			classification = document.contact_form.classification[i].value;
		}
	}
	if (!$defined(classification)) {
		errors += 'You must choose one of the contact options above.<br />';
    } else {
		params += '&classification='+classification;
	}

	var userType = "testcenter_user";
	if ($('student_user') != null) {
	    if ($('student_user').checked) {
	        userType = "student_user";
	    }
    }
	params += '&user_type='+userType;

	// send form data to server
	var formSubmit = new Request({
		url: '/cgi-bin/submit_request_form.pl',
		method: 'post',
		onSuccess: function(responseText, responseXML) {
			document.contact_form.innerHTML = responseText;
			scrollTo(0,0);
		},
		onFailure: function(xhr) {
			document.contact_form.innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
			scrollTo(0,0);
		}
	});
	formSubmit.send(params);
}

/* submit_form_sir()
 * Author:      Ryan Emling
 * Purpose:     Validates and sends the Supervisor's Irregulatory Report form data to server
 * Parameters:  None
 * Returns:     Nothing;  Updates contact_form HTML with status message (error or OK)
 * Modified:    2/24/2009
 */
function submit_form_sir() {
	var errors = '';
	var params = '';

	var testDate = document.contact_form.testDate.value;
	var dateFlag = validDate(testDate);
	if (!dateFlag) {
		errors += 'Test Date is not a valid date<br />';
	} else {
		params += 'test_date='+ testDate;
	}

	var testCenterCode = document.contact_form.testCenterCode.value;
	if (!$defined(testCenterCode) || testCenterCode == '') {
		errors += 'DSST Test Center Code is missing<br />';
	} else {
   		params += '&test_center_code='+escape(trim(testCenterCode));
	}

	var helpDeskId = document.contact_form.helpdesk_id.value;
	if (!$defined(helpDeskId) || helpDeskId == '') {

	} else {
	    params += '&helpdesk_id='+escape(trim(helpDeskId));
	}

	var adminName = document.contact_form.adminName.value;
	if (!$defined(adminName) || adminName == '') {
		errors += 'Test Administrator\'s Name is missing<br />';
	} else {
		params += '&admin_name='+escape(trim(adminName));
	}

	var candidatesInvolved = document.contact_form.candidatesInvolved.value;
	if (!$defined(candidatesInvolved) || candidatesInvolved == '') {
		errors += 'List the Candidates Involved is missing<br />';
	} else {
		params += '&candidates_involved='+escape(trim(candidatesInvolved));
	}

	var describeIssue = document.contact_form.describeIssue.value;
	if (!$defined(describeIssue) || describeIssue == '') {
		errors += 'Describe the Issue is missing<br />';
	} else {
		params += '&describe_issue='+escape(trim(describeIssue));
	}

	if (document.contact_form.checkBox.checked == true) {
		params += '&check_box='+'checked';
	} else {
	    params += '&check_box='+'unchecked';
	}

	var email = document.contact_form.email.value;
	if (!$defined(email) || email == '') {
		errors += 'Proctor\'s Email is missing<br />';
	} else if (email.match(/^(((\w+\-*\w+)|\w+)+\.??)+\@(((\w+\-*\w+)|(\w+))\.)+\w+$/)) {
	    	  params += '&email='+escape(trim(email));
		   } else {
				errors += 'Email is not valid<br />';
			 }

	var phoneNumber = document.contact_form.phoneNumber.value;
	if (!$defined(phoneNumber) || phoneNumber == '') {
		errors += 'Telephone Number is missing<br />';
	} else {
		params += '&phone_number='+escape(trim(phoneNumber));
	}

	var testCenterName = document.contact_form.testCenterName.value;
	if (!$defined(testCenterName) || testCenterName == '') {
		errors += 'Test Center Name is missing<br />';
	} else {
		params += '&test_center_name='+escape(trim(testCenterName));
	}

	var testCenterAddress1 = document.contact_form.testCenterAddress1.value;
	if (!$defined(testCenterAddress1) || testCenterAddress1 == '') {
		errors += 'Test Center Address is missing<br />';
	} else {
   		params += '&test_center_address1='+escape(trim(testCenterAddress1));
	}

	var testCenterAddress2 = document.contact_form.testCenterAddress2.value;
	if ($defined(testCenterAddress2)) {
		params +='&test_center_address2='+escape(trim(testCenterAddress2));
	}

	var testCenterCity = document.contact_form.testCenterCity.value;
	if (!$defined(testCenterCity) || testCenterCity == '') {
		errors += 'Test Center City is missing<br />';
	} else {
		params += '&test_center_city='+escape(trim(testCenterCity));
	}

	var testCenterState = document.contact_form.testCenterState.value;
	if (!$defined(testCenterState) || testCenterState == '') {
		errors += 'Test Center State is missing<br />';
	} else {
		params += '&test_center_state='+testCenterState.toUpperCase();
	}

	var testCenterZipcode = document.contact_form.testCenterZipcode.value;
	if (!$defined(testCenterZipcode) || testCenterZipcode == '') {
		errors += 'Test Center Zipcode is missing<br />';
	} else {
		params += '&test_center_zipcode='+escape(trim(testCenterZipcode));
	}

	var adminSignature = document.contact_form.adminSignature.value;
	if (!$defined(adminSignature) || adminSignature == '') {
		errors += 'DSST Test Administrator Signature is missing<br />';
	} else {
		params += '&admin_signature='+escape(trim(adminSignature));
	}

	var sigDate = document.contact_form.sigDate.value;
	var validLength = document.contact_form.sigDate.value.length;
	dateFlag = validDate(sigDate);
	if (!dateFlag || validLength != 10) {
		errors += 'Signature Date is not a valid date<br />';
	} else {
		params += '&sig_date='+ sigDate;
	}

	if (errors != '') {
		// display errors at the top of the form
		errors = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid. Please correct the following errors then click 'Submit' again:</p><p>" + errors + "</p>";
		$('errordiv').innerHTML = errors;
		$('errordiv').style.display = 'block';
		// display error message at the bottom of the form
		$('errordivBottom').innerHTML = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid.&nbsp;&nbsp;&nbsp;&nbsp;See list above."
		$('errordivBottom').style.display = 'block';
		scrollTo(0,0);
		return;
	 }

	 params += '&form_id=sir';
	 // send form data to server
	 var formSubmit = new Request({
	 	 url: '/cgi-bin/submit_request_form.pl',
		 method: 'post',
		 onSuccess: function(responseText, responseXML) {
			document.contact_form.innerHTML = responseText;
		 },
		 onFailure: function(xhr) {
			document.contact_form.innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
		 }
	 });
	 formSubmit.send(params);

}

/* submit_form_sar()
 * Author:      Ryan Emling
 * Purpose:     Validates and sends the Special Accommodations Request form data to server
 * Parameters:  None
 * Returns:     Nothing;  Updates contact_form HTML with status message (error or OK)
 * Modified:    2/25/2009
 */
function submit_form_sar() {
	var errors = '';
	var params = '';

	var disability = document.contact_form.disability.value;
	if (!$defined(disability) || disability == '') {
		errors += 'Disability is missing<br />';
	} else {
		params += 'disability='+escape(trim(disability));
	}

	var accommodations = document.contact_form.accommodations.value;
	if (!$defined(accommodations) || accommodations == '') {
		errors += 'Accommodations are missing<br />';
	} else {
		params += '&accommodations='+escape(trim(accommodations));
	}

	var testCenterName = document.contact_form.testCenterName.value;
	if (!$defined(testCenterName) || testCenterName == '') {
		errors += 'Test Center Name is missing<br />';
	} else {
		params += '&test_center_name='+escape(trim(testCenterName));
	}

	var testCenterAddress1 = document.contact_form.testCenterAddress1.value;
	if (!$defined(testCenterAddress1) || testCenterAddress1 == '') {
		errors += 'Test Center Address is missing<br />';
	} else {
   		params += '&test_center_address1='+escape(trim(testCenterAddress1));
	}

	var testCenterAddress2 = document.contact_form.testCenterAddress2.value;
	if ($defined(testCenterAddress2)) {
		params +='&test_center_address2='+escape(trim(testCenterAddress2));
	}

	var testCenterCity = document.contact_form.testCenterCity.value;
	if (!$defined(testCenterCity) || testCenterCity == '') {
		errors += 'Test Center City is missing<br />';
	} else {
		params += '&test_center_city='+escape(trim(testCenterCity));
	}

	var testCenterState = document.contact_form.testCenterState.value;
	if (!$defined(testCenterState) || testCenterState == '') {
		errors += 'Test Center State is missing<br />';
	} else {
		params += '&test_center_state='+testCenterState.toUpperCase();
	}

	var testCenterZipcode = document.contact_form.testCenterZipcode.value;
	if (!$defined(testCenterZipcode) || testCenterZipcode == '') {
		errors += 'Test Center Zipcode is missing<br />';
	} else {
		params += '&test_center_zipcode='+escape(trim(testCenterZipcode));
	}

	var lastName = document.contact_form.lastName.value;
	if (!$defined(lastName) || lastName == '') {
		errors += 'Last Name is missing<br />';
	} else {
		params += '&last_name='+escape(trim(lastName));
	}

	var firstName = document.contact_form.firstName.value;
	if (!$defined(firstName) || firstName == '') {
		errors += 'First Name is missing<br />';
	} else {
   		params += '&first_name='+escape(trim(firstName));
	}

	var address1 = document.contact_form.address1.value;
	if (!$defined(address1) || address1 == '') {
		errors += 'Your Address is missing<br />';
	} else {
		params += '&address1='+escape(trim(address1));
	}

	var address2 = document.contact_form.address2.value;
	if ($defined(address2)) {
		params +='&address2='+escape(trim(address2));
	}

	var city = document.contact_form.city.value;
	if (!$defined(city) || city == '') {
		errors += 'Your City is missing<br />';
	} else {
		params += '&city='+escape(trim(city));
	}

	var state = document.contact_form.state.value;
	if (!$defined(state) || state == '') {
		errors += 'Your State is missing<br />';
    } else {
		params += '&state='+state.toUpperCase();
	}

	var zipcode = document.contact_form.zipcode.value;
	if (!$defined(zipcode) || zipcode == '') {
		errors += 'Your Zipcode is missing<br />';
    } else {
		params += '&zipcode='+escape(trim(zipcode));
	}

	var email = document.contact_form.email.value;
	if (!$defined(email) || email == '') {
		errors += 'Email is missing<br />';
	} else if (email.match(/^(((\w+\-*\w+)|\w+)+\.??)+\@(((\w+\-*\w+)|(\w+))\.)+\w+$/)) {
	    	  params += '&email='+escape(trim(email));
		   } else {
				errors += 'Email is not valid<br />';
			 }

	var signature = document.contact_form.signature.value;
	if (!$defined(signature) || signature == '') {
		errors += 'Signature is missing<br />';
    } else {
		params += '&signature='+escape(trim(signature));
	}

	var date = document.contact_form.date.value;
	var validLength = document.contact_form.date.value.length;
	var dateFlag = validDate(date);
	if (!dateFlag || validLength != 10) {
		errors += 'Date is not a valid date<br />';
	} else {
		params += '&date='+ date;
	}

	if (errors != '') {
		// display errors at the top of the form
		errors = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid. Please correct the following errors then click 'Submit' again:</p><p>" + errors + "</p>";
		$('errordiv').innerHTML = errors;
		$('errordiv').style.display = 'block';
		// display error message at the bottom of the form
		$('errordivBottom').innerHTML = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid.&nbsp;&nbsp;&nbsp;&nbsp;See list above."
		$('errordivBottom').style.display = 'block';
		return false;
	}
	else {
		document.getElementById('entireForm').style.display = 'none';
		document.getElementById('bounceBack').style.display = 'block';
		return true;
	}
}

/* submit_form_sar()
 * Author:      Ryan Emling
 * Purpose:     Validates and sends the Test Administrator Update form data to server
 * Parameters:  None
 * Returns:     Nothing;  Updates contact_form HTML with status message (error or OK)
 * Modified:    2/25/2009
 */
function submit_form_tau() {
	var errors = '';
	var params = '';

	var selection = '';
	for (i=0; i <= document.contact_form.radioChoices.length - 1; i++) {
		if (document.contact_form.radioChoices[i].checked) {
			selection = document.contact_form.radioChoices[i].value;
			break;
		}
	}

	if (selection == '') {
		errors += 'You must choose a radio button<br />';
	} else {
		params += 'selection='+escape(trim(selection));
	}

	var lastName = document.contact_form.lastName.value;
	if (!$defined(lastName) || lastName == '') {
		errors += 'Last Name is missing<br />';
	} else {
		params += '&last_name='+escape(trim(lastName));
	}

	var firstName = document.contact_form.firstName.value;
	if (!$defined(firstName) || firstName == '') {
		errors += 'First Name is missing<br />';
	} else {
		params += '&first_name='+escape(trim(firstName));
	}

	var workTitle = document.contact_form.workTitle.value;
	if (!$defined(workTitle) || workTitle == '') {
		errors += 'Title is missing<br />';
	} else {
		params += '&work_title='+escape(trim(workTitle));
	}

	var institutionName = document.contact_form.institutionName.value;
	if (!$defined(institutionName) || institutionName == '') {
		errors += 'Institution Name is missing<br />';
	} else {
   		params += '&institution_name='+escape(trim(institutionName));
	}

	var dsstID = document.contact_form.dsstID.value;
	if (!$defined(dsstID) || dsstID == '') {
		errors += 'DSST ID Number is missing<br />';
	} else {
   		params += '&dsst_id='+escape(trim(dsstID));
	}

	var address1 = document.contact_form.address1.value;
	if (!$defined(address1) || address1 == '') {
		errors += 'Address is missing<br />';
	} else {
		params += '&address1='+escape(trim(address1));
	}

	var address2 = document.contact_form.address2.value;
	if ($defined(address2)) {
		params +='&address2='+escape(trim(address2));
	}

	var city = document.contact_form.city.value;
	if (!$defined(city) || city == '') {
		errors += 'City is missing<br />';
	} else {
		params += '&city='+escape(trim(city));
	}

	var state = document.contact_form.state.value;
	if (!$defined(state) || state == '') {
		errors += 'State is missing<br />';
    } else {
		params += '&state='+state.toUpperCase();
	}

	var zipcode = document.contact_form.zipcode.value;
	if (!$defined(zipcode) || zipcode == '') {
		errors += 'Zipcode is missing<br />';
    } else {
		params += '&zipcode='+escape(trim(zipcode));
	}

	var phoneNumber = document.contact_form.phoneNumber.value;
	if (!$defined(phoneNumber) || phoneNumber == '') {
		errors += 'Phone Number is missing<br />';
	} else {
		params += '&phone_number='+escape(trim(phoneNumber));
	}

	var email = document.contact_form.email.value;
	if (!$defined(email) || email == '') {
		errors += 'Email is missing<br />';
	} else if (email.match(/^(((\w+\-*\w+)|\w+)+\.??)+\@(((\w+\-*\w+)|(\w+))\.)+\w+$/)) {
	    	  params += '&email='+escape(trim(email));
		   } else {
				errors += 'Email is not valid<br />';
			 }

	var recipient = document.contact_form.recipient.value;
	if ($defined(recipient)) {
		params +='&recipient='+escape(trim(recipient));
	}

	if (document.contact_form.checkBox.checked == true) {
		params += '&check_box='+'checked';
	} else {
	    errors += 'Please read our procedures and acknowledge by checking the box<br />';
	}

	var namesToDelete = document.contact_form.names_to_delete.value;
	if ($defined(namesToDelete)) {
	    params += '&names_to_delete='+escape(trim(namesToDelete));
	}

	var signature = document.contact_form.signature.value;
	if (!$defined(signature) || signature == '') {
		errors += 'Signature is missing<br />';
    } else {
		params += '&signature='+escape(trim(signature));
	}

	var date = document.contact_form.date.value;
	var validLength = document.contact_form.date.value.length;
	var dateFlag = validDate(date);
	if (!dateFlag  || validLength != 10) {
		errors += 'Date is not a valid date<br />';
	} else {
		params += '&date='+ date;
	}

	if (errors != '') {
		// display errors at the top of the form
		errors = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid. Please correct the following errors then click 'Submit' again:</p><p>" + errors + "</p>";
		$('errordiv').innerHTML = errors;
		$('errordiv').style.display = 'block';
		// display error message at the bottom of the form
		$('errordivBottom').innerHTML = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid.&nbsp;&nbsp;&nbsp;&nbsp;See list above."
		$('errordivBottom').style.display = 'block';
		scrollTo(0,0);
		return;
	}

	params += '&form_id=tau';
	// send form data to server
	var formSubmit = new Request({
	 	url: '/cgi-bin/submit_request_form.pl',
		method: 'post',
		onSuccess: function(responseText, responseXML) {
			document.contact_form.innerHTML = responseText;
		},
		onFailure: function(xhr) {
			document.contact_form.innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
		}
	});

	formSubmit.send(params);

}

/* submit_form_collateral()
 * Author:      Ryan Emling
 * Purpose:
 * Parameters:  None
 * Returns:     Nothing;  Updates contact_form HTML with status message (error or OK)
 * Modified:    4/27/2009
 */

function submit_form_collateral() {

    var errors = '';
	var params = '';

	var lastName = document.contact_form.lastName.value;
	if (!$defined(lastName) || lastName == '') {
		errors += 'Last Name is missing<br />';
	} else {
		params += '&last_name='+escape(trim(lastName));
	}

	var firstName = document.contact_form.firstName.value;
	if (!$defined(firstName) || firstName == '') {
		errors += 'First Name is missing<br />';
	} else {
		params += '&first_name='+escape(trim(firstName));
	}

	var workTitle = document.contact_form.workTitle.value;
	if (!$defined(workTitle) || workTitle == '') {
		errors += 'Title is missing<br />';
	} else {
		params += '&work_title='+escape(trim(workTitle));
	}

	var institutionName = document.contact_form.institutionName.value;
	if (!$defined(institutionName) || institutionName == '') {
		errors += 'Institution Name is missing<br />';
	} else {
   		params += '&institution_name='+escape(trim(institutionName));
	}

	var dsstID = document.contact_form.dsstID.value;
	if (!$defined(dsstID) || dsstID == '') {
		errors += 'DSST ID Number is missing<br />';
	} else {
   		params += '&dsst_id='+escape(trim(dsstID));
	}

	var address1 = document.contact_form.address1.value;
	if (!$defined(address1) || address1 == '') {
		errors += 'Address is missing<br />';
	} else {
		params += '&address1='+escape(trim(address1));
	}

	var address2 = document.contact_form.address2.value;
	if ($defined(address2)) {
		params +='&address2='+escape(trim(address2));
	}

	var city = document.contact_form.city.value;
	if (!$defined(city) || city == '') {
		errors += 'City is missing<br />';
	} else {
		params += '&city='+escape(trim(city));
	}

	var state = document.contact_form.state.value;
	if (!$defined(state) || state == '') {
		errors += 'State is missing<br />';
    } else {
		params += '&state='+state.toUpperCase();
	}

	var zipcode = document.contact_form.zipcode.value;
	if (!$defined(zipcode) || zipcode == '') {
		errors += 'Zipcode is missing<br />';
    } else {
		params += '&zipcode='+escape(trim(zipcode));
	}

	var phoneNumber = document.contact_form.phoneNumber.value;
	if (!$defined(phoneNumber) || phoneNumber == '') {
		errors += 'Phone Number is missing<br />';
	} else {
		params += '&phone_number='+escape(trim(phoneNumber));
	}

	var email = document.contact_form.email.value;
	if (!$defined(email) || email == '') {
		errors += 'Email is missing<br />';
	} else if (email.match(/^(((\w+\-*\w+)|\w+)+\.??)+\@(((\w+\-*\w+)|(\w+))\.)+\w+$/)) {
	    	  params += '&email='+escape(trim(email));
		   } else {
				errors += 'Email is not valid<br />';
			 }

    var formOrderFlag = 0;

    var studentBrochure = document.contact_form.student_brochure.value;
	if ($defined(studentBrochure) && (studentBrochure != 0)) {
		formOrderFlag = 1;
		params +='&student_brochure='+studentBrochure;
	}
	var brochureHolder = document.contact_form.brochure_holder.value;
	if ($defined(brochureHolder) && (brochureHolder != 0)) {
		formOrderFlag = 1;
		params +='&brochure_holder='+brochureHolder;
	}
	var examList = document.contact_form.exam_list.value;
	if ($defined(examList) && (examList != 0)) {
		formOrderFlag = 1;
		params +='&exam_list='+examList;
	}
	var nonMilitaryPoster = document.contact_form.non_military_poster.value;
	if ($defined(nonMilitaryPoster) && (nonMilitaryPoster != 0)) {
		formOrderFlag = 1;
		params +='&non_military_poster='+nonMilitaryPoster;
	}
	var examScoring = document.contact_form.exam_scoring.value;
	if ($defined(examScoring) && (examScoring != 0)) {
		formOrderFlag = 1;
		params +='&exam_scoring='+examScoring;
	}
	var militaryBrochure = document.contact_form.military_brochure.value;
	if ($defined(militaryBrochure) && (militaryBrochure != 0)) {
		formOrderFlag = 1;
		params +='&military_brochure='+militaryBrochure;
	}
    var fundingGuide = document.contact_form.funding_guide.value;
	if ($defined(fundingGuide) && (fundingGuide != 0)) {
		formOrderFlag = 1;
		params +='&funding_guide='+fundingGuide;
	}
	var militaryPoster = document.contact_form.military_poster.value;
	if ($defined(militaryPoster) && (militaryPoster != 0)) {
		formOrderFlag = 1;
		params +='&military_poster='+militaryPoster;
	}

	if (formOrderFlag == 0){
	    errors += 'You did not select any materials to be mailed to you.<br />';
	}

	if (errors != '') {
		// display errors at the top of the form
		errors = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid. Please correct the following errors then click 'Submit' again:</p><p>" + errors + "</p>";
		$('errordiv').innerHTML = errors;
		$('errordiv').style.display = 'block';
		// display error message at the bottom of the form
		$('errordivBottom').innerHTML = "<p style=\"color: white; background-color: #CC3333; padding: 3px;\">Some of the required information is missing or invalid.&nbsp;&nbsp;&nbsp;&nbsp;See list above."
		$('errordivBottom').style.display = 'block';
		return;
	}

    params += '&form_id=collateral';
	// send form data to server
	var formSubmit = new Request({
	 	url: '/cgi-bin/submit_request_form.pl',
		method: 'post',
		onSuccess: function(responseText, responseXML) {
			document.contact_form.innerHTML = responseText;
		},
		onFailure: function(xhr) {
	    	document.contact_form.innerHTML = '<p style="color:red;">'+xhr.status+'</p>';
		}
	});

	formSubmit.send(params);
}

/* toggle_contact_email
 * Author:      David Coleman
 * Purpose:     Toggles displayed email address based on user's self identification
 *              when viewing default contact form
 * Parameters:
 * Returns:
 */
function toggle_contact_email() {
    if ($('student_user').checked) {
        $('contact_email').href = "mailto:getcollegecredit@prometric.com";
        $('contact_email').innerHTML = "getcollegecredit@prometric.com";
    } else {
        $('contact_email').href = "mailto:dsst@prometric.com";
        $('contact_email').innerHTML = "dsst@prometric.com";
    }
}

