///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	CoPilot Creative Front End CMS AJAX Call Functions 
//	Copy Right of Propel Studios, LLC.
//	Developed by Thomas Eisenbeis
//	
//	
//	File Name = ajax_framework.js
// 	This will hold all neccessary functions for calls to actions within site that will allow updates without refresh
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* -------------------------- */
/* LOGIN                      */
/* -------------------------- */
function login()
{
	
	$('#loginError').empty().html('Processing your request...<img src="/admin/images/ajax-loader.gif" width="220" height="19" title="Loader" alt="Loader" />').show('fast');
	
	var username = $('#username').val();
	var password = $('#password').val();
	$('#submit').attr("disabled", true); 
	
	$.ajax({
		type: "POST",
		url: "/admin/process/eventHandler.php",
		data: 'username='+username+'&password='+password+'&mode=login',
	   success: function(data){
		   
			if(data == 'closed')
			{
				
				// FAILED
				$('#loginError').empty().text('Your account has been closed.  Please contact support.').show(500);
				$('#submit').removeAttr("disabled");
			
			}
			else if(data == 'pending')
			{
				
				// FAILED
				$('#loginError').empty().text('Your account is pending closure. Please contact support.').show(500);
				$('#submit').removeAttr("disabled");
			
			}
			else if(data == 'suspend')
			{
						
				// FAILED
				$('#loginError').empty().text('Your account has been suspended.  Please contact support.').show(500);
				$('#submit').removeAttr("disabled");
				
			}
			else if (data == false)
			{
						
				// FAILED
				$('#loginError').empty().text('Login failed! Verify username and password.').show(500);
				$('#submit').removeAttr("disabled");
				
			}
			else
			{
				
				// SUCCESS
				closelogin();
			
			}
	   },
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$('#message').removeClass().addClass('error').text('There was an error. Please try again.').show(500);
		}
	 });
	
	return false;
}

/* -------------------------- */
/* GET PASSWORD               */
/* -------------------------- */
function getPassword()
{
	
	$('#message').empty().html('<span class="red">Processing your request...</span> <img src="/admin/images/loader.gif" width="220" height="19" title="Loader" alt="Loader" />').show('fast');
	
	var email = $('#email').val();
	
	if (email == null || email == '')
	{
	
		$('#message').empty().addClass('error').text('Please enter an email.').show('fast');
		
		return false;
		
	}
	else
	{
	
		$.ajax({
			type: "POST",
			url: "/admin/process/eventHandler.php",
			data: 'email='+email+'&mode=forgotPassword',
		   success: function(data){
			   
				if(data == false){
							
					// FAILED
					$('#message').empty().removeClass('success').addClass('error').text('Email provided does not match our records.  Please try again.').hide().fadeIn(500);
				
				} else if (data == 'error'){
					
					// ERROR
					$('#message').empty().removeClass('success').addClass('error').text('There was an error. Please try again.').hide().fadeIn(500);
				
				} else {
					
					// SUCCESS
					$('#message').empty().removeClass('error').addClass('success').text('Email has been sent with your new password.').hide().fadeIn(500);
				
				}
		   },
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$('#message').removeClass().addClass('error').text('There was an error. Please try again.').hide().fadeIn(500);
			}
		 });
		
		return false;
		
	}
	
}
