﻿$(document).ready(function() {

    $("#firstName").validation({ vg: "acc", vt: ["req"], error: ["Please enter your firstname"] });
    $("#lastName").validation({ vg: "acc", vt: ["req"], error: ["Please enter your last name"] });
    $("#email").validation({ vg: "acc", vt: ["req", "emailaddress"], error: ["Please enter your email address", "Your email address seems to be incorrect"] });
    $("#btnDonate").validation({ vg: "acc" });
    $("#btnDonate").attr("OnValidated", "DonateNow()");

    $("input[type=radio]").click(function() {
        if ($(this).attr("name") != "period") {
            Unselect();
            this.checked = true;
        }
    });

    $("#singleOther").mousedown(function() {
    if (isNaN($("#singleOther").val())) {
            $("#singleOther").val("");
            $("#singleOther").css("font-size", "16px")
        }
        Unselect();
        $("#radioSingle").attr("checked", true);
    });

    $("#regularOther").mousedown(function() {
        if (isNaN($("#regularOther").val())) {
            $("#regularOther").val("");
            $("#regularOther").css("font-size", "16px")
        }
        Unselect();
        $("#radioRegular").attr("checked", true);
    });
});

function Unselect() {
    $("input[name!=period]").each(function(index, obj) {
        if (obj.checked) obj.checked = false;
        $("#singleOther").val("");
        $("#regularOther").val("");
        
    });
}


function DisplayErrors($invalids) {

    HideErrors();
    var errors = "<p>Please correct the following items.</p><ul>";
    var errorlength = errors.length + 5;

    $.each($invalids, function(i, invalid) {
        errors += "<li>" + $(invalid).attr("error") + "</li>";
        $(invalid).addClass("error");
    });

    errors += "</ul>";

    if (errors.length > errorlength) {
        $(".validation-summary").append(errors);
        $(".validation-summary").css("display", "block");
    }
}

function ShowDetails() {

    var found = false;
    var other = false;
    var box = null;
    $("input[name!=period]").each(function(index, obj) {
        if (obj.checked) {
            found = true;
            if ($(obj).val() == "0") {
                // it's one of those others;
                other = true;
                if ($(obj).attr("name") == "single") {
                    if ( !isCurrency($("#singleOther").val()) ) {
                        found = false;
                        box = $("#singleOther"); 
                        }
                }
                else {
                    if ( !isCurrency($("#regularOther").val()) ) {
                        found = false;
                        box = $("#regularOther");
                    }
                }
            }
        }
    });

    if (found) {
        $("#donation").css("display", "none");
        $("#details").css("display", "block");
    }
    else {
        if (other) {
            if (!isCurrency($(box).val())) {
                $(box).css("font-size", "11px")
                $(box).val("Please enter a donation amount in pounds & pence, eg 5.25");
                
            }
        }
        else {
            // do nothing at the mo
        }
    }

}


// -----------------
// check to see input number is a proper currency, eg ##.##
function isCurrency(value) {
    var rxCurrency = new RegExp(/^[0-9]*\.[0-9]{2}$/);
    if (value.match(rxCurrency)) {
        return true;
    } else {
    return false;
    }
}

function GetValue() {
    var val;
    $("input[type=radio]").each(function(index, obj) {
        if (obj.checked && ($(obj).attr("name") != "period")) {
            if ($(obj).val() == "0") {
                // it's one of those others;
                if ($(obj).attr("name") == "single")
                    val = $("#singleOther").val();
                else
                    val = $("#regularOther").val();
            }
            else
                val = $(obj).val();
        }
    });    
    return val;
}
function GetType() {
    var val;
    $("input[type=radio]").each(function(index, obj) {        
        if (obj.checked && ($(obj).attr("name") != "period"))
            val = $(obj).attr("name");
    });
    return val;
}

function GetPeriod() {
    var val = "1";
//    $("#donation input[name=period]").each(function(index, obj) {
//        if (obj.checked)
//            val = ""$(obj).val();
//    });
    return val;
}

function ShowSingle() {
    $("#single").css("display", "block");
    $("#regular").css("display", "none");

    $("#tabSingle").addClass("donate-tab");
    $("#tabSingle").removeClass("donate-tab-off");

    $("#tabRegular").addClass("donate-tab-off");
    $("#tabRegular").removeClass("donate-tab");

}
function ShowRegular() {
    $("#single").css("display", "none");
    $("#regular").css("display", "block");

    $("#tabRegular").removeClass("donate-tab-off");
    $("#tabRegular").addClass("donate-tab");

    $("#tabSingle").removeClass("donate-tab");
    $("#tabSingle").addClass("donate-tab-off");
}


function DonateNow() {
    // send details to the handler and retrieve the form to post
    var url = "/common/handlers/postform.ashx";
    $.post(url, { firstname: $("#firstName").val(),
        lastname: $("#lastName").val(),
        email: $("#email").val(),
        telephone: $("#telephone").val(),
        note: $("#note").val(),
        optin: $("#optin").attr("checked"),
        giftaid: $("#giftaid").attr("checked"),
        donation: GetValue(),
        type: GetType(),
        period: GetPeriod()
    }, function(data) {
        $("#donateForm").html(data);
        
        document.frmPayment.submit();
    });
}
