﻿function initVoucher() {
var scriptname;
    var extraRowClass;
    var isForm = false;
    if ($("#VoucherModule").length == 0) {
        alert("Geen VoucherModule-id gevonden");
    } else {
        scriptname = $("#VoucherModule").attr("scriptName");
        extraRowClass = $("#VoucherModule").attr("extraRowClass");
        isForm = ($("#VoucherModule").attr("extraRowClass") == "True");
    }
    
    var CurrentVoucher =$(".mandProdVoucherWorkValue").html()

    if (extraRowClass != '') {
        //Schakel de extra regel voor het zicht uit
        $("." + extraRowClass).each(function() {
            //Komt de tekst van de voucher voor dan mag de regel uit
            if (($(this).html().indexOf(CurrentVoucher) >= 0) && (!isForm) && ($(this).attr("id") != 'VoucherModule')) {
                $(this).hide();
            }
        });
    }
    
    
    initVoucherEnter();
    
    //Alleen initialiseren en uitvoeren als er een script is
    
    if (scriptname != '') {
        
        
        //Het inleveren van de voucher
        $(".usingVoucher").unbind("click");
        $(".usingVoucher").click(function() {
            var voucherCode = $("#voucherCode").val();
            if (voucherCode != '') {
            $.ajax({
                type: "POST",
                url: scriptname + "/vouchercalls",
                data: "{'Action':'getVoucher','voucherCode': '" + voucherCode + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var dataContainer 
                    if (response.d == null) {
                        dataContainer = response;
                    } else {
                        dataContainer = response.d
                    }
                    var data = (typeof dataContainer) == 'string' ? eval('(' + dataContainer + ')') : dataContainer;
                    $(".voucherError").hide();
                    if (data.VoucherValid == 'True') {
                        document.location = scriptname;
                    } else {
                        if (data.vName == '') {
                            $(".incorrectVoucher").show();
                        } else {
                            $(".invalidAmount").html('Het minimale bestelbedrag voor deze voucher bedraagt &euro; ' + data.MinPurchasePrice);
                            $(".invalidAmount").show();
                        }
                    }
                    initVoucherEnter();
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    document.location = scriptname;
                }
            });
                
            }
        });
        
        //Het verwijderen van een voucher
        $(".destroyCurrentVoucher").unbind("click");
        $(".destroyCurrentVoucher").click(function() {
            var script = $(this).attr("scriptname");
            $.ajax({
                type: "POST",
                url: scriptname + "/vouchercalls",
                data: "{'Action':'deleteVoucher','voucherCode': ''}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(response) {
                    var data = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
                    document.location = scriptname;
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    document.location = scriptname;
                }
            });
            return false;
        });
    }
    
    $("#voucherCode").unbind("keyup");
    
    if ($(".voucherContainer").length != 0) {
        $("#voucherPopupTrigger").fancybox({
            'centerOnScroll' : true
        });
        if ($(".voucherContainer").attr("voucherTrigger") == "True") {
            if ($(".voucherContainer").attr("voucherIsValid") == "True") {
                $("#voucherNotValidPopup").hide();
                $("#voucherAdded").show();
            } else {
                $("#voucherNotValidPopup").show();
                $("#voucherAdded").hide();
            }
            setTimeout(function () {$("#voucherPopupTrigger").click();},500)
        }
    }
}

$(function() {initVoucher()});

function initVoucherEnter() {
        $("#voucherCode").unbind("keydown");
        $("#voucherCode").keydown(function(event) {
            if (event.keyCode == '13' && $("#voucherCode").val() != '') {
                $(".usingVoucher").click();
            }
        });
}
