﻿var Sandryds;

if (typeof Sandryds === "undefined") {
    Sandryds = {};
}

Sandryds = function () {
    // OnInit
    function _init() {
    }

    return {
        // Returns alert on existing
        ClassExists: function () {
            alert('Sandryds class exists!');
        },
        // Sets empty labels to text inputs
        TextboxDefaultText: function () {
            $('.emptymessage:text').each(function () {
                this.value = $(this).attr('title'); // value for empty inputs
                $(this).addClass('text-label');

                // input on focus event
                $(this).focus(function () {
                    if (this.value == $(this).attr('title')) {
                        this.value = '';
                        $(this).removeClass('text-label');
                    }
                });

                // input on blur events
                $(this).blur(function () {
                    if (this.value == '') {
                        this.value = $(this).attr('title');
                        $(this).addClass('text-label');
                    }
                });
            });
        },
        // Open dialog
        OpenDialogBox: function (elementID) {
            $('#' + elementID).dialog('open');
        },
        // Close dialog
        CloseDialogBox: function (elementID) {
            $('#' + elementID).dialog('close');
        },
        ValidateCheckbox: function (oSrc, args) {
            args.IsValid = ($("#termscheckbox").attr('checked')) ? true : false;
        },
        HasBackorder: function () {
            var hasBackorder = false;
            $('.buyrow').each(function (e) {
                var quantity = $(this).find('.sizequantity').find('input[type=hidden]').val();

                quantity = parseInt(quantity);
                var orderQuantity = parseInt($(this).find('.backorder').val());
                if (isNaN(orderQuantity))
                    orderQuantity = 0;

                if (!hasBackorder)
                    hasBackorder = quantity < orderQuantity;
            });

            if (hasBackorder) {
                Sandryds.OpenDialogBox('dialog-backorder-form');
                return false;
            }

            return false;
        },
        ValidateFieldLength: function (source, args) {
            var isValid = false;
            var cntrlId = $(source).attr("controltovalidate");
            var max = parseInt($('#' + cntrlId).attr('data-max'));
            var validate = $('#' + cntrlId).val().length;
            if (validate != null && validate <= max) {
                $(source).siblings('.error').css('display', 'none');
                isValid = true;
            }
            if (isValid == false) {
                $(source).siblings('.error').css('display', 'block');
            }
            args.IsValid = isValid;
            Sandryds.SetCheckoutBoxesHeight();
        },
        SetCheckoutBoxesHeight: function () {
            $('.step2').removeAttr('style');
            $('.step3').removeAttr('style');
            $('.step4').removeAttr('style');
            var heightStep2 = $('.step2').height();
            var heightStep3 = $('.step3').height();
            var heightStep4 = $('.step4').height();

            var height = Math.max(heightStep2, heightStep3, heightStep4);
            $('.step2').height(height);
            $('.step3').height(height);
            $('.step4').height(height);
        }
    };
} ();

// Document Ready functions
$(document).ready(function () {
  

    // Load default text to textboxes
    if (typeof Sandryds != "undefined") {
        // Set empty labels to text inputs
        Sandryds.TextboxDefaultText();
    }

    if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(AjaxEnd);
        Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(AjaxBegin);
    }

    // Handling login form
    if ($("#dialog-login-form").is('*')) {
        $("#dialog-login-form").dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            width: 350,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }

    // Handling Buy terms
    if ($("#dialog-buyterms").is('*')) {
        $("#dialog-buyterms").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 600,
            height: 450,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }

    // Handling Backorder form
    if ($("#dialog-backorder-form").is('*')) {
        $("#dialog-backorder-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 600,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }

    // Handling Nextdelivery form
    if ($("#dialog-nextdelivery-form").is('*')) {
        $("#dialog-nextdelivery-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 600,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }

    // Handling Nextdelivery form
    if ($("#dialog-editdelivery-form").is('*')) {
        $("#dialog-editdelivery-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-editusers-form").is('*')) {
        $("#dialog-editusers-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-deletedelivery-form").is('*')) {
        $("#dialog-deletedelivery-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-addnewdelivery-form").is('*')) {
        $("#dialog-addnewdelivery-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-deleteusers-form").is('*')) {
        $("#dialog-deleteusers-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-addusers-form").is('*')) {
        $("#dialog-addusers-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-producttrading-form").is('*')) {
        $("#dialog-producttrading-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 430,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-productlogoinfo-form").is('*')) {
        $("#dialog-productlogoinfo-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }
    if ($("#dialog-ordersparcelnr-form").is('*')) {
        $("#dialog-ordersparcelnr-form").dialog({
            autoOpen: false,
            draggable: true,
            resizable: false,
            width: 350,
            minHeight: 150,
            maxHeight: 650,
            modal: true,
            open: function (type, data) {
                $(this).parent().appendTo("form");
            }
        });
    }

    // Enviroment images colorbox
    $("a[rel='enviroment']").colorbox({ rel: 'enviroment', transition: 'elastic', current: $('.snapshotstitle').attr('title'), maxWidth: 600, maxHeight: 600, speed: 300, iframe: false });
});

function AjaxEnd(sender, args) {
    if (args.get_error() != undefined && $('#ajaxerror').is('*')) {
        $('#preloader').addClass('error');
        $('#ajaxerror').show();
    }
    else {
        $('#ajax-preloaderbox').hide();
    }

    $('.preloader-button').each(function (e) {
        $(this).show();
    });
    $('.preloader-button-image').each(function (e) {
        $(this).hide();
    });
}

function AjaxBegin(sender, args) {
    if ($('#ajaxerror').is('*')) {
        $('#preloader').removeClass('error');
        $('#ajaxerror').hide();
    }
    $('#ajax-preloaderbox').show();

    $('.preloader-button').each(function (e) {
        $(this).hide();
    });

    $('.preloader-button-image').each(function (e) {
        $(this).show();
    });
}
