﻿var emmisRecaptchaHelper = {
    displayID: "",
    modalPopupID: ""
};

function reloadRecaptcha() {

    if (jQuery('div.captchawrap').length == 1) {
        showRecaptcha(jQuery('div.captchawrap').attr("id"));
    }
}

function showRecaptcha(recaptchaWrapperID) {
    if (jQuery('#' + recaptchaWrapperID).length == 1) {
        eval(recaptchaWrapperID + "_captcha.init()");
    }
}

function emmisRecaptcha(ei_recaptcha_container, ei_recaptcha_theme) {
    return emmisRecaptcha(ei_recaptcha_container, ei_recaptcha_theme, 'en');
}

function emmisRecaptcha(ei_recaptcha_container, ei_recaptcha_theme, ei_recaptcha_lang) {

    if (jQuery.trim(emmisRecaptchaHelper.displayID) != '') {
        if (jQuery.trim(emmisRecaptchaHelper.displayID).toLowerCase() != ei_recaptcha_container.toLowerCase()) {
            return;
        }
    }
    
    this.recaptchaContainer = ei_recaptcha_container;
    this.recaptchaTheme = ei_recaptcha_theme;

    this.init = function() {
        try {
            var thisEmmisRecaptcha = this;

            if (ei_recaptcha_key == '') {
                return null;
            }

            if (typeof (Recaptcha) != 'undefined') {
                Recaptcha.destroy();

                Recaptcha.create(ei_recaptcha_key,
                this.recaptchaContainer, {
                    theme: this.recaptchaTheme,
                    callback: Recaptcha.focus_response_field,
                    lang: ei_recaptcha_lang
                });

                return thisEmmisRecaptcha;
            }
            else {
                return null;
            }
        }
        catch (err) { }
    }

    this.show = function() {
        this.init();
    }

    this.hide = function() {
        Recaptcha.destroy();
    }

    this.show();
    return this;
}

function showRecaptchaOverlay(modalPopupID, recaptchaWrapperID) {
    $find(modalPopupID).show();
    showRecaptcha(recaptchaWrapperID + "_recaptcha_recaptchaControl");
}
