﻿function calculatePrice() {


}

function sendToPaymentSystem() {
    document.getElementById('processDialog').style.display = '';
    document.theForm.submit();
}



function remove_seat(eventid, seatid, seatcat, localid) {
    document.getElementById('waitDialog').style.display = '';
    var event = $('#airplane').attr('eventid');
    if (event == eventid) {
        var seat = $('#seat_' + seatid);
        var type = $(seat).attr('class');
        if (type == 'selected') {
            if ($(seat).attr('sc') == seatcat) {
                var sc = $(seat).attr('sc');
                $(seat).removeClass();
                $(seat).toggleClass('active');
                $.post("/sv-SE/Internal/RemoveSeat/" + seatid, "SeatCategory=" + seatcat, function (theResponse) {
                    document.getElementById('waitDialog').style.display = 'none';
                    $('#row_' + seatid).remove();
                    $('#seat_' + seatid).removeClass();
                    $('#seat_' + seatid).toggleClass('free');
                    this.blur();
                });
            }
            else {
                alert($(seat).attr('sc') + " != " + seatcat);
            }
        }
    }
    else {
        $.post("/sv-SE/Internal/RemoveSeat/" + seatid, "SeatCategory=" + seatcat + "&LocalID=" + localid + "&EventID=" + eventid, function (theResponse) {
            $('#row_' + seatid).remove();
            document.getElementById('waitDialog').style.display = 'none';
            this.blur();
        });
    }

}


function categoryChange(selectObj) {
    var idx = selectObj.selectedIndex;
    var tc = selectObj.options[idx].value;
    //alert("Seat : " + seat + " Category : " + tc);
    var seat = $(selectObj).attr('id').replace('S', '');
    var ev = $(selectObj).attr('ev');
    var sc = $(selectObj).attr('sc');
    var lo = $(selectObj).attr('lo');
    var input = "sc=" + sc + "&ev=" + ev + "&lo=" + lo + "&tc=" + tc;
    //alert(input);
    $.post("/Internal/ChangeCategory/" + seat + "/", "sc=" + sc + "&ev=" + ev + "&lo=" + lo + "&tc=" + tc, function (theResponse) {
        //alert(theResponse);
    });

}

$(document).ready(function () {
    //Selected performance in perfromancelist
    $.post("/sv-SE/DynamicContent/LeftMenu/", function (theResponse) {
        $("#leftcontent").html(theResponse);
    });

    $('#deliveryMethod').click(function () {
        var id = $(this).attr('value');

        var link = "/sv-SE/CreateOrder/PaymentMethods/" + id;
        if ($(this).is(':checked')) {
            $.post(link, function (theResponse) {
                $('.paymentlist').html(theResponse);
            });
        }
    });


    $('.showPerformanceButton').click(function () {
        var ev = $(this).attr('performance');
        $("#waitDialog").html("<h1>Vänta, hämtar information om tillgång på biljetter</h1>");
        $("#waitDialog").dialog("open");
        $.post("/sv-SE/Performance/Index/" + ev, function (theResponse) {
            $("#waitDialog").html(theResponse);
            $("#waitDialog").dialog({ buttons: {
                'Avbryt': function () {
                    $(this).dialog("close");
                }
            }
            });
        });
    });
    $('.showPerformanceButtonRedirect').click(function () {
        var ev = $(this).attr('performance');
        window.location = "/sv-SE/Performance/Index/" + ev + "?mode=kiosk";
    });

    $("#waitDialog").dialog("destroy");
    $("#waitDialog").dialog({
        autoOpen: false,
        height: 600,
        width: 800,
        modal: true,

        buttons: {
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });


    //OLD AF-Functions!
    $("#locals").change(function () {

        var tc = $(this).val();
        //alert(tc);
        //$.post("/Internal/GetEvents/" + tc + "/", "", function(theResponse) {
        //    $("#Event").html(theResponse);
        //});
        var div = $('#events');
        div.html('<div style="text-aling:center;"> <h1>Laddar...</h1></div>');


        window.location.replace("/Internal/Seatmap?LocalID=" + tc);

    });
    $(".txtsearch").keypress(function () {
        var inputValue = $(".txtsearch").val();
        var div = $('.searchResult');
        $.post("/sv-SE/Search/Result/" + inputValue, function (theResponse) {
            div.html(theResponse);
        });

    });
    $("#events").keypress(function () {
        var tc = $(this).val();
        var local = $("#locals").val();
        var div = $('#container');
        div.html('<div style="text-aling:center;"> <h1>Laddar...</h1></div>');


        window.location.replace("/Internal/Seatmap?EventID=" + tc + "&LocalID=" + local);
    });
    $(".ticketcategoryselect").change(function () {

        var tc = $(this).val();
        var seat = $(this).attr('id');
        //alert("Seat : " + seat + " Category : " + tc);
        var ev = $(this).attr('ev');
        var sc = $(this).attr('sc');
        var lo = $(this).attr('lo');
        //  int sc,int tc, int ev, int lo)
        //alert("Seat : " + seat + " Category : " + tc);
        var input = "sc=" + sc + "&ev=" + ev + "&lo=" + lo + "&tc=" + tc;
        //alert(input);
        $.post("/Internal/ChangeCategory/" + seat + "/", "sc=" + sc + "&ev=" + ev + "&lo=" + lo + "&tc=" + tc, function (theResponse) {
            //alert(theResponse);
        });

    });

    // Add click listener to seats
    $('#unnumbered a').click(function () {

        // Asign value of the link target
        var thisTarget = $(this).attr('href');
        document.getElementById('waitDialog').style.display = '';
        //$('.waitDialog').style.display = 'none';
        //alert('Hej!');
        var type = $(this).attr('class');
        var sc = $(this).attr('sc');
        var ev = $(this).attr('ev');
        $(this).removeClass();
        $(this).toggleClass('active');
        //alert("/Internal/AddSeat/" + thisTarget.substr(6) + "?sc=" + sc + "&ev=" + ev);
        $.post("/Internal/AddSeat/" + thisTarget.substr(6), "sc=" + sc + "&ev=" + ev, function (theResponse) {
            //alert(theResponse.toString());
            document.getElementById('waitDialog').style.display = 'none';
            if (theResponse.toString().substr(0, 1) == "0") {
                alert(theResponse.toString().substr(1));
                this.blur();
            }
            else if (theResponse.toString().substr(0, 1) == "1") {
                alert(theResponse.toString().substr(1));
                this.blur();
            }
            else {
                $('#tickets tr:last').after(theResponse.toString());
                this.blur();
            }
        });

        // Toggle selected class on/off

        this.blur();
        return false;
    }
		);

    $('#airplane a').click(function () {
        // Asign value of the link target
        var thisTarget = $(this).attr('href');

        var type = $(this).attr('class');
        if (type == 'selected') {
            var sc = $(this).attr('sc');
            $(this).removeClass();
            $(this).toggleClass('active');

            //document.getElementById('row_' + thisTarget.substr(6)).remove();
            $.post("/Internal/RemoveSeat/" + thisTarget.substr(6), "SeatCategory=" + sc, function (theResponse) {
                $('#row_' + thisTarget.substr(6)).remove();
                $('#seat_' + thisTarget.substr(6)).removeClass();
                $('#seat_' + thisTarget.substr(6)).toggleClass('free');
                this.blur();
                $(".ticketcategoryselect").change(function () {

                    var tc = $(this).val();
                    var seat = $(this).attr('id');
                    alert("Seat : " + seat + " Category : " + tc);
                    var ev = $(this).attr('ev');
                    var sc = $(this).attr('sc');
                    var lo = $(this).attr('lo');
                    //  int sc,int tc, int ev, int lo)
                    //alert("Seat : " + seat + " Category : " + tc);
                    var input = "sc=" + sc + "&ev=" + ev + "&lo=" + lo + "&tc=" + tc;
                    alert(input);
                    document.getElementById('waitDialog').style.display = '';
                    $.post("/Internal/ChangeCategory/" + seat + "/", "sc=" + sc + "&ev=" + ev + "&lo=" + lo + "&tc=" + tc, function (theResponse) {
                        document.getElementById('waitDialog').style.display = 'none';
                        //alert(theResponse);
                    });

                });
            });
            //alert('#row_' + thisTarget.substr(6));
        }
        else if (type == 'free') {
            var sc = $(this).attr('sc');
            $(this).removeClass();
            $(this).toggleClass('active');
            //alert("/Internal/AddSeat/" + thisTarget.substr(6) + "?sc=" + sc);
            document.getElementById('waitDialog').style.display = '';
            $.post("/Internal/AddSeat/" + thisTarget.substr(6), "sc=" + sc, function (theResponse) {
                //$('#row_' + thisTarget.substr(6)).remove();
                document.getElementById('waitDialog').style.display = 'none';
                if (theResponse.toString().substr(0, 1) == "0") {
                    alert(theResponse.toString().substr(1));
                    $('#seat_' + thisTarget.substr(6)).removeClass();
                    $('#seat_' + thisTarget.substr(6)).toggleClass('notavailable');
                    this.blur();
                }
                else if (theResponse.toString().substr(0, 1) == "1") {
                    alert(theResponse.toString().substr(1));
                    $('#seat_' + thisTarget.substr(6)).removeClass();
                    $('#seat_' + thisTarget.substr(6)).toggleClass('free');
                    this.blur();
                }
                else {
                    $('#tickets tr:last').after(theResponse.toString());
                    $('#seat_' + thisTarget.substr(6)).removeClass();
                    $('#seat_' + thisTarget.substr(6)).toggleClass('selected');
                    this.blur();
                }
            });

            // Show target tbody and hide others

            //$(thisTarget).addClass('selected');


            // Assign the value of the parent <li class="*">
        }

        // Toggle selected class on/off

        this.blur();
        return false;
    }
		);

    // Assign function to master checkbox
    $('#check_all').click(function () {
        if (this.checked === true) {
            // Add class="selected" to seat
            $('#airplane a, #passenger_details tbody tr').addClass('selected');
            $('#passenger_details input, #passenger_details select').removeAttr('disabled');
            this.blur();
        }
        else {
            // Remove class="selected" from seat
            $('#airplane a, #passenger_details tbody tr').removeClass('selected');
            $('#passenger_details input').attr('disabled', 'disabled').val('');
            $('#passenger_details select').each(function () {
                this.disabled = true;
                this.selectedIndex = 0;
            }
					);
            this.blur();
        }
    });
});

function toggle(div_id) {
    var el = document.getElementById(div_id);
    if (el.style.display == 'none') { el.style.display = 'block'; }
    else { el.style.display = 'none'; }
}
function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
    var popUpDiv = document.getElementById(popUpDivVar);
    popUpDiv_height = blanket_height / 2 - 150; //150 is half popup's height
    popUpDiv.style.top = popUpDiv_height + 'px';
}
function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
    var popUpDiv = document.getElementById(popUpDivVar);
    window_width = window_width / 2 - 150; //150 is half popup's width
    popUpDiv.style.left = window_width + 'px';
}
function popup(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);
}

