﻿/*jslint bitwise: true, browser: true, eqeqeq: true, nomen: true, undef: true, white: true */
/*extern $get, BLACKBAUD, isIE, Sys, $addHandler, Recaptcha, setInnerHTML */

// Only one captcha instance can communicate with the reCAPTCHA server at a time because it expects the controls to have certain IDs.
BLACKBAUD.netcommunity.captchaOperationPending = false;

BLACKBAUD.netcommunity.Captcha = function(params) {
    var $ = jQuery;
    //jQuery objects
    var container;
    var response;
    var challenge;
    var lblNoCaptcha;
    var dlYesCaptcha;

    function getResponse() {
        return response;
    }

    function getChallenge() {
        saveChallenge();
        return challenge;
    }

    function displayFailMessage() {
        lblNoCaptcha.show();
        clearInterval(params.creationTimer);
    }

    function checkForLoaded() {
        if (container.attr('id') == 'recaptcha_image' && response.attr('id') == 'recaptcha_response_field') {
            if (container.html() !== "") {
                clearInterval(params.checkForLoadedTimer);

                clearTimeout(params.errorTimer);
                lblNoCaptcha.hide();
                dlYesCaptcha.show();
                container.get(0).style.width = "";
                container.get(0).style.height = "";

                // Set the IDs back to what ASP expects
                container.attr('id', params.pnlImageContainer);
                response.attr('id', params.txtResponse);
                BLACKBAUD.netcommunity.captchaOperationPending = false;
            }
        }
    }

    function setupLoadedMonitor() {
        container = $('#' + params.pnlImageContainer);
        response = $('#' + params.txtResponse); ;
        if (container.get(0) && response.get(0)) {
            // set the IDs to what the naive reCAPTCHA script will be looking for
            container.attr('id', 'recaptcha_image');
            response.attr('id', 'recaptcha_response_field');

            setInnerHTML(container.get(0), "");
            params.checkForLoadedTimer = setInterval(function() {
                checkForLoaded();
            }, 100);
        }
    }

    function tryCreate() {
        //Recaptcha is a variable in Recaptcha's js file that is external http://api.recaptcha.net/js/recaptcha_ajax.js
        if (typeof (Recaptcha) !== "undefined" && !BLACKBAUD.netcommunity.captchaOperationPending) {            
            BLACKBAUD.netcommunity.captchaOperationPending = true;
            clearInterval(params.creationTimer);
            setupLoadedMonitor();
            Recaptcha.create(BLACKBAUD.netcommunity.recaptchaPublicKey, "", {
                theme: "custom"
            });

            //CR309997-101508
            //The href has been set, so we must properly stop the default action from occurring
            $('#' + params.lnkReload).bind('click', function(evt) {
                reload();
                evt.preventDefault();
            });
            $('#' + params.lnkGetAudio).bind('click', function(evt) {
                getAudio();
                evt.preventDefault();
            });
            $('#' + params.lnkGetImage).bind('click', function(evt) {
                getImage();
                evt.preventDefault();
            });
        }
    }

    function startup() {

        lblNoCaptcha = $('#' + params.lblNoCaptcha);
        dlYesCaptcha = $('#' + params.dlYesCaptcha);

        // Give it 3 seconds before displaying the message
        params.errorTimer = setTimeout(function() {
            displayFailMessage();
        }, 3000);

        // Create the captcha
        params.creationTimer = setInterval(function() {
            tryCreate();
        }, 100);

        params.initialized = true;
    }

    function reload(type) {
        //type t will not cause focus
        if (!params.initialized) {
            startup();
        }
        else if (BLACKBAUD.netcommunity.captchaOperationPending) {
            setTimeout(function() {
                reload(type);
            }, 100);
        }
        else if (typeof (Recaptcha) !== "undefined") {
            BLACKBAUD.netcommunity.captchaOperationPending = true;
            setupLoadedMonitor();
            Recaptcha.reload(type);
        }
    }

    function getImage() {
        if (BLACKBAUD.netcommunity.captchaOperationPending) {
            setTimeout(function() {
                getImage();
            }, 100);
        }
        else if (typeof (Recaptcha) !== "undefined") {
            setupLoadedMonitor();
            Recaptcha.switch_type('image');
        }
        return false;
    }

    function getAudio() {
        if (BLACKBAUD.netcommunity.captchaOperationPending) {
            setTimeout(function() {
                getAudio();
            }, 100);
        }
        else if (typeof (Recaptcha) !== "undefined") {
            setupLoadedMonitor();
            Recaptcha.switch_type('audio');
        }
        return false;
    }

    function saveChallenge() {
        challenge = $('#' + params.hidChallenge);

        if (challenge && typeof (Recaptcha) !== 'undefined') {
            challenge.val(Recaptcha.get_challenge());
        }
    }

    // public accessors
    this.startup = startup;
    this.reload = reload;
    this.getImage = getImage;
    this.getAudio = getAudio;
    this.saveChallenge = saveChallenge;
    this.getResponse = getResponse;
    this.getChallenge = getChallenge;
};


////////////////////////////////////////////
// End of script
if (typeof(Sys) !== 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}
////////////////////////////////////////////
// Do not add any code below this
////////////////////////////////////////////
