
function freeGED(selection) {
	var freegeddiv = document.getElementById('ged_popup_div');
	if (selection == "nohighschool") freegeddiv.style.display = 'block';
	else freegeddiv.style.display = 'none';
}

function clearError(id) {
	if (id == 'special_dob') {
		document.getElementById('real_b_month').style.background = '#FFFFFF';
		document.getElementById('real_b_day').style.background = '#FFFFFF';
		document.getElementById('real_b_year').style.background = '#FFFFFF';
		document.getElementById('b_text').style.color = '#333333';
	} else {
		document.getElementById(id + '_box').style.background = '#FFFFFF';
		document.getElementById(id + '_text').style.color = '#333333';
	}
}

function showRemove(course_id) {
	document.getElementById('remove-' + course_id).style.visibility = 'visible';
	document.getElementById('cell-remove-' + course_id).style.background = document.getElementById('i-hate-IE-' + course_id).style.background;
}

function showCourses(link_reference) {
	link_reference.innerHTML = 'Hide';
	link_reference.onclick = function() { hideCourses(link_reference); return false; }
	
	if(navigator.appName.indexOf("Microsoft") > -1) $('tr[id^=i-hate-IE]').css('display', 'block');
	else $('tr[id^=i-hate-IE]').css('display', 'table-row')
}

function hideCourses(link_reference) {
	link_reference.innerHTML = 'View';
	link_reference.onclick = function() { showCourses(link_reference); return false; }
	
	$('tr#individual-courses-row ~ tr[id^=i-hate-IE]').css('display', 'none');
}

function validateTextFields() {
	var textfields = new Array(
		'ship_first_name',
		'ship_last_name',
		'ship_address_one',
		'ship_city',
		'ship_state_or_province',
		'ship_zip_or_postal_code',
		'ship_country_code',
		'ship_home_phone',
		'previous_education'
	);
	
	ok = true;
	
	for (i = 0; i < textfields.length; i++) {
		field_id = textfields[i];
		if (document.getElementById(field_id + '_box').value == '') {
			document.getElementById(field_id + '_box').style.background = '#FFCCCC';
			document.getElementById(field_id + '_text').style.color = '#FF0000';
			ok = false;
		} else clearError(field_id);
	}
	
	return ok;
}

function validateCourseSelected() {
	for (i = 0; i < programs.length; i++) {
		if (document.getElementById(programs[i]).checked) return true;
	}
	
	for (i = 0; i < courses.length; i++) {
		for (j = 1; j <= 5; j++) {
			if (document.getElementById(courses[i]).checked) return true;
		}
	}
	
	return false;
}

// ethan@waveriderdesign.com 8.26.2008 - replace with much smarter validateEmail in /javascript/email_validate.js
//function validateEmail(email) {
//	var regex = /^[A-Za-z0-9_.]+@([A-Za-z0-9_.]+)*[A-Za-z0-9_]+\.[A-Za-z0-9_]+$/;
//	return regex.test(email);
//}

function validateDateOfBirth() {
	var datecontroller = new Date();
	thisyear = datecontroller.getFullYear();
	year = document.getElementById('real_b_year').options[document.getElementById('real_b_year').selectedIndex].value;
	y = thisyear - year;
	
	if (y > 18) return true;
	if (y < 18) return false;
	
	thismonth = datecontroller.getMonth() + 1;
	month = document.getElementById('real_b_month').options[document.getElementById('real_b_month').selectedIndex].value;
	m = thismonth - month;
	
	if (m > 0) return true;
	if (m < 0) return false;
	
	today = datecontroller.getDate();
	day = document.getElementById('real_b_day').options[document.getElementById('real_b_day').selectedIndex].value;
	d = today - day;
	
	if (d < 0) return false;
	return true;
}

function validateAgreement() {
	return $('#i-agree').attr('checked') == true;
}

function validate() {
	ok1 = validateDateOfBirth();
	if (!ok1) {
		window.location.hash = 'personal-info-top';
		document.getElementById('birth_date_error_div').style.display = 'block';
		document.getElementById('real_b_month').style.background = '#FFCCCC';
		document.getElementById('real_b_day').style.background = '#FFCCCC';
		document.getElementById('real_b_year').style.background = '#FFCCCC';
		document.getElementById('b_text').style.color = '#FF0000';
	} else {
		document.getElementById('birth_date_error_div').style.display = 'none';
		clearError('special_dob');
	}
	
	ok2 = validateEmail(document.getElementById('ship_email_box').value);
	if (!ok2) {
		window.location.hash = 'personal-info-top';
		document.getElementById('email_error_div').style.display = 'block';
		document.getElementById('ship_email_text').style.color = '#FF0000';
		document.getElementById('ship_email_box').style.background = '#FFCCCC';
	} else {
		document.getElementById('email_error_div').style.display = 'none';
		clearError('ship_email');
	}
	
	ok3 = validateTextFields();
	if (!ok3) {
		window.location.hash = 'personal-info-top';
		errordiv = document.getElementById('empty_text_fields');
		if(navigator.appName.indexOf("Microsoft") > -1) errordiv.style.display = 'block';
		else errordiv.style.display = 'table-row';
	} else document.getElementById('empty_text_fields').style.display = 'none';
		
	ok4 = validateCourseSelected();
	if (!ok4) {
		window.location.hash = 'top-of-content';
		errordiv = document.getElementById('no_course_selected');
		if(navigator.appName.indexOf("Microsoft") > -1) errordiv.style.display = 'block';
		else errordiv.style.display = 'table-row';	
	} else document.getElementById('no_course_selected').style.display = 'none';
	
	ok5 = validateAgreement();
	if (!ok5) $('#i-agree-error').css('display', 'inline');
	else $('#i-agree-error').css('display', 'none');
	
	return (ok1 && ok2 && ok3 && ok4 && ok5);
}

function deselectAll(course_id) {
	document.getElementById(course_id).checked = false;
	document.getElementById('remove-' + course_id).style.visibility = "hidden";
	document.getElementById('cell-remove-' + course_id).style.background = '#ebdfb9';
}