/* Blank-Label Ajax Subscribe 1.0
 * 
 * Dependencies:
 * jQuery Core 1.4 [http://jquery.com]
 */

(function($){
	
	$.fn.ajaxify = function(options) {
		var defaults = {
			successOK: function(){
				emailBox.css({"background-color":"lightgreen", "color":"white"});
				emailBox.val("thank you!");
			},
			successDup: function(){
				emailBox.css({"background-color":"red", "color":"white"});
				emailBox.val("already subscribed!");
			},
			valid: function(){
				emailBox.css({"background-color":"white", "color":originalColor});
			},
			invalid: function(){
				emailBox.css({"background-color":"red", "color":"white"});
				emailBox.val("invalid email");
			},
			error: function(){
				emailBox.css({"background-color":"red", "color":"white"});
				emailBox.val("error. try again");
			},
			processing: function(){
				emailBox.css({"background-color":"yellow", "color":"gray"});
				emailBox.val("processing...");
			},
			autoFocus: false
		}
		var options = $.extend(defaults, options);
		
		// caches email input box selector
		var emailBox = $("#" + $(this).attr("id") + " input[name='email']");
		var originalColor = emailBox.css("color");
		
		return this.each(function(){
			//initialize email input box with text
			emailBox.val("email");
			
			//if autoFocus is set to true, focus on email box on page load
			if (options.autoFocus) {
				emailBox.focus();
			}
			
			//bind events to email input box
			emailBox.bind("keyup change", function(){
				if (isBadEmail(emailBox) == false) {
					options.valid();
				}
			})
			emailBox.focus(function(){
				if ($(this).val() == "email") {
					$(this).val("");
				}
			})
			emailBox.blur(function(){
				if ($(this).val() == "") {
					$(this).val("email");
				}
			});
			
			//bind form submit event
			$(this).submit(function(){
				//callbacks contains four functions: invalid(), successOk(), successDup(), and error()
				var email = emailBox.val();
								
				if (isBadEmail(emailBox)) {
					options.invalid();
				} else {
					var dataString = "email=" + email;
					$.ajax({
						type: "GET",
						url: "subscribe.aspx",
						data: dataString,
						success: function(data) {
							switch (data.toLowerCase()) {
								case "ok": options.successOK();
								break;
								case "dbl": options.successDup();
								break;
								case "err": options.error();
							}
						},
						error: function(){
							options.error();
						}
					});
					options.processing();
				}
				return false;
			});
		});
	}
	
	function isBadEmail(emailInputBox) {
		//get inputed email address
		var email = emailInputBox.val().toLowerCase();
		
		//if email does not contain the @ symbol or starts with a dot, email is invalid
		if (email.indexOf("@") == -1 || email.charAt(0) == ".") {
			return true;
		} else {
			
			//otherwise, split string by @
			var emailHalves = email.split("@");
			
			//if we have more than two parts (meaning there is more than one @ symbol), email is invalid
			if (emailHalves.length > 2) {
				return true;
			} else {
				
				//otherwise, create empty to string to hold our validated email
				var validEmail = "";
				var allowed = "+-.0123456789abcdefghijklmnopqrstuvwxyz_";
				//loop through each half and each character in the email and compare to each character in the allowed character string. add to validEmail only if they are equal. if equal, break since there's no point in comparing the rest
				var dotCounter = 0;
				for (k = 0; k <= 1; k++) {
					for (i = 0; i < emailHalves[k].length; i++) {
						for (j = 0; j <= allowed.length; j++) {
							if (emailHalves[k].charAt(i) == allowed.charAt(j)) {
								//emails with two dots next to each other are invalid
								if (allowed.charAt(j) == ".") {
									if (dotCounter == 1) {
										return true;
									} else {
										dotCounter++;
									}
								} else {
									dotCounter = 0;
								}
								validEmail += emailHalves[k].charAt(i);
								break;
							}
						}
					}
					//add @ symbol back to new email string
					if (k == 0) {
						validEmail += "@";
					}
				}
				
				//if email does not equal validEmail, email is invalid
				if (email == validEmail) {
					return false;
				} else {
					return true;
				}
			}
		}
	}
})(jQuery);