function getQuestionSpecificError(tdId) {
	var errorMsg = "";
	tdId = tdId.replace(/answertd/, "");
	if (tdId == "legallyworkus") {
		errorMsg = "We require that all candidates be able to legally work in the United States.";
	}
	else if (tdId == "validssn") {
		errorMsg = "A valid Social Security number is required to apply for a position at LifeWorx.";
	}
	else if (tdId == "yearsdrive") {
		errorMsg = "More than a year of driving experience is generally necessary, as candidates need to be comfortable driving to/from jobs, and potentially driving clients.";
	}
	else if (tdId == "driveviolations") {
		errorMsg = "Candidates with 2+ driving violations are unlikely to be hired, as candidates will need to drive to/from jobs, and only candidates with a clean record may drive clients.";
	}
	else if (tdId == "reliablecar") {
		errorMsg = "Candidates must own a reliable car in order to drive to/from jobs and to potentially drive clients.";
	}
	else if (tdId == "speakenglish") {
		errorMsg = "Communication is very important at LifeWorx, and inadequate English proficiency could led to difficulty understanding assignments or communicating with clients.";
	}
	else if (tdId == "weekhours") {
		errorMsg = "Candidates must be available for at least 15 hours a week.";
	}
	else if (tdId == "credithistory") {
		errorMsg = "Candidates must have a minimum credit history to work for LifeWorx, since they work in clients' homes.";
	}
	else if (tdId == "cellphone") {
		errorMsg = "Candidates must have cell phones as they must be reachable.";
	}
	else if (tdId == "work") {
		errorMsg = "Candidates must have at least 2 years of work experience in relevant field(s).";
	}
	return errorMsg;
}

function processQuestionYnResponse (tdId, sel) {
	if (sel == "yes") {
		removeQuestionSpecificError(tdId);
	}
	if (sel == "no") {
		setQuestionSpecificError(tdId, 1);
	}
}

function processQuestionNumLessEqualResponse(tdId, msgDisplaysIf) {
	entered = document.getElementById(tdId).getElementsByTagName('input')[0].value;
	if (entered != "" && entered <= msgDisplaysIf) {
		setQuestionSpecificError(tdId, 1);
	}
	else {
		removeQuestionSpecificError(tdId);
	}
	
	if (tdId == "yearsdriveanswertd") {
		if (entered == 1) {
			document.getElementById("yearsdrive_afterlabel").firstChild.nodeValue = " year";
		}
		else {
			document.getElementById("yearsdrive_afterlabel").firstChild.nodeValue = " years";
		}
	}
	if (tdId == "weekhoursanswertd") {
		if (entered == 1) {
			document.getElementById("weekhours_afterlabel").firstChild.nodeValue = " hour";
		}
		else {
			document.getElementById("weekhours_afterlabel").firstChild.nodeValue = " hours";
		}
	}
	if (tdId == "workanswertd") {
		if (entered == 1) {
			document.getElementById("work_afterlabel").firstChild.nodeValue = " year";
		}
		else {
			document.getElementById("work_afterlabel").firstChild.nodeValue = " years";
		}
	}
}

function setQuestionSpecificError (tdId, inP) {
	removeQuestionSpecificError(tdId);
	tdEl = document.getElementById(tdId);
	errorMsg = getQuestionSpecificError(tdId);
	spanEl = document.createElement('span');
	spanEl.appendChild(document.createTextNode(errorMsg));
	spanEl.className="errormsg";
	if (inP == 1) {
		pEl = document.createElement('p');
		pEl.id=tdId+"err";
		pEl.className = 'questionerror';
		pEl.appendChild(spanEl);
		tdEl.appendChild(pEl);
	}
	else {
		spanEl.id=tdId+"err";
		tdEl.appendChild(spanEl);
	}
}

function removeQuestionSpecificError (tdId) {
	tdErrorEl = document.getElementById(tdId+"err");
	if (tdErrorEl) {
		tdErrorEl.parentNode.removeChild(tdErrorEl);
	}
}

window.onload = function () {
	//show messages for yn fields
	var ynQuestNoButtons = getElementsByClassName(document, "input", "ynquestnobutton");
	var amtOfNoButtons = ynQuestNoButtons.length;
	for (var c = 0; c < amtOfNoButtons; c++) {
		if (ynQuestNoButtons[c].checked == true) {
			idOfTd = ynQuestNoButtons[c].id.replace(/_no/, 'answertd');
			processQuestionYnResponse(idOfTd, "no");
		}
	}
	
	//special cases
	var yearsDriveNum = document.getElementById("yearsdrive").value;
	document.getElementById("yearsdrive").onkeydown = onlyNumsForInput;
	if (yearsDriveNum != "" && yearsDriveNum <= 1) {
		setQuestionSpecificError("yearsdriveanswertd", 1);
	}
	
	var driveViolations2pOption = document.getElementById("driveviolations2p");
	if (driveViolations2pOption.selected == true) {
		setQuestionSpecificError("driveviolationsanswertd", 1);
	}
	
	var speakEnglishLittleOption = document.getElementById("speakenglishlittle");
	if (speakEnglishLittleOption.selected == true) {
		setQuestionSpecificError("speakenglishanswertd", 1);
	}
	
	var weekHoursNum = document.getElementById("weekhours").value;
	document.getElementById("weekhours").onkeydown = onlyNumsForInput;
	if (weekHoursNum != "" && weekHoursNum <= 14) {
		setQuestionSpecificError("weekhoursanswertd", 1);
	}
	
	var creditHistoryPoorOption = document.getElementById("credithistorypoor");
	if (creditHistoryPoorOption.selected == true) {
		setQuestionSpecificError("credithistoryanswertd", 1);
	}
	
	var workYearsNum = document.getElementById("work").value;
	document.getElementById("work").onkeydown = onlyNumsForInput;
	if (workYearsNum != "" && workYearsNum <= 1) {
		setQuestionSpecificError("workanswertd", 1);
	}
}