﻿$(document).ready(function () {
    /*
    ############################################################################
    NAVIGATION FUNCTIONS: BEGIN
    ############################################################################
    */
    var timeout = 300;
    var closetimer = 0;
    var ddmenuitem = 0;

    function nav_open() {
        //cancel close timer...
        nav_canceltimer();

        //close old layer...
        nav_close();

        //get new layer and show it...
        ddmenuitem = $(this).find('div').css('visibility', 'visible');
        $(this).find('a').css('background-position', '0 -45px');
    }

    function nav_close() {
        if (ddmenuitem) {
            $(ddmenuitem).siblings('a').css('background-position', '0 0');
            $(ddmenuitem).css('visibility', 'hidden');
        }
    }

    function nav_timer() {
        closetimer = window.setTimeout(nav_close, timeout);
    }

    function nav_canceltimer() {
        if (closetimer) {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    }

    $('.n_sub').bind('mouseover', nav_open);
    $('.n_sub').bind('mouseout', nav_timer);
    $('.n_home,.n_contactus').bind('mouseover', nav_close);
    /*
    ############################################################################
    NAVIGATION FUNCTIONS: END
    ############################################################################
    */

    /* prevent sharing on images through meebo */
    $(":image").attr("meebo:notsharable", "true");
    $("a").attr("meebo:notsharable", "true");

    //setup homepage slideshow
    $('#photos').galleryView({
        panel_width: 610,
        panel_height: 165,
        frame_width: 50,
        frame_height: 50,
        pause_on_hover: true,
        nav_theme: 'light'
    });

    //donation page...
    $("input[id*=chkOneTimeDonation]")
        .change(function () {
            if ($(this).is(':checked')) {
                //close the other section...
                $("input[id*=chkRecurringDonation]")
                    .attr('checked', false);

                $("div#divRecurringDonation")
                    .hide('fast');
                //.animate({ height: 0, 'visibility': 'hidden' }, 'slow', 'swing');

                //open this section...
                $("div#divOneTimeDonation")
                    .show('fast');
                //.animate({ height: 70, 'visibility': 'visible' }, 'slow', 'swing');

                $("[id*=ddGiveToProgram]")
                    .val('GEN-D')
                    .attr("disabled", "");
            }
            else {
                //open the other section...
                $("input[id*=chkRecurringDonation]")
                    .attr('checked', true);

                $("div#divRecurringDonation")
                    .show('fast');
                //.animate({ height: 0, 'visibility': 'hidden' }, 'slow', 'swing');

                //close this section...
                $("div#divOneTimeDonation")
                    .hide('fast');
                //.animate({ height: 70, 'visibility': 'visible' }, 'slow', 'swing');

                $("[id*=ddGiveToProgram]")
                    .val('GEN-D')
                    .attr("disabled", "disabled");
            }
        });

    $("input[id*=chkRecurringDonation]")
        .change(function () {
            if ($(this).is(':checked')) {
                //close the other section...
                $("input[id*=chkOneTimeDonation]")
                    .attr('checked', false);

                $("div#divOneTimeDonation")
                    .hide('fast');
                //.animate({ height: 0, 'visibility': 'hidden' }, 'slow', 'swing');

                //open this section...
                $("div#divRecurringDonation")
                    .show('fast');
                //.animate({ height: 70, 'visibility': 'visible' }, 'slow', 'swing');
                
                $("[id*=ddGiveToProgram]")
                    .val('GEN-D')
                    .attr("disabled", "disabled");
            }
            else {
                //open the other section...
                $("input[id*=chkOneTimeDonation]")
                    .attr('checked', true);

                $("div#divOneTimeDonation")
                    .show('fast');
                //.animate({ height: 0, 'visibility': 'hidden' }, 'slow', 'swing');

                //close this section...
                $("div#divRecurringDonation")
                    .hide('fast');
                //.animate({ height: 70, 'visibility': 'visible' }, 'slow', 'swing');

                $("[id*=ddGiveToProgram]")
                    .val('GEN-D')
                    .attr("disabled", "");
            }
        });

    $("input[type=radio]")
            .change(function () {
                if ($(this).val() == 0) {
                    $("div#divOtherRecurringAmount")
                        .show('fast');
                }
                else {
                    $("div#divOtherRecurringAmount")
                        .hide('fast');
                }
            });
});

//////////////////////////////////////////////////////////////////////////////////////
//Launch a modal popup...
//////////////////////////////////////////////////////////////////////////////////////
var popupStatus = 0;

function launchModal(modw, modh, url) {
    if (popupStatus == 0) {
        $("div#modalPopupBackground").css({
            'opacity': '0.7'
        });

        $("div#modalPopupBackground").css({
            'height': $(document).height(),
            'width': $(document).width()
        }).fadeIn("slow");
        $("div#modalPopup").css({
            'height': modh,
            'width': modw
        }).fadeIn("slow");

        $("div#modalPopup").center();

        $("iframe#modalIframe")
            .css({
                'width': '100%',
                'height': '100%'
            })
            .attr({
                'src': url
            });

        popupStatus = 1;
    }
}

//////////////////////////////////////////////////////////////////////////////////////
//Close a modal popup...
//////////////////////////////////////////////////////////////////////////////////////
function closeModal() {
    if (popupStatus == 1) {
        $("div#modalPopup").fadeOut('slow');
        $("div#modalPopupBackground").fadeOut('slow');
        $("iframe#modalIframe")
            .attr({
                'src': ''
            });
        popupStatus = 0;
    }
}

//////////////////////////////////////////////////////////////////////////////////////
//Validate the donation form...
//////////////////////////////////////////////////////////////////////////////////////
function validate_donate_form() {
    var msg = '';
    var undef = false;
    var ret = true;

    $("ul.validation_summary").remove();

    if ($("input[id*=chkOneTimeDonation]").is(":checked")) {
        //making a one time donation...

        //make sure the user entered a whole value greater than 0...
        var amt = $("input[id*=txtOneTimeAmount]").val();
        if (amt.indexOf('.') > -1 || amt == 0 || amt == '') {
            $("input[id*=txtOneTimeAmount]")
                .attr('class', 'donate_amt_fld_invalid');

            ret = false;
            msg = msg + '<li>Enter a whole dollar amount greater than 0.</li>';
        }
    }
    else {
        if($("input[type=radio]:last").is(":checked")) {
            var amt = $("input[id*=txtOtherRecurringAmount]").val();
            if (amt.indexOf('.') > -1 || amt == 0 || amt == null) {
                $("input[id*=txtOtherRecurringAmount]")
                .attr('class', 'donate_amt_fld_invalid');

                ret = false;
                msg = msg + '<li>Enter a whole dollar amount greater than 0.</li>';
            }
        }
    }

    //required fields...
    $("[rel=required]").each(function (index) {
        if ($(this).val() == '') {
            $(this).attr('class', 'input_invalid');

            ret = false;
            if (!undef) {
                msg = msg + '<li>Please fill in all required fields.</li>';
                undef = true;
            }
        }
        else {
            $(this).attr('class', 'input_border');
        }
    });

    //match fields...
    if($("input[id*=txtEmail]").val() !== $("input[id*=txtConfirmEmail]").val()) {
        $("input[id*=txtEmail]").attr('class', 'input_invalid');
        $("input[id*=txtConfirmEmail]").attr('class', 'input_invalid');

        ret = false;
        msg = msg + '<li>Email addresses do not match.</li>';
    }
    else {
        $("input[id*=txtEmail]").attr('class', 'input_border');
        $("input[id*=txtConfirmEmail]").attr('class', 'input_border');
    }

    //recaptcah
    if (!validateReCaptcha()) {
        ret = false;
        msg = msg + '<li>Please enter the text in the picture, again.</li>';
    }

    if (!ret) {
        $("h1.donate_title").after('<ul class="validation_summary">' + msg + '</ul>');
        window.scrollTo(0, 0);

        return false;
    }
    else {
        return true;
    }
}

//////////////////////////////////////////////////////////////////////////////////////
//Validate the checkout form...
//////////////////////////////////////////////////////////////////////////////////////
function validate_checkout_form() {
    var msg = '';
    var undef = false;
    var ret = true;

    $("ul.validation_summary").remove();

    //required fields...
    $("[rel=required]").each(function (index) {
        if ($(this).val() == '') {
            $(this).attr('class', 'input_invalid');

            ret = false;
            if (!undef) {
                msg = msg + '<li>Please fill in all required fields.</li>';
                undef = true;
            }
        }
        else {
            $(this).attr('class', 'input_border');
        }
    });

    if (!ret) {
        $("h1.donate_title").after('<ul class="validation_summary">' + msg + '</ul>');
        window.scrollTo(0, 0);

        return false;
    }
    else {
        return true;
    }
}

//##############
//reCaptcha validation
//##############

function validateReCaptcha() {
    var challengeField = $("#recaptcha_challenge_field").val();
    var responseField = $("#recaptcha_response_field").val();

    //alert(challengeField);
    //alert(responseField);

    var recaptchaResult = $.ajax({
        type: 'POST',
        url:    'ajaxRecaptcha.aspx',
        data:   'recaptcha_challenge_field=' + challengeField + '&recaptcha_response_field=' + responseField,
        async:  false
    }).responseText;

    //alert(recaptchaResult);
    if (recaptchaResult == "True") {
        return true;
    }
    else {
        if (typeof Recaptcha.old_finish_reload === 'undefined') {
            Recaptcha.old_finish_reload = Recaptcha.finish_reload;
            Recaptcha.finish_reload = function (a, b, c, d) {
                Recaptcha.old_finish_reload(a, b, c, d);
            };
        }
        Recaptcha.reload();
        return false;
    }
}
