﻿//$(document).ready(InitOverlay);

$(document).ready(function () {
    InitOverlay();

    if (isHomepage()) {
        $(".quick_reg .select_field, .quick_reg .terms input").uniform();
    }
    $(".quick_reg .close").click(function () {
        dismissFirstLoad();
    });

    $(".quick_reg .success_close").click(function () {
        $(".quick_reg .message").hide();
        dismissFirstLoad();
    });

    $(".quick_reg .error_close").click(function () {
        $(".quick_reg .message").hide();
    });

    $("#aContinue").click(function () {
        $(".quick_reg .message").hide();
        dismissFirstLoad();
    });

    var KEYCODE_ESC = 27;
    $(document).keyup(function (e) {
        if (e.keyCode == KEYCODE_ESC) {
            quickRegCloseOnEsc();
        }
    }); 

});

function quickRegCloseOnEsc() {
    if ($(".quick_reg, .quick_reg_overlay").is(":visible")) {
        quickRegClose();
    }
}

function isHomepage() {
    if ($("body#home").length > 0 || $("body#homepage").length > 0) {
        return true;
    }
    else {
        return false;
    }
}

function GetShouldTakeoverBeVisible() {
    var cookie = getCookie(OverlayCookieName + getUName());
    if (cookie == '' || cookie == 'on') {
        if (!isUserLoggedIn())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else {
        return false;
    }
}

function SetShouldTakeoverBeVisible(value) {
    setCookie(OverlayCookieName + getUName(), value, 365);
}

function InitOverlay() {

    if (!isHomepage()) {
        return;
    }

    if (ShowOverlay == 'off') {
        return;
    }

    if (!GetShouldTakeoverBeVisible()) {
        return;
    }

    $(".quick_reg, .quick_reg_overlay").show();
}

function showPreRegistrationOverlayControl()
{
    return (GetShouldTakeoverBeVisible() && ShowOverlay != 'off');
}

function quickRegClose() {
    dismissFirstLoad();
    saveActionOnClose();
}

function dismissFirstLoad() {
    SetShouldTakeoverBeVisible('off');
    hideOverlay();
}

function hideOverlay() {
    $(".quick_reg, .quick_reg_overlay").hide();
}

function SetisVisible(isVisible, control)
{
    if (isVisible) {
        control.css('display', 'block');
    }
    else {
        control.css('display', 'none');
    }
}

function cChange() {
    //$('.terms').attr("checked", false);
    $('.checker_big span').removeClass('checked');
    if ($('.select_field').val() == 'uk') {
        $('#divTerms').show();
        SetisVisible(true, $('#ukAgreementHolder'));
        SetisVisible(false, $('#irelandAgreementHolder'));
    }
    else if ($('.select_field').val() == 'ireland') {
        $('#divTerms').show();
        SetisVisible(false, $('#ukAgreementHolder'));
        SetisVisible(true, $('#irelandAgreementHolder'));
    }
    else {
        $('#divTerms').hide();
        SetisVisible(false, $('#ukAgreementHolder'));
        SetisVisible(false, $('#irelandAgreementHolder'));
    }
}

function ValidateQRData() {
    $(".quick_reg .message .error .hError").text('');
    $(".quick_reg .message .error .hError").html('');
    $(".quick_reg .message .error").hide();
    $(".quick_reg .message .success").hide();

    var isValid = true;

    var email = $(".input_field");
    if (email.val() == '' || email.val() == 'email address...' || email.val() == 'email address..' || email.val() == 'email address.') {
        isValid = false;
        $(".quick_reg .message .error .hError").html('Oops! You need to put in your email address before you can continue<br />');
    }

    var country = $(".select_field");
    if (country.val() != 'uk' && country.val() != 'ireland') {
        isValid = false;
        $(".quick_reg .message .error .hError").html($(".quick_reg .message .error .hError").html() + 'Please choose your country!<br />');
    }
    else {
        if (!$('.cb_field').attr('checked')) {
            isValid = false;
            $(".quick_reg .message .error .hError").html($(".quick_reg .message .error .hError").html() + 'Before you can proceed, you must agree to our terms and conditions');
        }
    }

    if (!isValid) {
        $(".quick_reg .message").show();
        $(".quick_reg .message .error").show();
    }
    else 
    {
        var channelSubmitted = "SvSLightbox";
        quickRegisterUser(email.val(), country.val(), channelSubmitted);
    }
}

function quickRegisterUser(emailAddress, country, channelSubmitted) {
    try {
        $.ajax({
            url: "/publicProxy.aspx",
            data: "command=preRegisterUser&em=" + encodeURL(emailAddress) + "&co=" + country + "&cs=" + channelSubmitted, 
            success: function (response) {
                var data = eval(response);
                var success = data[0].success;
                var errorMessage = data[1].errorMessage;
                if (!success) {
                    $(".quick_reg .message .error .hError").html(errorMessage);
                    $(".quick_reg .message .error").show();
                }
                else 
                {
                    $(".quick_reg .message .success").show();
                    setCookie("preregistered", "true", 365);
                    setCookie("preregistrationdata", emailAddress, 365);
                    contentPreRegistration.hide();
                    contentAlreadyPreRegistered.show();
                }
                $(".quick_reg .message").show();
            }
        });
    }
    catch (e) {
        $(".quick_reg .message .error .hError").html('There was an error processing the request. Please try again later!');
        $(".quick_reg .message .error").show();
        $(".quick_reg .message").show();
    }
}

function saveActionOnClose() {
    var urlServiceCloseQuickRegOverlay = usrServiceUrlforOverlay + "/closeOverlayJSON";
    try {
        $.ajax({
            url: urlServiceCloseQuickRegOverlay,
            dataType: "jsonp",
            success: function () { }
        });
    }
    catch (e) {
        
    }
}

function txtBoxClear(idControl) {
    var c = $(idControl);
    if (c.val() != '' && (c.val() == 'email address...' || c.val() == 'email address..' || c.val() == 'email address.')) {
        c.val('');
    }
}

function txtSetWatermark(idControl) {
    var c = $(idControl);
    if (c.val() == '') {
        c.val('email address...');
    }
}

