﻿var FloridaResidentTicketsPanel = Class.create(Panel, {
    initialize: function ($super, passportReference, tridionPanelId, panelPosition) {
        $super(passportReference, tridionPanelId);
        this.panelId = 'FloridaResidentTickets';
        this.panelPosition = panelPosition;
        this.strParams = [];
        this.classPrefix = 'frt';
    },
    doWork: function () {
        this.calendarInput = this.panelHTML.select('[name=dateOfArrival]')[0];
        this.adultInput = this.panelHTML.select('[name=inputAdultQuantity]')[0];
        this.childInput = this.panelHTML.select('[name=inputChildQuantity]')[0];

        if (this.calendarInput != undefined) this.__setupCalendar(this.calendarInput);
        if (this.adultInput != undefined) this.__setupInput(this.adultInput);
        if (this.childInput != undefined) this.__setupInput(this.childInput);

        var btwbd = this.panelHTML.select('.park1Table')[0];
        var btnbd = this.panelHTML.select('.park1TableNB')[0];
        var ptwbd = this.panelHTML.select('.park2Table')[0];
        var ptnbd = this.panelHTML.select('.park2TableNB')[0];

        if (btwbd != undefined) {
            this.dataPark1 = this.__filterDayTicketByParkAndBlockout(this.data.Lt, "1", "True");
            this.__generateRowsFor2Prices('dp1', 'productVariantCatalog', this.dataPark1, btwbd, this.adultInput, this.childInput);
        }

        if (btnbd != undefined) {
            this.dataPark3 = this.__filterDayTicketByParkAndBlockout(this.data.Lt, "1", "");
            this.__generateRowsFor2Prices('dp3', 'productVariantCatalog', this.dataPark3, btnbd, this.adultInput, this.childInput);
        }

        if (ptwbd != undefined) {
            this.dataPark2 = this.__filterDayTicketByParkAndBlockout(this.data.Lt, "2", "True");
            this.__generateRowsFor2Prices('dp2', 'productVariantCatalog', this.dataPark2, ptwbd, this.adultInput, this.childInput);
        }

        if (ptnbd != undefined) {
            this.dataPark4 = this.__filterDayTicketByParkAndBlockout(this.data.Lt, "2", "");
            this.__generateRowsFor2Prices('dp4', 'productVariantCatalog', this.dataPark4, ptnbd, this.adultInput, this.childInput);
        }

        var anp = this.panelHTML.select('.park2Table')[1];

        if (anp != undefined) {
            this.annualPassData = this.__filterAnnualPassData(this.data.Lap);
            this.__generateRowsFor2PricesWithCheck('dp5', 'productVariantCatalog', this.annualPassData, anp, this.adultInput, this.childInput);
        }

        this.__setupAddToCart();
        this.__loadValues(['text/dateOfArrival', 'text/inputAdultQuantity', 'text/inputChildQuantity', 'radio/productVariantCatalog', 'checkbox/subProduct']);
    },
    _calculateTotalPrices: function () {
        this.__generateTotalPrice2Prices('dp1', this.dataPark1, this.adultInput, this.childInput);
        this.__generateTotalPrice2Prices('dp2', this.dataPark2, this.adultInput, this.childInput);
        this.__generateTotalPrice2Prices('dp3', this.dataPark3, this.adultInput, this.childInput);
        this.__generateTotalPrice2Prices('dp4', this.dataPark4, this.adultInput, this.childInput);
        this.__generateTotalPrice2Prices('dp5', this.annualPassData, this.adultInput, this.childInput);
    },
    _formatDataAndSend: function (data) {
        var ppqc = data.productVariantCatalog.split('|');
        if (data['subProduct'] != undefined) {
            ppqc = data['subProduct'].split('|');
        }
        var dataFormatted;
        if (ppqc.size() == 8 && ppqc[6] != '' && ppqc[7] != '') {
            var totalQuantity = parseInt(data.inputAdultQuantity) + parseInt(data.inputChildQuantity);
            dataFormatted = {
                productID: ppqc[0] + "|" + ppqc[1] + "|" + ppqc[6],
                productVariantID: ppqc[2] + "|" + ppqc[3] + "|" + ppqc[7],
                qty: data.inputAdultQuantity + "|" + data.inputChildQuantity + "|" + totalQuantity,
                catalogName: ppqc[4] + "|" + ppqc[5] + "|" + ppqc[5]
            };
        }
        else {
            dataFormatted = {
                productID: ppqc[0] + "|" + ppqc[1],
                productVariantID: ppqc[2] + "|" + ppqc[3],
                qty: data.inputAdultQuantity + "|" + data.inputChildQuantity,
                catalogName: ppqc[4] + "|" + ppqc[5]
            };
        }

        this.passportReference.addToCart(dataFormatted);
    },
    _validateInputData: function (data) {
        var adultQuantity = jsHelper.toInt(data.inputAdultQuantity);
        var childQuantity = jsHelper.toInt(data.inputChildQuantity);
        return (data.productVariantCatalog != undefined && (adultQuantity > 0 || childQuantity > 0));
    }
});
