$(document).ready(function() {
	
	var validateEmail = function (scope) {
		
		$('input[@name="email"]', scope).blur(function() {
		
			var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			
			if ($(this).val().match(emailRegEx)) {
				
				$('#email_check')
				.css({'font-weight': 'bold', 'color': 'blue'})
				.html('E-mail address is valid');
				
			} else {
			
				$('#email_check')
				.css({'font-weight': 'bold', 'color': 'red'})
				.html('Not a valid e-mail address');
			
			}
			
		});
		
	};

	var CheckEmail = function (scope) {
		
		$('input[@name="EmailVerify"]', scope).blur(function() {
															 
			if ($(this).val() != $('input[@name="email"]').val()) {
													 
				$('#email_same')
				.css({'font-weight': 'bold', 'color': 'red'})
				.html('Does not match above address');
													 
			} else {
				
				$('#email_same')
				.css({'font-weight': 'bold', 'color': 'blue'})
				.html('Matches above address');
				
			}
													  
		});
		
	};
	
	var showFeedback = function(stuff) {
		
		$('#application').html(stuff);
		changeHeight();
		
	};
	
	var Save = function() {
		
		if ($.cookie('catoint_username')) {
		
			if ($('input[@name="nocookie"]').val() == 0) {
							 
				var queryString = $('#application input:not(:hidden, :button), #application select, #application textarea').fieldSerialize();
				
				showFeedback ('<center><img src="images/ajax-loader.gif" alt="Saving..." /><br /><br />Saving...</center>');
				
				$.post('app_save3.php', queryString);
				
			}
			
		}
		
	};
	
	var redoCaptcha = function() {
		
		var killCache = new Date().getTime();
		
		$.get('redo_captcha.php', {cachekiller: killCache}, function(data) {
													  
			$('#captcha_div').html(data);
										   
		});
		
	};
	
	var regToggle = function (scope) {
		
		$('input[@name="nonreg"]', scope).click(function() {
	
			if ($(this).attr('checked') == true) {
				
				$('#reginfo').show();
				$('#reginfo :input').removeAttr('disabled');
				changeHeight();
		
			} else {
		
				$('#reginfo').hide();
				$('#reginfo :input').attr('disabled', true);
				changeHeight();
		
			}
			
		});
	
	};
	
	var appLogin = function(scope) {
	
		$('input[@name="login"]', scope).click(function() {
												  
			$('#loginfeedback').html('<center><img style="margin-top: -4px;" src="images/ajax-login.gif" alt="Loading..." /></center>');
			
			$.post('app_login3.php', $('#application :text,#application :password,#application input:hidden,#application :checkbox').fieldSerialize(), function(data) {
														   
				switch (data) {
				
					case 'loggedin':
					$.cookie('catoint_username', $('input[@name="username"]').val());
					
					$('#tablogin')
					.removeClass()
					.attr('rel', 'logout')
					.html('Log Out');
					
					$('#tabinfo').css('display', 'inline');
					$('#formsubmit').show();
					$('#savewarning').show();
					
					$('#tabmenu .tabs:first').trigger('click');
					
					break;
					
					case 'captchafail':
					$('#captcha_feedback').html('Value entered did not match the image');
					
					$('#loginfeedback').html('');
					
					redoCaptcha();
					
					changeHeight();
					
					break;
					
					default:
					$('#captcha_feedback').html('');
					
					$('#loginfeedback').html(data);
					
					changeHeight();
					
					ajaxAdapt();
					
					break;
				
				}
														   
			});
												  
		});
		
	};
	
	var nextTab = function(scope) {
		
		$('#next', scope).click(function() {
			
			$('.active').parent().next().children().trigger('click');
										 
		});
		
	};
	
	var prevTab = function(scope) {
		
		$('#prev', scope).click(function() {
			
			$('.active').parent().prev().children().trigger('click');
										 
		});
		
	};
	
	var regToggle = function(scope) {
		
		$('#nonreg', scope).click(function() {
								  
			if ($(this).attr('checked')) {
				
				$('#reginfo').show();
				$('#reginfo input').removeAttr('disabled');
				changeHeight();
				
			} else {
				
				$('#reginfo').hide();
				$('#reginfo input').attr('disabled', true);
				changeHeight();
				
			}
			
		});
		
	};
	
	var loadForgotPw = function(scope) {
		
		$('#forgotpw', scope).click(function() {
											 
			tabSwitch ('forgotpw');
											 
		});
		
	};
	
	var getNewPw = function(scope) {
		
		$('#getnewpw', scope).click(function() {
				
				$('#pwfeedback').html('<center><img src="images/ajax-login.gif" alt="Processing..." /><br /><br />Processing...</center>');
				
				changeHeight();
				
				$.post('app_forgotpw.php', {email: $('#application input[name="email"]').val()}, function(data) {
																							  
					$('#pwfeedback').html(data);
					changeHeight();
															  
				});
											 
		});
		
	};
	
	var changeInfo = function(scope) {
		
		$('#changeinfo', scope).click(function() {
											   
			var queryString = $('#application input:not(:hidden, :button)').fieldSerialize();
			
			$('#infofeedback').html('<center><img src="images/ajax-login.gif" alt="Changing info..." /><br /><br />Changing info...</center>');
			changeHeight();
			
			$.post('app_changeinfo.php', queryString, function(data) {
															   
				switch (data) {
					
					case 'infochanged':
	
					$.cookie('catoint_username', $('input[@name="username"]').val());
					
					$('#infofeedback').html('<p>You have changed your information.</p>');
	
					break;
	
					default:
	
					$('#infofeedback').html(data);
	
					break;
					
				}
				
				changeHeight();
														  
			});
										   
		});
		
	};
	
	var ajaxAdapt = function (scope) {
		
		regToggle (scope);
		CheckEmail (scope);
		validateEmail (scope);
		appLogin (scope);
		nextTab (scope);
		prevTab (scope);
		regToggle (scope);
		loadForgotPw (scope);
		getNewPw (scope);
		changeInfo (scope);
		
	};

	
	ajaxAdapt ($(this));
	
	var tabSwitch = function(destination) {
		
		showFeedback ('<center><img style="margin-top: 20px;" src="images/ajax-loader.gif" alt="Loading..." /><br /><br />Loading...</center>');
		
		if (destination == 'info') {
			
			$('#fieldsreq').hide();
			
		} else {
			
			$('#fieldsreq').show();
			
		}
		
		$('#tabmenu .tabs').removeClass('active');
		
		var killCache = new Date().getTime();
		
		$.get('app_sections3.php', {section: destination, cachekiller: killCache}, function(data) {
		
			showFeedback (data);
			ajaxAdapt ('#application');
		
		});
		
	};
	
	$('#tabmenu .tabs').click(function() {
		
		Save();
		
		tabSwitch ($(this).attr('rel'));
		
		$(this).addClass('active');
									   
	});
	
	$('#tablogin[@rel="logout"]').click(function() {
		
		Save();
		
		$.cookie('catoint_username', null);
		
		$('#tablogin')
		.addClass('tabs')
		.addClass('active')
		.attr('rel', 'login')
		.removeAttr('title')
		.html('Log In');
		
		$('#tabinfo').hide();
		$('#formsubmit').hide();
		$('#savewarning').hide();
		
		tabSwitch ('login');
														  
	});
	
	var formSend = function(response) {
		
		showFeedback ('<center><img src="images/ajax-loader.gif" alt="Submitting..." /><br /><br />Submitting...</center>');
		
		$.post('app_post_zend.php', response, function(data) {
												  
			showFeedback (data);
												  
		});
		
	};
	
	$('#submit').click(function() {
								
		Save();
		
		var killcache = new Date().getTime();
		
		$('#tabmenu .tabs').removeClass('active');
		
		showFeedback ('<center><img src="images/ajax-loader.gif" alt="Processing..." /><br /><br />Processing...</center>');
		
		$.get('app_process.php', {cachekiller: killcache}, function(data) {
																	
			if (data.search ('<p style="font-weight: bold; color: red;">Error:</p>') != -1) {
				
				showFeedback (data);
				
			} else {
			
				formSend (data);
				
			}
																	
		});
								
	});

});
