"use strict";

(function () {    
	var confirm, response, payForm, subForm,
	prefix = window.location.href.match('192.168') ? '/~scott/NEWNOLES' : '',
	
	BAD_EMAIL = 'Please provide a valid email address!',
	SUB_SUCCESS = 'You are now subscribed to the mailing list!',
	SUB_ALREADY = 'You have already subscribed to the mailing list!';
	
	function hideTimeout(node) {
		window.setTimeout(function () {
			node.html(''); 
		}, 3000);
	}
	
	function registerPrePay() {   
	    $.ajax({
			type	: 'post',
			url		: prefix + '/subscribers/prepay',
			data	: payForm.serialize(),
			success : function (data) {   
				if (parseInt(data, 10) === 0) {       
					confirm.html('All Fields are Required!').show();
					
					hideTimeout(confirm);
				} else {                                                   
					confirm.html(data).show(); 
				}
			}
		});  
		return false;
	}
	
	function registerEmail() {
        $.ajax({
			type	: 'post',
			url		: prefix + '/subscribers/email',
			data	: subForm.serialize(),
			success : function (data) {  
				switch (data) {
				case '2':
					response.html(BAD_EMAIL).show();	
					break;
				case '1':
					response.html(SUB_SUCCESS).show();
					break;					
				case '0':
					response.html(SUB_ALREADY).show(); 
					break;								
				}
			},
			complete: function () {
			    hideTimeout(response); 
			}
		});	 
		return false;     
	} 
	
	$(document).ready(function () {
		confirm = $('#confirm'); 
		response = $('#response'); 
		payForm = $('#member_form');
		subForm = $('#SubscriberAddForm');
		
		payForm.submit(registerPrePay); 
		subForm.submit(registerEmail);
	});
}());