if(typeof Zopa == 'undefined') var Zopa = {};

//Popup for MemberLogger - User Data
function memberLogger() {
    openPopupWindowCustom('/ZopaWeb/pages/logger.aspx', 'MemberLogger', 640, 300, 0, 1);
}

Zopa.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

(function($) {
    //Find where the borrowerFee span is referenced
    $(function() {
        $(".borrowerFee").each(function() {
            var feeHolder = $(this);
            var fee = getBorrowerFee(feeHolder.data("loanSize"), Zopa.borrowerFee);
            feeHolder.html(fee);
        });

        $("input[type='number']").each(function() {
            var input = $(this);
            var min = input.attr("min");
            var max = input.attr("max");
            var required = input.attr("required");
            var step = $(this).attr("step");

            min = min.length > 0 ? "min[" + min + "]," : "";
            max = max.length > 0 ? "max[" + max + "]," : "";
            required = required ? "required," : "";

            var custom = "";
            if (step === "any" || step.length === 0) {
                custom = "number";
            } else if (parseFloat(step) < 1) {
                custom = "number";
            } else {
                custom = "integer";
            }

            $(this).addClass("validate[" + required + "custom[" + custom + "]," + min + max + "]");
        });

        $("input[type='number']").keypress(function(event) {
            if (event.which === 13 || event.which === 0) {
                // Enter
                return;
            }
            if (event.which === 8 || event.which === 0) {
                // backspace
                return;
            }

            if (event.which < 48 || event.which > 57) {
                var step = $(this).attr("step");
                if (step === "any" && event.which === 46) {
                    // allow periods if the step is any
                    return;
                } else if (parseFloat(step) < 1 && event.which === 46) {
                    // allow periods if the step is a decimal
                    return;
                } else {
                    // don't allow keypresses that aren't numbers
                    event.preventDefault();
                }
            }
        });
    });

    // Calculate the borrower Fee
    function getBorrowerFee(amount, fee) {
        amount = typeof (amount) === "undefined" ? 0 : amount;
        if (fee === null) {
            var value = 'Unavailable';
            return value;
        } else {
            var calc = amount + fee;
            var value = '&pound;' + calc;
            return value;
        }
    }
})(jQuery);
