﻿/// <reference path="Plugins/jquery-1.3.2-vsdoc.js" />
$(document).ready(function() {
    var isAliasOK = true;
});
function validateAlias(ref, context) {
    var boxElement = "#" + $(ref).attr('id') + "Box";
    $(boxElement).trigger('cssClassChanged');
    if (!$(ref).val())
        return false;
    if (validateEmail($(this)))
        okView($(ref));
    if ($(ref).val() && reg.test($(ref).val())) {
        $.post("/SelfRegister/VerifyEmails", { emailInput: $(ref).val() }, function(data) {
            if (data.validatedAnswer == "1") {
                errorViewClient($(ref));
                $(boxElement).addClass('foundCustomer');
                $(boxElement, context).trigger('cssClassChanged');
                $('input[type="password"]').show().focus();
            }
            else {
                $(boxElement).removeClass('foundCustomer');
                $(boxElement, context).trigger('cssClassChanged');

            }
        });
    }
}

function gotoCheckoutLightBoxWrapLoad(isAjax, context, element) {
    if (isAjax) {
        //Check if the returned content contains any html
        if (context.html().indexOf("</") == -1) {
            data = eval('(' + context.html() + ')');
            $.log("JSON tillbaka");
            if (data.Status && data.Status == "OK") {
                context.html('');
                document.location = htmlDecode(data.Redirect);
                return false;
            }
        }
        isAliasOK = false;
        validateAlias('#Alias');
    }
    else {
        isAliasOK = true;
        CheckMails($('#Alias'), context);
        $('#Alias', context).blur(function(event) {
            validateAlias(this, context);
        });
        $('#Alias', context).change(function() {
            validateAlias(this, context);
        });
        $('#Alias', context).keyup(function(event) {
            validateAlias(this, context);
        });
    }
}
function loginPartialLoad(isAjax, context, element) {
    if (isAjax) {
        //Check if the returned content contains any html
        if (context.html().indexOf("</") == -1) {
            data = eval('(' + context.html() + ')');
            $.log("JSON tillbaka");
            if (data.Status && data.Status == "OK") {
                context.html('');
                document.location = data.Redirect;
                return false;
            }
        }
        isAliasOK = false;
        validateAlias('#Username');
    }
    else {
        isAliasOK = true;
        CheckMails($('#Username'), context);
        $('#Username', context).blur(function(event) {
            validateAlias(this, context);
        });
        $('#Username', context).change(function() {
            validateAlias(this, context);
        });
        $('#Username', context).keyup(function(event) {
            validateAlias(this, context);
        });

    }

    //$(".AjaxBehaviorMiniLogin").AjaxLink({ loadingClass: "jMiniLoginLoading" });
    $.log("Jminilogn end");
}
function CheckMails(ref, context) {
    var boxElement = "#" + $(ref).attr('id') + "Box";
    $(boxElement, context).bind('cssClassChanged', function() {
        if ($(this).hasClass('foundCustomer')) {
            $("fieldset").show();
            okView($(ref));
            $('.ValidUser').show();
            $('.NotValidUser').hide();
        }
        else {
            $(boxElement).siblings("fieldset").not('.alwaysShow').not(boxElement).hide();
            $('.NotValidUser').show();
            $('.ValidUser').hide();

            if (validateEmail($(ref))) {
                okView($(ref));
            } else if (isAliasOK) {
                requiredView($(ref));
            } else {
                errorView($(ref));
            }
        }
    });
}
