$(function() {
	$("#accordion").accordion({ autoHeight: false });
	
	// dialog
	$('#dialog').dialog({ width: 670,
						autoOpen: false,
		 				modal: true })
	
	// reminder popup - set reminder via ajax
	check_register();

	function check_register () {
		$("button[id^='property_btn_']").click(function () {
			var id = $(this).attr("id").substring(13);
			// set id in form
			$("input#property_id").val(id);
			
			$.ajax({
	            type: "POST",
				url: "/register/check_register/"+id,
	            success: function(textStatus)
	            {
					if (textStatus.length > 0) {
						
						
						url = '/a_assets/pdfs/properties/'+textStatus;
						location.href = url;
					} else {
						$("#dialog").dialog('open');
					};

				}
			})
		})

	}
	
	if ($("input#error").val() == 'true') {
		$("#dialog").dialog('open');
	};
	
	$('#register_btn').click(function() {
		$("#dialog").dialog('close');
	})
	
		
		
	// calculator
	$(".calc_radio").click(function() {
		show_hide_options();
	})
	
	show_hide_options()
	function show_hide_options () {
		if ($("#yield").is(":checked")) {
			$("#yield_inputs").show();
                        $("div.calc_title_1").html("Price (&pound;)");
                        $("div.calc_title_2").html("Rent (&pound;)");
                        $("div.calc_title_3").html("Costs (%)");
		} else {
			$("#yield_inputs").hide();
		};

		if ($("#price").is(":checked")) {
			$("#price_inputs").show();
                        $("div.calc_title_1").html("Yield (%)");
                        $("div.calc_title_2").html("Rent (&pound;)");
                        $("div.calc_title_3").html("Costs (%)");
		} else {
			$("#price_inputs").hide();
		};

		if ($("#rent").is(":checked")) {
			$("#rent_inputs").show();
                        $("div.calc_title_1").html("Price (&pound;)");
                        $("div.calc_title_2").html("Yield (%)");
                        $("div.calc_title_3").html("Costs (%)");
		} else {
			$("#rent_inputs").hide();
		};
	}
	
	
	calculate()
	function calculate() {
		reformat_nums()
		$("#calc_button").click(function() {
			var ammount;
			
			if ($("#yield").is(":checked")) {
				
				var yield_price = eeparseFloatTh($("#yield_price").val());
				var yield_costs = eeparseFloatTh($("#yield_costs").val());
				var yield_rent = eeparseFloatTh($("#yield_rent").val());

				if (isNaN(yield_price) || isNaN(yield_costs) || isNaN(yield_rent)) {
					alert("Please enter numbers in the boxes")
				} else {
					ammount = (yield_rent/(yield_price*(1+(yield_costs/100))))*100
					ammount = "Yield: "+ammount.toFixed(2)+"%"
				}
			}
			
			if ($("#price").is(":checked")) {
				var price_rent = eeparseFloatTh($("#price_rent").val());
				var price_yield = eeparseFloatTh($("#price_yield").val());
				var price_costs = eeparseFloatTh($("#price_costs").val());

				if (isNaN(price_rent) || isNaN(price_yield) || isNaN(price_costs)) {
					alert("Please enter numbers in the boxes")
				} else {
					ammount = (price_rent/(price_yield/100))/(1+(price_costs/100))
					ammount = "Price: £"+eedisplayFloatNDTh(eeparseFloatTh(ammount.toFixed(2)),0)
				}
				
			}
			
			if ($("#rent").is(":checked")) {
				var rent_price = eeparseFloatTh($("#rent_price").val());
				var rent_costs = eeparseFloatTh($("#rent_costs").val());
				var rent_yield = eeparseFloatTh($("#rent_yield").val());

				if (isNaN(rent_price) || isNaN(rent_costs) || isNaN(rent_yield)) {
					alert("Please enter numbers in the boxes")
				} else {
					ammount = (rent_price*(1+(rent_costs/100)))*(rent_yield/100)
					ammount = "Rent: £"+eedisplayFloatNDTh(eeparseFloatTh(ammount.toFixed(2)),0)
				}
			}
			
			$("#calc_output").html(ammount);
		})
	}
	
	
function reformat_nums() {
    //if ($("#yield").is(":checked")) {
        // reformat numbers
        $("#yield_price").blur(function() {
            $("#yield_price").val(eedisplayFloatNDTh(eeparseFloatTh($("#yield_price").val()), 0))
        })

        $("#yield_costs").blur(function() {
            $("#yield_costs").val(eedisplayFloatNDTh(eeparseFloatTh($("#yield_costs").val()), 2))
        })

        $("#yield_rent").blur(function() {
            $("#yield_rent").val(eedisplayFloatNDTh(eeparseFloatTh($("#yield_rent").val()), 0))
        })
    //} else if ($("#price").is(":checked")) {
        // reformat numbers
        $("#price_rent").blur(function() {
            $("#price_rent").val(eedisplayFloatNDTh(eeparseFloatTh($("#price_rent").val()), 0))
        })

        $("#price_yield").blur(function() {
            $("#price_yield").val(eedisplayFloatNDTh(eeparseFloatTh($("#price_yield").val()), 2))
        })

        $("#price_costs").blur(function() {
            $("#price_costs").val(eedisplayFloatNDTh(eeparseFloatTh($("#price_costs").val()), 2))
        })
    //} else if ($("#rent").is(":checked")) {
        // reformat numbers
        $("#rent_price").blur(function() {
            $("#rent_price").val(eedisplayFloatNDTh(eeparseFloatTh($("#rent_price").val()), 0))
        })

        $("#rent_costs").blur(function() {
            $("#rent_costs").val(eedisplayFloatNDTh(eeparseFloatTh($("#rent_costs").val()), 2))
        })

        $("#rent_yield").blur(function() {
            $("#rent_yield").val(eedisplayFloatNDTh(eeparseFloatTh($("#rent_yield").val()), 2))
        })
    //}
	}
});


