
var subscriptions = ({
	initialize: function(){
		$("#loading").css({
			opacity: 0
		});		
		
	    var options = { 
	        beforeSubmit:  subscriptions.showRequest,  // pre-submit callback 
	        success:       subscriptions.showResponse  // post-submit callback 

	        // other available options: 
	        //target:    element	 // target element(s) to be updated with server response 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 

	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    };
			
		$("#subscription-form").ajaxForm(options);

		$("#signup-submit").click(function(ev){
			ev.preventDefault();
			
			var email = $("#email-address");
			var emailConf = $("#email-address-confirm");
			
			var dm = $("#driving-matters");
			var tt = $("#track-talk");
			
			var error = $("#error");
			error.css('opacity',0);
			var msg = "";
						
			if(email.attr("value") != emailConf.attr("value")){
			    
			    msg = "<strong>Oops!</strong> Please make sure the email addresses you entered match.";
		    }

			if(email.attr("value") == emailConf.attr("value")){

				var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
				if(email.attr("value").search(isEmail_re) == -1){
					msg = "<strong>Oops!</strong> That doesn't look like a valid email address - can you check it for any typos?"; 
				}
			
			}
			
			if(email.attr("value").length == 0 && emailConf.attr("value").length == 0){
			    msg = "<strong>Oops!</strong> We can't send an email to nobody! Please enter your email address.";
			}
			
			if(dm.attr("checked") == false && tt.attr("checked") == false){

			    msg = "<strong>Oops!</strong> Please select at least one newsletter to subscribe to.";
				
			}
		
		    if(msg.length > 0){
			
		    	$('#error span').html(msg);
				error.animate({
					opacity: 1
				});
				msg = "";
				return false;

			}else{
				
				$("#subscription-form").submit();
				
			}
			
			
		});
		
	},
	showRequest: function(formData, jqForm, options){
		var queryString = $.param(formData);
		$("#loading").animate({
			opacity: 1
		});		
		
	},
	showResponse: function(responseText, statusText){
		$("#loading").animate({
			opacity: 0
		});		
		var dmCopy = $("#dm-copy");
		var ttCopy = $("#tt-copy");
		var both = $("#dm-copy, #tt-copy");
		var msg = "<p>Thank you for selecting to receive e-newsletters from Valvoline. You can update your subscriptions at any time by visiting <a href=\"http://newsletters.valvoline.com\" class=\"ext\">My Subscriptions</a>.</p>"
		var newTexts = ["<h3>Thank You for signing up for Driving Matters</h3>" + msg, "<h3>Thank You for signing up for Track Talk</h3>" + msg];

		var responseActions = responseText.replace(/^\s+|\s+$/g,"").split(":");
		var dmOptIn = responseActions[0] == "dm1" ? true : false;
		var ttOptIn = responseActions[1] == "tt1" ? true : false;
		var invalidEmail = responseActions[0] == "invalidEmail" ? true : false;
		
		var optIns = [dmOptIn,ttOptIn]
		
		if(invalidEmail){
			var msg = "<h2>You've already signed up</h2><p>We're sorry, our records show the email address you are entering is already signed up for Valvoline Newsletters.  If you are not receiving emails from Valvoline, <a href=\"our-business/contact-us\">click here</a> for Valvoline assistance. If you need to update your email address, simply manage your account by going to <a href\"http://newsletters.valvoline.com\" class=\"ext\">My Subscriptions</a>.</p>"
		}
		
		both.each(function(index){
			if(optIns[index]){
				$(this).animate({
					opacity: 0
				}).html(newTexts[index]).animate({
					opacity: 1
				});
			}
		});
		
		$("#dm-copy .ext, #tt-copy .ext").each(function(){
			$(this).attr("target","_blank");
		});
		
	}
});
