var onething = {
	init: function() {
		$('form-body').onkeyup = onething.text_counter;
		$('form-body').onkeydown = onething.text_counter;
	},
	
	post: function() {
		var valid = new Validation('form-post', {onSubmit:false});	
		var result = valid.validate();
		if (result == true) {
			new Ajax.Request('/inc/remote_func.php?action=savePost&' + Form.serialize('form-post'), {asynchronous:true, onSuccess:onething.postSuccess});
			onething.sendingData();
		}
	},
	
	sendingData: function() {
		$('p-form-submit').style.display = 'none';
		$('p-sending').style.display = 'block';
	},
	
	postSuccess: function(t) {
		var msg = '';
	
		if (t.responseText == -2) msg += "Each tag must be at least 3 characters in length.<br />";
		if (t.responseText == -1) msg += "Please complete all fields.<br />";
		if (t.responseText == 0) msg += "Sorry, we experienced a database error.<br />";
		if (t.responseText == 1) { 
			msg += 'Your post was saved. Thank you!<br /><a href="/onethingaccountability.php">Need some accountability for your "One Thing"? Click here.</a>';
			$('form-subject').value='';
			$('form-body').value='';
			$('form-tags').value='';
			setTimeout('window.location = "onethingaccountability.php"', 4500);
		}
		$('p-sending').style.display = 'none';
		$('p-form-submit').style.display = 'block';
		var span = $('msg-return');
		span.innerHTML = msg;
		span.style.display = 'block';
		new Effect.Fade(span, {from:0, to:1, duration: 1});
	},
	
	text_counter: function() {
		var maxlimit = 750;
		var charcount = $F('form-body').length;
		// if too long...trim it!
		if (charcount > maxlimit) {
			$('form-body').value = $('form-body').value.substring(0, maxlimit);
			//$('textarea-body-remlen').style.color = 'black';
			new Effect.Highlight($('form-body-remlen'), {startcolor:'#66090B', endcolor:'#000000', duration: 2});
		}
		// otherwise, update 'characters left' counter
		else {
			var charleft = maxlimit - charcount;
			$('form-body-remlen').style.color = '#FFFFFF';
			$('form-body-remlen').style.background = 'none';
			replaceText($('form-body-remlen'), charleft + " characters left");
		}
	}
}

var accountability = {
	saveForm: function() {
		var valid = new Validation('form-accountability', {onSubmit:false});
		var result = valid.validate();
		if (result == true) {
			new Ajax.Request('/inc/remote_func.php?action=saveEmail&' + Form.serialize('form-accountability'), {asynchronous:true, onSuccess:accountability.formSuccess});
			accountability.sendingData();
		}
	},
	
	sendingData: function() {
		$('p-form-submit').style.display = 'none';
		$('p-sending').style.display = 'block';
	},
	
	formSuccess: function(t) {
		var msg = '';
	
		if (t.responseText == -1) msg += "Please complete the email field with one valid email address.<br />"; 
		if (t.responseText == 0) msg += "Sorry, we experienced a database error.<br />";
		if (t.responseText == -2) msg += "Sorry, we experienced an error sending out the confirmation email.<br />";
		if (t.responseText == 1) { 
			msg += 'Your email was saved and a confirmation sent to your email address, please check your email to confirm and we will drop you an email 30 days from now. Thank you!';
			$('form-email').value = '';
		}
		if (t.responseText == 2) {
			msg += 'It looks as though you have entered your email already, but we went ahead and sent you a confirmation email.  Please check your email and confirm and we will drop you an email 30 days from now.  Thank you!';
			$('form-email').value = '';
		}
		$('p-sending').style.display = 'none';
		$('p-form-submit').style.display = 'block';
		var span = $('msg-return');
		span.innerHTML = msg;
		span.style.display = 'block';
		new Effect.Fade(span, {from:0, to:1, duration: 1});
	}
}