

// Application

$('#total_income, #total_trading_income').blur(function() {
	var a = $('#total_income').val().replace(/,/g, "");
	//alert(a);
	var b = $('#total_trading_income').val().replace(/,/g, "");
	var c = Math.round((b / a) * 100);
	if (!isNaN(c)) {
		$('#prop').text(c + '%');
		$('#income_proportion').attr('value', c);
	}
});

$('.login #username, .login #password').focus(function() {
	$(this).attr('value', '');
});

$('.confirm').click(function(event) {
	event.preventDefault();
	var success = confirm('Are you sure you wish to ' + $(this).text() + ' this application?');
	if (success) {
		window.location.href = $(this).attr('href');
	} else {
		return false;
	}
});

function requestAssistance() {
	$('#assist').val(1);
	$('#process').val(1);
	document.application.submit();
}

$('.hidden').hide();
if (!$('.hidden').is(':visible')) {
	$('#admin-hidden-toggle').html('Show Menu');
}

$('#admin-hidden-toggle').click(function() {
	if ($('.hidden').is(':visible')) {
		$('.hidden').hide();
		$(this).html('Show Menu');
	} else {
		$('.hidden').show();
		$(this).html('Hide Menu');
	}
});

$('.open-tip').click(function() {
	$(this).parent().next().slideToggle();
});

$('.tip').click(function() {
	$(this).slideUp();
});

$('#subtotal, #discount_amount, #vat_amount').blur(function() {
	var subtotal = parseFloat( $('#subtotal').val().replace(/,/g, "") );
	var discount_amount = parseFloat( $('#discount_amount').val().replace(/,/g, "") );
	var vat_amount = parseFloat( $('#vat_amount').val().replace(/,/g, "") );
	var total = (( subtotal - discount_amount ) + vat_amount).toFixed(2);
	if (!isNaN(total)) {
		$('#total-amount').text(total);
		$('#total').attr('value', total);
	}
});

$('.app-submit').click(function() {
	$(this).val('Processing...');
	$(this).addClass('app-submitted');
	setTimeout(function(){ $('#app-loading').show(); }, 1000);
});

$('.scroll-save').click(function(e) {
	e.preventDefault();
	$(window).scrollTop( $('.app-submit').offset().top )
});

var offset = $(".stickybox").offset();
$(window).scroll(function() {
   if ($(window).scrollTop() > offset.top) {
	   $(".stickybox").stop().animate({
		   marginTop: $(window).scrollTop() - offset.top 
	   });
   } else {
	   $(".stickybox").stop().animate({
		   marginTop: 0
	   });
   };
});

