/*
** Register.js
** Copyright Ronningen Design, LLC (http://www.websitesthatdostuff.com)
** Subcontracted to Substratal Code LLC (http://www.substratalcode.com)
** Tampered with by Way Short of Heaven (http://www.wayshortofheaven.com)
**
*/

$(window).load(function() {
	checkRole();
	
	// Set up the dialog:
	$("#notificationModal").dialog({
		autoOpen: false,
		modal: true,
		dialogClass: 'notificationModal register',
		close: function(event, ui) { closeNotificationDialog(); }
	});
	
	$("#camp_role").click(function() {
		checkRole();
	});
	
	$("#camp_role").keyup(function() {
		checkRole();
	});
	
	$("#closeNotificationModal").click(function () {
		$("#notificationModal").dialog('close');
		return false;
	});
	
	$("#registrationSubmit").click(function () {
		if( $("#registerForm").validationEngine({promptPosition:"topRight", returnIsValid:true}) ) {
			submitRegistrationRequest();
		}
		return false;
	});
	
	$("#forgotPasswordSubmit").click(function () {
		if( $("#forgotPasswordForm").validationEngine({promptPosition:"topRight",returnIsValid:true}) ) {
			submitPasswordRequest();
		}
		return false;
	});
	
});

$(document).ready(function() {
	$("#forgotPasswordForm").validationEngine({
		liveEvent: true,
		promptPosition: "topRight"
	});
	$("#registerForm").validationEngine({
		liveEvent: true,
		promptPosition: "topRight"
	});
});


function checkRole() {
	// Show the parent div if the registrant is a camper:
	if($("#camp_role").val() == 'camper') {
		$("#parentFields").show();
	} else {
		$("#parentFields").hide();
	}
}

/* 
** Function to clean up stuff after closing the notification dialog.
*/
function submitRegistrationRequest() {
	// Get the data:
	var data = $("#registerForm").serialize();
	
	$.ajax({
		type: 'POST',
		url: "/users/register/", 
		data: data, 
		dataType: 'json',
		beforeSend: function() {
			$("#registrationSubmit").toggleClass("loginLoader");
		},
		complete: function() {
			$("#registrationSubmit").toggleClass("loginLoader");
		},
		success: function (response) {
			if(response.success == true) {
				// Display Thank You:
				$("#notificationThanks").text(response.data);
				$("#notificationThanks").show();
				$("#notificationError").hide();
			} else {
				$("#notificationError").text(response.data);
				$("#notificationThanks").hide();
				$("#notificationError").show();
			}
			
			$("#notificationModal").dialog('open');
		},
	});
}

/* 
** Function to clean up stuff after closing the notification dialog.
*/
function submitPasswordRequest() {
	var data = $("#forgotPasswordForm").serialize();
	
	$.ajax({
		type: "POST",
		url: "/users/requestPassword", 
		data: data,
		dataType: 'json',
		beforeSend: function() {
			$("#forgotPasswordSubmit").toggleClass("loginLoader");
		},
		complete: function() {
			$("#forgotPasswordSubmit").toggleClass("loginLoader");
		},
		success: function (response) {
			if(response.success == true) {
				// Display Thank You:
				$("#thanksMessage").text(response.data);
				$("#notificationThanks").show();
				$("#notificationError").hide();
			} else {
				$("#errorMessage").text(response.data);
				$("#notificationThanks").hide();
				$("#notificationError").show();
			}
			
			$("#notificationModal").dialog('open');
		}
	});
}


/* 
** Function to clean up stuff after closing the notification dialog.
*/
function closeNotificationDialog() {
	
}
