/* 
 * All content copyright Mozzaiq, LLC
 * 
 * Plugin: Website Login/Authentication Script
 * Version: 0.7
 * Author: Mike Quinn (Mozzaiq)
 * 
 * Last Modified: April 16, 2009
 * Date Created: April 9, 2009
 * 
 * Usage Instructions:
 *   - The "login" method is applied to the HTML form containing 
 *     the userID & password fields
 * 
 * Assumptions:
 *   - The "userID" is the only text input in the login form
 *     (The HTML ID attribute of the userID field does not need to be specified) 
 *   - The "password" field is the only password input in the login form
 *     (The HTML ID attributed of the password field does not need to be specified)
 *   - The form has a "submit" button
 *   
 * Parameter Values:
 *   - processingID: ID of the div tag that contains the message to be shown during 
 *                   the authentication process
 *                   
 *   - resultID:     ID of the div tag where a message will be displayed to user 
 *   				 upon authentication failure
 *                   
 *   - remoteScript: URL of remote script file to use to authenticate user 
 *   		         credentials
 *   
 *   - idType: 		 There are two types of login IDs supported: email and username
 *   				 "email" utilizes the stored email address as the user login
 *   				 "username" utilizes a specific unique name stored for the user 
 *   				 on this website
 *   
 *   - successPage:  URL of the page to show user if the login credentials are
 *   				 verified
 *   
 */

(function($){  
	$.fn.login = function(options) {
		
		// set default parameter values
		var defaults = {
			processingID: "processing",
			resultID: "result",
			remoteScript: "users/processLogin.php",
			idType: "username",
			successPage: "a.php"
		};
		var options = $.extend(defaults, options);
				
		return this.each(function() {
			// "obj" is the login form to be submitted for authentication
			var obj = $(this);
			
			// determine the userID and password input fields
			var userIDField = "#" + obj.attr("id") + " input:text";
			var passField = "#" + obj.attr("id") + " input:password";
			
			// Initialize form by hiding the Processing and Result fields
			$('#' + options.processingID).hide();
			$('#' + options.resultID).hide();
			
			// Login form is submitted
			obj.submit(function() {
				// Reset form - remove any previous error messages and hide the Result field
				$('#' + options.resultID).html('');
				$('#' + options.resultID).removeClass("error");
				$('#' + options.resultID).hide();
				
				// Show the Processing icon
				$('#' + options.processingID).show();
								
				// Get the user entered userID and password values
				var userID = $(userIDField).val();
				var pass = $(passField).val();
				
				// Submit credentials for authentication
				$.post(options.remoteScript,
						{ username: userID, password: pass, idType: options.idType },
						function(response) {
							
							// Response Format: SuccessFlag*Data
							//   - Authentication Success:  1*RedirectURL
				     		//   - Authentication Failure: 0*ErrorMessage			
							
							piece = response.split("*");
							
							// AUTHENTICATION SUCCESS
							if(piece[0] == "1") { 
								// Hide the processing icon
								$('#' + options.processingID).hide();
								$(userIDField).val("");
								$(passField).val("");
								$('#' + options.resultID).html("Login successful");
								$('#' + options.resultID).addClass("success");
								$('#' + options.resultID).show();
								
								// Redirect user to specified page
								document.location.href = piece[1];
								//document.location.href = options.successPage;
							}
							
							// AUTHENTICATION FAILURE
							else {	
								// Hide the processing icon
								$('#' + options.processingID).hide();
								
								// Show the error message generated
								$('#' + options.resultID).html(piece[1]);
								$('#' + options.resultID).addClass("error");
								$('#' + options.resultID).show();
								
								// Clear the password field and put the cursor focus in the userID field								
								$(userIDField).focus();
								$(passField).val("");
							}			
							
						});
				return false;
			});			
		});
	};
})(jQuery); 

//var body = obj.html();

