//////////////////////////////////////////////////////////////////////////////
//
//		This file does the error checking for our form.  The error checks aren't
// 	too in depth simply because we aren't "storing" any of this data in a
//	database.  We simply want to make sure that the data is presentable i.e.
//  not have a1/4r/w343 for a date.
//
//	Coded by Jeff Speed
//	
//	Some functions (validations) were pulled from various websites with the 
//	belief that they are in the public domain.
//
//////////////////////////////////////////////////////////////////////////////

// These are the global variables to handle our error message
var errored_fields = new Array();
var error_msg = "";
var error_ind = false;
var error_count = 0;


//////////////////////////////////////////////////////////////////////////////
//
//	This is the main function of the javascript file.  It calls all of the 
//  support functions in proper order.  It then will call the function to
//  print any error messages if necessary otherwise it returns a value of 
//  true.  A value of true means that all the data in the form is as we 
//  expected.
//
//////////////////////////////////////////////////////////////////////////////
function validate_content(){
	
	Errored_fields = getElementbyClass(' error');
	if (errored_fields.length > 0){remove_error_class(' error');}
	
	validate_personal();
	validate_race();			
	validate_emergency(); 
	validate_degree();	
	validate_highschool(); 
	validate_college();	 
	validate_act();		
	validate_felony();
	validate_medical();	
	if (error_ind == false) {validate_stuver();}
	
	if (error_ind) { print_error('errormsg',error_msg); return false; }
	else return true;
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Personal 
//  Details section of the form.
//
//////////////////////////////////////////////////////////////////////////////
function validate_personal(){
	
	var first_val = '', last_val = '', ssn_val = '', dob_val = '', dayphone_val = '';
	var nightphone_val = '', email_val = '', addr_val = '', city_val = '', state_val = '';
	var zip_val = '', county_val = '', citizen_val = '';
	
	// get the values of the fields
	first_val = document.getElementById('sFirstname').value;
	last_val = document.getElementById('sLastname').value;
	ssn_val = document.getElementById('nSSN1').value + "-" + document.getElementById('nSSN2').value + "-" +document.getElementById('nSSN3').value;
	dayphone_val = document.getElementById('sDayphone1').value + "-" + document.getElementById('sDayphone2').value + "-" + document.getElementById('sDayphone3').value;
	nightphone_val = document.getElementById('sNightphone1').value + "-" + document.getElementById('sNightphone2').value + "-" + document.getElementById('sNightphone3').value;
	email_val = document.getElementById('sEmail').value;
	addr_val = document.getElementById('sAddress1').value;
	city_val = document.getElementById('sCity').value;
	state_val = document.getElementById('sState').value;
	zip_val = document.getElementById('sPostcode').value;
	county_val = document.getElementById('sCounty').value;
	for (i=0;i<document.forms['onlineapp'].nDobmonth.length;i++)
	{
		if (document.forms['onlineapp'].nDobmonth[i].selected) {	dob_val = document.forms['onlineapp'].nDobmonth[i].value; }
	}
	dob_val += "/" + document.getElementById('nDobday').value + "/" + document.getElementById('nDobyear').value;
	for (i=0;i<document.forms['onlineapp'].bCitizen.length;i++)
	{
		if (document.forms['onlineapp'].bCitizen[i].checked) {	citizen_val = document.forms['onlineapp'].bCitizen[i].value; }
	}
	
	// error check the fields
		if (first_val.length == 0){
			errored_fields[error_count] = 'sFirstnamediv';
			error_count++;
			error_msg += "Please tell us your first name.\n<br>\n";
			error_ind = true;
		}
		if (last_val.length == 0){
			errored_fields[error_count] = 'sLastnamediv';
			error_count++;
			error_msg += "Please tell us your last name.\n<br>\n";
			error_ind = true;
		}
		var tempssnmsg = SSNValidation(ssn_val);
		if (tempssnmsg != ""){
				errored_fields[error_count] = 'nSSNdiv';
				error_count++;
				error_msg += "Personal Details. " + tempssnmsg + "\n<br>\n";
				error_ind = true;
			}
		var tempdobmsg = isValidDate(dob_val);
		if (tempdobmsg != ""){
				errored_fields[error_count] = 'nDobdiv';
				error_count++;
				error_msg += "Personal Details. " + tempdobmsg + "\n<br>\n";
				error_ind = true;
			}
		if (validate_phone(dayphone_val) == null){
			errored_fields[error_count] = 'sDayphonediv';
			error_count++;
			error_msg += "Invalid phone number. Please enter the phone number where you can be reached during the day.\n<br>\n";
			error_ind = true;
		}	
		if (validate_phone(nightphone_val) == null){
			errored_fields[error_count] = 'sNightphonediv';
			error_count++;
			error_msg += "Invalid phone number. Please enter the phone number where you can be reached during the evening.\n<br>\n";
			error_ind = true;
		}	
		if (validate_email(email_val) == null){
			errored_fields[error_count] = 'sEmaildiv';
			error_count++;
			error_msg += "Invalid email address. Please enter the email address where we can reach you.\n<br>\n";
			error_ind = true;
		}
		if (addr_val.length == 0){
			errored_fields[error_count] = 'sAddress1div';
			error_count++;
			error_msg += "Please tell us your address.\n<br>\n";
			error_ind = true;
		}
		if (city_val.length == 0){
			errored_fields[error_count] = 'sCitydiv';
			error_count++;
			error_msg += "Please tell us the city where you live.\n<br>\n";
			error_ind = true;
		}
		if (validate_state(state_val) == null){
			errored_fields[error_count] = 'sStatediv';
			error_count++;
			error_msg += "Invalid State. Please enter the state abbreviation for the state where you live.\n<br>\n";
			error_ind = true;
		}			
	  var tempzipmsg = validateZIP(zip_val);
		if (tempzipmsg != ""){
				errored_fields[error_count] = 'sPostcodediv';
				error_count++;
				error_msg += "Personal Details. " + tempzipmsg + "\n<br>\n";
				error_ind = true;
			}
		if (county_val.length == 0){
			errored_fields[error_count] = 'sCountydiv';
			error_count++;
			error_msg += "Please tell us the county where you live.\n<br>\n";
			error_ind = true;
		}
		if (citizen_val == ''){
			errored_fields[error_count] = 'bCitizendiv';
			error_count++;
			error_msg += "Please tell us if you are a citizen of the United States.\n<br>\n";
			error_ind = true;
	}
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Race
//  section of the form even though it isn't required information.
//
//////////////////////////////////////////////////////////////////////////////
function validate_race(){
	
	var race_val = '';
	
	// get the form value
	for (i=0;i<document.forms['onlineapp'].bRace.length;i++)
	{
		if (document.forms['onlineapp'].bRace[i].checked) {	race_val = document.forms['onlineapp'].bRace[i].value; }
	}
	
	// error check
	if (race_val == 'O'){
			if (document.getElementById('sRaceOther').value.length == 0){
			errored_fields[error_count] = 'sRaceOtherdiv';
			error_count++;
			error_msg += "You specified you were of Other race.  Please tell us that race.\n<br>\n";
			error_ind = true;
		}
	}	
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Emergency
//  Contact section of the form.
//
//////////////////////////////////////////////////////////////////////////////
function validate_emergency(){
	
	var emerfirst_val = '', emerlast_val = '', emeraddr1_val = '', emercity_val = '';
	var emerst_val = '', emerzip_val = '', emerphone_val = '', emerrel_val = '';
	
	// get form values
	emerfirst_val = document.getElementById('sEmerFirst').value;
	emerlast_val = document.getElementById('sEmerLast').value;
	emeraddr1_val = document.getElementById('sEmerAddress1').value;
	emercity_val = document.getElementById('sEmerCity').value;
	emerst_val = document.getElementById('sEmerState').value;
	emerzip_val = document.getElementById('sEmerZip').value;
	emerphone_val = document.getElementById('sEmerphone1').value + "-" + document.getElementById('sEmerphone2').value + "-" + document.getElementById('sEmerphone3').value;
	
	for (i=0;i<document.forms['onlineapp'].bRelation.length;i++)
	{
		if (document.forms['onlineapp'].bRelation[i].checked) {	emerrel_val = document.forms['onlineapp'].bRelation[i].value; }
	}
	
	// error check
	if (emerfirst_val.length == 0){
			errored_fields[error_count] = 'sEmerFirstdiv';
			error_count++;
			error_msg += "Please tell us the first name of your emergency contact.\n<br>\n";
			error_ind = true;
		}
	if (emerlast_val.length == 0){
			errored_fields[error_count] = 'sEmerLastdiv';
			error_count++;
			error_msg += "Please tell us the last name of your emergency contact.\n<br>\n";
			error_ind = true;
		}
	if (emeraddr1_val.length == 0){
			errored_fields[error_count] = 'sEmerAddress1div';
			error_count++;
			error_msg += "Please tell us the address of your emergency contact.\n<br>\n";
			error_ind = true;
		}
	if (emercity_val.length == 0){
			errored_fields[error_count] = 'sEmerCitydiv';
			error_count++;
			error_msg += "Please tell us the city of your emergency contact.\n<br>\n";
			error_ind = true;
		}
	if (validate_state(emerst_val) == null){
			errored_fields[error_count] = 'sEmerStatediv';
			error_count++;
			error_msg += "Invalid State. Please enter the state abbreviation for your emergency contact's state.\n<br>\n";
			error_ind = true;
		}	
  var tempzipmsg = validateZIP(emerzip_val);
	if (tempzipmsg != ""){
			errored_fields[error_count] = 'sEmerZipdiv';
			error_count++;
			error_msg += "Emergency Contact Zip. " + tempzipmsg + "\n<br>\n";
			error_ind = true;
		}
	if (validate_phone(emerphone_val) == null){
			errored_fields[error_count] = 'sEmerphonediv';
			error_count++;
			error_msg += "Invalid phone number. Please enter the phone number where your emergency contact can be reached.\n<br>\n";
			error_ind = true;
		}	
	switch(emerrel_val){
		case '' :
				errored_fields[error_count] = 'bRelationdiv';
				error_count++;
				error_msg += "Please tell us the relationship with your emergency contact.\n<br>\n";
				error_ind = true;
				break;
		case 'O' :
				if (document.getElementById('sEmerOther').value.length == 0){
					errored_fields[error_count] = 'sEmerOtherdiv';
					error_count++;
					error_msg += "You specified you an Other relationship with your emergency contact.  Please tell us that relationship.\n<br>\n";
					error_ind = true;
					}
				break;	
	}
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Degree
//	section.
//
//////////////////////////////////////////////////////////////////////////////
function validate_degree(){
	
	var degree_val = '', major_val = '', campus_val = '', term_val = '';
	
	// get values
	major_val = document.getElementById('sMajor').value;
	for (i=0;i<document.forms['onlineapp'].bdegree.length;i++)
	{
		if (document.forms['onlineapp'].bdegree[i].checked) {	degree_val = document.forms['onlineapp'].bdegree[i].value; }
	}
	for (i=0;i<document.forms['onlineapp'].bCampus.length;i++)
	{
		if (document.forms['onlineapp'].bCampus[i].checked) {	campus_val = document.forms['onlineapp'].bCampus[i].value; }
	}
	for (i=0;i<document.forms['onlineapp'].bTerm.length;i++)
	{
		if (document.forms['onlineapp'].bTerm[i].checked) {	term_val = document.forms['onlineapp'].bTerm[i].value; }
	}
	
	// error check
	if (major_val.length == 0){
			errored_fields[error_count] = 'sMajordiv';
			error_count++;
			error_msg += "Please tell us what major you intend to pursue.\n<br>\n";
			error_ind = true;
		}
	if (degree_val == ''){
			errored_fields[error_count] = 'bdegreediv';
			error_count++;
			error_msg += "Please tell us what type of degree you intend to pursue.\n<br>\n";
			error_ind = true;
	}
	switch(campus_val){
		case '' :
				errored_fields[error_count] = 'bCampusdiv';
				error_count++;
				error_msg += "Please tell us what campus you plan on attending.\n<br>\n";
				error_ind = true;
				break;
		case 'O' :
				if (document.getElementById('sCampusOther').value.length == 0){
					errored_fields[error_count] = 'sCampusOtherdiv';
					error_count++;
					error_msg += "You specified you were attending an Other campus.  Please tell us that campus.\n<br>\n";
					error_ind = true;
					}
				break;	
	}
	if (term_val == ''){
			errored_fields[error_count] = 'bTermdiv';
			error_count++;
			error_msg += "Please tell us what term you plan on starting.\n<br>\n";
			error_ind = true;
	}
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the High School
//	section.
//
//////////////////////////////////////////////////////////////////////////////
function validate_highschool(){
	
	var highschool_val = '', hsaddr_val = '', grad_val = '', graddate_val = '', gedst_val = '';
	var prevattend_val = '', prevdate_val = '', prevname_val = '', earned_ged_val = '', ged_date_val = '';
	var act_date_reg = /(0?[1-9]|1[012])[- /.](19|20)\d\d/;
	
	// get values
	highschool_val = document.getElementById('sHighSchool').value;
	hsaddr_val = document.getElementById('sHSAddress1').value;
	graddate_val = document.getElementById('sHSGradDate').value;
	gedst_val = document.getElementById('sGEDState').value;
	ged_date_val = document.getElementById('sGEDDate').value;
	prevdate_val = document.getElementById('sPrevAttendDate').value;
	prevname_val = document.getElementById('sPrevAttendName').value;
	for (i=0;i<document.forms['onlineapp'].bGraduate.length;i++)
	{
		if (document.forms['onlineapp'].bGraduate[i].checked) {	grad_val = document.forms['onlineapp'].bGraduate[i].value; }
	}
	for (i=0;i<document.forms['onlineapp'].bPrevAttend.length;i++)
	{
		if (document.forms['onlineapp'].bPrevAttend[i].checked) {	prevattend_val = document.forms['onlineapp'].bPrevAttend[i].value; }
	}
	for (i=0;i<document.forms['onlineapp'].bEarnGED.length;i++)
	{
		if (document.forms['onlineapp'].bEarnGED[i].checked) {	earned_ged_val = document.forms['onlineapp'].bEarnGED[i].value; }
	}
	
	// error check
	if (highschool_val.length == 0){
			errored_fields[error_count] = 'sHighSchooldiv';
			error_count++;
			error_msg += "Please tell us what high school you attended.\n<br>\n";
			error_ind = true;
		}
	if (hsaddr_val.length == 0){
			errored_fields[error_count] = 'sHSAddress1div';
			error_count++;
			error_msg += "Please tell us the state of the high school you attended.\n<br>\n";
			error_ind = true;
		}
	switch(grad_val){
		case 'Y' :
				if (graddate_val.match(act_date_reg) == null){
						errored_fields[error_count] = 'sHSGradDatediv';
						error_count++;
						error_msg += "Please tell us when you graduated from high school.\n<br>\n";
						error_ind = true;
				}
				break;
		case 'N' :
					
				break;
		default :
			errored_fields[error_count] = 'bGraduatediv';
			error_count++;
			error_msg += "Please tell us whether or not you graduated from high school.\n<br>\n";
			error_ind = true;	
	}
	switch(earned_ged_val){
		case 'Y' :
				if (validate_state(gedst_val) == null){
						errored_fields[error_count] = 'sGEDStatediv';
						error_count++;
						error_msg += "Invalid State. Please enter the state abbreviation for where you got your GED.\n<br>\n";
						error_ind = true;
					}
				if (ged_date_val.match(act_date_reg) == null){
						errored_fields[error_count] = 'sGEDDatediv';
						error_count++;
						error_msg += "Invalid Date. Please enter the date you earned your GED in a mm/yyyy format.\n<br>\n";
						error_ind = true;
				}
				break;
		case 'N' :
					
				break;
		default :
			errored_fields[error_count] = 'bEarnGEDdiv';
			error_count++;
			error_msg += "Please tell us whether or not you earned a GED.\n<br>\n";
			error_ind = true;	
	}
	switch(prevattend_val){
		case 'Y' :
				// check for prev date
				if (prevdate_val.length == 0){
						errored_fields[error_count] = 'sPrevAttendDatediv';
						error_count++;
						error_msg += "Please tell us when you previously attended PRCC.\n<br>\n";
						error_ind = true;
					}
				// check for prev name
				if (prevname_val.length == 0){
						errored_fields[error_count] = 'sPrevAttendNamediv';
						error_count++;
						error_msg += "Please tell us the name you used when you previously attended PRCC.\n<br>\n";
						error_ind = true;
					}
				break;
		case 'N' :
				// nothing to check
				break;
		default :
			errored_fields[error_count] = 'bPrevAttenddiv';
			error_count++;
			error_msg += "Please tell us whether or not you previously attended PRCC.\n<br>\n";
			error_ind = true;	
	}
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the College
//	and Universities section.
//
//////////////////////////////////////////////////////////////////////////////
function validate_college(){
	
	var inst1_val = '', attend1_val = '', state1_val = '', city1_val = '', credit1_val = '', degree1_val = '';
	var inst2_val = '', attend2_val = '', state2_val = '', city2_val = '', credit2_val = '', degree2_val = '';
	var inst3_val = '', attend3_val = '', state3_val = '', city3_val = '', credit3_val = '', degree3_val = '';
	
	//get values
	inst1_val = document.getElementById('sInstitution1').value;
	attend1_val = document.getElementById('sAttendance1').value;
	city1_val = document.getElementById('sInstCity1').value;
	state1_val = document.getElementById('sInstState1').value;
	credit1_val = document.getElementById('sCredits1').value;
	degree1_val = document.getElementById('sDegree1').value;
	
	inst2_val = document.getElementById('sInstitution2').value;
	attend2_val = document.getElementById('sAttendance2').value;
	city2_val = document.getElementById('sInstCity2').value;
	state2_val = document.getElementById('sInstState2').value;
	credit2_val = document.getElementById('sCredits2').value;
	degree2_val = document.getElementById('sDegree2').value;
	
	inst3_val = document.getElementById('sInstitution3').value;
	attend3_val = document.getElementById('sAttendance3').value;
	city3_val = document.getElementById('sInstCity3').value;
	state3_val = document.getElementById('sInstState3').value;
	credit3_val = document.getElementById('sCredits3').value;
	degree3_val = document.getElementById('sDegree3').value;
	
	// error check
	if (inst1_val.length != 0){
			if (attend1_val.length == 0){
						errored_fields[error_count] = 'sAttendance1div';
						error_count++;
						error_msg += "Please tell us when you attended " + inst1_val + ".\n<br>\n";
						error_ind = true;
					}	
			if (city1_val.length == 0){
						errored_fields[error_count] = 'sInstCity1div';
						error_count++;
						error_msg += "Please tell us what city " + inst1_val + " is located.\n<br>\n";
						error_ind = true;
					}
			if (state1_val.length == 0){
						errored_fields[error_count] = 'sInstState1div';
						error_count++;
						error_msg += "Please tell us what state " + inst1_val + " is located.\n<br>\n";
						error_ind = true;
					}
			if (credit1_val.length == 0){
						errored_fields[error_count] = 'sCredits1div';
						error_count++;
						error_msg += "Please tell us how many credits you earned while attending " + inst1_val + ".\n<br>\n";
						error_ind = true;
					}
			if (degree1_val.length == 0){
						errored_fields[error_count] = 'sDegree1div';
						error_count++;
						error_msg += "Please tell us what degree you earned while attending " + inst1_val + ".\n<br>\n";
						error_ind = true;
					}																	
	}

	if (inst2_val.length != 0){
			if (attend2_val.length == 0){
						errored_fields[error_count] = 'sAttendance2div';
						error_count++;
						error_msg += "Please tell us when you attended " + inst2_val + ".\n<br>\n";
						error_ind = true;
					}	
			if (city2_val.length == 0){
						errored_fields[error_count] = 'sInstCity2div';
						error_count++;
						error_msg += "Please tell us what city " + inst2_val + " is located.\n<br>\n";
						error_ind = true;
					}
			if (state2_val.length == 0){
						errored_fields[error_count] = 'sInstState2div';
						error_count++;
						error_msg += "Please tell us what state " + inst2_val + " is located.\n<br>\n";
						error_ind = true;
					}
			if (credit2_val.length == 0){
						errored_fields[error_count] = 'sCredits2div';
						error_count++;
						error_msg += "Please tell us how many credits you earned while attending " + inst2_val + ".\n<br>\n";
						error_ind = true;
					}
			if (degree2_val.length == 0){
						errored_fields[error_count] = 'sDegree2div';
						error_count++;
						error_msg += "Please tell us what degree you earned while attending " + inst2_val + ".\n<br>\n";
						error_ind = true;
					}																	
	}
	
	if (inst3_val.length != 0){
			if (attend3_val.length == 0){
						errored_fields[error_count] = 'sAttendance3div';
						error_count++;
						error_msg += "Please tell us when you attended " + inst3_val + ".\n<br>\n";
						error_ind = true;
					}	
			if (city3_val.length == 0){
						errored_fields[error_count] = 'sInstCity3div';
						error_count++;
						error_msg += "Please tell us what city " + inst3_val + " is located.\n<br>\n";
						error_ind = true;
					}
			if (state3_val.length == 0){
						errored_fields[error_count] = 'sInstState3div';
						error_count++;
						error_msg += "Please tell us what state " + inst3_val + " is located.\n<br>\n";
						error_ind = true;
					}
			if (credit3_val.length == 0){
						errored_fields[error_count] = 'sCredits3div';
						error_count++;
						error_msg += "Please tell us how many credits you earned while attending " + inst3_val + ".\n<br>\n";
						error_ind = true;
					}
			if (degree3_val.length == 0){
						errored_fields[error_count] = 'sDegree3div';
						error_count++;
						error_msg += "Please tell us what degree you earned while attending " + inst3_val + ".\n<br>\n";
						error_ind = true;
					}																	
	}		
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the ACT section.
//
//////////////////////////////////////////////////////////////////////////////
function validate_act(){
	
	var act_val = '', act_date_val = '', act_prcc_val = '';
	var act_date_reg = /(0?[1-9]|1[012])[- /.](19|20)\d\d/;
	
	// get value
	for (i=0;i<document.forms['onlineapp'].bACT.length;i++)
	{
		if (document.forms['onlineapp'].bACT[i].checked) {	act_val = document.forms['onlineapp'].bACT[i].value; }
	}
	
	// error check
	switch (act_val){
		case "" :
				errored_fields[error_count] = 'bACTdiv';
				error_count++;
				error_msg += "Please tell us whether or not you have taken the ACT.\n<br>\n";
				error_ind = true;
				break;
		case "Y" :
				act_date_val = document.getElementById('sACTDate').value;
				for (i=0;i<document.forms['onlineapp'].bACTPRCC.length;i++)
				{
					if (document.forms['onlineapp'].bACTPRCC[i].checked) {	act_prcc_val = document.forms['onlineapp'].bACTPRCC[i].value; }
				}
				if (act_date_val.match(act_date_reg) == null){
						errored_fields[error_count] = 'sACTDatediv';
						error_count++;
						error_msg += "Invalid Date. Please enter the date you took the ACT in a mm/yyyy format.\n<br>\n";
						error_ind = true;
				}
				if (act_prcc_val == ''){
						errored_fields[error_count] = 'bACTPRCCdiv';
						error_count++;
						error_msg += "Please tell us whether or not you sent your ACT scores to Pearl River CC.\n<br>\n";
						error_ind = true;
				}					
				break;
		case "N" :
				break;
		default :	break;						
	}
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Other 
//	Information section.  
//
//////////////////////////////////////////////////////////////////////////////
function validate_felony(){
	
	var felony_val = '';
	var dismissed_val = '';
	
	// get values
	for (i=0;i<document.forms['onlineapp'].bFelony.length;i++)
	{
		if (document.forms['onlineapp'].bFelony[i].checked) {	felony_val = document.forms['onlineapp'].bFelony[i].value; }
	}
	
	for (i=0;i<document.forms['onlineapp'].bDismissed.length;i++)
	{
		if (document.forms['onlineapp'].bDismissed[i].checked) { dismissed_val = document.forms['onlineapp'].bDismissed[i].value; }
	}
	
	// error check
	if (felony_val == ''){
			errored_fields[error_count] = 'bFelonydiv';
			error_count++;
			error_msg += "Please tell us whether or not you have committed a felony.\n<br>\n";
			error_ind = true;
	}

	if (dismissed_val == ''){
			errored_fields[error_count] = 'bDismisseddiv';
			error_count++;
			error_msg += "Please tell us whether or not you have been previously dismissed from a college.\n<br>\n";
			error_ind = true;
	}	
	
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Medical
//	Authorization section.
//
//////////////////////////////////////////////////////////////////////////////
function validate_medical(){
	
	var stuinit = '', parinit = '', studobdate = '', pardobdate = '', addrphone = '';
	var student_birth = '';
	
	// Get the Student's Birthdate
	for (i=0;i<document.forms['onlineapp'].nDobmonth.length;i++)
	{
		if (document.forms['onlineapp'].nDobmonth[i].selected) {	student_birth = document.forms['onlineapp'].nDobmonth[i].value; }
	}
	student_birth += "/" + document.getElementById('nDobday').value + "/" + document.getElementById('nDobyear').value; 

// Get the Parent's Submitted Birthdate
	for (i=0;i<document.forms['onlineapp'].nPGAMPDobmonth.length;i++)
	{
		if (document.forms['onlineapp'].nPGAMPDobmonth[i].selected) {	pardobdate = document.forms['onlineapp'].nPGAMPDobmonth[i].value; }
	}
	pardobdate += "/" + document.getElementById('nPGAMPDobday').value + "/" + document.getElementById('nPGAMPDobyear').value; 

// Get the Student's Submitted Birthdate
	for (i=0;i<document.forms['onlineapp'].nStuAMPDobmonth.length;i++)
	{	
		if (document.forms['onlineapp'].nStuAMPDobmonth[i].selected) {	studobdate = document.forms['onlineapp'].nStuAMPDobmonth[i].value; }
	}
	studobdate += "/" + document.getElementById('nStuAMPDobday').value + "/" + document.getElementById('nStuAMPDobyear').value; 
	
	stuinit = document.getElementById('sStuAMP').value;
	parinit = document.getElementById('sPGAMP').value;
	addrphone = document.getElementById('sAMPaddr').value;
	
	// Make sure the birthdate is valid
	var tempdobmsg = isValidDate(student_birth);
		// Birth date is valid
		if (tempdobmsg == ""){
				// Get the student's age
				if ((age(student_birth)) >= 21){
						// validate student section
							var first_val = document.getElementById('sFirstname').value;
							var last_val = document.getElementById('sLastname').value;
							var middle_val = document.getElementById('smiddlename').value;
	
							if (stuinit.length == 0){
									errored_fields[error_count] = 'sStuAMPdiv';
									error_count++;
									error_msg += "Authorization for Medical Procedures. Please enter your initials as a form of signature.\n<br>\n";
									error_ind = true;
									
									}
							else switch (stuinit.length){
											case 2 :
															if (stuinit.toUpperCase() != (first_val.charAt(0).toUpperCase() + last_val.charAt(0).toUpperCase())){
															errored_fields[error_count] = 'sStuAMPdiv';
															error_count++;
															error_msg += "Authorization for Medical Procedures. Your verification initials did not match up with the first and last name given in the Person Details section.\n<br>\n";
															error_ind = true;	
															}
															break;
											case 3 :
															if (stuinit.toUpperCase() != (first_val.charAt(0).toUpperCase() + middle_val.charAt(0).toUpperCase() + last_val.charAt(0).toUpperCase())){
															errored_fields[error_count] = 'sStuAMPdiv';
															error_count++;
															error_msg += "Authorization for Medical Procedures. Your verification initials did not match up with the first, middle, and last name given in the Person Details section.\n<br>\n";
															error_ind = true;	
															}
															break;
											default : 	
															errored_fields[error_count] = 'sStuAMPdiv';
															error_count++;
															error_msg += "Authorization for Medical Procedures. Only 2 or 3 letters are allowed for initials.\n<br>\n";
															error_ind = true;
											}
																							
							var tempdobmsg2 = isValidDate(studobdate);
								if (tempdobmsg2 != ""){
										errored_fields[error_count] = 'sStuAMPDOBdiv';
										error_count++;
										error_msg += "Authorization for Medical Procedures. " + tempdobmsg2 + "\n<br>\n";
										error_ind = true;
									}
								else {
											if (student_birth != studobdate){
													errored_fields[error_count] = 'sStuAMPDOBdiv';
													error_count++;
													error_msg += "Your verficiation date of birth does not match the one given in the Personal Details section.\n<br>\n";
													error_ind = true;
											}
										}
				}
				else {
								//validate parent section
								if (parinit.length == 0){
																	errored_fields[error_count] = 'sPGAMPdiv';
																	error_count++;
																	error_msg += "Authorization for Medical Procedures. Please enter your initials as a form of signature.\n<br>\n";
																	error_ind = true;
																}
																												
								var tempdobmsg3 = isValidDate(pardobdate);
													if (tempdobmsg3 != ""){
															errored_fields[error_count] = 'sPGAMPDOBdiv';
															error_count++;
															error_msg += "Authorization for Medical Procedures. " + tempdobmsg3 + "\n<br>\n";
															error_ind = true;
														}
													
							}		
				if (addrphone.length == 0){
						errored_fields[error_count] = 'sAMPaddrdiv';
						error_count++;
						error_msg += "Please give us an address and phone number for the Authorization for Medical Procedures section.\n<br>\n";
						error_ind = true;
					}
		}
		// Not a valid birthdate, so highlight the section
		else {
						errored_fields[error_count] = 'sStuAMPdiv';
						error_count++;
						errored_fields[error_count] = 'sStuAMPDOBdiv';
						error_count++;
						errored_fields[error_count] = 'sPGAMPdiv';
						error_count++;
						errored_fields[error_count] = 'sPGAMPDOBdiv';
						error_count++;
						errored_fields[error_count] = 'sAMPaddrdiv';
						error_count++;
						error_msg += "Authorization for Medical Procedures cannot be checked because an invalid date of birth was enter in the Personal Details section.\n<br>\n";																								
						error_ind = true;
					}

	
}


//////////////////////////////////////////////////////////////////////////////
//
//	This is function will validate all the required fields in the Student 
//	Verification section.
//
//////////////////////////////////////////////////////////////////////////////
function validate_stuver(){
	
	var initial_val = '', vdob_val = '';
	var first_val = document.getElementById('sFirstname').value;
	var last_val = document.getElementById('sLastname').value;
	var middle_val = document.getElementById('smiddlename').value;
	
	// get form values
	initial_val = document.getElementById('sVerifyInitials').value;
	for (i=0;i<document.forms['onlineapp'].nVDobmonth.length;i++)
	{
		if (document.forms['onlineapp'].nVDobmonth[i].selected) {	vdob_val = document.forms['onlineapp'].nVDobmonth[i].value; }
	}
	vdob_val += "/" + document.getElementById('nVDobday').value + "/" + document.getElementById('nVDobyear').value;
	
	// error check
	if (initial_val.length == 0){
						errored_fields[error_count] = 'sVerifyInitialsdiv';
						error_count++;
						error_msg += "Please enter your initials as a form of signature.\n<br>\n";
						error_ind = true;
					}
	else switch (initial_val.length){
					case 2 :
									if (initial_val.toUpperCase() != (first_val.charAt(0).toUpperCase() + last_val.charAt(0).toUpperCase())){
									errored_fields[error_count] = 'sVerifyInitialsdiv';
									error_count++;
									error_msg += "Your verification initials did not match up with the first and last name given in the Person Details section.\n<br>\n";
									error_ind = true;	
									}
									break;
					case 3 :
									if (initial_val.toUpperCase() != (first_val.charAt(0).toUpperCase() + middle_val.charAt(0).toUpperCase() + last_val.charAt(0).toUpperCase())){
									errored_fields[error_count] = 'sVerifyInitialsdiv';
									error_count++;
									error_msg += "Your verification initials did not match up with the first, middle, and last name given in the Person Details section.\n<br>\n";
									error_ind = true;	
									}
									break;
					default : 	
									errored_fields[error_count] = 'sVerifyInitialsdiv';
									error_count++;
									error_msg += "Only 2 or 3 letters are allowed for initials.\n<br>\n";
									error_ind = true;
					}
									
	var tempdobmsg = isValidDate(vdob_val);
		if (tempdobmsg != ""){
				errored_fields[error_count] = 'sVerifyDOBdiv';
				error_count++;
				error_msg += "Verification. " + tempdobmsg + "\n<br>\n";
				error_ind = true;
			}
		else {
						var dob_val = '';
						for (i=0;i<document.forms['onlineapp'].nDobmonth.length;i++)
								{
									if (document.forms['onlineapp'].nDobmonth[i].selected) {	dob_val = document.forms['onlineapp'].nDobmonth[i].value; }
								}
						dob_val += "/" + document.getElementById('nDobday').value + "/" + document.getElementById('nDobyear').value;
					if (dob_val != vdob_val){
							errored_fields[error_count] = 'sVerifyDOBdiv';
							error_count++;
							error_msg += "Your verficiation date of birth does not match the one given in the Personal Details section.\n<br>\n";
							error_ind = true;
					}
				}						
					
}

//***********************************************************
//
//				SUPPORT FUNCTIONS
//
//***********************************************************


/////////////////////////////////////////////////////
//
//		This function will calculate a person's age
//		based on a given date.
//
/////////////////////////////////////////////////////
function age(bDay){
 now = new Date();
 bD = new Array(); 
 bD = bDay.split('/');
 if(bD.length==3){
   born = new Date(bD[2], bD[0]*1-1, bD[1]);
   years = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));
   return years;
 }
}



/////////////////////////////////////////////////////
//
//		This function will print out the error message
//		as well as assign the errored fields the error
//		class.  This will allow the fields to stand out
//		on the form.
//
/////////////////////////////////////////////////////
function print_error(id,text)
{	
	x = document.getElementById(id);
	x.innerHTML = '';
	x.innerHTML = text;
	
	for(i=0;i<errored_fields.length;i++)
			{
			y = document.getElementById(errored_fields[i]);
			y.className += " error";
			}
	
	document.getElementById('errormsganchor').focus(); 

}


/////////////////////////////////////////////////////
//
//		This function allows us to get all the HTML
//		elements that are associated with a particular
//		class.
//
/////////////////////////////////////////////////////
function getElementbyClass(classname){
 
 var inc=0;
 var alltags=document.all? document.all : document.getElementsByTagName('*');
 var customcollection = new Array();
 var tempstring;
 
 for (i=0; i<alltags.length; i++){
 	 tempstring = alltags[i].className;
   if (tempstring.indexOf(classname) > -1){
     customcollection[inc] = alltags[i].id;
     inc++;
    }
 	}
 return customcollection;
}  


/////////////////////////////////////////////////////
//
//	This function will clear the the errorclass from
//	each HTML element.  This is used as a way to 
//	initialize the error checking procedure.
//
/////////////////////////////////////////////////////
function remove_error_class(errorclass){
	
	error_count = 0;
	error_msg = "";
	error_ind = false;
	
	for (i=0; i<errored_fields.length; i++){
			var x = document.getElementById(errored_fields[i]).className
			x = x.replace(errorclass, '');
			document.getElementById(errored_fields[i]).className = x;		
	}
	
	errored_fields.length = 0;
	
}


/////////////////////////////////////////////////////
//
//	This function validates a user input to make sure
//	it matches a valid state abbreviation.
//
/////////////////////////////////////////////////////
function validate_state(userinput){
	
	var state_regex = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
	
	return userinput.match(state_regex);
	
}


/////////////////////////////////////////////////////
//
//		This function validates a phone number to make
//		sure it is in a 123-456-7890 format.
//
/////////////////////////////////////////////////////
function validate_phone(userinput){
	
	var phone_regex = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
	
	return userinput.match(phone_regex);
	
}


/////////////////////////////////////////////////////
//
//		This function makes sure a value is a valid zip
//		code.  A valid zip code is either 12345 or 
//		12345-6789.
//
/////////////////////////////////////////////////////
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;
	var emsg = "";
	
	if (field.length!=5 && field.length!=10) {
	emsg = "Please enter your 5 digit or 5 digit+4 zip code.";
	return emsg;
	}
	for (var i=0; i < field.length; i++) {
	temp = "" + field.substring(i, i+1);
	if (temp == "-") hyphencount++;
	if (valid.indexOf(temp) == "-1") {
	emsg = "Invalid characters in your zip code.";
	return emsg;
	}
	if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
	emsg = "The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.";
	return emsg;
	   }
	}
	return emsg;
}


/////////////////////////////////////////////////////
//
//		This function makes sure that a passed value is
// 		a valid SSN.
//
/////////////////////////////////////////////////////
function SSNValidation(ssn) {
	var msg = "";
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.split('-').length - 1;
	
	if (matchArr == null || numDashes == 1) {
				msg = "Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.";
				return msg;
		}
	else 
				if (parseInt(matchArr[1],10)==0) {
						msg = "Invalid SSN: SSN's can't start with 000.";
						return msg;
					}
				else {
							return msg;
	   			}
}


/////////////////////////////////////////////////////
//
//	This function checks to make sure a date is valid.
//  See comments within code for more detail.
//
/////////////////////////////////////////////////////
function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	
	// This regex is for an optional 4 digit year
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	// To require a 4 digit year entry, use this line instead:
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var msg = "";
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
			msg = "Date is not in a valid format.";
			return msg;
		}
	
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	
	if (month < 1 || month > 12) { // check month range
			msg = "Month must be between 1 and 12.";
			return msg;
		}
	
	if (day < 1 || day > 31) {
			msg = "Day must be between 1 and 31.";
			return msg;
		}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			msg = "Month "+month+" doesn't have 31 days."
			return msg
		}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
				msg = "February " + year + " doesn't have " + day + " days.";
				return msg;
	   	}
		}
	
	return msg;  // date is valid
}


/////////////////////////////////////////////////////
//
//		This function checks to make sure a value is in 
//    a format that is equal to abcd@abcd.efg
//
/////////////////////////////////////////////////////
function validate_email(userinput){
	
	var email_regex = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	
	return userinput.match(email_regex);
	
}

