$(document).ready( function() {
	
	var inputId;
	
	// -----------------------
	
	// Income Calculator
	$('.income-calculator').bind('show', function(e, data) {
		$(this).popupLayer(data.clickEvent);
	} );
	
	$('.caculate-income').click( function(e) {
		
		$('.costs-calculator').slideUp();
		$('.income-calculator').trigger('show', { clickEvent: e } );
		
		inputId = $(this).prev().find('input').attr('id');
		
		$('#'+ inputId).attr('disabled', 'disabled');
		
	} );
	
	$('.income-calculator a.submit, .income-calculator a.cancel').click( function() {
		
		if($(this).hasClass('submit') && !isNaN($('#txtGrossYearIncomeResult').val())) {
			$('#'+ inputId).val(
				$('#txtGrossYearIncomeResult').val()
			);
		}
		
		$('.income-calculator').slideUp().clearForm();
		
		$('#'+ inputId).attr('disabled', '').trigger('keydown');
		
	} );
	
	// -----------------------
	
	// Costs Calculator
	$('.costs-calculator').bind('show', function(e, data) {
		$(this).popupLayer(data.clickEvent);
	} );
	
	$('.caculate-costs').click( function(e) {
		
		$('.income-calculator').slideUp();
		$('.costs-calculator').trigger('show', { clickEvent: e } );
		
		inputId = $(this).prev().find('input').attr('id');
		
		$('#'+ inputId).attr('disabled', 'disabled');
		
	} );
	
	$('.costs-calculator a.submit, .costs-calculator a.cancel').click( function() {
		
		if($(this).hasClass('submit') && !isNaN($('#txtMonthlyObligationsResult').val())) {
			$('#'+ inputId).val(
				$('#txtMonthlyObligationsResult').val()
			);
		}
		
		$('.costs-calculator').slideUp().clearForm();
		
		$('#'+ inputId).attr('disabled', '').trigger('keydown');
		
	} );
	
	// -----------------------
	
	// disable NaN input
	$('.income-calculator input[type=text], .costs-calculator input[type=text]').bind('keydown keyup', function() {
		if(isNaN($(this).val()) || $(this).val().indexOf('.') !== -1 || $(this).val().indexOf(',') !== -1) 
			$(this).val($(this).val().substr(0, $(this).val().length - 1));
	} );
	
} );


$(document).ready( function() {
	
	// Calculate income
	$("#calculateIt").click(function(){
		
		var m, f1, f2, f3, f4, P;
		
		m = parseFloat($("#txtGrossMonthlySalary").val());
		m = (isNaN(m)) ? 0 : m;
		
		f1 = m * 12;
		
		if ($("#rbHolidayPay-y:checked").val()) {
			f2 = Math.ceil(f1 * 0.08);
		}
		else {
			f2 = 0;
		}	

		if ($("#rbMonth13-y:checked").val()) {
			f3 = m;
		}
		else {
			f3 = 0;
		}	
		
		if ($("#rbOtherIncome-y:checked").val()) {
			f4 = parseFloat($("#txtOtherIncomeAmount").val()) * 12;
			f4 = (isNaN(f4)) ? 0 : f4;
		}
		else {
			f4 = 0;
		}
		
		
		P = (f1+f2+f3+f4);
		
		
		if(!isNaN(P))
		{
			$("#txtGrossYearIncomeResult").val(P);
		}
		else
		{
			$("#txtGrossYearIncomeResult").val('Error');
		}
		
		return false;
		
	});

} );


$(document).ready( function() {
	
	// Calculate costs
	$("#calculateIt2").click(function(){
	
		var mo1, mo2, mo3, Pm;

		mo1 = parseFloat($("#txtInterestRelays").val());
		mo1 = (isNaN(mo1)) ? 0 : mo1;
		mo2 = parseFloat($("#txtAlimentation").val());
		mo2 = (isNaN(mo2)) ? 0 : mo2;
		mo3 = parseFloat($("#txtLongLease").val());
		mo3 = (isNaN(mo3)) ? 0 : mo3;
		
		Pm = mo1 + mo2 + mo3;
	
	
		if(!isNaN(Pm))
		{
			$("#txtMonthlyObligationsResult").val(Pm);
		}
		else
		{
			$("#txtMonthlyObligationsResult").val('Error');
		}

		return false;
	
	});

} );