﻿//Set height equally for main columns.
$(window).load(function () {
    var height = $("#body").height();
    $("#body #rightcol .inner").height(height - 10);
    $("#body #content").height(height - 10);
    $(".downloads").height(height);
})

$(document).ready(function () {

    //Set default enter button
    $(".enter-submit").live("keypress", function (e) {
        if ($(this).parents(".enter-form").find(".submit-button").length <= 0) return true;

        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {

            $(this).parents(".enter-form").find(".submit-button").click();
            return false;
        } else {
            return true;
        }
    });


    //Set Message Modal popup 
    $("#dialog").dialog({
        autoOpen: false,
        title: 'Message',
        height: 100,
        resizable: false
    })

    $("#closeDialog").click(function () {
        $("#dialog").dialog('close');
        return false;
    })

    //Forgotten password functions
    $("#sendpassword").click(function () {

        $(this).children("span").hide();
        $(this).children("img").show();

        $.ajax({
            type: "POST",
            url: "/member-area/services.asmx/RemindPassword",
            data: "{'email': '" + $("#reminderEmail").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d == "Successful") {
                    $("#reminderError").html("We have sent the login details to your email address. Please check your inbox.");
                    $("#reminderError").removeClass("error");
                    $("#reminderError").addClass("success");
                    $("#reminderError").fadeIn();
                    $("#sendpassword").hide();
                }
                else {
                    $("#reminderError").html(msg.d);
                    $("#reminderError").fadeIn();
                    $("#sendpassword").children("span").show();
                    $("#sendpassword").children("img").hide();
                }
            },
            error: function (msg) {
                $("#reminderError").html("There has been an error while sending your login details. Please check your internet connection or contact techinal support and reference error code ERR002.");
                $("#reminderError").fadeIn();

                $("#sendpassword").children("span").show();
                $("#sendpassword").children("img").hide();
            }
        });

        return false;

    });

    $("#forgottenpwd").dialog({
        autoOpen: false,
        width: 250,
        resizable: false
    })

    $("#openreminder").click(function () {
        $("#forgottenpwd").dialog('open');
        return false;
    })

    //Login functions
    $("#butLogin").click(function (event) {

        $(this).children("span").hide();
        $(this).children("img").show();

        var request = "{" +
                    "Email: '" + $("#loginEmail").val() + "'," +
                    "Password: '" + $("#loginPassword").val() + "'" +
                "}";

        var chkRememberMe = $("#chkRememberMe").attr('checked');

        $.ajax({
            type: "POST",
            url: "/member-area/services.asmx/MemberLogin",
            data: "{'mu': " + request + ", 'chkRememberMe': " + chkRememberMe + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d[0] == "") {
                    window.location.href = msg.d[1];
                }
                else {
                    $("#butLogin").children("span").show();
                    $("#butLogin").children("img").hide();
                    $("#dialog p").html(msg.d[0]);
                    $("#dialog").dialog('open');
                }
            },
            error: function (msg) {
                $("#butLogin").children("span").show();
                $("#butLogin").children("img").hide();
                $("#dialog p").html("There has been an error during the login process. Please check your internet connection or contact techinal support and reference error code ERR001.");
                $("#dialog").dialog('open');
            }
        });

        return false;
    });


    //Left/Right Always Visible.

    var offset = $(".floatingdiv").offset();
    var topPadding = 15;
    $(window).scroll(function () {
        if ($(window).scrollTop() > offset.top) {
            if ((($("#body").height() - $(".floatingdiv").height()) + $("#body").offset().top) > $(window).scrollTop()) {
                $(".floatingdiv").stop().animate({
                    marginTop: $(window).scrollTop() - offset.top + topPadding
                });
            }

        } else {
            $(".floatingdiv").stop().animate({
                marginTop: 0
            });
        };
    });


});
