module.exports = {
	events:
	{
		"click #contact-submit": "validateAndSend",
		"click .hideCover": "hc",
		"keydown": "blockKeyDownArrow",
		"input input, textarea": "validateMe",
		"focus input, textarea": "becameActive",
		"onactivate input, textarea": "becameActive",
		"focusout input, textarea": "notActive",
	},
	blockKeyDownArrow: function(e)
	{
		if(e.keyCode == 39 || e.keyCode == 37)
		{
			e.stopPropagation();
		}
	},
	validateAndSend: function(e)
	{
		$('#formStatus').addClass('show');
		e.preventDefault();
		out = {}
		var inputs = $('input, textarea', this.el);
		var dontSend = false;
		for(var ip=0;ip<inputs.length;ip++)
		{
			out[inputs[ip].id] = inputs[ip].value || inputs[ip].textContent
			if(out[inputs[ip].id] == "" && inputs[ip].id != "contact-submit")
			{
				$(inputs[ip]).parent().addClass('invalid');
				//$('#formStatus').show().text('You have to fill out all fields')
				dontSend = true;
			}
		}
		if(!dontSend)
		{
			this.lastOut = out;
			var that = this;
			this.msgPending();
			$.ajax({
			  timeout: 10000,
			  type: "POST",
			  url: '/contact',
			  data: out,
			  success: function(data)
			  {
			  	$('#formStatus').show().text(data.data.formStatus.message);
			  	that.msgSent()
			  },
			  error: function(xhr, textStatus, errorThrown)
			  {
			  	that.msgSendFailed(textStatus, errorThrown)
			  },
			  dataType: "json"
			});
		}
	},
	hc: function()
	{
		if(this.$el.hasClass('success'))
		{
			$('input,textarea').val("");
			$('div.input-container').removeClass('active').removeClass('valid');	
		}
		this.$el.removeClass('fail pending success');
		$('#formStatus').hide();
	},
	msgPending: function()
	{
		$('#formStatus').show().text("Sending message");
		this.$el.addClass('pending');
		$(".formCover .name").text(this.lastOut.name)
		$(".formCover .phemail").text(this.lastOut.respondTo)
		$(".formCover .message").text(this.lastOut.message)
	},
	msgSent: function()
	{
		var inputs = $('input:not([type=submit]), textarea', this.el);
		inputs.val("");
		this.$el.removeClass('fail pending success').addClass('success');
	},
	msgSendFailed: function(e1, e2)
	{
		if(!e1 && !e2)
		{
			$('#formStatus').show().text("Message send failed for unknown reason. Try again or contact us at hello@home-technology.com");
		}
		else if(e1)
		{
			$('#formStatus').show().text("Message send failed (Error: " + e1 + "). Try again or contact us at hello@home-technology.com");
		}
		else
		{
			$('#formStatus').show().text("Message send failed (Server Error: " + e2 + "). Try again or contact us at hello@home-technology.com");
		}
		this.$el.removeClass('fail pending success').addClass('fail');
	},
	validateMe: function(e)
	{
		var targetVal = $(e.target).val();
		var $containerDiv = $(e.target).parent()
		if(targetVal != "") {
			$containerDiv.removeClass('invalid').addClass('valid');
		}else{
			$containerDiv.removeClass('valid invalid')	.addClass('empty');
		}
	},
	becameActive: function(e)
	{
		$('div.input-container').removeClass('active');
		$(e.target).parent().addClass('active');
	},
	notActive: function(e)
	{
		$(e.target).parent().removeClass('active');
	}
}