function submitForm()
{   var submitFlg = true;
    submitFlg = verifyTutorSubmission();
    if ( submitFlg ) {
        document.forms['form1'].submit();
    }
}

function checkCBs() {
	var subjSelected = false;
	var selectionListObj = document.getElementById("subjectSelection");
	var aCBs = selectionListObj.getElementsByTagName('input');
	//alert(toggle);
	for (i = 0; i < aCBs.length; i++) {
		if (aCBs[i].checked == true) {
			subjSelected = true;
			break;
		}
	}

	if (!subjSelected) {
		msg = "Please select at least one subject to tutor.";
		alert(msg);
		//document.forms[FormName].elements[x].focus()
		document.forms['form1'].elements[aCBs[0].id].focus()
	}

	return subjSelected;
}
function evalKey(pKey) {
	var v_day;
	var pDay = pKey.substr(0,1)
	switch(Number(pDay)) {
		case 1:
			v_day = "Sunday";
		break;
		case 2:
			v_day =  "Monday";
		break;
		case 3:
			v_day =  "Tuesday";
		break;
		case 4:
			v_day =  "Wednesday";
		break;
		case 5:
			v_day =  "Thursday";
		break;
		case 6:
			v_day =  "Friday";
		break;
		case 7:
			v_day =  "Saturday";
		break;
	}
	return v_day + ", period " + pKey.substr(1,1);
}

function checkAvailability() {
	var selectionListObj = document.getElementById("availabilitySelection");
	var aHoursAvailable = selectionListObj.getElementsByTagName('select');
	var msg='The following errors have occurred:\n';
	var startTimes = new Object();
	var endTimes = new Object();
	var selectOK = false;
	//aHoursAvailable.length

	for(i=0; i<aHoursAvailable.length; i++) {
		theIndex = aHoursAvailable[i].selectedIndex

		if (!selectOK &&  theIndex != 0) {
			selectOK = true;
		}
		if (aHoursAvailable[i].id.indexOf('_st') != -1 && theIndex != 0) {
			//day is always position 7
			//period star is always position 12 for start
			dayTime = aHoursAvailable[i].id.substr(7,1) + aHoursAvailable[i].id.substr(11,1);
			startTimes[dayTime] = aHoursAvailable[i].options[theIndex].value;

		}
		if (aHoursAvailable[i].id.indexOf('_end') != -1 && theIndex != 0) {
			//day is always position 7
			//period star is always position 11 for start
			dayTime = aHoursAvailable[i].id.substr(7,1) + aHoursAvailable[i].id.substr(12,1);
			endTimes[dayTime] = aHoursAvailable[i].options[theIndex].value;
		}
	}

	if (selectOK) {
		var errArray = new Array();
		z=0;
		for(var stKey in startTimes) {
			var endTimeFound = false;
			var goodPeriodFound = false;
			for (var endKey in endTimes) {
				if (stKey == endKey) {
					endTimeFound = true;
					// check to see if the associated end time is good.
					if (Number(startTimes[stKey]) == Number(endTimes[endKey])) {
						errArray[z++] = "The start time for " + evalKey(stKey) + " is the same as the ending time.";
					} else if (Number(startTimes[stKey]) > Number(endTimes[endKey])){
						errArray[z++] = "The start time for " + evalKey(stKey) + " is greater than the ending time.";
					} else {
						goodPeriodFound = true;
					}
					break;
				}
			}
			if (!endTimeFound) {
				//missing end time
				errArray[z++] = "The ending time for " + evalKey(stKey) + " is missing.";
			} else if (goodPeriodFound) {
				//good period found, check to see if another starting/end period occur within our current period.
				for (var testKey in startTimes) {
					if (stKey.substr(0,1) == testKey.substr(0,1) && stKey != testKey) {
						// found another period for the current day that is a starting time
						if (Number(startTimes[stKey]) == Number(startTimes[testKey])) {
							errArray[z++] = "The start time for " + evalKey(testKey) + " is the same as the the start time for " + evalKey(stKey) + ".";
						} else if (Number(startTimes[stKey]) < Number(startTimes[testKey]) &&
							Number(endTimes[stKey]) > Number(startTimes[testKey]) ) {
							// another start time is within our time period
							errArray[z++] = "The start time for " + evalKey(testKey) + " is within " + evalKey(stKey) + ".";
						}
					}
				}
				for (var testKey in endTimes) {
					if (stKey.substr(0,1) == testKey.substr(0,1) && stKey != testKey) {
						// found another period for the current day that is an ending time
						if (Number(endTimes[stKey]) == Number(endTimes[testKey])) {
							errArray[z++] = "The ending time for " + evalKey(testKey) + " is the same as the the ending time for " + evalKey(stKey) + ".";
						} else if (Number(startTimes[stKey]) < Number(endTimes[testKey]) &&
							Number(endTimes[stKey]) > Number(endTimes[testKey]) ) {
							// another ending time is within our time period
							errArray[z++] = "The ending time for " + evalKey(testKey) + " is within " + evalKey(stKey) + ".";
						}
					}
				}
			}

		} // end start time eval

		for(var endKey in endTimes) {
			var stTimeFound = false;
			for(var stKey in startTimes) {
				if (endKey == stKey) {
					stTimeFound = true;
					break;
				}
			}

			if (!stTimeFound) {
				errArray[z++] = "The starting time for " + evalKey(endKey) + " is missing.";
			}

		}//end end time eval
		if (errArray.length != 0) {
			for(key in errArray) {
				msg+=key+' '+errArray[key]+"\n";
				selectOK = false;
			}
			alert(msg+'\n'+'Please modify your starting or ending times');
		}
		return selectOK;
	}

	alert ('Please select the hours you\'re available to tutor.');
	return false;
}

function checkIfEmpty(list) {
    var verified = true;
    for(var i = 0; i <list.length; i++) {
        var item_id = 'id_' + list[i];
        var item = document.getElementById(item_id);
        if(item.value == "") {
            verified = false;
            item.style.backgroundColor = '#FF445D';
        }
        else {
            item.style.backgroundColor = '#FFFFFF';
        }
    }
    if(verified == false) {
        alert("Some of the information you submitted is missing or incorrect. Please check the fields marked in red");
    }

    return verified;
}

function verifyTutorSubmission() {
    var list =  Array ('fname','lname','phone', 'experience', 'email','address1','city','zip','how_did_you_hear');
    var verified = false;
	if (checkIfEmpty(list) && checkCBs() && checkAvailability()) {
		verified = true;
	}
	return verified;
}