// JavaScript Document
function onChangeStaffMember(selctrl)
{
	if (selctrl.value == "yes") 
	{
		document.getElementById("staff_school_prop").style.display = "block";		
		document.getElementById("staff_school_val").style.display = "block";		
	} else {
		document.getElementById("staff_school_prop").style.display = "none";		
		document.getElementById("staff_school_val").style.display = "none";		
	}
}
function validate_reg_inputs(_form)
{
	if (!checkexistence(_form.first.value)) {
		alert ("Please enter your first name.");
		return false;
	}
	if (!checkexistence(_form.last.value)) {
		alert ("Please enter your last name.");
		return false;
	}
	if (!checkexistence(_form.address1.value)) {
		alert ("Please enter your address.");
		return false;
	}
	if (!checkexistence(_form.city.value)) {
		alert ("Please enter your city.");
		return false;
	}
	if (!checkexistence(_form.state.value)) {
		alert ("Please select your state.");
		return false;
	}
	if (_form.state.value == "none") {
		alert ("Please select your state.");
		return false;
	}
	if (!checkexistence(_form.zip.value)) {
		alert ("Please enter your postal code.");
		return false;
	}
	if (!checkexistence(_form.day_phone.value)) {
		alert ("Please enter your daytime phone number.");
		return false;
	}
	if (!checkexistence(_form.eve_phone.value)) {
		alert ("Please enter your evening phone number.");
		return false;
	}
	if (!checkexistence(_form.username.value)) {
		alert ("Please enter a username for your account.");
		return false;
	}
	if (_form.username.value != _form.confirm_username.value) {
		alert ("Usernames do not match! Please try again.");
		return false;
	}
	if (!checkexistence(_form.email.value)) {
		alert ("Please enter your email address.");
		return false;
	}
	if (!isEmailValid(_form.email.value)) {
		alert ("Please enter a valid email address.");
		return false;
	}
	if (!checkexistence(_form.password.value)) {
		alert ("Please enter your account password.");
		return false;
	}
	if (_form.password.value != _form.confirm_password.value) {
		alert ("Passwords do not match! Please try again.");
		return false;
	}
	var is_staff = 0;
	if (_form.is_staff.value == "yes") {
		if (!checkexistence(_form.school_passwd_1.value)) {
			alert ("Please enter the school password of the school you are a staff member of.");
			return false;
		}
		is_staff = 1;
	}
	if (!is_staff) {
		if (!_form.num_children.checked) {
			alert ("You must indicate that you are registering children if you are not a staff member ordering meals for yourself.");
			return false;
		}
		if (!checkexistence(_form.school_passwd_1.value)) {
			alert ("Please enter the password of the school you are registering your children.");
			return false;
		}
	}

	return true;
}
//////////////////////////////////////////////////////
// registration profile functions
///////////////////////////////////////////////////
function get_school_rooms_cb (lSchool)
{
	
	document.getElementById(lSchool.elem_id).innerHTML = lSchool.html_code;
}

function get_school_rooms(elem_id, school_id, selctrl_name)
{
	var myObj = new Object();
	myObj.elem_id = elem_id;
	myObj.school_id = school_id;
	myObj.sel_name = selctrl_name;
	
	x_listRooms(myObj, get_school_rooms_cb);
}

function get_staff_school_rooms()
{
	var school_id = document.regForm.staff_school_id.value;
	var elem_id = "staff_school_rooms";
	var selctrl_name = "staff_rtg";
	
	get_school_rooms(elem_id, school_id, selctrl_name);
	
}
function get_student_school_rooms()
{
	var _form = document.regForm;
	var school_id = document.regForm.student_school.value;
	var elem_id = "student_school_rooms";
	var selctrl_name = "student_rtg";
	
	get_school_rooms(elem_id, school_id, selctrl_name);
}
/////////////////////////////
// add staff profile
/////////////////////////////
var status_message = '';
function add_staff_profile_cb(lStudent)
{
	alert (lStudent.error_message);	
	if (lStudent.error_code == 1) {
		status_message = status_message + lStudent.status_message;
		//alert (status_message);
		//var stat_html = "<font color=\"#00aa00\"><strong>" + status_message + "</strong></font>";
		//document.getElementById("status_msg").innerHTML = stat_html;
		//document.getElementById("staff_profile_section").style.display = "none";
		document.getElementById("staff_school_rooms").innerHTML = lStudent.school_room_name;
		document.getElementById("staff_add_button").style.display = "none";
		get_profile_status();
	}
}

function add_staff_profile()
{
	var _form = document.regForm;
	var myObj = new Object();
	myObj.parent_id = _form.parent_id.value;
	myObj.school_id = _form.staff_school_id.value;
	myObj.room_id = _form.staff_rtg.value;
	myObj.first = _form.parent_first_name.value;
	myObj.last = _form.parent_last_name.value;
	myObj.is_staff = 1;

	
	x_addProfile(myObj, add_staff_profile_cb);
	
}
//////////////////////////////////
// student profile
///////////////////////////////
function add_student_profile_cb(lStudent)
{
	if (lStudent.error_code == -1) {
		// profile exists, but not associated with account
		if (confirm(lStudent.error_message)) {
			// call different sajax function
			x_associateProfile(lStudent, add_student_profile_cb);
		}
	} else {
		alert (lStudent.error_message);	
		if (lStudent.error_code == 1) {
			document.regForm.student_first.value = '';
			document.regForm.student_last.value = '';
			status_message = status_message + lStudent.status_message;
			get_profile_status();
		} 
	}
}
function add_student_profile()
{
	var _form = document.regForm;
	if (!checkexistence(_form.student_first.value)) {
		alert ("Please enter the student's first name.");
		return false;
	}
	if (!checkexistence(_form.student_last.value)) {
		alert ("Please enter the student's last name.");
		return false;
	}
	if (_form.student_rtg.value == "none") {
		alert ("Please select your child's school room.");
		return false;
	}
	
	var myObj = new Object();
	myObj.parent_id = _form.parent_id.value;
	myObj.school_id = _form.student_school.value;
	myObj.room_id = _form.student_rtg.value;
	myObj.first = _form.student_first.value;
	myObj.last = _form.student_last.value;
	myObj.is_staff = 0;

	
	x_addProfile(myObj, add_student_profile_cb);
	return true;
}
///////////////////////////////////////////////
// get profile status
////////////////////////////////////////////////
function get_profile_status_cb(lProfile)
{
	document.getElementById("status_msg").innerHTML = "<br /><font color=\"#457731\"><strong>" + lProfile.profile_status + "</strong></font><br /><br />";
}
function get_profile_status()
{
	var _form = document.regForm;
	var myObj = new Object();
	myObj.parent_id = _form.parent_id.value;
	
	x_getProfileStatus(myObj, get_profile_status_cb);
}
//////////////////////////////////
// student profile
///////////////////////////////
function add_student_profile_cb(lStudent)
{
	if (lStudent.error_code == -1) {
		// profile exists, but not associated with account
		if (confirm(lStudent.error_message)) {
			// call different sajax function
			x_associateProfile(lStudent, add_student_profile_cb);
		}
	} else {
		alert (lStudent.error_message);	
		if (lStudent.error_code == 1) {
			document.regForm.student_first.value = '';
			document.regForm.student_last.value = '';
			status_message = status_message + lStudent.status_message;
			get_profile_status();
		} 
	}
}
function add_student_profile()
{
	var _form = document.regForm;
	if (!checkexistence(_form.student_first.value)) {
		alert ("Please enter the student's first name.");
		return false;
	}
	if (!checkexistence(_form.student_last.value)) {
		alert ("Please enter the student's last name.");
		return false;
	}
	if (_form.student_rtg.value == "none") {
		alert ("Please select your child's school room.");
		return false;
	}
	
	var myObj = new Object();
	myObj.parent_id = _form.parent_id.value;
	myObj.school_id = _form.student_school.value;
	myObj.room_id = _form.student_rtg.value;
	myObj.first = _form.student_first.value;
	myObj.last = _form.student_last.value;
	myObj.is_staff = 0;

	
	x_addProfile(myObj, add_student_profile_cb);
	return true;
}

function finished_registering()
{
	if (confirm("You should see all your registered profiles at the top of this page (in green color). If you don't see all of your students in this list, please go back and make sure you press the \"add student profile\" button after entering in the student's profile information. Do you want to continue and exit this page?")) {
		window.open('register_thanks.php','_top');
		return true;
	}
	return false;
}
function show_parent_info()
{
	alert ("Please register all of your student/staff profiles here. Enter all required information and click the \"add student profile\" or the \"add staff profile\" button to register each person. You should see a list of all successfully registered profiles at the top of this page (in green color) when you are done.");	
	
}

