//=========================================================================================================
//										Flow Dough Account Functions									  =
//=========================================================================================================

function flowdough_deposit(serial_number) {
	
	if (serial_number.length > 14) {
	
		$.post("scripts/php/flowdough-add.php", { fdserial:serial_number }, function (return_data) {
																					  
				var status = return_data.slice(return_data.lastIndexOf(":") + 1);	
				
				if (return_data.indexOf("deposit:success") > -1) {	//if successful
					// update user's status message, fade it in
					$("#deposit-status").html("Way to go! Your Flow Dough has been deposited in your account!").fadeIn("slow");
					//update user's flow dough total onscreen
					$("#flowdough-value").empty().text("$" + status);
				
				$("#txt_serial").val('');
					
					$.post("scripts/php/deposit_history.php", {}, function(return_data_2) {
					
						if (return_data_2.length > 50)
							$("#flowdough-transaction-details").empty().html(return_data_2);
																		   
					});
					
				}
		
				if (return_data.indexOf("deposit:fail") > -1) {	//if un-successful
					//if serial # is invalid, set user status to say so, fade in
					if (status.indexOf("invalid") > -1)	
						$("#deposit-status").html("Oops! You’ve entered an invalid serial number.<br>Recheck your Flow Dough and try again!"+status).fadeIn("slow");
					
					//if serial # is inactive, set user status to say so, fade in
					if (status.indexOf("inactive") > -1)	
						$("#deposit-status").html("Sorry...the Flow Dough with that serial number was already deposited.<br>Recheck your Flow Dough and try again!"+status).fadeIn("slow");
					
					//if there was a database error, set user status to say so, fade in
					if (status.indexOf("database") > -1)	
						$("#deposit-status").html("So sorry...there was an error while depositing.<br>Please try again!").fadeIn("slow");
				}	
																						   
		});
	
	} else {	// if serial_number length != 16
	
		$("#deposit-status").html("Oops! You’ve entered an invalid serial number.<br>Recheck your Flow Dough and try again!").fadeIn("slow");
	
	}
	
	// set the status message to fade out after 2 seconds, and clear it for next time.
	setTimeout(function() { $("#deposit-status").fadeOut("slow", function() { $("#deposit-status").text(''); });	}, 3500);
				

}

function account_edit() {
	
	var c_title = $("#opt_title").val();
	var c_first = $("#txt_first").val();
	var c_last = $("#txt_last").val();
	var c_address = $("#txt_address").val();
	var c_city = $("#txt_city").val();
	var c_state = $("#lst_state").val();
	var c_zip = $("#txt_zip").val();
	var c_phone = $("#txt_phone").val();
	var c_email = $("#txt_email").val();
	var c_practice = $("#txt_practice_name").val();
	var c_dealer = $("#txt_dealer_name").val();
	var c_dealer_city = $("#txt_dealer_city").val();
	var c_dealer_state = $("#lst_dealer_state").val();
	var c_username = $("#txt_username").val();
	var c_password = $("#txt_password").val();
	var c_password_confirm = $("#txt_password_confirm").val();
	
	$.post("scripts/php/edit_account.php", 
		   {
			
			title:c_title,
			first:c_first,
			last:c_last,
			address:c_address,
			city:c_city,
			state:c_state,
			zip:c_zip,
			phone:c_phone,
			email:c_email,
			practice:c_practice,
			dealer:c_dealer,
			dealer_city:c_dealer_city,
			dealer_state:c_dealer_state,
			username:c_username,
			password:c_password,
			password_confirm:c_password_confirm
			
			}, 
	
	function(return_data) {
		
		//if (return_data.indexOf("modify:success") > -1) {	//if successful
		
		if (return_data.length > 75) {
			// update user's status message, fade it in
			$(".show-details").text("Edit");
			$(".show-details").addClass("show-edit");
			$(".show-details").removeClass("show-details");
			$("#flowdough-account-edit").fadeOut("medium", function() {
				$("#flowdough-account-details").empty().html(return_data).fadeIn("medium");		
				$("#account-status").text("Your Account Info Has Been Successfully Updated!").fadeIn("slow");
			});
		} else {
			var error = return_data.slice(return_data.lastIndexOf(":") + 1);	
			$("#account-status").text(error);
		}

		// set the status message to fade out after 2 seconds, and clear it for next time.
		setTimeout(function() { $("#account-status").fadeOut("slow", function() { $("#account-status").text(''); });	}, 3500);
													
														
	});

}


function account_register() {
	
	var c_title = $("#opt_title").val();
	var c_first = $("#txt_first").val();
	var c_last = $("#txt_last").val();
	var c_address = $("#txt_address").val();
	var c_city = $("#txt_city").val();
	var c_state = $("#lst_state").val();
	var c_zip = $("#txt_zip").val();
	var c_phone = $("#txt_phone").val();
	var c_email = $("#txt_email").val();
	var c_practice = $("#txt_practice_name").val();
	var c_dealer = $("#txt_dealer_name").val();
	var c_dealer_city = $("#txt_dealer_city").val();
	var c_dealer_state = $("#lst_dealer_state").val();
	var c_username = $("#txt_username").val();
	var c_password = $("#txt_password").val();
	var c_password_confirm = $("#txt_password_confirm").val();
	
	$.post("scripts/php/register.php", 
		   {
			
			title:c_title,
			first:c_first,
			last:c_last,
			address:c_address,
			city:c_city,
			state:c_state,
			zip:c_zip,
			phone:c_phone,
			email:c_email,
			practice:c_practice,
			dealer:c_dealer,
			dealer_city:c_dealer_city,
			dealer_state:c_dealer_state,
			username:c_username,
			password:c_password,
			password_confirm:c_password_confirm
			
			}, 
	
	function(return_data) {
		
		if (return_data.indexOf("register:success") > -1) {	//if successful
			window.location="main.php?page=flowdough";
		} else {
			$("#register-message").empty().html(return_data);
			$("#register-status").fadeIn("medium");
		}												
														
	});

}	//end account_register



//=========================================================================================================
//										Assign Functions to DOM											  =
//=========================================================================================================
$(document).ready(function() {

	$("#deposit-status").empty().hide();
	$("#register-status").hide();
	$("#flowdough-account-edit").hide();
	
	$("#button-register").live("click", function() {
		account_register();	
	});

	$("#flowdough-add").live("click", function() {
		var serial_number = $("#txt_serial").val().toUpperCase();
		flowdough_deposit(serial_number);
	});	
	
	$(".show-edit").live("click", function() {
		$(this).text("Back To Account Details");
		$(this).addClass("show-details");
		$(this).removeClass("show-edit");
		$("#flowdough-account-details").fadeOut("medium", function() {
			$("#flowdough-account-edit").fadeIn("medium");														   
		});
		return false;
	});
	
	$("#button-save").live("click", function() {
		/*$(this).text("Edit");
		$(this).addClass("show-edit");
		$(this).removeClass("show-details");
		$("#flowdough-account-edit").fadeOut("medium", function() {
			$("#flowdough-account-details").fadeIn("medium");														   
		});
		return false;*/
		account_edit();
											 
	});


	$(".show-details").live("click", function() {
		$(this).text("Edit");
		$(this).addClass("show-edit");
		$(this).removeClass("show-details");
		$("#flowdough-account-edit").fadeOut("medium", function() {
			$("#flowdough-account-details").fadeIn("medium");														   
		});
		return false;
	});	
				   


});


