﻿$(".recalculateTax").live('change', recalculateTax);

function recalculateTax() {
    var recalculateTaxZip = $(".recalculateTaxZip");
    var recalculateState = $(".recalculateTaxState");
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/webservices/client.asmx/recalculatetax",
        data: JSON.stringify({ zip: recalculateTaxZip.val(), idState: recalculateState.val() }),
        dataType: "json",
        success: function (data, textStatus, XMLHttpRequest) {
            if (data.d.IsValid) {
                $(".recalculateTaxTotal").html(data.d.TotalPriceText);
                $(".recalculateTaxTax").html(data.d.TaxText);
                $(".recalculateTaxGrandTotal").html(data.d.GrandTotalText);
            }
        }
    });
}

function resizeModals() {
    var modals = $(".mmui_dialog_content");
    var windowHeight = $(window).height();
    $.each(modals, function (indexInArray1, valueOfElementModal) {
        var modal = $(valueOfElementModal);
        if (modal.height()+100 > windowHeight) {
            modal.height(windowHeight-140);
            var parents = modal.parents(".mmui_dialog");
            $.each(parents, function (indexInArray2, valueOfElementParent) {
                var parent = $(valueOfElementParent);
                parent.css('left', (parent.width() / 2) * -1);
                parent.css('top', (parent.height() / 2) * -1);
            });
        }
    });
    setTimeout(function() { resizeModals(); }, 50);
}
resizeModals();

function updatingLinkButton(sender, value, disable) {
    if (value == null || value == "") {
        value = "Updating...";
    }
    $(sender).html(value);
    if (disable == true) {
        setTimeout(function () {
            $(sender).attr('disabled', 'disabled');
        }, 10);
    }
    return true;
}

function changeButton(sender, newButtonClass) {
    $(sender).hide();
    $("." + newButtonClass).show();
    return true;
}

function displaySaved(parentClientId) {
    var item = $("#" + parentClientId + " .changesSaved");
    item.fadeIn('slow', function () {
        setTimeout(function () { item.fadeOut('slow'); }, 2000);
    });
}

function zipKeyUp(sender, txtCityClientId, ddlStateClientId, e) {
    var unicode = e.keyCode ? e.keyCode : e.charCode
    if (unicode != 16 && unicode != 17 && unicode != 18 && unicode != 9) {
        var textbox = $("#" + sender.id);
        if (textbox.val().length >= 5) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/webservices/client.asmx/geocodezip",
                data: JSON.stringify({ zip: textbox.val() }),
                dataType: "json",
                success: function (data, textStatus, XMLHttpRequest) {
                    if (data.d.IsValid) {
                        if (txtCityClientId != null && txtCityClientId != "") {
                            $("#" + txtCityClientId).val(data.d.City);
                        }
                        if (ddlStateClientId != null && ddlStateClientId != "") {
                            var exists = $("#" + ddlStateClientId + " option[value=" + data.d.IdState + "]").length != 0;
                            if (exists) {
                                $("#" + ddlStateClientId).val(data.d.IdState);
                            }
                        }
                    }
                }
            });
        }
    }
}

function keepalive() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/webservices/client.asmx/keepalive",
        dataType: "json",
        success: function (data, textStatus, jqXHR) { },
        complete: function (jqXHR, textStatus) { },
        error: function (jqXHR, textStatus, errorThrown) { }
    });
}
setInterval(function () { keepalive(); }, 300000);


function centerModals() {
    setTimeout(function () {
        for (var i = 0; i < currentModals.length; i++) {
            var tempModal = currentModals[i];
            if (tempModal != null) {
                tempModal.dialog("option", "position", "center");
            }
        }
    }, 0);
}

function openModal(modalClass, udpClientId) {
    actionModal(modalClass, udpClientId, "open");
}

function closeModal(modalClass, udpClientId) {
    actionModal(modalClass, udpClientId, "close");
}

function actionModal(modalClass, udpClientId, mode) {
    var modals = $(modalClass);
    modals.each(function (index, elem) {
        var modal = $(elem);
        var udp = modal.find("#" + udpClientId);
        if (udp.length > 0) {
            modal.dialog(mode);
            currentModals[currentModals.length] = modal;
        }
    });
    centerModals();
}

function fixModal(sender) {
    $(sender).parent().appendTo("form");
}

function setupModal(className, title, width) {
    $(document).ready(function () {
        $(className).dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            closeText: "",
            modal: true,
            title: title,
            open: function (type, data) { fixModal(this); },
            width: width
        });
    });
}
