/*
 * *************************************************************************************
 * Start of DOM  
 * *************************************************************************************
*/

$(document).ready(function() {

    $('#loginBlock').hide();
    $('#clickme').click(function() {
        $('#loginBlock').toggle('slow', function() {
        });
    });


    /*
    * *************************************************************************************
    * radio button switch on payments - One Off / Direct Debit.
    * *************************************************************************************
    */

    $("input[name$='frequency']").click(function() {
        ShowPaymentOptions(this, "donation");
    });
    /*
        var radio_value = $(this).val();
        if (radio_value == 'one off') {
            $opt_a.show();
            $opt_b.hide();
        }
        else if (radio_value == 'monthly') {
            $opt_b.show();
            $opt_a.hide();
        } */
    



    /*
    * *************************************************************************************
    * grab the age of user and split into array
    * *************************************************************************************
    */
    $("input.dobb").blur(function() {
        var dob = $(this).val();
        //checkDate(dob);
        var valueArray = dob.split("/");
        //console.log(valueArray);
        CalAge(valueArray);
    });

    //check the DOB using the drop down selects
    $("select.dob").change(function() {
        ShowAgeRelatedMemberships(true);
    });


    /*
    * *************************************************************************************
    * if user is lt or gt 60 dislay item blocks  
    * *************************************************************************************
    */

    $("input[name$='MembershipType']").click(function() {
        ShowPaymentOptions(this, "membership");
    });
    /*    var radio_value = $(this).val();
        if (radio_value == '1' || radio_value == '3') {
            $opt_b.show();
            $opt_a.hide();
        }
        else if (radio_value == '2' || radio_value == '4' || radio_value == '5') {
            $opt_a.show();
            $opt_b.hide();
        }*/
    //$('#StandardAnnual')[0].checked = true; //sets the checkbox to checked



    // Have to run these functions at page load


    var $num_membership_checkboxes = $("input[name$='MembershipType']").length;
    if ($num_membership_checkboxes > 0) {
       
        var $rad_selected = $("input[name$='MembershipType']:checked").val();
        if ($rad_selected == undefined) {
            $('.StandardAnnual')[0].checked = true;
        }
        ShowAgeRelatedMemberships(false);
    }
    ShowPaymentOptions(null, null);

});            //end of DOM  


/*
 * *************************************************************************************
 * Functions 
 * *************************************************************************************
*/

// used to clear input onclick.
function formDefault(theInput) {
    if (theInput.value=='') {
        theInput.value = theInput.defaultValue;
    }
}   
            
// calculate and display depending on age
function CalAge(valueArray) {
    var dd = valueArray[0];
    var mm = valueArray[1];
    var yy = valueArray[2];
    var age = 60;

    var mydate = new Date();
    mydate.setFullYear(yy, mm-1, dd);

    var currdate = new Date();
    currdate.setFullYear(currdate.getFullYear() - age);
    
    if ((currdate - mydate) < 0){
        $(".standard").show();
        $(".oversixty").hide();
        $('#StandardAnnual')[0].checked = true;
        
    }
    else if ((currdate - mydate) > age) {
        $(".oversixty").show();
        $(".standard").hide();
        $('#Over60Annual')[0].checked = true;
    }
}


function ShowAgeRelatedMemberships(setCheckbox) {

    var dd = $(".dob_ddlDay").val();
    var mm = $(".dob_ddlMonth").val();
    var yy = $(".dob_ddlYear").val();

//    var dd = $("#ctl00_ddlDay").val();
//    var mm = $("#ctl00_ddlMonth").val();
//    var yy = $("#ctl00_ddlYear").val();
    var age = 60;

    var mydate = new Date();
    mydate.setFullYear(yy, mm - 1, dd);
    
    var $radio_value = $("input[name$='MembershipType']:checked").val()

    var currdate = new Date();
    currdate.setFullYear(currdate.getFullYear() - age);
    if ((currdate - mydate) < 0){
        $(".standard").show();
        $(".oversixty").hide();
        // If the radio selected is NOT already one of the over-sixty options, or Life membership
        //  set the radio selected to Standard Annual
        if (setCheckbox && ($radio_value == '3' || $radio_value == '4')) {
            // If the seleted option was the one-off Standard, set the selected radio to Over-sixty one-off
            if ($radio_value == '4') {
                $('.StandardOneOff')[0].checked = true;
            }
            else {
                $('.StandardAnnual')[0].checked = true; 
            }
        }
    }
    else if ((currdate - mydate) > age) {
        $(".oversixty").show();
        $(".standard").hide();
        if (setCheckbox && ($radio_value == '1' || $radio_value == '2')) {
            if ($radio_value == '2') {
                $('.Over60OneOff')[0].checked = true;
            }
            else {
                $('.Over60Annual')[0].checked = true; 
            }
        } // If the seleted option was the one-off Standard, set the selected radio to Over-sixty one-off
         
        
    }
}

// Switches between the Credit Card and Direct Debit panes.
// the params will either be set or null.
function ShowPaymentOptions(selectedRad, interfaceType) {
    var $radio_value = "";
    var $opt_a = $(".opt-a-yes");
    var $opt_b = $(".opt-a-no");

    
    // FOr donations, select the radio button value that chooses monthly or One-off payment
    if (interfaceType == "donation" || interfaceType == null) {
        var $num_frequency_radios;
        if (selectedRad != null) {
            $radio_value = $(selectedRad).val();
        }
        else         {
            $num_frequency_radios = $("input[name$='frequency']").length;
            if ($num_frequency_radios > 0) {
                $radio_value = $("input[name$='frequency']:checked").val();
            }
        }

        if ($radio_value != "") {
            if ($radio_value == 'one off') {
                $opt_a.show();
                $opt_b.hide();
            }
            else {
                $opt_b.show();
                $opt_a.hide();
            }
        }
    }
    
    $radio_value = "";
    // For membership, the pane is chosen depending on the membership type selected.
    if (interfaceType == "membership" || interfaceType == null) {
        var $num_membership_checkboxes;

        if (selectedRad != null) {
            $radio_value = $(selectedRad).val();
        }
        else         {
            $num_membership_checkboxes = $("input[name$='MembershipType']").length;
            if ($num_membership_checkboxes > 0) {
                $radio_value = $("input[name$='MembershipType']:checked").val();
            }
        }
        if ($radio_value != "") {
            if ($radio_value == '1' || $radio_value == '3') {
                $opt_b.show();
                $opt_a.hide();
            }
            else if ($radio_value == '2' || $radio_value == '4' || $radio_value == '5') {
                $opt_a.show();
                $opt_b.hide();
            }
        }
    }
}


// check date format
function checkDate(dob) {
    var day, mo, yr;
    var entry = dob.value;
    var reLong = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var reShort = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{2}\b/;
    var valid = (reLong.test(entry)) || (reShort.test(entry));
    if (valid) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        // handle two-digit year
        if (yr < 100) {
            var today = new Date();
            // get current century floor (e.g., 2000)
            var currCent = parseInt(today.getFullYear() / 100) * 100;
            // two digits up to this year + 15 expands to current century
            var threshold = (today.getFullYear() + 15) - currCent;
            if (yr > threshold) {
                yr += currCent - 100;
            } else {
                yr += currCent;
            }
        }
        var testDate = new Date(yr, mo-1, day);
        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    // fill field with database-friendly format
                    dob.value = day + "/" + mo + "/" + yr;
                    return true;
                } else {
                    alert("There is a problem with the year entry.");
                }
            } else {
                alert("There is a problem with the month entry.");
            }
        } else {
            alert("There is a problem with the date entry.");
        }
    } else {
        alert("Incorrect date format. Enter as dd/mm/yyyy.");
    }
    return false;
}
