﻿/*jslint bitwise: true, browser: true, eqeqeq: true, nomen: true, undef: true, white: true */
/*extern $get, debuggerMaybe, isIE, Sys, $addHandler, Recaptcha, recaptchaPublicKey, setInnerHTML */

var captcha = {};

captcha.startup = function (captchaVars)
{
    // Fire this periodically to make sure the postback contains the challenge information
    setInterval(function () {
        captcha.saveChallenge(captchaVars);
    }, 500);
    
    // Give it 3 seconds before displaying the message
    captchaVars.errorTimer = setTimeout(function () {
        captcha.displayFailMessage(captchaVars);
    }, 3000);

    // Create the captcha
    captchaVars.creationTimer = setInterval(function () {
        captcha.tryCreate(captchaVars);
    }, 100);
    
    captchaVars.initialized = true;
};

captcha.saveChallenge = function (captchaVars)
{
    var challenge = $get(captchaVars.hidChallengeID);
    if (challenge && typeof(Recaptcha) !== 'undefined')
    {
        challenge.value = Recaptcha.get_challenge();
    }
};

captcha.displayFailMessage = function (captchaVars)
{
    $get(captchaVars.lblNoCaptchaID).style.display = "";
};

captcha.tryCreate = function (captchaVars)
{
    if (typeof(Recaptcha) !== "undefined" && !captcha.pending)
    {
        captcha.pending = true;
        clearInterval(captchaVars.creationTimer);
        captcha.setupLoadedMonitor(captchaVars);
        Recaptcha.create(recaptchaPublicKey, "", {
            theme : "custom"
        });
    }
};

captcha.reload = function (captchaVars)
{
    if (!captchaVars.initialized)
    {
        captcha.startup(captchaVars);
    }
    else if (captcha.pending)
    {
        setTimeout(function () {
            captcha.reload(captchaVars);
        }, 100);
    }
    else if (typeof(Recaptcha) !== "undefined")
    {
        captcha.pending = true;
        captcha.setupLoadedMonitor(captchaVars);
        Recaptcha.reload();
    }
};

captcha.getAudio = function (captchaVars)
{
    if (captcha.pending)
    {
        setTimeout(function () {
            captcha.getAudio(captchaVars);
        }, 100);
    }
    else if (typeof(Recaptcha) !== "undefined")
    {
        captcha.setupLoadedMonitor(captchaVars);
        Recaptcha.switch_type('audio');
    }
};

captcha.getImage = function (captchaVars)
{
    if (captcha.pending)
    {
        setTimeout(function () {
            captcha.getImage(captchaVars);
        }, 100);
    }
    else if (typeof(Recaptcha) !== "undefined")
    {
        captcha.setupLoadedMonitor(captchaVars);
        Recaptcha.switch_type('image');
    }
};

captcha.setupLoadedMonitor = function (captchaVars)
{
    var container = $get(captchaVars.pnlImageContainerID);
    var response = $get(captchaVars.txtResponseID);
    if (container && response)
    {
        // set the IDs to what the naive reCAPTCHA script will be looking for
        container.id = "recaptcha_image";
        response.id = "recaptcha_response_field";
        
        setInnerHTML(container, "");
        captchaVars.checkForLoadedTimer = setInterval(function () {
            captcha.checkForLoaded(captchaVars);
        }, 100);
    }
};

captcha.checkForLoaded = function (captchaVars)
{
    var container = $get("recaptcha_image");
    var response = $get("recaptcha_response_field");
    if (container && response)
    {
        if (container.innerHTML !== "")
        {
            clearInterval(captchaVars.checkForLoadedTimer);
            
            clearTimeout(captchaVars.errorTimer);
            $get(captchaVars.lblNoCaptchaID).style.display = "none";
            $get(captchaVars.dlYesCaptchaID).style.display = "";
            container.style.width = "";
            container.style.height = "";
            
            // Set the IDs back to what ASP expects
            container.id = captchaVars.pnlImageContainerID;
            response.id = captchaVars.txtResponseID;
            captcha.pending = false;
        }
    }
};

////////////////////////////////////////////
// End of script
if (typeof(Sys) !== 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}
////////////////////////////////////////////
