// JavaScript Document for TRC Questions Pages

var focused=false;
if (document.captureEvents) document.captureEvents(Event.KEYPRESS);
document.onkeypress=function(e) {
if (!focused) return true;
if (!e && event) e={which:event.keyCode};
if (e.which < 48 || e.which >57) return false;
}

// string prototypes for trim funciton
String.prototype.lTrim = function () {
    var whitespace = new RegExp("^\\s+","gm");
    return this.replace(whitespace, "");
}

String.prototype.rTrim = function () {
    var whitespace = new RegExp("\\s+$","gm");
    return this.replace(whitespace, "");
}

String.prototype.trim = function () {
    return this.rTrim().lTrim();
}

function check_fields()
{
	
	//verify if all the required fields were selected
	//create variables for verification of selection and assign default value
	var blQuestionAnswered = "N";
	//check for question about access to PC at home
	for (var i=0; i< document.trc_athome_frm.pc_home.length; i++)
	{
		if(document.trc_athome_frm.pc_home[i].checked)
		{
			blQuestionAnswered = "Y";
			break;
		}
	}//end for loop 
	
	if (blQuestionAnswered == "N")
	{
		//alert("Please answer question #1 before submitting the form. \n Note that all the questions marked with a * are required and must be answered before continuing with the application process.")	;
		displayAlert("Please answer question #1 before submitting the form.");
		return false;
	}
	
	//reset variable 
	blQuestionAnswered = "N";
	
	//check for question about PC type only if the question one answer was 'Y' 
	if(document.trc_athome_frm.pc_home[0].checked)
	{
		for (var i=0; i< document.trc_athome_frm.pc_type.length; i++)
		{
			if(document.trc_athome_frm.pc_type[i].checked)
			{
				blQuestionAnswered = "Y";
				break;
			}
		}//end for loop 
	
		if (blQuestionAnswered == "N")
		{
			displayAlert("Please answer question #2 before submitting the form.");
			return false;
		}
		
	}//end if
	
	//reset variable 
	blQuestionAnswered = "N";
	//check for question about PC Windows version only if the question #2 has 'windows' as the answer
	if(document.trc_athome_frm.pc_type[i].value == 'windows')
	{
		
		for (var i=0; i< document.trc_athome_frm.pc_version.length; i++)
		{
			if(document.trc_athome_frm.pc_version[i].checked)
			{
				blQuestionAnswered = "Y";
				break;
			}
		}//end for loop 
	
		if (blQuestionAnswered == "N")
		{
			alert("Please answer question #3 about Windows version before submitting the form.");
			return false;
		}
		else
		{
			var strOtherSpecify = document.trc_athome_frm.pc_version_specify.value;
			strOtherSpecify = strOtherSpecify.trim();
			if(	(document.trc_athome_frm.pc_version[4].checked) && (strOtherSpecify.length == 0) )// if the user selected 'other' ask to specify 
			{
				alert("Please, specify which other version of Windows you have.");
				document.trc_athome_frm.pc_version_specify.focus();
				return false;
			}
		}
		
	}//end if for pc type
	
	//reset variable 
	blQuestionAnswered = "N";
	
	//check for question about Internet access
	for (var i=0; i< document.trc_athome_frm.internet_home.length; i++)
	{
		if(document.trc_athome_frm.internet_home[i].checked)
		{
			blQuestionAnswered = "Y";
			break;
		}
	}//end for loop 
	
	if (blQuestionAnswered == "N")
	{
		displayAlert("Please answer question #4 before submitting the form.");
		return false;
	}
	
	//reset variable 
	blQuestionAnswered = "N";
	
	if(document.trc_athome_frm.internet_home[i].value == 'yes')
	{
		//check for question about internet access type only if the question #4 has 'yes' as the answer
		for (var i=0; i< document.trc_athome_frm.internet_type.length; i++)
		{
			if(document.trc_athome_frm.internet_type[i].checked)
			{
				blQuestionAnswered = "Y";
				break;
			}
		}//end for loop 
		
		if (blQuestionAnswered == "N")
		{
			alert("Please answer question #5 about type of Internet access before submitting the form.");
			return false;
		}
		
	}//end if for internet home
	
	//reset variable 
	blQuestionAnswered = "N";
	
	//check for question about telephone land line
	for (var i=0; i< document.trc_athome_frm.land_line.length; i++)
	{
		if(document.trc_athome_frm.land_line[i].checked)
		{
			blQuestionAnswered = "Y";
			break;
		}
	}//end for loop 
	
	if (blQuestionAnswered == "N")
	{
		displayAlert("Please answer question #6 before submitting the form.");
		return false;
	}
	
	//reset variable 
	blQuestionAnswered = "N";
	
	//check for question about age of 18 or older
	for (var i=0; i< document.trc_athome_frm.age_18.length; i++)
	{
		if(document.trc_athome_frm.age_18[i].checked)
		{
			blQuestionAnswered = "Y";
			break;
		}
	}//end for loop 
	
	if (blQuestionAnswered == "N")
	{
		displayAlert("Please answer question #7 before submitting the form.");
		return false;
	}
	
	var intZipCode = document.trc_athome_frm.zip_code.value
	intZipCode = intZipCode.trim()
	if(intZipCode.length > 0)
	{
		if(intZipCode.length != 5)
		{
			alert("Please enter 5 numbers for the zip code.")
			document.trc_athome_frm.zip_code.focus();
			document.trc_athome_frm.zip_code.select();
			return false
		}
		if(intZipCode != parseInt(intZipCode,10))
		{
			alert ("Please enter only numbers for the Zip Code.");
			document.trc_athome_frm.zip_code.focus();
			document.trc_athome_frm.zip_code.select();
			return false;
		}
	}
	else
	{
		displayAlert("Please enter your zip code before submitting the form.");
		return false;	
		
	}
	
	//reset variable 
	blQuestionAnswered = "N";
	
	//check for question about bilingual skill
	for (var i=0; i< document.trc_athome_frm.bilingual.length; i++)
	{
		if(document.trc_athome_frm.bilingual[i].checked)
		{
			blQuestionAnswered = "Y";
			break;
		}
	}//end for loop 
	
	if (blQuestionAnswered == "N")
	{
		displayAlert("Please answer question #9 before submitting the form.");
		return false;
	}

	//reset variable 
	blQuestionAnswered = "N";
	
	//check for question about how they learned about the ad
	for (var i=0; i< document.trc_athome_frm.source_type.length; i++)
	{
		if(document.trc_athome_frm.source_type[i].checked)
		{
			//check to see if the selection is web ad
			if (document.trc_athome_frm.source_type[i].value == "wa")
			{
				if (document.trc_athome_frm.web_ad[document.trc_athome_frm.web_ad.selectedIndex].value == 0)
				{
					displayAlert("Please select which Web ad you saw the job posting for this position before continuing.");
					return false;
				}
			}
			//check to see if the selection is newspaper ad
			if (document.trc_athome_frm.source_type[i].value == "np")
			{
				if (document.trc_athome_frm.newspaper_ad[document.trc_athome_frm.newspaper_ad.selectedIndex].value == 0)
				{
					displayAlert("Please select which Newspaper ad you saw the job posting for this position before continuing.");
					return false;
				}
			}
			//check to see if the selection is other
			if (document.trc_athome_frm.source_type[i].value == "ot")
			{
				var strOtherSourceType = document.trc_athome_frm.source_type_specify.value;
				strOtherSourceType = strOtherSourceType.trim();
				if(strOtherSourceType.length == 0)
				{
					displayAlert("Please specify the other source where you saw the job posting for this position before continuing.");
					return false;
				}
			}
			blQuestionAnswered = "Y";
			break;
		}
	}//end for loop 
	
	if (blQuestionAnswered == "N")
	{
		displayAlert("Please answer question #9 before submitting the form.");
		return false;
	}
	
	
	return true;
	
} //end of check_fields function

function displayAlert(strMSG)
{
	
	alert(strMSG + "\n Note that all the questions marked with a * are required and must be answered before continuing with the application process.")
}

function clearWindowsVersion()
{
	//if type of computer is a MAC, it makes sure that all Windows versions are cleared, if any was selected.
	if(document.trc_athome_frm.pc_type[1].value == 'mac')
	{
		for (var i=0; i< document.trc_athome_frm.pc_version.length; i++)
		{
			document.trc_athome_frm.pc_version[i].checked = false;
				
		}
	}
	

}//end funciton clearWindowsVersion

function setPCType()
{
	document.trc_athome_frm.pc_type[0].checked = true;
	
}// end function setPCType

function clearInternetType()
{
	//if the user does not have Internet access from home, it makes sure that all type of Internet access answered are cleared, if any was previously selected.	
	if(document.trc_athome_frm.internet_home[1].value == 'N')
	{
		for (var i=0; i< document.trc_athome_frm.internet_type.length; i++)
		{
			document.trc_athome_frm.internet_type[i].checked = false;
			
		}//end for loop 
	}
} // end function clearInternetType

function setInternetAccess()
{
	//set Internet Access to Yes if the use selects on any type of Internet Access
	document.trc_athome_frm.internet_home[0].checked = true;
}

function setSourceType(strType)
{
	if(strType == "wa")
	{
		document.trc_athome_frm.source_type[1].checked = true;
	}
	if(strType == "np")
	{
		document.trc_athome_frm.source_type[0].checked = true;
	}
	
	return true;
}

//enable the newspaper ad list and disable the web ad list, if enabled
function enableNewspaperList()
{
	document.trc_athome_frm.newspaper_ad.disabled = false;
	document.trc_athome_frm.newspaper_ad.options[0].selected;
	document.trc_athome_frm.web_ad.selectedIndex = 0;
	document.trc_athome_frm.web_ad.disabled = true;
	disable_other_source_type();
	
}

// enable the web ad list and disable the newspaper ad list, if enabled
function enableWebList()
{
	document.trc_athome_frm.web_ad.disabled = false;
	document.trc_athome_frm.web_ad.options[0].selected;
	document.trc_athome_frm.newspaper_ad.disabled = true;
	document.trc_athome_frm.newspaper_ad.selectedIndex = 0;
	disable_other_source_type();
	
}

// function to disable the web and newspaper ad lists when clicking on the other options for the source type
function disableAdLists()
{
	document.trc_athome_frm.newspaper_ad.disabled = true;
	document.trc_athome_frm.web_ad.disabled = true;
	document.trc_athome_frm.web_ad.selectedIndex = 0;
	document.trc_athome_frm.newspaper_ad.selectedIndex = 0;
	//disable the other specify field if other is not selected
	if(document.trc_athome_frm.source_type[4].checked == false)
	{
		disable_other_source_type();
	}
}

function enable_other_source_type()
{
	document.trc_athome_frm.source_type_specify.disabled = false;	
	document.trc_athome_frm.source_type_specify.focus();
}

function disable_other_source_type()
{
	document.trc_athome_frm.source_type_specify.value= "";
	document.trc_athome_frm.source_type_specify.disabled = true;
	
}