function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

$(document).ready(function() {

	$("#amountother").numeric();

	$("#errorholder").hide();

	$("#amountother").focus(function() {
		$("#amountx").attr('checked', true);
	});


	$("#amountx").click(function() {
		$("#amountother").focus();
	});


	$("#donate").submit(function() {

		// Define variables
		var $amountchecked = false;
		var $amount = "";
		var $amountother = parseFloat($("#amountother").val()) || 0;
		var $mindonation = parseFloat($("#mindonation").val());
		var $haserror = false;
		var $errormessage;

		$("input[@name='amount']").each (
			function()
			{
				if ($("input[@name='amount']:checked").val()) {
					$amountchecked = true;
				}
			}
		);

		/*
		 * Check to see if company name exists, if so validate corperate fields
		 */
		if($('#company_name').length>0)
		{
			if(
				$('#name').val().length==0 ||
				$('#company_name').val().length==0 ||
				$('#email').val().length==0 ||
				$('#telephone').val().length==0
			)
			{
				$haserror = true;
				$errormessage = "Please complete all fields";
			}
				

		}

		if($amountchecked)
		{
			 $amount = $("input[@name='amount']:checked").val();
		}


		// Validate from
		if($amountchecked == false)
		{
			$haserror = true;
			$errormessage = "Please select an amount to donate";
		}

		if($amount == "other")
		{
			if($amountother == 0 || $amountother == '')
			{
				$haserror = true;
				$errormessage = "Please enter the amount you would like to donate";
			}
			if($amountother < $mindonation)
			{
				$haserror = true;
				$("#amountother").addClass("error");
				$errormessage = "The minimum donation is &pound;" + number_format($mindonation,2) + ". Please enter a higher amount.";
			}
		}



		// If we have an error alter the message and return false
		if($haserror)
		{
			$("#errorholder").html($errormessage);
			$("#errorholder").show();
			return false;
		}
		else
		{
			$('#donate_submit_button').attr("disabled", "disabled");
			return true;
		}

	});
	
	$(window).unload(
			function()
			{
				
				//Renable the submit button when leaving the page.
				$('#donate_submit_button').attr("disabled", "");
			}
		);
});
