$(function() {
    $('.forgotPasswordLink').click(function () {
        $('#forgotPasswordLink').blur();
        $.get("passwordHint.html?method=viewSendPassword&amp;" + new Date(), function(response) {
            $('#forgotPasswordDispaly').html("");
            $('#forgotPasswordDispaly').html(response);
            $('#forgotPasswordDispaly').modal({overlayClose:true,minHeight:30,minWidth:15,position: ['5%','25%'],overlay:30, overlayCss:{backgroundColor:"#000"},dataCss:{height:"100px", width:"500"},onOpen: function (dialog) {
                dialog.overlay.fadeIn('slow', function () {
                    dialog.data.hide();
                    dialog.container.fadeIn('slow', function () {
                        dialog.data.slideDown('slow');
                    });
                });
            }});
        });
    });

    $('#check-availability').click(function () {
        var userName = $("#user-name").val();
        if (userName == "") {
            $('#codestatus').html("Ange v&auml;rde");
            $('#codestatus').show();
        } else {
            $.get('viewRegistration.html?method=checkAvailability&amp;' + new Date(), {"userName": userName}, function(response) {
                var message = response.status;
                $('#codestatus').html(message);
                $('#codestatus').show();
            }, "json");
        }
    });
    $('#address').blur(function() {
        var val = $('#registration-signup-form').validate().element("#address");
    });
    $('#registration-signup-form input').blur(function() {
        id = $(this)[0].id;
        if (id != "") {
            var val = $('#registration-signup-form').validate().element("#" + $(this)[0].id);
            if (val) {
                if (id == "user-name") {
                    userName_blur();
                } else if (id == "mobile") {
                    mobile_blur();
                }
            }
            if (id != "user-name" && id != "mobile" && $("#registration-signup-form label.error:visible").length > 0) {
                $("#zipCode-city  label.error").css({ 'width': '66px', 'float': 'left', 'padding-left':'13px'});
                $('#registration-submit').hide();
            } else {
                $('#registration-submit').show();
            }
        }
    });
    $('#start').click(function() {
        $("#award-winning").hide();
        $("#static-page").hide();
        $('div#content a#next').click();
        $("#slider").css({'height' : '743px'});
    });
    $('#award-link').click(function() {
        $("#award-winning").show();
        $('div#content a#next').click();
        $('#youtube-holder').fadeIn('slow');
        $("#slider").css({'height' : '970px'});
    });
    $('#award-to-pkg').click(function() {
        $('div#content a#next').click();
        $('#youtube-holder').hide();
        $("#slider").css({'height' : '743px'});
    });
    $('.mobil-link').click(function() {
        $("#award-winning").hide();
        $("#static-page").show();
        $('div#content a#next').click();
        $("#slider").css({'height' : '2430px'});
    });
    $('#register').click(function() {
        $("#slider").css({'height' : '743px'});
        $('div#content a#next').click();
        $('#youtube-holder').hide();
    });
    $('#package1').click(function() {
        $('div#content a#next').click();
        $('#selectPackage').attr("value", 2);
        $('#package_label').html("");
        $('#package_label').html($("#selected-package").html());
        $("#slider").css({'height' : '970px'});
    });
    $('#package2').click(function() {
        $('div#content a#next').click();
        $('#selectPackage').attr("value", 1);
        $("#slider").css({'height' : '970px'});
    });
    $('#package3').click(function() {
        alert("Kommer snart");
        $('#main-body').removeClass('landing-bg');
        $('#main-body').addClass('static-bg');
    });
    $('#club-minute').click(function() {
        $('div#content a#next').click();
    });
});

function userName_blur() {
    var userName = $('#user-name').val();
    if (/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i.test(userName)) {
        $.get('viewRegistration.html?method=checkAvailability&amp;' + new Date(), {"userName": userName}, function(response) {
            var message = response.status;
            if (message.indexOf("error#") >= 0) {
                $('#codestatus').html("");
                $('#user-name').after("<label class=\"error\" generated=\"true\">" + message.replace("error#", "") + "</label>")
            } else {
                $('#codestatus').html("");
                $('#codestatus').html(message);
                $('#codestatus').show();
            }
            if ($("#registration-signup-form label.error:visible").length > 0) {
                $('#registration-submit').hide();
            } else {
                $('#registration-submit').show();
            }
        }, "json");
    }
}

function mobile_blur() {
    var mobile = $('#mobile').val();
    var interNationalValidation = $('#international-validation').html().trim();
    if (interNationalValidation == "true") {
        if (/^[0-9]+$/.test(mobile) && mobile.length >= 8 && mobile.length <= 15) {
            $.get('viewRegistration.html?method=checkMobile&amp;' + new Date(), {"mobile": mobile}, function(response) {
                var message = response.status;
                if (message.indexOf("error#") >= 0) {
                    $('#mobile').after("<label class=\"error\" generated=\"true\">" + message.replace("error#", "") + "</label>")
                } else {
                    $('#m-status').html("");
                    $('#m-status').html(message);
                    $('#m-status').show();
                }
                if ($("#registration-signup-form label.error:visible").length > 0) {
                    $('#registration-submit').hide();
                } else {
                    $('#registration-submit').show();
                }
            }, "json");
        }
    } else {
        if (/^\(?(0|([+]?46))\)?(\d{9})$/i.test(mobile)) {
            $.get('viewRegistration.html?method=checkMobile&amp;' + new Date(), {"mobile": mobile}, function(response) {
                var message = response.status;
                if (message.indexOf("error#") >= 0) {
                    $('#mobile').after("<label class=\"error\" generated=\"true\">" + message.replace("error#", "") + "</label>")
                } else {
                    $('#m-status').html("");
                    $('#m-status').html(message);
                    $('#m-status').show();
                }
                if ($("#registration-signup-form label.error:visible").length > 0) {
                    $('#registration-submit').hide();
                } else {
                    $('#registration-submit').show();
                }
            }, "json");
        }
    }
}
