var form_options = {
    beforeSubmit: preRequest,
    success: postRequest,
    clearForm: true
};

var DoneIt = false;

$(document).ready(function() {
// Set up validation and AJAX

    $('.leftbox').corner();
    $('.rightbox').corner();
    $('.mainbox').corner();

    $('#waiver_dlg').hide();
    $('#postpay').hide();
// Add a custom date expiry validator
	$.validator.addMethod("expirydate", function( value, element ) {
		var result = this.optional(element) || value.length == 7 && /^[0-9]{2,2}\/[0-9]{4,4}$/.test(value);
		if (!result) {
			var validator = this;
		}
		return result;
	}, "Invalid expiry date");

	$.validator.addMethod("validdate", function( value, element ) {
		var result = this.optional(element) || value.length >= 8 && /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4,4}$/.test(value);
		if (!result) {
			var validator = this;
		}
		return result;
	}, "Invalid date");

    $('#races_select').change(function() {
        var TotalPayment = 0;
        var NumberSelected = 0;
        var RaceList = "";
        var RaceIDList = "";
        var format_price = "";
        $('#races_select option:selected').each(function() {
            var fee = parseInt($(this).val().split("-")[1]);
            RaceIDList += $(this).val().split("-")[0]+"|";
            format_price = FormatDecString(fee);
            var race_name = $(this).text().split('(')[0];
            RaceList += "<tr><td>"+race_name+"</td><td>"+format_price+"</td></tr>";
            TotalPayment += fee; 
            NumberSelected++;
        });
        RaceList = "<table><tr><td align='center'><b>Race</b></td><td align='center'><b>Fee (euro)</b></td></tr>"+RaceList+"<tr><td><b>Total Fee</b></td><td><b>"+FormatDecString(TotalPayment)+"</b></td></tr></table>";
        $('#racespicked').html(RaceList);
        $('#no_selected').text(NumberSelected);
        $('#event_ids').val(RaceIDList);
        SetPaymentAmount(TotalPayment);
    });

    $('#odlform').submit(function() {
        if (!$('#odlform').valid()) {
            return(false);
        }
        if (DoneIt) {
            return(false);
        }
        $(this).ajaxSubmit(form_options);
        return false;
    });

    $('#odlform').validate({
        errorPlacement: function(error, element) {
            error.insertAfter(element);
        }
    });

    $('#races_select').val("");
    SetPaymentAmount(0);
// Set focus
    $('#full_name').focus();
});

function SetPaymentAmount(total_to_pay) {
    var formatted = FormatDecString(total_to_pay);
    $('#total_fee').val(formatted);
    $('#total_payment').val(total_to_pay);
}

function FormatDecString(in_string) {
    var formatted = (in_string/100).toString();
    if (formatted.indexOf(".") == -1) {
        formatted += ".00";
    }
    else {
        formatted += "00".substr(0, 2 - formatted.split(".")[1].length);
    }
    return(formatted);
}

function preRequest(formData, jqForm, update_form_options) {
    var selected = $('#races_select option:selected').text();
    if (!$('#odlform').valid()) {
        return(false);
    }
    if (selected.length == 0) {
        $.blockUI({message: '<p>You have not selected any races. Please select at least one race to proceed<p><input type="button" value="OK" onClick="$.unblockUI();$(\'#races_select\').focus();"><p>'});
        return(false);
    }
    else {
        $.blockUI({message: '<h3><img src="/Images/busy.gif"/> Please wait. Authorising...</h3>'});
        DoneIt = true;
        return(true);
    }
}

function ShowWaiver() {
        $('#waiver_dlg').show();
        $('#waiver_dlg').dialog({modal:true, title:"Waiver", width:"600px", height:"350px", position:"top"});
}

function postRequest(responseText, statusText) {
    if (responseText.substr(0, 7) == "SUCCESS") {
        $('#MakePayment').attr("disabled", "disabled");
        $('#postpay').show();
    }
//        $('#payment_submit').hide();
//    $.blockUI({message: responseText+'<p><input type="button" value="OK" onClick="$.unblockUI();"><p>'});
    $.blockUI({message: responseText+'<p><input type="button" value="OK" onClick="$.unblockUI();"><p>'});
}

